1 var ItemModel
= require( './ItemModel.js' ),
7 * @class mw.rcfilters.dm.FilterItem
8 * @extends mw.rcfilters.dm.ItemModel
11 * @param {string} param Filter param name
12 * @param {mw.rcfilters.dm.FilterGroup} groupModel Filter group model
13 * @param {Object} config Configuration object
14 * @cfg {string[]} [excludes=[]] A list of filter names this filter, if
15 * selected, makes inactive.
16 * @cfg {string[]} [subset] Defining the names of filters that are a subset of this filter
17 * @cfg {Object} [conflicts] Defines the conflicts for this filter
18 * @cfg {boolean} [visible=true] The visibility of the group
20 FilterItem
= function MwRcfiltersDmFilterItem( param
, groupModel
, config
) {
21 config
= config
|| {};
23 this.groupModel
= groupModel
;
26 FilterItem
.parent
.call( this, param
, $.extend( {
27 namePrefix
: this.groupModel
.getNamePrefix()
30 OO
.EventEmitter
.call( this );
32 // Interaction definitions
33 this.subset
= config
.subset
|| [];
34 this.conflicts
= config
.conflicts
|| {};
36 this.visible
= config
.visible
=== undefined ? true : !!config
.visible
;
39 this.included
= false;
40 this.conflicted
= false;
41 this.fullyCovered
= false;
46 OO
.inheritClass( FilterItem
, ItemModel
);
51 * Return the representation of the state of this item.
53 * @return {Object} State of the object
55 FilterItem
.prototype.getState = function () {
57 selected
: this.isSelected(),
58 included
: this.isIncluded(),
59 conflicted
: this.isConflicted(),
60 fullyCovered
: this.isFullyCovered()
65 * Get the message for the display area for the currently active conflict
68 * @return {string} Conflict result message key
70 FilterItem
.prototype.getCurrentConflictResultMessage = function () {
73 // First look in filter's own conflicts
74 details
= this.getConflictDetails( this.getOwnConflicts(), 'globalDescription' );
75 if ( !details
.message
) {
76 // Fall back onto conflicts in the group
77 details
= this.getConflictDetails( this.getGroupModel().getConflicts(), 'globalDescription' );
80 return details
.message
;
84 * Get the details of the active conflict on this filter
87 * @param {Object} conflicts Conflicts to examine
88 * @param {string} [key='contextDescription'] Message key
89 * @return {Object} Object with conflict message and conflict items
90 * @return {string} return.message Conflict message
91 * @return {string[]} return.names Conflicting item labels
93 FilterItem
.prototype.getConflictDetails = function ( conflicts
, key
) {
98 key
= key
|| 'contextDescription';
100 // eslint-disable-next-line no-jquery/no-each-util
101 $.each( conflicts
, function ( filterName
, conflict
) {
102 if ( !conflict
.item
.isSelected() ) {
106 if ( !conflictMessage
) {
107 conflictMessage
= conflict
[ key
];
108 group
= conflict
.group
;
111 if ( group
=== conflict
.group
) {
112 itemLabels
.push( mw
.msg( 'quotation-marks', conflict
.item
.getLabel() ) );
117 message
: conflictMessage
,
126 FilterItem
.prototype.getStateMessage = function () {
127 var messageKey
, details
, superset
,
130 if ( this.isSelected() ) {
131 if ( this.isConflicted() ) {
132 // First look in filter's own conflicts
133 details
= this.getConflictDetails( this.getOwnConflicts() );
134 if ( !details
.message
) {
135 // Fall back onto conflicts in the group
136 details
= this.getConflictDetails( this.getGroupModel().getConflicts() );
139 messageKey
= details
.message
;
140 affectingItems
= details
.names
;
141 } else if ( this.isIncluded() && !this.isHighlighted() ) {
142 // We only show the 'no effect' full-coverage message
143 // if the item is also not highlighted. See T161273
144 superset
= this.getSuperset();
145 // For this message we need to collect the affecting superset
146 affectingItems
= this.getGroupModel().findSelectedItems( this )
147 .filter( function ( item
) {
148 return superset
.indexOf( item
.getName() ) !== -1;
150 .map( function ( item
) {
151 return mw
.msg( 'quotation-marks', item
.getLabel() );
154 messageKey
= 'rcfilters-state-message-subset';
155 } else if ( this.isFullyCovered() && !this.isHighlighted() ) {
156 affectingItems
= this.getGroupModel().findSelectedItems( this )
157 .map( function ( item
) {
158 return mw
.msg( 'quotation-marks', item
.getLabel() );
161 messageKey
= 'rcfilters-state-message-fullcoverage';
169 mw
.language
.listToText( affectingItems
),
170 affectingItems
.length
174 // Display description
175 return this.getDescription();
179 * Get the model of the group this filter belongs to
181 * @return {mw.rcfilters.dm.FilterGroup} Filter group model
183 FilterItem
.prototype.getGroupModel = function () {
184 return this.groupModel
;
188 * Get the group name this filter belongs to
190 * @return {string} Filter group name
192 FilterItem
.prototype.getGroupName = function () {
193 return this.groupModel
.getName();
198 * This is a list of filter names that are defined to be included
199 * when this filter is selected.
201 * @return {string[]} Filter subset
203 FilterItem
.prototype.getSubset = function () {
208 * Get filter superset
209 * This is a generated list of filters that define this filter
210 * to be included when either of them is selected.
212 * @return {string[]} Filter superset
214 FilterItem
.prototype.getSuperset = function () {
215 return this.superset
;
219 * Check whether the filter is currently in a conflict state
221 * @return {boolean} Filter is in conflict state
223 FilterItem
.prototype.isConflicted = function () {
224 return this.conflicted
;
228 * Check whether the filter is currently in an already included subset
230 * @return {boolean} Filter is in an already-included subset
232 FilterItem
.prototype.isIncluded = function () {
233 return this.included
;
237 * Check whether the filter is currently fully covered
239 * @return {boolean} Filter is in fully-covered state
241 FilterItem
.prototype.isFullyCovered = function () {
242 return this.fullyCovered
;
246 * Get all conflicts associated with this filter or its group
248 * Conflict object is set up by filter name keys and conflict
249 * definition. For example:
253 * filter: filterName,
259 * filter: filterName2,
266 * @return {Object} Filter conflicts
268 FilterItem
.prototype.getConflicts = function () {
269 return $.extend( {}, this.conflicts
, this.getGroupModel().getConflicts() );
273 * Get the conflicts associated with this filter
275 * @return {Object} Filter conflicts
277 FilterItem
.prototype.getOwnConflicts = function () {
278 return this.conflicts
;
282 * Set conflicts for this filter. See #getConflicts for the expected
283 * structure of the definition.
285 * @param {Object} conflicts Conflicts for this filter
287 FilterItem
.prototype.setConflicts = function ( conflicts
) {
288 this.conflicts
= conflicts
|| {};
292 * Set filter superset
294 * @param {string[]} superset Filter superset
296 FilterItem
.prototype.setSuperset = function ( superset
) {
297 this.superset
= superset
|| [];
303 * @param {string[]} subset Filter subset
305 FilterItem
.prototype.setSubset = function ( subset
) {
306 this.subset
= subset
|| [];
310 * Check whether a filter exists in the subset list for this filter
312 * @param {string} filterName Filter name
313 * @return {boolean} Filter name is in the subset list
315 FilterItem
.prototype.existsInSubset = function ( filterName
) {
316 return this.subset
.indexOf( filterName
) > -1;
320 * Check whether this item has a potential conflict with the given item
322 * This checks whether the given item is in the list of conflicts of
323 * the current item, but makes no judgment about whether the conflict
324 * is currently at play (either one of the items may not be selected)
326 * @param {mw.rcfilters.dm.FilterItem} filterItem Filter item
327 * @return {boolean} This item has a conflict with the given item
329 FilterItem
.prototype.existsInConflicts = function ( filterItem
) {
330 return Object
.prototype.hasOwnProperty
.call( this.getConflicts(), filterItem
.getName() );
334 * Set the state of this filter as being conflicted
335 * (This means any filters in its conflicts are selected)
337 * @param {boolean} [conflicted] Filter is in conflict state
340 FilterItem
.prototype.toggleConflicted = function ( conflicted
) {
341 conflicted
= conflicted
=== undefined ? !this.conflicted
: conflicted
;
343 if ( this.conflicted
!== conflicted
) {
344 this.conflicted
= conflicted
;
345 this.emit( 'update' );
350 * Set the state of this filter as being already included
351 * (This means any filters in its superset are selected)
353 * @param {boolean} [included] Filter is included as part of a subset
356 FilterItem
.prototype.toggleIncluded = function ( included
) {
357 included
= included
=== undefined ? !this.included
: included
;
359 if ( this.included
!== included
) {
360 this.included
= included
;
361 this.emit( 'update' );
366 * Toggle the fully covered state of the item
368 * @param {boolean} [isFullyCovered] Filter is fully covered
371 FilterItem
.prototype.toggleFullyCovered = function ( isFullyCovered
) {
372 isFullyCovered
= isFullyCovered
=== undefined ? !this.fullycovered
: isFullyCovered
;
374 if ( this.fullyCovered
!== isFullyCovered
) {
375 this.fullyCovered
= isFullyCovered
;
376 this.emit( 'update' );
381 * Toggle the visibility of this item
383 * @param {boolean} [isVisible] Item is visible
385 FilterItem
.prototype.toggleVisible = function ( isVisible
) {
386 isVisible
= isVisible
=== undefined ? !this.visible
: !!isVisible
;
388 if ( this.visible
!== isVisible
) {
389 this.visible
= isVisible
;
390 this.emit( 'update' );
395 * Check whether the item is visible
397 * @return {boolean} Item is visible
399 FilterItem
.prototype.isVisible = function () {
403 module
.exports
= FilterItem
;