Merge "RCFilters UI: Add a 'what's this?' link to filter groups"
[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 whatsThis: {
236 body: data.whatsThisBody,
237 header: data.whatsThisHeader,
238 linkText: data.whatsThisLinkText,
239 url: data.whatsThisUrl
240 }
241 } );
242 }
243
244 if ( data.conflicts ) {
245 groupConflictMap[ group ] = data.conflicts;
246 }
247
248 selectedFilterNames = [];
249 for ( i = 0; i < data.filters.length; i++ ) {
250 data.filters[ i ].subset = data.filters[ i ].subset || [];
251 data.filters[ i ].subset = data.filters[ i ].subset.map( function ( el ) {
252 return el.filter;
253 } );
254
255 filterItem = new mw.rcfilters.dm.FilterItem( data.filters[ i ].name, model.groups[ group ], {
256 group: group,
257 label: mw.msg( data.filters[ i ].label ),
258 description: mw.msg( data.filters[ i ].description ),
259 cssClass: data.filters[ i ].cssClass
260 } );
261
262 if ( data.filters[ i ].subset ) {
263 subsetNames = [];
264 data.filters[ i ].subset.forEach( function ( subsetFilterName ) { // eslint-disable-line no-loop-func
265 var subsetName = model.groups[ group ].getNamePrefix() + subsetFilterName;
266 // For convenience, we should store each filter's "supersets" -- these are
267 // the filters that have that item in their subset list. This will just
268 // make it easier to go through whether the item has any other items
269 // that affect it (and are selected) at any given time
270 supersetMap[ subsetName ] = supersetMap[ subsetName ] || [];
271 addArrayElementsUnique(
272 supersetMap[ subsetName ],
273 filterItem.getName()
274 );
275
276 // Translate subset param name to add the group name, so we
277 // get consistent naming. We know that subsets are only within
278 // the same group
279 subsetNames.push( subsetName );
280 } );
281
282 // Set translated subset
283 filterItem.setSubset( subsetNames );
284 }
285
286 // Store conflicts
287 if ( data.filters[ i ].conflicts ) {
288 filterConflictMap[ filterItem.getName() ] = data.filters[ i ].conflicts;
289 }
290
291 if ( data.type === 'send_unselected_if_any' ) {
292 // Store the default parameter state
293 // For this group type, parameter values are direct
294 model.defaultParams[ data.filters[ i ].name ] = Number( !!data.filters[ i ].default );
295 } else if (
296 data.type === 'string_options' &&
297 data.filters[ i ].default
298 ) {
299 selectedFilterNames.push( data.filters[ i ].name );
300 }
301
302 model.groups[ group ].addItems( filterItem );
303 items.push( filterItem );
304 }
305
306 if ( data.type === 'string_options' ) {
307 // Store the default parameter group state
308 // For this group, the parameter is group name and value is the names
309 // of selected items
310 model.defaultParams[ group ] = model.sanitizeStringOptionGroup( group, selectedFilterNames ).join( model.groups[ group ].getSeparator() );
311 }
312 } );
313
314 // Expand conflicts
315 groupConflictResult = expandConflictDefinitions( groupConflictMap );
316 filterConflictResult = expandConflictDefinitions( filterConflictMap );
317
318 // Set conflicts for groups
319 $.each( groupConflictResult, function ( group, conflicts ) {
320 model.groups[ group ].setConflicts( conflicts );
321 } );
322
323 items.forEach( function ( filterItem ) {
324 // Apply the superset map
325 filterItem.setSuperset( supersetMap[ filterItem.getName() ] );
326
327 // set conflicts for item
328 if ( filterConflictResult[ filterItem.getName() ] ) {
329 filterItem.setConflicts( filterConflictResult[ filterItem.getName() ] );
330 }
331 } );
332
333 // Create a map between known parameters and their models
334 $.each( this.groups, function ( group, groupModel ) {
335 if ( groupModel.getType() === 'send_unselected_if_any' ) {
336 // Individual filters
337 groupModel.getItems().forEach( function ( filterItem ) {
338 model.parameterMap[ filterItem.getParamName() ] = filterItem;
339 } );
340 } else if ( groupModel.getType() === 'string_options' ) {
341 // Group
342 model.parameterMap[ groupModel.getName() ] = groupModel;
343 }
344 } );
345
346 // Add items to the model
347 this.addItems( items );
348
349 this.emit( 'initialize' );
350 };
351
352 /**
353 * Get the names of all available filters
354 *
355 * @return {string[]} An array of filter names
356 */
357 mw.rcfilters.dm.FiltersViewModel.prototype.getFilterNames = function () {
358 return this.getItems().map( function ( item ) { return item.getName(); } );
359 };
360
361 /**
362 * Get the object that defines groups by their name.
363 *
364 * @return {Object} Filter groups
365 */
366 mw.rcfilters.dm.FiltersViewModel.prototype.getFilterGroups = function () {
367 return this.groups;
368 };
369
370 /**
371 * Get the value of a specific parameter
372 *
373 * @param {string} name Parameter name
374 * @return {number|string} Parameter value
375 */
376 mw.rcfilters.dm.FiltersViewModel.prototype.getParamValue = function ( name ) {
377 return this.parameters[ name ];
378 };
379
380 /**
381 * Get the current selected state of the filters
382 *
383 * @return {Object} Filters selected state
384 */
385 mw.rcfilters.dm.FiltersViewModel.prototype.getSelectedState = function () {
386 var i,
387 items = this.getItems(),
388 result = {};
389
390 for ( i = 0; i < items.length; i++ ) {
391 result[ items[ i ].getName() ] = items[ i ].isSelected();
392 }
393
394 return result;
395 };
396
397 /**
398 * Get the current full state of the filters
399 *
400 * @return {Object} Filters full state
401 */
402 mw.rcfilters.dm.FiltersViewModel.prototype.getFullState = function () {
403 var i,
404 items = this.getItems(),
405 result = {};
406
407 for ( i = 0; i < items.length; i++ ) {
408 result[ items[ i ].getName() ] = {
409 selected: items[ i ].isSelected(),
410 conflicted: items[ i ].isConflicted(),
411 included: items[ i ].isIncluded()
412 };
413 }
414
415 return result;
416 };
417
418 /**
419 * Get the default parameters object
420 *
421 * @return {Object} Default parameter values
422 */
423 mw.rcfilters.dm.FiltersViewModel.prototype.getDefaultParams = function () {
424 return this.defaultParams;
425 };
426
427 /**
428 * Set all filter states to default values
429 */
430 mw.rcfilters.dm.FiltersViewModel.prototype.setFiltersToDefaults = function () {
431 var defaultFilterStates = this.getFiltersFromParameters( this.getDefaultParams() );
432
433 this.toggleFiltersSelected( defaultFilterStates );
434 };
435
436 /**
437 * Analyze the groups and their filters and output an object representing
438 * the state of the parameters they represent.
439 *
440 * @param {Object} [filterGroups] An object defining the filter groups to
441 * translate to parameters. Its structure must follow that of this.groups
442 * see #getFilterGroups
443 * @return {Object} Parameter state object
444 */
445 mw.rcfilters.dm.FiltersViewModel.prototype.getParametersFromFilters = function ( filterGroups ) {
446 var result = {},
447 groupItems = filterGroups || this.getFilterGroups();
448
449 $.each( groupItems, function ( group, model ) {
450 $.extend( result, model.getParamRepresentation() );
451 } );
452
453 return result;
454 };
455
456 /**
457 * Get the highlight parameters based on current filter configuration
458 *
459 * @return {object} Object where keys are "<filter name>_color" and values
460 * are the selected highlight colors.
461 */
462 mw.rcfilters.dm.FiltersViewModel.prototype.getHighlightParameters = function () {
463 var result = { highlight: Number( this.isHighlightEnabled() ) };
464
465 this.getItems().forEach( function ( filterItem ) {
466 result[ filterItem.getName() + '_color' ] = filterItem.getHighlightColor();
467 } );
468 return result;
469 };
470
471 /**
472 * Sanitize value group of a string_option groups type
473 * Remove duplicates and make sure to only use valid
474 * values.
475 *
476 * @private
477 * @param {string} groupName Group name
478 * @param {string[]} valueArray Array of values
479 * @return {string[]} Array of valid values
480 */
481 mw.rcfilters.dm.FiltersViewModel.prototype.sanitizeStringOptionGroup = function( groupName, valueArray ) {
482 var result = [],
483 validNames = this.getGroupFilters( groupName ).map( function ( filterItem ) {
484 return filterItem.getParamName();
485 } );
486
487 if ( valueArray.indexOf( 'all' ) > -1 ) {
488 // If anywhere in the values there's 'all', we
489 // treat it as if only 'all' was selected.
490 // Example: param=valid1,valid2,all
491 // Result: param=all
492 return [ 'all' ];
493 }
494
495 // Get rid of any dupe and invalid parameter, only output
496 // valid ones
497 // Example: param=valid1,valid2,invalid1,valid1
498 // Result: param=valid1,valid2
499 valueArray.forEach( function ( value ) {
500 if (
501 validNames.indexOf( value ) > -1 &&
502 result.indexOf( value ) === -1
503 ) {
504 result.push( value );
505 }
506 } );
507
508 return result;
509 };
510
511 /**
512 * Check whether the current filter state is set to all false.
513 *
514 * @return {boolean} Current filters are all empty
515 */
516 mw.rcfilters.dm.FiltersViewModel.prototype.areCurrentFiltersEmpty = function () {
517 // Check if there are either any selected items or any items
518 // that have highlight enabled
519 return !this.getItems().some( function ( filterItem ) {
520 return filterItem.isSelected() || filterItem.isHighlighted();
521 } );
522 };
523
524 /**
525 * Check whether the default values of the filters are all false.
526 *
527 * @return {boolean} Default filters are all false
528 */
529 mw.rcfilters.dm.FiltersViewModel.prototype.areDefaultFiltersEmpty = function () {
530 var defaultFilters;
531
532 if ( this.defaultFiltersEmpty !== null ) {
533 // We only need to do this test once,
534 // because defaults are set once per session
535 defaultFilters = this.getFiltersFromParameters();
536 this.defaultFiltersEmpty = Object.keys( defaultFilters ).every( function ( filterName ) {
537 return !defaultFilters[ filterName ];
538 } );
539 }
540
541 return this.defaultFiltersEmpty;
542 };
543
544 /**
545 * This is the opposite of the #getParametersFromFilters method; this goes over
546 * the given parameters and translates into a selected/unselected value in the filters.
547 *
548 * @param {Object} params Parameters query object
549 * @return {Object} Filter state object
550 */
551 mw.rcfilters.dm.FiltersViewModel.prototype.getFiltersFromParameters = function ( params ) {
552 var i,
553 groupMap = {},
554 model = this,
555 base = this.getDefaultParams(),
556 result = {};
557
558 params = $.extend( {}, base, params );
559
560 // Go over the given parameters
561 $.each( params, function ( paramName, paramValue ) {
562 var itemOrGroup = model.parameterMap[ paramName ];
563
564 if ( itemOrGroup instanceof mw.rcfilters.dm.FilterItem ) {
565 // Mark the group if it has any items that are selected
566 groupMap[ itemOrGroup.getGroupName() ] = groupMap[ itemOrGroup.getGroupName() ] || {};
567 groupMap[ itemOrGroup.getGroupName() ].hasSelected = (
568 groupMap[ itemOrGroup.getGroupName() ].hasSelected ||
569 !!Number( paramValue )
570 );
571
572 // Add filters
573 groupMap[ itemOrGroup.getGroupName() ].filters = groupMap[ itemOrGroup.getGroupName() ].filters || [];
574 groupMap[ itemOrGroup.getGroupName() ].filters.push( itemOrGroup );
575 } else if ( itemOrGroup instanceof mw.rcfilters.dm.FilterGroup ) {
576 groupMap[ itemOrGroup.getName() ] = groupMap[ itemOrGroup.getName() ] || {};
577 // This parameter represents a group (values are the filters)
578 // this is equivalent to checking if the group is 'string_options'
579 groupMap[ itemOrGroup.getName() ].filters = itemOrGroup.getItems();
580 }
581 } );
582
583 // Now that we know the groups' selection states, we need to go over
584 // the filters in the groups and mark their selected states appropriately
585 $.each( groupMap, function ( group, data ) {
586 var paramValues, filterItem,
587 allItemsInGroup = data.filters;
588
589 if ( model.groups[ group ].getType() === 'send_unselected_if_any' ) {
590 for ( i = 0; i < allItemsInGroup.length; i++ ) {
591 filterItem = allItemsInGroup[ i ];
592
593 result[ filterItem.getName() ] = groupMap[ filterItem.getGroupName() ].hasSelected ?
594 // Flip the definition between the parameter
595 // state and the filter state
596 // This is what the 'toggleSelected' value of the filter is
597 !Number( params[ filterItem.getParamName() ] ) :
598 // Otherwise, there are no selected items in the
599 // group, which means the state is false
600 false;
601 }
602 } else if ( model.groups[ group ].getType() === 'string_options' ) {
603 paramValues = model.sanitizeStringOptionGroup(
604 group,
605 params[ group ].split(
606 model.groups[ group ].getSeparator()
607 )
608 );
609
610 for ( i = 0; i < allItemsInGroup.length; i++ ) {
611 filterItem = allItemsInGroup[ i ];
612
613 result[ filterItem.getName() ] = (
614 // If it is the word 'all'
615 paramValues.length === 1 && paramValues[ 0 ] === 'all' ||
616 // All values are written
617 paramValues.length === model.groups[ group ].getItemCount()
618 ) ?
619 // All true (either because all values are written or the term 'all' is written)
620 // is the same as all filters set to false
621 false :
622 // Otherwise, the filter is selected only if it appears in the parameter values
623 paramValues.indexOf( filterItem.getParamName() ) > -1;
624 }
625 }
626 } );
627
628 return result;
629 };
630
631 /**
632 * Get the item that matches the given name
633 *
634 * @param {string} name Filter name
635 * @return {mw.rcfilters.dm.FilterItem} Filter item
636 */
637 mw.rcfilters.dm.FiltersViewModel.prototype.getItemByName = function ( name ) {
638 return this.getItems().filter( function ( item ) {
639 return name === item.getName();
640 } )[ 0 ];
641 };
642
643 /**
644 * Set all filters to false or empty/all
645 * This is equivalent to display all.
646 */
647 mw.rcfilters.dm.FiltersViewModel.prototype.emptyAllFilters = function () {
648 this.getItems().forEach( function ( filterItem ) {
649 this.toggleFilterSelected( filterItem.getName(), false );
650 }.bind( this ) );
651 };
652
653 /**
654 * Toggle selected state of one item
655 *
656 * @param {string} name Name of the filter item
657 * @param {boolean} [isSelected] Filter selected state
658 */
659 mw.rcfilters.dm.FiltersViewModel.prototype.toggleFilterSelected = function ( name, isSelected ) {
660 var item = this.getItemByName( name );
661
662 if ( item ) {
663 item.toggleSelected( isSelected );
664 }
665 };
666
667 /**
668 * Toggle selected state of items by their names
669 *
670 * @param {Object} filterDef Filter definitions
671 */
672 mw.rcfilters.dm.FiltersViewModel.prototype.toggleFiltersSelected = function ( filterDef ) {
673 Object.keys( filterDef ).forEach( function ( name ) {
674 this.toggleFilterSelected( name, filterDef[ name ] );
675 }.bind( this ) );
676 };
677
678 /**
679 * Get a group model from its name
680 *
681 * @param {string} groupName Group name
682 * @return {mw.rcfilters.dm.FilterGroup} Group model
683 */
684 mw.rcfilters.dm.FiltersViewModel.prototype.getGroup = function ( groupName ) {
685 return this.groups[ groupName ];
686 };
687
688 /**
689 * Get all filters within a specified group by its name
690 *
691 * @param {string} groupName Group name
692 * @return {mw.rcfilters.dm.FilterItem[]} Filters belonging to this group
693 */
694 mw.rcfilters.dm.FiltersViewModel.prototype.getGroupFilters = function ( groupName ) {
695 return ( this.getGroup( groupName ) && this.getGroup( groupName ).getItems() ) || [];
696 };
697
698 /**
699 * Find items whose labels match the given string
700 *
701 * @param {string} query Search string
702 * @return {Object} An object of items to show
703 * arranged by their group names
704 */
705 mw.rcfilters.dm.FiltersViewModel.prototype.findMatches = function ( query ) {
706 var i,
707 groupTitle,
708 result = {},
709 items = this.getItems();
710
711 // Normalize so we can search strings regardless of case
712 query = query.toLowerCase();
713
714 // item label starting with the query string
715 for ( i = 0; i < items.length; i++ ) {
716 if ( items[ i ].getLabel().toLowerCase().indexOf( query ) === 0 ) {
717 result[ items[ i ].getGroupName() ] = result[ items[ i ].getGroupName() ] || [];
718 result[ items[ i ].getGroupName() ].push( items[ i ] );
719 }
720 }
721
722 if ( $.isEmptyObject( result ) ) {
723 // item containing the query string in their label, description, or group title
724 for ( i = 0; i < items.length; i++ ) {
725 groupTitle = items[ i ].getGroupModel().getTitle();
726 if (
727 items[ i ].getLabel().toLowerCase().indexOf( query ) > -1 ||
728 items[ i ].getDescription().toLowerCase().indexOf( query ) > -1 ||
729 groupTitle.toLowerCase().indexOf( query ) > -1
730 ) {
731 result[ items[ i ].getGroupName() ] = result[ items[ i ].getGroupName() ] || [];
732 result[ items[ i ].getGroupName() ].push( items[ i ] );
733 }
734 }
735 }
736
737 return result;
738 };
739
740 /**
741 * Get items that are highlighted
742 *
743 * @return {mw.rcfilters.dm.FilterItem[]} Highlighted items
744 */
745 mw.rcfilters.dm.FiltersViewModel.prototype.getHighlightedItems = function () {
746 return this.getItems().filter( function ( filterItem ) {
747 return filterItem.isHighlightSupported() &&
748 filterItem.getHighlightColor();
749 } );
750 };
751
752 /**
753 * Toggle the highlight feature on and off.
754 * Propagate the change to filter items.
755 *
756 * @param {boolean} enable Highlight should be enabled
757 * @fires highlightChange
758 */
759 mw.rcfilters.dm.FiltersViewModel.prototype.toggleHighlight = function ( enable ) {
760 enable = enable === undefined ? !this.highlightEnabled : enable;
761
762 if ( this.highlightEnabled !== enable ) {
763 this.highlightEnabled = enable;
764
765 this.getItems().forEach( function ( filterItem ) {
766 filterItem.toggleHighlight( this.highlightEnabled );
767 }.bind( this ) );
768
769 this.emit( 'highlightChange', this.highlightEnabled );
770 }
771 };
772
773 /**
774 * Check if the highlight feature is enabled
775 * @return {boolean}
776 */
777 mw.rcfilters.dm.FiltersViewModel.prototype.isHighlightEnabled = function () {
778 return !!this.highlightEnabled;
779 };
780
781 /**
782 * Set highlight color for a specific filter item
783 *
784 * @param {string} filterName Name of the filter item
785 * @param {string} color Selected color
786 */
787 mw.rcfilters.dm.FiltersViewModel.prototype.setHighlightColor = function ( filterName, color ) {
788 this.getItemByName( filterName ).setHighlightColor( color );
789 };
790
791 /**
792 * Clear highlight for a specific filter item
793 *
794 * @param {string} filterName Name of the filter item
795 */
796 mw.rcfilters.dm.FiltersViewModel.prototype.clearHighlightColor = function ( filterName ) {
797 this.getItemByName( filterName ).clearHighlightColor();
798 };
799
800 /**
801 * Clear highlight for all filter items
802 */
803 mw.rcfilters.dm.FiltersViewModel.prototype.clearAllHighlightColors = function () {
804 this.getItems().forEach( function ( filterItem ) {
805 filterItem.clearHighlightColor();
806 } );
807 };
808 }( mediaWiki, jQuery ) );