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 // mw.widgets.SelectWithInputWidget
23 expiryWidget
= infuseOrNull( 'mw-input-wpExpiry' );
25 function updateBlockOptions() {
26 var blocktarget
= $.trim( blockTargetWidget
.getValue() ),
27 isEmpty
= blocktarget
=== '',
28 isIp
= mw
.util
.isIPAddress( blocktarget
, true ),
29 isIpRange
= isIp
&& blocktarget
.match( /\/\d+$/ ),
30 isNonEmptyIp
= isIp
&& !isEmpty
,
31 expiryValue
= expiryWidget
.dropdowninput
.getValue(),
32 // infinityValues are the values the SpecialBlock class accepts as infinity (sf. wfIsInfinity)
33 infinityValues
= [ 'infinite', 'indefinite', 'infinity', 'never' ],
34 isIndefinite
= infinityValues
.indexOf( expiryValue
) !== -1 ||
35 ( expiryValue
=== 'other' && infinityValues
.indexOf( expiryWidget
.textinput
.getValue() ) !== -1 );
37 if ( enableAutoblockField
) {
38 enableAutoblockField
.toggle( !( isNonEmptyIp
) );
40 if ( hideUserField
) {
41 hideUserField
.toggle( !( isNonEmptyIp
|| !isIndefinite
) );
43 if ( anonOnlyField
) {
44 anonOnlyField
.toggle( !( !isIp
&& !isEmpty
) );
46 if ( watchUserField
) {
47 watchUserField
.toggle( !( isIpRange
&& !isEmpty
) );
51 if ( blockTargetWidget
) {
52 // Bind functions so they're checked whenever stuff changes
53 blockTargetWidget
.on( 'change', updateBlockOptions
);
54 expiryWidget
.dropdowninput
.on( 'change', updateBlockOptions
);
55 expiryWidget
.textinput
.on( 'change', updateBlockOptions
);
57 // Call them now to set initial state (ie. Special:Block/Foobar?wpBlockExpiry=2+hours)
61 }( mediaWiki
, jQuery
) );