/* 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
'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;
}
};