Debian Codegraph @

resources/src/mediawiki.rcfilters/dm/FilterGroup.js

raw symbols SCIP
1 const FilterItem = require( './FilterItem.js' ),
2 utils = require( '../utils.js' );
3
4 /**
5 * View model for a filter group.
6 *
7 * @class mw.rcfilters.dm.FilterGroup
8 * @ignore
9 * @mixes OO.EventEmitter
10 * @mixes OO.EmitterList
11 *
12 * @param {string} name Group name
13 * @param {Object} [config] Configuration options
14 * @param {string} [config.type='send_unselected_if_any'] Group type
15 * @param {string} [config.view='default'] Name of the display group this group
16 * is a part of.
17 * @param {boolean} [config.sticky] This group is 'sticky'. It is synchronized
18 * with a preference, does not participate in Saved Queries, and is
19 * not shown in the active filters area.
20 * @param {string} [config.title] Group title
21 * @param {boolean} [config.hidden] This group is hidden from the regular menu views
22 * and the active filters area.
23 * @param {boolean} [config.allowArbitrary] Allows for an arbitrary value to be added to the
24 * group from the URL, even if it wasn't initially set up.
25 * @param {number} [config.range] An object defining minimum and maximum values for numeric
26 * groups. { min: x, max: y }
27 * @param {number} [config.minValue] Minimum value for numeric groups
28 * @param {string} [config.separator='|'] Value separator for 'string_options' groups
29 * @param {boolean} [config.supportsAll=true] For 'string_options' groups, whether the magic 'all' value
30 * is understood to mean all options are selected.
31 * @param {boolean} [config.active] Group is active
32 * @param {boolean} [config.fullCoverage] This filters in this group collectively cover all results
33 * @param {Object} [config.conflicts] Defines the conflicts for this filter group
34 * @param {string|Object} [config.labelPrefixKey] An i18n key defining the prefix label for this
35 * group. If the prefix has 'invert' state, the parameter is expected to be an object
36 * with 'default' and 'inverted' as keys.
37 * @param {Object} [config.whatsThis] Defines the messages that should appear for the 'what's this' popup
38 * @param {string} [config.whatsThis.header] The header of the whatsThis popup message
39 * @param {string} [config.whatsThis.body] The body of the whatsThis popup message
40 * @param {string} [config.whatsThis.url] The url for the link in the whatsThis popup message
41 * @param {string} [config.whatsThis.linkMessage] The text for the link in the whatsThis popup message
42 * @param {boolean} [config.visible=true] The visibility of the group
43 */
44 const FilterGroup = function MwRcfiltersDmFilterGroup( name, config ) {
45 config = config || {};
46
47 // Mixin constructor
48 OO.EventEmitter.call( this );
49 OO.EmitterList.call( this );
50
51 this.name = name;
52 this.type = config.type || 'send_unselected_if_any';
53 this.view = config.view || 'default';
54 this.sticky = !!config.sticky;
55 this.title = config.title || name;
56 this.hidden = !!config.hidden;
57 this.allowArbitrary = !!config.allowArbitrary;
58 this.numericRange = config.range;
59 this.separator = config.separator || '|';
60 this.supportsAll = config.supportsAll === undefined ? true : !!config.supportsAll;
61 this.labelPrefixKey = config.labelPrefixKey;
62 this.visible = config.visible === undefined ? true : !!config.visible;
63
64 this.currSelected = null;
65 this.active = !!config.active;
66 this.fullCoverage = !!config.fullCoverage;
67
68 this.whatsThis = config.whatsThis || {};
69
70 this.conflicts = config.conflicts || {};
71 this.defaultParams = {};
72 this.defaultFilters = {};
73
74 this.aggregate( { update: 'filterItemUpdate' } );
75 this.connect( this, { filterItemUpdate: 'onFilterItemUpdate' } );
76 };
77
78 /* Initialization */
79 OO.initClass( FilterGroup );
80 OO.mixinClass( FilterGroup, OO.EventEmitter );
81 OO.mixinClass( FilterGroup, OO.EmitterList );
82
83 /* Events */
84
85 /**
86 * Group state has been updated.
87 *
88 * @event update
89 * @ignore
90 */
91
92 /* Methods */
93
94 /**
95 * Initialize the group and create its filter items
96 *
97 * @param {Object} filterDefinition Filter definition for this group
98 * @param {string|Object} [groupDefault] Definition of the group default
99 */
100 FilterGroup.prototype.initializeFilters = function ( filterDefinition, groupDefault ) {
101 let defaultParam;
102 const supersetMap = {},
103 model = this,
104 items = [];
105
106 filterDefinition.forEach( ( filter ) => {
107 // Instantiate an item
108 const filterItem = new FilterItem( filter.name, model, {
109 group: model.getName(),
110 label: filter.label || filter.name,
111 description: filter.description || '',
112 labelPrefixKey: model.labelPrefixKey,
113 cssClass: filter.cssClass,
114 helpLink: filter.helpLink,
115 identifiers: filter.identifiers,
116 defaultHighlightColor: filter.defaultHighlightColor
117 } );
118
119 if ( filter.subset ) {
120 filter.subset = filter.subset.map( ( el ) => el.filter );
121
122 const subsetNames = [];
123
124 filter.subset.forEach( ( subsetFilterName ) => {
125 // Subsets (unlike conflicts) are always inside the same group
126 // We can re-map the names of the filters we are getting from
127 // the subsets with the group prefix
128 const subsetName = model.getPrefixedName( subsetFilterName );
129 // For convenience, we should store each filter's "supersets" -- these are
130 // the filters that have that item in their subset list. This will just
131 // make it easier to go through whether the item has any other items
132 // that affect it (and are selected) at any given time
133 supersetMap[ subsetName ] = supersetMap[ subsetName ] || [];
134 utils.addArrayElementsUnique(
135 supersetMap[ subsetName ],
136 filterItem.getName()
137 );
138
139 // Translate subset param name to add the group name, so we
140 // get consistent naming. We know that subsets are only within
141 // the same group
142 subsetNames.push( subsetName );
143 } );
144
145 // Set translated subset
146 filterItem.setSubset( subsetNames );
147 }
148
149 items.push( filterItem );
150
151 // Store default parameter state; in this case, default is defined per filter
152 if (
153 model.getType() === 'send_unselected_if_any' ||
154 model.getType() === 'boolean'
155 ) {
156 // Store the default parameter state
157 // For this group type, parameter values are direct
158 // We need to convert from a boolean to a string ('1' and '0')
159 model.defaultParams[ filter.name ] = String( Number( filter.default || 0 ) );
160 } else if ( model.getType() === 'any_value' ) {
161 model.defaultParams[ filter.name ] = filter.default;
162 }
163 } );
164
165 // Add items
166 this.addItems( items );
167
168 // Now that we have all items, we can apply the superset map
169 this.getItems().forEach( ( filterItem ) => {
170 filterItem.setSuperset( supersetMap[ filterItem.getName() ] );
171 } );
172
173 // Store default parameter state; in this case, default is defined per the
174 // entire group, given by groupDefault method parameter
175 if ( this.getType() === 'string_options' ) {
176 // Store the default parameter group state
177 // For this group, the parameter is group name and value is the names
178 // of selected items
179 this.defaultParams[ this.getName() ] = utils.normalizeParamOptions(
180 // Current values
181 groupDefault ?
182 groupDefault.split( this.getSeparator() ) :
183 [],
184 // Legal values
185 this.getItems().map( ( item ) => item.getParamName() )
186 ).join( this.getSeparator() );
187 } else if ( this.getType() === 'single_option' ) {
188 defaultParam = groupDefault !== undefined ?
189 groupDefault : this.getItems()[ 0 ].getParamName();
190
191 // For this group, the parameter is the group name,
192 // and a single item can be selected: default or first item
193 this.defaultParams[ this.getName() ] = defaultParam;
194 }
195
196 // add highlights to defaultParams
197 this.getItems().forEach( ( filterItem ) => {
198 if ( filterItem.isHighlighted() ) {
199 this.defaultParams[ filterItem.getName() + '_color' ] = filterItem.getHighlightColor();
200 }
201 } );
202
203 // Store default filter state based on default params
204 this.defaultFilters = this.getFilterRepresentation( this.getDefaultParams() );
205
206 // Check for filters that should be initially selected by their default value
207 if ( this.isSticky() ) {
208 const defaultFilters = this.defaultFilters;
209 for ( const filterName in defaultFilters ) {
210 const filterValue = defaultFilters[ filterName ];
211 model.getItemByName( filterName ).toggleSelected( filterValue );
212 }
213 }
214
215 // Verify that single_option group has at least one item selected
216 if (
217 this.getType() === 'single_option' &&
218 this.findSelectedItems().length === 0
219 ) {
220 defaultParam = groupDefault !== undefined ?
221 groupDefault : this.getItems()[ 0 ].getParamName();
222
223 // Single option means there must be a single option
224 // selected, so we have to either select the default
225 // or select the first option
226 this.selectItemByParamName( defaultParam );
227 }
228 };
229
230 /**
231 * Respond to filterItem update event
232 *
233 * @param {mw.rcfilters.dm.FilterItem} item Updated filter item
234 * @fires update
235 */
236 FilterGroup.prototype.onFilterItemUpdate = function ( item ) {
237 // Update state
238 let changed = false;
239 const active = this.areAnySelected(),
240 model = this;
241
242 if ( this.getType() === 'single_option' ) {
243 // This group must have one item selected always
244 // and must never have more than one item selected at a time
245 if ( this.findSelectedItems().length === 0 ) {
246 // Nothing is selected anymore
247 // Select the default or the first item
248 this.currSelected = this.getItemByParamName( this.defaultParams[ this.getName() ] ) ||
249 this.getItems()[ 0 ];
250 this.currSelected.toggleSelected( true );
251 changed = true;
252 } else if ( this.findSelectedItems().length > 1 ) {
253 // There is more than one item selected
254 // This should only happen if the item given
255 // is the one that is selected, so unselect
256 // all items that is not it
257 this.findSelectedItems().forEach( ( itemModel ) => {
258 // Note that in case the given item is actually
259 // not selected, this loop will end up unselecting
260 // all items, which would trigger the case above
261 // when the last item is unselected anyways
262 const selected = itemModel.getName() === item.getName() &&
263 item.isSelected();
264
265 itemModel.toggleSelected( selected );
266 if ( selected ) {
267 model.currSelected = itemModel;
268 }
269 } );
270 changed = true;
271 }
272 }
273
274 if ( this.isSticky() ) {
275 // If this group is sticky, then change the default according to the
276 // current selection.
277 this.defaultParams = this.getParamRepresentation( this.getSelectedState() );
278 }
279
280 if (
281 changed ||
282 this.active !== active ||
283 this.currSelected !== item
284 ) {
285 this.active = active;
286 this.currSelected = item;
287
288 this.emit( 'update' );
289 }
290 };
291
292 /**
293 * Get group active state
294 *
295 * @return {boolean} Active state
296 */
297 FilterGroup.prototype.isActive = function () {
298 return this.active;
299 };
300
301 /**
302 * Get group hidden state
303 *
304 * @return {boolean} Hidden state
305 */
306 FilterGroup.prototype.isHidden = function () {
307 return this.hidden;
308 };
309
310 /**
311 * Get group allow arbitrary state
312 *
313 * @return {boolean} Group allows an arbitrary value from the URL
314 */
315 FilterGroup.prototype.isAllowArbitrary = function () {
316 return this.allowArbitrary;
317 };
318
319 /**
320 * Get group maximum value for numeric groups
321 *
322 * @return {number|null} Group max value
323 */
324 FilterGroup.prototype.getMaxValue = function () {
325 return this.numericRange && this.numericRange.max !== undefined ?
326 this.numericRange.max : null;
327 };
328
329 /**
330 * Get group minimum value for numeric groups
331 *
332 * @return {number|null} Group max value
333 */
334 FilterGroup.prototype.getMinValue = function () {
335 return this.numericRange && this.numericRange.min !== undefined ?
336 this.numericRange.min : null;
337 };
338
339 /**
340 * Get group name
341 *
342 * @return {string} Group name
343 */
344 FilterGroup.prototype.getName = function () {
345 return this.name;
346 };
347
348 /**
349 * Get the default param state of this group
350 *
351 * @return {Object} Default param state
352 */
353 FilterGroup.prototype.getDefaultParams = function () {
354 return this.defaultParams;
355 };
356
357 /**
358 * Get the default filter state of this group
359 *
360 * @return {Object} Default filter state
361 */
362 FilterGroup.prototype.getDefaultFilters = function () {
363 return this.defaultFilters;
364 };
365
366 /**
367 * Get the messags defining the 'whats this' popup for this group
368 *
369 * @return {Object} What's this messages
370 */
371 FilterGroup.prototype.getWhatsThis = function () {
372 return this.whatsThis;
373 };
374
375 /**
376 * Check whether this group has a 'what's this' message
377 *
378 * @return {boolean} This group has a what's this message
379 */
380 FilterGroup.prototype.hasWhatsThis = function () {
381 return !!this.whatsThis.body;
382 };
383
384 /**
385 * Get the conflicts associated with the entire group.
386 *
387 * Conflict object is set up by filter name keys and conflict
388 * definition.
389 *
390 * @example
391 * [
392 * {
393 * filterName: {
394 * filter: filterName,
395 * group: group1
396 * }
397 * },
398 * {
399 * filterName2: {
400 * filter: filterName2,
401 * group: group2
402 * }
403 * }
404 * ]
405 *
406 * @return {Object} Conflict definition
407 */
408 FilterGroup.prototype.getConflicts = function () {
409 return this.conflicts;
410 };
411
412 /**
413 * Set conflicts for this group. See #getConflicts for the expected
414 * structure of the definition.
415 *
416 * @param {Object} conflicts Conflicts for this group
417 */
418 FilterGroup.prototype.setConflicts = function ( conflicts ) {
419 this.conflicts = conflicts;
420 };
421
422 /**
423 * Check whether this item has a potential conflict with the given item
424 *
425 * This checks whether the given item is in the list of conflicts of
426 * the current item, but makes no judgment about whether the conflict
427 * is currently at play (either one of the items may not be selected)
428 *
429 * @param {mw.rcfilters.dm.FilterItem} filterItem Filter item
430 * @return {boolean} This item has a conflict with the given item
431 */
432 FilterGroup.prototype.existsInConflicts = function ( filterItem ) {
433 return Object.prototype.hasOwnProperty.call( this.getConflicts(), filterItem.getName() );
434 };
435
436 /**
437 * Check whether there are any items selected
438 *
439 * @return {boolean} Any items in the group are selected
440 */
441 FilterGroup.prototype.areAnySelected = function () {
442 return this.getItems().some( ( filterItem ) => filterItem.isSelected() );
443 };
444
445 /**
446 * Check whether all items selected
447 *
448 * @return {boolean} All items are selected
449 */
450 FilterGroup.prototype.areAllSelected = function () {
451 const selected = [],
452 unselected = [];
453
454 this.getItems().forEach( ( filterItem ) => {
455 if ( filterItem.isSelected() ) {
456 selected.push( filterItem );
457 } else {
458 unselected.push( filterItem );
459 }
460 } );
461
462 if ( unselected.length === 0 ) {
463 return true;
464 }
465
466 // check if every unselected is a subset of a selected
467 return unselected.every( ( unselectedFilterItem ) => selected.some( ( selectedFilterItem ) => selectedFilterItem.existsInSubset( unselectedFilterItem.getName() ) ) );
468 };
469
470 /**
471 * Get all selected items in this group
472 *
473 * @ignore
474 * @param {mw.rcfilters.dm.FilterItem} [excludeItem] Item to exclude from the list
475 * @return {mw.rcfilters.dm.FilterItem[]} Selected items
476 */
477 FilterGroup.prototype.findSelectedItems = function ( excludeItem ) {
478 const excludeName = ( excludeItem && excludeItem.getName() ) || '';
479
480 return this.getItems().filter( ( item ) => item.getName() !== excludeName && item.isSelected() );
481 };
482
483 /**
484 * Check whether all selected items are in conflict with the given item
485 *
486 * @param {mw.rcfilters.dm.FilterItem} filterItem Filter item to test
487 * @return {boolean} All selected items are in conflict with this item
488 */
489 FilterGroup.prototype.areAllSelectedInConflictWith = function ( filterItem ) {
490 const selectedItems = this.findSelectedItems( filterItem );
491
492 return selectedItems.length > 0 &&
493 (
494 // The group as a whole is in conflict with this item
495 this.existsInConflicts( filterItem ) ||
496 // All selected items are in conflict individually
497 selectedItems.every( ( selectedFilter ) => selectedFilter.existsInConflicts( filterItem ) )
498 );
499 };
500
501 /**
502 * Check whether any of the selected items are in conflict with the given item
503 *
504 * @param {mw.rcfilters.dm.FilterItem} filterItem Filter item to test
505 * @return {boolean} Any of the selected items are in conflict with this item
506 */
507 FilterGroup.prototype.areAnySelectedInConflictWith = function ( filterItem ) {
508 const selectedItems = this.findSelectedItems( filterItem );
509
510 return selectedItems.length > 0 && (
511 // The group as a whole is in conflict with this item
512 this.existsInConflicts( filterItem ) ||
513 // Any selected items are in conflict individually
514 selectedItems.some( ( selectedFilter ) => selectedFilter.existsInConflicts( filterItem ) )
515 );
516 };
517
518 /**
519 * Get the parameter representation from this group
520 *
521 * @param {Object} [filterRepresentation] An object defining the state
522 * of the filters in this group, keyed by their name and current selected
523 * state value.
524 * @return {Object} Parameter representation
525 */
526 FilterGroup.prototype.getParamRepresentation = function ( filterRepresentation ) {
527 let areAnySelected = false;
528 const buildFromCurrentState = !filterRepresentation,
529 defaultFilters = this.getDefaultFilters(),
530 result = {},
531 model = this,
532 filterParamNames = {},
533 getSelectedParameter = function ( filters ) {
534 const selected = [];
535
536 // Find if any are selected
537 // eslint-disable-next-line no-jquery/no-each-util
538 $.each( filters, ( name, value ) => {
539 if ( value ) {
540 selected.push( name );
541 }
542 } );
543
544 const item = model.getItemByName( selected[ 0 ] );
545 return ( item && item.getParamName() ) || '';
546 };
547
548 filterRepresentation = filterRepresentation || {};
549
550 // Create or complete the filterRepresentation definition
551 this.getItems().forEach( ( item ) => {
552 // Map filter names to their parameter names
553 filterParamNames[ item.getName() ] = item.getParamName();
554
555 if ( buildFromCurrentState ) {
556 // This means we have not been given a filter representation
557 // so we are building one based on current state
558 filterRepresentation[ item.getName() ] = item.getValue();
559 } else if ( filterRepresentation[ item.getName() ] === undefined ) {
560 // We are given a filter representation, but we have to make
561 // sure that we fill in the missing filters if there are any
562 // we will assume they are all falsey
563 if ( model.isSticky() ) {
564 filterRepresentation[ item.getName() ] = !!defaultFilters[ item.getName() ];
565 } else {
566 filterRepresentation[ item.getName() ] = false;
567 }
568 }
569
570 if ( filterRepresentation[ item.getName() ] ) {
571 areAnySelected = true;
572 }
573 } );
574
575 // Build result
576 if (
577 this.getType() === 'send_unselected_if_any' ||
578 this.getType() === 'boolean' ||
579 this.getType() === 'any_value'
580 ) {
581 // First, check if any of the items are selected at all.
582 // If none is selected, we're treating it as if they are
583 // all false
584
585 // Go over the items and define the correct values
586 // eslint-disable-next-line no-jquery/no-each-util
587 $.each( filterRepresentation, ( name, value ) => {
588 // We must store all parameter values as strings '0' or '1'
589 if ( model.getType() === 'send_unselected_if_any' ) {
590 result[ filterParamNames[ name ] ] = areAnySelected ?
591 String( Number( !value ) ) :
592 '0';
593 } else if ( model.getType() === 'boolean' ) {
594 // Representation is straight-forward and direct from
595 // the parameter value to the filter state
596 result[ filterParamNames[ name ] ] = String( Number( !!value ) );
597 } else if ( model.getType() === 'any_value' ) {
598 result[ filterParamNames[ name ] ] = value;
599 }
600 } );
601 } else if ( this.getType() === 'string_options' ) {
602 const values = [];
603
604 // eslint-disable-next-line no-jquery/no-each-util
605 $.each( filterRepresentation, ( name, value ) => {
606 // Collect values
607 if ( value ) {
608 values.push( filterParamNames[ name ] );
609 }
610 } );
611
612 result[ this.getName() ] = this.getSupportsAll() &&
613 values.length === Object.keys( filterRepresentation ).length ?
614 'all' : values.join( this.getSeparator() );
615 } else if ( this.getType() === 'single_option' ) {
616 result[ this.getName() ] = getSelectedParameter( filterRepresentation );
617 }
618
619 return result;
620 };
621
622 /**
623 * Get the filter representation this group would provide
624 * based on given parameter states.
625 *
626 * @param {Object} [paramRepresentation] An object defining a parameter
627 * state to translate the filter state from. If not given, an object
628 * representing all filters as falsey is returned; same as if the parameter
629 * given were an empty object, or had some of the filters missing.
630 * @return {Object} Filter representation
631 */
632 FilterGroup.prototype.getFilterRepresentation = function ( paramRepresentation ) {
633 let areAnySelected,
634 oneWasSelected = false;
635 const defaultParams = this.getDefaultParams(),
636 expandedParams = $.extend( true, {}, paramRepresentation ),
637 model = this,
638 paramToFilterMap = {},
639 result = {};
640
641 if ( this.isSticky() ) {
642 // If the group is sticky, check if all parameters are represented
643 // and for those that aren't represented, add them with their default
644 // values
645 paramRepresentation = $.extend( true, {}, this.getDefaultParams(), paramRepresentation );
646 }
647
648 paramRepresentation = paramRepresentation || {};
649 if (
650 this.getType() === 'send_unselected_if_any' ||
651 this.getType() === 'boolean' ||
652 this.getType() === 'any_value'
653 ) {
654 // Go over param representation; map and check for selections
655 this.getItems().forEach( ( filterItem ) => {
656 const paramName = filterItem.getParamName();
657
658 expandedParams[ paramName ] = paramRepresentation[ paramName ] || '0';
659 paramToFilterMap[ paramName ] = filterItem;
660
661 if ( Number( paramRepresentation[ filterItem.getParamName() ] ) ) {
662 areAnySelected = true;
663 }
664 } );
665
666 // eslint-disable-next-line no-jquery/no-each-util
667 $.each( expandedParams, ( paramName, paramValue ) => {
668 const filterItem = paramToFilterMap[ paramName ];
669
670 if ( model.getType() === 'send_unselected_if_any' ) {
671 // Flip the definition between the parameter
672 // state and the filter state
673 // This is what the 'toggleSelected' value of the filter is
674 result[ filterItem.getName() ] = areAnySelected ?
675 !Number( paramValue ) :
676 // Otherwise, there are no selected items in the
677 // group, which means the state is false
678 false;
679 } else if ( model.getType() === 'boolean' ) {
680 // Straight-forward definition of state
681 result[ filterItem.getName() ] = !!Number( paramRepresentation[ filterItem.getParamName() ] );
682 } else if ( model.getType() === 'any_value' ) {
683 result[ filterItem.getName() ] = paramRepresentation[ filterItem.getParamName() ];
684 }
685 } );
686 } else if ( this.getType() === 'string_options' ) {
687 const currentValue = paramRepresentation[ this.getName() ] || '';
688
689 // Normalize the given parameter values
690 const paramValues = utils.normalizeParamOptions(
691 // Given
692 currentValue.split(
693 this.getSeparator()
694 ),
695 // Allowed values
696 this.getItems().map( ( filterItem ) => filterItem.getParamName() ),
697 this.getSupportsAll()
698 );
699 // Translate the parameter values into a filter selection state
700 this.getItems().forEach( ( filterItem ) => {
701 // If the parameter is set to 'all', set all filters to true
702 result[ filterItem.getName() ] = (
703 this.getSupportsAll() && paramValues.length === 1 && paramValues[ 0 ] === 'all'
704 ) ?
705 true :
706 // Otherwise, the filter is selected only if it appears in the parameter values
707 paramValues.indexOf( filterItem.getParamName() ) > -1;
708 } );
709 } else if ( this.getType() === 'single_option' ) {
710 // There is parameter that fits a single filter and if not, get the default
711 this.getItems().forEach( ( filterItem ) => {
712 const selected = filterItem.getParamName() === paramRepresentation[ model.getName() ];
713
714 result[ filterItem.getName() ] = selected;
715 oneWasSelected = oneWasSelected || selected;
716 } );
717 }
718
719 // Go over result and make sure all filters are represented.
720 // If any filters are missing, they will get a falsey value
721 this.getItems().forEach( ( filterItem ) => {
722 if ( result[ filterItem.getName() ] === undefined ) {
723 result[ filterItem.getName() ] = this.getFalsyValue();
724 }
725 } );
726
727 // Make sure that at least one option is selected in
728 // single_option groups, no matter what path was taken
729 // If none was selected by the given definition, then
730 // we need to select the one in the base state -- either
731 // the default given, or the first item
732 if (
733 this.getType() === 'single_option' &&
734 !oneWasSelected
735 ) {
736 let item = this.getItems()[ 0 ];
737 if ( defaultParams[ this.getName() ] ) {
738 item = this.getItemByParamName( defaultParams[ this.getName() ] );
739 }
740
741 result[ item.getName() ] = true;
742 }
743
744 return result;
745 };
746
747 /**
748 * @return {any} The appropriate falsy value for this group type
749 */
750 FilterGroup.prototype.getFalsyValue = function () {
751 return this.getType() === 'any_value' ? '' : false;
752 };
753
754 /**
755 * Get current selected state of all filter items in this group
756 *
757 * @return {Object} Selected state
758 */
759 FilterGroup.prototype.getSelectedState = function () {
760 const state = {};
761
762 this.getItems().forEach( ( filterItem ) => {
763 state[ filterItem.getName() ] = filterItem.getValue();
764 } );
765
766 return state;
767 };
768
769 /**
770 * Get item by its filter name
771 *
772 * @ignore
773 * @param {string} filterName Filter name
774 * @return {mw.rcfilters.dm.FilterItem} Filter item
775 */
776 FilterGroup.prototype.getItemByName = function ( filterName ) {
777 return this.getItems().filter( ( item ) => item.getName() === filterName )[ 0 ];
778 };
779
780 /**
781 * Select an item by its parameter name
782 *
783 * @param {string} paramName Filter parameter name
784 */
785 FilterGroup.prototype.selectItemByParamName = function ( paramName ) {
786 this.getItems().forEach( ( item ) => {
787 item.toggleSelected( item.getParamName() === String( paramName ) );
788 } );
789 };
790
791 /**
792 * Get item by its parameter name
793 *
794 * @ignore
795 * @param {string} paramName Parameter name
796 * @return {mw.rcfilters.dm.FilterItem} Filter item
797 */
798 FilterGroup.prototype.getItemByParamName = function ( paramName ) {
799 return this.getItems().filter( ( item ) => item.getParamName() === String( paramName ) )[ 0 ];
800 };
801
802 /**
803 * Get group type
804 *
805 * @return {string} Group type
806 */
807 FilterGroup.prototype.getType = function () {
808 return this.type;
809 };
810
811 /**
812 * Check whether this group is represented by a single parameter
813 * or whether each item is its own parameter
814 *
815 * @return {boolean} This group is a single parameter
816 */
817 FilterGroup.prototype.isPerGroupRequestParameter = function () {
818 return (
819 this.getType() === 'string_options' ||
820 this.getType() === 'single_option'
821 );
822 };
823
824 /**
825 * Get display group
826 *
827 * @return {string} Display group
828 */
829 FilterGroup.prototype.getView = function () {
830 return this.view;
831 };
832
833 /**
834 * Get the prefix used for the filter names inside this group.
835 *
836 * @return {string} Group prefix
837 */
838 FilterGroup.prototype.getNamePrefix = function () {
839 return this.getName() + '__';
840 };
841
842 /**
843 * Get a filter name with the prefix used for the filter names inside this group.
844 *
845 * @param {string} name Filter name to prefix
846 * @return {string} Group prefix
847 */
848 FilterGroup.prototype.getPrefixedName = function ( name ) {
849 return this.getNamePrefix() + name;
850 };
851
852 /**
853 * Get group's title
854 *
855 * @return {string} Title
856 */
857 FilterGroup.prototype.getTitle = function () {
858 return this.title;
859 };
860
861 /**
862 * Get group's values separator
863 *
864 * @return {string} Values separator
865 */
866 FilterGroup.prototype.getSeparator = function () {
867 return this.separator;
868 };
869
870 /**
871 * Check whether the group supports the magic 'all' value to indicate that all values are selected.
872 *
873 * @return {boolean} Group supports the magic 'all' value
874 */
875 FilterGroup.prototype.getSupportsAll = function () {
876 return this.supportsAll;
877 };
878
879 /**
880 * Check whether the group is defined as full coverage
881 *
882 * @return {boolean} Group is full coverage
883 */
884 FilterGroup.prototype.isFullCoverage = function () {
885 return this.fullCoverage;
886 };
887
888 /**
889 * Check whether the group is defined as sticky default
890 *
891 * @return {boolean} Group is sticky default
892 */
893 FilterGroup.prototype.isSticky = function () {
894 return this.sticky;
895 };
896
897 /**
898 * Normalize a value given to this group. This is mostly for correcting
899 * arbitrary values for 'single option' groups, given by the user settings
900 * or the URL that can go outside the limits that are allowed.
901 *
902 * @param {string} value Given value
903 * @return {string} Corrected value
904 */
905 FilterGroup.prototype.normalizeArbitraryValue = function ( value ) {
906 if (
907 this.getType() === 'single_option' &&
908 this.isAllowArbitrary()
909 ) {
910 if (
911 this.getMaxValue() !== null &&
912 value > this.getMaxValue()
913 ) {
914 // Change the value to the actual max value
915 return String( this.getMaxValue() );
916 } else if (
917 this.getMinValue() !== null &&
918 value < this.getMinValue()
919 ) {
920 // Change the value to the actual min value
921 return String( this.getMinValue() );
922 }
923 }
924
925 return value;
926 };
927
928 /**
929 * Toggle the visibility of this group
930 *
931 * @param {boolean} [isVisible] Item is visible
932 */
933 FilterGroup.prototype.toggleVisible = function ( isVisible ) {
934 isVisible = isVisible === undefined ? !this.visible : isVisible;
935
936 if ( this.visible !== isVisible ) {
937 this.visible = isVisible;
938 this.emit( 'update' );
939 }
940 };
941
942 /**
943 * Check whether the group is visible
944 *
945 * @return {boolean} Group is visible
946 */
947 FilterGroup.prototype.isVisible = function () {
948 return this.visible;
949 };
950
951 /**
952 * Set the visibility of the items under this group by the given items array
953 *
954 * @param {mw.rcfilters.dm.ItemModel[]} visibleItems An array of visible items
955 */
956 FilterGroup.prototype.setVisibleItems = function ( visibleItems ) {
957 this.getItems().forEach( ( itemModel ) => {
958 itemModel.toggleVisible( visibleItems.indexOf( itemModel ) !== -1 );
959 } );
960 };
961
962 module.exports = FilterGroup;