r82293 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r82292‎ | r82293 | r82294 >
Date:20:37, 16 February 2011
Author:btongminh
Status:ok (Comments)
Tags:todo 
Comment:
jQueryize the toggle upload source type code
Modified paths:
  • /trunk/phase3/resources/mediawiki.special/mediawiki.special.upload.js (modified) (history)
  • /trunk/phase3/skins/common/upload.js (modified) (history)

Diff [purge]

Index: trunk/phase3/skins/common/upload.js
@@ -35,11 +35,6 @@
3636 }
3737 }
3838
39 - // Toggle source type
40 - var sourceTypeCheckboxes = document.getElementsByName( 'wpSourceType' );
41 - for ( var i = 0; i < sourceTypeCheckboxes.length; i++ ) {
42 - sourceTypeCheckboxes[i].onchange = toggleUploadInputs;
43 - }
4439
4540 // AJAX wpDestFile warnings
4641 if ( wgAjaxUploadDestCheck ) {
@@ -86,44 +81,6 @@
8782 };
8883 };
8984
90 -/**
91 - * Iterate over all upload source fields and disable all except the selected one.
92 - *
93 - * @return emptiness
94 - */
95 -window.toggleUploadInputs = function() {
96 - // Iterate over all rows with UploadSourceField
97 - var rows;
98 - if ( document.getElementsByClassName ) {
99 - rows = document.getElementsByClassName( 'mw-htmlform-field-UploadSourceField' );
100 - } else {
101 - // Older browsers don't support getElementsByClassName
102 - rows = new Array();
103 -
104 - var allRows = document.getElementsByTagName( 'tr' );
105 - for ( var i = 0; i < allRows.length; i++ ) {
106 - if ( allRows[i].className == 'mw-htmlform-field-UploadSourceField' )
107 - rows.push( allRows[i] );
108 - }
109 - }
110 -
111 - for ( var i = 0; i < rows.length; i++ ) {
112 - var inputs = rows[i].getElementsByTagName( 'input' );
113 -
114 - // Check if this row is selected
115 - var isChecked = true; // Default true in case wpSourceType is not found
116 - for ( var j = 0; j < inputs.length; j++ ) {
117 - if ( inputs[j].name == 'wpSourceType' )
118 - isChecked = inputs[j].checked;
119 - }
120 -
121 - // Disable all unselected rows
122 - for ( var j = 0; j < inputs.length; j++ ) {
123 - if ( inputs[j].type != 'radio')
124 - inputs[j].disabled = !isChecked;
125 - }
126 - }
127 -};
12885
12986 window.wgUploadWarningObj = {
13087 'responseCache' : { '' : '&nbsp;' },
Index: trunk/phase3/resources/mediawiki.special/mediawiki.special.upload.js
@@ -3,6 +3,9 @@
44 * Note that additional code still lives in skins/common/upload.js
55 */
66
 7+/**
 8+ * Add a preview to the upload form
 9+ */
710 jQuery( function( $ ) {
811 /**
912 * Is the FileAPI available with sufficient functionality?
@@ -203,3 +206,24 @@
204207 } );
205208 }
206209 } );
 210+
 211+/**
 212+ * Disable all upload source fields except the selected one
 213+ */
 214+jQuery( function ( $ ) {
 215+ var rows = $( '.mw-htmlform-field-UploadSourceField' );
 216+ for ( var i = rows.length; i; i-- ) {
 217+ var row = rows[i - 1];
 218+ $( 'input[name="wpSourceType"]', row ).change( function () {
 219+ var currentRow = row; // Store current row in our own scope
 220+ return function () {
 221+ if ( this.checked ) {
 222+ // Disable all inputs
 223+ $( 'input[name!="wpSourceType"]', rows ).attr( 'disabled', true );
 224+ // Re-enable the current one
 225+ $( 'input', currentRow ).attr( 'disabled', false );
 226+ }
 227+ };
 228+ }() );
 229+ }
 230+} );

Sign-offs

UserFlagDate
Krinkleinspected20:13, 17 February 2011

Comments

#Comment by Krinkle (talk | contribs)   19:40, 7 March 2011

Why the function in the return ?


+			return function () {
};

attr( 'disabled', true ); attr( 'disabled', false ); are better done with removeAttr( 'disabled') and attr( 'disabled', 'disabled' );

See also $.fn.each, something like the following

$( 'selector' ).each( function( i, el ) {
  // i is a counter, starting at 0
  // el is the same as 'this', the current item of the loop. either 'el' or 'this' can be used
  // saves having to do "var el = this;" if 'this' gets overwritten in the loop with another subscrope.
  $(this).find( 'input[name]' ).change( fn );


} );

Status & tagging log