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