Merge "maintenance: Script to rename titles for Unicode uppercasing changes"
[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 // Disable for partial blocks, unless the block is against the User_talk namespace
63 preventTalkPageEditWidget.setDisabled(
64 // Partial block that doesn't block editing
65 !editingIsSelected ||
66 // Partial block that blocks editing and doesn't block the User_talk namespace
67 (
68 editingRestrictionValue === 'partial' &&
69 namespaceRestrictionsWidget.getValue().indexOf(
70 String( mw.config.get( 'wgNamespaceIds' ).user_talk )
71 ) === -1
72 )
73 );
74 }
75 }
76
77 if ( !userChangedCreateAccount ) {
78 updatingBlockOptions = true;
79 createAccountWidget.setSelected( isSitewide );
80 updatingBlockOptions = false;
81 }
82
83 }
84
85 // This code is also loaded on the "block succeeded" page where there is no form,
86 // so check for block target widget; if it exists, the form is present
87 blockTargetWidget = infuseIfExists( $( '#mw-bi-target' ) );
88
89 if ( blockTargetWidget ) {
90 data = require( './config.json' );
91 enablePartialBlocks = data.EnablePartialBlocks;
92 blockAllowsUTEdit = data.BlockAllowsUTEdit;
93 userChangedCreateAccount = mw.config.get( 'wgCreateAccountDirty' );
94 updatingBlockOptions = false;
95
96 // Always present if blockTargetWidget is present
97 editingWidget = OO.ui.infuse( $( '#mw-input-wpEditing' ) );
98 expiryWidget = OO.ui.infuse( $( '#mw-input-wpExpiry' ) );
99 createAccountWidget = OO.ui.infuse( $( '#mw-input-wpCreateAccount' ) );
100 enableAutoblockWidget = OO.ui.infuse( $( '#mw-input-wpAutoBlock' ) );
101 anonOnlyWidget = OO.ui.infuse( $( '#mw-input-wpHardBlock' ) );
102 blockTargetWidget.on( 'change', updateBlockOptions );
103 editingWidget.on( 'change', updateBlockOptions );
104 expiryWidget.on( 'change', updateBlockOptions );
105 createAccountWidget.on( 'change', function () {
106 if ( !updatingBlockOptions ) {
107 userChangedCreateAccount = true;
108 }
109 } );
110
111 // Present for certain rights
112 watchUserWidget = infuseIfExists( $( '#mw-input-wpWatch' ) );
113 hideUserWidget = infuseIfExists( $( '#mw-input-wpHideUser' ) );
114
115 // Present for certain global configs
116 if ( enablePartialBlocks ) {
117 editingRestrictionWidget = OO.ui.infuse( $( '#mw-input-wpEditingRestriction' ) );
118 pageRestrictionsWidget = OO.ui.infuse( $( '#mw-input-wpPageRestrictions' ) );
119 namespaceRestrictionsWidget = OO.ui.infuse( $( '#mw-input-wpNamespaceRestrictions' ) );
120 editingRestrictionWidget.on( 'change', updateBlockOptions );
121 namespaceRestrictionsWidget.on( 'change', updateBlockOptions );
122 }
123 if ( blockAllowsUTEdit ) {
124 preventTalkPageEditWidget = infuseIfExists( $( '#mw-input-wpDisableUTEdit' ) );
125 }
126
127 updateBlockOptions();
128 }
129 } );
130 }() );