mediawiki.special.upload: One event handling function is enough
authorrillke <rillke@wikipedia.de>
Sat, 14 Feb 2015 12:36:41 +0000 (13:36 +0100)
committerKrinkle <krinklemail@gmail.com>
Wed, 4 Mar 2015 15:48:57 +0000 (15:48 +0000)
- 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

resources/src/mediawiki.special/mediawiki.special.upload.js

index 2f03c51..eeccda5 100644 (file)
        };
 
        $( 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
 
        // 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 );
                }
        } );