5 * @extends mw.rcfilters.dm.ItemModel
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[]} [excludes=[]] A list of filter names this filter, if
12 * selected, makes inactive.
13 * @cfg {string[]} [subset] Defining the names of filters that are a subset of this filter
14 * @cfg {Object} [conflicts] Defines the conflicts for this filter
15 * @cfg {boolean} [visible=true] The visibility of the group
17 mw
.rcfilters
.dm
.FilterItem
= function MwRcfiltersDmFilterItem( param
, groupModel
, config
) {
18 config
= config
|| {};
20 this.groupModel
= groupModel
;
23 mw
.rcfilters
.dm
.FilterItem
.parent
.call( this, param
, $.extend( {
24 namePrefix
: this.groupModel
.getNamePrefix()
27 OO
.EventEmitter
.call( this );
29 // Interaction definitions
30 this.subset
= config
.subset
|| [];
31 this.conflicts
= config
.conflicts
|| {};
33 this.visible
= config
.visible
=== undefined ? true : !!config
.visible
;
36 this.included
= false;
37 this.conflicted
= false;
38 this.fullyCovered
= false;
43 OO
.inheritClass( mw
.rcfilters
.dm
.FilterItem
, mw
.rcfilters
.dm
.ItemModel
);
48 * Return the representation of the state of this item.
50 * @return {Object} State of the object
52 mw
.rcfilters
.dm
.FilterItem
.prototype.getState = function () {
54 selected
: this.isSelected(),
55 included
: this.isIncluded(),
56 conflicted
: this.isConflicted(),
57 fullyCovered
: this.isFullyCovered()
62 * Get the message for the display area for the currently active conflict
65 * @return {string} Conflict result message key
67 mw
.rcfilters
.dm
.FilterItem
.prototype.getCurrentConflictResultMessage = function () {
70 // First look in filter's own conflicts
71 details
= this.getConflictDetails( this.getOwnConflicts(), 'globalDescription' );
72 if ( !details
.message
) {
73 // Fall back onto conflicts in the group
74 details
= this.getConflictDetails( this.getGroupModel().getConflicts(), 'globalDescription' );
77 return details
.message
;
81 * Get the details of the active conflict on this filter
84 * @param {Object} conflicts Conflicts to examine
85 * @param {string} [key='contextDescription'] Message key
86 * @return {Object} Object with conflict message and conflict items
87 * @return {string} return.message Conflict message
88 * @return {string[]} return.names Conflicting item labels
90 mw
.rcfilters
.dm
.FilterItem
.prototype.getConflictDetails = function ( conflicts
, key
) {
95 key
= key
|| 'contextDescription';
97 // eslint-disable-next-line no-restricted-properties
98 $.each( conflicts
, function ( filterName
, conflict
) {
99 if ( !conflict
.item
.isSelected() ) {
103 if ( !conflictMessage
) {
104 conflictMessage
= conflict
[ key
];
105 group
= conflict
.group
;
108 if ( group
=== conflict
.group
) {
109 itemLabels
.push( mw
.msg( 'quotation-marks', conflict
.item
.getLabel() ) );
114 message
: conflictMessage
,
123 mw
.rcfilters
.dm
.FilterItem
.prototype.getStateMessage = function () {
124 var messageKey
, details
, superset
,
127 if ( this.isSelected() ) {
128 if ( this.isConflicted() ) {
129 // First look in filter's own conflicts
130 details
= this.getConflictDetails( this.getOwnConflicts() );
131 if ( !details
.message
) {
132 // Fall back onto conflicts in the group
133 details
= this.getConflictDetails( this.getGroupModel().getConflicts() );
136 messageKey
= details
.message
;
137 affectingItems
= details
.names
;
138 } else if ( this.isIncluded() && !this.isHighlighted() ) {
139 // We only show the 'no effect' full-coverage message
140 // if the item is also not highlighted. See T161273
141 superset
= this.getSuperset();
142 // For this message we need to collect the affecting superset
143 affectingItems
= this.getGroupModel().findSelectedItems( this )
144 .filter( function ( item
) {
145 return superset
.indexOf( item
.getName() ) !== -1;
147 .map( function ( item
) {
148 return mw
.msg( 'quotation-marks', item
.getLabel() );
151 messageKey
= 'rcfilters-state-message-subset';
152 } else if ( this.isFullyCovered() && !this.isHighlighted() ) {
153 affectingItems
= this.getGroupModel().findSelectedItems( this )
154 .map( function ( item
) {
155 return mw
.msg( 'quotation-marks', item
.getLabel() );
158 messageKey
= 'rcfilters-state-message-fullcoverage';
166 mw
.language
.listToText( affectingItems
),
167 affectingItems
.length
171 // Display description
172 return this.getDescription();
176 * Get the model of the group this filter belongs to
178 * @return {mw.rcfilters.dm.FilterGroup} Filter group model
180 mw
.rcfilters
.dm
.FilterItem
.prototype.getGroupModel = function () {
181 return this.groupModel
;
185 * Get the group name this filter belongs to
187 * @return {string} Filter group name
189 mw
.rcfilters
.dm
.FilterItem
.prototype.getGroupName = function () {
190 return this.groupModel
.getName();
195 * This is a list of filter names that are defined to be included
196 * when this filter is selected.
198 * @return {string[]} Filter subset
200 mw
.rcfilters
.dm
.FilterItem
.prototype.getSubset = function () {
205 * Get filter superset
206 * This is a generated list of filters that define this filter
207 * to be included when either of them is selected.
209 * @return {string[]} Filter superset
211 mw
.rcfilters
.dm
.FilterItem
.prototype.getSuperset = function () {
212 return this.superset
;
216 * Check whether the filter is currently in a conflict state
218 * @return {boolean} Filter is in conflict state
220 mw
.rcfilters
.dm
.FilterItem
.prototype.isConflicted = function () {
221 return this.conflicted
;
225 * Check whether the filter is currently in an already included subset
227 * @return {boolean} Filter is in an already-included subset
229 mw
.rcfilters
.dm
.FilterItem
.prototype.isIncluded = function () {
230 return this.included
;
234 * Check whether the filter is currently fully covered
236 * @return {boolean} Filter is in fully-covered state
238 mw
.rcfilters
.dm
.FilterItem
.prototype.isFullyCovered = function () {
239 return this.fullyCovered
;
243 * Get all conflicts associated with this filter or its group
245 * Conflict object is set up by filter name keys and conflict
246 * definition. For example:
249 * filter: filterName,
255 * filter: filterName2,
262 * @return {Object} Filter conflicts
264 mw
.rcfilters
.dm
.FilterItem
.prototype.getConflicts = function () {
265 return $.extend( {}, this.conflicts
, this.getGroupModel().getConflicts() );
269 * Get the conflicts associated with this filter
271 * @return {Object} Filter conflicts
273 mw
.rcfilters
.dm
.FilterItem
.prototype.getOwnConflicts = function () {
274 return this.conflicts
;
278 * Set conflicts for this filter. See #getConflicts for the expected
279 * structure of the definition.
281 * @param {Object} conflicts Conflicts for this filter
283 mw
.rcfilters
.dm
.FilterItem
.prototype.setConflicts = function ( conflicts
) {
284 this.conflicts
= conflicts
|| {};
288 * Set filter superset
290 * @param {string[]} superset Filter superset
292 mw
.rcfilters
.dm
.FilterItem
.prototype.setSuperset = function ( superset
) {
293 this.superset
= superset
|| [];
299 * @param {string[]} subset Filter subset
301 mw
.rcfilters
.dm
.FilterItem
.prototype.setSubset = function ( subset
) {
302 this.subset
= subset
|| [];
306 * Check whether a filter exists in the subset list for this filter
308 * @param {string} filterName Filter name
309 * @return {boolean} Filter name is in the subset list
311 mw
.rcfilters
.dm
.FilterItem
.prototype.existsInSubset = function ( filterName
) {
312 return this.subset
.indexOf( filterName
) > -1;
316 * Check whether this item has a potential conflict with the given item
318 * This checks whether the given item is in the list of conflicts of
319 * the current item, but makes no judgment about whether the conflict
320 * is currently at play (either one of the items may not be selected)
322 * @param {mw.rcfilters.dm.FilterItem} filterItem Filter item
323 * @return {boolean} This item has a conflict with the given item
325 mw
.rcfilters
.dm
.FilterItem
.prototype.existsInConflicts = function ( filterItem
) {
326 return Object
.prototype.hasOwnProperty
.call( this.getConflicts(), filterItem
.getName() );
330 * Set the state of this filter as being conflicted
331 * (This means any filters in its conflicts are selected)
333 * @param {boolean} [conflicted] Filter is in conflict state
336 mw
.rcfilters
.dm
.FilterItem
.prototype.toggleConflicted = function ( conflicted
) {
337 conflicted
= conflicted
=== undefined ? !this.conflicted
: conflicted
;
339 if ( this.conflicted
!== conflicted
) {
340 this.conflicted
= conflicted
;
341 this.emit( 'update' );
346 * Set the state of this filter as being already included
347 * (This means any filters in its superset are selected)
349 * @param {boolean} [included] Filter is included as part of a subset
352 mw
.rcfilters
.dm
.FilterItem
.prototype.toggleIncluded = function ( included
) {
353 included
= included
=== undefined ? !this.included
: included
;
355 if ( this.included
!== included
) {
356 this.included
= included
;
357 this.emit( 'update' );
362 * Toggle the fully covered state of the item
364 * @param {boolean} [isFullyCovered] Filter is fully covered
367 mw
.rcfilters
.dm
.FilterItem
.prototype.toggleFullyCovered = function ( isFullyCovered
) {
368 isFullyCovered
= isFullyCovered
=== undefined ? !this.fullycovered
: isFullyCovered
;
370 if ( this.fullyCovered
!== isFullyCovered
) {
371 this.fullyCovered
= isFullyCovered
;
372 this.emit( 'update' );
377 * Toggle the visibility of this item
379 * @param {boolean} [isVisible] Item is visible
381 mw
.rcfilters
.dm
.FilterItem
.prototype.toggleVisible = function ( isVisible
) {
382 isVisible
= isVisible
=== undefined ? !this.visible
: !!isVisible
;
384 if ( this.visible
!== isVisible
) {
385 this.visible
= isVisible
;
386 this.emit( 'update' );
391 * Check whether the item is visible
393 * @return {boolean} Item is visible
395 mw
.rcfilters
.dm
.FilterItem
.prototype.isVisible = function () {