r92266 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r92265‎ | r92266 | r92267 >
Date:18:21, 15 July 2011
Author:diebuche
Status:deferred (Comments)
Tags:
Comment:
AjaxCategories: Making MultiEdit mores stable and getting rid of other bugs
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,26 +4,24 @@
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 soft redirect
98 // * Handle normal redirects
10 -
11 -// * Fixme on narrow windows
129 // * Enter to submit
1310
1411 ( function( $, mw ) {
1512
1613 var ajaxCategories = function ( options ) {
 14+ //Save scope in shortcut
 15+ var aC = this;
 16+
1717 // TODO grab these out of option object.
1818
1919 var catLinkWrapper = '<li/>';
2020 var $container = $( '.catlinks' );
2121 var $containerNormal = $( '#mw-normal-catlinks' );
2222
23 - var categoryLinkSelector = '#mw-normal-catlinks li a';
 23+ var categoryLinkSelector = '#mw-normal-catlinks li a:not(.icon)';
2424 var _request;
2525
26 - var _catElements = {};
27 -
2826 var namespaceIds = mw.config.get( 'wgNamespaceIds' );
2927 var categoryNamespaceId = namespaceIds['category'];
3028 var categoryNamespace = mw.config.get( 'wgFormattedNamespaces' )[categoryNamespaceId];
@@ -75,9 +73,9 @@
7674 * Insert a newly added category into the DOM
7775 *
7876 * @param string category name.
79 - * @param boolean isHidden (unused)
 77+ * @return jQuery object
8078 */
81 - _insertCatDOM = function ( cat, isHidden ) {
 79+ _createCatDOM = function ( cat ) {
8280 // User can implicitely state a sort key.
8381 // Remove before display
8482 cat = cat.replace(/\|.*/, '');
@@ -85,7 +83,7 @@
8684 // strip out bad characters
8785 cat = _stripIllegals ( cat );
8886
89 - if ( $.isEmpty( cat ) || this.containsCat( cat ) ) {
 87+ if ( $.isEmpty( cat ) || aC.containsCat( cat ) ) {
9088 return;
9189 }
9290
@@ -93,12 +91,10 @@
9492 var $anchor = $( '<a/>' ).append( cat );
9593 $catLinkWrapper.append( $anchor );
9694 $anchor.attr( { target: "_blank", href: _catLink( cat ) } );
97 - if ( isHidden ) {
98 - $container.find( '#mw-hidden-catlinks ul' ).append( $catLinkWrapper );
99 - } else {
100 - $container.find( '#mw-normal-catlinks ul' ).append( $catLinkWrapper );
101 - }
 95+
10296 _createCatButtons( $anchor.get(0) );
 97+
 98+ return $anchor;
10399 };
104100
105101 _makeSuggestionBox = function ( prefill, callback, buttonVal ) {
@@ -162,7 +158,7 @@
163159 };
164160
165161 /**
166 - * This get's called by all action buttons
 162+ * This gets called by all action buttons
167163 * Displays a dialog to confirm the action
168164 * Afterwords do the actual edit
169165 *
@@ -171,13 +167,15 @@
172168 * @param function fn doneFn callback after everything is done
173169 * @return boolean True for exists
174170 */
175 - _confirmEdit = function ( fn, actionSummary, doneFn, all ) {
 171+ _confirmEdit = function ( fn, actionSummary, doneFn, $link, action ) {
176172 // Check whether to use multiEdit mode
177 - if ( _multiEdit && !all ) {
 173+ if ( _multiEdit && action != 'all' ) {
178174 // Stash away
 175+ $link.data('stashIndex', _stash.fns.length );
179176 _stash.summaries.push( actionSummary );
180177 _stash.fns.push( fn );
181178
 179+
182180 //TODO add Cancel button
183181 _saveAllButton.show();
184182 //_cancelAllButton.show();
@@ -249,19 +247,25 @@
250248 // Save fns
251249 fns = _stash.fns;
252250
253 - // RTL?
254 - var summary = _stash.summaries.join('. ');
 251+ //TODO Add spaces in msg
 252+ var summary = _stash.summaries.join();
 253+ if ( summary == '' ) {
 254+ _saveAllButton.hide();
 255+ return;
 256+ }
255257 var combinedFn = function( oldtext ) {
256258 // Run the text through all action functions
257259 newtext = oldtext;
258260 for ( var i = 0; i < fns.length; i++ ) {
259 - newtext = fns[i]( newtext );
 261+ if ( $.isFunction ( fns[i] ) ) {
 262+ newtext = fns[i]( newtext );
 263+ }
260264 }
261265 return newtext;
262266 };
263267 var doneFn = _resetToActual;
264268
265 - _confirmEdit( combinedFn, summary, doneFn, true );
 269+ _confirmEdit( combinedFn, summary, doneFn, '', 'all' );
266270 };
267271
268272 _resetToActual = function() {
@@ -390,11 +394,11 @@
391395 _multiEdit ? mw.msg( 'ajax-confirm-ok' ) : mw.msg( 'ajax-confirm-save' )
392396 );
393397 $link.after( $input ).hide();
394 - _catElements[category].editButton.hide();
395 - _catElements[category].deleteButton.unbind('click').click( function() {
 398+ $link.data('editButton').hide();
 399+ $link.data('deleteButton').unbind('click').click( function() {
396400 $input.remove();
397401 $link.show();
398 - _catElements[category].editButton.show();
 402+ $link.data('editButton').show();
399403 $( this ).unbind('click').click( _handleDeleteLink );
400404 });
401405 };
@@ -406,12 +410,18 @@
407411 };
408412
409413 _handleDeleteLink = function ( e ) {
410 - e.preventDefault();
411 -
412414 var $this = $( this );
413415 var $link = $this.parent().find( 'a:not(.icon)' );
414416 var category = $link.text();
415417
 418+ if ( $link.hasClass('mw-added-category') ) {
 419+ // We're just cancelling the addition
 420+ _removeStashItem ( $link );
 421+
 422+ $link.parent().remove();
 423+ return;
 424+ }
 425+
416426 var categoryRegex = _buildRegex( category );
417427
418428 var summary = mw.msg( 'ajax-remove-category-summary', category );
@@ -436,20 +446,26 @@
437447 function( unsaved ) {
438448 if ( unsaved ) {
439449 //TODO Make revertable
440 - $link.addClass('.mw-removed-category');
 450+ $link.addClass('mw-removed-category');
441451 } else {
442452 $this.parent().remove();
443453 }
444 - }
 454+ },
 455+ $link,
 456+ 'delete'
445457 );
446458 };
447459
448460 _handleCategoryAdd = function ( e ) {
 461+ var $this = $( this );
 462+
449463 // Grab category text
450 - var category = $( this ).parent().find( '.mw-addcategory-input' ).val();
 464+ var category = $this.parent().find( '.mw-addcategory-input' ).val();
451465 category = $.ucFirst( category );
452466
453 - if ( this.containsCat(category) ) {
 467+ var $link = _createCatDOM( category );
 468+
 469+ if ( aC.containsCat(category) ) {
454470 _showError( mw.msg( 'ajax-category-already-present', category ) );
455471 return;
456472 }
@@ -462,28 +478,41 @@
463479 return newText + appendText;
464480 },
465481 summary,
466 - function() {
 482+ function( unsaved ) {
467483 $container.find( '#mw-normal-catlinks>.mw-addcategory-prompt' ).toggle();
468 - _insertCatDOM( category, false );
469 - }
 484+ $container.find( '#mw-normal-catlinks ul' ).append( $link.parent() );
 485+ if ( unsaved ) {
 486+ $link.addClass( 'mw-added-category' );
 487+ }
 488+ },
 489+ $link,
 490+ 'add'
470491 );
471492 };
472493
473494 _handleCategoryEdit = function ( e ) {
474 - //FIXME: in MultiEdit Mode handle successive edits to same category
475 - e.preventDefault();
 495+ var $this = $( this );
476496
477497 // Grab category text
478 - var categoryNew = $( this ).parent().find( '.mw-addcategory-input' ).val();
 498+ var categoryNew = $this.parent().find( '.mw-addcategory-input' ).val();
479499 categoryNew = $.ucFirst( categoryNew );
480500
481 - var $this = $( this );
482501 var $link = $this.parent().parent().find( 'a:not(.icon)' );
483 - var category = $link.text();
 502+ if ( $link.hasClass('mw-removed-category') ) {
 503+ _removeStashItem ( $link );
 504+ $link.removeClass( 'mw-removed-category' );
 505+ }
 506+ if ( $link.data( 'origCat' ) ) {
 507+ var category = $link.data( 'origCat' );
 508+ _removeStashItem ( $link );
 509+ } else {
 510+ var category = $link.text();
 511+ }
 512+
484513
485514 // User didn't change anything.
486515 if ( category == categoryNew ) {
487 - _catElements[category].deleteButton.click();
 516+ $link.data('deleteButton').click();
488517 return;
489518 }
490519 categoryRegex = _buildRegex( category );
@@ -519,14 +548,18 @@
520549 return newText;
521550 },
522551 summary,
523 - function() {
 552+ function( unsaved ) {
524553 // Remove input box & button
525 - _catElements[category].deleteButton.click();
526 - _catElements[categoryNew] = _catElements[category];
527 - delete _catElements[category];
 554+ $link.data('deleteButton').click();
 555+
528556 // Update link text and href
529557 $link.show().text( categoryNew ).attr( 'href', _catLink( categoryNew ) );
530 - }
 558+ if ( unsaved ) {
 559+ $link.data( 'origCat', category ).addClass( 'mw-changed-category' );
 560+ }
 561+ },
 562+ $link,
 563+ 'edit'
531564 );
532565 };
533566
@@ -596,13 +629,11 @@
597630 $( element ).after( deleteButton ).after( editButton );
598631
599632 //Save references to all links and buttons
600 - _catElements[$( element ).text()] = {
601 - link : $( element ),
602 - parent : $( element ).parent(),
 633+ $( element ).data({
603634 saveButton : saveButton,
604635 deleteButton: deleteButton,
605636 editButton : editButton
606 - };
 637+ });
607638 };
608639 this.setup = function () {
609640 // Could be set by gadgets like HotCat etc.
@@ -615,6 +646,10 @@
616647 // Unhide hidden category holders.
617648 $('#mw-hidden-catlinks').show();
618649
 650+ var $li = $('<li>', {
 651+ 'class' : 'mw-ajax-addcategory-holder'
 652+ });
 653+ // $containerNormal.find('ul').append( $li)
619654 // Create [Add Category] link
620655 var addLink = _createButton('icon-add',
621656 mw.msg( 'ajax-add-category' ),
@@ -629,7 +664,7 @@
630665 promptContainer.hide();
631666
632667 // Create edit & delete link for each category.
633 - $( '#catlinks li a' ).each( function( e ) {
 668+ $( '#catlinks li a' ).each( function() {
634669 _createCatButtons( this );
635670 });
636671
@@ -648,7 +683,7 @@
649684 );
650685 _saveAllButton.click( _handleStashedCategories ).hide();
651686 _cancelAllButton.hide();
652 -
 687+ //TODO wrap in div display:inline-block
653688 $containerNormal.append( _saveAllButton ).append( _cancelAllButton );
654689 };
655690
@@ -656,6 +691,11 @@
657692 summaries : [],
658693 fns : []
659694 };
 695+ _removeStashItem = function ( $link ) {
 696+ var i = $link.data( 'stashIndex' );
 697+ delete _stash.fns[i];
 698+ delete _stash.summaries[i];
 699+ }
660700 _hooks = {
661701 beforeAdd : [],
662702 beforeChange : [],

Comments

#Comment by Krinkle (talk | contribs)   12:49, 11 August 2011

Marking deferred. Most of this has been rewritten by you and me. Review by Roan as a whole.

Status & tagging log