Disable ns selector checkboxes when 'all' namespace is selected
authorAntoine Musso <hashar@users.mediawiki.org>
Tue, 28 Jun 2011 06:40:49 +0000 (06:40 +0000)
committerAntoine Musso <hashar@users.mediawiki.org>
Tue, 28 Jun 2011 06:40:49 +0000 (06:40 +0000)
* based on an idea by Aaron on r90866
* comes with QUnit test
* expect the special 'all' namespace to be the first in the list
* function build on mediawiki.special form r90941

includes/Xml.php
includes/specials/SpecialRecentchanges.php
resources/Resources.php
resources/mediawiki.special/mediawiki.special.recentchanges.js [new file with mode: 0644]
tests/qunit/index.html
tests/qunit/suites/resources/mediawiki.special/mediawiki.special.recentchanges.js [new file with mode: 0644]

index a100a8b..1e8696b 100644 (file)
@@ -132,6 +132,8 @@ class Xml {
                }
 
                if( !is_null( $all ) )
+                       # Please make sure the 'namespacesall' is the first or you will break
+                       # such an assumption (ex js: mw.special.recentchanges.updateCheckboxes)
                        $namespaces = array( $all => wfMsg( 'namespacesall' ) ) + $namespaces;
                foreach( $namespaces as $index => $name ) {
                        if( $index < NS_MAIN ) {
index da5d5f0..da7babe 100644 (file)
@@ -140,6 +140,7 @@ class SpecialRecentChanges extends IncludableSpecialPage {
                $opts = $this->getOptions();
                $this->setHeaders();
                $this->outputHeader();
+               $this->addRecentChangesJS();
 
                // Fetch results, prepare a batch link existence check query
                $conds = $this->buildMainQueryConds( $opts );
@@ -839,4 +840,14 @@ class SpecialRecentChanges extends IncludableSpecialPage {
                $rclistfrom = wfMsgExt( 'rclistfrom', array( 'parseinline', 'replaceafter' ), $tl );
                return "{$note}$rclinks<br />$rclistfrom";
        }
+
+       /**
+        * add javascript specific to the [[Special:RecentChanges]] page
+        */
+       function addRecentChangesJS() {
+               global $wgOut;
+               $wgOut->addModules( array(
+                       'mediawiki.special.recentchanges',
+               ) );
+       }
 }
index 38965c5..c942e28 100644 (file)
@@ -534,6 +534,13 @@ return array(
                'scripts' => 'resources/mediawiki.special/mediawiki.special.movePage.js',
                'dependencies' => 'jquery.byteLimit',
        ),
+       'mediawiki.special' => array(
+               'scripts' => 'resources/mediawiki/mediawiki.special.js',
+       ),
+       'mediawiki.special.recentchanges' => array(
+               'scripts' => 'resources/mediawiki.special/mediawiki.special.recentchanges.js',
+               'dependencies' => array( 'mediawiki.special' ),
+       ),
        'mediawiki.special.upload' => array(
                // @TODO: merge in remainder of mediawiki.legacy.upload
                'scripts' => 'resources/mediawiki.special/mediawiki.special.upload.js',
diff --git a/resources/mediawiki.special/mediawiki.special.recentchanges.js b/resources/mediawiki.special/mediawiki.special.recentchanges.js
new file mode 100644 (file)
index 0000000..1006a9d
--- /dev/null
@@ -0,0 +1,38 @@
+/* JavaScript for Special:RecentChanges */
+( function( $, mw ) {
+
+mw.special.recentchanges = {
+       // -- Variables
+       'select' : false,
+       'checkboxes' : [ 'nsassociated', 'nsinvert' ],
+
+       // -- Methods
+       'init' : function() {
+               this.select = $( 'select#namespace' );
+
+               // Register an onChange trigger for the <select> element
+               this.select.change( function() {
+                       mw.special.recentchanges.updateCheckboxes();
+               });
+               // on load, trigger the event to eventually update checkboxes statuses
+               this.select.change();
+       },
+       
+       /**
+        * handler to disable/enable the namespace selector checkboxes when the
+        * special 'all' namespace is selected/unselected respectively.
+        */
+       'updateCheckboxes' : function() {
+               // The 'all' namespace is the FIRST in the list.
+               var isAllNS = this.select.find( 'option' ).first().is( ':selected' );
+
+               // Iterates over checkboxes and propagate the selected option
+               $.map( this.checkboxes, function(id) {
+                       $( 'input#'+id ).attr( 'disabled', isAllNS );
+               });
+       },
+};
+
+mw.special.recentchanges.init();
+
+}(jQuery, mediaWiki ) );
index 828fc08..192f7eb 100644 (file)
@@ -44,6 +44,8 @@
        <script src="../../resources/jquery/jquery.tabIndex.js"></script>
        <script src="../../resources/jquery/jquery.tablesorter.js"></script>
        <script src="../../resources/mediawiki/mediawiki.Title.js"></script>
+       <script src="../../resources/mediawiki/mediawiki.special.js"></script>
+       <script src="../../resources/mediawiki.special/mediawiki.special.recentchanges.js"></script>
 
        <!-- QUnit: Load framework -->
        <link rel="stylesheet" href="../../resources/jquery/jquery.qunit.css" />
@@ -62,6 +64,7 @@
        <script src="suites/resources/jquery/jquery.tabIndex.js"></script>
        <script src="suites/resources/jquery/jquery.tablesorter.test.js" charset="UTF-8"></script>
        <script src="suites/resources/mediawiki/mediawiki.Title.js"></script>
+       <script src="suites/resources/mediawiki.special/mediawiki.special.recentchanges.js"></script>
 
        <!-- TestSwarm: If a test swarm is running this,
             the following script will allow it to extract the results.
diff --git a/tests/qunit/suites/resources/mediawiki.special/mediawiki.special.recentchanges.js b/tests/qunit/suites/resources/mediawiki.special/mediawiki.special.recentchanges.js
new file mode 100644 (file)
index 0000000..7175121
--- /dev/null
@@ -0,0 +1,67 @@
+module( 'mediawiki.special.preferences.js' );
+
+test( '-- Initial check', function() {
+       expect( 2 );
+       ok( mediaWiki.special.recentchanges.init,
+          'mediaWiki.special.recentchanges.init defined'
+         );
+       ok( mediaWiki.special.recentchanges.updateCheckboxes,
+          'mediaWiki.special.recentchanges.updateCheckboxes defined'
+         );
+       // TODO: verify checkboxes == [ 'nsassociated', 'nsinvert' ]
+});
+
+test( 'foobar', function() {
+
+       // from Special:Recentchanges
+       var select =
+       '<select id="namespace" name="namespace" class="namespaceselector">'
+       + '<option value="" selected="selected">all</option>'
+       + '<option value="0">(Main)</option>'
+       + '<option value="1">Talk</option>'
+       + '<option value="2">User</option>'
+       + '<option value="3">User talk</option>'
+       + '<option value="4">ProjectName</option>'
+       + '<option value="5">ProjectName talk</option>'
+       + '</select>'
+       + '<input name="invert" type="checkbox" value="1" id="nsinvert" title="no title" />'
+       + '<label for="nsinvert" title="no title">Invert selection</label>'
+       + '<input name="associated" type="checkbox" value="1" id="nsassociated" title="no title" />'
+       + '<label for="nsassociated" title="no title">Associated namespace</label>'
+       + '<input type="submit" value="Go" />'
+       + '<input type="hidden" value="Special:RecentChanges" name="title" />'
+       ;
+
+       var $env = $( '<div>' ).html( select ).appendTo( 'body' );
+       var enabled = undefined;
+
+       // TODO abstract the double strictEquals
+
+       // At first checkboxes are enabled
+       strictEqual( $('input#nsinvert').attr('disabled'), enabled);
+       strictEqual( $('input#nsassociated').attr('disabled'), enabled);
+
+       // load our magic code to disable them
+       mediaWiki.special.recentchanges.init();
+       strictEqual( $('#nsinvert').attr('disabled'), 'disabled');
+       strictEqual( $('#nsassociated').attr('disabled'), 'disabled' );
+
+       // select second option...
+       $('select#namespace option:nth-child(1)').removeAttr( 'selected' );
+       $('select#namespace option:nth-child(2)').attr( 'selected', 'selected' );
+       $('select#namespace').change();
+       // ... and checkboxes should be enabled again
+       strictEqual( $('input#nsinvert').attr('disabled'), enabled);
+       strictEqual( $('input#nsassociated').attr('disabled'), enabled);
+
+       // select first option ('all' namespace)...
+       $('select#namespace option:nth-child(1)').attr( 'selected', 'selected' );
+       $('select#namespace option:nth-child(2)').removeAttr( 'selected' );
+       $('select#namespace').change();
+       // ... and checkboxes should now be disabled
+       strictEqual( $('#nsinvert').attr('disabled'), 'disabled');
+       strictEqual( $('#nsassociated').attr('disabled'), 'disabled' );
+
+       // DOM cleanup
+       $env.remove();
+});