RCFilters UI: Separate name from paramName in filters
[lhc/web/wiklou.git] / resources / src / mediawiki.rcfilters / dm / mw.rcfilters.dm.FiltersViewModel.js
1 ( function ( mw, $ ) {
2 /**
3 * View model for the filters selection and display
4 *
5 * @mixins OO.EventEmitter
6 * @mixins OO.EmitterList
7 *
8 * @constructor
9 */
10 mw.rcfilters.dm.FiltersViewModel = function MwRcfiltersDmFiltersViewModel() {
11 // Mixin constructor
12 OO.EventEmitter.call( this );
13 OO.EmitterList.call( this );
14
15 this.groups = {};
16 this.defaultParams = {};
17 this.defaultFiltersEmpty = null;
18 this.highlightEnabled = false;
19 this.parameterMap = {};
20
21 // Events
22 this.aggregate( { update: 'filterItemUpdate' } );
23 this.connect( this, { filterItemUpdate: [ 'emit', 'itemUpdate' ] } );
24 };
25
26 /* Initialization */
27 OO.initClass( mw.rcfilters.dm.FiltersViewModel );
28 OO.mixinClass( mw.rcfilters.dm.FiltersViewModel, OO.EventEmitter );
29 OO.mixinClass( mw.rcfilters.dm.FiltersViewModel, OO.EmitterList );
30
31 /* Events */
32
33 /**
34 * @event initialize
35 *
36 * Filter list is initialized
37 */
38
39 /**
40 * @event itemUpdate
41 * @param {mw.rcfilters.dm.FilterItem} item Filter item updated
42 *
43 * Filter item has changed
44 */
45
46 /**
47 * @event highlightChange
48 * @param {boolean} Highlight feature is enabled
49 *
50 * Highlight feature has been toggled enabled or disabled
51 */
52
53 /* Methods */
54
55 /**
56 * Re-assess the states of filter items based on the interactions between them
57 *
58 * @param {mw.rcfilters.dm.FilterItem} [item] Changed item. If not given, the
59 * method will go over the state of all items
60 */
61 mw.rcfilters.dm.FiltersViewModel.prototype.reassessFilterInteractions = function ( item ) {
62 var allSelected,
63 model = this,
64 iterationItems = item !== undefined ? [ item ] : this.getItems();
65
66 iterationItems.forEach( function ( checkedItem ) {
67 var allCheckedItems = checkedItem.getSubset().concat( [ checkedItem.getName() ] ),
68 groupModel = checkedItem.getGroupModel();
69
70 // Check for subsets (included filters) plus the item itself:
71 allCheckedItems.forEach( function ( filterItemName ) {
72 var itemInSubset = model.getItemByName( filterItemName );
73
74 itemInSubset.toggleIncluded(
75 // If any of itemInSubset's supersets are selected, this item
76 // is included
77 itemInSubset.getSuperset().some( function ( supersetName ) {
78 return ( model.getItemByName( supersetName ).isSelected() );
79 } )
80 );
81 } );
82
83 // Update coverage for the changed group
84 if ( groupModel.isFullCoverage() ) {
85 allSelected = groupModel.areAllSelected();
86 groupModel.getItems().forEach( function ( filterItem ) {
87 filterItem.toggleFullyCovered( allSelected );
88 } );
89 }
90 } );
91
92 // Check for conflicts
93 // In this case, we must go over all items, since
94 // conflicts are bidirectional and depend not only on
95 // individual items, but also on the selected states of
96 // the groups they're in.
97 this.getItems().forEach( function ( filterItem ) {
98 var inConflict = false,
99 filterItemGroup = filterItem.getGroupModel();
100
101 // For each item, see if that item is still conflicting
102 $.each( model.groups, function ( groupName, groupModel ) {
103 if ( filterItem.getGroupName() === groupName ) {
104 // Check inside the group
105 inConflict = groupModel.areAnySelectedInConflictWith( filterItem );
106 } else {
107 // According to the spec, if two items conflict from two different
108 // groups, the conflict only lasts if the groups **only have selected
109 // items that are conflicting**. If a group has selected items that
110 // are conflicting and non-conflicting, the scope of the result has
111 // expanded enough to completely remove the conflict.
112
113 // For example, see two groups with conflicts:
114 // userExpLevel: [
115 // {
116 // name: 'experienced',
117 // conflicts: [ 'unregistered' ]
118 // }
119 // ],
120 // registration: [
121 // {
122 // name: 'registered',
123 // },
124 // {
125 // name: 'unregistered',
126 // }
127 // ]
128 // If we select 'experienced', then 'unregistered' is in conflict (and vice versa),
129 // because, inherently, 'experienced' filter only includes registered users, and so
130 // both filters are in conflict with one another.
131 // However, the minute we select 'registered', the scope of our results
132 // has expanded to no longer have a conflict with 'experienced' filter, and
133 // so the conflict is removed.
134
135 // In our case, we need to check if the entire group conflicts with
136 // the entire item's group, so we follow the above spec
137 inConflict = (
138 // The foreign group is in conflict with this item
139 groupModel.areAllSelectedInConflictWith( filterItem ) &&
140 // Every selected member of the item's own group is also
141 // in conflict with the other group
142 filterItemGroup.getSelectedItems().every( function ( otherGroupItem ) {
143 return groupModel.areAllSelectedInConflictWith( otherGroupItem );
144 } )
145 );
146 }
147
148 // If we're in conflict, this will return 'false' which
149 // will break the loop. Otherwise, we're not in conflict
150 // and the loop continues
151 return !inConflict;
152 } );
153
154 // Toggle the item state
155 filterItem.toggleConflicted( inConflict );
156 } );
157 };
158
159 /**
160 * Set filters and preserve a group relationship based on
161 * the definition given by an object
162 *
163 * @param {Array} filters Filter group definition
164 */
165 mw.rcfilters.dm.FiltersViewModel.prototype.initializeFilters = function ( filters ) {
166 var i, filterItem, selectedFilterNames, filterConflictResult, groupConflictResult, subsetNames,
167 model = this,
168 items = [],
169 supersetMap = {},
170 groupConflictMap = {},
171 filterConflictMap = {},
172 addArrayElementsUnique = function ( arr, elements ) {
173 elements = Array.isArray( elements ) ? elements : [ elements ];
174
175 elements.forEach( function ( element ) {
176 if ( arr.indexOf( element ) === -1 ) {
177 arr.push( element );
178 }
179 } );
180
181 return arr;
182 },
183 expandConflictDefinitions = function ( obj ) {
184 var result = {};
185
186 $.each( obj, function ( key, conflicts ) {
187 var filterName,
188 adjustedConflicts = {};
189
190 conflicts.forEach( function ( conflict ) {
191 if ( conflict.filter ) {
192 filterName = model.groups[ conflict.group ].getNamePrefix() + conflict.filter;
193
194 // Rename
195 adjustedConflicts[ filterName ] = $.extend(
196 {},
197 conflict,
198 { filter: filterName }
199 );
200 } else {
201 // This conflict is for an entire group. Split it up to
202 // represent each filter
203
204 // Get the relevant group items
205 model.groups[ conflict.group ].getItems().forEach( function ( groupItem ) {
206 // Rebuild the conflict
207 adjustedConflicts[ groupItem.getName() ] = $.extend(
208 {},
209 conflict,
210 { filter: groupItem.getName() }
211 );
212 } );
213 }
214 } );
215
216 result[ key ] = adjustedConflicts;
217 } );
218
219 return result;
220 };
221
222 // Reset
223 this.clearItems();
224 this.groups = {};
225
226 filters.forEach( function ( data ) {
227 var group = data.name;
228
229 if ( !model.groups[ group ] ) {
230 model.groups[ group ] = new mw.rcfilters.dm.FilterGroup( group, {
231 type: data.type,
232 title: mw.msg( data.title ),
233 separator: data.separator,
234 fullCoverage: !!data.fullCoverage
235 } );
236 }
237
238 if ( data.conflicts ) {
239 groupConflictMap[ group ] = data.conflicts;
240 }
241
242 selectedFilterNames = [];
243 for ( i = 0; i < data.filters.length; i++ ) {
244 data.filters[ i ].subset = data.filters[ i ].subset || [];
245 data.filters[ i ].subset = data.filters[ i ].subset.map( function ( el ) {
246 return el.filter;
247 } );
248
249 filterItem = new mw.rcfilters.dm.FilterItem( data.filters[ i ].name, model.groups[ group ], {
250 group: group,
251 label: mw.msg( data.filters[ i ].label ),
252 description: mw.msg( data.filters[ i ].description ),
253 cssClass: data.filters[ i ].cssClass
254 } );
255
256 if ( data.filters[ i ].subset ) {
257 subsetNames = [];
258 data.filters[ i ].subset.forEach( function ( subsetFilterName ) { // eslint-disable-line no-loop-func
259 var subsetName = model.groups[ group ].getNamePrefix() + subsetFilterName;
260 // For convenience, we should store each filter's "supersets" -- these are
261 // the filters that have that item in their subset list. This will just
262 // make it easier to go through whether the item has any other items
263 // that affect it (and are selected) at any given time
264 supersetMap[ subsetName ] = supersetMap[ subsetName ] || [];
265 addArrayElementsUnique(
266 supersetMap[ subsetName ],
267 filterItem.getName()
268 );
269
270 // Translate subset param name to add the group name, so we
271 // get consistent naming. We know that subsets are only within
272 // the same group
273 subsetNames.push( subsetName );
274 } );
275
276 // Set translated subset
277 filterItem.setSubset( subsetNames );
278 }
279
280 // Store conflicts
281 if ( data.filters[ i ].conflicts ) {
282 filterConflictMap[ filterItem.getName() ] = data.filters[ i ].conflicts;
283 }
284
285 if ( data.type === 'send_unselected_if_any' ) {
286 // Store the default parameter state
287 // For this group type, parameter values are direct
288 model.defaultParams[ data.filters[ i ].name ] = Number( !!data.filters[ i ].default );
289 } else if (
290 data.type === 'string_options' &&
291 data.filters[ i ].default
292 ) {
293 selectedFilterNames.push( data.filters[ i ].name );
294 }
295
296 model.groups[ group ].addItems( filterItem );
297 items.push( filterItem );
298 }
299
300 if ( data.type === 'string_options' ) {
301 // Store the default parameter group state
302 // For this group, the parameter is group name and value is the names
303 // of selected items
304 model.defaultParams[ group ] = model.sanitizeStringOptionGroup( group, selectedFilterNames ).join( model.groups[ group ].getSeparator() );
305 }
306 } );
307
308 // Expand conflicts
309 groupConflictResult = expandConflictDefinitions( groupConflictMap );
310 filterConflictResult = expandConflictDefinitions( filterConflictMap );
311
312 // Set conflicts for groups
313 $.each( groupConflictResult, function ( group, conflicts ) {
314 model.groups[ group ].setConflicts( conflicts );
315 } );
316
317 items.forEach( function ( filterItem ) {
318 // Apply the superset map
319 filterItem.setSuperset( supersetMap[ filterItem.getName() ] );
320
321 // set conflicts for item
322 if ( filterConflictResult[ filterItem.getName() ] ) {
323 filterItem.setConflicts( filterConflictResult[ filterItem.getName() ] );
324 }
325 } );
326
327 // Create a map between known parameters and their models
328 $.each( this.groups, function ( group, groupModel ) {
329 if ( groupModel.getType() === 'send_unselected_if_any' ) {
330 // Individual filters
331 groupModel.getItems().forEach( function ( filterItem ) {
332 model.parameterMap[ filterItem.getParamName() ] = filterItem;
333 } );
334 } else if ( groupModel.getType() === 'string_options' ) {
335 // Group
336 model.parameterMap[ groupModel.getName() ] = groupModel;
337 }
338 } );
339
340 // Add items to the model
341 this.addItems( items );
342
343 this.emit( 'initialize' );
344 };
345
346 /**
347 * Get the names of all available filters
348 *
349 * @return {string[]} An array of filter names
350 */
351 mw.rcfilters.dm.FiltersViewModel.prototype.getFilterNames = function () {
352 return this.getItems().map( function ( item ) { return item.getName(); } );
353 };
354
355 /**
356 * Get the object that defines groups by their name.
357 *
358 * @return {Object} Filter groups
359 */
360 mw.rcfilters.dm.FiltersViewModel.prototype.getFilterGroups = function () {
361 return this.groups;
362 };
363
364 /**
365 * Get the value of a specific parameter
366 *
367 * @param {string} name Parameter name
368 * @return {number|string} Parameter value
369 */
370 mw.rcfilters.dm.FiltersViewModel.prototype.getParamValue = function ( name ) {
371 return this.parameters[ name ];
372 };
373
374 /**
375 * Get the current selected state of the filters
376 *
377 * @return {Object} Filters selected state
378 */
379 mw.rcfilters.dm.FiltersViewModel.prototype.getSelectedState = function () {
380 var i,
381 items = this.getItems(),
382 result = {};
383
384 for ( i = 0; i < items.length; i++ ) {
385 result[ items[ i ].getName() ] = items[ i ].isSelected();
386 }
387
388 return result;
389 };
390
391 /**
392 * Get the current full state of the filters
393 *
394 * @return {Object} Filters full state
395 */
396 mw.rcfilters.dm.FiltersViewModel.prototype.getFullState = function () {
397 var i,
398 items = this.getItems(),
399 result = {};
400
401 for ( i = 0; i < items.length; i++ ) {
402 result[ items[ i ].getName() ] = {
403 selected: items[ i ].isSelected(),
404 conflicted: items[ i ].isConflicted(),
405 included: items[ i ].isIncluded()
406 };
407 }
408
409 return result;
410 };
411
412 /**
413 * Get the default parameters object
414 *
415 * @return {Object} Default parameter values
416 */
417 mw.rcfilters.dm.FiltersViewModel.prototype.getDefaultParams = function () {
418 return this.defaultParams;
419 };
420
421 /**
422 * Set all filter states to default values
423 */
424 mw.rcfilters.dm.FiltersViewModel.prototype.setFiltersToDefaults = function () {
425 var defaultFilterStates = this.getFiltersFromParameters( this.getDefaultParams() );
426
427 this.toggleFiltersSelected( defaultFilterStates );
428 };
429
430 /**
431 * Analyze the groups and their filters and output an object representing
432 * the state of the parameters they represent.
433 *
434 * @param {Object} [filterGroups] An object defining the filter groups to
435 * translate to parameters. Its structure must follow that of this.groups
436 * see #getFilterGroups
437 * @return {Object} Parameter state object
438 */
439 mw.rcfilters.dm.FiltersViewModel.prototype.getParametersFromFilters = function ( filterGroups ) {
440 var result = {},
441 groupItems = filterGroups || this.getFilterGroups();
442
443 $.each( groupItems, function ( group, model ) {
444 $.extend( result, model.getParamRepresentation() );
445 } );
446
447 return result;
448 };
449
450 /**
451 * Get the highlight parameters based on current filter configuration
452 *
453 * @return {object} Object where keys are "<filter name>_color" and values
454 * are the selected highlight colors.
455 */
456 mw.rcfilters.dm.FiltersViewModel.prototype.getHighlightParameters = function () {
457 var result = { highlight: Number( this.isHighlightEnabled() ) };
458
459 this.getItems().forEach( function ( filterItem ) {
460 result[ filterItem.getName() + '_color' ] = filterItem.getHighlightColor();
461 } );
462 return result;
463 };
464
465 /**
466 * Sanitize value group of a string_option groups type
467 * Remove duplicates and make sure to only use valid
468 * values.
469 *
470 * @private
471 * @param {string} groupName Group name
472 * @param {string[]} valueArray Array of values
473 * @return {string[]} Array of valid values
474 */
475 mw.rcfilters.dm.FiltersViewModel.prototype.sanitizeStringOptionGroup = function( groupName, valueArray ) {
476 var result = [],
477 validNames = this.getGroupFilters( groupName ).map( function ( filterItem ) {
478 return filterItem.getParamName();
479 } );
480
481 if ( valueArray.indexOf( 'all' ) > -1 ) {
482 // If anywhere in the values there's 'all', we
483 // treat it as if only 'all' was selected.
484 // Example: param=valid1,valid2,all
485 // Result: param=all
486 return [ 'all' ];
487 }
488
489 // Get rid of any dupe and invalid parameter, only output
490 // valid ones
491 // Example: param=valid1,valid2,invalid1,valid1
492 // Result: param=valid1,valid2
493 valueArray.forEach( function ( value ) {
494 if (
495 validNames.indexOf( value ) > -1 &&
496 result.indexOf( value ) === -1
497 ) {
498 result.push( value );
499 }
500 } );
501
502 return result;
503 };
504
505 /**
506 * Check whether the current filter state is set to all false.
507 *
508 * @return {boolean} Current filters are all empty
509 */
510 mw.rcfilters.dm.FiltersViewModel.prototype.areCurrentFiltersEmpty = function () {
511 // Check if there are either any selected items or any items
512 // that have highlight enabled
513 return !this.getItems().some( function ( filterItem ) {
514 return filterItem.isSelected() || filterItem.isHighlighted();
515 } );
516 };
517
518 /**
519 * Check whether the default values of the filters are all false.
520 *
521 * @return {boolean} Default filters are all false
522 */
523 mw.rcfilters.dm.FiltersViewModel.prototype.areDefaultFiltersEmpty = function () {
524 var defaultFilters;
525
526 if ( this.defaultFiltersEmpty !== null ) {
527 // We only need to do this test once,
528 // because defaults are set once per session
529 defaultFilters = this.getFiltersFromParameters();
530 this.defaultFiltersEmpty = Object.keys( defaultFilters ).every( function ( filterName ) {
531 return !defaultFilters[ filterName ];
532 } );
533 }
534
535 return this.defaultFiltersEmpty;
536 };
537
538 /**
539 * This is the opposite of the #getParametersFromFilters method; this goes over
540 * the given parameters and translates into a selected/unselected value in the filters.
541 *
542 * @param {Object} params Parameters query object
543 * @return {Object} Filter state object
544 */
545 mw.rcfilters.dm.FiltersViewModel.prototype.getFiltersFromParameters = function ( params ) {
546 var i,
547 groupMap = {},
548 model = this,
549 base = this.getDefaultParams(),
550 result = {};
551
552 params = $.extend( {}, base, params );
553
554 // Go over the given parameters
555 $.each( params, function ( paramName, paramValue ) {
556 var itemOrGroup = model.parameterMap[ paramName ];
557
558 if ( itemOrGroup instanceof mw.rcfilters.dm.FilterItem ) {
559 // Mark the group if it has any items that are selected
560 groupMap[ itemOrGroup.getGroupName() ] = groupMap[ itemOrGroup.getGroupName() ] || {};
561 groupMap[ itemOrGroup.getGroupName() ].hasSelected = (
562 groupMap[ itemOrGroup.getGroupName() ].hasSelected ||
563 !!Number( paramValue )
564 );
565
566 // Add filters
567 groupMap[ itemOrGroup.getGroupName() ].filters = groupMap[ itemOrGroup.getGroupName() ].filters || [];
568 groupMap[ itemOrGroup.getGroupName() ].filters.push( itemOrGroup );
569 } else if ( itemOrGroup instanceof mw.rcfilters.dm.FilterGroup ) {
570 groupMap[ itemOrGroup.getName() ] = groupMap[ itemOrGroup.getName() ] || {};
571 // This parameter represents a group (values are the filters)
572 // this is equivalent to checking if the group is 'string_options'
573 groupMap[ itemOrGroup.getName() ].filters = itemOrGroup.getItems();
574 }
575 } );
576
577 // Now that we know the groups' selection states, we need to go over
578 // the filters in the groups and mark their selected states appropriately
579 $.each( groupMap, function ( group, data ) {
580 var paramValues, filterItem,
581 allItemsInGroup = data.filters;
582
583 if ( model.groups[ group ].getType() === 'send_unselected_if_any' ) {
584 for ( i = 0; i < allItemsInGroup.length; i++ ) {
585 filterItem = allItemsInGroup[ i ];
586
587 result[ filterItem.getName() ] = groupMap[ filterItem.getGroupName() ].hasSelected ?
588 // Flip the definition between the parameter
589 // state and the filter state
590 // This is what the 'toggleSelected' value of the filter is
591 !Number( params[ filterItem.getParamName() ] ) :
592 // Otherwise, there are no selected items in the
593 // group, which means the state is false
594 false;
595 }
596 } else if ( model.groups[ group ].getType() === 'string_options' ) {
597 paramValues = model.sanitizeStringOptionGroup(
598 group,
599 params[ group ].split(
600 model.groups[ group ].getSeparator()
601 )
602 );
603
604 for ( i = 0; i < allItemsInGroup.length; i++ ) {
605 filterItem = allItemsInGroup[ i ];
606
607 result[ filterItem.getName() ] = (
608 // If it is the word 'all'
609 paramValues.length === 1 && paramValues[ 0 ] === 'all' ||
610 // All values are written
611 paramValues.length === model.groups[ group ].getItemCount()
612 ) ?
613 // All true (either because all values are written or the term 'all' is written)
614 // is the same as all filters set to false
615 false :
616 // Otherwise, the filter is selected only if it appears in the parameter values
617 paramValues.indexOf( filterItem.getParamName() ) > -1;
618 }
619 }
620 } );
621
622 return result;
623 };
624
625 /**
626 * Get the item that matches the given name
627 *
628 * @param {string} name Filter name
629 * @return {mw.rcfilters.dm.FilterItem} Filter item
630 */
631 mw.rcfilters.dm.FiltersViewModel.prototype.getItemByName = function ( name ) {
632 return this.getItems().filter( function ( item ) {
633 return name === item.getName();
634 } )[ 0 ];
635 };
636
637 /**
638 * Set all filters to false or empty/all
639 * This is equivalent to display all.
640 */
641 mw.rcfilters.dm.FiltersViewModel.prototype.emptyAllFilters = function () {
642 this.getItems().forEach( function ( filterItem ) {
643 this.toggleFilterSelected( filterItem.getName(), false );
644 }.bind( this ) );
645 };
646
647 /**
648 * Toggle selected state of one item
649 *
650 * @param {string} name Name of the filter item
651 * @param {boolean} [isSelected] Filter selected state
652 */
653 mw.rcfilters.dm.FiltersViewModel.prototype.toggleFilterSelected = function ( name, isSelected ) {
654 var item = this.getItemByName( name );
655
656 if ( item ) {
657 item.toggleSelected( isSelected );
658 }
659 };
660
661 /**
662 * Toggle selected state of items by their names
663 *
664 * @param {Object} filterDef Filter definitions
665 */
666 mw.rcfilters.dm.FiltersViewModel.prototype.toggleFiltersSelected = function ( filterDef ) {
667 Object.keys( filterDef ).forEach( function ( name ) {
668 this.toggleFilterSelected( name, filterDef[ name ] );
669 }.bind( this ) );
670 };
671
672 /**
673 * Get a group model from its name
674 *
675 * @param {string} groupName Group name
676 * @return {mw.rcfilters.dm.FilterGroup} Group model
677 */
678 mw.rcfilters.dm.FiltersViewModel.prototype.getGroup = function ( groupName ) {
679 return this.groups[ groupName ];
680 };
681
682 /**
683 * Get all filters within a specified group by its name
684 *
685 * @param {string} groupName Group name
686 * @return {mw.rcfilters.dm.FilterItem[]} Filters belonging to this group
687 */
688 mw.rcfilters.dm.FiltersViewModel.prototype.getGroupFilters = function ( groupName ) {
689 return ( this.getGroup( groupName ) && this.getGroup( groupName ).getItems() ) || [];
690 };
691
692 /**
693 * Find items whose labels match the given string
694 *
695 * @param {string} query Search string
696 * @return {Object} An object of items to show
697 * arranged by their group names
698 */
699 mw.rcfilters.dm.FiltersViewModel.prototype.findMatches = function ( query ) {
700 var i,
701 groupTitle,
702 result = {},
703 items = this.getItems();
704
705 // Normalize so we can search strings regardless of case
706 query = query.toLowerCase();
707
708 // item label starting with the query string
709 for ( i = 0; i < items.length; i++ ) {
710 if ( items[ i ].getLabel().toLowerCase().indexOf( query ) === 0 ) {
711 result[ items[ i ].getGroupName() ] = result[ items[ i ].getGroupName() ] || [];
712 result[ items[ i ].getGroupName() ].push( items[ i ] );
713 }
714 }
715
716 if ( $.isEmptyObject( result ) ) {
717 // item containing the query string in their label, description, or group title
718 for ( i = 0; i < items.length; i++ ) {
719 groupTitle = items[ i ].getGroupModel().getTitle();
720 if (
721 items[ i ].getLabel().toLowerCase().indexOf( query ) > -1 ||
722 items[ i ].getDescription().toLowerCase().indexOf( query ) > -1 ||
723 groupTitle.toLowerCase().indexOf( query ) > -1
724 ) {
725 result[ items[ i ].getGroupName() ] = result[ items[ i ].getGroupName() ] || [];
726 result[ items[ i ].getGroupName() ].push( items[ i ] );
727 }
728 }
729 }
730
731 return result;
732 };
733
734 /**
735 * Get items that are highlighted
736 *
737 * @return {mw.rcfilters.dm.FilterItem[]} Highlighted items
738 */
739 mw.rcfilters.dm.FiltersViewModel.prototype.getHighlightedItems = function () {
740 return this.getItems().filter( function ( filterItem ) {
741 return filterItem.isHighlightSupported() &&
742 filterItem.getHighlightColor();
743 } );
744 };
745
746 /**
747 * Toggle the highlight feature on and off.
748 * Propagate the change to filter items.
749 *
750 * @param {boolean} enable Highlight should be enabled
751 * @fires highlightChange
752 */
753 mw.rcfilters.dm.FiltersViewModel.prototype.toggleHighlight = function ( enable ) {
754 enable = enable === undefined ? !this.highlightEnabled : enable;
755
756 if ( this.highlightEnabled !== enable ) {
757 this.highlightEnabled = enable;
758
759 this.getItems().forEach( function ( filterItem ) {
760 filterItem.toggleHighlight( this.highlightEnabled );
761 }.bind( this ) );
762
763 this.emit( 'highlightChange', this.highlightEnabled );
764 }
765 };
766
767 /**
768 * Check if the highlight feature is enabled
769 * @return {boolean}
770 */
771 mw.rcfilters.dm.FiltersViewModel.prototype.isHighlightEnabled = function () {
772 return !!this.highlightEnabled;
773 };
774
775 /**
776 * Set highlight color for a specific filter item
777 *
778 * @param {string} filterName Name of the filter item
779 * @param {string} color Selected color
780 */
781 mw.rcfilters.dm.FiltersViewModel.prototype.setHighlightColor = function ( filterName, color ) {
782 this.getItemByName( filterName ).setHighlightColor( color );
783 };
784
785 /**
786 * Clear highlight for a specific filter item
787 *
788 * @param {string} filterName Name of the filter item
789 */
790 mw.rcfilters.dm.FiltersViewModel.prototype.clearHighlightColor = function ( filterName ) {
791 this.getItemByName( filterName ).clearHighlightColor();
792 };
793
794 /**
795 * Clear highlight for all filter items
796 */
797 mw.rcfilters.dm.FiltersViewModel.prototype.clearAllHighlightColors = function () {
798 this.getItems().forEach( function ( filterItem ) {
799 filterItem.clearHighlightColor();
800 } );
801 };
802 }( mediaWiki, jQuery ) );