Index: trunk/phase3/resources/mediawiki.page/mediawiki.page.ajaxCategories.js |
— | — | @@ -4,11 +4,10 @@ |
5 | 5 | // Something like: "Category:Foo added. Reason" |
6 | 6 | // Requirement: Be able to get msg with lang option. |
7 | 7 | // * Handle uneditable cats. Needs serverside changes! |
8 | | -// * Add Hooks for change, delete, add |
9 | 8 | // * Add Hooks for soft redirect |
10 | 9 | // * Handle normal redirects |
11 | | -// * Simple / MultiEditMode |
12 | 10 | |
| 11 | + |
13 | 12 | ( function( $, mw ) { |
14 | 13 | |
15 | 14 | var ajaxCategories = function ( options ) { |
— | — | @@ -253,11 +252,15 @@ |
254 | 253 | newtext = fns[i]( newtext ); |
255 | 254 | } |
256 | 255 | return newtext; |
257 | | - } |
| 256 | + }; |
258 | 257 | var doneFn = function() { |
259 | 258 | //Remove saveAllButton |
260 | 259 | _saveAllButton.hide(); |
261 | | - |
| 260 | + |
| 261 | + // Clean stash |
| 262 | + _stash.fns = []; |
| 263 | + _stash.summaries = []; |
| 264 | + |
262 | 265 | // TODO |
263 | 266 | // Any link with $link.css('text-decoration', 'line-through'); |
264 | 267 | // needs to be removed |
— | — | @@ -400,8 +403,9 @@ |
401 | 404 | |
402 | 405 | _confirmEdit( |
403 | 406 | function( oldText ) { |
| 407 | + newText = _runHooks ( oldText, 'beforeDelete' ); |
404 | 408 | //TODO Cleanup whitespace safely? |
405 | | - var newText = oldText.replace( categoryRegex, '' ); |
| 409 | + var newText = newText.replace( categoryRegex, '' ); |
406 | 410 | |
407 | 411 | if ( newText == oldText ) { |
408 | 412 | var error = mw.msg( 'ajax-remove-category-error' ); |
— | — | @@ -438,7 +442,10 @@ |
439 | 443 | var summary = mw.msg( 'ajax-add-category-summary', category ); |
440 | 444 | |
441 | 445 | _confirmEdit( |
442 | | - function( oldText ) { return oldText + appendText }, |
| 446 | + function( oldText ) { |
| 447 | + newText = _runHooks ( oldText, 'beforeAdd' ); |
| 448 | + return newText + appendText; |
| 449 | + }, |
443 | 450 | summary, |
444 | 451 | function() { |
445 | 452 | _insertCatDOM( category, false ); |
— | — | @@ -469,7 +476,9 @@ |
470 | 477 | |
471 | 478 | _confirmEdit( |
472 | 479 | function( oldText ) { |
473 | | - var matches = oldText.match( categoryRegex ); |
| 480 | + newText = _runHooks ( oldText, 'beforeChange' ); |
| 481 | + |
| 482 | + var matches = newText.match( categoryRegex ); |
474 | 483 | |
475 | 484 | //Old cat wasn't found, likely to be transcluded |
476 | 485 | if ( !$.isArray( matches ) ) { |
— | — | @@ -616,13 +625,41 @@ |
617 | 626 | mw.msg( 'ajax-confirm-save-all' ) |
618 | 627 | ); |
619 | 628 | _saveAllButton.click( _handleStashedCategories ).hide(); |
620 | | - $containerNormal.append( _saveAllButton ) |
| 629 | + $containerNormal.append( _saveAllButton ); |
621 | 630 | }; |
622 | 631 | |
623 | 632 | _stash = { |
624 | 633 | summaries : [], |
625 | 634 | fns : [] |
626 | 635 | }; |
| 636 | + _hooks = { |
| 637 | + beforeAdd : [], |
| 638 | + beforeChange : [], |
| 639 | + beforeDelete : [] |
| 640 | + }; |
| 641 | + _runHooks = function( oldtext, type ) { |
| 642 | + // No hooks registered |
| 643 | + if ( !_hooks[type] ) { |
| 644 | + return oldtext; |
| 645 | + } else { |
| 646 | + for (var i = 0; i < _hooks[type].length; i++) { |
| 647 | + oldtext = _hooks[type][i]( oldtext ); |
| 648 | + } |
| 649 | + return oldtext; |
| 650 | + } |
| 651 | + }; |
| 652 | + /** |
| 653 | + * Add hooks |
| 654 | + * Currently available: beforeAdd, beforeChange, beforeDelete |
| 655 | + * |
| 656 | + * @param string type Type of hook to add |
| 657 | + * @param function fn Hook function. This function is the old text passed |
| 658 | + * and it needs to return the modified text |
| 659 | + */ |
| 660 | + this.addHook = function( type, fn ) { |
| 661 | + if ( !_hooks[type] ) return; |
| 662 | + else hooks[type].push( fn ); |
| 663 | + }; |
627 | 664 | }; |
628 | 665 | // Now make a new version |
629 | 666 | mw.ajaxCategories = new ajaxCategories(); |