Moving IP-check to mw.util & some enhancements to and applying code conventions to...
authorKrinkle <krinkle@users.mediawiki.org>
Fri, 4 Mar 2011 01:48:24 +0000 (01:48 +0000)
committerKrinkle <krinkle@users.mediawiki.org>
Fri, 4 Mar 2011 01:48:24 +0000 (01:48 +0000)
* This commit addresses the @TODOs that were in mediawiki.legacy.block before it was migrated to /resources in r83183 ("@TODO: find some better JS file for [is IP functions]")
* JSHint warning fixed: Using ==== when comparing to '' (safer and faster than ==)
* Instead of checking the initial onload state by faking a onkeyup/onchange event, seperate the two functions and use those
** --> This fixes issue where it falsely triggers other bindings on-load that are globally bound to input elements via $('input').live() or $('[type="text"]').delegate() because of the .keyup()/.change() call )
* Caching selectors (var $wpBlockExpiry = $(..) etc.) instead of re-creating a dozen jQuery objects again and getting the document element by id – on *every* onkeyup/onchange
** --> Notable speed improvement in Safari 3 and IE6 with the caching, these had a little bit of a lag when typing fast, mw.legacy.block.js had the lag as well. Fixed now.
* Adding instantToggle-argument to switch between fade or hiding/showing right away. When setting the initial state they're called with instant=true so that there's no fading, the page loads and stuff hides that shouldn't be visible. Any later interaction will still have the fade.
** Calling a function like foo(true) sucks, adding a local DO_INSTANT = true.
* Making behaviour the same as the legacy.block was (if isEmpty, do bring the element back to view ( show() / fadeIn) instead of doing nothing)

resources/Resources.php
resources/mediawiki.special/mediawiki.special.block.js
resources/mediawiki.util/mediawiki.util.js

index ccb6bd7..e8947b9 100644 (file)
@@ -426,7 +426,6 @@ return array(
        ),
        'mediawiki.special.block' => array(
                'scripts' => 'resources/mediawiki.special/mediawiki.special.block.js',
-               'dependencies' => array( 'jquery.effects.blind' ),
        ),
        'mediawiki.special.upload' => array(
                // @TODO: merge in remainder of mediawiki.legacy.upload
index 4955545..ed0fa61 100644 (file)
@@ -1,57 +1,68 @@
 /* JavaScript for Special:Block */
+
+// Fade or snap depending on argument
+jQuery.fn.goIn = function( instantToggle ) {
+       if ( typeof instantToggle != 'undefined' && instantToggle === true ) {
+               return jQuery(this).show();
+       }
+       return jQuery(this).stop( true, true ).fadeIn();
+};
+jQuery.fn.goOut = function( instantToggle ) {
+       if ( typeof instantToggle != 'undefined' && instantToggle === true ) {
+               return jQuery(this).hide();
+       }
+       return jQuery(this).stop( true, true ).fadeOut();
+};
+
 jQuery( function( $ ) {
 
-       $('#mw-bi-target').keyup(function(){
-               var isIPv4Address = function( address ) {
-                       var RE_IP_BYTE = '(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|0?[0-9]?[0-9])';
-                       var RE_IP_ADD = '(?:' + RE_IP_BYTE + '\\.){3}' + RE_IP_BYTE;
-                       return address.search( new RegExp( '^' + RE_IP_ADD + '(?:\\/(?:3[0-2]|[12]?\\d))?$' ) ) != -1;
-               };
-               var isIPv6Address = function( address ) {
-                       var RE_IPV6_ADD =
-                       '(?:' + // starts with "::" (including "::")
-                               ':(?::|(?::' + '[0-9A-Fa-f]{1,4}' + '){1,7})' +
-                       '|' + // ends with "::" (except "::")
-                               '[0-9A-Fa-f]{1,4}' + '(?::' + '[0-9A-Fa-f]{1,4}' + '){0,6}::' +
-                       '|' + // contains no "::"
-                               '[0-9A-Fa-f]{1,4}' + '(?::' + '[0-9A-Fa-f]{1,4}' + '){7}' +
-                       ')';
-                       if ( address.search( new RegExp( '^' + RE_IPV6_ADD + '(?:\\/(?:12[0-8]|1[01][0-9]|[1-9]?\\d))?$' ) ) != -1 ) {
-                               return true;
-                       }
-                       var RE_IPV6_ADD_SHORT = // contains one "::" in the middle (single '::' check below)
-                               '[0-9A-Fa-f]{1,4}' + '(?:::?' + '[0-9A-Fa-f]{1,4}' + '){1,6}';
-                       return address.search( new RegExp( '^' + RE_IPV6_ADD_SHORT + '(?:\\/(?:12[0-8]|1[01][0-9]|[1-9]?\\d))?$' ) ) != -1
-                               && address.search( /::/ ) != -1 && address.search( /::.*::/ ) == -1;
-               };
-
-               var input = $('#mw-bi-target').val();
-
-               var isEmpty = ( input == "" );
-               var isIp = isIPv4Address( input ) || isIPv6Address( input );
-               var isIpRange = isIp && input.match(/\/\d+$/);
-
-               if( !isEmpty ){
-                       if( isIp ){
-                               $( '#wpAnonOnlyRow' ).stop( true, true ).delay(1000).fadeIn();
-                               $( '#wpEnableAutoblockRow, #wpEnableHideUser' ).stop( true, true ).delay(1000).fadeOut();
-                       } else {
-                               $( '#wpAnonOnlyRow' ).stop( true, true ).delay(1000).fadeOut();
-                               $( '#wpEnableAutoblockRow, #wpEnableHideUser' ).stop( true, true ).delay(1000).fadeIn();
-                       }
-                       if( isIpRange ){
-                               $( '#wpEnableWatchUser' ).stop( true, true ).delay(1000).fadeOut();
-                       } else {
-                               $( '#wpEnableWatchUser' ).stop( true, true ).delay(1000).fadeIn();
-                       }
+       var     DO_INSTANT = true,
+               $blockExpiry = $( '#wpBlockExpiry' ),   $blockOther = $( '#wpBlockOther' ),
+               $blockTarget = $( '#mw-bi-target' ),    $anonOnlyRow = $( '#wpAnonOnlyRow' ),
+               $enableAutoblockRow = $( '#wpEnableAutoblockRow' ),
+               $hideUser = $( '#wpEnableHideUser' ),   $watchUser = $( '#wpEnableWatchUser' );
+
+       var considerChangingExpiryFocus = function( instant ) {
+               if ( $blockExpiry.val() == 'other' ) {
+                       $blockOther.goIn( instant );
+               } else {
+                       $blockOther.goOut( instant );
+               }
+       };
+       var updateBlockOptions = function( instant ) {
+               if ( !$blockTarget.length ) {
+                       return;
                }
-       }).keyup();
 
-       $('#wpBlockExpiry').change( function(){
-               if( $(this).val() == 'other' ){
-                       $('#wpBlockOther').stop( true, true ).fadeIn();
+               var blocktarget = $.trim( $blockTarget.val() );
+               var isEmpty = ( blocktarget === '' );
+               var isIp = mw.util.isIPv4Address( blocktarget, true ) || mw.util.isIPv6Address( blocktarget, true );
+               var isIpRange = isIp && blocktarget.match( /\/\d+$/ );
+
+               if ( isIp && !isEmpty ) {
+                       $enableAutoblockRow.goOut( instant );
+                       $hideUser.goOut( instant );
                } else {
-                       $('#wpBlockOther').stop( true, true ).fadeOut();
+                       $enableAutoblockRow.goIn( instant );
+                       $hideUser.goIn( instant );
                }
-       }).change();
-} );
\ No newline at end of file
+               if ( !isIp && !isEmpty ) {
+                       $anonOnlyRow.goOut( instant );
+               } else {
+                       $anonOnlyRow.goIn( instant );
+               }
+               if ( isIpRange && !isEmpty ) {
+                       $watchUser.goOut( instant );
+               } else {
+                       $watchUser.goIn( instant );
+               }
+       };
+
+       // Bind functions so they're checked whenever stuff changes
+       $blockExpiry.change( considerChangingExpiryFocus );
+       $blockTarget.keyup( updateBlockOptions );
+
+       // Call them now to set initial state (ie. Special:Block/Foobar?wpBlockExpiry=2+hours)
+       considerChangingExpiryFocus( DO_INSTANT );
+       updateBlockOptions( DO_INSTANT );
+});
\ No newline at end of file
index 1b395a6..085c4f1 100644 (file)
                                        'i'
                                );
                        return (null !== mailtxt.match( HTML5_email_regexp ) );
+               },
+               // Note: borrows from IP.php
+               'isIPv4Address' : function( address, allowBlock ) {
+                       var block = allowBlock ? '(?:\\/(?:3[0-2]|[12]?\\d))?' : '';
+                       var RE_IP_BYTE = '(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|0?[0-9]?[0-9])';
+                       var RE_IP_ADD = '(?:' + RE_IP_BYTE + '\\.){3}' + RE_IP_BYTE;
+                       return address.search( new RegExp( '^' + RE_IP_ADD + block + '$' ) ) != -1;
+               },
+               // Note: borrows from IP.php
+               'isIPv6Address' : function( address, allowBlock ) {
+                       var block = allowBlock ? '(?:\\/(?:12[0-8]|1[01][0-9]|[1-9]?\\d))?' : '';
+                       var RE_IPV6_ADD =
+                       '(?:' + // starts with "::" (including "::")
+                       ':(?::|(?::' + '[0-9A-Fa-f]{1,4}' + '){1,7})' +
+                       '|' + // ends with "::" (except "::")
+                       '[0-9A-Fa-f]{1,4}' + '(?::' + '[0-9A-Fa-f]{1,4}' + '){0,6}::' +
+                       '|' + // contains no "::"
+                       '[0-9A-Fa-f]{1,4}' + '(?::' + '[0-9A-Fa-f]{1,4}' + '){7}' +
+                       ')';
+                       if ( address.search( new RegExp( '^' + RE_IPV6_ADD + block + '$' ) ) != -1 ) {
+                               return true;
+                       }
+                       RE_IPV6_ADD = // contains one "::" in the middle (single '::' check below)
+                               '[0-9A-Fa-f]{1,4}' + '(?:::?' + '[0-9A-Fa-f]{1,4}' + '){1,6}';
+                       return address.search( new RegExp( '^' + RE_IPV6_ADD + block + '$' ) ) != -1
+                               && address.search( /::/ ) != -1 && address.search( /::.*::/ ) == -1;
                }
 
        };