Merge "ApiSandbox: Move labels outside progress bars"
[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, anonOnlyWidget, enableAutoblockWidget, hideUserWidget, watchUserWidget,
15 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 enableAutoblockWidget.setDisabled( isNonEmptyIp );
34 if ( enableAutoblockWidget.isDisabled() ) {
35 enableAutoblockWidget.setSelected( false );
36 }
37
38 anonOnlyWidget.setDisabled( !isIp && !isEmpty );
39 if ( anonOnlyWidget.isDisabled() ) {
40 anonOnlyWidget.setSelected( false );
41 }
42
43 if ( hideUserWidget ) {
44 hideUserWidget.setDisabled( isNonEmptyIp || !isIndefinite || !isSitewide );
45 if ( hideUserWidget.isDisabled() ) {
46 hideUserWidget.setSelected( false );
47 }
48 }
49
50 if ( watchUserWidget ) {
51 watchUserWidget.setDisabled( isIpRange && !isEmpty );
52 if ( watchUserWidget.isDisabled() ) {
53 watchUserWidget.setSelected( false );
54 }
55 }
56
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(
65 editingIsSelected &&
66 editingRestrictionValue === 'partial' &&
67 namespaceRestrictionsWidget.getValue().indexOf(
68 String( mw.config.get( 'wgNamespaceIds' ).user_talk )
69 ) === -1
70 );
71 }
72 }
73
74 if ( !userChangedCreateAccount ) {
75 updatingBlockOptions = true;
76 createAccountWidget.setSelected( isSitewide );
77 updatingBlockOptions = false;
78 }
79
80 }
81
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' ) );
85
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;
92
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;
105 }
106 } );
107
108 // Present for certain rights
109 watchUserWidget = infuseIfExists( $( '#mw-input-wpWatch' ) );
110 hideUserWidget = infuseIfExists( $( '#mw-input-wpHideUser' ) );
111
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 );
119 }
120 if ( blockAllowsUTEdit ) {
121 preventTalkPageEditWidget = infuseIfExists( $( '#mw-input-wpDisableUTEdit' ) );
122 }
123
124 updateBlockOptions();
125 }
126 } );
127 }() );