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 hideUserWidget
= infuseIfExists( $( '#mw-input-wpHideUser' ) ),
20 hideUserField
= infuseIfExists( $( '#mw-input-wpHideUser' ).closest( '.oo-ui-fieldLayout' ) ),
21 watchUserField
= infuseIfExists( $( '#mw-input-wpWatch' ).closest( '.oo-ui-fieldLayout' ) ),
22 expiryWidget
= infuseIfExists( $( '#mw-input-wpExpiry' ) ),
23 editingWidget
= infuseIfExists( $( '#mw-input-wpEditing' ) ),
24 editingRestrictionWidget
= infuseIfExists( $( '#mw-input-wpEditingRestriction' ) ),
25 preventTalkPageEdit
= infuseIfExists( $( '#mw-input-wpDisableUTEdit' ) ),
26 pageRestrictionsWidget
= infuseIfExists( $( '#mw-input-wpPageRestrictions' ) ),
27 namespaceRestrictionsWidget
= infuseIfExists( $( '#mw-input-wpNamespaceRestrictions' ) );
29 function updateBlockOptions() {
30 var blocktarget
= blockTargetWidget
.getValue().trim(),
31 isEmpty
= blocktarget
=== '',
32 isIp
= mw
.util
.isIPAddress( blocktarget
, true ),
33 isIpRange
= isIp
&& blocktarget
.match( /\/\d+$/ ),
34 isNonEmptyIp
= isIp
&& !isEmpty
,
35 expiryValue
= expiryWidget
.getValue(),
36 // infinityValues are the values the SpecialBlock class accepts as infinity (sf. wfIsInfinity)
37 infinityValues
= [ 'infinite', 'indefinite', 'infinity', 'never' ],
38 isIndefinite
= infinityValues
.indexOf( expiryValue
) !== -1,
39 // editingRestrictionWidget only exists if partial blocks is enabled; if not, block must be sitewide
40 editingRestrictionValue
= editingRestrictionWidget
? editingRestrictionWidget
.getValue() : 'sitewide',
41 editingIsSelected
= editingWidget
? editingWidget
.isSelected() : false,
42 isSitewide
= editingIsSelected
&& editingRestrictionValue
=== 'sitewide';
44 if ( enableAutoblockField
) {
45 enableAutoblockField
.toggle( !isNonEmptyIp
);
47 if ( hideUserField
) {
48 hideUserField
.toggle( !isNonEmptyIp
&& isIndefinite
&& isSitewide
);
49 if ( !hideUserField
.isVisible() ) {
50 hideUserWidget
.setSelected( false );
53 if ( anonOnlyField
) {
54 anonOnlyField
.toggle( isIp
|| isEmpty
);
56 if ( watchUserField
) {
57 watchUserField
.toggle( !isIpRange
|| isEmpty
);
59 if ( editingRestrictionWidget
) {
60 editingRestrictionWidget
.setDisabled( !editingIsSelected
);
62 if ( pageRestrictionsWidget
) {
63 pageRestrictionsWidget
.setDisabled( !editingIsSelected
|| isSitewide
);
65 if ( namespaceRestrictionsWidget
) {
66 namespaceRestrictionsWidget
.setDisabled( !editingIsSelected
|| isSitewide
);
68 if ( preventTalkPageEdit
&& namespaceRestrictionsWidget
) {
69 // This option is disabled for partial blocks unless a namespace restriction
70 // for the User_talk namespace is in place.
71 preventTalkPageEdit
.setDisabled(
73 editingRestrictionValue
=== 'partial' &&
74 namespaceRestrictionsWidget
.getValue().indexOf(
75 String( mw
.config
.get( 'wgNamespaceIds' ).user_talk
)
82 if ( blockTargetWidget
) {
83 // Bind functions so they're checked whenever stuff changes
84 blockTargetWidget
.on( 'change', updateBlockOptions
);
85 expiryWidget
.on( 'change', updateBlockOptions
);
86 if ( editingWidget
) {
87 editingWidget
.on( 'change', updateBlockOptions
);
89 if ( editingRestrictionWidget
) {
90 editingRestrictionWidget
.on( 'change', updateBlockOptions
);
92 if ( namespaceRestrictionsWidget
) {
93 namespaceRestrictionsWidget
.on( 'change', updateBlockOptions
);
96 // Call them now to set initial state (ie. Special:Block/Foobar?wpBlockExpiry=2+hours)