From 4fcd89d173e7e7226e010364267a99360794139e Mon Sep 17 00:00:00 2001 From: rillke Date: Sat, 14 Feb 2015 13:36:41 +0100 Subject: [PATCH] mediawiki.special.upload: One event handling function is enough - Common event handling function for all radios - Only remove `mw-upload-source-error` once per radio change (there is no way to disable a radio button without enabling another one) Change-Id: I1e5c7c403efb26c74df39669491bc49602a9df4f --- .../mediawiki.special.upload.js | 51 +++++++++---------- 1 file changed, 23 insertions(+), 28 deletions(-) diff --git a/resources/src/mediawiki.special/mediawiki.special.upload.js b/resources/src/mediawiki.special/mediawiki.special.upload.js index 2f03c518f7..eeccda59ed 100644 --- a/resources/src/mediawiki.special/mediawiki.special.upload.js +++ b/resources/src/mediawiki.special/mediawiki.special.upload.js @@ -137,11 +137,6 @@ }; $( function () { - // Disable URL box if the URL copy upload source type is not selected - if ( !$( '#wpSourceTypeurl' ).prop( 'checked' ) ) { - $( '#wpUploadFileURL' ).prop( 'disabled', true ); - } - // AJAX wpDestFile warnings if ( ajaxUploadDestCheck ) { // Insert an event handler that fetches upload warnings when wpDestFile @@ -532,31 +527,31 @@ // Disable all upload source fields except the selected one $( function () { - var i, $row, - $rows = $( '.mw-htmlform-field-UploadSourceField' ); + var $rows = $( '.mw-htmlform-field-UploadSourceField' ); - /** - * @param {jQuery} $currentRow - * @return {Function} Handler - * @return {jQuery.Event} return.e - */ - function createHandler( $currentRow ) { - return function () { - $( '.mw-upload-source-error' ).remove(); - if ( this.checked ) { - // Disable all inputs - $rows.find( 'input[name!="wpSourceType"]' ).prop( 'disabled', true ); - // Re-enable the current one - $currentRow.find( 'input' ).prop( 'disabled', false ); - } - }; - } + $rows.on( 'change', 'input[type="radio"]', function ( e ) { + var currentRow = e.delegateTarget; + + if ( !this.checked ) { + return; + } + + $( '.mw-upload-source-error' ).remove(); + + // Enable selected upload method + $( currentRow ).find( 'input' ).prop( 'disabled', false ); - for ( i = $rows.length; i; i-- ) { - $row = $rows.eq( i - 1 ); - $row - .find( 'input[name="wpSourceType"]' ) - .change( createHandler( $row ) ); + // Disable inputs of other upload methods + // (except for the radio button to re-enable it) + $rows + .not( currentRow ) + .find( 'input[type!="radio"]' ) + .prop( 'disabled', true ); + } ); + + // Set initial state + if ( !$( '#wpSourceTypeurl' ).prop( 'checked' ) ) { + $( '#wpUploadFileURL' ).prop( 'disabled', true ); } } ); -- 2.20.1