Umherirrender has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/140936

Change subject: jQuery-ify legalcy protect.js
......................................................................

jQuery-ify legalcy protect.js

Replaced dom functions with jquery functions and some for loops with
each calls. Hopfully no function change and only javascript part is
changed. So no change between the form generation and javascript
interaction. This can be done in another patch set.

Change-Id: Ie4185dff1c98b43671ec9bf92c4e8807a230930e
---
M skins/common/protect.js
1 file changed, 73 insertions(+), 144 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/36/140936/1

diff --git a/skins/common/protect.js b/skins/common/protect.js
index dc142ca..13e08a3 100644
--- a/skins/common/protect.js
+++ b/skins/common/protect.js
@@ -6,6 +6,7 @@
        /**
         * Set up the protection chaining interface (i.e. "unlock move 
permissions" checkbox)
         * on the protection form
+        * Called from html form
         *
         * @param opts Object : parameters with members:
         *     tableId              Identifier of the table containing UI bits
@@ -14,44 +15,42 @@
         *     existingMatch        True if all the existing expiry times match
         */
        init: function ( opts ) {
-               var box, boxbody, row, cell, check, label;
+               var $box, $boxbody, $row, $cell, $check, $label;
 
-               if ( !( document.createTextNode && document.getElementById && 
document.getElementsByTagName ) ) {
+               $box = $( document.getElementById( opts.tableId ) );
+               if ( !$box.length ) {
                        return false;
                }
 
-               box = document.getElementById( opts.tableId );
-               if ( !box ) {
-                       return false;
-               }
-
-               boxbody = box.getElementsByTagName( 'tbody' )[0];
-               row = document.createElement( 'tr' );
-               boxbody.insertBefore( row, boxbody.firstChild.nextSibling );
+               $boxbody = $box.find( 'tbody:first' );
+               $row = $( '<tr>' );
+               $row.insertAfter( $boxbody.find( 'tr:first' ) );
 
                this.existingMatch = opts.existingMatch;
 
-               cell = document.createElement( 'td' );
-               row.appendChild( cell );
+               $cell = $( '<td>' );
+               $row.append( $cell );
                // If there is only one protection type, there is nothing to 
chain
                if ( opts.numTypes > 1 ) {
-                       check = document.createElement( 'input' );
-                       check.id = 'mwProtectUnchained';
-                       check.type = 'checkbox';
-                       $( check ).click( function () {
+                       $check = $( '<input>' );
+                       $check.attr( {
+                               'id': 'mwProtectUnchained',
+                               'type': 'checkbox'
+                       } );
+                       $check.click( function () {
                                ProtectionForm.onChainClick();
                        } );
 
-                       label = document.createElement( 'label' );
-                       label.htmlFor = 'mwProtectUnchained';
-                       label.appendChild( document.createTextNode( 
opts.labelText ) );
+                       $label = $( '<label>' );
+                       $label.attr( 'htmlFor', 'mwProtectUnchained' );
+                       $label.text( opts.labelText );
 
-                       cell.appendChild( check );
-                       cell.appendChild( document.createTextNode( ' ' ) );
-                       cell.appendChild( label );
+                       $cell.append( $check );
+                       $cell.append( ' ' );
+                       $cell.append( $label );
 
-                       check.checked = !this.areAllTypesMatching();
-                       this.enableUnchainedInputs( check.checked );
+                       $check.prop( 'checked', !this.areAllTypesMatching() );
+                       this.enableUnchainedInputs( $check.prop( 'checked' ) );
                }
 
                $( '#mwProtect-reason' ).byteLimit( 180 );
@@ -65,25 +64,31 @@
         * Sets the disabled attribute on the cascade checkbox depending on the 
current selected levels
         */
        updateCascadeCheckbox: function () {
-               var i, lists, items, selected;
+               var selected,
+                       form = this,
+                       found = false;
 
                // For non-existent titles, there is no cascade option
-               if ( !document.getElementById( 'mwProtect-cascade' ) ) {
+               $mwProtectCascade = $( '#mwProtect-cascade' );
+               if ( !$mwProtectCascade.length ) {
                        return;
                }
-               lists = this.getLevelSelectors();
-               for ( i = 0; i < lists.length; i++ ) {
-                       if ( lists[i].selectedIndex > -1 ) {
-                               items = lists[i].getElementsByTagName( 'option' 
);
-                               selected = items[ lists[i].selectedIndex 
].value;
-                               if ( !this.isCascadeableLevel( selected ) ) {
-                                       document.getElementById( 
'mwProtect-cascade' ).checked = false;
-                                       document.getElementById( 
'mwProtect-cascade' ).disabled = true;
-                                       return;
-                               }
+
+               this.getLevelSelectors().each( function ( index, element ) {
+                       selected = $( element ).val();
+                       if ( !form.isCascadeableLevel( selected ) ) {
+                               $mwProtectCascade.prop( {
+                                       'checked': false,
+                                       'disabled': true
+                               } );
+                               found = true;
+                               return false;
                        }
+               } );
+
+               if ( !found ) {
+                       $mwProtectCascade.prop( 'disabled', false );
                }
-               document.getElementById( 'mwProtect-cascade' ).disabled = false;
        },
 
        /**
@@ -109,6 +114,7 @@
        /**
         * When protection levels are locked together, update the rest
         * when one action's level changes
+        * Called on onchange from html form
         *
         * @param source Element Level selector that changed
         */
@@ -122,16 +128,16 @@
        /**
         * When protection levels are locked together, update the
         * expiries when one changes
+        * Called from onchange or onkeyup on the htmlfrom
         *
         * @param source Element expiry input that changed
         */
-
        updateExpiry: function ( source ) {
                var expiry, listId, list;
 
                if ( !this.isUnchained() ) {
                        expiry = source.value;
-                       this.forEachExpiryInput( function ( element ) {
+                       this.getExpiryInputs().each( function ( index, element 
) {
                                element.value = expiry;
                        } );
                }
@@ -141,7 +147,7 @@
                        if ( this.isUnchained() ) {
                                list.value = 'othertime';
                        } else {
-                               this.forEachExpirySelector( function ( element 
) {
+                               this.getExpirySelectors().each( function ( 
index, element ) {
                                        element.value = 'othertime';
                                } );
                        }
@@ -151,6 +157,7 @@
        /**
         * When protection levels are locked together, update the
         * expiry lists when one changes and clear the custom inputs
+        * Called from onchange on the html form
         *
         * @param source Element expiry selector that changed
         */
@@ -158,10 +165,10 @@
                var expiry;
                if ( !this.isUnchained() ) {
                        expiry = source.value;
-                       this.forEachExpirySelector( function ( element ) {
+                       this.getExpirySelectors().each( function ( index, 
element ) {
                                element.value = expiry;
                        } );
-                       this.forEachExpiryInput( function ( element ) {
+                       this.getExpiryInputs().each( function ( index, element 
) {
                                element.value = '';
                        } );
                }
@@ -220,9 +227,9 @@
         * @return bool
         */
        isUnchained: function () {
-               var element = document.getElementById( 'mwProtectUnchained' );
-               return element
-                       ? element.checked
+               var $element = $( '#mwProtectUnchained' );
+               return $element.length
+                       ? $element.prop( 'checked' )
                        : true; // No control, so we need to let the user set 
both levels
        },
 
@@ -231,7 +238,7 @@
         */
        getMaxLevel: function () {
                var maxIndex = -1;
-               this.forEachLevelSelector( function ( element ) {
+               this.getLevelSelectors().each( function ( index, element ) {
                        if ( element.selectedIndex > maxIndex ) {
                                maxIndex = element.selectedIndex;
                        }
@@ -245,139 +252,61 @@
         * @param index int Protection level
         */
        setAllSelectors: function ( index ) {
-               this.forEachLevelSelector( function ( element ) {
-                       if ( element.selectedIndex !== index ) {
-                               element.selectedIndex = index;
-                       }
-               } );
-       },
-
-       /**
-        * Apply a callback to each protection selector
-        *
-        * @param func callable Callback function
-        */
-       forEachLevelSelector: function ( func ) {
-               var i, selectors;
-
-               selectors = this.getLevelSelectors();
-               for ( i = 0; i < selectors.length; i++ ) {
-                       func( selectors[i] );
-               }
+               this.getLevelSelectors().prop( 'selectedIndex', index );
        },
 
        /**
         * Get a list of all protection selectors on the page
         *
-        * @return Array
+        * @return jQuery
         */
        getLevelSelectors: function () {
-               var i, ours, all, element;
-
-               all = document.getElementsByTagName( 'select' );
-               ours = [];
-               for ( i = 0; i < all.length; i++ ) {
-                       element = all[i];
-                       if ( element.id.match( /^mwProtect-level-/ ) ) {
-                               ours[ours.length] = element;
-                       }
-               }
-               return ours;
-       },
-
-       /**
-        * Apply a callback to each expiry input
-        *
-        * @param func callable Callback function
-        */
-       forEachExpiryInput: function ( func ) {
-               var i, inputs;
-
-               inputs = this.getExpiryInputs();
-               for ( i = 0; i < inputs.length; i++ ) {
-                       func( inputs[i] );
-               }
+               return $( 'select' ).filter( function ( index, element ) {
+                       return element.id.match( /^mwProtect-level-/ );
+               } );
        },
 
        /**
         * Get a list of all expiry inputs on the page
         *
-        * @return Array
+        * @return jQuery
         */
        getExpiryInputs: function () {
-               var i, all, element, ours;
-
-               all = document.getElementsByTagName( 'input' );
-               ours = [];
-               for ( i = 0; i < all.length; i++ ) {
-                       element = all[i];
-                       if ( element.name.match( /^mwProtect-expiry-/ ) ) {
-                               ours[ours.length] = element;
-                       }
-               }
-               return ours;
-       },
-
-       /**
-        * Apply a callback to each expiry selector list
-        * @param func callable Callback function
-        */
-       forEachExpirySelector: function ( func ) {
-               var i, inputs;
-
-               inputs = this.getExpirySelectors();
-               for ( i = 0; i < inputs.length; i++ ) {
-                       func( inputs[i] );
-               }
+               return $( 'input' ).filter( function ( index, element ) {
+                       return element.name.match( /^mwProtect-expiry-/ );
+               } );
        },
 
        /**
         * Get a list of all expiry selector lists on the page
         *
-        * @return Array
+        * @return jQuery
         */
        getExpirySelectors: function () {
-               var i, all, ours, element;
-
-               all = document.getElementsByTagName( 'select' );
-               ours = [];
-               for ( i = 0; i < all.length; i++ ) {
-                       element = all[i];
-                       if ( element.id.match( /^mwProtectExpirySelection-/ ) ) 
{
-                               ours[ours.length] = element;
-                       }
-               }
-               return ours;
+               return $( 'select' ).filter( function ( index, element ) {
+                       return element.id.match( /^mwProtectExpirySelection-/ );
+               } );
        },
 
        /**
         * Enable/disable protection selectors and expiry inputs
+        * Also called on onsubmit from the html form
         *
         * @param val boolean Enable?
         */
        enableUnchainedInputs: function ( val ) {
-               var first = true;
-
-               this.forEachLevelSelector( function ( element ) {
-                       if ( first ) {
-                               first = false;
-                       } else {
+               this.getLevelSelectors().each( function ( index, element ) {
+                       if ( index !== 0 ) {
                                element.disabled = !val;
                        }
                } );
-               first = true;
-               this.forEachExpiryInput( function ( element ) {
-                       if ( first ) {
-                               first = false;
-                       } else {
+               this.getExpiryInputs().each( function ( index, element ) {
+                       if ( index !== 0 ) {
                                element.disabled = !val;
                        }
                } );
-               first = true;
-               this.forEachExpirySelector( function ( element ) {
-                       if ( first ) {
-                               first = false;
-                       } else {
+               this.getExpirySelectors().each( function ( index, element ) {
+                       if ( index !== 0 ) {
                                element.disabled = !val;
                        }
                } );

-- 
To view, visit https://gerrit.wikimedia.org/r/140936
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ie4185dff1c98b43671ec9bf92c4e8807a230930e
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Umherirrender <umherirrender_de...@web.de>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to