5 * @mixins OO.EventEmitter
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
22 mw
.rcfilters
.dm
.FilterItem
= function MwRcfiltersDmFilterItem( param
, groupModel
, config
) {
23 config
= config
|| {};
26 OO
.EventEmitter
.call( this );
29 this.groupModel
= groupModel
;
30 this.name
= this.groupModel
.getNamePrefix() + param
;
32 this.label
= config
.label
|| this.name
;
33 this.description
= config
.description
;
34 this.selected
= !!config
.selected
;
36 // Interaction definitions
37 this.subset
= config
.subset
|| [];
38 this.conflicts
= config
.conflicts
|| {};
42 this.included
= false;
43 this.conflicted
= false;
44 this.fullyCovered
= false;
47 this.cssClass
= config
.cssClass
;
48 this.highlightColor
= null;
49 this.highlightEnabled
= false;
54 OO
.initClass( mw
.rcfilters
.dm
.FilterItem
);
55 OO
.mixinClass( mw
.rcfilters
.dm
.FilterItem
, OO
.EventEmitter
);
62 * The state of this filter has changed
68 * Return the representation of the state of this item.
70 * @return {Object} State of the object
72 mw
.rcfilters
.dm
.FilterItem
.prototype.getState = function () {
74 selected
: this.isSelected(),
75 included
: this.isIncluded(),
76 conflicted
: this.isConflicted(),
77 fullyCovered
: this.isFullyCovered()
82 * Get the name of this filter
84 * @return {string} Filter name
86 mw
.rcfilters
.dm
.FilterItem
.prototype.getName = function () {
91 * Get the param name or value of this filter
93 * @return {string} Filter param name
95 mw
.rcfilters
.dm
.FilterItem
.prototype.getParamName = function () {
100 * Get the message for the display area for the currently active conflict
102 * @return {string} Conflict result message key
104 mw
.rcfilters
.dm
.FilterItem
.prototype.getCurrentConflictResultMessage = function () {
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' );
114 return details
.message
;
118 * Get the details of the active conflict on this filter
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
126 mw
.rcfilters
.dm
.FilterItem
.prototype.getConflictDetails = function ( conflicts
, key
) {
128 conflictMessage
= '',
131 key
= key
|| 'contextDescription';
133 $.each( conflicts
, function ( filterName
, conflict
) {
134 if ( !conflict
.item
.isSelected() ) {
138 if ( !conflictMessage
) {
139 conflictMessage
= conflict
[ key
];
140 group
= conflict
.group
;
143 if ( group
=== conflict
.group
) {
144 itemLabels
.push( mw
.msg( 'quotation-marks', conflict
.item
.getLabel() ) );
149 message
: conflictMessage
,
156 * Get the message representing the state of this model.
158 * @return {string} State message
160 mw
.rcfilters
.dm
.FilterItem
.prototype.getStateMessage = function () {
161 var messageKey
, details
, superset
,
164 if ( this.isSelected() ) {
165 if ( this.isConflicted() ) {
166 // First look in filter's own conflicts
167 details
= this.getConflictDetails( this.getOwnConflicts() );
168 if ( !details
.message
) {
169 // Fall back onto conflicts in the group
170 details
= this.getConflictDetails( this.getGroupModel().getConflicts() );
173 messageKey
= details
.message
;
174 affectingItems
= details
.names
;
175 } else if ( this.isIncluded() ) {
176 superset
= this.getSuperset();
177 // For this message we need to collect the affecting superset
178 affectingItems
= this.getGroupModel().getSelectedItems( this )
179 .filter( function ( item
) {
180 return superset
.indexOf( item
.getName() ) !== -1;
182 .map( function ( item
) {
183 return mw
.msg( 'quotation-marks', item
.getLabel() );
186 messageKey
= 'rcfilters-state-message-subset';
187 } else if ( this.isFullyCovered() ) {
188 affectingItems
= this.getGroupModel().getSelectedItems( this )
189 .map( function ( item
) {
190 return mw
.msg( 'quotation-marks', item
.getLabel() );
193 messageKey
= 'rcfilters-state-message-fullcoverage';
201 mw
.language
.listToText( affectingItems
),
202 affectingItems
.length
206 // Display description
207 return this.getDescription();
211 * Get the model of the group this filter belongs to
213 * @return {mw.rcfilters.dm.FilterGroup} Filter group model
215 mw
.rcfilters
.dm
.FilterItem
.prototype.getGroupModel = function () {
216 return this.groupModel
;
220 * Get the group name this filter belongs to
222 * @return {string} Filter group name
224 mw
.rcfilters
.dm
.FilterItem
.prototype.getGroupName = function () {
225 return this.groupModel
.getName();
229 * Get the label of this filter
231 * @return {string} Filter label
233 mw
.rcfilters
.dm
.FilterItem
.prototype.getLabel = function () {
238 * Get the description of this filter
240 * @return {string} Filter description
242 mw
.rcfilters
.dm
.FilterItem
.prototype.getDescription = function () {
243 return this.description
;
247 * Get the default value of this filter
249 * @return {boolean} Filter default
251 mw
.rcfilters
.dm
.FilterItem
.prototype.getDefault = function () {
257 * This is a list of filter names that are defined to be included
258 * when this filter is selected.
260 * @return {string[]} Filter subset
262 mw
.rcfilters
.dm
.FilterItem
.prototype.getSubset = function () {
267 * Get filter superset
268 * This is a generated list of filters that define this filter
269 * to be included when either of them is selected.
271 * @return {string[]} Filter superset
273 mw
.rcfilters
.dm
.FilterItem
.prototype.getSuperset = function () {
274 return this.superset
;
278 * Get the selected state of this filter
280 * @return {boolean} Filter is selected
282 mw
.rcfilters
.dm
.FilterItem
.prototype.isSelected = function () {
283 return this.selected
;
287 * Check whether the filter is currently in a conflict state
289 * @return {boolean} Filter is in conflict state
291 mw
.rcfilters
.dm
.FilterItem
.prototype.isConflicted = function () {
292 return this.conflicted
;
296 * Check whether the filter is currently in an already included subset
298 * @return {boolean} Filter is in an already-included subset
300 mw
.rcfilters
.dm
.FilterItem
.prototype.isIncluded = function () {
301 return this.included
;
305 * Check whether the filter is currently fully covered
307 * @return {boolean} Filter is in fully-covered state
309 mw
.rcfilters
.dm
.FilterItem
.prototype.isFullyCovered = function () {
310 return this.fullyCovered
;
314 * Get all conflicts associated with this filter or its group
316 * Conflict object is set up by filter name keys and conflict
317 * definition. For example:
320 * filter: filterName,
326 * filter: filterName2,
333 * @return {Object} Filter conflicts
335 mw
.rcfilters
.dm
.FilterItem
.prototype.getConflicts = function () {
336 return $.extend( {}, this.conflicts
, this.getGroupModel().getConflicts() );
340 * Get the conflicts associated with this filter
342 * @return {Object} Filter conflicts
344 mw
.rcfilters
.dm
.FilterItem
.prototype.getOwnConflicts = function () {
345 return this.conflicts
;
349 * Set conflicts for this filter. See #getConflicts for the expected
350 * structure of the definition.
352 * @param {Object} conflicts Conflicts for this filter
354 mw
.rcfilters
.dm
.FilterItem
.prototype.setConflicts = function ( conflicts
) {
355 this.conflicts
= conflicts
|| {};
359 * Set filter superset
361 * @param {string[]} superset Filter superset
363 mw
.rcfilters
.dm
.FilterItem
.prototype.setSuperset = function ( superset
) {
364 this.superset
= superset
|| [];
370 * @param {string[]} subset Filter subset
372 mw
.rcfilters
.dm
.FilterItem
.prototype.setSubset = function ( subset
) {
373 this.subset
= subset
|| [];
377 * Check whether a filter exists in the subset list for this filter
379 * @param {string} filterName Filter name
380 * @return {boolean} Filter name is in the subset list
382 mw
.rcfilters
.dm
.FilterItem
.prototype.existsInSubset = function ( filterName
) {
383 return this.subset
.indexOf( filterName
) > -1;
387 * Check whether this item has a potential conflict with the given item
389 * This checks whether the given item is in the list of conflicts of
390 * the current item, but makes no judgment about whether the conflict
391 * is currently at play (either one of the items may not be selected)
393 * @param {mw.rcfilters.dm.FilterItem} filterItem Filter item
394 * @return {boolean} This item has a conflict with the given item
396 mw
.rcfilters
.dm
.FilterItem
.prototype.existsInConflicts = function ( filterItem
) {
397 return Object
.prototype.hasOwnProperty
.call( this.getConflicts(), filterItem
.getName() );
401 * Set the state of this filter as being conflicted
402 * (This means any filters in its conflicts are selected)
404 * @param {boolean} [conflicted] Filter is in conflict state
407 mw
.rcfilters
.dm
.FilterItem
.prototype.toggleConflicted = function ( conflicted
) {
408 conflicted
= conflicted
=== undefined ? !this.conflicted
: conflicted
;
410 if ( this.conflicted
!== conflicted
) {
411 this.conflicted
= conflicted
;
412 this.emit( 'update' );
417 * Set the state of this filter as being already included
418 * (This means any filters in its superset are selected)
420 * @param {boolean} [included] Filter is included as part of a subset
423 mw
.rcfilters
.dm
.FilterItem
.prototype.toggleIncluded = function ( included
) {
424 included
= included
=== undefined ? !this.included
: included
;
426 if ( this.included
!== included
) {
427 this.included
= included
;
428 this.emit( 'update' );
433 * Toggle the selected state of the item
435 * @param {boolean} [isSelected] Filter is selected
438 mw
.rcfilters
.dm
.FilterItem
.prototype.toggleSelected = function ( isSelected
) {
439 isSelected
= isSelected
=== undefined ? !this.selected
: isSelected
;
441 if ( this.selected
!== isSelected
) {
442 this.selected
= isSelected
;
443 this.emit( 'update' );
448 * Toggle the fully covered state of the item
450 * @param {boolean} [isFullyCovered] Filter is fully covered
453 mw
.rcfilters
.dm
.FilterItem
.prototype.toggleFullyCovered = function ( isFullyCovered
) {
454 isFullyCovered
= isFullyCovered
=== undefined ? !this.fullycovered
: isFullyCovered
;
456 if ( this.fullyCovered
!== isFullyCovered
) {
457 this.fullyCovered
= isFullyCovered
;
458 this.emit( 'update' );
463 * Set the highlight color
465 * @param {string|null} highlightColor
467 mw
.rcfilters
.dm
.FilterItem
.prototype.setHighlightColor = function ( highlightColor
) {
468 if ( this.highlightColor
!== highlightColor
) {
469 this.highlightColor
= highlightColor
;
470 this.emit( 'update' );
475 * Clear the highlight color
477 mw
.rcfilters
.dm
.FilterItem
.prototype.clearHighlightColor = function () {
478 this.setHighlightColor( null );
482 * Get the highlight color, or null if none is configured
484 * @return {string|null}
486 mw
.rcfilters
.dm
.FilterItem
.prototype.getHighlightColor = function () {
487 return this.highlightColor
;
491 * Get the CSS class that matches changes that fit this filter
492 * or null if none is configured
494 * @return {string|null}
496 mw
.rcfilters
.dm
.FilterItem
.prototype.getCssClass = function () {
497 return this.cssClass
;
501 * Toggle the highlight feature on and off for this filter.
502 * It only works if highlight is supported for this filter.
504 * @param {boolean} enable Highlight should be enabled
506 mw
.rcfilters
.dm
.FilterItem
.prototype.toggleHighlight = function ( enable
) {
507 enable
= enable
=== undefined ? !this.highlightEnabled
: enable
;
509 if ( !this.isHighlightSupported() ) {
513 if ( enable
=== this.highlightEnabled
) {
517 this.highlightEnabled
= enable
;
518 this.emit( 'update' );
522 * Check if the highlight feature is currently enabled for this filter
526 mw
.rcfilters
.dm
.FilterItem
.prototype.isHighlightEnabled = function () {
527 return !!this.highlightEnabled
;
531 * Check if the highlight feature is supported for this filter
535 mw
.rcfilters
.dm
.FilterItem
.prototype.isHighlightSupported = function () {
536 return !!this.getCssClass();
540 * Check if the filter is currently highlighted
544 mw
.rcfilters
.dm
.FilterItem
.prototype.isHighlighted = function () {
545 return this.isHighlightEnabled() && !!this.getHighlightColor();