Debian Codegraph @

resources/src/mediawiki.action/mediawiki.action.protect.js

raw symbols SCIP
1 /*!
2 * JavaScript for the mediawiki.action.protect module
3 */
4 ( function () {
5 'use strict';
6
7 const cascadeableLevels = require( './config.json' ).CascadingRestrictionLevels;
8 const cascadeCheckbox = $( '#mwProtect-cascade' ).length &&
9 OO.ui.CheckboxInputWidget.static.infuse( $( '#mwProtect-cascade' ) );
10 const mwProtectUnchained = new OO.ui.CheckboxInputWidget( {
11 selected: false,
12 id: 'mwProtectUnchained'
13 } );
14 const levelSelectors = $( '[id ^= mwProtect-level-]' ).toArray()
15 .map( ( element ) => OO.ui.infuse( element ) );
16 const expiryInputs = $( '[id ^= mwProtect-][id $= -expires]' ).toArray()
17 .map( ( element ) => OO.ui.infuse( element ) );
18 const expirySelectors = $( '[id ^= mwProtectExpirySelection-]' ).toArray()
19 .map( ( element ) => OO.ui.infuse( element ) );
20 const chainedInputs = [ levelSelectors, expirySelectors, expiryInputs ];
21
22 /**
23 * Enable/disable protection selectors and expiry inputs
24 *
25 * @ignore
26 * @param {boolean} enable
27 */
28 function toggleUnchainedInputs( enable ) {
29 chainedInputs.forEach( ( widgets ) => {
30 widgets.slice( 1 ).forEach( ( widget ) => {
31 widget.setDisabled( !enable );
32 } );
33 } );
34 }
35
36 /**
37 * Are all actions protected at the same level, with the same expiry time?
38 *
39 * @ignore
40 * @return {boolean}
41 */
42 function areAllTypesMatching() {
43 return chainedInputs.every( ( widgets ) => widgets.every( ( widget ) => widget.getValue() === widgets[ 0 ].getValue() ) );
44 }
45
46 /**
47 * Is protection chaining off?
48 *
49 * @ignore
50 * @return {boolean}
51 */
52 function isUnchained() {
53 return mwProtectUnchained.isElementAttached() ? mwProtectUnchained.isSelected() : true;
54 }
55
56 /**
57 * Find the highest protection level in any selector
58 *
59 * @ignore
60 * @return {number}
61 */
62 function getMaxLevel() {
63 return Math.max.apply( Math, levelSelectors.map( ( widget ) => widget.dropdownWidget.getMenu().getItems().map( ( item ) => item.selected ).indexOf( true ) ) );
64 }
65
66 /**
67 * Set all protection level selectors to the same protection level
68 *
69 * @ignore
70 * @param {number|string} val Protection level index or value
71 */
72 function setAllSelectors( val ) {
73 levelSelectors.forEach( ( widget ) => {
74 if ( typeof val === 'number' ) {
75 widget.setValue( widget.dropdownWidget.getMenu().getItems()[ val ].getData() );
76 } else {
77 widget.setValue( val );
78 }
79 } );
80 }
81
82 /**
83 * Set the value of all widgets to the value of the first widget
84 *
85 * @ignore
86 * @param {OO.ui.Widget[]} widgets Array of widgets
87 */
88 function setAllToFirst( widgets ) {
89 widgets.slice( 1 ).forEach( ( widget ) => {
90 widget.setValue( widgets[ 0 ].getValue() );
91 } );
92 }
93
94 /**
95 * Enables or disables the cascade checkbox depending on the current selected levels
96 * Disables expiry inputs when there is not protection
97 *
98 * @ignore
99 * @return {boolean|undefined}
100 */
101 function updateCascadeAndExpire() {
102 levelSelectors.forEach( ( val, index ) => {
103 const disable = !val.getValue() || index && !isUnchained();
104 expirySelectors[ index ].setDisabled( disable );
105 expiryInputs[ index ].setDisabled( disable );
106 } );
107 if ( cascadeCheckbox ) {
108 levelSelectors.some( ( widget ) => {
109 if ( cascadeableLevels.indexOf( widget.getValue() ) === -1 ) {
110 cascadeCheckbox.setSelected( false ).setDisabled( true );
111 return true;
112 }
113 cascadeCheckbox.setDisabled( false );
114 return false;
115 } );
116 }
117 }
118
119 // Enable on inputs on submit
120 $( '#mw-Protect-Form' ).on( 'submit', () => {
121 chainedInputs.forEach( ( widgets ) => {
122 widgets.forEach( ( widget ) => {
123 widget.setDisabled( false );
124 } );
125 } );
126 } );
127
128 // Change value of chained selectors and expiry inputs
129 expirySelectors.forEach( ( widget ) => {
130 widget.on( 'change', ( val ) => {
131 if ( isUnchained() ) {
132 if ( val !== 'othertime' ) {
133 expiryInputs[ expirySelectors.indexOf( widget ) ].setValue( '' );
134 }
135 } else {
136 setAllToFirst( expirySelectors );
137 if ( val !== 'othertime' ) {
138 expiryInputs[ 0 ].setValue( '' );
139 setAllToFirst( expiryInputs );
140 }
141 }
142 } );
143 } );
144
145 // Change value of chained inputs and expiry selectors
146 expiryInputs.forEach( ( widget ) => {
147 widget.on( 'change', ( val ) => {
148 if ( isUnchained() ) {
149 if ( val ) {
150 expirySelectors[ expiryInputs.indexOf( widget ) ].setValue( 'othertime' );
151 }
152 } else {
153 setAllToFirst( expiryInputs );
154 if ( val ) {
155 expirySelectors[ 0 ].setValue( 'othertime' );
156 setAllToFirst( expirySelectors );
157 }
158 }
159 } );
160 } );
161
162 // Change value of chained level selectors and update cascade checkbox
163 levelSelectors.forEach( ( widget ) => {
164 widget.on( 'change', ( val ) => {
165 if ( !isUnchained() ) {
166 setAllSelectors( val );
167 }
168 updateCascadeAndExpire();
169 } );
170 } );
171
172 // If there is only one protection type, there is nothing to chain
173 if ( $( '.oo-ui-panelLayout-framed .oo-ui-panelLayout-framed' ).length > 1 ) {
174 mwProtectUnchained.on( 'change', () => {
175 toggleUnchainedInputs( isUnchained() );
176 if ( !isUnchained() ) {
177 setAllSelectors( getMaxLevel() );
178 setAllToFirst( expirySelectors );
179 setAllToFirst( expiryInputs );
180 }
181 updateCascadeAndExpire();
182 } ).setSelected( !areAllTypesMatching() );
183 $( '.oo-ui-panelLayout-framed .oo-ui-panelLayout-framed' ).first().after(
184 ( new OO.ui.FieldLayout( mwProtectUnchained, {
185 label: mw.msg( 'protect-unchain-permissions' ),
186 align: 'inline'
187 } ) ).$element
188 );
189 toggleUnchainedInputs( !areAllTypesMatching() );
190 }
191
192 const reasonList = OO.ui.infuse( $( '#wpProtectReasonSelection' ) );
193 const reason = OO.ui.infuse( $( '#mwProtect-reason' ) );
194
195 // Arbitrary 75 to leave some space for the autogenerated null edit's summary
196 mw.widgets.visibleCodePointLimitWithDropdown( reason, reasonList, mw.config.get( 'wgCommentCodePointLimit' ) - 75 );
197
198 updateCascadeAndExpire();
199 }() );