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