Three small fixes to mediawiki.specials.preferences.js
authorKrinkle <krinkle@users.mediawiki.org>
Sun, 6 Feb 2011 03:31:32 +0000 (03:31 +0000)
committerKrinkle <krinkle@users.mediawiki.org>
Sun, 6 Feb 2011 03:31:32 +0000 (03:31 +0000)
* Moving class-adder from mousedown() to click().

Mousedown also fires when opening the contextmenu (right-click). If I right-clicked, say, preftab-4 to open in a new tab, it would work (thanks to r81573), but it would also trigger the 'selected'-class on that tab (which is wrong and out-of-sync since click() did not and should not fire). This behaviour was the case in Safari 5/Firefox 3 (Mac) and Chrome (Windows). Moving it to click() should not have any affect on the behaviour in other browsers.
Test it at: http://api.jquery.com/mousedown/

* Wrapping the hash-jump in a document.ready function.

This module is enclosed in a self-executing anonymous function to enforce private scope passing mediaWiki and jQuery. Although that, (<code>(function($,mw){ /* ... */ })(jQuery,mediaWiki);</code>) may look like a shortcut for jQuery(document).ready(), it is not. (Because both <code>;</code> and <code>jQuery(function{ /* ... */ });</code> look alike and are shortcuts). Anyway, wrapping it now to avoid it jumping too early.

* Using second argument of .each(), which is a reference to 'this'

By using that (named) variable instead there's no confusion with the other 'this' inside the click event binder – which refers to the then-clicked element (the anchor tag) rather than the legend-element.

resources/mediawiki.special/mediawiki.special.preferences.js

index a6988bc..7fbd6d5 100644 (file)
@@ -12,24 +12,22 @@ $( '#preferences' )
                .addClass( 'prefsection' )
                .children( 'legend' )
                        .addClass( 'mainLegend' )
-                       .each( function( i ) {
-                                       $(this).parent().attr( 'id', 'prefsection-' + i );
+                       .each( function( i, legend ) {
+                                       $(legend).parent().attr( 'id', 'prefsection-' + i );
                                        if ( i === 0 ) {
-                                               $(this).parent().show();
+                                               $(legend).parent().show();
                                        }
                                        $( '#preftoc' ).append(
                                                $( '<li></li>' )
                                                        .addClass( i === 0 ? 'selected' : null )
                                                        .append(
                                                                $( '<a></a>')
-                                                                       .text( $(this).text() )
+                                                                       .text( $(legend).text() )
                                                                        .attr( 'id', 'preftab-' + i + '-tab' )
                                                                        .attr( 'href', '#preftab-' + i ) // Use #preftab-N instead of #prefsection-N to avoid jumping on click
-                                                                       .mousedown( function( e ) {
+                                                                       .click( function() {
                                                                                $(this).parent().parent().find( 'li' ).removeClass( 'selected' );
-                                                                               $(this).parent().addClass( 'selected' );
-                                                                       } )
-                                                                       .click( function( e ) {
+                                                                               $(this).parent().addClass( 'selected' )
                                                                                $( '#preferences > fieldset' ).hide();
                                                                                $( '#prefsection-' + i ).show();
                                                                        } )
@@ -40,11 +38,14 @@ $( '#preferences' )
 
 // If we've reloaded the page or followed an open-in-new-window,
 // make the selected tab visible.
-var hash = window.location.hash;
-if( hash.match( /^#preftab-[\d]+$/ ) ) {
-       var tab = $( hash + '-tab' );
-       tab.mousedown().click();
-}
+// On document ready:
+$( function() {
+       var hash = window.location.hash;
+       if( hash.match( /^#preftab-[\d]+$/ ) ) {
+               var $tab = $( hash + '-tab' );
+               $tab.click();
+       }
+} );
 
 /**
  * Given an email validity status (true, false, null) update the label CSS class