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' ),
23 editingRestrictionWidget
= infuseOrNull( 'mw-input-wpEditingRestriction' ),
24 pageRestrictionsWidget
= infuseOrNull( 'mw-input-wpPageRestrictions' );
26 function updateBlockOptions() {
27 var blocktarget
= blockTargetWidget
.getValue().trim(),
28 isEmpty
= blocktarget
=== '',
29 isIp
= mw
.util
.isIPAddress( blocktarget
, true ),
30 isIpRange
= isIp
&& blocktarget
.match( /\/\d+$/ ),
31 isNonEmptyIp
= isIp
&& !isEmpty
,
32 expiryValue
= expiryWidget
.getValue(),
33 // infinityValues are the values the SpecialBlock class accepts as infinity (sf. wfIsInfinity)
34 infinityValues
= [ 'infinite', 'indefinite', 'infinity', 'never' ],
35 isIndefinite
= infinityValues
.indexOf( expiryValue
) !== -1,
36 editingRestrictionValue
= editingRestrictionWidget
? editingRestrictionWidget
.getValue() : undefined;
38 if ( enableAutoblockField
) {
39 enableAutoblockField
.toggle( !( isNonEmptyIp
) );
41 if ( hideUserField
) {
42 hideUserField
.toggle( !( isNonEmptyIp
|| !isIndefinite
) );
44 if ( anonOnlyField
) {
45 anonOnlyField
.toggle( !( !isIp
&& !isEmpty
) );
47 if ( watchUserField
) {
48 watchUserField
.toggle( !( isIpRange
&& !isEmpty
) );
50 if ( pageRestrictionsWidget
) {
51 pageRestrictionsWidget
.setDisabled( editingRestrictionValue
=== 'sitewide' );
55 if ( blockTargetWidget
) {
56 // Bind functions so they're checked whenever stuff changes
57 blockTargetWidget
.on( 'change', updateBlockOptions
);
58 expiryWidget
.on( 'change', updateBlockOptions
);
59 editingRestrictionWidget
.on( 'change', updateBlockOptions
);
61 // Call them now to set initial state (ie. Special:Block/Foobar?wpBlockExpiry=2+hours)