1 /* eslint-disable no-restricted-properties */
4 * View model for the filters selection and display
6 * @mixins OO.EventEmitter
7 * @mixins OO.EmitterList
11 mw
.rcfilters
.dm
.FiltersViewModel
= function MwRcfiltersDmFiltersViewModel() {
13 OO
.EventEmitter
.call( this );
14 OO
.EmitterList
.call( this );
17 this.defaultParams
= {};
18 this.highlightEnabled
= false;
19 this.parameterMap
= {};
20 this.emptyParameterState
= null;
23 this.currentView
= 'default';
24 this.searchQuery
= null;
27 this.aggregate( { update
: 'filterItemUpdate' } );
28 this.connect( this, { filterItemUpdate
: [ 'emit', 'itemUpdate' ] } );
32 OO
.initClass( mw
.rcfilters
.dm
.FiltersViewModel
);
33 OO
.mixinClass( mw
.rcfilters
.dm
.FiltersViewModel
, OO
.EventEmitter
);
34 OO
.mixinClass( mw
.rcfilters
.dm
.FiltersViewModel
, OO
.EmitterList
);
41 * Filter list is initialized
47 * Model has been updated
52 * @param {mw.rcfilters.dm.FilterItem} item Filter item updated
54 * Filter item has changed
58 * @event highlightChange
59 * @param {boolean} Highlight feature is enabled
61 * Highlight feature has been toggled enabled or disabled
67 * Re-assess the states of filter items based on the interactions between them
69 * @param {mw.rcfilters.dm.FilterItem} [item] Changed item. If not given, the
70 * method will go over the state of all items
72 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.reassessFilterInteractions = function ( item
) {
75 iterationItems
= item
!== undefined ? [ item
] : this.getItems();
77 iterationItems
.forEach( function ( checkedItem
) {
78 var allCheckedItems
= checkedItem
.getSubset().concat( [ checkedItem
.getName() ] ),
79 groupModel
= checkedItem
.getGroupModel();
81 // Check for subsets (included filters) plus the item itself:
82 allCheckedItems
.forEach( function ( filterItemName
) {
83 var itemInSubset
= model
.getItemByName( filterItemName
);
85 itemInSubset
.toggleIncluded(
86 // If any of itemInSubset's supersets are selected, this item
88 itemInSubset
.getSuperset().some( function ( supersetName
) {
89 return ( model
.getItemByName( supersetName
).isSelected() );
94 // Update coverage for the changed group
95 if ( groupModel
.isFullCoverage() ) {
96 allSelected
= groupModel
.areAllSelected();
97 groupModel
.getItems().forEach( function ( filterItem
) {
98 filterItem
.toggleFullyCovered( allSelected
);
103 // Check for conflicts
104 // In this case, we must go over all items, since
105 // conflicts are bidirectional and depend not only on
106 // individual items, but also on the selected states of
107 // the groups they're in.
108 this.getItems().forEach( function ( filterItem
) {
109 var inConflict
= false,
110 filterItemGroup
= filterItem
.getGroupModel();
112 // For each item, see if that item is still conflicting
113 $.each( model
.groups
, function ( groupName
, groupModel
) {
114 if ( filterItem
.getGroupName() === groupName
) {
115 // Check inside the group
116 inConflict
= groupModel
.areAnySelectedInConflictWith( filterItem
);
118 // According to the spec, if two items conflict from two different
119 // groups, the conflict only lasts if the groups **only have selected
120 // items that are conflicting**. If a group has selected items that
121 // are conflicting and non-conflicting, the scope of the result has
122 // expanded enough to completely remove the conflict.
124 // For example, see two groups with conflicts:
127 // name: 'experienced',
128 // conflicts: [ 'unregistered' ]
133 // name: 'registered',
136 // name: 'unregistered',
139 // If we select 'experienced', then 'unregistered' is in conflict (and vice versa),
140 // because, inherently, 'experienced' filter only includes registered users, and so
141 // both filters are in conflict with one another.
142 // However, the minute we select 'registered', the scope of our results
143 // has expanded to no longer have a conflict with 'experienced' filter, and
144 // so the conflict is removed.
146 // In our case, we need to check if the entire group conflicts with
147 // the entire item's group, so we follow the above spec
149 // The foreign group is in conflict with this item
150 groupModel
.areAllSelectedInConflictWith( filterItem
) &&
151 // Every selected member of the item's own group is also
152 // in conflict with the other group
153 filterItemGroup
.findSelectedItems().every( function ( otherGroupItem
) {
154 return groupModel
.areAllSelectedInConflictWith( otherGroupItem
);
159 // If we're in conflict, this will return 'false' which
160 // will break the loop. Otherwise, we're not in conflict
161 // and the loop continues
165 // Toggle the item state
166 filterItem
.toggleConflicted( inConflict
);
171 * Get whether the model has any conflict in its items
173 * @return {boolean} There is a conflict
175 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.hasConflict = function () {
176 return this.getItems().some( function ( filterItem
) {
177 return filterItem
.isSelected() && filterItem
.isConflicted();
182 * Get the first item with a current conflict
184 * @return {mw.rcfilters.dm.FilterItem} Conflicted item
186 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFirstConflictedItem = function () {
189 this.getItems().forEach( function ( filterItem
) {
190 if ( filterItem
.isSelected() && filterItem
.isConflicted() ) {
191 conflictedItem
= filterItem
;
196 return conflictedItem
;
200 * Set filters and preserve a group relationship based on
201 * the definition given by an object
203 * @param {Array} filterGroups Filters definition
204 * @param {Object} [views] Extra views definition
205 * Expected in the following format:
208 * label: 'namespaces', // Message key
213 * name: 'namespaces' // Parameter name
214 * title: 'namespaces' // Message key
215 * type: 'string_options',
217 * labelPrefixKey: { 'default': 'rcfilters-tag-prefix-namespace', inverted: 'rcfilters-tag-prefix-namespace-inverted' },
225 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.initializeFilters = function ( filterGroups
, views
) {
226 var filterConflictResult
, groupConflictResult
,
230 groupConflictMap
= {},
231 filterConflictMap
= {},
233 * Expand a conflict definition from group name to
234 * the list of all included filters in that group.
235 * We do this so that the direct relationship in the
236 * models are consistently item->items rather than
237 * mixing item->group with item->item.
239 * @param {Object} obj Conflict definition
240 * @return {Object} Expanded conflict definition
242 expandConflictDefinitions = function ( obj
) {
245 $.each( obj
, function ( key
, conflicts
) {
247 adjustedConflicts
= {};
249 conflicts
.forEach( function ( conflict
) {
252 if ( conflict
.filter
) {
253 filterName
= model
.groups
[ conflict
.group
].getPrefixedName( conflict
.filter
);
254 filter
= model
.getItemByName( filterName
);
257 adjustedConflicts
[ filterName
] = $.extend(
266 // This conflict is for an entire group. Split it up to
267 // represent each filter
269 // Get the relevant group items
270 model
.groups
[ conflict
.group
].getItems().forEach( function ( groupItem
) {
271 // Rebuild the conflict
272 adjustedConflicts
[ groupItem
.getName() ] = $.extend(
276 filter
: groupItem
.getName(),
284 result
[ key
] = adjustedConflicts
;
296 filterGroups
= OO
.copy( filterGroups
);
298 // Normalize definition from the server
299 filterGroups
.forEach( function ( data
) {
301 // What's this information needs to be normalized
303 body
: data
.whatsThisBody
,
304 header
: data
.whatsThisHeader
,
305 linkText
: data
.whatsThisLinkText
,
306 url
: data
.whatsThisUrl
309 // Title is a msg-key
310 data
.title
= data
.title
? mw
.msg( data
.title
) : data
.name
;
312 // Filters are given to us with msg-keys, we need
313 // to translate those before we hand them off
314 for ( i
= 0; i
< data
.filters
.length
; i
++ ) {
315 data
.filters
[ i
].label
= data
.filters
[ i
].label
? mw
.msg( data
.filters
[ i
].label
) : data
.filters
[ i
].name
;
316 data
.filters
[ i
].description
= data
.filters
[ i
].description
? mw
.msg( data
.filters
[ i
].description
) : '';
321 allViews
= $.extend( true, {
323 title
: mw
.msg( 'rcfilters-filterlist-title' ),
329 $.each( allViews
, function ( viewName
, viewData
) {
331 model
.views
[ viewName
] = {
333 title
: viewData
.title
,
334 trigger
: viewData
.trigger
338 viewData
.groups
.forEach( function ( groupData
) {
339 var group
= groupData
.name
;
341 if ( !model
.groups
[ group
] ) {
342 model
.groups
[ group
] = new mw
.rcfilters
.dm
.FilterGroup(
344 $.extend( true, {}, groupData
, { view
: viewName
} )
348 model
.groups
[ group
].initializeFilters( groupData
.filters
, groupData
.default );
349 items
= items
.concat( model
.groups
[ group
].getItems() );
352 if ( groupData
.conflicts
) {
354 groupConflictMap
[ group
] = groupData
.conflicts
;
357 groupData
.filters
.forEach( function ( itemData
) {
358 var filterItem
= model
.groups
[ group
].getItemByParamName( itemData
.name
);
360 if ( itemData
.conflicts
) {
361 filterConflictMap
[ filterItem
.getName() ] = itemData
.conflicts
;
367 // Add item references to the model, for lookup
368 this.addItems( items
);
371 groupConflictResult
= expandConflictDefinitions( groupConflictMap
);
372 filterConflictResult
= expandConflictDefinitions( filterConflictMap
);
374 // Set conflicts for groups
375 $.each( groupConflictResult
, function ( group
, conflicts
) {
376 model
.groups
[ group
].setConflicts( conflicts
);
379 // Set conflicts for items
380 $.each( filterConflictResult
, function ( filterName
, conflicts
) {
381 var filterItem
= model
.getItemByName( filterName
);
382 // set conflicts for items in the group
383 filterItem
.setConflicts( conflicts
);
386 // Create a map between known parameters and their models
387 $.each( this.groups
, function ( group
, groupModel
) {
389 groupModel
.getType() === 'send_unselected_if_any' ||
390 groupModel
.getType() === 'boolean' ||
391 groupModel
.getType() === 'any_value'
393 // Individual filters
394 groupModel
.getItems().forEach( function ( filterItem
) {
395 model
.parameterMap
[ filterItem
.getParamName() ] = filterItem
;
398 groupModel
.getType() === 'string_options' ||
399 groupModel
.getType() === 'single_option'
402 model
.parameterMap
[ groupModel
.getName() ] = groupModel
;
406 this.setSearch( '' );
408 this.updateHighlightedState();
410 // Finish initialization
411 this.emit( 'initialize' );
415 * Update filter view model state based on a parameter object
417 * @param {Object} params Parameters object
419 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.updateStateFromParams = function ( params
) {
421 // For arbitrary numeric single_option values make sure the values
422 // are normalized to fit within the limits
423 $.each( this.getFilterGroups(), function ( groupName
, groupModel
) {
424 params
[ groupName
] = groupModel
.normalizeArbitraryValue( params
[ groupName
] );
427 // Update filter values
428 filtersValue
= this.getFiltersFromParameters( params
);
429 Object
.keys( filtersValue
).forEach( function ( filterName
) {
430 this.getItemByName( filterName
).setValue( filtersValue
[ filterName
] );
433 // Update highlight state
434 this.getItemsSupportingHighlights().forEach( function ( filterItem
) {
435 var color
= params
[ filterItem
.getName() + '_color' ];
437 filterItem
.setHighlightColor( color
);
439 filterItem
.clearHighlightColor();
442 this.updateHighlightedState();
444 // Check all filter interactions
445 this.reassessFilterInteractions();
449 * Get a representation of an empty (falsey) parameter state
451 * @return {Object} Empty parameter state
453 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getEmptyParameterState = function () {
454 if ( !this.emptyParameterState
) {
455 this.emptyParameterState
= $.extend(
458 this.getParametersFromFilters( {} ),
459 this.getEmptyHighlightParameters()
462 return this.emptyParameterState
;
466 * Get a representation of only the non-falsey parameters
468 * @param {Object} [parameters] A given parameter state to minimize. If not given the current
469 * state of the system will be used.
470 * @return {Object} Empty parameter state
472 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getMinimizedParamRepresentation = function ( parameters
) {
475 parameters
= parameters
? $.extend( true, {}, parameters
) : this.getCurrentParameterState();
478 $.each( this.getEmptyParameterState(), function ( param
, value
) {
479 if ( parameters
[ param
] !== undefined && parameters
[ param
] !== value
) {
480 result
[ param
] = parameters
[ param
];
485 Object
.keys( this.getEmptyHighlightParameters() ).forEach( function ( param
) {
486 if ( parameters
[ param
] ) {
487 // If a highlight parameter is not undefined and not null
488 // add it to the result
489 result
[ param
] = parameters
[ param
];
497 * Get a representation of the full parameter list, including all base values
499 * @return {Object} Full parameter representation
501 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getExpandedParamRepresentation = function () {
505 this.getEmptyParameterState(),
506 this.getCurrentParameterState()
511 * Get a parameter representation of the current state of the model
513 * @param {boolean} [removeStickyParams] Remove sticky filters from final result
514 * @return {Object} Parameter representation of the current state of the model
516 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getCurrentParameterState = function ( removeStickyParams
) {
517 var state
= this.getMinimizedParamRepresentation( $.extend(
520 this.getParametersFromFilters( this.getSelectedState() ),
521 this.getHighlightParameters()
524 if ( removeStickyParams
) {
525 state
= this.removeStickyParams( state
);
532 * Delete sticky parameters from given object.
534 * @param {Object} paramState Parameter state
535 * @return {Object} Parameter state without sticky parameters
537 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.removeStickyParams = function ( paramState
) {
538 this.getStickyParams().forEach( function ( paramName
) {
539 delete paramState
[ paramName
];
546 * Turn the highlight feature on or off
548 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.updateHighlightedState = function () {
549 this.toggleHighlight( this.getHighlightedItems().length
> 0 );
553 * Get the object that defines groups by their name.
555 * @return {Object} Filter groups
557 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFilterGroups = function () {
562 * Get the object that defines groups that match a certain view by their name.
564 * @param {string} [view] Requested view. If not given, uses current view
565 * @return {Object} Filter groups matching a display group
567 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFilterGroupsByView = function ( view
) {
570 view
= view
|| this.getCurrentView();
572 $.each( this.groups
, function ( groupName
, groupModel
) {
573 if ( groupModel
.getView() === view
) {
574 result
[ groupName
] = groupModel
;
582 * Get an array of filters matching the given display group.
584 * @param {string} [view] Requested view. If not given, uses current view
585 * @return {mw.rcfilters.dm.FilterItem} Filter items matching the group
587 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFiltersByView = function ( view
) {
591 view
= view
|| this.getCurrentView();
593 groups
= this.getFilterGroupsByView( view
);
595 $.each( groups
, function ( groupName
, groupModel
) {
596 result
= result
.concat( groupModel
.getItems() );
603 * Get the trigger for the requested view.
605 * @param {string} view View name
606 * @return {string} View trigger, if exists
608 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getViewTrigger = function ( view
) {
609 return ( this.views
[ view
] && this.views
[ view
].trigger
) || '';
613 * Get the value of a specific parameter
615 * @param {string} name Parameter name
616 * @return {number|string} Parameter value
618 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getParamValue = function ( name
) {
619 return this.parameters
[ name
];
623 * Get the current selected state of the filters
625 * @param {boolean} [onlySelected] return an object containing only the filters with a value
626 * @return {Object} Filters selected state
628 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getSelectedState = function ( onlySelected
) {
630 items
= this.getItems(),
633 for ( i
= 0; i
< items
.length
; i
++ ) {
634 if ( !onlySelected
|| items
[ i
].getValue() ) {
635 result
[ items
[ i
].getName() ] = items
[ i
].getValue();
643 * Get the current full state of the filters
645 * @return {Object} Filters full state
647 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFullState = function () {
649 items
= this.getItems(),
652 for ( i
= 0; i
< items
.length
; i
++ ) {
653 result
[ items
[ i
].getName() ] = {
654 selected
: items
[ i
].isSelected(),
655 conflicted
: items
[ i
].isConflicted(),
656 included
: items
[ i
].isIncluded()
664 * Get an object representing default parameters state
666 * @return {Object} Default parameter values
668 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getDefaultParams = function () {
671 // Get default filter state
672 $.each( this.groups
, function ( name
, model
) {
673 if ( !model
.isSticky() ) {
674 $.extend( true, result
, model
.getDefaultParams() );
682 * Get a parameter representation of all sticky parameters
684 * @return {Object} Sticky parameter values
686 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getStickyParams = function () {
689 $.each( this.groups
, function ( name
, model
) {
690 if ( model
.isSticky() ) {
691 if ( model
.isPerGroupRequestParameter() ) {
694 // Each filter is its own param
695 result
= result
.concat( model
.getItems().map( function ( filterItem
) {
696 return filterItem
.getParamName();
706 * Get a parameter representation of all sticky parameters
708 * @return {Object} Sticky parameter values
710 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getStickyParamsValues = function () {
713 $.each( this.groups
, function ( name
, model
) {
714 if ( model
.isSticky() ) {
715 $.extend( true, result
, model
.getParamRepresentation() );
723 * Analyze the groups and their filters and output an object representing
724 * the state of the parameters they represent.
726 * @param {Object} [filterDefinition] An object defining the filter values,
727 * keyed by filter names.
728 * @return {Object} Parameter state object
730 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getParametersFromFilters = function ( filterDefinition
) {
731 var groupItemDefinition
,
733 groupItems
= this.getFilterGroups();
735 if ( filterDefinition
) {
736 groupItemDefinition
= {};
737 // Filter definition is "flat", but in effect
738 // each group needs to tell us its result based
739 // on the values in it. We need to split this list
740 // back into groupings so we can "feed" it to the
741 // loop below, and we need to expand it so it includes
742 // all filters (set to false)
743 this.getItems().forEach( function ( filterItem
) {
744 groupItemDefinition
[ filterItem
.getGroupName() ] = groupItemDefinition
[ filterItem
.getGroupName() ] || {};
745 groupItemDefinition
[ filterItem
.getGroupName() ][ filterItem
.getName() ] = filterItem
.coerceValue( filterDefinition
[ filterItem
.getName() ] );
749 $.each( groupItems
, function ( group
, model
) {
752 model
.getParamRepresentation(
753 groupItemDefinition
?
754 groupItemDefinition
[ group
] : null
763 * This is the opposite of the #getParametersFromFilters method; this goes over
764 * the given parameters and translates into a selected/unselected value in the filters.
766 * @param {Object} params Parameters query object
767 * @return {Object} Filter state object
769 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFiltersFromParameters = function ( params
) {
774 // Go over the given parameters, break apart to groupings
775 // The resulting object represents the group with its parameter
776 // values. For example:
783 // group2: "param4|param5"
785 $.each( params
, function ( paramName
, paramValue
) {
787 itemOrGroup
= model
.parameterMap
[ paramName
];
790 groupName
= itemOrGroup
instanceof mw
.rcfilters
.dm
.FilterItem
?
791 itemOrGroup
.getGroupName() : itemOrGroup
.getName();
793 groupMap
[ groupName
] = groupMap
[ groupName
] || {};
794 groupMap
[ groupName
][ paramName
] = paramValue
;
798 // Go over all groups, so we make sure we get the complete output
799 // even if the parameters don't include a certain group
800 $.each( this.groups
, function ( groupName
, groupModel
) {
801 result
= $.extend( true, {}, result
, groupModel
.getFilterRepresentation( groupMap
[ groupName
] ) );
808 * Get the highlight parameters based on current filter configuration
810 * @return {Object} Object where keys are `<filter name>_color` and values
811 * are the selected highlight colors.
813 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getHighlightParameters = function () {
814 var highlightEnabled
= this.isHighlightEnabled(),
817 this.getItems().forEach( function ( filterItem
) {
818 if ( filterItem
.isHighlightSupported() ) {
819 result
[ filterItem
.getName() + '_color' ] = highlightEnabled
&& filterItem
.isHighlighted() ?
820 filterItem
.getHighlightColor() :
829 * Get an object representing the complete empty state of highlights
831 * @return {Object} Object containing all the highlight parameters set to their negative value
833 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getEmptyHighlightParameters = function () {
836 this.getItems().forEach( function ( filterItem
) {
837 if ( filterItem
.isHighlightSupported() ) {
838 result
[ filterItem
.getName() + '_color' ] = null;
846 * Get an array of currently applied highlight colors
848 * @return {string[]} Currently applied highlight colors
850 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getCurrentlyUsedHighlightColors = function () {
853 if ( this.isHighlightEnabled() ) {
854 this.getHighlightedItems().forEach( function ( filterItem
) {
855 var color
= filterItem
.getHighlightColor();
857 if ( result
.indexOf( color
) === -1 ) {
858 result
.push( color
);
867 * Sanitize value group of a string_option groups type
868 * Remove duplicates and make sure to only use valid
872 * @param {string} groupName Group name
873 * @param {string[]} valueArray Array of values
874 * @return {string[]} Array of valid values
876 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.sanitizeStringOptionGroup = function ( groupName
, valueArray
) {
877 var validNames
= this.getGroupFilters( groupName
).map( function ( filterItem
) {
878 return filterItem
.getParamName();
881 return mw
.rcfilters
.utils
.normalizeParamOptions( valueArray
, validNames
);
885 * Check whether no visible filter is selected.
887 * Filter groups that are hidden or sticky are not shown in the
888 * active filters area and therefore not included in this check.
890 * @return {boolean} No visible filter is selected
892 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.areVisibleFiltersEmpty = function () {
893 // Check if there are either any selected items or any items
894 // that have highlight enabled
895 return !this.getItems().some( function ( filterItem
) {
896 var visible
= !filterItem
.getGroupModel().isSticky() && !filterItem
.getGroupModel().isHidden(),
897 active
= ( filterItem
.isSelected() || filterItem
.isHighlighted() );
898 return visible
&& active
;
903 * Check whether the invert state is a valid one. A valid invert state is one where
904 * there are actual namespaces selected.
906 * This is done to compare states to previous ones that may have had the invert model
907 * selected but effectively had no namespaces, so are not effectively different than
908 * ones where invert is not selected.
910 * @return {boolean} Invert is effectively selected
912 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.areNamespacesEffectivelyInverted = function () {
913 return this.getInvertModel().isSelected() &&
914 this.findSelectedItems().some( function ( itemModel
) {
915 return itemModel
.getGroupModel().getName() === 'namespace';
920 * Get the item that matches the given name
922 * @param {string} name Filter name
923 * @return {mw.rcfilters.dm.FilterItem} Filter item
925 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getItemByName = function ( name
) {
926 return this.getItems().filter( function ( item
) {
927 return name
=== item
.getName();
932 * Set all filters to false or empty/all
933 * This is equivalent to display all.
935 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.emptyAllFilters = function () {
936 this.getItems().forEach( function ( filterItem
) {
937 if ( !filterItem
.getGroupModel().isSticky() ) {
938 this.toggleFilterSelected( filterItem
.getName(), false );
944 * Toggle selected state of one item
946 * @param {string} name Name of the filter item
947 * @param {boolean} [isSelected] Filter selected state
949 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.toggleFilterSelected = function ( name
, isSelected
) {
950 var item
= this.getItemByName( name
);
953 item
.toggleSelected( isSelected
);
958 * Toggle selected state of items by their names
960 * @param {Object} filterDef Filter definitions
962 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.toggleFiltersSelected = function ( filterDef
) {
963 Object
.keys( filterDef
).forEach( function ( name
) {
964 this.toggleFilterSelected( name
, filterDef
[ name
] );
969 * Get a group model from its name
971 * @param {string} groupName Group name
972 * @return {mw.rcfilters.dm.FilterGroup} Group model
974 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getGroup = function ( groupName
) {
975 return this.groups
[ groupName
];
979 * Get all filters within a specified group by its name
981 * @param {string} groupName Group name
982 * @return {mw.rcfilters.dm.FilterItem[]} Filters belonging to this group
984 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getGroupFilters = function ( groupName
) {
985 return ( this.getGroup( groupName
) && this.getGroup( groupName
).getItems() ) || [];
989 * Find items whose labels match the given string
991 * @param {string} query Search string
992 * @param {boolean} [returnFlat] Return a flat array. If false, the result
993 * is an object whose keys are the group names and values are an array of
994 * filters per group. If set to true, returns an array of filters regardless
996 * @return {Object} An object of items to show
997 * arranged by their group names
999 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.findMatches = function ( query
, returnFlat
) {
1000 var i
, searchIsEmpty
,
1004 view
= this.getViewByTrigger( query
.substr( 0, 1 ) ),
1005 items
= this.getFiltersByView( view
);
1007 // Normalize so we can search strings regardless of case and view
1008 query
= query
.trim().toLowerCase();
1009 if ( view
!== 'default' ) {
1010 query
= query
.substr( 1 );
1012 // Trim again to also intercept cases where the spaces were after the trigger
1014 query
= query
.trim();
1016 // Check if the search if actually empty; this can be a problem when
1017 // we use prefixes to denote different views
1018 searchIsEmpty
= query
.length
=== 0;
1020 // item label starting with the query string
1021 for ( i
= 0; i
< items
.length
; i
++ ) {
1024 items
[ i
].getLabel().toLowerCase().indexOf( query
) === 0 ||
1026 // For tags, we want the parameter name to be included in the search
1028 items
[ i
].getParamName().toLowerCase().indexOf( query
) > -1
1031 result
[ items
[ i
].getGroupName() ] = result
[ items
[ i
].getGroupName() ] || [];
1032 result
[ items
[ i
].getGroupName() ].push( items
[ i
] );
1033 flatResult
.push( items
[ i
] );
1037 if ( $.isEmptyObject( result
) ) {
1038 // item containing the query string in their label, description, or group title
1039 for ( i
= 0; i
< items
.length
; i
++ ) {
1040 groupTitle
= items
[ i
].getGroupModel().getTitle();
1043 items
[ i
].getLabel().toLowerCase().indexOf( query
) > -1 ||
1044 items
[ i
].getDescription().toLowerCase().indexOf( query
) > -1 ||
1045 groupTitle
.toLowerCase().indexOf( query
) > -1 ||
1047 // For tags, we want the parameter name to be included in the search
1049 items
[ i
].getParamName().toLowerCase().indexOf( query
) > -1
1052 result
[ items
[ i
].getGroupName() ] = result
[ items
[ i
].getGroupName() ] || [];
1053 result
[ items
[ i
].getGroupName() ].push( items
[ i
] );
1054 flatResult
.push( items
[ i
] );
1059 return returnFlat
? flatResult
: result
;
1063 * Get items that are highlighted
1065 * @return {mw.rcfilters.dm.FilterItem[]} Highlighted items
1067 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getHighlightedItems = function () {
1068 return this.getItems().filter( function ( filterItem
) {
1069 return filterItem
.isHighlightSupported() &&
1070 filterItem
.getHighlightColor();
1075 * Get items that allow highlights even if they're not currently highlighted
1077 * @return {mw.rcfilters.dm.FilterItem[]} Items supporting highlights
1079 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getItemsSupportingHighlights = function () {
1080 return this.getItems().filter( function ( filterItem
) {
1081 return filterItem
.isHighlightSupported();
1086 * Get all selected items
1088 * @return {mw.rcfilters.dm.FilterItem[]} Selected items
1090 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.findSelectedItems = function () {
1091 var allSelected
= [];
1093 $.each( this.getFilterGroups(), function ( groupName
, groupModel
) {
1094 allSelected
= allSelected
.concat( groupModel
.findSelectedItems() );
1101 * Get the current view
1103 * @return {string} Current view
1105 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getCurrentView = function () {
1106 return this.currentView
;
1110 * Get the label for the current view
1112 * @param {string} viewName View name
1113 * @return {string} Label for the current view
1115 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getViewTitle = function ( viewName
) {
1116 viewName
= viewName
|| this.getCurrentView();
1118 return this.views
[ viewName
] && this.views
[ viewName
].title
;
1122 * Get the view that fits the given trigger
1124 * @param {string} trigger Trigger
1125 * @return {string} Name of view
1127 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getViewByTrigger = function ( trigger
) {
1128 var result
= 'default';
1130 $.each( this.views
, function ( name
, data
) {
1131 if ( data
.trigger
=== trigger
) {
1140 * Return a version of the given string that is without any
1143 * @param {string} str Given string
1144 * @return {string} Result
1146 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.removeViewTriggers = function ( str
) {
1147 if ( this.getViewFromString( str
) !== 'default' ) {
1148 str
= str
.substr( 1 );
1155 * Get the view from the given string by a trigger, if it exists
1157 * @param {string} str Given string
1158 * @return {string} View name
1160 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getViewFromString = function ( str
) {
1161 return this.getViewByTrigger( str
.substr( 0, 1 ) );
1165 * Set the current search for the system.
1166 * This also dictates what items and groups are visible according
1167 * to the search in #findMatches
1169 * @param {string} searchQuery Search query, including triggers
1170 * @fires searchChange
1172 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.setSearch = function ( searchQuery
) {
1173 var visibleGroups
, visibleGroupNames
;
1175 if ( this.searchQuery
!== searchQuery
) {
1176 // Check if the view changed
1177 this.switchView( this.getViewFromString( searchQuery
) );
1179 visibleGroups
= this.findMatches( searchQuery
);
1180 visibleGroupNames
= Object
.keys( visibleGroups
);
1182 // Update visibility of items and groups
1183 $.each( this.getFilterGroups(), function ( groupName
, groupModel
) {
1184 // Check if the group is visible at all
1185 groupModel
.toggleVisible( visibleGroupNames
.indexOf( groupName
) !== -1 );
1186 groupModel
.setVisibleItems( visibleGroups
[ groupName
] || [] );
1189 this.searchQuery
= searchQuery
;
1190 this.emit( 'searchChange', this.searchQuery
);
1195 * Get the current search
1197 * @return {string} Current search query
1199 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getSearch = function () {
1200 return this.searchQuery
;
1204 * Switch the current view
1207 * @param {string} view View name
1209 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.switchView = function ( view
) {
1210 if ( this.views
[ view
] && this.currentView
!== view
) {
1211 this.currentView
= view
;
1216 * Toggle the highlight feature on and off.
1217 * Propagate the change to filter items.
1219 * @param {boolean} enable Highlight should be enabled
1220 * @fires highlightChange
1222 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.toggleHighlight = function ( enable
) {
1223 enable
= enable
=== undefined ? !this.highlightEnabled
: enable
;
1225 if ( this.highlightEnabled
!== enable
) {
1226 this.highlightEnabled
= enable
;
1227 this.emit( 'highlightChange', this.highlightEnabled
);
1232 * Check if the highlight feature is enabled
1235 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.isHighlightEnabled = function () {
1236 return !!this.highlightEnabled
;
1240 * Toggle the inverted namespaces property on and off.
1241 * Propagate the change to namespace filter items.
1243 * @param {boolean} enable Inverted property is enabled
1245 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.toggleInvertedNamespaces = function ( enable
) {
1246 this.toggleFilterSelected( this.getInvertModel().getName(), enable
);
1250 * Get the model object that represents the 'invert' filter
1252 * @return {mw.rcfilters.dm.FilterItem}
1254 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getInvertModel = function () {
1255 return this.getGroup( 'invertGroup' ).getItemByParamName( 'invert' );
1259 * Set highlight color for a specific filter item
1261 * @param {string} filterName Name of the filter item
1262 * @param {string} color Selected color
1264 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.setHighlightColor = function ( filterName
, color
) {
1265 this.getItemByName( filterName
).setHighlightColor( color
);
1269 * Clear highlight for a specific filter item
1271 * @param {string} filterName Name of the filter item
1273 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.clearHighlightColor = function ( filterName
) {
1274 this.getItemByName( filterName
).clearHighlightColor();