summaryrefslogtreecommitdiff
path: root/www/wiki/resources/src/mediawiki.rcfilters/dm/mw.rcfilters.dm.FilterItem.js
blob: d7f7b02111fb978cdc926617361ddaaeebf3a9e7 (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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
( function ( mw ) {
	/**
	 * Filter item model
	 *
	 * @extends mw.rcfilters.dm.ItemModel
	 *
	 * @constructor
	 * @param {string} param Filter param name
	 * @param {mw.rcfilters.dm.FilterGroup} groupModel Filter group model
	 * @param {Object} config Configuration object
	 * @cfg {string[]} [excludes=[]] A list of filter names this filter, if
	 *  selected, makes inactive.
	 * @cfg {string[]} [subset] Defining the names of filters that are a subset of this filter
	 * @cfg {Object} [conflicts] Defines the conflicts for this filter
	 * @cfg {boolean} [visible=true] The visibility of the group
	 */
	mw.rcfilters.dm.FilterItem = function MwRcfiltersDmFilterItem( param, groupModel, config ) {
		config = config || {};

		this.groupModel = groupModel;

		// Parent
		mw.rcfilters.dm.FilterItem.parent.call( this, param, $.extend( {
			namePrefix: this.groupModel.getNamePrefix()
		}, config ) );
		// Mixin constructor
		OO.EventEmitter.call( this );

		// Interaction definitions
		this.subset = config.subset || [];
		this.conflicts = config.conflicts || {};
		this.superset = [];
		this.visible = config.visible === undefined ? true : !!config.visible;

		// Interaction states
		this.included = false;
		this.conflicted = false;
		this.fullyCovered = false;
	};

	/* Initialization */

	OO.inheritClass( mw.rcfilters.dm.FilterItem, mw.rcfilters.dm.ItemModel );

	/* Methods */

	/**
	 * Return the representation of the state of this item.
	 *
	 * @return {Object} State of the object
	 */
	mw.rcfilters.dm.FilterItem.prototype.getState = function () {
		return {
			selected: this.isSelected(),
			included: this.isIncluded(),
			conflicted: this.isConflicted(),
			fullyCovered: this.isFullyCovered()
		};
	};

	/**
	 * Get the message for the display area for the currently active conflict
	 *
	 * @private
	 * @return {string} Conflict result message key
	 */
	mw.rcfilters.dm.FilterItem.prototype.getCurrentConflictResultMessage = function () {
		var details = {};

		// First look in filter's own conflicts
		details = this.getConflictDetails( this.getOwnConflicts(), 'globalDescription' );
		if ( !details.message ) {
			// Fall back onto conflicts in the group
			details = this.getConflictDetails( this.getGroupModel().getConflicts(), 'globalDescription' );
		}

		return details.message;
	};

	/**
	 * Get the details of the active conflict on this filter
	 *
	 * @private
	 * @param {Object} conflicts Conflicts to examine
	 * @param {string} [key='contextDescription'] Message key
	 * @return {Object} Object with conflict message and conflict items
	 * @return {string} return.message Conflict message
	 * @return {string[]} return.names Conflicting item labels
	 */
	mw.rcfilters.dm.FilterItem.prototype.getConflictDetails = function ( conflicts, key ) {
		var group,
			conflictMessage = '',
			itemLabels = [];

		key = key || 'contextDescription';

		$.each( conflicts, function ( filterName, conflict ) {
			if ( !conflict.item.isSelected() ) {
				return;
			}

			if ( !conflictMessage ) {
				conflictMessage = conflict[ key ];
				group = conflict.group;
			}

			if ( group === conflict.group ) {
				itemLabels.push( mw.msg( 'quotation-marks', conflict.item.getLabel() ) );
			}
		} );

		return {
			message: conflictMessage,
			names: itemLabels
		};

	};

	/**
	 * @inheritdoc
	 */
	mw.rcfilters.dm.FilterItem.prototype.getStateMessage = function () {
		var messageKey, details, superset,
			affectingItems = [];

		if ( this.isSelected() ) {
			if ( this.isConflicted() ) {
				// First look in filter's own conflicts
				details = this.getConflictDetails( this.getOwnConflicts() );
				if ( !details.message ) {
					// Fall back onto conflicts in the group
					details = this.getConflictDetails( this.getGroupModel().getConflicts() );
				}

				messageKey = details.message;
				affectingItems = details.names;
			} else if ( this.isIncluded() && !this.isHighlighted() ) {
				// We only show the 'no effect' full-coverage message
				// if the item is also not highlighted. See T161273
				superset = this.getSuperset();
				// For this message we need to collect the affecting superset
				affectingItems = this.getGroupModel().findSelectedItems( this )
					.filter( function ( item ) {
						return superset.indexOf( item.getName() ) !== -1;
					} )
					.map( function ( item ) {
						return mw.msg( 'quotation-marks', item.getLabel() );
					} );

				messageKey = 'rcfilters-state-message-subset';
			} else if ( this.isFullyCovered() && !this.isHighlighted() ) {
				affectingItems = this.getGroupModel().findSelectedItems( this )
					.map( function ( item ) {
						return mw.msg( 'quotation-marks', item.getLabel() );
					} );

				messageKey = 'rcfilters-state-message-fullcoverage';
			}
		}

		if ( messageKey ) {
			// Build message
			return mw.msg(
				messageKey,
				mw.language.listToText( affectingItems ),
				affectingItems.length
			);
		}

		// Display description
		return this.getDescription();
	};

	/**
	 * Get the model of the group this filter belongs to
	 *
	 * @return {mw.rcfilters.dm.FilterGroup} Filter group model
	 */
	mw.rcfilters.dm.FilterItem.prototype.getGroupModel = function () {
		return this.groupModel;
	};

	/**
	 * Get the group name this filter belongs to
	 *
	 * @return {string} Filter group name
	 */
	mw.rcfilters.dm.FilterItem.prototype.getGroupName = function () {
		return this.groupModel.getName();
	};

	/**
	 * Get filter subset
	 * This is a list of filter names that are defined to be included
	 * when this filter is selected.
	 *
	 * @return {string[]} Filter subset
	 */
	mw.rcfilters.dm.FilterItem.prototype.getSubset = function () {
		return this.subset;
	};

	/**
	 * Get filter superset
	 * This is a generated list of filters that define this filter
	 * to be included when either of them is selected.
	 *
	 * @return {string[]} Filter superset
	 */
	mw.rcfilters.dm.FilterItem.prototype.getSuperset = function () {
		return this.superset;
	};

	/**
	 * Check whether the filter is currently in a conflict state
	 *
	 * @return {boolean} Filter is in conflict state
	 */
	mw.rcfilters.dm.FilterItem.prototype.isConflicted = function () {
		return this.conflicted;
	};

	/**
	 * Check whether the filter is currently in an already included subset
	 *
	 * @return {boolean} Filter is in an already-included subset
	 */
	mw.rcfilters.dm.FilterItem.prototype.isIncluded = function () {
		return this.included;
	};

	/**
	 * Check whether the filter is currently fully covered
	 *
	 * @return {boolean} Filter is in fully-covered state
	 */
	mw.rcfilters.dm.FilterItem.prototype.isFullyCovered = function () {
		return this.fullyCovered;
	};

	/**
	 * Get all conflicts associated with this filter or its group
	 *
	 * Conflict object is set up by filter name keys and conflict
	 * definition. For example:
	 * 		{
	 * 			filterName: {
	 * 				filter: filterName,
	 * 				group: group1,
	 * 				label: itemLabel,
	 * 				item: itemModel
	 * 			}
	 * 			filterName2: {
	 * 				filter: filterName2,
	 * 				group: group2
	 * 				label: itemLabel2,
	 * 				item: itemModel2
	 * 			}
	 * 		}
	 *
	 * @return {Object} Filter conflicts
	 */
	mw.rcfilters.dm.FilterItem.prototype.getConflicts = function () {
		return $.extend( {}, this.conflicts, this.getGroupModel().getConflicts() );
	};

	/**
	 * Get the conflicts associated with this filter
	 *
	 * @return {Object} Filter conflicts
	 */
	mw.rcfilters.dm.FilterItem.prototype.getOwnConflicts = function () {
		return this.conflicts;
	};

	/**
	 * Set conflicts for this filter. See #getConflicts for the expected
	 * structure of the definition.
	 *
	 * @param {Object} conflicts Conflicts for this filter
	 */
	mw.rcfilters.dm.FilterItem.prototype.setConflicts = function ( conflicts ) {
		this.conflicts = conflicts || {};
	};

	/**
	 * Set filter superset
	 *
	 * @param {string[]} superset Filter superset
	 */
	mw.rcfilters.dm.FilterItem.prototype.setSuperset = function ( superset ) {
		this.superset = superset || [];
	};

	/**
	 * Set filter subset
	 *
	 * @param {string[]} subset Filter subset
	 */
	mw.rcfilters.dm.FilterItem.prototype.setSubset = function ( subset ) {
		this.subset = subset || [];
	};

	/**
	 * Check whether a filter exists in the subset list for this filter
	 *
	 * @param {string} filterName Filter name
	 * @return {boolean} Filter name is in the subset list
	 */
	mw.rcfilters.dm.FilterItem.prototype.existsInSubset = function ( filterName ) {
		return this.subset.indexOf( filterName ) > -1;
	};

	/**
	 * Check whether this item has a potential conflict with the given item
	 *
	 * This checks whether the given item is in the list of conflicts of
	 * the current item, but makes no judgment about whether the conflict
	 * is currently at play (either one of the items may not be selected)
	 *
	 * @param {mw.rcfilters.dm.FilterItem} filterItem Filter item
	 * @return {boolean} This item has a conflict with the given item
	 */
	mw.rcfilters.dm.FilterItem.prototype.existsInConflicts = function ( filterItem ) {
		return Object.prototype.hasOwnProperty.call( this.getConflicts(), filterItem.getName() );
	};

	/**
	 * Set the state of this filter as being conflicted
	 * (This means any filters in its conflicts are selected)
	 *
	 * @param {boolean} [conflicted] Filter is in conflict state
	 * @fires update
	 */
	mw.rcfilters.dm.FilterItem.prototype.toggleConflicted = function ( conflicted ) {
		conflicted = conflicted === undefined ? !this.conflicted : conflicted;

		if ( this.conflicted !== conflicted ) {
			this.conflicted = conflicted;
			this.emit( 'update' );
		}
	};

	/**
	 * Set the state of this filter as being already included
	 * (This means any filters in its superset are selected)
	 *
	 * @param {boolean} [included] Filter is included as part of a subset
	 * @fires update
	 */
	mw.rcfilters.dm.FilterItem.prototype.toggleIncluded = function ( included ) {
		included = included === undefined ? !this.included : included;

		if ( this.included !== included ) {
			this.included = included;
			this.emit( 'update' );
		}
	};

	/**
	 * Toggle the fully covered state of the item
	 *
	 * @param {boolean} [isFullyCovered] Filter is fully covered
	 * @fires update
	 */
	mw.rcfilters.dm.FilterItem.prototype.toggleFullyCovered = function ( isFullyCovered ) {
		isFullyCovered = isFullyCovered === undefined ? !this.fullycovered : isFullyCovered;

		if ( this.fullyCovered !== isFullyCovered ) {
			this.fullyCovered = isFullyCovered;
			this.emit( 'update' );
		}
	};

	/**
	 * Toggle the visibility of this item
	 *
	 * @param {boolean} [isVisible] Item is visible
	 */
	mw.rcfilters.dm.FilterItem.prototype.toggleVisible = function ( isVisible ) {
		isVisible = isVisible === undefined ? !this.visible : !!isVisible;

		if ( this.visible !== isVisible ) {
			this.visible = isVisible;
			this.emit( 'update' );
		}
	};

	/**
	 * Check whether the item is visible
	 *
	 * @return {boolean} Item is visible
	 */
	mw.rcfilters.dm.FilterItem.prototype.isVisible = function () {
		return this.visible;
	};

}( mediaWiki ) );