From: Krinkle Date: Sun, 8 Jan 2012 22:36:13 +0000 (+0000) Subject: [mediawiki.action.history.js] Clean up X-Git-Tag: 1.31.0-rc.0~25414 X-Git-Url: https://git.cyclocoop.org/%7B%24www_url%7Dadmin/compta/banques/?a=commitdiff_plain;h=d959330724ff08fe40a541424e138a4522e77509;p=lhc%2Fweb%2Fwiklou.git [mediawiki.action.history.js] Clean up * 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. --- diff --git a/resources/mediawiki.action/mediawiki.action.history.js b/resources/mediawiki.action/mediawiki.action.history.js index 1b5b3a00a7..04263c262d 100644 --- a/resources/mediawiki.action/mediawiki.action.history.js +++ b/resources/mediawiki.action/mediawiki.action.history.js @@ -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 +} );