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 {boolean} [hidden] This group is hidden from the regular menu views
16 * @cfg {boolean} [allowArbitrary] Allows for an arbitrary value to be added to the
17 * group from the URL, even if it wasn't initially set up.
18 * @cfg {string} [separator='|'] Value separator for 'string_options' groups
19 * @cfg {boolean} [active] Group is active
20 * @cfg {boolean} [fullCoverage] This filters in this group collectively cover all results
21 * @cfg {Object} [conflicts] Defines the conflicts for this filter group
22 * @cfg {string|Object} [labelPrefixKey] An i18n key defining the prefix label for this
23 * group. If the prefix has 'invert' state, the parameter is expected to be an object
24 * with 'default' and 'inverted' as keys.
25 * @cfg {Object} [whatsThis] Defines the messages that should appear for the 'what's this' popup
26 * @cfg {string} [whatsThis.header] The header of the whatsThis popup message
27 * @cfg {string} [whatsThis.body] The body of the whatsThis popup message
28 * @cfg {string} [whatsThis.url] The url for the link in the whatsThis popup message
29 * @cfg {string} [whatsThis.linkMessage] The text for the link in the whatsThis popup message
31 mw
.rcfilters
.dm
.FilterGroup
= function MwRcfiltersDmFilterGroup( name
, config
) {
32 config
= config
|| {};
35 OO
.EventEmitter
.call( this );
36 OO
.EmitterList
.call( this );
39 this.type
= config
.type
|| 'send_unselected_if_any';
40 this.view
= config
.view
|| 'default';
41 this.title
= config
.title
|| name
;
42 this.hidden
= !!config
.hidden
;
43 this.allowArbitrary
= !!config
.allowArbitrary
;
44 this.separator
= config
.separator
|| '|';
45 this.labelPrefixKey
= config
.labelPrefixKey
;
47 this.currSelected
= null;
48 this.active
= !!config
.active
;
49 this.fullCoverage
= !!config
.fullCoverage
;
51 this.whatsThis
= config
.whatsThis
|| {};
53 this.conflicts
= config
.conflicts
|| {};
54 this.defaultParams
= {};
56 this.aggregate( { update
: 'filterItemUpdate' } );
57 this.connect( this, { filterItemUpdate
: 'onFilterItemUpdate' } );
61 OO
.initClass( mw
.rcfilters
.dm
.FilterGroup
);
62 OO
.mixinClass( mw
.rcfilters
.dm
.FilterGroup
, OO
.EventEmitter
);
63 OO
.mixinClass( mw
.rcfilters
.dm
.FilterGroup
, OO
.EmitterList
);
70 * Group state has been updated
76 * Initialize the group and create its filter items
78 * @param {Object} filterDefinition Filter definition for this group
79 * @param {string|Object} [groupDefault] Definition of the group default
81 mw
.rcfilters
.dm
.FilterGroup
.prototype.initializeFilters = function ( filterDefinition
, groupDefault
) {
87 filterDefinition
.forEach( function ( filter
) {
88 // Instantiate an item
90 filterItem
= new mw
.rcfilters
.dm
.FilterItem( filter
.name
, model
, {
91 group
: model
.getName(),
92 label
: filter
.label
|| filter
.name
,
93 description
: filter
.description
|| '',
94 labelPrefixKey
: model
.labelPrefixKey
,
95 cssClass
: filter
.cssClass
,
96 identifiers
: filter
.identifiers
99 if ( filter
.subset
) {
100 filter
.subset
= filter
.subset
.map( function ( el
) {
106 filter
.subset
.forEach( function ( subsetFilterName
) { // eslint-disable-line no-loop-func
107 // Subsets (unlike conflicts) are always inside the same group
108 // We can re-map the names of the filters we are getting from
109 // the subsets with the group prefix
110 var subsetName
= model
.getPrefixedName( subsetFilterName
);
111 // For convenience, we should store each filter's "supersets" -- these are
112 // the filters that have that item in their subset list. This will just
113 // make it easier to go through whether the item has any other items
114 // that affect it (and are selected) at any given time
115 supersetMap
[ subsetName
] = supersetMap
[ subsetName
] || [];
116 mw
.rcfilters
.utils
.addArrayElementsUnique(
117 supersetMap
[ subsetName
],
121 // Translate subset param name to add the group name, so we
122 // get consistent naming. We know that subsets are only within
124 subsetNames
.push( subsetName
);
127 // Set translated subset
128 filterItem
.setSubset( subsetNames
);
131 items
.push( filterItem
);
133 // Store default parameter state; in this case, default is defined per filter
134 if ( model
.getType() === 'send_unselected_if_any' ) {
135 // Store the default parameter state
136 // For this group type, parameter values are direct
137 // We need to convert from a boolean to a string ('1' and '0')
138 model
.defaultParams
[ filter
.name
] = String( Number( !!filter
.default ) );
143 this.addItems( items
);
145 // Now that we have all items, we can apply the superset map
146 this.getItems().forEach( function ( filterItem
) {
147 filterItem
.setSuperset( supersetMap
[ filterItem
.getName() ] );
150 // Store default parameter state; in this case, default is defined per the
151 // entire group, given by groupDefault method parameter
152 if ( this.getType() === 'string_options' ) {
153 // Store the default parameter group state
154 // For this group, the parameter is group name and value is the names
156 this.defaultParams
[ this.getName() ] = mw
.rcfilters
.utils
.normalizeParamOptions(
159 groupDefault
.split( this.getSeparator() ) :
162 this.getItems().map( function ( item
) {
163 return item
.getParamName();
165 ).join( this.getSeparator() );
166 } else if ( this.getType() === 'single_option' ) {
167 defaultParam
= groupDefault
!== undefined ?
168 groupDefault
: this.getItems()[ 0 ].getParamName();
170 // For this group, the parameter is the group name,
171 // and a single item can be selected: default or first item
172 this.defaultParams
[ this.getName() ] = defaultParam
;
174 // Single option means there must be a single option
175 // selected, so we have to either select the default
176 // or select the first option
177 this.selectItemByParamName( defaultParam
);
182 * Respond to filterItem update event
184 * @param {mw.rcfilters.dm.FilterItem} item Updated filter item
187 mw
.rcfilters
.dm
.FilterGroup
.prototype.onFilterItemUpdate = function ( item
) {
189 var active
= this.areAnySelected();
193 this.getType() === 'single_option' &&
195 this.currSelected
!== item
197 this.currSelected
.toggleSelected( false );
201 this.active
!== active
||
202 this.currSelected
!== item
204 this.active
= active
;
205 this.currSelected
= item
;
207 this.emit( 'update' );
212 * Get group active state
214 * @return {boolean} Active state
216 mw
.rcfilters
.dm
.FilterGroup
.prototype.isActive = function () {
221 * Get group hidden state
223 * @return {boolean} Hidden state
225 mw
.rcfilters
.dm
.FilterGroup
.prototype.isHidden = function () {
230 * Get group allow arbitrary state
232 * @return {boolean} Group allows an arbitrary value from the URL
234 mw
.rcfilters
.dm
.FilterGroup
.prototype.isAllowArbitrary = function () {
235 return this.allowArbitrary
;
241 * @return {string} Group name
243 mw
.rcfilters
.dm
.FilterGroup
.prototype.getName = function () {
248 * Get the default param state of this group
250 * @return {Object} Default param state
252 mw
.rcfilters
.dm
.FilterGroup
.prototype.getDefaultParams = function () {
253 return this.defaultParams
;
257 * This is for a single_option and string_options group types
258 * it returns the value of the default
260 * @return {string} Value of the default
262 mw
.rcfilters
.dm
.FilterGroup
.prototype.getDefaulParamValue = function () {
263 return this.defaultParams
[ this.getName() ];
266 * Get the messags defining the 'whats this' popup for this group
268 * @return {Object} What's this messages
270 mw
.rcfilters
.dm
.FilterGroup
.prototype.getWhatsThis = function () {
271 return this.whatsThis
;
275 * Check whether this group has a 'what's this' message
277 * @return {boolean} This group has a what's this message
279 mw
.rcfilters
.dm
.FilterGroup
.prototype.hasWhatsThis = function () {
280 return !!this.whatsThis
.body
;
284 * Get the conflicts associated with the entire group.
285 * Conflict object is set up by filter name keys and conflict
286 * definition. For example:
290 * filter: filterName,
296 * filter: filterName2,
301 * @return {Object} Conflict definition
303 mw
.rcfilters
.dm
.FilterGroup
.prototype.getConflicts = function () {
304 return this.conflicts
;
308 * Set conflicts for this group. See #getConflicts for the expected
309 * structure of the definition.
311 * @param {Object} conflicts Conflicts for this group
313 mw
.rcfilters
.dm
.FilterGroup
.prototype.setConflicts = function ( conflicts
) {
314 this.conflicts
= conflicts
;
318 * Set conflicts for each filter item in the group based on the
321 * @param {Object} conflicts Object representing the conflict map,
322 * keyed by the item name, where its value is an object for all its conflicts
324 mw
.rcfilters
.dm
.FilterGroup
.prototype.setFilterConflicts = function ( conflicts
) {
325 this.getItems().forEach( function ( filterItem
) {
326 if ( conflicts
[ filterItem
.getName() ] ) {
327 filterItem
.setConflicts( conflicts
[ filterItem
.getName() ] );
333 * Check whether this item has a potential conflict with the given item
335 * This checks whether the given item is in the list of conflicts of
336 * the current item, but makes no judgment about whether the conflict
337 * is currently at play (either one of the items may not be selected)
339 * @param {mw.rcfilters.dm.FilterItem} filterItem Filter item
340 * @return {boolean} This item has a conflict with the given item
342 mw
.rcfilters
.dm
.FilterGroup
.prototype.existsInConflicts = function ( filterItem
) {
343 return Object
.prototype.hasOwnProperty
.call( this.getConflicts(), filterItem
.getName() );
347 * Check whether there are any items selected
349 * @return {boolean} Any items in the group are selected
351 mw
.rcfilters
.dm
.FilterGroup
.prototype.areAnySelected = function () {
352 return this.getItems().some( function ( filterItem
) {
353 return filterItem
.isSelected();
358 * Check whether all items selected
360 * @return {boolean} All items are selected
362 mw
.rcfilters
.dm
.FilterGroup
.prototype.areAllSelected = function () {
366 this.getItems().forEach( function ( filterItem
) {
367 if ( filterItem
.isSelected() ) {
368 selected
.push( filterItem
);
370 unselected
.push( filterItem
);
374 if ( unselected
.length
=== 0 ) {
378 // check if every unselected is a subset of a selected
379 return unselected
.every( function ( unselectedFilterItem
) {
380 return selected
.some( function ( selectedFilterItem
) {
381 return selectedFilterItem
.existsInSubset( unselectedFilterItem
.getName() );
387 * Get all selected items in this group
389 * @param {mw.rcfilters.dm.FilterItem} [excludeItem] Item to exclude from the list
390 * @return {mw.rcfilters.dm.FilterItem[]} Selected items
392 mw
.rcfilters
.dm
.FilterGroup
.prototype.getSelectedItems = function ( excludeItem
) {
393 var excludeName
= ( excludeItem
&& excludeItem
.getName() ) || '';
395 return this.getItems().filter( function ( item
) {
396 return item
.getName() !== excludeName
&& item
.isSelected();
401 * Check whether all selected items are in conflict with the given item
403 * @param {mw.rcfilters.dm.FilterItem} filterItem Filter item to test
404 * @return {boolean} All selected items are in conflict with this item
406 mw
.rcfilters
.dm
.FilterGroup
.prototype.areAllSelectedInConflictWith = function ( filterItem
) {
407 var selectedItems
= this.getSelectedItems( filterItem
);
409 return selectedItems
.length
> 0 &&
411 // The group as a whole is in conflict with this item
412 this.existsInConflicts( filterItem
) ||
413 // All selected items are in conflict individually
414 selectedItems
.every( function ( selectedFilter
) {
415 return selectedFilter
.existsInConflicts( filterItem
);
421 * Check whether any of the selected items are in conflict with the given item
423 * @param {mw.rcfilters.dm.FilterItem} filterItem Filter item to test
424 * @return {boolean} Any of the selected items are in conflict with this item
426 mw
.rcfilters
.dm
.FilterGroup
.prototype.areAnySelectedInConflictWith = function ( filterItem
) {
427 var selectedItems
= this.getSelectedItems( filterItem
);
429 return selectedItems
.length
> 0 && (
430 // The group as a whole is in conflict with this item
431 this.existsInConflicts( filterItem
) ||
432 // Any selected items are in conflict individually
433 selectedItems
.some( function ( selectedFilter
) {
434 return selectedFilter
.existsInConflicts( filterItem
);
440 * Get the parameter representation from this group
442 * @param {Object} [filterRepresentation] An object defining the state
443 * of the filters in this group, keyed by their name and current selected
445 * @return {Object} Parameter representation
447 mw
.rcfilters
.dm
.FilterGroup
.prototype.getParamRepresentation = function ( filterRepresentation
) {
449 areAnySelected
= false,
450 buildFromCurrentState
= !filterRepresentation
,
453 filterParamNames
= {},
454 getSelectedParameter = function ( filters
) {
458 // Find if any are selected
459 $.each( filters
, function ( name
, value
) {
461 selected
.push( name
);
465 item
= model
.getItemByName( selected
[ 0 ] );
466 return ( item
&& item
.getParamName() ) || '';
469 filterRepresentation
= filterRepresentation
|| {};
471 // Create or complete the filterRepresentation definition
472 this.getItems().forEach( function ( item
) {
473 // Map filter names to their parameter names
474 filterParamNames
[ item
.getName() ] = item
.getParamName();
476 if ( buildFromCurrentState
) {
477 // This means we have not been given a filter representation
478 // so we are building one based on current state
479 filterRepresentation
[ item
.getName() ] = item
.isSelected();
480 } else if ( !filterRepresentation
[ item
.getName() ] ) {
481 // We are given a filter representation, but we have to make
482 // sure that we fill in the missing filters if there are any
483 // we will assume they are all falsey
484 filterRepresentation
[ item
.getName() ] = false;
487 if ( filterRepresentation
[ item
.getName() ] ) {
488 areAnySelected
= true;
493 if ( this.getType() === 'send_unselected_if_any' ) {
494 // First, check if any of the items are selected at all.
495 // If none is selected, we're treating it as if they are
498 // Go over the items and define the correct values
499 $.each( filterRepresentation
, function ( name
, value
) {
500 // We must store all parameter values as strings '0' or '1'
501 result
[ filterParamNames
[ name
] ] = areAnySelected
?
502 String( Number( !value
) ) :
505 } else if ( this.getType() === 'string_options' ) {
508 $.each( filterRepresentation
, function ( name
, value
) {
511 values
.push( filterParamNames
[ name
] );
515 result
[ this.getName() ] = ( values
.length
=== Object
.keys( filterRepresentation
).length
) ?
516 'all' : values
.join( this.getSeparator() );
517 } else if ( this.getType() === 'single_option' ) {
518 result
[ this.getName() ] = getSelectedParameter( filterRepresentation
);
525 * Get the filter representation this group would provide
526 * based on given parameter states.
528 * @param {Object|string} [paramRepresentation] An object defining a parameter
529 * state to translate the filter state from. If not given, an object
530 * representing all filters as falsey is returned; same as if the parameter
531 * given were an empty object, or had some of the filters missing.
532 * @return {Object} Filter representation
534 mw
.rcfilters
.dm
.FilterGroup
.prototype.getFilterRepresentation = function ( paramRepresentation
) {
535 var areAnySelected
, paramValues
, defaultValue
, item
,
536 oneWasSelected
= false,
538 paramToFilterMap
= {},
541 if ( this.getType() === 'send_unselected_if_any' ) {
542 paramRepresentation
= paramRepresentation
|| {};
543 // Expand param representation to include all filters in the group
544 this.getItems().forEach( function ( filterItem
) {
545 var paramName
= filterItem
.getParamName();
547 paramRepresentation
[ paramName
] = paramRepresentation
[ paramName
] || '0';
548 paramToFilterMap
[ paramName
] = filterItem
;
550 if ( Number( paramRepresentation
[ filterItem
.getParamName() ] ) ) {
551 areAnySelected
= true;
555 $.each( paramRepresentation
, function ( paramName
, paramValue
) {
556 var filterItem
= paramToFilterMap
[ paramName
];
558 // Flip the definition between the parameter
559 // state and the filter state
560 // This is what the 'toggleSelected' value of the filter is
561 result
[ filterItem
.getName() ] = areAnySelected
?
562 !Number( paramValue
) :
563 // Otherwise, there are no selected items in the
564 // group, which means the state is false
567 } else if ( this.getType() === 'string_options' ) {
568 paramRepresentation
= paramRepresentation
|| '';
570 // Normalize the given parameter values
571 paramValues
= mw
.rcfilters
.utils
.normalizeParamOptions(
573 paramRepresentation
.split(
577 this.getItems().map( function ( filterItem
) {
578 return filterItem
.getParamName();
581 // Translate the parameter values into a filter selection state
582 this.getItems().forEach( function ( filterItem
) {
583 // All true (either because all values are written or the term 'all' is written)
584 // is the same as all filters set to true
585 result
[ filterItem
.getName() ] = (
586 // If it is the word 'all'
587 paramValues
.length
=== 1 && paramValues
[ 0 ] === 'all' ||
588 // All values are written
589 paramValues
.length
=== model
.getItemCount()
592 // Otherwise, the filter is selected only if it appears in the parameter values
593 paramValues
.indexOf( filterItem
.getParamName() ) > -1;
595 } else if ( this.getType() === 'single_option' ) {
596 // There is parameter that fits a single filter and if not, get the default
597 this.getItems().forEach( function ( filterItem
) {
598 result
[ filterItem
.getName() ] = filterItem
.getParamName() === paramRepresentation
;
599 oneWasSelected
= oneWasSelected
|| filterItem
.getParamName() === paramRepresentation
;
603 // Go over result and make sure all filters are represented.
604 // If any filters are missing, they will get a falsey value
605 this.getItems().forEach( function ( filterItem
) {
606 result
[ filterItem
.getName() ] = !!result
[ filterItem
.getName() ];
607 oneWasSelected
= oneWasSelected
|| !!result
[ filterItem
.getName() ];
610 // Make sure that at least one option is selected in
611 // single_option groups, no matter what path was taken
612 // If none was selected by the given definition, then
613 // we need to select the one in the base state -- either
614 // the default given, or the first item
616 this.getType() === 'single_option' &&
619 defaultValue
= this.getDefaultParams();
620 item
= this.getItemByParamName( defaultValue
[ this.getName() ] );
621 result
[ item
.getName() ] = true;
628 * Get item by its filter name
630 * @param {string} filterName Filter name
631 * @return {mw.rcfilters.dm.FilterItem} Filter item
633 mw
.rcfilters
.dm
.FilterGroup
.prototype.getItemByName = function ( filterName
) {
634 return this.getItems().filter( function ( item
) {
635 return item
.getName() === filterName
;
640 * Select an item by its parameter name
642 * @param {string} paramName Filter parameter name
644 mw
.rcfilters
.dm
.FilterGroup
.prototype.selectItemByParamName = function ( paramName
) {
645 this.getItems().forEach( function ( item
) {
646 item
.toggleSelected( item
.getParamName() === paramName
);
651 * Get item by its parameter name
653 * @param {string} paramName Parameter name
654 * @return {mw.rcfilters.dm.FilterItem} Filter item
656 mw
.rcfilters
.dm
.FilterGroup
.prototype.getItemByParamName = function ( paramName
) {
657 return this.getItems().filter( function ( item
) {
658 return item
.getParamName() === paramName
;
665 * @return {string} Group type
667 mw
.rcfilters
.dm
.FilterGroup
.prototype.getType = function () {
674 * @return {string} Display group
676 mw
.rcfilters
.dm
.FilterGroup
.prototype.getView = function () {
681 * Get the prefix used for the filter names inside this group.
683 * @param {string} [name] Filter name to prefix
684 * @return {string} Group prefix
686 mw
.rcfilters
.dm
.FilterGroup
.prototype.getNamePrefix = function () {
687 return this.getName() + '__';
691 * Get a filter name with the prefix used for the filter names inside this group.
693 * @param {string} name Filter name to prefix
694 * @return {string} Group prefix
696 mw
.rcfilters
.dm
.FilterGroup
.prototype.getPrefixedName = function ( name
) {
697 return this.getNamePrefix() + name
;
703 * @return {string} Title
705 mw
.rcfilters
.dm
.FilterGroup
.prototype.getTitle = function () {
710 * Get group's values separator
712 * @return {string} Values separator
714 mw
.rcfilters
.dm
.FilterGroup
.prototype.getSeparator = function () {
715 return this.separator
;
719 * Check whether the group is defined as full coverage
721 * @return {boolean} Group is full coverage
723 mw
.rcfilters
.dm
.FilterGroup
.prototype.isFullCoverage = function () {
724 return this.fullCoverage
;