summaryrefslogtreecommitdiffstats
path: root/skins/common/protect.js
blob: e037f08a0efd103db15be11e6c06dda12b902527 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351

var ProtectionForm = {
	'existingMatch': false,

	/**
	 * Set up the protection chaining interface (i.e. "unlock move permissions" checkbox)
	 * on the protection form
	 *
	 * @param Object opts : parameters with members:
	 *     tableId              Identifier of the table containing UI bits
	 *     labelText            Text to use for the checkbox label
	 *     numTypes             The number of protection types
	 *     existingMatch        True if all the existing expiry times match
	 */
	'init': function( opts ) {
		if( !( document.createTextNode && document.getElementById && document.getElementsByTagName ) )
			return false;

		var box = document.getElementById( opts.tableId );
		if( !box )
			return false;
		
		var boxbody = box.getElementsByTagName('tbody')[0]
		var row = document.createElement( 'tr' );
		boxbody.insertBefore( row, boxbody.firstChild.nextSibling );

		this.existingMatch = opts.existingMatch;

		var cell = document.createElement( 'td' );
		row.appendChild( cell );
		// If there is only one protection type, there is nothing to chain
		if( opts.numTypes > 1 ) {
			var check = document.createElement( 'input' );
			check.id = 'mwProtectUnchained';
			check.type = 'checkbox';
			cell.appendChild( check );
			addClickHandler( check, function() { ProtectionForm.onChainClick(); } );

			cell.appendChild( document.createTextNode( ' ' ) );
			var label = document.createElement( 'label' );
			label.htmlFor = 'mwProtectUnchained';
			label.appendChild( document.createTextNode( opts.labelText ) );
			cell.appendChild( label );

			check.checked = !this.areAllTypesMatching();
			this.enableUnchainedInputs( check.checked );
		}

		this.updateCascadeCheckbox();

		return true;
	},

	/**
	 * Sets the disabled attribute on the cascade checkbox depending on the current selected levels
	 */
	'updateCascadeCheckbox': function() {
		// For non-existent titles, there is no cascade option
		if( !document.getElementById( 'mwProtect-cascade' ) ) {
			return;
		}
		var lists = this.getLevelSelectors();
		for( var i = 0; i < lists.length; i++ ) {
			if( lists[i].selectedIndex > -1 ) {
				var items = lists[i].getElementsByTagName( 'option' );
				var selected = items[ lists[i].selectedIndex ].value;
				if( !this.isCascadeableLevel(selected) ) {
					document.getElementById( 'mwProtect-cascade' ).checked = false;
					document.getElementById( 'mwProtect-cascade' ).disabled = true;
					return;
				}
			}
		}
		document.getElementById( 'mwProtect-cascade' ).disabled = false;
	},

	/**
	 * Is this protection level cascadeable?
	 * @param String level
	 *
	 * @return boolean
	 *
	 */
	'isCascadeableLevel': function( level ) {
		for (var k = 0; k < wgCascadeableLevels.length; k++) {
			if ( wgCascadeableLevels[k] == level ) {
				return true;
			}
		}
		return false;
	},

	/**
	 * When protection levels are locked together, update the rest
	 * when one action's level changes
	 *
	 * @param Element source Level selector that changed
	 */
	'updateLevels': function(source) {
		if( !this.isUnchained() )
			this.setAllSelectors( source.selectedIndex );
		this.updateCascadeCheckbox();
	},

	/**
	 * When protection levels are locked together, update the
	 * expiries when one changes
	 *
	 * @param Element source expiry input that changed
	 */

	'updateExpiry': function(source) {
		if( !this.isUnchained() ) {
			var expiry = source.value;
			this.forEachExpiryInput(function(element) {
				element.value = expiry;
			});
		}
		var listId = source.id.replace( /^mwProtect-(\w+)-expires$/, 'mwProtectExpirySelection-$1' );
		var list = document.getElementById( listId );
		if (list && list.value != 'othertime' ) {
			if ( this.isUnchained() ) {
				list.value = 'othertime';
			} else {
				this.forEachExpirySelector(function(element) {
					element.value = 'othertime';
				});
			}
		}
	},

	/**
	 * When protection levels are locked together, update the
	 * expiry lists when one changes and clear the custom inputs
	 *
	 * @param Element source expiry selector that changed
	 */
	'updateExpiryList': function(source) {
		if( !this.isUnchained() ) {
			var expiry = source.value;
			this.forEachExpirySelector(function(element) {
				element.value = expiry;
			});
			this.forEachExpiryInput(function(element) {
				element.value = '';
			});
		}
	},

	/**
	 * Update chain status and enable/disable various bits of the UI
	 * when the user changes the "unlock move permissions" checkbox
	 */
	'onChainClick': function() {
		if( this.isUnchained() ) {
			this.enableUnchainedInputs( true );
		} else {
			this.setAllSelectors( this.getMaxLevel() );
			this.enableUnchainedInputs( false );
		}
		this.updateCascadeCheckbox();
	},

	/**
	 * Returns true if the named attribute in all objects in the given array are matching
	 */
	'matchAttribute' : function( objects, attrName ) {
		var value = null;

		// Check levels
		for ( var i = 0; i < objects.length; i++ ) {
			var element = objects[i];
			if ( value == null ) {
				value = element[attrName];
			} else {
				if ( value != element[attrName] ) {
					return false;
				}
			}
		}
		return true;
	},

	/**
	 * Are all actions protected at the same level, with the same expiry time?
	 *
	 * @return boolean
	 */
	'areAllTypesMatching': function() {
		return this.existingMatch
			&& this.matchAttribute( this.getLevelSelectors(), 'selectedIndex' )
			&& this.matchAttribute( this.getExpirySelectors(), 'selectedIndex' )
			&& this.matchAttribute( this.getExpiryInputs(), 'value' );
	},

	/**
	 * Is protection chaining off?
	 *
	 * @return bool
	 */
	'isUnchained': function() {
		var element = document.getElementById( 'mwProtectUnchained' );
		return element
			? element.checked
			: true; // No control, so we need to let the user set both levels
	},

	/**
	 * Find the highest protection level in any selector
	 */
	'getMaxLevel': function() {
		var maxIndex = -1;
		this.forEachLevelSelector(function(element) {
			if (element.selectedIndex > maxIndex) {
				maxIndex = element.selectedIndex;
			}
		});
		return maxIndex;
	},

	/**
	 * Protect all actions at the specified level
	 *
	 * @param int index Protection level
	 */
	'setAllSelectors': function(index) {
		this.forEachLevelSelector(function(element) {
			if (element.selectedIndex != index) {
				element.selectedIndex = index;
			}
		});
	},

	/**
	 * Apply a callback to each protection selector
	 *
	 * @param callable func Callback function
	 */
	'forEachLevelSelector': function(func) {
		var selectors = this.getLevelSelectors();
		for (var i = 0; i < selectors.length; i++) {
			func(selectors[i]);
		}
	},

	/**
	 * Get a list of all protection selectors on the page
	 *
	 * @return Array
	 */
	'getLevelSelectors': function() {
		var all = document.getElementsByTagName("select");
		var ours = new Array();
		for (var i = 0; i < all.length; i++) {
			var element = all[i];
			if (element.id.match(/^mwProtect-level-/)) {
				ours[ours.length] = element;
			}
		}
		return ours;
	},

	/**
	 * Apply a callback to each expiry input
	 *
	 * @param callable func Callback function
	 */
	'forEachExpiryInput': function(func) {
		var inputs = this.getExpiryInputs();
		for (var i = 0; i < inputs.length; i++) {
			func(inputs[i]);
		}
	},

	/**
	 * Get a list of all expiry inputs on the page
	 *
	 * @return Array
	 */
	'getExpiryInputs': function() {
		var all = document.getElementsByTagName("input");
		var ours = new Array();
		for (var i = 0; i < all.length; i++) {
			var element = all[i];
			if (element.name.match(/^mwProtect-expiry-/)) {
				ours[ours.length] = element;
			}
		}
		return ours;
	},

	/**
	 * Apply a callback to each expiry selector list
	 * @param callable func Callback function
	 */
	'forEachExpirySelector': function(func) {
		var inputs = this.getExpirySelectors();
		for (var i = 0; i < inputs.length; i++) {
			func(inputs[i]);
		}
	},

	/**
	 * Get a list of all expiry selector lists on the page
	 *
	 * @return Array
	 */
	'getExpirySelectors': function() {
		var all = document.getElementsByTagName("select");
		var ours = new Array();
		for (var i = 0; i < all.length; i++) {
			var element = all[i];
			if (element.id.match(/^mwProtectExpirySelection-/)) {
				ours[ours.length] = element;
			}
		}
		return ours;
	},

	/**
	 * Enable/disable protection selectors and expiry inputs
	 *
	 * @param boolean val Enable?
	 */
	'enableUnchainedInputs': function(val) {
		var first = true;
		this.forEachLevelSelector(function(element) {
			if (first) {
				first = false;
			} else {
				element.disabled = !val;
			}
		});
		first = true;
		this.forEachExpiryInput(function(element) {
			if (first) {
				first = false;
			} else {
				element.disabled = !val;
			}
		});
		first = true;
		this.forEachExpirySelector(function(element) {
			if (first) {
				first = false;
			} else {
				element.disabled = !val;
			}
		});
	}
}