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 var blockTargetWidget
, anonOnlyWidget
, enableAutoblockWidget
, hideUserWidget
, watchUserWidget
,
15 expiryWidget
, editingWidget
, editingRestrictionWidget
, preventTalkPageEditWidget
,
16 pageRestrictionsWidget
, namespaceRestrictionsWidget
, createAccountWidget
, data
,
17 enablePartialBlocks
, blockAllowsUTEdit
, userChangedCreateAccount
, updatingBlockOptions
;
19 function updateBlockOptions() {
20 var blocktarget
= blockTargetWidget
.getValue().trim(),
21 isEmpty
= blocktarget
=== '',
22 isIp
= mw
.util
.isIPAddress( blocktarget
, true ),
23 isIpRange
= isIp
&& blocktarget
.match( /\/\d+$/ ),
24 isNonEmptyIp
= isIp
&& !isEmpty
,
25 expiryValue
= expiryWidget
.getValue(),
26 // infinityValues are the values the SpecialBlock class accepts as infinity (sf. wfIsInfinity)
27 infinityValues
= [ 'infinite', 'indefinite', 'infinity', 'never' ],
28 isIndefinite
= infinityValues
.indexOf( expiryValue
) !== -1,
29 editingRestrictionValue
= enablePartialBlocks
? editingRestrictionWidget
.getValue() : 'sitewide',
30 editingIsSelected
= editingWidget
.isSelected(),
31 isSitewide
= editingIsSelected
&& editingRestrictionValue
=== 'sitewide';
33 enableAutoblockWidget
.setDisabled( isNonEmptyIp
);
34 if ( enableAutoblockWidget
.isDisabled() ) {
35 enableAutoblockWidget
.setSelected( false );
38 anonOnlyWidget
.setDisabled( !isIp
&& !isEmpty
);
39 if ( anonOnlyWidget
.isDisabled() ) {
40 anonOnlyWidget
.setSelected( false );
43 if ( hideUserWidget
) {
44 hideUserWidget
.setDisabled( isNonEmptyIp
|| !isIndefinite
|| !isSitewide
);
45 if ( hideUserWidget
.isDisabled() ) {
46 hideUserWidget
.setSelected( false );
50 if ( watchUserWidget
) {
51 watchUserWidget
.setDisabled( isIpRange
&& !isEmpty
);
52 if ( watchUserWidget
.isDisabled() ) {
53 watchUserWidget
.setSelected( false );
57 if ( enablePartialBlocks
) {
58 editingRestrictionWidget
.setDisabled( !editingIsSelected
);
59 pageRestrictionsWidget
.setDisabled( !editingIsSelected
|| isSitewide
);
60 namespaceRestrictionsWidget
.setDisabled( !editingIsSelected
|| isSitewide
);
61 if ( blockAllowsUTEdit
) {
62 // This option is disabled for partial blocks unless a namespace restriction
63 // for the User_talk namespace is in place.
64 preventTalkPageEditWidget
.setDisabled(
66 editingRestrictionValue
=== 'partial' &&
67 namespaceRestrictionsWidget
.getValue().indexOf(
68 String( mw
.config
.get( 'wgNamespaceIds' ).user_talk
)
74 if ( !userChangedCreateAccount
) {
75 updatingBlockOptions
= true;
76 createAccountWidget
.setSelected( isSitewide
);
77 updatingBlockOptions
= false;
82 // This code is also loaded on the "block succeeded" page where there is no form,
83 // so check for block target widget; if it exists, the form is present
84 blockTargetWidget
= infuseIfExists( $( '#mw-bi-target' ) );
86 if ( blockTargetWidget
) {
87 data
= require( './config.json' );
88 enablePartialBlocks
= data
.EnablePartialBlocks
;
89 blockAllowsUTEdit
= data
.BlockAllowsUTEdit
;
90 userChangedCreateAccount
= mw
.config
.get( 'wgCreateAccountDirty' );
91 updatingBlockOptions
= false;
93 // Always present if blockTargetWidget is present
94 editingWidget
= OO
.ui
.infuse( $( '#mw-input-wpEditing' ) );
95 expiryWidget
= OO
.ui
.infuse( $( '#mw-input-wpExpiry' ) );
96 createAccountWidget
= OO
.ui
.infuse( $( '#mw-input-wpCreateAccount' ) );
97 enableAutoblockWidget
= OO
.ui
.infuse( $( '#mw-input-wpAutoBlock' ) );
98 anonOnlyWidget
= OO
.ui
.infuse( $( '#mw-input-wpHardBlock' ) );
99 blockTargetWidget
.on( 'change', updateBlockOptions
);
100 editingWidget
.on( 'change', updateBlockOptions
);
101 expiryWidget
.on( 'change', updateBlockOptions
);
102 createAccountWidget
.on( 'change', function () {
103 if ( !updatingBlockOptions
) {
104 userChangedCreateAccount
= true;
108 // Present for certain rights
109 watchUserWidget
= infuseIfExists( $( '#mw-input-wpWatch' ) );
110 hideUserWidget
= infuseIfExists( $( '#mw-input-wpHideUser' ) );
112 // Present for certain global configs
113 if ( enablePartialBlocks
) {
114 editingRestrictionWidget
= OO
.ui
.infuse( $( '#mw-input-wpEditingRestriction' ) );
115 pageRestrictionsWidget
= OO
.ui
.infuse( $( '#mw-input-wpPageRestrictions' ) );
116 namespaceRestrictionsWidget
= OO
.ui
.infuse( $( '#mw-input-wpNamespaceRestrictions' ) );
117 editingRestrictionWidget
.on( 'change', updateBlockOptions
);
118 namespaceRestrictionsWidget
.on( 'change', updateBlockOptions
);
120 if ( blockAllowsUTEdit
) {
121 preventTalkPageEditWidget
= infuseIfExists( $( '#mw-input-wpDisableUTEdit' ) );
124 updateBlockOptions();