r108373 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r108372‎ | r108373 | r108374 >
Date:22:36, 8 January 2012
Author:krinkle
Status:ok (Comments)
Tags:
Comment:
[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.
Modified paths:
  • /trunk/phase3/resources/mediawiki.action/mediawiki.action.history.js (modified) (history)

Diff [purge]

Index: trunk/phase3/resources/mediawiki.action/mediawiki.action.history.js
@@ -1,53 +1,71 @@
2 -/*
 2+/**
33 * JavaScript for History action
44 */
5 -jQuery( function( $ ) {
6 - var $lis = $( 'ul#pagehistory li' );
7 - var updateDiffRadios = function() {
 5+jQuery( document ).ready( function ( $ ) {
 6+ var $lis = $( '#pagehistory > li' ),
 7+ $radios;
 8+
 9+ /**
 10+ * @context {Element} input
 11+ * @param e {jQuery.Event}
 12+ */
 13+ function updateDiffRadios() {
814 var diffLi = false, // the li where the diff radio is checked
915 oldLi = false; // the li where the oldid radio is checked
1016
1117 if ( !$lis.length ) {
1218 return true;
1319 }
14 - $lis.removeClass( 'selected' );
15 - $lis.each( function() {
16 - var $this = $(this);
17 - var $inputs = $this.find( 'input[type="radio"]' );
18 - if ( $inputs.length !== 2 ) {
 20+
 21+ $lis
 22+ .removeClass( 'selected' )
 23+ .each( function () {
 24+ var $li = $(this),
 25+ $inputs = $li.find( 'input[type="radio"]' ),
 26+ $oldidRadio = $inputs.filter( '[name="oldid"]' ).eq(0),
 27+ $diffRadio = $inputs.filter( '[name="diff"]' ).eq(0);
 28+
 29+ if ( !$oldidRadio.length || !$diffRadio.length ) {
1930 return true;
2031 }
2132
22 - // this row has a checked radio button
23 - if ( $inputs.get(0).checked ) {
 33+ if ( $oldidRadio.prop( 'checked' ) ) {
2434 oldLi = true;
25 - $this.addClass( 'selected' );
26 - $inputs.eq(0).css( 'visibility', 'visible' );
27 - $inputs.eq(1).css( 'visibility', 'hidden' );
28 - } else if ( $inputs.get(1).checked ) {
 35+ $li.addClass( 'selected' );
 36+ $oldidRadio.css( 'visibility', 'visible' );
 37+ $diffRadio.css( 'visibility', 'hidden' );
 38+
 39+ } else if ( $diffRadio.prop( 'checked' ) ) {
2940 diffLi = true;
30 - $this.addClass( 'selected' );
31 - $inputs.eq(0).css( 'visibility', 'hidden' );
32 - $inputs.eq(1).css( 'visibility', 'visible' );
 41+ $li.addClass( 'selected' );
 42+ $oldidRadio.css( 'visibility', 'hidden' );
 43+ $diffRadio.css( 'visibility', 'visible' );
 44+
 45+ // This list item has neither checked
3346 } else {
34 - // no radio is checked in this row
 47+ // We're below the selected radios
3548 if ( diffLi && oldLi ) {
36 - // We're below the selected radios
37 - $inputs.eq(0).css( 'visibility', 'visible' );
38 - $inputs.eq(1).css( 'visibility', 'hidden' );
 49+ $oldidRadio.css( 'visibility', 'visible' );
 50+ $diffRadio.css( 'visibility', 'hidden' );
 51+
 52+ // We're between the selected radios
3953 } else if ( diffLi ) {
40 - // We're between the selected radios
41 - $inputs.css( 'visibility', 'visible' );
 54+ $diffRadio.css( 'visibility', 'visible' );
 55+ $oldidRadio.css( 'visibility', 'visible' );
 56+
 57+ // We're above the selected radios
4258 } else {
43 - // We're above the selected radios
44 - $inputs.eq(1).css( 'visibility', 'visible' );
45 - $inputs.eq(0).css( 'visibility', 'hidden' );
 59+ $diffRadio.css( 'visibility', 'visible' );
 60+ $oldidRadio.css( 'visibility', 'hidden' );
4661 }
4762 }
4863 });
 64+
4965 return true;
50 - };
 66+ }
5167
52 - $( '#pagehistory li input[name="diff"], #pagehistory li input[name="oldid"]' ).click( updateDiffRadios );
 68+ $radios = $( '#pagehistory li input[name="diff"], #pagehistory li input[name="oldid"]' ).click( updateDiffRadios );
 69+
 70+ // Set initial state
5371 updateDiffRadios();
54 -});
\ No newline at end of file
 72+} );

Comments

#Comment by Robmoen (talk | contribs)   19:11, 20 January 2012

I see that you removed the redundant $radios in in r108380. Looks good.

Status & tagging log