3 * View model for the filters selection and display
5 * @mixins OO.EventEmitter
6 * @mixins OO.EmitterList
10 mw
.rcfilters
.dm
.FiltersViewModel
= function MwRcfiltersDmFiltersViewModel() {
12 OO
.EventEmitter
.call( this );
13 OO
.EmitterList
.call( this );
16 this.defaultParams
= {};
17 this.defaultFiltersEmpty
= null;
18 this.highlightEnabled
= false;
19 this.parameterMap
= {};
22 this.currentView
= 'default';
25 this.aggregate( { update
: 'filterItemUpdate' } );
26 this.connect( this, { filterItemUpdate
: [ 'emit', 'itemUpdate' ] } );
30 OO
.initClass( mw
.rcfilters
.dm
.FiltersViewModel
);
31 OO
.mixinClass( mw
.rcfilters
.dm
.FiltersViewModel
, OO
.EventEmitter
);
32 OO
.mixinClass( mw
.rcfilters
.dm
.FiltersViewModel
, OO
.EmitterList
);
39 * Filter list is initialized
45 * Model has been updated
50 * @param {mw.rcfilters.dm.FilterItem} item Filter item updated
52 * Filter item has changed
56 * @event highlightChange
57 * @param {boolean} Highlight feature is enabled
59 * Highlight feature has been toggled enabled or disabled
65 * Re-assess the states of filter items based on the interactions between them
67 * @param {mw.rcfilters.dm.FilterItem} [item] Changed item. If not given, the
68 * method will go over the state of all items
70 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.reassessFilterInteractions = function ( item
) {
73 iterationItems
= item
!== undefined ? [ item
] : this.getItems();
75 iterationItems
.forEach( function ( checkedItem
) {
76 var allCheckedItems
= checkedItem
.getSubset().concat( [ checkedItem
.getName() ] ),
77 groupModel
= checkedItem
.getGroupModel();
79 // Check for subsets (included filters) plus the item itself:
80 allCheckedItems
.forEach( function ( filterItemName
) {
81 var itemInSubset
= model
.getItemByName( filterItemName
);
83 itemInSubset
.toggleIncluded(
84 // If any of itemInSubset's supersets are selected, this item
86 itemInSubset
.getSuperset().some( function ( supersetName
) {
87 return ( model
.getItemByName( supersetName
).isSelected() );
92 // Update coverage for the changed group
93 if ( groupModel
.isFullCoverage() ) {
94 allSelected
= groupModel
.areAllSelected();
95 groupModel
.getItems().forEach( function ( filterItem
) {
96 filterItem
.toggleFullyCovered( allSelected
);
101 // Check for conflicts
102 // In this case, we must go over all items, since
103 // conflicts are bidirectional and depend not only on
104 // individual items, but also on the selected states of
105 // the groups they're in.
106 this.getItems().forEach( function ( filterItem
) {
107 var inConflict
= false,
108 filterItemGroup
= filterItem
.getGroupModel();
110 // For each item, see if that item is still conflicting
111 $.each( model
.groups
, function ( groupName
, groupModel
) {
112 if ( filterItem
.getGroupName() === groupName
) {
113 // Check inside the group
114 inConflict
= groupModel
.areAnySelectedInConflictWith( filterItem
);
116 // According to the spec, if two items conflict from two different
117 // groups, the conflict only lasts if the groups **only have selected
118 // items that are conflicting**. If a group has selected items that
119 // are conflicting and non-conflicting, the scope of the result has
120 // expanded enough to completely remove the conflict.
122 // For example, see two groups with conflicts:
125 // name: 'experienced',
126 // conflicts: [ 'unregistered' ]
131 // name: 'registered',
134 // name: 'unregistered',
137 // If we select 'experienced', then 'unregistered' is in conflict (and vice versa),
138 // because, inherently, 'experienced' filter only includes registered users, and so
139 // both filters are in conflict with one another.
140 // However, the minute we select 'registered', the scope of our results
141 // has expanded to no longer have a conflict with 'experienced' filter, and
142 // so the conflict is removed.
144 // In our case, we need to check if the entire group conflicts with
145 // the entire item's group, so we follow the above spec
147 // The foreign group is in conflict with this item
148 groupModel
.areAllSelectedInConflictWith( filterItem
) &&
149 // Every selected member of the item's own group is also
150 // in conflict with the other group
151 filterItemGroup
.getSelectedItems().every( function ( otherGroupItem
) {
152 return groupModel
.areAllSelectedInConflictWith( otherGroupItem
);
157 // If we're in conflict, this will return 'false' which
158 // will break the loop. Otherwise, we're not in conflict
159 // and the loop continues
163 // Toggle the item state
164 filterItem
.toggleConflicted( inConflict
);
169 * Get whether the model has any conflict in its items
171 * @return {boolean} There is a conflict
173 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.hasConflict = function () {
174 return this.getItems().some( function ( filterItem
) {
175 return filterItem
.isSelected() && filterItem
.isConflicted();
180 * Get the first item with a current conflict
182 * @return {mw.rcfilters.dm.FilterItem} Conflicted item
184 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFirstConflictedItem = function () {
187 $.each( this.getItems(), function ( index
, filterItem
) {
188 if ( filterItem
.isSelected() && filterItem
.isConflicted() ) {
189 conflictedItem
= filterItem
;
194 return conflictedItem
;
198 * Set filters and preserve a group relationship based on
199 * the definition given by an object
201 * @param {Array} filterGroups Filters definition
202 * @param {Object} [views] Extra views definition
203 * Expected in the following format:
206 * label: 'namespaces', // Message key
211 * name: 'namespaces' // Parameter name
212 * title: 'namespaces' // Message key
213 * type: 'string_options',
215 * labelPrefixKey: { 'default': 'rcfilters-tag-prefix-namespace', inverted: 'rcfilters-tag-prefix-namespace-inverted' },
223 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.initializeFilters = function ( filterGroups
, views
) {
224 var filterConflictResult
, groupConflictResult
,
228 groupConflictMap
= {},
229 filterConflictMap
= {},
231 * Expand a conflict definition from group name to
232 * the list of all included filters in that group.
233 * We do this so that the direct relationship in the
234 * models are consistently item->items rather than
235 * mixing item->group with item->item.
237 * @param {Object} obj Conflict definition
238 * @return {Object} Expanded conflict definition
240 expandConflictDefinitions = function ( obj
) {
243 $.each( obj
, function ( key
, conflicts
) {
245 adjustedConflicts
= {};
247 conflicts
.forEach( function ( conflict
) {
250 if ( conflict
.filter
) {
251 filterName
= model
.groups
[ conflict
.group
].getPrefixedName( conflict
.filter
);
252 filter
= model
.getItemByName( filterName
);
255 adjustedConflicts
[ filterName
] = $.extend(
264 // This conflict is for an entire group. Split it up to
265 // represent each filter
267 // Get the relevant group items
268 model
.groups
[ conflict
.group
].getItems().forEach( function ( groupItem
) {
269 // Rebuild the conflict
270 adjustedConflicts
[ groupItem
.getName() ] = $.extend(
274 filter
: groupItem
.getName(),
282 result
[ key
] = adjustedConflicts
;
294 filterGroups
= OO
.copy( filterGroups
);
296 // Normalize definition from the server
297 filterGroups
.forEach( function ( data
) {
299 // What's this information needs to be normalized
301 body
: data
.whatsThisBody
,
302 header
: data
.whatsThisHeader
,
303 linkText
: data
.whatsThisLinkText
,
304 url
: data
.whatsThisUrl
307 // Title is a msg-key
308 data
.title
= data
.title
? mw
.msg( data
.title
) : data
.name
;
310 // Filters are given to us with msg-keys, we need
311 // to translate those before we hand them off
312 for ( i
= 0; i
< data
.filters
.length
; i
++ ) {
313 data
.filters
[ i
].label
= data
.filters
[ i
].label
? mw
.msg( data
.filters
[ i
].label
) : data
.filters
[ i
].name
;
314 data
.filters
[ i
].description
= data
.filters
[ i
].description
? mw
.msg( data
.filters
[ i
].description
) : '';
319 allViews
= $.extend( true, {
321 title
: mw
.msg( 'rcfilters-filterlist-title' ),
327 $.each( allViews
, function ( viewName
, viewData
) {
329 model
.views
[ viewName
] = {
331 title
: viewData
.title
,
332 trigger
: viewData
.trigger
336 viewData
.groups
.forEach( function ( groupData
) {
337 var group
= groupData
.name
;
339 if ( !model
.groups
[ group
] ) {
340 model
.groups
[ group
] = new mw
.rcfilters
.dm
.FilterGroup(
342 $.extend( true, {}, groupData
, { view
: viewName
} )
346 model
.groups
[ group
].initializeFilters( groupData
.filters
, groupData
.default );
347 items
= items
.concat( model
.groups
[ group
].getItems() );
350 if ( groupData
.conflicts
) {
352 groupConflictMap
[ group
] = groupData
.conflicts
;
355 groupData
.filters
.forEach( function ( itemData
) {
356 var filterItem
= model
.groups
[ group
].getItemByParamName( itemData
.name
);
358 if ( itemData
.conflicts
) {
359 filterConflictMap
[ filterItem
.getName() ] = itemData
.conflicts
;
365 // Add item references to the model, for lookup
366 this.addItems( items
);
369 groupConflictResult
= expandConflictDefinitions( groupConflictMap
);
370 filterConflictResult
= expandConflictDefinitions( filterConflictMap
);
372 // Set conflicts for groups
373 $.each( groupConflictResult
, function ( group
, conflicts
) {
374 model
.groups
[ group
].setConflicts( conflicts
);
377 // Set conflicts for items
378 $.each( filterConflictResult
, function ( filterName
, conflicts
) {
379 var filterItem
= model
.getItemByName( filterName
);
380 // set conflicts for items in the group
381 filterItem
.setConflicts( conflicts
);
384 // Create a map between known parameters and their models
385 $.each( this.groups
, function ( group
, groupModel
) {
387 groupModel
.getType() === 'send_unselected_if_any' ||
388 groupModel
.getType() === 'boolean'
390 // Individual filters
391 groupModel
.getItems().forEach( function ( filterItem
) {
392 model
.parameterMap
[ filterItem
.getParamName() ] = filterItem
;
395 groupModel
.getType() === 'string_options' ||
396 groupModel
.getType() === 'single_option'
399 model
.parameterMap
[ groupModel
.getName() ] = groupModel
;
403 this.currentView
= 'default';
405 if ( this.getHighlightedItems().length
> 0 ) {
406 this.toggleHighlight( true );
409 // Finish initialization
410 this.emit( 'initialize' );
414 * Get the names of all available filters
416 * @return {string[]} An array of filter names
418 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFilterNames = function () {
419 return this.getItems().map( function ( item
) { return item
.getName(); } );
423 * Get the object that defines groups by their name.
425 * @return {Object} Filter groups
427 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFilterGroups = function () {
432 * Get the object that defines groups that match a certain view by their name.
434 * @param {string} [view] Requested view. If not given, uses current view
435 * @return {Object} Filter groups matching a display group
437 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFilterGroupsByView = function ( view
) {
440 view
= view
|| this.getCurrentView();
442 $.each( this.groups
, function ( groupName
, groupModel
) {
443 if ( groupModel
.getView() === view
) {
444 result
[ groupName
] = groupModel
;
452 * Get an array of filters matching the given display group.
454 * @param {string} [view] Requested view. If not given, uses current view
455 * @return {mw.rcfilters.dm.FilterItem} Filter items matching the group
457 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFiltersByView = function ( view
) {
461 view
= view
|| this.getCurrentView();
463 groups
= this.getFilterGroupsByView( view
);
465 $.each( groups
, function ( groupName
, groupModel
) {
466 result
= result
.concat( groupModel
.getItems() );
473 * Get the trigger for the requested view.
475 * @param {string} view View name
476 * @return {string} View trigger, if exists
478 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getViewTrigger = function ( view
) {
479 return ( this.views
[ view
] && this.views
[ view
].trigger
) || '';
482 * Get the value of a specific parameter
484 * @param {string} name Parameter name
485 * @return {number|string} Parameter value
487 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getParamValue = function ( name
) {
488 return this.parameters
[ name
];
492 * Get the current selected state of the filters
494 * @return {Object} Filters selected state
496 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getSelectedState = function () {
498 items
= this.getItems(),
501 for ( i
= 0; i
< items
.length
; i
++ ) {
502 result
[ items
[ i
].getName() ] = items
[ i
].isSelected();
509 * Get the current full state of the filters
511 * @return {Object} Filters full state
513 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFullState = function () {
515 items
= this.getItems(),
518 for ( i
= 0; i
< items
.length
; i
++ ) {
519 result
[ items
[ i
].getName() ] = {
520 selected
: items
[ i
].isSelected(),
521 conflicted
: items
[ i
].isConflicted(),
522 included
: items
[ i
].isIncluded()
530 * Get an object representing default parameters state
532 * @return {Object} Default parameter values
534 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getDefaultParams = function () {
537 // Get default filter state
538 $.each( this.groups
, function ( name
, model
) {
539 $.extend( true, result
, model
.getDefaultParams() );
546 * Get a parameter representation of all sticky parameters
548 * @return {Object} Sticky parameter values
550 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getStickyParams = function () {
553 $.each( this.groups
, function ( name
, model
) {
554 if ( model
.isSticky() ) {
555 $.extend( true, result
, model
.getDefaultParams() );
563 * Get a filter representation of all sticky parameters
565 * @return {Object} Sticky filters values
567 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getStickyFiltersState = function () {
570 $.each( this.groups
, function ( name
, model
) {
571 if ( model
.isSticky() ) {
572 $.extend( true, result
, model
.getSelectedState() );
580 * Get a filter representation of all parameters that are marked
581 * as being excluded from saved query.
583 * @return {Object} Excluded filters values
585 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getExcludedFiltersState = function () {
588 $.each( this.groups
, function ( name
, model
) {
589 if ( model
.isExcludedFromSavedQueries() ) {
590 $.extend( true, result
, model
.getSelectedState() );
598 * Get the parameter names that represent filters that are excluded
599 * from saved queries.
601 * @return {string[]} Parameter names
603 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getExcludedParams = function () {
606 $.each( this.groups
, function ( name
, model
) {
607 if ( model
.isExcludedFromSavedQueries() ) {
608 if ( model
.isPerGroupRequestParameter() ) {
611 // Each filter is its own param
612 result
= result
.concat( model
.getItems().map( function ( filterItem
) {
613 return filterItem
.getParamName();
623 * Analyze the groups and their filters and output an object representing
624 * the state of the parameters they represent.
626 * @param {Object} [filterDefinition] An object defining the filter values,
627 * keyed by filter names.
628 * @return {Object} Parameter state object
630 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getParametersFromFilters = function ( filterDefinition
) {
631 var groupItemDefinition
,
633 groupItems
= this.getFilterGroups();
635 if ( filterDefinition
) {
636 groupItemDefinition
= {};
637 // Filter definition is "flat", but in effect
638 // each group needs to tell us its result based
639 // on the values in it. We need to split this list
640 // back into groupings so we can "feed" it to the
641 // loop below, and we need to expand it so it includes
642 // all filters (set to false)
643 this.getItems().forEach( function ( filterItem
) {
644 groupItemDefinition
[ filterItem
.getGroupName() ] = groupItemDefinition
[ filterItem
.getGroupName() ] || {};
645 groupItemDefinition
[ filterItem
.getGroupName() ][ filterItem
.getName() ] = !!filterDefinition
[ filterItem
.getName() ];
649 $.each( groupItems
, function ( group
, model
) {
652 model
.getParamRepresentation(
653 groupItemDefinition
?
654 groupItemDefinition
[ group
] : null
663 * This is the opposite of the #getParametersFromFilters method; this goes over
664 * the given parameters and translates into a selected/unselected value in the filters.
666 * @param {Object} params Parameters query object
667 * @return {Object} Filter state object
669 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFiltersFromParameters = function ( params
) {
674 // Go over the given parameters, break apart to groupings
675 // The resulting object represents the group with its parameter
676 // values. For example:
683 // group2: "param4|param5"
685 $.each( params
, function ( paramName
, paramValue
) {
687 itemOrGroup
= model
.parameterMap
[ paramName
];
690 groupName
= itemOrGroup
instanceof mw
.rcfilters
.dm
.FilterItem
?
691 itemOrGroup
.getGroupName() : itemOrGroup
.getName();
693 groupMap
[ groupName
] = groupMap
[ groupName
] || {};
694 groupMap
[ groupName
][ paramName
] = paramValue
;
698 // Go over all groups, so we make sure we get the complete output
699 // even if the parameters don't include a certain group
700 $.each( this.groups
, function ( groupName
, groupModel
) {
701 result
= $.extend( true, {}, result
, groupModel
.getFilterRepresentation( groupMap
[ groupName
] ) );
708 * Get the highlight parameters based on current filter configuration
710 * @return {Object} Object where keys are `<filter name>_color` and values
711 * are the selected highlight colors.
713 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getHighlightParameters = function () {
716 this.getItems().forEach( function ( filterItem
) {
717 result
[ filterItem
.getName() + '_color' ] = filterItem
.getHighlightColor() || null;
719 result
.highlight
= String( Number( this.isHighlightEnabled() ) );
725 * Get an object representing the complete empty state of highlights
727 * @return {Object} Object containing all the highlight parameters set to their negative value
729 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getEmptyHighlightParameters = function () {
732 this.getItems().forEach( function ( filterItem
) {
733 result
[ filterItem
.getName() + '_color' ] = null;
735 result
.highlight
= '0';
741 * Extract the highlight values from given object. Since highlights are
742 * the same for filter and parameters, it doesn't matter which one is
743 * given; values will be returned with a full list of the highlights
744 * with colors or null values.
746 * @param {Object} representation Object containing representation of
747 * some or all highlight values
748 * @return {Object} Object where keys are `<filter name>_color` and values
749 * are the selected highlight colors. The returned object
750 * contains all available filters either with a color value
753 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.extractHighlightValues = function ( representation
) {
756 this.getItems().forEach( function ( filterItem
) {
757 var highlightName
= filterItem
.getName() + '_color';
758 result
[ highlightName
] = representation
[ highlightName
] || null;
765 * Get an array of currently applied highlight colors
767 * @return {string[]} Currently applied highlight colors
769 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getCurrentlyUsedHighlightColors = function () {
772 this.getHighlightedItems().forEach( function ( filterItem
) {
773 var color
= filterItem
.getHighlightColor();
775 if ( result
.indexOf( color
) === -1 ) {
776 result
.push( color
);
784 * Sanitize value group of a string_option groups type
785 * Remove duplicates and make sure to only use valid
789 * @param {string} groupName Group name
790 * @param {string[]} valueArray Array of values
791 * @return {string[]} Array of valid values
793 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.sanitizeStringOptionGroup = function ( groupName
, valueArray
) {
794 var validNames
= this.getGroupFilters( groupName
).map( function ( filterItem
) {
795 return filterItem
.getParamName();
798 return mw
.rcfilters
.utils
.normalizeParamOptions( valueArray
, validNames
);
802 * Check whether the current filter state is set to all false.
804 * @return {boolean} Current filters are all empty
806 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.areCurrentFiltersEmpty = function () {
807 // Check if there are either any selected items or any items
808 // that have highlight enabled
809 return !this.getItems().some( function ( filterItem
) {
810 return !filterItem
.getGroupModel().isHidden() && ( filterItem
.isSelected() || filterItem
.isHighlighted() );
815 * Get the item that matches the given name
817 * @param {string} name Filter name
818 * @return {mw.rcfilters.dm.FilterItem} Filter item
820 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getItemByName = function ( name
) {
821 return this.getItems().filter( function ( item
) {
822 return name
=== item
.getName();
827 * Set all filters to false or empty/all
828 * This is equivalent to display all.
830 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.emptyAllFilters = function () {
831 this.getItems().forEach( function ( filterItem
) {
832 if ( !filterItem
.getGroupModel().isSticky() ) {
833 this.toggleFilterSelected( filterItem
.getName(), false );
839 * Toggle selected state of one item
841 * @param {string} name Name of the filter item
842 * @param {boolean} [isSelected] Filter selected state
844 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.toggleFilterSelected = function ( name
, isSelected
) {
845 var item
= this.getItemByName( name
);
848 item
.toggleSelected( isSelected
);
853 * Toggle selected state of items by their names
855 * @param {Object} filterDef Filter definitions
857 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.toggleFiltersSelected = function ( filterDef
) {
858 Object
.keys( filterDef
).forEach( function ( name
) {
859 this.toggleFilterSelected( name
, filterDef
[ name
] );
864 * Get a group model from its name
866 * @param {string} groupName Group name
867 * @return {mw.rcfilters.dm.FilterGroup} Group model
869 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getGroup = function ( groupName
) {
870 return this.groups
[ groupName
];
874 * Get all filters within a specified group by its name
876 * @param {string} groupName Group name
877 * @return {mw.rcfilters.dm.FilterItem[]} Filters belonging to this group
879 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getGroupFilters = function ( groupName
) {
880 return ( this.getGroup( groupName
) && this.getGroup( groupName
).getItems() ) || [];
884 * Find items whose labels match the given string
886 * @param {string} query Search string
887 * @param {boolean} [returnFlat] Return a flat array. If false, the result
888 * is an object whose keys are the group names and values are an array of
889 * filters per group. If set to true, returns an array of filters regardless
891 * @return {Object} An object of items to show
892 * arranged by their group names
894 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.findMatches = function ( query
, returnFlat
) {
895 var i
, searchIsEmpty
,
899 view
= this.getViewByTrigger( query
.substr( 0, 1 ) ),
900 items
= this.getFiltersByView( view
);
902 // Normalize so we can search strings regardless of case and view
903 query
= query
.trim().toLowerCase();
904 if ( view
!== 'default' ) {
905 query
= query
.substr( 1 );
907 // Trim again to also intercept cases where the spaces were after the trigger
909 query
= query
.trim();
911 // Check if the search if actually empty; this can be a problem when
912 // we use prefixes to denote different views
913 searchIsEmpty
= query
.length
=== 0;
915 // item label starting with the query string
916 for ( i
= 0; i
< items
.length
; i
++ ) {
919 items
[ i
].getLabel().toLowerCase().indexOf( query
) === 0 ||
921 // For tags, we want the parameter name to be included in the search
923 items
[ i
].getParamName().toLowerCase().indexOf( query
) > -1
926 result
[ items
[ i
].getGroupName() ] = result
[ items
[ i
].getGroupName() ] || [];
927 result
[ items
[ i
].getGroupName() ].push( items
[ i
] );
928 flatResult
.push( items
[ i
] );
932 if ( $.isEmptyObject( result
) ) {
933 // item containing the query string in their label, description, or group title
934 for ( i
= 0; i
< items
.length
; i
++ ) {
935 groupTitle
= items
[ i
].getGroupModel().getTitle();
938 items
[ i
].getLabel().toLowerCase().indexOf( query
) > -1 ||
939 items
[ i
].getDescription().toLowerCase().indexOf( query
) > -1 ||
940 groupTitle
.toLowerCase().indexOf( query
) > -1 ||
942 // For tags, we want the parameter name to be included in the search
944 items
[ i
].getParamName().toLowerCase().indexOf( query
) > -1
947 result
[ items
[ i
].getGroupName() ] = result
[ items
[ i
].getGroupName() ] || [];
948 result
[ items
[ i
].getGroupName() ].push( items
[ i
] );
949 flatResult
.push( items
[ i
] );
954 return returnFlat
? flatResult
: result
;
958 * Get items that are highlighted
960 * @return {mw.rcfilters.dm.FilterItem[]} Highlighted items
962 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getHighlightedItems = function () {
963 return this.getItems().filter( function ( filterItem
) {
964 return filterItem
.isHighlightSupported() &&
965 filterItem
.getHighlightColor();
970 * Get items that allow highlights even if they're not currently highlighted
972 * @return {mw.rcfilters.dm.FilterItem[]} Items supporting highlights
974 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getItemsSupportingHighlights = function () {
975 return this.getItems().filter( function ( filterItem
) {
976 return filterItem
.isHighlightSupported();
981 * Get all selected items
983 * @return {mw.rcfilters.dm.FilterItem[]} Selected items
985 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getSelectedItems = function () {
986 var allSelected
= [];
988 $.each( this.getFilterGroups(), function ( groupName
, groupModel
) {
989 allSelected
= allSelected
.concat( groupModel
.getSelectedItems() );
995 * Switch the current view
997 * @param {string} view View name
1000 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.switchView = function ( view
) {
1001 if ( this.views
[ view
] && this.currentView
!== view
) {
1002 this.currentView
= view
;
1003 this.emit( 'update' );
1008 * Get the current view
1010 * @return {string} Current view
1012 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getCurrentView = function () {
1013 return this.currentView
;
1017 * Get the label for the current view
1019 * @param {string} viewName View name
1020 * @return {string} Label for the current view
1022 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getViewTitle = function ( viewName
) {
1023 viewName
= viewName
|| this.getCurrentView();
1025 return this.views
[ viewName
] && this.views
[ viewName
].title
;
1029 * Get an array of all available view names
1031 * @return {string} Available view names
1033 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getAvailableViews = function () {
1034 return Object
.keys( this.views
);
1038 * Get the view that fits the given trigger
1040 * @param {string} trigger Trigger
1041 * @return {string} Name of view
1043 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getViewByTrigger = function ( trigger
) {
1044 var result
= 'default';
1046 $.each( this.views
, function ( name
, data
) {
1047 if ( data
.trigger
=== trigger
) {
1056 * Toggle the highlight feature on and off.
1057 * Propagate the change to filter items.
1059 * @param {boolean} enable Highlight should be enabled
1060 * @fires highlightChange
1062 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.toggleHighlight = function ( enable
) {
1063 enable
= enable
=== undefined ? !this.highlightEnabled
: enable
;
1065 if ( this.highlightEnabled
!== enable
) {
1066 // HACK make sure highlights are disabled globally while we toggle on the items,
1067 // otherwise we'll call clearHighlight() and applyHighlight() many many times
1068 this.highlightEnabled
= false;
1069 this.getItems().forEach( function ( filterItem
) {
1070 filterItem
.toggleHighlight( enable
);
1073 this.highlightEnabled
= enable
;
1074 this.emit( 'highlightChange', this.highlightEnabled
);
1079 * Check if the highlight feature is enabled
1082 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.isHighlightEnabled = function () {
1083 return !!this.highlightEnabled
;
1087 * Toggle the inverted namespaces property on and off.
1088 * Propagate the change to namespace filter items.
1090 * @param {boolean} enable Inverted property is enabled
1092 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.toggleInvertedNamespaces = function ( enable
) {
1093 this.toggleFilterSelected( this.getInvertModel().getName(), enable
);
1097 * Get the model object that represents the 'invert' filter
1099 * @return {mw.rcfilters.dm.FilterItem}
1101 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getInvertModel = function () {
1102 return this.getGroup( 'invertGroup' ).getItemByParamName( 'invert' );
1106 * Set highlight color for a specific filter item
1108 * @param {string} filterName Name of the filter item
1109 * @param {string} color Selected color
1111 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.setHighlightColor = function ( filterName
, color
) {
1112 this.getItemByName( filterName
).setHighlightColor( color
);
1116 * Clear highlight for a specific filter item
1118 * @param {string} filterName Name of the filter item
1120 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.clearHighlightColor = function ( filterName
) {
1121 this.getItemByName( filterName
).clearHighlightColor();
1125 * Clear highlight for all filter items
1127 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.clearAllHighlightColors = function () {
1128 this.getItems().forEach( function ( filterItem
) {
1129 filterItem
.clearHighlightColor();
1134 * Return a version of the given string that is without any
1137 * @param {string} str Given string
1138 * @return {string} Result
1140 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.removeViewTriggers = function ( str
) {
1141 if ( this.getViewByTrigger( str
.substr( 0, 1 ) ) !== 'default' ) {
1142 str
= str
.substr( 1 );
1147 }( mediaWiki
, jQuery
) );