[mediawiki.action.history.js] Clean up
authorKrinkle <krinkle@users.mediawiki.org>
Sun, 8 Jan 2012 22:36:13 +0000 (22:36 +0000)
committerKrinkle <krinkle@users.mediawiki.org>
Sun, 8 Jan 2012 22:36:13 +0000 (22:36 +0000)
* Using named variables instead of eq(0) and eq(1), also future proof by checking name="" instead of depending on their respective position in the DOM.

resources/mediawiki.action/mediawiki.action.history.js

index 1b5b3a0..04263c2 100644 (file)
@@ -1,53 +1,71 @@
-/*
+/**
  * JavaScript for History action
  */
-jQuery( function( $ ) {
-       var $lis = $( 'ul#pagehistory li' );
-       var updateDiffRadios = function() {
+jQuery( document ).ready( function ( $ ) {
+       var     $lis = $( '#pagehistory > li' ),
+               $radios;
+
+       /**
+        * @context {Element} input
+        * @param e {jQuery.Event}
+        */
+       function updateDiffRadios() {
                var diffLi = false, // the li where the diff radio is checked
                        oldLi = false; // the li where the oldid radio is checked
 
                if ( !$lis.length ) {
                        return true;
                }
-               $lis.removeClass( 'selected' );
-               $lis.each( function() {
-                       var $this = $(this);
-                       var $inputs = $this.find( 'input[type="radio"]' );
-                       if ( $inputs.length !== 2 ) {
+
+               $lis
+               .removeClass( 'selected' )
+               .each( function () {
+                       var     $li = $(this),
+                               $inputs = $li.find( 'input[type="radio"]' ),
+                               $oldidRadio = $inputs.filter( '[name="oldid"]' ).eq(0),
+                               $diffRadio = $inputs.filter( '[name="diff"]' ).eq(0);
+
+                       if ( !$oldidRadio.length || !$diffRadio.length ) {
                                return true;
                        }
 
-                       // this row has a checked radio button
-                       if ( $inputs.get(0).checked ) { 
+                       if ( $oldidRadio.prop( 'checked' ) ) { 
                                oldLi = true;
-                               $this.addClass( 'selected' );
-                               $inputs.eq(0).css( 'visibility', 'visible' );
-                               $inputs.eq(1).css( 'visibility', 'hidden' );
-                       } else if ( $inputs.get(1).checked ) {
+                               $li.addClass( 'selected' );
+                               $oldidRadio.css( 'visibility', 'visible' );
+                               $diffRadio.css( 'visibility', 'hidden' );
+
+                       } else if ( $diffRadio.prop( 'checked' ) ) { 
                                diffLi = true;
-                               $this.addClass( 'selected' );
-                               $inputs.eq(0).css( 'visibility', 'hidden' );
-                               $inputs.eq(1).css( 'visibility', 'visible' );
+                               $li.addClass( 'selected' );
+                               $oldidRadio.css( 'visibility', 'hidden' );
+                               $diffRadio.css( 'visibility', 'visible' );
+
+                       // This list item has neither checked
                        } else { 
-                               // no radio is checked in this row
+                               // We're below the selected radios
                                if ( diffLi && oldLi ) {
-                                       // We're below the selected radios
-                                       $inputs.eq(0).css( 'visibility', 'visible' );
-                                       $inputs.eq(1).css( 'visibility', 'hidden' );
+                                       $oldidRadio.css( 'visibility', 'visible' );
+                                       $diffRadio.css( 'visibility', 'hidden' );
+
+                               // We're between the selected radios
                                } else if ( diffLi ) {
-                                       // We're between the selected radios
-                                       $inputs.css( 'visibility', 'visible' );
+                                       $diffRadio.css( 'visibility', 'visible' );
+                                       $oldidRadio.css( 'visibility', 'visible' );
+
+                               // We're above the selected radios
                                } else {
-                                       // We're above the selected radios
-                                       $inputs.eq(1).css( 'visibility', 'visible' );
-                                       $inputs.eq(0).css( 'visibility', 'hidden' );
+                                       $diffRadio.css( 'visibility', 'visible' );
+                                       $oldidRadio.css( 'visibility', 'hidden' );
                                }
                        }
                });
+
                return true;
-       };
+       }
+
+       $radios = $( '#pagehistory li input[name="diff"], #pagehistory li input[name="oldid"]' ).click( updateDiffRadios );
 
-       $( '#pagehistory li input[name="diff"], #pagehistory li input[name="oldid"]' ).click( updateDiffRadios );
+       // Set initial state
        updateDiffRadios();
-});
\ No newline at end of file
+} );