Merge "Replace deprecated Context::getStats() with MWServices::getStatsdDataFactory()"
[lhc/web/wiklou.git] / resources / src / mediawiki.rcfilters / dm / mw.rcfilters.dm.FilterItem.js
1 ( function ( mw ) {
2 /**
3 * Filter item model
4 *
5 * @mixins OO.EventEmitter
6 *
7 * @constructor
8 * @param {string} param Filter param name
9 * @param {mw.rcfilters.dm.FilterGroup} groupModel Filter group model
10 * @param {Object} config Configuration object
11 * @cfg {string} [group] The group this item belongs to
12 * @cfg {string} [label] The label for the filter
13 * @cfg {string} [description] The description of the filter
14 * @cfg {boolean} [active=true] The filter is active and affecting the result
15 * @cfg {string[]} [excludes=[]] A list of filter names this filter, if
16 * selected, makes inactive.
17 * @cfg {boolean} [selected] The item is selected
18 * @cfg {string[]} [subset] Defining the names of filters that are a subset of this filter
19 * @cfg {Object} [conflicts] Defines the conflicts for this filter
20 * @cfg {string} [cssClass] The class identifying the results that match this filter
21 */
22 mw.rcfilters.dm.FilterItem = function MwRcfiltersDmFilterItem( param, groupModel, config ) {
23 config = config || {};
24
25 // Mixin constructor
26 OO.EventEmitter.call( this );
27
28 this.param = param;
29 this.groupModel = groupModel;
30 this.name = this.groupModel.getNamePrefix() + param;
31
32 this.label = config.label || this.name;
33 this.description = config.description;
34 this.selected = !!config.selected;
35
36 // Interaction definitions
37 this.subset = config.subset || [];
38 this.conflicts = config.conflicts || {};
39 this.superset = [];
40
41 // Interaction states
42 this.included = false;
43 this.conflicted = false;
44 this.fullyCovered = false;
45
46 // Highlight
47 this.cssClass = config.cssClass;
48 this.highlightColor = null;
49 this.highlightEnabled = false;
50 };
51
52 /* Initialization */
53
54 OO.initClass( mw.rcfilters.dm.FilterItem );
55 OO.mixinClass( mw.rcfilters.dm.FilterItem, OO.EventEmitter );
56
57 /* Events */
58
59 /**
60 * @event update
61 *
62 * The state of this filter has changed
63 */
64
65 /* Methods */
66
67 /**
68 * Return the representation of the state of this item.
69 *
70 * @return {Object} State of the object
71 */
72 mw.rcfilters.dm.FilterItem.prototype.getState = function () {
73 return {
74 selected: this.isSelected(),
75 included: this.isIncluded(),
76 conflicted: this.isConflicted(),
77 fullyCovered: this.isFullyCovered()
78 };
79 };
80
81 /**
82 * Get the name of this filter
83 *
84 * @return {string} Filter name
85 */
86 mw.rcfilters.dm.FilterItem.prototype.getName = function () {
87 return this.name;
88 };
89
90 /**
91 * Get the param name or value of this filter
92 *
93 * @return {string} Filter param name
94 */
95 mw.rcfilters.dm.FilterItem.prototype.getParamName = function () {
96 return this.param;
97 };
98
99 /**
100 * Get the message for the display area for the currently active conflict
101 *
102 * @return {string} Conflict result message key
103 */
104 mw.rcfilters.dm.FilterItem.prototype.getCurrentConflictResultMessage = function () {
105 var details = {};
106
107 // First look in filter's own conflicts
108 details = this.getConflictDetails( this.getOwnConflicts(), 'globalDescription' );
109 if ( !details.message ) {
110 // Fall back onto conflicts in the group
111 details = this.getConflictDetails( this.getGroupModel().getConflicts(), 'globalDescription' );
112 }
113
114 return details.message;
115 };
116
117 /**
118 * Get the details of the active conflict on this filter
119 *
120 * @param {Object} conflicts Conflicts to examine
121 * @param {string} [key='contextDescription'] Message key
122 * @return {Object} Object with conflict message and conflict items
123 * @return {string} return.message Conflict message
124 * @return {string[]} return.names Conflicting item labels
125 */
126 mw.rcfilters.dm.FilterItem.prototype.getConflictDetails = function ( conflicts, key ) {
127 var group,
128 conflictMessage = '',
129 itemLabels = [];
130
131 key = key || 'contextDescription';
132
133 $.each( conflicts, function ( filterName, conflict ) {
134 if ( !conflict.item.isSelected() ) {
135 return;
136 }
137
138 if ( !conflictMessage ) {
139 conflictMessage = conflict[ key ];
140 group = conflict.group;
141 }
142
143 if ( group === conflict.group ) {
144 itemLabels.push( mw.msg( 'quotation-marks', conflict.item.getLabel() ) );
145 }
146 } );
147
148 return {
149 message: conflictMessage,
150 names: itemLabels
151 };
152
153 };
154
155 /**
156 * Get the message representing the state of this model.
157 *
158 * @return {string} State message
159 */
160 mw.rcfilters.dm.FilterItem.prototype.getStateMessage = function () {
161 var messageKey, details, superset,
162 affectingItems = [];
163
164 if ( this.isConflicted() ) {
165 // First look in filter's own conflicts
166 details = this.getConflictDetails( this.getOwnConflicts() );
167 if ( !details.message ) {
168 // Fall back onto conflicts in the group
169 details = this.getConflictDetails( this.getGroupModel().getConflicts() );
170 }
171
172 messageKey = details.message;
173 affectingItems = details.names;
174 } else if ( this.isIncluded() ) {
175 superset = this.getSuperset();
176 // For this message we need to collect the affecting superset
177 affectingItems = this.getGroupModel().getSelectedItems( this )
178 .filter( function ( item ) {
179 return superset.indexOf( item.getName() ) !== -1;
180 } )
181 .map( function ( item ) {
182 return mw.msg( 'quotation-marks', item.getLabel() );
183 } );
184
185 messageKey = 'rcfilters-state-message-subset';
186 } else if ( this.isFullyCovered() ) {
187 affectingItems = this.getGroupModel().getSelectedItems( this )
188 .map( function ( item ) {
189 return mw.msg( 'quotation-marks', item.getLabel() );
190 } );
191
192 messageKey = 'rcfilters-state-message-fullcoverage';
193 }
194
195 if ( messageKey ) {
196 // Build message
197 return mw.msg(
198 messageKey,
199 mw.language.listToText( affectingItems ),
200 affectingItems.length
201 );
202 }
203
204 // Display description
205 return this.getDescription();
206 };
207
208 /**
209 * Get the model of the group this filter belongs to
210 *
211 * @return {mw.rcfilters.dm.FilterGroup} Filter group model
212 */
213 mw.rcfilters.dm.FilterItem.prototype.getGroupModel = function () {
214 return this.groupModel;
215 };
216
217 /**
218 * Get the group name this filter belongs to
219 *
220 * @return {string} Filter group name
221 */
222 mw.rcfilters.dm.FilterItem.prototype.getGroupName = function () {
223 return this.groupModel.getName();
224 };
225
226 /**
227 * Get the label of this filter
228 *
229 * @return {string} Filter label
230 */
231 mw.rcfilters.dm.FilterItem.prototype.getLabel = function () {
232 return this.label;
233 };
234
235 /**
236 * Get the description of this filter
237 *
238 * @return {string} Filter description
239 */
240 mw.rcfilters.dm.FilterItem.prototype.getDescription = function () {
241 return this.description;
242 };
243
244 /**
245 * Get the default value of this filter
246 *
247 * @return {boolean} Filter default
248 */
249 mw.rcfilters.dm.FilterItem.prototype.getDefault = function () {
250 return this.default;
251 };
252
253 /**
254 * Get filter subset
255 * This is a list of filter names that are defined to be included
256 * when this filter is selected.
257 *
258 * @return {string[]} Filter subset
259 */
260 mw.rcfilters.dm.FilterItem.prototype.getSubset = function () {
261 return this.subset;
262 };
263
264 /**
265 * Get filter superset
266 * This is a generated list of filters that define this filter
267 * to be included when either of them is selected.
268 *
269 * @return {string[]} Filter superset
270 */
271 mw.rcfilters.dm.FilterItem.prototype.getSuperset = function () {
272 return this.superset;
273 };
274
275 /**
276 * Get the selected state of this filter
277 *
278 * @return {boolean} Filter is selected
279 */
280 mw.rcfilters.dm.FilterItem.prototype.isSelected = function () {
281 return this.selected;
282 };
283
284 /**
285 * Check whether the filter is currently in a conflict state
286 *
287 * @return {boolean} Filter is in conflict state
288 */
289 mw.rcfilters.dm.FilterItem.prototype.isConflicted = function () {
290 return this.conflicted;
291 };
292
293 /**
294 * Check whether the filter is currently in an already included subset
295 *
296 * @return {boolean} Filter is in an already-included subset
297 */
298 mw.rcfilters.dm.FilterItem.prototype.isIncluded = function () {
299 return this.included;
300 };
301
302 /**
303 * Check whether the filter is currently fully covered
304 *
305 * @return {boolean} Filter is in fully-covered state
306 */
307 mw.rcfilters.dm.FilterItem.prototype.isFullyCovered = function () {
308 return this.fullyCovered;
309 };
310
311 /**
312 * Get all conflicts associated with this filter or its group
313 *
314 * Conflict object is set up by filter name keys and conflict
315 * definition. For example:
316 * {
317 * filterName: {
318 * filter: filterName,
319 * group: group1,
320 * label: itemLabel,
321 * item: itemModel
322 * }
323 * filterName2: {
324 * filter: filterName2,
325 * group: group2
326 * label: itemLabel2,
327 * item: itemModel2
328 * }
329 * }
330 *
331 * @return {Object} Filter conflicts
332 */
333 mw.rcfilters.dm.FilterItem.prototype.getConflicts = function () {
334 return $.extend( {}, this.conflicts, this.getGroupModel().getConflicts() );
335 };
336
337 /**
338 * Get the conflicts associated with this filter
339 *
340 * @return {Object} Filter conflicts
341 */
342 mw.rcfilters.dm.FilterItem.prototype.getOwnConflicts = function () {
343 return this.conflicts;
344 };
345
346 /**
347 * Set conflicts for this filter. See #getConflicts for the expected
348 * structure of the definition.
349 *
350 * @param {Object} conflicts Conflicts for this filter
351 */
352 mw.rcfilters.dm.FilterItem.prototype.setConflicts = function ( conflicts ) {
353 this.conflicts = conflicts || {};
354 };
355
356 /**
357 * Set filter superset
358 *
359 * @param {string[]} superset Filter superset
360 */
361 mw.rcfilters.dm.FilterItem.prototype.setSuperset = function ( superset ) {
362 this.superset = superset || [];
363 };
364
365 /**
366 * Set filter subset
367 *
368 * @param {string[]} subset Filter subset
369 */
370 mw.rcfilters.dm.FilterItem.prototype.setSubset = function ( subset ) {
371 this.subset = subset || [];
372 };
373
374 /**
375 * Check whether a filter exists in the subset list for this filter
376 *
377 * @param {string} filterName Filter name
378 * @return {boolean} Filter name is in the subset list
379 */
380 mw.rcfilters.dm.FilterItem.prototype.existsInSubset = function ( filterName ) {
381 return this.subset.indexOf( filterName ) > -1;
382 };
383
384 /**
385 * Check whether this item has a potential conflict with the given item
386 *
387 * This checks whether the given item is in the list of conflicts of
388 * the current item, but makes no judgment about whether the conflict
389 * is currently at play (either one of the items may not be selected)
390 *
391 * @param {mw.rcfilters.dm.FilterItem} filterItem Filter item
392 * @return {boolean} This item has a conflict with the given item
393 */
394 mw.rcfilters.dm.FilterItem.prototype.existsInConflicts = function ( filterItem ) {
395 return Object.prototype.hasOwnProperty.call( this.getConflicts(), filterItem.getName() );
396 };
397
398 /**
399 * Set the state of this filter as being conflicted
400 * (This means any filters in its conflicts are selected)
401 *
402 * @param {boolean} [conflicted] Filter is in conflict state
403 * @fires update
404 */
405 mw.rcfilters.dm.FilterItem.prototype.toggleConflicted = function ( conflicted ) {
406 conflicted = conflicted === undefined ? !this.conflicted : conflicted;
407
408 if ( this.conflicted !== conflicted ) {
409 this.conflicted = conflicted;
410 this.emit( 'update' );
411 }
412 };
413
414 /**
415 * Set the state of this filter as being already included
416 * (This means any filters in its superset are selected)
417 *
418 * @param {boolean} [included] Filter is included as part of a subset
419 * @fires update
420 */
421 mw.rcfilters.dm.FilterItem.prototype.toggleIncluded = function ( included ) {
422 included = included === undefined ? !this.included : included;
423
424 if ( this.included !== included ) {
425 this.included = included;
426 this.emit( 'update' );
427 }
428 };
429
430 /**
431 * Toggle the selected state of the item
432 *
433 * @param {boolean} [isSelected] Filter is selected
434 * @fires update
435 */
436 mw.rcfilters.dm.FilterItem.prototype.toggleSelected = function ( isSelected ) {
437 isSelected = isSelected === undefined ? !this.selected : isSelected;
438
439 if ( this.selected !== isSelected ) {
440 this.selected = isSelected;
441 this.emit( 'update' );
442 }
443 };
444
445 /**
446 * Toggle the fully covered state of the item
447 *
448 * @param {boolean} [isFullyCovered] Filter is fully covered
449 * @fires update
450 */
451 mw.rcfilters.dm.FilterItem.prototype.toggleFullyCovered = function ( isFullyCovered ) {
452 isFullyCovered = isFullyCovered === undefined ? !this.fullycovered : isFullyCovered;
453
454 if ( this.fullyCovered !== isFullyCovered ) {
455 this.fullyCovered = isFullyCovered;
456 this.emit( 'update' );
457 }
458 };
459
460 /**
461 * Set the highlight color
462 *
463 * @param {string|null} highlightColor
464 */
465 mw.rcfilters.dm.FilterItem.prototype.setHighlightColor = function ( highlightColor ) {
466 if ( this.highlightColor !== highlightColor ) {
467 this.highlightColor = highlightColor;
468 this.emit( 'update' );
469 }
470 };
471
472 /**
473 * Clear the highlight color
474 */
475 mw.rcfilters.dm.FilterItem.prototype.clearHighlightColor = function () {
476 this.setHighlightColor( null );
477 };
478
479 /**
480 * Get the highlight color, or null if none is configured
481 *
482 * @return {string|null}
483 */
484 mw.rcfilters.dm.FilterItem.prototype.getHighlightColor = function () {
485 return this.highlightColor;
486 };
487
488 /**
489 * Get the CSS class that matches changes that fit this filter
490 * or null if none is configured
491 *
492 * @return {string|null}
493 */
494 mw.rcfilters.dm.FilterItem.prototype.getCssClass = function () {
495 return this.cssClass;
496 };
497
498 /**
499 * Toggle the highlight feature on and off for this filter.
500 * It only works if highlight is supported for this filter.
501 *
502 * @param {boolean} enable Highlight should be enabled
503 */
504 mw.rcfilters.dm.FilterItem.prototype.toggleHighlight = function ( enable ) {
505 enable = enable === undefined ? !this.highlightEnabled : enable;
506
507 if ( !this.isHighlightSupported() ) {
508 return;
509 }
510
511 if ( enable === this.highlightEnabled ) {
512 return;
513 }
514
515 this.highlightEnabled = enable;
516 this.emit( 'update' );
517 };
518
519 /**
520 * Check if the highlight feature is currently enabled for this filter
521 *
522 * @return {boolean}
523 */
524 mw.rcfilters.dm.FilterItem.prototype.isHighlightEnabled = function () {
525 return !!this.highlightEnabled;
526 };
527
528 /**
529 * Check if the highlight feature is supported for this filter
530 *
531 * @return {boolean}
532 */
533 mw.rcfilters.dm.FilterItem.prototype.isHighlightSupported = function () {
534 return !!this.getCssClass();
535 };
536
537 /**
538 * Check if the filter is currently highlighted
539 *
540 * @return {boolean}
541 */
542 mw.rcfilters.dm.FilterItem.prototype.isHighlighted = function () {
543 return this.isHighlightEnabled() && !!this.getHighlightColor();
544 };
545 }( mediaWiki ) );