1 /* eslint-disable no-restricted-properties */
4 * View model for a filter group
6 * @mixins OO.EventEmitter
7 * @mixins OO.EmitterList
10 * @param {string} name Group name
11 * @param {Object} [config] Configuration options
12 * @cfg {string} [type='send_unselected_if_any'] Group type
13 * @cfg {string} [view='default'] Name of the display group this group
15 * @cfg {boolean} [sticky] This group is 'sticky'. It is synchronized
16 * with a preference, does not participate in Saved Queries, and is
17 * not shown in the active filters area.
18 * @cfg {string} [title] Group title
19 * @cfg {boolean} [hidden] This group is hidden from the regular menu views
20 * and the active filters area.
21 * @cfg {boolean} [allowArbitrary] Allows for an arbitrary value to be added to the
22 * group from the URL, even if it wasn't initially set up.
23 * @cfg {number} [range] An object defining minimum and maximum values for numeric
24 * groups. { min: x, max: y }
25 * @cfg {number} [minValue] Minimum value for numeric groups
26 * @cfg {string} [separator='|'] Value separator for 'string_options' groups
27 * @cfg {boolean} [active] Group is active
28 * @cfg {boolean} [fullCoverage] This filters in this group collectively cover all results
29 * @cfg {Object} [conflicts] Defines the conflicts for this filter group
30 * @cfg {string|Object} [labelPrefixKey] An i18n key defining the prefix label for this
31 * group. If the prefix has 'invert' state, the parameter is expected to be an object
32 * with 'default' and 'inverted' as keys.
33 * @cfg {Object} [whatsThis] Defines the messages that should appear for the 'what's this' popup
34 * @cfg {string} [whatsThis.header] The header of the whatsThis popup message
35 * @cfg {string} [whatsThis.body] The body of the whatsThis popup message
36 * @cfg {string} [whatsThis.url] The url for the link in the whatsThis popup message
37 * @cfg {string} [whatsThis.linkMessage] The text for the link in the whatsThis popup message
38 * @cfg {boolean} [visible=true] The visibility of the group
40 mw
.rcfilters
.dm
.FilterGroup
= function MwRcfiltersDmFilterGroup( name
, config
) {
41 config
= config
|| {};
44 OO
.EventEmitter
.call( this );
45 OO
.EmitterList
.call( this );
48 this.type
= config
.type
|| 'send_unselected_if_any';
49 this.view
= config
.view
|| 'default';
50 this.sticky
= !!config
.sticky
;
51 this.title
= config
.title
|| name
;
52 this.hidden
= !!config
.hidden
;
53 this.allowArbitrary
= !!config
.allowArbitrary
;
54 this.numericRange
= config
.range
;
55 this.separator
= config
.separator
|| '|';
56 this.labelPrefixKey
= config
.labelPrefixKey
;
57 this.visible
= config
.visible
=== undefined ? true : !!config
.visible
;
59 this.currSelected
= null;
60 this.active
= !!config
.active
;
61 this.fullCoverage
= !!config
.fullCoverage
;
63 this.whatsThis
= config
.whatsThis
|| {};
65 this.conflicts
= config
.conflicts
|| {};
66 this.defaultParams
= {};
67 this.defaultFilters
= {};
69 this.aggregate( { update
: 'filterItemUpdate' } );
70 this.connect( this, { filterItemUpdate
: 'onFilterItemUpdate' } );
74 OO
.initClass( mw
.rcfilters
.dm
.FilterGroup
);
75 OO
.mixinClass( mw
.rcfilters
.dm
.FilterGroup
, OO
.EventEmitter
);
76 OO
.mixinClass( mw
.rcfilters
.dm
.FilterGroup
, OO
.EmitterList
);
83 * Group state has been updated
89 * Initialize the group and create its filter items
91 * @param {Object} filterDefinition Filter definition for this group
92 * @param {string|Object} [groupDefault] Definition of the group default
94 mw
.rcfilters
.dm
.FilterGroup
.prototype.initializeFilters = function ( filterDefinition
, groupDefault
) {
100 filterDefinition
.forEach( function ( filter
) {
101 // Instantiate an item
102 var subsetNames
= [],
103 filterItem
= new mw
.rcfilters
.dm
.FilterItem( filter
.name
, model
, {
104 group
: model
.getName(),
105 label
: filter
.label
|| filter
.name
,
106 description
: filter
.description
|| '',
107 labelPrefixKey
: model
.labelPrefixKey
,
108 cssClass
: filter
.cssClass
,
109 identifiers
: filter
.identifiers
,
110 defaultHighlightColor
: filter
.defaultHighlightColor
113 if ( filter
.subset
) {
114 filter
.subset
= filter
.subset
.map( function ( el
) {
120 filter
.subset
.forEach( function ( subsetFilterName
) { // eslint-disable-line no-loop-func
121 // Subsets (unlike conflicts) are always inside the same group
122 // We can re-map the names of the filters we are getting from
123 // the subsets with the group prefix
124 var subsetName
= model
.getPrefixedName( subsetFilterName
);
125 // For convenience, we should store each filter's "supersets" -- these are
126 // the filters that have that item in their subset list. This will just
127 // make it easier to go through whether the item has any other items
128 // that affect it (and are selected) at any given time
129 supersetMap
[ subsetName
] = supersetMap
[ subsetName
] || [];
130 mw
.rcfilters
.utils
.addArrayElementsUnique(
131 supersetMap
[ subsetName
],
135 // Translate subset param name to add the group name, so we
136 // get consistent naming. We know that subsets are only within
138 subsetNames
.push( subsetName
);
141 // Set translated subset
142 filterItem
.setSubset( subsetNames
);
145 items
.push( filterItem
);
147 // Store default parameter state; in this case, default is defined per filter
149 model
.getType() === 'send_unselected_if_any' ||
150 model
.getType() === 'boolean'
152 // Store the default parameter state
153 // For this group type, parameter values are direct
154 // We need to convert from a boolean to a string ('1' and '0')
155 model
.defaultParams
[ filter
.name
] = String( Number( filter
.default || 0 ) );
156 } else if ( model
.getType() === 'any_value' ) {
157 model
.defaultParams
[ filter
.name
] = filter
.default;
162 this.addItems( items
);
164 // Now that we have all items, we can apply the superset map
165 this.getItems().forEach( function ( filterItem
) {
166 filterItem
.setSuperset( supersetMap
[ filterItem
.getName() ] );
169 // Store default parameter state; in this case, default is defined per the
170 // entire group, given by groupDefault method parameter
171 if ( this.getType() === 'string_options' ) {
172 // Store the default parameter group state
173 // For this group, the parameter is group name and value is the names
175 this.defaultParams
[ this.getName() ] = mw
.rcfilters
.utils
.normalizeParamOptions(
178 groupDefault
.split( this.getSeparator() ) :
181 this.getItems().map( function ( item
) {
182 return item
.getParamName();
184 ).join( this.getSeparator() );
185 } else if ( this.getType() === 'single_option' ) {
186 defaultParam
= groupDefault
!== undefined ?
187 groupDefault
: this.getItems()[ 0 ].getParamName();
189 // For this group, the parameter is the group name,
190 // and a single item can be selected: default or first item
191 this.defaultParams
[ this.getName() ] = defaultParam
;
194 // add highlights to defaultParams
195 this.getItems().forEach( function ( filterItem
) {
196 if ( filterItem
.isHighlighted() ) {
197 this.defaultParams
[ filterItem
.getName() + '_color' ] = filterItem
.getHighlightColor();
201 // Store default filter state based on default params
202 this.defaultFilters
= this.getFilterRepresentation( this.getDefaultParams() );
204 // Check for filters that should be initially selected by their default value
205 if ( this.isSticky() ) {
206 // eslint-disable-next-line jquery/no-each-util
207 $.each( this.defaultFilters
, function ( filterName
, filterValue
) {
208 model
.getItemByName( filterName
).toggleSelected( filterValue
);
212 // Verify that single_option group has at least one item selected
214 this.getType() === 'single_option' &&
215 this.findSelectedItems().length
=== 0
217 defaultParam
= groupDefault
!== undefined ?
218 groupDefault
: this.getItems()[ 0 ].getParamName();
220 // Single option means there must be a single option
221 // selected, so we have to either select the default
222 // or select the first option
223 this.selectItemByParamName( defaultParam
);
228 * Respond to filterItem update event
230 * @param {mw.rcfilters.dm.FilterItem} item Updated filter item
233 mw
.rcfilters
.dm
.FilterGroup
.prototype.onFilterItemUpdate = function ( item
) {
236 active
= this.areAnySelected(),
239 if ( this.getType() === 'single_option' ) {
240 // This group must have one item selected always
241 // and must never have more than one item selected at a time
242 if ( this.findSelectedItems().length
=== 0 ) {
243 // Nothing is selected anymore
244 // Select the default or the first item
245 this.currSelected
= this.getItemByParamName( this.defaultParams
[ this.getName() ] ) ||
246 this.getItems()[ 0 ];
247 this.currSelected
.toggleSelected( true );
249 } else if ( this.findSelectedItems().length
> 1 ) {
250 // There is more than one item selected
251 // This should only happen if the item given
252 // is the one that is selected, so unselect
253 // all items that is not it
254 this.findSelectedItems().forEach( function ( itemModel
) {
255 // Note that in case the given item is actually
256 // not selected, this loop will end up unselecting
257 // all items, which would trigger the case above
258 // when the last item is unselected anyways
259 var selected
= itemModel
.getName() === item
.getName() &&
262 itemModel
.toggleSelected( selected
);
264 model
.currSelected
= itemModel
;
271 if ( this.isSticky() ) {
272 // If this group is sticky, then change the default according to the
273 // current selection.
274 this.defaultParams
= this.getParamRepresentation( this.getSelectedState() );
279 this.active
!== active
||
280 this.currSelected
!== item
282 this.active
= active
;
283 this.currSelected
= item
;
285 this.emit( 'update' );
290 * Get group active state
292 * @return {boolean} Active state
294 mw
.rcfilters
.dm
.FilterGroup
.prototype.isActive = function () {
299 * Get group hidden state
301 * @return {boolean} Hidden state
303 mw
.rcfilters
.dm
.FilterGroup
.prototype.isHidden = function () {
308 * Get group allow arbitrary state
310 * @return {boolean} Group allows an arbitrary value from the URL
312 mw
.rcfilters
.dm
.FilterGroup
.prototype.isAllowArbitrary = function () {
313 return this.allowArbitrary
;
317 * Get group maximum value for numeric groups
319 * @return {number|null} Group max value
321 mw
.rcfilters
.dm
.FilterGroup
.prototype.getMaxValue = function () {
322 return this.numericRange
&& this.numericRange
.max
!== undefined ?
323 this.numericRange
.max
: null;
327 * Get group minimum value for numeric groups
329 * @return {number|null} Group max value
331 mw
.rcfilters
.dm
.FilterGroup
.prototype.getMinValue = function () {
332 return this.numericRange
&& this.numericRange
.min
!== undefined ?
333 this.numericRange
.min
: null;
339 * @return {string} Group name
341 mw
.rcfilters
.dm
.FilterGroup
.prototype.getName = function () {
346 * Get the default param state of this group
348 * @return {Object} Default param state
350 mw
.rcfilters
.dm
.FilterGroup
.prototype.getDefaultParams = function () {
351 return this.defaultParams
;
355 * Get the default filter state of this group
357 * @return {Object} Default filter state
359 mw
.rcfilters
.dm
.FilterGroup
.prototype.getDefaultFilters = function () {
360 return this.defaultFilters
;
364 * This is for a single_option and string_options group types
365 * it returns the value of the default
367 * @return {string} Value of the default
369 mw
.rcfilters
.dm
.FilterGroup
.prototype.getDefaulParamValue = function () {
370 return this.defaultParams
[ this.getName() ];
373 * Get the messags defining the 'whats this' popup for this group
375 * @return {Object} What's this messages
377 mw
.rcfilters
.dm
.FilterGroup
.prototype.getWhatsThis = function () {
378 return this.whatsThis
;
382 * Check whether this group has a 'what's this' message
384 * @return {boolean} This group has a what's this message
386 mw
.rcfilters
.dm
.FilterGroup
.prototype.hasWhatsThis = function () {
387 return !!this.whatsThis
.body
;
391 * Get the conflicts associated with the entire group.
392 * Conflict object is set up by filter name keys and conflict
393 * definition. For example:
397 * filter: filterName,
403 * filter: filterName2,
408 * @return {Object} Conflict definition
410 mw
.rcfilters
.dm
.FilterGroup
.prototype.getConflicts = function () {
411 return this.conflicts
;
415 * Set conflicts for this group. See #getConflicts for the expected
416 * structure of the definition.
418 * @param {Object} conflicts Conflicts for this group
420 mw
.rcfilters
.dm
.FilterGroup
.prototype.setConflicts = function ( conflicts
) {
421 this.conflicts
= conflicts
;
425 * Set conflicts for each filter item in the group based on the
428 * @param {Object} conflicts Object representing the conflict map,
429 * keyed by the item name, where its value is an object for all its conflicts
431 mw
.rcfilters
.dm
.FilterGroup
.prototype.setFilterConflicts = function ( conflicts
) {
432 this.getItems().forEach( function ( filterItem
) {
433 if ( conflicts
[ filterItem
.getName() ] ) {
434 filterItem
.setConflicts( conflicts
[ filterItem
.getName() ] );
440 * Check whether this item has a potential conflict with the given item
442 * This checks whether the given item is in the list of conflicts of
443 * the current item, but makes no judgment about whether the conflict
444 * is currently at play (either one of the items may not be selected)
446 * @param {mw.rcfilters.dm.FilterItem} filterItem Filter item
447 * @return {boolean} This item has a conflict with the given item
449 mw
.rcfilters
.dm
.FilterGroup
.prototype.existsInConflicts = function ( filterItem
) {
450 return Object
.prototype.hasOwnProperty
.call( this.getConflicts(), filterItem
.getName() );
454 * Check whether there are any items selected
456 * @return {boolean} Any items in the group are selected
458 mw
.rcfilters
.dm
.FilterGroup
.prototype.areAnySelected = function () {
459 return this.getItems().some( function ( filterItem
) {
460 return filterItem
.isSelected();
465 * Check whether all items selected
467 * @return {boolean} All items are selected
469 mw
.rcfilters
.dm
.FilterGroup
.prototype.areAllSelected = function () {
473 this.getItems().forEach( function ( filterItem
) {
474 if ( filterItem
.isSelected() ) {
475 selected
.push( filterItem
);
477 unselected
.push( filterItem
);
481 if ( unselected
.length
=== 0 ) {
485 // check if every unselected is a subset of a selected
486 return unselected
.every( function ( unselectedFilterItem
) {
487 return selected
.some( function ( selectedFilterItem
) {
488 return selectedFilterItem
.existsInSubset( unselectedFilterItem
.getName() );
494 * Get all selected items in this group
496 * @param {mw.rcfilters.dm.FilterItem} [excludeItem] Item to exclude from the list
497 * @return {mw.rcfilters.dm.FilterItem[]} Selected items
499 mw
.rcfilters
.dm
.FilterGroup
.prototype.findSelectedItems = function ( excludeItem
) {
500 var excludeName
= ( excludeItem
&& excludeItem
.getName() ) || '';
502 return this.getItems().filter( function ( item
) {
503 return item
.getName() !== excludeName
&& item
.isSelected();
508 * Check whether all selected items are in conflict with the given item
510 * @param {mw.rcfilters.dm.FilterItem} filterItem Filter item to test
511 * @return {boolean} All selected items are in conflict with this item
513 mw
.rcfilters
.dm
.FilterGroup
.prototype.areAllSelectedInConflictWith = function ( filterItem
) {
514 var selectedItems
= this.findSelectedItems( filterItem
);
516 return selectedItems
.length
> 0 &&
518 // The group as a whole is in conflict with this item
519 this.existsInConflicts( filterItem
) ||
520 // All selected items are in conflict individually
521 selectedItems
.every( function ( selectedFilter
) {
522 return selectedFilter
.existsInConflicts( filterItem
);
528 * Check whether any of the selected items are in conflict with the given item
530 * @param {mw.rcfilters.dm.FilterItem} filterItem Filter item to test
531 * @return {boolean} Any of the selected items are in conflict with this item
533 mw
.rcfilters
.dm
.FilterGroup
.prototype.areAnySelectedInConflictWith = function ( filterItem
) {
534 var selectedItems
= this.findSelectedItems( filterItem
);
536 return selectedItems
.length
> 0 && (
537 // The group as a whole is in conflict with this item
538 this.existsInConflicts( filterItem
) ||
539 // Any selected items are in conflict individually
540 selectedItems
.some( function ( selectedFilter
) {
541 return selectedFilter
.existsInConflicts( filterItem
);
547 * Get the parameter representation from this group
549 * @param {Object} [filterRepresentation] An object defining the state
550 * of the filters in this group, keyed by their name and current selected
552 * @return {Object} Parameter representation
554 mw
.rcfilters
.dm
.FilterGroup
.prototype.getParamRepresentation = function ( filterRepresentation
) {
556 areAnySelected
= false,
557 buildFromCurrentState
= !filterRepresentation
,
558 defaultFilters
= this.getDefaultFilters(),
561 filterParamNames
= {},
562 getSelectedParameter = function ( filters
) {
566 // Find if any are selected
567 // eslint-disable-next-line jquery/no-each-util
568 $.each( filters
, function ( name
, value
) {
570 selected
.push( name
);
574 item
= model
.getItemByName( selected
[ 0 ] );
575 return ( item
&& item
.getParamName() ) || '';
578 filterRepresentation
= filterRepresentation
|| {};
580 // Create or complete the filterRepresentation definition
581 this.getItems().forEach( function ( item
) {
582 // Map filter names to their parameter names
583 filterParamNames
[ item
.getName() ] = item
.getParamName();
585 if ( buildFromCurrentState
) {
586 // This means we have not been given a filter representation
587 // so we are building one based on current state
588 filterRepresentation
[ item
.getName() ] = item
.getValue();
589 } else if ( filterRepresentation
[ item
.getName() ] === undefined ) {
590 // We are given a filter representation, but we have to make
591 // sure that we fill in the missing filters if there are any
592 // we will assume they are all falsey
593 if ( model
.isSticky() ) {
594 filterRepresentation
[ item
.getName() ] = !!defaultFilters
[ item
.getName() ];
596 filterRepresentation
[ item
.getName() ] = false;
600 if ( filterRepresentation
[ item
.getName() ] ) {
601 areAnySelected
= true;
607 this.getType() === 'send_unselected_if_any' ||
608 this.getType() === 'boolean' ||
609 this.getType() === 'any_value'
611 // First, check if any of the items are selected at all.
612 // If none is selected, we're treating it as if they are
615 // Go over the items and define the correct values
616 // eslint-disable-next-line jquery/no-each-util
617 $.each( filterRepresentation
, function ( name
, value
) {
618 // We must store all parameter values as strings '0' or '1'
619 if ( model
.getType() === 'send_unselected_if_any' ) {
620 result
[ filterParamNames
[ name
] ] = areAnySelected
?
621 String( Number( !value
) ) :
623 } else if ( model
.getType() === 'boolean' ) {
624 // Representation is straight-forward and direct from
625 // the parameter value to the filter state
626 result
[ filterParamNames
[ name
] ] = String( Number( !!value
) );
627 } else if ( model
.getType() === 'any_value' ) {
628 result
[ filterParamNames
[ name
] ] = value
;
631 } else if ( this.getType() === 'string_options' ) {
634 // eslint-disable-next-line jquery/no-each-util
635 $.each( filterRepresentation
, function ( name
, value
) {
638 values
.push( filterParamNames
[ name
] );
642 result
[ this.getName() ] = ( values
.length
=== Object
.keys( filterRepresentation
).length
) ?
643 'all' : values
.join( this.getSeparator() );
644 } else if ( this.getType() === 'single_option' ) {
645 result
[ this.getName() ] = getSelectedParameter( filterRepresentation
);
652 * Get the filter representation this group would provide
653 * based on given parameter states.
655 * @param {Object} [paramRepresentation] An object defining a parameter
656 * state to translate the filter state from. If not given, an object
657 * representing all filters as falsey is returned; same as if the parameter
658 * given were an empty object, or had some of the filters missing.
659 * @return {Object} Filter representation
661 mw
.rcfilters
.dm
.FilterGroup
.prototype.getFilterRepresentation = function ( paramRepresentation
) {
662 var areAnySelected
, paramValues
, item
, currentValue
,
663 oneWasSelected
= false,
664 defaultParams
= this.getDefaultParams(),
665 expandedParams
= $.extend( true, {}, paramRepresentation
),
667 paramToFilterMap
= {},
670 if ( this.isSticky() ) {
671 // If the group is sticky, check if all parameters are represented
672 // and for those that aren't represented, add them with their default
674 paramRepresentation
= $.extend( true, {}, this.getDefaultParams(), paramRepresentation
);
677 paramRepresentation
= paramRepresentation
|| {};
679 this.getType() === 'send_unselected_if_any' ||
680 this.getType() === 'boolean' ||
681 this.getType() === 'any_value'
683 // Go over param representation; map and check for selections
684 this.getItems().forEach( function ( filterItem
) {
685 var paramName
= filterItem
.getParamName();
687 expandedParams
[ paramName
] = paramRepresentation
[ paramName
] || '0';
688 paramToFilterMap
[ paramName
] = filterItem
;
690 if ( Number( paramRepresentation
[ filterItem
.getParamName() ] ) ) {
691 areAnySelected
= true;
695 // eslint-disable-next-line jquery/no-each-util
696 $.each( expandedParams
, function ( paramName
, paramValue
) {
697 var filterItem
= paramToFilterMap
[ paramName
];
699 if ( model
.getType() === 'send_unselected_if_any' ) {
700 // Flip the definition between the parameter
701 // state and the filter state
702 // This is what the 'toggleSelected' value of the filter is
703 result
[ filterItem
.getName() ] = areAnySelected
?
704 !Number( paramValue
) :
705 // Otherwise, there are no selected items in the
706 // group, which means the state is false
708 } else if ( model
.getType() === 'boolean' ) {
709 // Straight-forward definition of state
710 result
[ filterItem
.getName() ] = !!Number( paramRepresentation
[ filterItem
.getParamName() ] );
711 } else if ( model
.getType() === 'any_value' ) {
712 result
[ filterItem
.getName() ] = paramRepresentation
[ filterItem
.getParamName() ];
715 } else if ( this.getType() === 'string_options' ) {
716 currentValue
= paramRepresentation
[ this.getName() ] || '';
718 // Normalize the given parameter values
719 paramValues
= mw
.rcfilters
.utils
.normalizeParamOptions(
725 this.getItems().map( function ( filterItem
) {
726 return filterItem
.getParamName();
729 // Translate the parameter values into a filter selection state
730 this.getItems().forEach( function ( filterItem
) {
731 // All true (either because all values are written or the term 'all' is written)
732 // is the same as all filters set to true
733 result
[ filterItem
.getName() ] = (
734 // If it is the word 'all'
735 paramValues
.length
=== 1 && paramValues
[ 0 ] === 'all' ||
736 // All values are written
737 paramValues
.length
=== model
.getItemCount()
740 // Otherwise, the filter is selected only if it appears in the parameter values
741 paramValues
.indexOf( filterItem
.getParamName() ) > -1;
743 } else if ( this.getType() === 'single_option' ) {
744 // There is parameter that fits a single filter and if not, get the default
745 this.getItems().forEach( function ( filterItem
) {
746 var selected
= filterItem
.getParamName() === paramRepresentation
[ model
.getName() ];
748 result
[ filterItem
.getName() ] = selected
;
749 oneWasSelected
= oneWasSelected
|| selected
;
753 // Go over result and make sure all filters are represented.
754 // If any filters are missing, they will get a falsey value
755 this.getItems().forEach( function ( filterItem
) {
756 if ( result
[ filterItem
.getName() ] === undefined ) {
757 result
[ filterItem
.getName() ] = this.getFalsyValue();
761 // Make sure that at least one option is selected in
762 // single_option groups, no matter what path was taken
763 // If none was selected by the given definition, then
764 // we need to select the one in the base state -- either
765 // the default given, or the first item
767 this.getType() === 'single_option' &&
770 item
= this.getItems()[ 0 ];
771 if ( defaultParams
[ this.getName() ] ) {
772 item
= this.getItemByParamName( defaultParams
[ this.getName() ] );
775 result
[ item
.getName() ] = true;
782 * @return {*} The appropriate falsy value for this group type
784 mw
.rcfilters
.dm
.FilterGroup
.prototype.getFalsyValue = function () {
785 return this.getType() === 'any_value' ? '' : false;
789 * Get current selected state of all filter items in this group
791 * @return {Object} Selected state
793 mw
.rcfilters
.dm
.FilterGroup
.prototype.getSelectedState = function () {
796 this.getItems().forEach( function ( filterItem
) {
797 state
[ filterItem
.getName() ] = filterItem
.getValue();
804 * Get item by its filter name
806 * @param {string} filterName Filter name
807 * @return {mw.rcfilters.dm.FilterItem} Filter item
809 mw
.rcfilters
.dm
.FilterGroup
.prototype.getItemByName = function ( filterName
) {
810 return this.getItems().filter( function ( item
) {
811 return item
.getName() === filterName
;
816 * Select an item by its parameter name
818 * @param {string} paramName Filter parameter name
820 mw
.rcfilters
.dm
.FilterGroup
.prototype.selectItemByParamName = function ( paramName
) {
821 this.getItems().forEach( function ( item
) {
822 item
.toggleSelected( item
.getParamName() === String( paramName
) );
827 * Get item by its parameter name
829 * @param {string} paramName Parameter name
830 * @return {mw.rcfilters.dm.FilterItem} Filter item
832 mw
.rcfilters
.dm
.FilterGroup
.prototype.getItemByParamName = function ( paramName
) {
833 return this.getItems().filter( function ( item
) {
834 return item
.getParamName() === String( paramName
);
841 * @return {string} Group type
843 mw
.rcfilters
.dm
.FilterGroup
.prototype.getType = function () {
848 * Check whether this group is represented by a single parameter
849 * or whether each item is its own parameter
851 * @return {boolean} This group is a single parameter
853 mw
.rcfilters
.dm
.FilterGroup
.prototype.isPerGroupRequestParameter = function () {
855 this.getType() === 'string_options' ||
856 this.getType() === 'single_option'
863 * @return {string} Display group
865 mw
.rcfilters
.dm
.FilterGroup
.prototype.getView = function () {
870 * Get the prefix used for the filter names inside this group.
872 * @param {string} [name] Filter name to prefix
873 * @return {string} Group prefix
875 mw
.rcfilters
.dm
.FilterGroup
.prototype.getNamePrefix = function () {
876 return this.getName() + '__';
880 * Get a filter name with the prefix used for the filter names inside this group.
882 * @param {string} name Filter name to prefix
883 * @return {string} Group prefix
885 mw
.rcfilters
.dm
.FilterGroup
.prototype.getPrefixedName = function ( name
) {
886 return this.getNamePrefix() + name
;
892 * @return {string} Title
894 mw
.rcfilters
.dm
.FilterGroup
.prototype.getTitle = function () {
899 * Get group's values separator
901 * @return {string} Values separator
903 mw
.rcfilters
.dm
.FilterGroup
.prototype.getSeparator = function () {
904 return this.separator
;
908 * Check whether the group is defined as full coverage
910 * @return {boolean} Group is full coverage
912 mw
.rcfilters
.dm
.FilterGroup
.prototype.isFullCoverage = function () {
913 return this.fullCoverage
;
917 * Check whether the group is defined as sticky default
919 * @return {boolean} Group is sticky default
921 mw
.rcfilters
.dm
.FilterGroup
.prototype.isSticky = function () {
926 * Normalize a value given to this group. This is mostly for correcting
927 * arbitrary values for 'single option' groups, given by the user settings
928 * or the URL that can go outside the limits that are allowed.
930 * @param {string} value Given value
931 * @return {string} Corrected value
933 mw
.rcfilters
.dm
.FilterGroup
.prototype.normalizeArbitraryValue = function ( value
) {
935 this.getType() === 'single_option' &&
936 this.isAllowArbitrary()
939 this.getMaxValue() !== null &&
940 value
> this.getMaxValue()
942 // Change the value to the actual max value
943 return String( this.getMaxValue() );
945 this.getMinValue() !== null &&
946 value
< this.getMinValue()
948 // Change the value to the actual min value
949 return String( this.getMinValue() );
957 * Toggle the visibility of this group
959 * @param {boolean} [isVisible] Item is visible
961 mw
.rcfilters
.dm
.FilterGroup
.prototype.toggleVisible = function ( isVisible
) {
962 isVisible
= isVisible
=== undefined ? !this.visible
: isVisible
;
964 if ( this.visible
!== isVisible
) {
965 this.visible
= isVisible
;
966 this.emit( 'update' );
971 * Check whether the group is visible
973 * @return {boolean} Group is visible
975 mw
.rcfilters
.dm
.FilterGroup
.prototype.isVisible = function () {
980 * Set the visibility of the items under this group by the given items array
982 * @param {mw.rcfilters.dm.ItemModel[]} visibleItems An array of visible items
984 mw
.rcfilters
.dm
.FilterGroup
.prototype.setVisibleItems = function ( visibleItems
) {
985 this.getItems().forEach( function ( itemModel
) {
986 itemModel
.toggleVisible( visibleItems
.indexOf( itemModel
) !== -1 );