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' ) ),
28 createAccountWidget
= infuseIfExists( $( '#mw-input-wpCreateAccount' ) ),
29 userChangedCreateAccount
= mw
.config
.get( 'wgCreateAccountDirty' ),
30 updatingBlockOptions
= false;
32 function updateBlockOptions() {
33 var blocktarget
= blockTargetWidget
.getValue().trim(),
34 isEmpty
= blocktarget
=== '',
35 isIp
= mw
.util
.isIPAddress( blocktarget
, true ),
36 isIpRange
= isIp
&& blocktarget
.match( /\/\d+$/ ),
37 isNonEmptyIp
= isIp
&& !isEmpty
,
38 expiryValue
= expiryWidget
.getValue(),
39 // infinityValues are the values the SpecialBlock class accepts as infinity (sf. wfIsInfinity)
40 infinityValues
= [ 'infinite', 'indefinite', 'infinity', 'never' ],
41 isIndefinite
= infinityValues
.indexOf( expiryValue
) !== -1,
42 // editingRestrictionWidget only exists if partial blocks is enabled; if not, block must be sitewide
43 editingRestrictionValue
= editingRestrictionWidget
? editingRestrictionWidget
.getValue() : 'sitewide',
44 editingIsSelected
= editingWidget
? editingWidget
.isSelected() : false,
45 isSitewide
= editingIsSelected
&& editingRestrictionValue
=== 'sitewide';
47 if ( enableAutoblockField
) {
48 enableAutoblockField
.toggle( !isNonEmptyIp
);
50 if ( hideUserField
) {
51 hideUserField
.toggle( !isNonEmptyIp
&& isIndefinite
&& isSitewide
);
52 if ( !hideUserField
.isVisible() ) {
53 hideUserWidget
.setSelected( false );
56 if ( anonOnlyField
) {
57 anonOnlyField
.toggle( isIp
|| isEmpty
);
59 if ( watchUserField
) {
60 watchUserField
.toggle( !isIpRange
|| isEmpty
);
62 if ( editingRestrictionWidget
) {
63 editingRestrictionWidget
.setDisabled( !editingIsSelected
);
65 if ( pageRestrictionsWidget
) {
66 pageRestrictionsWidget
.setDisabled( !editingIsSelected
|| isSitewide
);
68 if ( namespaceRestrictionsWidget
) {
69 namespaceRestrictionsWidget
.setDisabled( !editingIsSelected
|| isSitewide
);
71 if ( preventTalkPageEdit
&& namespaceRestrictionsWidget
) {
72 // This option is disabled for partial blocks unless a namespace restriction
73 // for the User_talk namespace is in place.
74 preventTalkPageEdit
.setDisabled(
76 editingRestrictionValue
=== 'partial' &&
77 namespaceRestrictionsWidget
.getValue().indexOf(
78 String( mw
.config
.get( 'wgNamespaceIds' ).user_talk
)
82 if ( !userChangedCreateAccount
) {
83 updatingBlockOptions
= true;
84 createAccountWidget
.setSelected( isSitewide
);
85 updatingBlockOptions
= false;
90 if ( blockTargetWidget
) {
91 // Bind functions so they're checked whenever stuff changes
92 blockTargetWidget
.on( 'change', updateBlockOptions
);
93 expiryWidget
.on( 'change', updateBlockOptions
);
94 if ( editingWidget
) {
95 editingWidget
.on( 'change', updateBlockOptions
);
97 if ( editingRestrictionWidget
) {
98 editingRestrictionWidget
.on( 'change', updateBlockOptions
);
100 if ( namespaceRestrictionsWidget
) {
101 namespaceRestrictionsWidget
.on( 'change', updateBlockOptions
);
104 createAccountWidget
.on( 'change', function () {
105 if ( !updatingBlockOptions
) {
106 userChangedCreateAccount
= true;
110 // Call them now to set initial state (ie. Special:Block/Foobar?wpBlockExpiry=2+hours)
111 updateBlockOptions();