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