b17355f5bab70ee303326ea7cf4ccd510b4f59fb
[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 anyHighlighted,
97 supersetMap = {},
98 model = this,
99 items = [];
100
101 filterDefinition.forEach( function ( filter ) {
102 // Instantiate an item
103 var subsetNames = [],
104 filterItem = new mw.rcfilters.dm.FilterItem( filter.name, model, {
105 group: model.getName(),
106 label: filter.label || filter.name,
107 description: filter.description || '',
108 labelPrefixKey: model.labelPrefixKey,
109 cssClass: filter.cssClass,
110 identifiers: filter.identifiers,
111 defaultHighlightColor: filter.defaultHighlightColor
112 } );
113
114 if ( filter.subset ) {
115 filter.subset = filter.subset.map( function ( el ) {
116 return el.filter;
117 } );
118
119 subsetNames = [];
120
121 filter.subset.forEach( function ( subsetFilterName ) { // eslint-disable-line no-loop-func
122 // Subsets (unlike conflicts) are always inside the same group
123 // We can re-map the names of the filters we are getting from
124 // the subsets with the group prefix
125 var subsetName = model.getPrefixedName( subsetFilterName );
126 // For convenience, we should store each filter's "supersets" -- these are
127 // the filters that have that item in their subset list. This will just
128 // make it easier to go through whether the item has any other items
129 // that affect it (and are selected) at any given time
130 supersetMap[ subsetName ] = supersetMap[ subsetName ] || [];
131 mw.rcfilters.utils.addArrayElementsUnique(
132 supersetMap[ subsetName ],
133 filterItem.getName()
134 );
135
136 // Translate subset param name to add the group name, so we
137 // get consistent naming. We know that subsets are only within
138 // the same group
139 subsetNames.push( subsetName );
140 } );
141
142 // Set translated subset
143 filterItem.setSubset( subsetNames );
144 }
145
146 items.push( filterItem );
147
148 // Store default parameter state; in this case, default is defined per filter
149 if (
150 model.getType() === 'send_unselected_if_any' ||
151 model.getType() === 'boolean'
152 ) {
153 // Store the default parameter state
154 // For this group type, parameter values are direct
155 // We need to convert from a boolean to a string ('1' and '0')
156 model.defaultParams[ filter.name ] = String( Number( filter.default || 0 ) );
157 }
158 } );
159
160 // Add items
161 this.addItems( items );
162
163 // Now that we have all items, we can apply the superset map
164 this.getItems().forEach( function ( filterItem ) {
165 filterItem.setSuperset( supersetMap[ filterItem.getName() ] );
166 } );
167
168 // Store default parameter state; in this case, default is defined per the
169 // entire group, given by groupDefault method parameter
170 if ( this.getType() === 'string_options' ) {
171 // Store the default parameter group state
172 // For this group, the parameter is group name and value is the names
173 // of selected items
174 this.defaultParams[ this.getName() ] = mw.rcfilters.utils.normalizeParamOptions(
175 // Current values
176 groupDefault ?
177 groupDefault.split( this.getSeparator() ) :
178 [],
179 // Legal values
180 this.getItems().map( function ( item ) {
181 return item.getParamName();
182 } )
183 ).join( this.getSeparator() );
184 } else if ( this.getType() === 'single_option' ) {
185 defaultParam = groupDefault !== undefined ?
186 groupDefault : this.getItems()[ 0 ].getParamName();
187
188 // For this group, the parameter is the group name,
189 // and a single item can be selected: default or first item
190 this.defaultParams[ this.getName() ] = defaultParam;
191 }
192
193 // add highlights to defaultParams
194 anyHighlighted = false;
195 this.getItems().forEach( function ( filterItem ) {
196 if ( filterItem.isHighlighted() ) {
197 anyHighlighted = true;
198 this.defaultParams[ filterItem.getName() + '_color' ] = filterItem.getHighlightColor();
199 }
200 }.bind( this ) );
201 if ( anyHighlighted ) {
202 this.defaultParams.highlight = '1';
203 }
204
205 // Store default filter state based on default params
206 this.defaultFilters = this.getFilterRepresentation( this.getDefaultParams() );
207
208 // Check for filters that should be initially selected by their default value
209 if ( this.isSticky() ) {
210 $.each( this.defaultFilters, function ( filterName, filterValue ) {
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.getSelectedItems().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 mw.rcfilters.dm.FilterGroup.prototype.onFilterItemUpdate = function ( item ) {
237 // Update state
238 var changed = false,
239 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.getSelectedItems().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.getSelectedItems().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.getSelectedItems().forEach( function ( 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 var 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 (
275 changed ||
276 this.active !== active ||
277 this.currSelected !== item
278 ) {
279 if ( this.isSticky() ) {
280 // If this group is sticky, then change the default according to the
281 // current selection.
282 this.defaultParams = this.getParamRepresentation( this.getSelectedState() );
283 }
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 mw.rcfilters.dm.FilterGroup.prototype.isActive = function () {
298 return this.active;
299 };
300
301 /**
302 * Get group hidden state
303 *
304 * @return {boolean} Hidden state
305 */
306 mw.rcfilters.dm.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 mw.rcfilters.dm.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 mw.rcfilters.dm.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 mw.rcfilters.dm.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 mw.rcfilters.dm.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 mw.rcfilters.dm.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 mw.rcfilters.dm.FilterGroup.prototype.getDefaultFilters = function () {
363 return this.defaultFilters;
364 };
365
366 /**
367 * This is for a single_option and string_options group types
368 * it returns the value of the default
369 *
370 * @return {string} Value of the default
371 */
372 mw.rcfilters.dm.FilterGroup.prototype.getDefaulParamValue = function () {
373 return this.defaultParams[ this.getName() ];
374 };
375 /**
376 * Get the messags defining the 'whats this' popup for this group
377 *
378 * @return {Object} What's this messages
379 */
380 mw.rcfilters.dm.FilterGroup.prototype.getWhatsThis = function () {
381 return this.whatsThis;
382 };
383
384 /**
385 * Check whether this group has a 'what's this' message
386 *
387 * @return {boolean} This group has a what's this message
388 */
389 mw.rcfilters.dm.FilterGroup.prototype.hasWhatsThis = function () {
390 return !!this.whatsThis.body;
391 };
392
393 /**
394 * Get the conflicts associated with the entire group.
395 * Conflict object is set up by filter name keys and conflict
396 * definition. For example:
397 * [
398 * {
399 * filterName: {
400 * filter: filterName,
401 * group: group1
402 * }
403 * },
404 * {
405 * filterName2: {
406 * filter: filterName2,
407 * group: group2
408 * }
409 * }
410 * ]
411 * @return {Object} Conflict definition
412 */
413 mw.rcfilters.dm.FilterGroup.prototype.getConflicts = function () {
414 return this.conflicts;
415 };
416
417 /**
418 * Set conflicts for this group. See #getConflicts for the expected
419 * structure of the definition.
420 *
421 * @param {Object} conflicts Conflicts for this group
422 */
423 mw.rcfilters.dm.FilterGroup.prototype.setConflicts = function ( conflicts ) {
424 this.conflicts = conflicts;
425 };
426
427 /**
428 * Set conflicts for each filter item in the group based on the
429 * given conflict map
430 *
431 * @param {Object} conflicts Object representing the conflict map,
432 * keyed by the item name, where its value is an object for all its conflicts
433 */
434 mw.rcfilters.dm.FilterGroup.prototype.setFilterConflicts = function ( conflicts ) {
435 this.getItems().forEach( function ( filterItem ) {
436 if ( conflicts[ filterItem.getName() ] ) {
437 filterItem.setConflicts( conflicts[ filterItem.getName() ] );
438 }
439 } );
440 };
441
442 /**
443 * Check whether this item has a potential conflict with the given item
444 *
445 * This checks whether the given item is in the list of conflicts of
446 * the current item, but makes no judgment about whether the conflict
447 * is currently at play (either one of the items may not be selected)
448 *
449 * @param {mw.rcfilters.dm.FilterItem} filterItem Filter item
450 * @return {boolean} This item has a conflict with the given item
451 */
452 mw.rcfilters.dm.FilterGroup.prototype.existsInConflicts = function ( filterItem ) {
453 return Object.prototype.hasOwnProperty.call( this.getConflicts(), filterItem.getName() );
454 };
455
456 /**
457 * Check whether there are any items selected
458 *
459 * @return {boolean} Any items in the group are selected
460 */
461 mw.rcfilters.dm.FilterGroup.prototype.areAnySelected = function () {
462 return this.getItems().some( function ( filterItem ) {
463 return filterItem.isSelected();
464 } );
465 };
466
467 /**
468 * Check whether all items selected
469 *
470 * @return {boolean} All items are selected
471 */
472 mw.rcfilters.dm.FilterGroup.prototype.areAllSelected = function () {
473 var selected = [],
474 unselected = [];
475
476 this.getItems().forEach( function ( filterItem ) {
477 if ( filterItem.isSelected() ) {
478 selected.push( filterItem );
479 } else {
480 unselected.push( filterItem );
481 }
482 } );
483
484 if ( unselected.length === 0 ) {
485 return true;
486 }
487
488 // check if every unselected is a subset of a selected
489 return unselected.every( function ( unselectedFilterItem ) {
490 return selected.some( function ( selectedFilterItem ) {
491 return selectedFilterItem.existsInSubset( unselectedFilterItem.getName() );
492 } );
493 } );
494 };
495
496 /**
497 * Get all selected items in this group
498 *
499 * @param {mw.rcfilters.dm.FilterItem} [excludeItem] Item to exclude from the list
500 * @return {mw.rcfilters.dm.FilterItem[]} Selected items
501 */
502 mw.rcfilters.dm.FilterGroup.prototype.getSelectedItems = function ( excludeItem ) {
503 var excludeName = ( excludeItem && excludeItem.getName() ) || '';
504
505 return this.getItems().filter( function ( item ) {
506 return item.getName() !== excludeName && item.isSelected();
507 } );
508 };
509
510 /**
511 * Check whether all selected items are in conflict with the given item
512 *
513 * @param {mw.rcfilters.dm.FilterItem} filterItem Filter item to test
514 * @return {boolean} All selected items are in conflict with this item
515 */
516 mw.rcfilters.dm.FilterGroup.prototype.areAllSelectedInConflictWith = function ( filterItem ) {
517 var selectedItems = this.getSelectedItems( filterItem );
518
519 return selectedItems.length > 0 &&
520 (
521 // The group as a whole is in conflict with this item
522 this.existsInConflicts( filterItem ) ||
523 // All selected items are in conflict individually
524 selectedItems.every( function ( selectedFilter ) {
525 return selectedFilter.existsInConflicts( filterItem );
526 } )
527 );
528 };
529
530 /**
531 * Check whether any of the selected items are in conflict with the given item
532 *
533 * @param {mw.rcfilters.dm.FilterItem} filterItem Filter item to test
534 * @return {boolean} Any of the selected items are in conflict with this item
535 */
536 mw.rcfilters.dm.FilterGroup.prototype.areAnySelectedInConflictWith = function ( filterItem ) {
537 var selectedItems = this.getSelectedItems( filterItem );
538
539 return selectedItems.length > 0 && (
540 // The group as a whole is in conflict with this item
541 this.existsInConflicts( filterItem ) ||
542 // Any selected items are in conflict individually
543 selectedItems.some( function ( selectedFilter ) {
544 return selectedFilter.existsInConflicts( filterItem );
545 } )
546 );
547 };
548
549 /**
550 * Get the parameter representation from this group
551 *
552 * @param {Object} [filterRepresentation] An object defining the state
553 * of the filters in this group, keyed by their name and current selected
554 * state value.
555 * @return {Object} Parameter representation
556 */
557 mw.rcfilters.dm.FilterGroup.prototype.getParamRepresentation = function ( filterRepresentation ) {
558 var values,
559 areAnySelected = false,
560 buildFromCurrentState = !filterRepresentation,
561 defaultFilters = this.getDefaultFilters(),
562 result = {},
563 model = this,
564 filterParamNames = {},
565 getSelectedParameter = function ( filters ) {
566 var item,
567 selected = [];
568
569 // Find if any are selected
570 $.each( filters, function ( name, value ) {
571 if ( value ) {
572 selected.push( name );
573 }
574 } );
575
576 item = model.getItemByName( selected[ 0 ] );
577 return ( item && item.getParamName() ) || '';
578 };
579
580 filterRepresentation = filterRepresentation || {};
581
582 // Create or complete the filterRepresentation definition
583 this.getItems().forEach( function ( item ) {
584 // Map filter names to their parameter names
585 filterParamNames[ item.getName() ] = item.getParamName();
586
587 if ( buildFromCurrentState ) {
588 // This means we have not been given a filter representation
589 // so we are building one based on current state
590 filterRepresentation[ item.getName() ] = item.isSelected();
591 } else if ( filterRepresentation[ item.getName() ] === undefined ) {
592 // We are given a filter representation, but we have to make
593 // sure that we fill in the missing filters if there are any
594 // we will assume they are all falsey
595 if ( model.isSticky() ) {
596 filterRepresentation[ item.getName() ] = !!defaultFilters[ item.getName() ];
597 } else {
598 filterRepresentation[ item.getName() ] = false;
599 }
600 }
601
602 if ( filterRepresentation[ item.getName() ] ) {
603 areAnySelected = true;
604 }
605 } );
606
607 // Build result
608 if (
609 this.getType() === 'send_unselected_if_any' ||
610 this.getType() === 'boolean'
611 ) {
612 // First, check if any of the items are selected at all.
613 // If none is selected, we're treating it as if they are
614 // all false
615
616 // Go over the items and define the correct values
617 $.each( filterRepresentation, function ( name, value ) {
618 // We must store all parameter values as strings '0' or '1'
619 if ( model.getType() === 'send_unselected_if_any' ) {
620 result[ filterParamNames[ name ] ] = areAnySelected ?
621 String( Number( !value ) ) :
622 '0';
623 } else if ( model.getType() === 'boolean' ) {
624 // Representation is straight-forward and direct from
625 // the parameter value to the filter state
626 result[ filterParamNames[ name ] ] = String( Number( !!value ) );
627 }
628 } );
629 } else if ( this.getType() === 'string_options' ) {
630 values = [];
631
632 $.each( filterRepresentation, function ( name, value ) {
633 // Collect values
634 if ( value ) {
635 values.push( filterParamNames[ name ] );
636 }
637 } );
638
639 result[ this.getName() ] = ( values.length === Object.keys( filterRepresentation ).length ) ?
640 'all' : values.join( this.getSeparator() );
641 } else if ( this.getType() === 'single_option' ) {
642 result[ this.getName() ] = getSelectedParameter( filterRepresentation );
643 }
644
645 return result;
646 };
647
648 /**
649 * Get the filter representation this group would provide
650 * based on given parameter states.
651 *
652 * @param {Object} [paramRepresentation] An object defining a parameter
653 * state to translate the filter state from. If not given, an object
654 * representing all filters as falsey is returned; same as if the parameter
655 * given were an empty object, or had some of the filters missing.
656 * @return {Object} Filter representation
657 */
658 mw.rcfilters.dm.FilterGroup.prototype.getFilterRepresentation = function ( paramRepresentation ) {
659 var areAnySelected, paramValues, item, currentValue,
660 oneWasSelected = false,
661 defaultParams = this.getDefaultParams(),
662 expandedParams = $.extend( true, {}, paramRepresentation ),
663 model = this,
664 paramToFilterMap = {},
665 result = {};
666
667 if ( this.isSticky() ) {
668 // If the group is sticky, check if all parameters are represented
669 // and for those that aren't represented, add them with their default
670 // values
671 paramRepresentation = $.extend( true, {}, this.getDefaultParams(), paramRepresentation );
672 }
673
674 paramRepresentation = paramRepresentation || {};
675 if (
676 this.getType() === 'send_unselected_if_any' ||
677 this.getType() === 'boolean'
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 }
707 } );
708 } else if ( this.getType() === 'string_options' ) {
709 currentValue = paramRepresentation[ this.getName() ] || '';
710
711 // Normalize the given parameter values
712 paramValues = mw.rcfilters.utils.normalizeParamOptions(
713 // Given
714 currentValue.split(
715 this.getSeparator()
716 ),
717 // Allowed values
718 this.getItems().map( function ( filterItem ) {
719 return filterItem.getParamName();
720 } )
721 );
722 // Translate the parameter values into a filter selection state
723 this.getItems().forEach( function ( filterItem ) {
724 // All true (either because all values are written or the term 'all' is written)
725 // is the same as all filters set to true
726 result[ filterItem.getName() ] = (
727 // If it is the word 'all'
728 paramValues.length === 1 && paramValues[ 0 ] === 'all' ||
729 // All values are written
730 paramValues.length === model.getItemCount()
731 ) ?
732 true :
733 // Otherwise, the filter is selected only if it appears in the parameter values
734 paramValues.indexOf( filterItem.getParamName() ) > -1;
735 } );
736 } else if ( this.getType() === 'single_option' ) {
737 // There is parameter that fits a single filter and if not, get the default
738 this.getItems().forEach( function ( filterItem ) {
739 var selected = filterItem.getParamName() === paramRepresentation[ model.getName() ];
740
741 result[ filterItem.getName() ] = selected;
742 oneWasSelected = oneWasSelected || selected;
743 } );
744 }
745
746 // Go over result and make sure all filters are represented.
747 // If any filters are missing, they will get a falsey value
748 this.getItems().forEach( function ( filterItem ) {
749 if ( result[ filterItem.getName() ] === undefined ) {
750 result[ filterItem.getName() ] = false;
751 }
752 } );
753
754 // Make sure that at least one option is selected in
755 // single_option groups, no matter what path was taken
756 // If none was selected by the given definition, then
757 // we need to select the one in the base state -- either
758 // the default given, or the first item
759 if (
760 this.getType() === 'single_option' &&
761 !oneWasSelected
762 ) {
763 item = this.getItems()[ 0 ];
764 if ( defaultParams[ this.getName() ] ) {
765 item = this.getItemByParamName( defaultParams[ this.getName() ] );
766 }
767
768 result[ item.getName() ] = true;
769 }
770
771 return result;
772 };
773
774 /**
775 * Get current selected state of all filter items in this group
776 *
777 * @return {Object} Selected state
778 */
779 mw.rcfilters.dm.FilterGroup.prototype.getSelectedState = function () {
780 var state = {};
781
782 this.getItems().forEach( function ( filterItem ) {
783 state[ filterItem.getName() ] = filterItem.isSelected();
784 } );
785
786 return state;
787 };
788
789 /**
790 * Get item by its filter name
791 *
792 * @param {string} filterName Filter name
793 * @return {mw.rcfilters.dm.FilterItem} Filter item
794 */
795 mw.rcfilters.dm.FilterGroup.prototype.getItemByName = function ( filterName ) {
796 return this.getItems().filter( function ( item ) {
797 return item.getName() === filterName;
798 } )[ 0 ];
799 };
800
801 /**
802 * Select an item by its parameter name
803 *
804 * @param {string} paramName Filter parameter name
805 */
806 mw.rcfilters.dm.FilterGroup.prototype.selectItemByParamName = function ( paramName ) {
807 this.getItems().forEach( function ( item ) {
808 item.toggleSelected( item.getParamName() === String( paramName ) );
809 } );
810 };
811
812 /**
813 * Get item by its parameter name
814 *
815 * @param {string} paramName Parameter name
816 * @return {mw.rcfilters.dm.FilterItem} Filter item
817 */
818 mw.rcfilters.dm.FilterGroup.prototype.getItemByParamName = function ( paramName ) {
819 return this.getItems().filter( function ( item ) {
820 return item.getParamName() === String( paramName );
821 } )[ 0 ];
822 };
823
824 /**
825 * Get group type
826 *
827 * @return {string} Group type
828 */
829 mw.rcfilters.dm.FilterGroup.prototype.getType = function () {
830 return this.type;
831 };
832
833 /**
834 * Check whether this group is represented by a single parameter
835 * or whether each item is its own parameter
836 *
837 * @return {boolean} This group is a single parameter
838 */
839 mw.rcfilters.dm.FilterGroup.prototype.isPerGroupRequestParameter = function () {
840 return (
841 this.getType() === 'string_options' ||
842 this.getType() === 'single_option'
843 );
844 };
845
846 /**
847 * Get display group
848 *
849 * @return {string} Display group
850 */
851 mw.rcfilters.dm.FilterGroup.prototype.getView = function () {
852 return this.view;
853 };
854
855 /**
856 * Get the prefix used for the filter names inside this group.
857 *
858 * @param {string} [name] Filter name to prefix
859 * @return {string} Group prefix
860 */
861 mw.rcfilters.dm.FilterGroup.prototype.getNamePrefix = function () {
862 return this.getName() + '__';
863 };
864
865 /**
866 * Get a filter name with the prefix used for the filter names inside this group.
867 *
868 * @param {string} name Filter name to prefix
869 * @return {string} Group prefix
870 */
871 mw.rcfilters.dm.FilterGroup.prototype.getPrefixedName = function ( name ) {
872 return this.getNamePrefix() + name;
873 };
874
875 /**
876 * Get group's title
877 *
878 * @return {string} Title
879 */
880 mw.rcfilters.dm.FilterGroup.prototype.getTitle = function () {
881 return this.title;
882 };
883
884 /**
885 * Get group's values separator
886 *
887 * @return {string} Values separator
888 */
889 mw.rcfilters.dm.FilterGroup.prototype.getSeparator = function () {
890 return this.separator;
891 };
892
893 /**
894 * Check whether the group is defined as full coverage
895 *
896 * @return {boolean} Group is full coverage
897 */
898 mw.rcfilters.dm.FilterGroup.prototype.isFullCoverage = function () {
899 return this.fullCoverage;
900 };
901
902 /**
903 * Check whether the group is defined as sticky default
904 *
905 * @return {boolean} Group is sticky default
906 */
907 mw.rcfilters.dm.FilterGroup.prototype.isSticky = function () {
908 return this.sticky;
909 };
910
911 /**
912 * Check whether the group value is excluded from saved queries
913 *
914 * @return {boolean} Group value is excluded from saved queries
915 */
916 mw.rcfilters.dm.FilterGroup.prototype.isExcludedFromSavedQueries = function () {
917 return this.excludedFromSavedQueries;
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 }( mediaWiki ) );