r92166 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r92165‎ | r92166 | r92167 >
Date:17:14, 14 July 2011
Author:diebuche
Status:deferred (Comments)
Tags:
Comment:
AjaxCategories: Add basic hook functionality. Can be used by sites for stuff like {{uncategorized}} templates etc.
Modified paths:
  • /trunk/phase3/resources/mediawiki.page/mediawiki.page.ajaxCategories.js (modified) (history)

Diff [purge]

Index: trunk/phase3/resources/mediawiki.page/mediawiki.page.ajaxCategories.js
@@ -4,11 +4,10 @@
55 // Something like: "Category:Foo added. Reason"
66 // Requirement: Be able to get msg with lang option.
77 // * Handle uneditable cats. Needs serverside changes!
8 -// * Add Hooks for change, delete, add
98 // * Add Hooks for soft redirect
109 // * Handle normal redirects
11 -// * Simple / MultiEditMode
1210
 11+
1312 ( function( $, mw ) {
1413
1514 var ajaxCategories = function ( options ) {
@@ -253,11 +252,15 @@
254253 newtext = fns[i]( newtext );
255254 }
256255 return newtext;
257 - }
 256+ };
258257 var doneFn = function() {
259258 //Remove saveAllButton
260259 _saveAllButton.hide();
261 -
 260+
 261+ // Clean stash
 262+ _stash.fns = [];
 263+ _stash.summaries = [];
 264+
262265 // TODO
263266 // Any link with $link.css('text-decoration', 'line-through');
264267 // needs to be removed
@@ -400,8 +403,9 @@
401404
402405 _confirmEdit(
403406 function( oldText ) {
 407+ newText = _runHooks ( oldText, 'beforeDelete' );
404408 //TODO Cleanup whitespace safely?
405 - var newText = oldText.replace( categoryRegex, '' );
 409+ var newText = newText.replace( categoryRegex, '' );
406410
407411 if ( newText == oldText ) {
408412 var error = mw.msg( 'ajax-remove-category-error' );
@@ -438,7 +442,10 @@
439443 var summary = mw.msg( 'ajax-add-category-summary', category );
440444
441445 _confirmEdit(
442 - function( oldText ) { return oldText + appendText },
 446+ function( oldText ) {
 447+ newText = _runHooks ( oldText, 'beforeAdd' );
 448+ return newText + appendText;
 449+ },
443450 summary,
444451 function() {
445452 _insertCatDOM( category, false );
@@ -469,7 +476,9 @@
470477
471478 _confirmEdit(
472479 function( oldText ) {
473 - var matches = oldText.match( categoryRegex );
 480+ newText = _runHooks ( oldText, 'beforeChange' );
 481+
 482+ var matches = newText.match( categoryRegex );
474483
475484 //Old cat wasn't found, likely to be transcluded
476485 if ( !$.isArray( matches ) ) {
@@ -616,13 +625,41 @@
617626 mw.msg( 'ajax-confirm-save-all' )
618627 );
619628 _saveAllButton.click( _handleStashedCategories ).hide();
620 - $containerNormal.append( _saveAllButton )
 629+ $containerNormal.append( _saveAllButton );
621630 };
622631
623632 _stash = {
624633 summaries : [],
625634 fns : []
626635 };
 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+ };
627664 };
628665 // Now make a new version
629666 mw.ajaxCategories = new ajaxCategories();

Comments

#Comment by Krinkle (talk | contribs)   20:55, 3 October 2011

AjaxCategories moved out of core. Marking deferred for now.

Status & tagging log