From 7f1f6a200821b0ce3e6adff359821cb41a08bf77 Mon Sep 17 00:00:00 2001 From: Bryan Tong Minh Date: Wed, 16 Feb 2011 20:37:16 +0000 Subject: [PATCH] jQueryize the toggle upload source type code --- .../mediawiki.special.upload.js | 24 +++++++++++ skins/common/upload.js | 43 ------------------- 2 files changed, 24 insertions(+), 43 deletions(-) diff --git a/resources/mediawiki.special/mediawiki.special.upload.js b/resources/mediawiki.special/mediawiki.special.upload.js index e27761f2b8..dcaa727257 100644 --- a/resources/mediawiki.special/mediawiki.special.upload.js +++ b/resources/mediawiki.special/mediawiki.special.upload.js @@ -3,6 +3,9 @@ * Note that additional code still lives in skins/common/upload.js */ +/** + * Add a preview to the upload form + */ jQuery( function( $ ) { /** * Is the FileAPI available with sufficient functionality? @@ -203,3 +206,24 @@ jQuery( function( $ ) { } ); } } ); + +/** + * Disable all upload source fields except the selected one + */ +jQuery( function ( $ ) { + var rows = $( '.mw-htmlform-field-UploadSourceField' ); + for ( var i = rows.length; i; i-- ) { + var row = rows[i - 1]; + $( 'input[name="wpSourceType"]', row ).change( function () { + var currentRow = row; // Store current row in our own scope + return function () { + if ( this.checked ) { + // Disable all inputs + $( 'input[name!="wpSourceType"]', rows ).attr( 'disabled', true ); + // Re-enable the current one + $( 'input', currentRow ).attr( 'disabled', false ); + } + }; + }() ); + } +} ); diff --git a/skins/common/upload.js b/skins/common/upload.js index 4185ec9b8a..99917c75bd 100644 --- a/skins/common/upload.js +++ b/skins/common/upload.js @@ -35,11 +35,6 @@ window.wgUploadSetup = function() { } } - // Toggle source type - var sourceTypeCheckboxes = document.getElementsByName( 'wpSourceType' ); - for ( var i = 0; i < sourceTypeCheckboxes.length; i++ ) { - sourceTypeCheckboxes[i].onchange = toggleUploadInputs; - } // AJAX wpDestFile warnings if ( wgAjaxUploadDestCheck ) { @@ -86,44 +81,6 @@ window.wgUploadSetup = function() { }; }; -/** - * Iterate over all upload source fields and disable all except the selected one. - * - * @return emptiness - */ -window.toggleUploadInputs = function() { - // Iterate over all rows with UploadSourceField - var rows; - if ( document.getElementsByClassName ) { - rows = document.getElementsByClassName( 'mw-htmlform-field-UploadSourceField' ); - } else { - // Older browsers don't support getElementsByClassName - rows = new Array(); - - var allRows = document.getElementsByTagName( 'tr' ); - for ( var i = 0; i < allRows.length; i++ ) { - if ( allRows[i].className == 'mw-htmlform-field-UploadSourceField' ) - rows.push( allRows[i] ); - } - } - - for ( var i = 0; i < rows.length; i++ ) { - var inputs = rows[i].getElementsByTagName( 'input' ); - - // Check if this row is selected - var isChecked = true; // Default true in case wpSourceType is not found - for ( var j = 0; j < inputs.length; j++ ) { - if ( inputs[j].name == 'wpSourceType' ) - isChecked = inputs[j].checked; - } - - // Disable all unselected rows - for ( var j = 0; j < inputs.length; j++ ) { - if ( inputs[j].type != 'radio') - inputs[j].disabled = !isChecked; - } - } -}; window.wgUploadWarningObj = { 'responseCache' : { '' : ' ' }, -- 2.20.1