mediawiki.page.ajax.watch: Clean up and simplify code
authorTimo Tijhof <krinklemail@gmail.com>
Fri, 14 Feb 2014 17:39:26 +0000 (09:39 -0800)
committerKrinkle <krinklemail@gmail.com>
Tue, 4 Mar 2014 23:56:52 +0000 (23:56 +0000)
Change-Id: Ie66d08b84a39ad260866de6e0ff821e356133ad0

resources/mediawiki.page/mediawiki.page.watch.ajax.js

index 9489403..a491c6a 100644 (file)
@@ -72,7 +72,7 @@
                actionPaths = mw.config.get( 'wgActionPaths' );
 
                // TODO: Does MediaWiki give action path or query param
-               // precedence ? If the former, move this to the bottom
+               // precedence? If the former, move this to the bottom
                action = mw.util.getParamValue( 'action', url );
                if ( action !== null ) {
                        return action;
@@ -81,7 +81,7 @@
                for ( key in actionPaths ) {
                        if ( actionPaths.hasOwnProperty( key ) ) {
                                parts = actionPaths[key].split( '$1' );
-                               for ( i = 0; i < parts.length; i += 1 ) {
+                               for ( i = 0; i < parts.length; i++ ) {
                                        parts[i] = $.escapeRE( parts[i] );
                                }
                                m = new RegExp( parts.join( '(.+)' ) ).exec( url );
                        updateWatchLink( $link, action, 'loading' );
 
                        api = new mw.Api();
-                       api[action]( title )
-                       .done( function ( watchResponse ) {
-                               var otherAction;
 
-                               otherAction = action === 'watch' ? 'unwatch' : 'watch';
+                       api[action]( title )
+                               .done( function ( watchResponse ) {
+                                       var otherAction = action === 'watch' ? 'unwatch' : 'watch';
 
-                               mw.notify( $.parseHTML( watchResponse.message ), {
-                                       tag: 'watch-self'
-                               } );
+                                       mw.notify( $.parseHTML( watchResponse.message ), {
+                                               tag: 'watch-self'
+                                       } );
 
-                               // Set link to opposite
-                               updateWatchLink( $link, otherAction );
+                                       // Set link to opposite
+                                       updateWatchLink( $link, otherAction );
 
-                               // Bug 12395 - update the watch checkbox on edit pages when the
-                               // page is watched or unwatched via the tab.
-                               if ( watchResponse.watched !== undefined ) {
-                                       $( '#wpWatchthis' ).prop( 'checked', true );
-                               } else {
-                                       $( '#wpWatchthis' ).prop( 'checked', false );
-                               }
-                       } )
-                       .fail( function () {
-                               var cleanTitle, msg, link;
-
-                               // Reset link to non-loading mode
-                               updateWatchLink( $link, action );
-
-                               // Format error message
-                               cleanTitle = title.replace( /_/g, ' ' );
-                               link = mw.html.element(
-                                       'a', {
-                                               href: mw.util.getUrl( title ),
-                                               title: cleanTitle
-                                       }, cleanTitle
-                               );
-                               msg = mw.message( 'watcherrortext', link );
-
-                               // Report to user about the error
-                               mw.notify( msg, { tag: 'watch-self' } );
-                       } );
+                                       // Update the "Watch this page" checkbox on action=edit when the
+                                       // page is watched or unwatched via the tab (bug 12395).
+                                       $( '#wpWatchthis' ).prop( 'checked', watchResponse.watched !== undefined );
+                               } )
+                               .fail( function () {
+                                       var cleanTitle, msg, link;
+
+                                       // Reset link to non-loading mode
+                                       updateWatchLink( $link, action );
+
+                                       // Format error message
+                                       cleanTitle = title.replace( /_/g, ' ' );
+                                       link = mw.html.element(
+                                               'a', {
+                                                       href: mw.util.getUrl( title ),
+                                                       title: cleanTitle
+                                               }, cleanTitle
+                                       );
+                                       msg = mw.message( 'watcherrortext', link );
+
+                                       // Report to user about the error
+                                       mw.notify( msg, { tag: 'watch-self' } );
+                               } );
                } );
        } );