summaryrefslogtreecommitdiff
path: root/resources/src/mediawiki.legacy/protect.js
blob: 3f4b263efdbf7c7769ca355ac42d76aca4073242 (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
( function ( mw, $ ) {

var ProtectionForm = window.ProtectionForm = {
	/**
	 * Set up the protection chaining interface (i.e. "unlock move permissions" checkbox)
	 * on the protection form
	 */
	init: function () {
		var $cell = $( '<td>' ),
			$row = $( '<tr>' ).append( $cell );

		if ( !$( '#mwProtectSet' ).length ) {
			return false;
		}

		if ( mw.config.get( 'wgCascadeableLevels' ) !== undefined ) {
			$( 'form#mw-Protect-Form' ).submit( this.toggleUnchainedInputs.bind( ProtectionForm, true ) );
		}
		this.getExpirySelectors().each( function () {
			$( this ).change( ProtectionForm.updateExpiryList.bind( ProtectionForm, this ) );
		} );
		this.getExpiryInputs().each( function () {
			$( this ).on( 'keyup change', ProtectionForm.updateExpiry.bind( ProtectionForm, this ) );
		} );
		this.getLevelSelectors().each( function () {
			$( this ).change( ProtectionForm.updateLevels.bind( ProtectionForm, this ) );
		} );

		$( '#mwProtectSet > tbody > tr:first' ).after( $row );

		// If there is only one protection type, there is nothing to chain
		if ( $( '[id ^= mw-protect-table-]' ).length > 1 ) {
			$cell.append(
				$( '<input>' )
					.attr( { id: 'mwProtectUnchained', type: 'checkbox' } )
					.click( this.onChainClick.bind( this ) )
					.prop( 'checked', !this.areAllTypesMatching() ),
				document.createTextNode( ' ' ),
				$( '<label>' )
					.attr( 'for', 'mwProtectUnchained' )
					.text( mw.msg( 'protect-unchain-permissions' ) )
			);

			this.toggleUnchainedInputs( !this.areAllTypesMatching() );
		}

		$( '#mwProtect-reason' ).byteLimit( 180 );

		this.updateCascadeCheckbox();
	},

	/**
	 * Sets the disabled attribute on the cascade checkbox depending on the current selected levels
	 */
	updateCascadeCheckbox: function () {
		this.getLevelSelectors().each( function () {
			if ( !ProtectionForm.isCascadeableLevel( $( this ).val() ) ) {
				$( '#mwProtect-cascade' ).prop( { checked: false, disabled: true } );
				return false;
			} else {
				$( '#mwProtect-cascade' ).prop( 'disabled', false );
			}
		} );
	},

	/**
	 * Checks if a certain protection level is cascadeable.
	 *
	 * @param {string} level
	 * @return {boolean}
	 */
	isCascadeableLevel: function ( level ) {
		return $.inArray( level, mw.config.get( 'wgCascadeableLevels' ) ) !== -1;
	},

	/**
	 * 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() ) {
			this.getExpiryInputs().each( function () {
				this.value = source.value;
			} );
		}
		if ( this.isUnchained() ) {
			$( '#' + source.id.replace( /^mwProtect-(\w+)-expires$/, 'mwProtectExpirySelection-$1' ) ).val( 'othertime' );
		} else {
			this.getExpirySelectors().each( function () {
				this.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() ) {
			this.getExpirySelectors().each( function () {
				this.value = source.value;
			} );
			this.getExpiryInputs().each( function () {
				this.value = '';
			} );
		}
	},

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

	/**
	 * Returns true if the named attribute in all objects in the given array are matching
	 *
	 * @param {Object[]} objects
	 * @param {string} attrName
	 * @return {boolean}
	 */
	matchAttribute: function ( objects, attrName ) {
		return $.map( objects, function ( object ) {
			return object[attrName];
		} ).filter( function ( item, index, a ) {
			return index === a.indexOf( item );
		} ).length === 1;
	},

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

	/**
	 * Is protection chaining off?
	 *
	 * @return {boolean}
	 */
	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
	 * @return {number}
	 */
	getMaxLevel: function () {
		return Math.max.apply( Math, this.getLevelSelectors().map( function () {
			return this.selectedIndex;
		} ) );
	},

	/**
	 * Protect all actions at the specified level
	 *
	 * @param {number} index Protection level
	 */
	setAllSelectors: function ( index ) {
		this.getLevelSelectors().each( function () {
			this.selectedIndex = index;
		} );
	},

	/**
	 * Get a list of all protection selectors on the page
	 *
	 * @return {jQuery}
	 */
	getLevelSelectors: function () {
		return $( 'select[id ^= mwProtect-level-]' );
	},

	/**
	 * Get a list of all expiry inputs on the page
	 *
	 * @return {jQuery}
	 */
	getExpiryInputs: function () {
		return $( 'input[id ^= mwProtect-][id $= -expires]' );
	},

	/**
	 * Get a list of all expiry selector lists on the page
	 *
	 * @return {jQuery}
	 */
	getExpirySelectors: function () {
		return $( 'select[id ^= mwProtectExpirySelection-]' );
	},

	/**
	 * Enable/disable protection selectors and expiry inputs
	 *
	 * @param {boolean} val Enable?
	 */
	toggleUnchainedInputs: function ( val ) {
		var setDisabled = function () { this.disabled = !val; };
		this.getLevelSelectors().slice( 1 ).each( setDisabled );
		this.getExpiryInputs().slice( 1 ).each( setDisabled );
		this.getExpirySelectors().slice( 1 ).each( setDisabled );
	}
};

$( ProtectionForm.init.bind( ProtectionForm ) );

}( mediaWiki, jQuery ) );