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' ) ),
26 namespaceRestrictionsWidget
= infuseIfExists( $( '#mw-input-wpNamespaceRestrictions' ) );
28 function updateBlockOptions() {
29 var blocktarget
= blockTargetWidget
.getValue().trim(),
30 isEmpty
= blocktarget
=== '',
31 isIp
= mw
.util
.isIPAddress( blocktarget
, true ),
32 isIpRange
= isIp
&& blocktarget
.match( /\/\d+$/ ),
33 isNonEmptyIp
= isIp
&& !isEmpty
,
34 expiryValue
= expiryWidget
.getValue(),
35 // infinityValues are the values the SpecialBlock class accepts as infinity (sf. wfIsInfinity)
36 infinityValues
= [ 'infinite', 'indefinite', 'infinity', 'never' ],
37 isIndefinite
= infinityValues
.indexOf( expiryValue
) !== -1,
38 editingRestrictionValue
= editingRestrictionWidget
? editingRestrictionWidget
.getValue() : undefined,
39 editingIsSelected
= editingWidget
? editingWidget
.isSelected() : false;
41 if ( enableAutoblockField
) {
42 enableAutoblockField
.toggle( !isNonEmptyIp
);
44 if ( hideUserField
) {
45 hideUserField
.toggle( !isNonEmptyIp
&& isIndefinite
);
47 if ( anonOnlyField
) {
48 anonOnlyField
.toggle( isIp
|| isEmpty
);
50 if ( watchUserField
) {
51 watchUserField
.toggle( !isIpRange
|| isEmpty
);
53 if ( editingRestrictionWidget
) {
54 editingRestrictionWidget
.setDisabled( !editingIsSelected
);
56 if ( pageRestrictionsWidget
) {
57 pageRestrictionsWidget
.setDisabled( !editingIsSelected
|| editingRestrictionValue
=== 'sitewide' );
59 if ( namespaceRestrictionsWidget
) {
60 namespaceRestrictionsWidget
.setDisabled( !editingIsSelected
|| editingRestrictionValue
=== 'sitewide' );
62 if ( preventTalkPageEdit
&& namespaceRestrictionsWidget
) {
63 // This option is disabled for partial blocks unless a namespace restriction
64 // for the User_talk namespace is in place.
65 preventTalkPageEdit
.setDisabled(
67 editingRestrictionValue
=== 'partial' &&
68 namespaceRestrictionsWidget
.getValue().indexOf(
69 String( mw
.config
.get( 'wgNamespaceIds' ).user_talk
)
76 if ( blockTargetWidget
) {
77 // Bind functions so they're checked whenever stuff changes
78 blockTargetWidget
.on( 'change', updateBlockOptions
);
79 expiryWidget
.on( 'change', updateBlockOptions
);
80 if ( editingWidget
) {
81 editingWidget
.on( 'change', updateBlockOptions
);
83 if ( editingRestrictionWidget
) {
84 editingRestrictionWidget
.on( 'change', updateBlockOptions
);
86 if ( namespaceRestrictionsWidget
) {
87 namespaceRestrictionsWidget
.on( 'change', updateBlockOptions
);
90 // Call them now to set initial state (ie. Special:Block/Foobar?wpBlockExpiry=2+hours)