2 * JavaScript for Special:Block
5 // Like OO.ui.infuse(), but if the element doesn't exist, return null instead of throwing an exception.
6 function infuseOrNull( elem
) {
8 return OO
.ui
.infuse( elem
);
15 // This code is also loaded on the "block succeeded" page where there is no form,
16 // so username and expiry fields might also be missing.
17 var blockTargetWidget
= infuseOrNull( 'mw-bi-target' ),
18 anonOnlyField
= infuseOrNull( $( '#mw-input-wpHardBlock' ).closest( '.oo-ui-fieldLayout' ) ),
19 enableAutoblockField
= infuseOrNull( $( '#mw-input-wpAutoBlock' ).closest( '.oo-ui-fieldLayout' ) ),
20 hideUserField
= infuseOrNull( $( '#mw-input-wpHideUser' ).closest( '.oo-ui-fieldLayout' ) ),
21 watchUserField
= infuseOrNull( $( '#mw-input-wpWatch' ).closest( '.oo-ui-fieldLayout' ) ),
22 expiryWidget
= infuseOrNull( 'mw-input-wpExpiry' );
24 function updateBlockOptions() {
25 var blocktarget
= blockTargetWidget
.getValue().trim(),
26 isEmpty
= blocktarget
=== '',
27 isIp
= mw
.util
.isIPAddress( blocktarget
, true ),
28 isIpRange
= isIp
&& blocktarget
.match( /\/\d+$/ ),
29 isNonEmptyIp
= isIp
&& !isEmpty
,
30 expiryValue
= expiryWidget
.getValue(),
31 // infinityValues are the values the SpecialBlock class accepts as infinity (sf. wfIsInfinity)
32 infinityValues
= [ 'infinite', 'indefinite', 'infinity', 'never' ],
33 isIndefinite
= infinityValues
.indexOf( expiryValue
) !== -1;
35 if ( enableAutoblockField
) {
36 enableAutoblockField
.toggle( !( isNonEmptyIp
) );
38 if ( hideUserField
) {
39 hideUserField
.toggle( !( isNonEmptyIp
|| !isIndefinite
) );
41 if ( anonOnlyField
) {
42 anonOnlyField
.toggle( !( !isIp
&& !isEmpty
) );
44 if ( watchUserField
) {
45 watchUserField
.toggle( !( isIpRange
&& !isEmpty
) );
49 if ( blockTargetWidget
) {
50 // Bind functions so they're checked whenever stuff changes
51 blockTargetWidget
.on( 'change', updateBlockOptions
);
52 expiryWidget
.on( 'change', updateBlockOptions
);
54 // Call them now to set initial state (ie. Special:Block/Foobar?wpBlockExpiry=2+hours)
58 }( mediaWiki
, jQuery
) );