Simplify checking for widgets on special block page
[lhc/web/wiklou.git] / resources / src / mediawiki.special.block.js
1 /*!
2 * JavaScript for Special:Block
3 */
4 ( function () {
5 // Like OO.ui.infuse(), but if the element doesn't exist, return null instead of throwing an exception.
6 function infuseIfExists( $el ) {
7 if ( !$el.length ) {
8 return null;
9 }
10 return OO.ui.infuse( $el );
11 }
12
13 $( function () {
14 var blockTargetWidget, anonOnlyField, enableAutoblockField, hideUserWidget, hideUserField,
15 watchUserField, expiryWidget, editingWidget, editingRestrictionWidget, preventTalkPageEditWidget,
16 pageRestrictionsWidget, namespaceRestrictionsWidget, createAccountWidget, data,
17 enablePartialBlocks, blockAllowsUTEdit, userChangedCreateAccount, updatingBlockOptions;
18
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';
32
33 enableAutoblockField.toggle( !isNonEmptyIp );
34 anonOnlyField.toggle( isIp || isEmpty );
35
36 if ( hideUserField ) {
37 hideUserField.toggle( !isNonEmptyIp && isIndefinite && isSitewide );
38 if ( !hideUserField.isVisible() ) {
39 hideUserWidget.setSelected( false );
40 }
41 }
42
43 if ( watchUserField ) {
44 watchUserField.toggle( !isIpRange || isEmpty );
45 }
46
47 if ( enablePartialBlocks ) {
48 editingRestrictionWidget.setDisabled( !editingIsSelected );
49 pageRestrictionsWidget.setDisabled( !editingIsSelected || isSitewide );
50 namespaceRestrictionsWidget.setDisabled( !editingIsSelected || isSitewide );
51 if ( blockAllowsUTEdit ) {
52 // This option is disabled for partial blocks unless a namespace restriction
53 // for the User_talk namespace is in place.
54 preventTalkPageEditWidget.setDisabled(
55 editingIsSelected &&
56 editingRestrictionValue === 'partial' &&
57 namespaceRestrictionsWidget.getValue().indexOf(
58 String( mw.config.get( 'wgNamespaceIds' ).user_talk )
59 ) === -1
60 );
61 }
62 }
63
64 if ( !userChangedCreateAccount ) {
65 updatingBlockOptions = true;
66 createAccountWidget.setSelected( isSitewide );
67 updatingBlockOptions = false;
68 }
69
70 }
71
72 // This code is also loaded on the "block succeeded" page where there is no form,
73 // so check for block target widget; if it exists, the form is present
74 blockTargetWidget = infuseIfExists( $( '#mw-bi-target' ) );
75
76 if ( blockTargetWidget ) {
77 data = require( './config.json' );
78 enablePartialBlocks = data.EnablePartialBlocks;
79 blockAllowsUTEdit = data.BlockAllowsUTEdit;
80 userChangedCreateAccount = mw.config.get( 'wgCreateAccountDirty' );
81 updatingBlockOptions = false;
82
83 // Always present if blockTargetWidget is present
84 editingWidget = OO.ui.infuse( $( '#mw-input-wpEditing' ) );
85 expiryWidget = OO.ui.infuse( $( '#mw-input-wpExpiry' ) );
86 createAccountWidget = OO.ui.infuse( $( '#mw-input-wpCreateAccount' ) );
87 enableAutoblockField = OO.ui.infuse( $( '#mw-input-wpAutoBlock' ).closest( '.oo-ui-fieldLayout' ) );
88 anonOnlyField = OO.ui.infuse( $( '#mw-input-wpHardBlock' ).closest( '.oo-ui-fieldLayout' ) );
89 blockTargetWidget.on( 'change', updateBlockOptions );
90 editingWidget.on( 'change', updateBlockOptions );
91 expiryWidget.on( 'change', updateBlockOptions );
92 createAccountWidget.on( 'change', function () {
93 if ( !updatingBlockOptions ) {
94 userChangedCreateAccount = true;
95 }
96 } );
97
98 // Present for certain rights
99 watchUserField = infuseIfExists( $( '#mw-input-wpWatch' ).closest( '.oo-ui-fieldLayout' ) );
100 hideUserField = infuseIfExists( $( '#mw-input-wpHideUser' ).closest( '.oo-ui-fieldLayout' ) );
101 hideUserWidget = infuseIfExists( $( '#mw-input-wpHideUser' ) );
102
103 // Present for certain global configs
104 if ( enablePartialBlocks ) {
105 editingRestrictionWidget = OO.ui.infuse( $( '#mw-input-wpEditingRestriction' ) );
106 pageRestrictionsWidget = OO.ui.infuse( $( '#mw-input-wpPageRestrictions' ) );
107 namespaceRestrictionsWidget = OO.ui.infuse( $( '#mw-input-wpNamespaceRestrictions' ) );
108 editingRestrictionWidget.on( 'change', updateBlockOptions );
109 namespaceRestrictionsWidget.on( 'change', updateBlockOptions );
110 }
111 if ( blockAllowsUTEdit ) {
112 preventTalkPageEditWidget = infuseIfExists( $( '#mw-input-wpDisableUTEdit' ) );
113 }
114
115 updateBlockOptions();
116 }
117 } );
118 }() );