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 editingWidget
= infuseIfExists( $( '#mw-input-wpEditing' ) ),
23 editingRestrictionWidget
= infuseIfExists( $( '#mw-input-wpEditingRestriction' ) ),
24 preventTalkPageEdit
= infuseIfExists( $( '#mw-input-wpDisableUTEdit' ) ),
25 pageRestrictionsWidget
= infuseIfExists( $( '#mw-input-wpPageRestrictions' ) );
27 function updateBlockOptions() {
28 var blocktarget
= blockTargetWidget
.getValue().trim(),
29 isEmpty
= blocktarget
=== '',
30 isIp
= mw
.util
.isIPAddress( blocktarget
, true ),
31 isIpRange
= isIp
&& blocktarget
.match( /\/\d+$/ ),
32 isNonEmptyIp
= isIp
&& !isEmpty
,
33 expiryValue
= expiryWidget
.getValue(),
34 // infinityValues are the values the SpecialBlock class accepts as infinity (sf. wfIsInfinity)
35 infinityValues
= [ 'infinite', 'indefinite', 'infinity', 'never' ],
36 isIndefinite
= infinityValues
.indexOf( expiryValue
) !== -1,
37 editingRestrictionValue
= editingRestrictionWidget
? editingRestrictionWidget
.getValue() : undefined,
38 editingIsSelected
= editingWidget
? editingWidget
.isSelected() : undefined;
40 if ( enableAutoblockField
) {
41 enableAutoblockField
.toggle( !( isNonEmptyIp
) );
43 if ( hideUserField
) {
44 hideUserField
.toggle( !( isNonEmptyIp
|| !isIndefinite
) );
46 if ( anonOnlyField
) {
47 anonOnlyField
.toggle( !( !isIp
&& !isEmpty
) );
49 if ( watchUserField
) {
50 watchUserField
.toggle( !( isIpRange
&& !isEmpty
) );
52 if ( pageRestrictionsWidget
) {
53 editingRestrictionWidget
.setDisabled( !editingIsSelected
);
54 pageRestrictionsWidget
.setDisabled( !editingIsSelected
|| editingRestrictionValue
=== 'sitewide' );
56 if ( preventTalkPageEdit
) {
57 // TODO: (T210475) this option is disabled for partial blocks unless
58 // a namespace restriction for User_talk namespace is in place.
59 // This needs to be updated once Namespace restrictions is available
60 if ( editingRestrictionValue
=== 'partial' && editingIsSelected
) {
61 preventTalkPageEdit
.setDisabled( true );
63 preventTalkPageEdit
.setDisabled( false );
69 if ( blockTargetWidget
) {
70 // Bind functions so they're checked whenever stuff changes
71 blockTargetWidget
.on( 'change', updateBlockOptions
);
72 expiryWidget
.on( 'change', updateBlockOptions
);
73 if ( editingRestrictionWidget
) {
74 editingRestrictionWidget
.on( 'change', updateBlockOptions
);
76 if ( editingWidget
) {
77 editingWidget
.on( 'change', updateBlockOptions
);
80 // Call them now to set initial state (ie. Special:Block/Foobar?wpBlockExpiry=2+hours)