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 infuseIfExists( $el
) {
10 return OO
.ui
.infuse( $el
);
14 // This code is also loaded on the "block succeeded" page where there is no form,
15 // so username and expiry fields might also be missing.
16 var blockTargetWidget
= infuseIfExists( $( '#mw-bi-target' ) ),
17 anonOnlyField
= infuseIfExists( $( '#mw-input-wpHardBlock' ).closest( '.oo-ui-fieldLayout' ) ),
18 enableAutoblockField
= infuseIfExists( $( '#mw-input-wpAutoBlock' ).closest( '.oo-ui-fieldLayout' ) ),
19 hideUserField
= infuseIfExists( $( '#mw-input-wpHideUser' ).closest( '.oo-ui-fieldLayout' ) ),
20 watchUserField
= infuseIfExists( $( '#mw-input-wpWatch' ).closest( '.oo-ui-fieldLayout' ) ),
21 expiryWidget
= infuseIfExists( $( '#mw-input-wpExpiry' ) ),
22 editingRestrictionWidget
= infuseIfExists( $( '#mw-input-wpEditingRestriction' ) ),
23 pageRestrictionsWidget
= infuseIfExists( $( '#mw-input-wpPageRestrictions' ) );
25 function updateBlockOptions() {
26 var blocktarget
= blockTargetWidget
.getValue().trim(),
27 isEmpty
= blocktarget
=== '',
28 isIp
= mw
.util
.isIPAddress( blocktarget
, true ),
29 isIpRange
= isIp
&& blocktarget
.match( /\/\d+$/ ),
30 isNonEmptyIp
= isIp
&& !isEmpty
,
31 expiryValue
= expiryWidget
.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 editingRestrictionValue
= editingRestrictionWidget
? editingRestrictionWidget
.getValue() : undefined;
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
) );
49 if ( pageRestrictionsWidget
) {
50 pageRestrictionsWidget
.setDisabled( editingRestrictionValue
=== 'sitewide' );
54 if ( blockTargetWidget
) {
55 // Bind functions so they're checked whenever stuff changes
56 blockTargetWidget
.on( 'change', updateBlockOptions
);
57 expiryWidget
.on( 'change', updateBlockOptions
);
58 editingRestrictionWidget
.on( 'change', updateBlockOptions
);
60 // Call them now to set initial state (ie. Special:Block/Foobar?wpBlockExpiry=2+hours)