From 730eeb8e26c5adbef90a597a67b8cb092c6b01fb Mon Sep 17 00:00:00 2001 From: Krinkle Date: Fri, 4 Mar 2011 01:48:24 +0000 Subject: [PATCH] =?utf8?q?Moving=20IP-check=20to=20mw.util=20&=20some=20en?= =?utf8?q?hancements=20to=20and=20applying=20code=20conventions=20to=20mw.?= =?utf8?q?special.block.js=20*=20This=20commit=20addresses=20the=20@TODOs?= =?utf8?q?=20that=20were=20in=20mediawiki.legacy.block=20before=20it=20was?= =?utf8?q?=20migrated=20to=20/resources=20in=20r83183=20("@TODO:=20find=20?= =?utf8?q?some=20better=20JS=20file=20for=20[is=20IP=20functions]")=20*=20?= =?utf8?q?JSHint=20warning=20fixed:=20Using=20=3D=3D=3D=3D=20when=20compar?= =?utf8?q?ing=20to=20''=20(safer=20and=20faster=20than=20=3D=3D)=20*=20Ins?= =?utf8?q?tead=20of=20checking=20the=20initial=20onload=20state=20by=20fak?= =?utf8?q?ing=20a=20onkeyup/onchange=20event,=20seperate=20the=20two=20fun?= =?utf8?q?ctions=20and=20use=20those=20**=20-->=20This=20fixes=20issue=20w?= =?utf8?q?here=20it=20falsely=20triggers=20other=20bindings=20on-load=20th?= =?utf8?q?at=20are=20globally=20bound=20to=20input=20elements=20via=20$('i?= =?utf8?q?nput').live()=20or=20$('[type=3D"text"]').delegate()=20because?= =?utf8?q?=20of=20the=20.keyup()/.change()=20call=20)=20*=20Caching=20sele?= =?utf8?q?ctors=20(var=20$wpBlockExpiry=20=3D=20$(..)=20etc.)=20instead=20?= =?utf8?q?of=20re-creating=20a=20dozen=20jQuery=20objects=20again=20and=20?= =?utf8?q?getting=20the=20document=20element=20by=20id=20=E2=80=93=20on=20?= =?utf8?q?*every*=20onkeyup/onchange=20**=20-->=20Notable=20speed=20improv?= =?utf8?q?ement=20in=20Safari=203=20and=20IE6=20with=20the=20caching,=20th?= =?utf8?q?ese=20had=20a=20little=20bit=20of=20a=20lag=20when=20typing=20fa?= =?utf8?q?st,=20mw.legacy.block.js=20had=20the=20lag=20as=20well.=20Fixed?= =?utf8?q?=20now.=20*=20Adding=20instantToggle-argument=20to=20switch=20be?= =?utf8?q?tween=20fade=20or=20hiding/showing=20right=20away.=20When=20sett?= =?utf8?q?ing=20the=20initial=20state=20they're=20called=20with=20instant?= =?utf8?q?=3Dtrue=20so=20that=20there's=20no=20fading,=20the=20page=20load?= =?utf8?q?s=20and=20stuff=20hides=20that=20shouldn't=20be=20visible.=20Any?= =?utf8?q?=20later=20interaction=20will=20still=20have=20the=20fade.=20**?= =?utf8?q?=20Calling=20a=20function=20like=20foo(true)=20sucks,=20adding?= =?utf8?q?=20a=20local=20DO=5FINSTANT=20=3D=20true.=20*=20Making=20behavio?= =?utf8?q?ur=20the=20same=20as=20the=20legacy.block=20was=20(if=20isEmpty,?= =?utf8?q?=20do=20bring=20the=20element=20back=20to=20view=20(=20show()=20?= =?utf8?q?/=20fadeIn)=20instead=20of=20doing=20nothing)?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- resources/Resources.php | 1 - .../mediawiki.special.block.js | 111 ++++++++++-------- resources/mediawiki.util/mediawiki.util.js | 26 ++++ 3 files changed, 87 insertions(+), 51 deletions(-) diff --git a/resources/Resources.php b/resources/Resources.php index ccb6bd7c12..e8947b9b24 100644 --- a/resources/Resources.php +++ b/resources/Resources.php @@ -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 diff --git a/resources/mediawiki.special/mediawiki.special.block.js b/resources/mediawiki.special/mediawiki.special.block.js index 4955545707..ed0fa61beb 100644 --- a/resources/mediawiki.special/mediawiki.special.block.js +++ b/resources/mediawiki.special/mediawiki.special.block.js @@ -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 diff --git a/resources/mediawiki.util/mediawiki.util.js b/resources/mediawiki.util/mediawiki.util.js index 1b395a6d27..085c4f138b 100644 --- a/resources/mediawiki.util/mediawiki.util.js +++ b/resources/mediawiki.util/mediawiki.util.js @@ -544,6 +544,32 @@ '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; } }; -- 2.20.1