3 * View model for a filter group
5 * @mixins OO.EventEmitter
6 * @mixins OO.EmitterList
9 * @param {string} name Group name
10 * @param {Object} [config] Configuration options
11 * @cfg {string} [type='send_unselected_if_any'] Group type
12 * @cfg {string} [view='default'] Name of the display group this group
14 * @cfg {string} [title] Group title
15 * @cfg {string} [separator='|'] Value separator for 'string_options' groups
16 * @cfg {boolean} [active] Group is active
17 * @cfg {boolean} [fullCoverage] This filters in this group collectively cover all results
18 * @cfg {Object} [conflicts] Defines the conflicts for this filter group
19 * @cfg {string|Object} [labelPrefixKey] An i18n key defining the prefix label for this
20 * group. If the prefix has 'invert' state, the parameter is expected to be an object
21 * with 'default' and 'inverted' as keys.
22 * @cfg {Object} [whatsThis] Defines the messages that should appear for the 'what's this' popup
23 * @cfg {string} [whatsThis.header] The header of the whatsThis popup message
24 * @cfg {string} [whatsThis.body] The body of the whatsThis popup message
25 * @cfg {string} [whatsThis.url] The url for the link in the whatsThis popup message
26 * @cfg {string} [whatsThis.linkMessage] The text for the link in the whatsThis popup message
28 mw
.rcfilters
.dm
.FilterGroup
= function MwRcfiltersDmFilterGroup( name
, config
) {
29 config
= config
|| {};
32 OO
.EventEmitter
.call( this );
33 OO
.EmitterList
.call( this );
36 this.type
= config
.type
|| 'send_unselected_if_any';
37 this.view
= config
.view
|| 'default';
38 this.title
= config
.title
|| name
;
39 this.separator
= config
.separator
|| '|';
40 this.labelPrefixKey
= config
.labelPrefixKey
;
42 this.active
= !!config
.active
;
43 this.fullCoverage
= !!config
.fullCoverage
;
45 this.whatsThis
= config
.whatsThis
|| {};
47 this.conflicts
= config
.conflicts
|| {};
48 this.defaultParams
= {};
50 this.aggregate( { update
: 'filterItemUpdate' } );
51 this.connect( this, { filterItemUpdate
: 'onFilterItemUpdate' } );
55 OO
.initClass( mw
.rcfilters
.dm
.FilterGroup
);
56 OO
.mixinClass( mw
.rcfilters
.dm
.FilterGroup
, OO
.EventEmitter
);
57 OO
.mixinClass( mw
.rcfilters
.dm
.FilterGroup
, OO
.EmitterList
);
64 * Group state has been updated
70 * Initialize the group and create its filter items
72 * @param {Object} filterDefinition Filter definition for this group
73 * @param {string|Object} [groupDefault] Definition of the group default
75 mw
.rcfilters
.dm
.FilterGroup
.prototype.initializeFilters = function ( filterDefinition
, groupDefault
) {
80 filterDefinition
.forEach( function ( filter
) {
81 // Instantiate an item
83 filterItem
= new mw
.rcfilters
.dm
.FilterItem( filter
.name
, model
, {
84 group
: model
.getName(),
85 label
: filter
.label
|| filter
.name
,
86 description
: filter
.description
|| '',
87 labelPrefixKey
: model
.labelPrefixKey
,
88 cssClass
: filter
.cssClass
,
89 identifiers
: filter
.identifiers
92 filter
.subset
= filter
.subset
|| [];
93 filter
.subset
= filter
.subset
.map( function ( el
) {
97 if ( filter
.subset
) {
99 filter
.subset
.forEach( function ( subsetFilterName
) { // eslint-disable-line no-loop-func
100 // Subsets (unlike conflicts) are always inside the same group
101 // We can re-map the names of the filters we are getting from
102 // the subsets with the group prefix
103 var subsetName
= model
.getPrefixedName( subsetFilterName
);
104 // For convenience, we should store each filter's "supersets" -- these are
105 // the filters that have that item in their subset list. This will just
106 // make it easier to go through whether the item has any other items
107 // that affect it (and are selected) at any given time
108 supersetMap
[ subsetName
] = supersetMap
[ subsetName
] || [];
109 mw
.rcfilters
.utils
.addArrayElementsUnique(
110 supersetMap
[ subsetName
],
114 // Translate subset param name to add the group name, so we
115 // get consistent naming. We know that subsets are only within
117 subsetNames
.push( subsetName
);
120 // Set translated subset
121 filterItem
.setSubset( subsetNames
);
124 items
.push( filterItem
);
126 // Store default parameter state; in this case, default is defined per filter
127 if ( model
.getType() === 'send_unselected_if_any' ) {
128 // Store the default parameter state
129 // For this group type, parameter values are direct
130 // We need to convert from a boolean to a string ('1' and '0')
131 model
.defaultParams
[ filter
.name
] = String( Number( !!filter
.default ) );
136 this.addItems( items
);
138 // Now that we have all items, we can apply the superset map
139 this.getItems().forEach( function ( filterItem
) {
140 filterItem
.setSuperset( supersetMap
[ filterItem
.getName() ] );
143 // Store default parameter state; in this case, default is defined per the
144 // entire group, given by groupDefault method parameter
145 if ( this.getType() === 'string_options' ) {
146 // Store the default parameter group state
147 // For this group, the parameter is group name and value is the names
149 this.defaultParams
[ this.getName() ] = mw
.rcfilters
.utils
.normalizeParamOptions(
152 groupDefault
.split( this.getSeparator() ) :
155 this.getItems().map( function ( item
) {
156 return item
.getParamName();
158 ).join( this.getSeparator() );
163 * Respond to filterItem update event
167 mw
.rcfilters
.dm
.FilterGroup
.prototype.onFilterItemUpdate = function () {
169 var active
= this.areAnySelected();
171 if ( this.active
!== active
) {
172 this.active
= active
;
173 this.emit( 'update' );
178 * Get group active state
180 * @return {boolean} Active state
182 mw
.rcfilters
.dm
.FilterGroup
.prototype.isActive = function () {
189 * @return {string} Group name
191 mw
.rcfilters
.dm
.FilterGroup
.prototype.getName = function () {
196 * Get the default param state of this group
198 * @return {Object} Default param state
200 mw
.rcfilters
.dm
.FilterGroup
.prototype.getDefaultParams = function () {
201 return this.defaultParams
;
205 * Get the messags defining the 'whats this' popup for this group
207 * @return {Object} What's this messages
209 mw
.rcfilters
.dm
.FilterGroup
.prototype.getWhatsThis = function () {
210 return this.whatsThis
;
214 * Check whether this group has a 'what's this' message
216 * @return {boolean} This group has a what's this message
218 mw
.rcfilters
.dm
.FilterGroup
.prototype.hasWhatsThis = function () {
219 return !!this.whatsThis
.body
;
223 * Get the conflicts associated with the entire group.
224 * Conflict object is set up by filter name keys and conflict
225 * definition. For example:
229 * filter: filterName,
235 * filter: filterName2,
240 * @return {Object} Conflict definition
242 mw
.rcfilters
.dm
.FilterGroup
.prototype.getConflicts = function () {
243 return this.conflicts
;
247 * Set conflicts for this group. See #getConflicts for the expected
248 * structure of the definition.
250 * @param {Object} conflicts Conflicts for this group
252 mw
.rcfilters
.dm
.FilterGroup
.prototype.setConflicts = function ( conflicts
) {
253 this.conflicts
= conflicts
;
257 * Set conflicts for each filter item in the group based on the
260 * @param {Object} conflicts Object representing the conflict map,
261 * keyed by the item name, where its value is an object for all its conflicts
263 mw
.rcfilters
.dm
.FilterGroup
.prototype.setFilterConflicts = function ( conflicts
) {
264 this.getItems().forEach( function ( filterItem
) {
265 if ( conflicts
[ filterItem
.getName() ] ) {
266 filterItem
.setConflicts( conflicts
[ filterItem
.getName() ] );
272 * Check whether this item has a potential conflict with the given item
274 * This checks whether the given item is in the list of conflicts of
275 * the current item, but makes no judgment about whether the conflict
276 * is currently at play (either one of the items may not be selected)
278 * @param {mw.rcfilters.dm.FilterItem} filterItem Filter item
279 * @return {boolean} This item has a conflict with the given item
281 mw
.rcfilters
.dm
.FilterGroup
.prototype.existsInConflicts = function ( filterItem
) {
282 return Object
.prototype.hasOwnProperty
.call( this.getConflicts(), filterItem
.getName() );
286 * Check whether there are any items selected
288 * @return {boolean} Any items in the group are selected
290 mw
.rcfilters
.dm
.FilterGroup
.prototype.areAnySelected = function () {
291 return this.getItems().some( function ( filterItem
) {
292 return filterItem
.isSelected();
297 * Check whether all items selected
299 * @return {boolean} All items are selected
301 mw
.rcfilters
.dm
.FilterGroup
.prototype.areAllSelected = function () {
305 this.getItems().forEach( function ( filterItem
) {
306 if ( filterItem
.isSelected() ) {
307 selected
.push( filterItem
);
309 unselected
.push( filterItem
);
313 if ( unselected
.length
=== 0 ) {
317 // check if every unselected is a subset of a selected
318 return unselected
.every( function ( unselectedFilterItem
) {
319 return selected
.some( function ( selectedFilterItem
) {
320 return selectedFilterItem
.existsInSubset( unselectedFilterItem
.getName() );
326 * Get all selected items in this group
328 * @param {mw.rcfilters.dm.FilterItem} [excludeItem] Item to exclude from the list
329 * @return {mw.rcfilters.dm.FilterItem[]} Selected items
331 mw
.rcfilters
.dm
.FilterGroup
.prototype.getSelectedItems = function ( excludeItem
) {
332 var excludeName
= ( excludeItem
&& excludeItem
.getName() ) || '';
334 return this.getItems().filter( function ( item
) {
335 return item
.getName() !== excludeName
&& item
.isSelected();
340 * Check whether all selected items are in conflict with the given item
342 * @param {mw.rcfilters.dm.FilterItem} filterItem Filter item to test
343 * @return {boolean} All selected items are in conflict with this item
345 mw
.rcfilters
.dm
.FilterGroup
.prototype.areAllSelectedInConflictWith = function ( filterItem
) {
346 var selectedItems
= this.getSelectedItems( filterItem
);
348 return selectedItems
.length
> 0 &&
350 // The group as a whole is in conflict with this item
351 this.existsInConflicts( filterItem
) ||
352 // All selected items are in conflict individually
353 selectedItems
.every( function ( selectedFilter
) {
354 return selectedFilter
.existsInConflicts( filterItem
);
360 * Check whether any of the selected items are in conflict with the given item
362 * @param {mw.rcfilters.dm.FilterItem} filterItem Filter item to test
363 * @return {boolean} Any of the selected items are in conflict with this item
365 mw
.rcfilters
.dm
.FilterGroup
.prototype.areAnySelectedInConflictWith = function ( filterItem
) {
366 var selectedItems
= this.getSelectedItems( filterItem
);
368 return selectedItems
.length
> 0 && (
369 // The group as a whole is in conflict with this item
370 this.existsInConflicts( filterItem
) ||
371 // Any selected items are in conflict individually
372 selectedItems
.some( function ( selectedFilter
) {
373 return selectedFilter
.existsInConflicts( filterItem
);
379 * Get the parameter representation from this group
381 * @param {Object} [filterRepresentation] An object defining the state
382 * of the filters in this group, keyed by their name and current selected
384 * @return {Object} Parameter representation
386 mw
.rcfilters
.dm
.FilterGroup
.prototype.getParamRepresentation = function ( filterRepresentation
) {
388 areAnySelected
= false,
389 buildFromCurrentState
= !filterRepresentation
,
391 filterParamNames
= {};
393 filterRepresentation
= filterRepresentation
|| {};
395 // Create or complete the filterRepresentation definition
396 this.getItems().forEach( function ( item
) {
397 // Map filter names to their parameter names
398 filterParamNames
[ item
.getName() ] = item
.getParamName();
400 if ( buildFromCurrentState
) {
401 // This means we have not been given a filter representation
402 // so we are building one based on current state
403 filterRepresentation
[ item
.getName() ] = item
.isSelected();
404 } else if ( !filterRepresentation
[ item
.getName() ] ) {
405 // We are given a filter representation, but we have to make
406 // sure that we fill in the missing filters if there are any
407 // we will assume they are all falsey
408 filterRepresentation
[ item
.getName() ] = false;
411 if ( filterRepresentation
[ item
.getName() ] ) {
412 areAnySelected
= true;
417 if ( this.getType() === 'send_unselected_if_any' ) {
418 // First, check if any of the items are selected at all.
419 // If none is selected, we're treating it as if they are
422 // Go over the items and define the correct values
423 $.each( filterRepresentation
, function ( name
, value
) {
424 result
[ filterParamNames
[ name
] ] = areAnySelected
?
425 // We must store all parameter values as strings '0' or '1'
426 String( Number( !value
) ) :
429 } else if ( this.getType() === 'string_options' ) {
432 $.each( filterRepresentation
, function ( name
, value
) {
435 values
.push( filterParamNames
[ name
] );
439 result
[ this.getName() ] = ( values
.length
=== Object
.keys( filterRepresentation
).length
) ?
440 'all' : values
.join( this.getSeparator() );
447 * Get the filter representation this group would provide
448 * based on given parameter states.
450 * @param {Object|string} [paramRepresentation] An object defining a parameter
451 * state to translate the filter state from. If not given, an object
452 * representing all filters as falsey is returned; same as if the parameter
453 * given were an empty object, or had some of the filters missing.
454 * @return {Object} Filter representation
456 mw
.rcfilters
.dm
.FilterGroup
.prototype.getFilterRepresentation = function ( paramRepresentation
) {
457 var areAnySelected
, paramValues
,
459 paramToFilterMap
= {},
462 if ( this.getType() === 'send_unselected_if_any' ) {
463 paramRepresentation
= paramRepresentation
|| {};
464 // Expand param representation to include all filters in the group
465 this.getItems().forEach( function ( filterItem
) {
466 var paramName
= filterItem
.getParamName();
468 paramRepresentation
[ paramName
] = paramRepresentation
[ paramName
] || '0';
469 paramToFilterMap
[ paramName
] = filterItem
;
471 if ( Number( paramRepresentation
[ filterItem
.getParamName() ] ) ) {
472 areAnySelected
= true;
476 $.each( paramRepresentation
, function ( paramName
, paramValue
) {
477 var filterItem
= paramToFilterMap
[ paramName
];
479 result
[ filterItem
.getName() ] = areAnySelected
?
480 // Flip the definition between the parameter
481 // state and the filter state
482 // This is what the 'toggleSelected' value of the filter is
483 !Number( paramValue
) :
484 // Otherwise, there are no selected items in the
485 // group, which means the state is false
488 } else if ( this.getType() === 'string_options' ) {
489 paramRepresentation
= paramRepresentation
|| '';
491 // Normalize the given parameter values
492 paramValues
= mw
.rcfilters
.utils
.normalizeParamOptions(
494 paramRepresentation
.split(
498 this.getItems().map( function ( filterItem
) {
499 return filterItem
.getParamName();
502 // Translate the parameter values into a filter selection state
503 this.getItems().forEach( function ( filterItem
) {
504 result
[ filterItem
.getName() ] = (
505 // If it is the word 'all'
506 paramValues
.length
=== 1 && paramValues
[ 0 ] === 'all' ||
507 // All values are written
508 paramValues
.length
=== model
.getItemCount()
510 // All true (either because all values are written or the term 'all' is written)
511 // is the same as all filters set to true
513 // Otherwise, the filter is selected only if it appears in the parameter values
514 paramValues
.indexOf( filterItem
.getParamName() ) > -1;
518 // Go over result and make sure all filters are represented.
519 // If any filters are missing, they will get a falsey value
520 this.getItems().forEach( function ( filterItem
) {
521 result
[ filterItem
.getName() ] = !!result
[ filterItem
.getName() ];
528 * Get item by its parameter name
530 * @param {string} paramName Parameter name
531 * @return {mw.rcfilters.dm.FilterItem} Filter item
533 mw
.rcfilters
.dm
.FilterGroup
.prototype.getItemByParamName = function ( paramName
) {
534 return this.getItems().filter( function ( item
) {
535 return item
.getParamName() === paramName
;
542 * @return {string} Group type
544 mw
.rcfilters
.dm
.FilterGroup
.prototype.getType = function () {
551 * @return {string} Display group
553 mw
.rcfilters
.dm
.FilterGroup
.prototype.getView = function () {
558 * Get the prefix used for the filter names inside this group.
560 * @param {string} [name] Filter name to prefix
561 * @return {string} Group prefix
563 mw
.rcfilters
.dm
.FilterGroup
.prototype.getNamePrefix = function () {
564 return this.getName() + '__';
568 * Get a filter name with the prefix used for the filter names inside this group.
570 * @param {string} name Filter name to prefix
571 * @return {string} Group prefix
573 mw
.rcfilters
.dm
.FilterGroup
.prototype.getPrefixedName = function ( name
) {
574 return this.getNamePrefix() + name
;
580 * @return {string} Title
582 mw
.rcfilters
.dm
.FilterGroup
.prototype.getTitle = function () {
587 * Get group's values separator
589 * @return {string} Values separator
591 mw
.rcfilters
.dm
.FilterGroup
.prototype.getSeparator = function () {
592 return this.separator
;
596 * Check whether the group is defined as full coverage
598 * @return {boolean} Group is full coverage
600 mw
.rcfilters
.dm
.FilterGroup
.prototype.isFullCoverage = function () {
601 return this.fullCoverage
;