2 var FilterGroup
= require( './FilterGroup.js' ),
3 FilterItem
= require( './FilterItem.js' ),
7 * View model for the filters selection and display
9 * @class mw.rcfilters.dm.FiltersViewModel
10 * @mixins OO.EventEmitter
11 * @mixins OO.EmitterList
15 FiltersViewModel
= function MwRcfiltersDmFiltersViewModel() {
17 OO
.EventEmitter
.call( this );
18 OO
.EmitterList
.call( this );
21 this.defaultParams
= {};
22 this.highlightEnabled
= false;
23 this.parameterMap
= {};
24 this.emptyParameterState
= null;
27 this.currentView
= 'default';
28 this.searchQuery
= null;
31 this.aggregate( { update
: 'filterItemUpdate' } );
32 this.connect( this, { filterItemUpdate
: [ 'emit', 'itemUpdate' ] } );
36 OO
.initClass( FiltersViewModel
);
37 OO
.mixinClass( FiltersViewModel
, OO
.EventEmitter
);
38 OO
.mixinClass( FiltersViewModel
, OO
.EmitterList
);
45 * Filter list is initialized
51 * Model has been updated
56 * @param {mw.rcfilters.dm.FilterItem} item Filter item updated
58 * Filter item has changed
62 * @event highlightChange
63 * @param {boolean} Highlight feature is enabled
65 * Highlight feature has been toggled enabled or disabled
71 * Re-assess the states of filter items based on the interactions between them
73 * @param {mw.rcfilters.dm.FilterItem} [item] Changed item. If not given, the
74 * method will go over the state of all items
76 FiltersViewModel
.prototype.reassessFilterInteractions = function ( item
) {
79 iterationItems
= item
!== undefined ? [ item
] : this.getItems();
81 iterationItems
.forEach( function ( checkedItem
) {
82 var allCheckedItems
= checkedItem
.getSubset().concat( [ checkedItem
.getName() ] ),
83 groupModel
= checkedItem
.getGroupModel();
85 // Check for subsets (included filters) plus the item itself:
86 allCheckedItems
.forEach( function ( filterItemName
) {
87 var itemInSubset
= model
.getItemByName( filterItemName
);
89 itemInSubset
.toggleIncluded(
90 // If any of itemInSubset's supersets are selected, this item
92 itemInSubset
.getSuperset().some( function ( supersetName
) {
93 return ( model
.getItemByName( supersetName
).isSelected() );
98 // Update coverage for the changed group
99 if ( groupModel
.isFullCoverage() ) {
100 allSelected
= groupModel
.areAllSelected();
101 groupModel
.getItems().forEach( function ( filterItem
) {
102 filterItem
.toggleFullyCovered( allSelected
);
107 // Check for conflicts
108 // In this case, we must go over all items, since
109 // conflicts are bidirectional and depend not only on
110 // individual items, but also on the selected states of
111 // the groups they're in.
112 this.getItems().forEach( function ( filterItem
) {
113 var inConflict
= false,
114 filterItemGroup
= filterItem
.getGroupModel();
116 // For each item, see if that item is still conflicting
117 // eslint-disable-next-line no-jquery/no-each-util
118 $.each( model
.groups
, function ( groupName
, groupModel
) {
119 if ( filterItem
.getGroupName() === groupName
) {
120 // Check inside the group
121 inConflict
= groupModel
.areAnySelectedInConflictWith( filterItem
);
123 // According to the spec, if two items conflict from two different
124 // groups, the conflict only lasts if the groups **only have selected
125 // items that are conflicting**. If a group has selected items that
126 // are conflicting and non-conflicting, the scope of the result has
127 // expanded enough to completely remove the conflict.
129 // For example, see two groups with conflicts:
132 // name: 'experienced',
133 // conflicts: [ 'unregistered' ]
138 // name: 'registered',
141 // name: 'unregistered',
144 // If we select 'experienced', then 'unregistered' is in conflict (and vice versa),
145 // because, inherently, 'experienced' filter only includes registered users, and so
146 // both filters are in conflict with one another.
147 // However, the minute we select 'registered', the scope of our results
148 // has expanded to no longer have a conflict with 'experienced' filter, and
149 // so the conflict is removed.
151 // In our case, we need to check if the entire group conflicts with
152 // the entire item's group, so we follow the above spec
154 // The foreign group is in conflict with this item
155 groupModel
.areAllSelectedInConflictWith( filterItem
) &&
156 // Every selected member of the item's own group is also
157 // in conflict with the other group
158 filterItemGroup
.findSelectedItems().every( function ( otherGroupItem
) {
159 return groupModel
.areAllSelectedInConflictWith( otherGroupItem
);
164 // If we're in conflict, this will return 'false' which
165 // will break the loop. Otherwise, we're not in conflict
166 // and the loop continues
170 // Toggle the item state
171 filterItem
.toggleConflicted( inConflict
);
176 * Get whether the model has any conflict in its items
178 * @return {boolean} There is a conflict
180 FiltersViewModel
.prototype.hasConflict = function () {
181 return this.getItems().some( function ( filterItem
) {
182 return filterItem
.isSelected() && filterItem
.isConflicted();
187 * Get the first item with a current conflict
189 * @return {mw.rcfilters.dm.FilterItem|undefined} Conflicted item or undefined when not found
191 FiltersViewModel
.prototype.getFirstConflictedItem = function () {
192 var i
, filterItem
, items
= this.getItems();
193 for ( i
= 0; i
< items
.length
; i
++ ) {
194 filterItem
= items
[ i
];
195 if ( filterItem
.isSelected() && filterItem
.isConflicted() ) {
202 * Set filters and preserve a group relationship based on
203 * the definition given by an object
205 * @param {Array} filterGroups Filters definition
206 * @param {Object} [views] Extra views definition
207 * Expected in the following format:
210 * label: 'namespaces', // Message key
215 * name: 'namespaces' // Parameter name
216 * title: 'namespaces' // Message key
217 * type: 'string_options',
219 * labelPrefixKey: { 'default': 'rcfilters-tag-prefix-namespace', inverted: 'rcfilters-tag-prefix-namespace-inverted' },
227 FiltersViewModel
.prototype.initializeFilters = function ( filterGroups
, views
) {
228 var filterConflictResult
, groupConflictResult
,
232 groupConflictMap
= {},
233 filterConflictMap
= {},
235 * Expand a conflict definition from group name to
236 * the list of all included filters in that group.
237 * We do this so that the direct relationship in the
238 * models are consistently item->items rather than
239 * mixing item->group with item->item.
241 * @param {Object} obj Conflict definition
242 * @return {Object} Expanded conflict definition
244 expandConflictDefinitions = function ( obj
) {
247 // eslint-disable-next-line no-jquery/no-each-util
248 $.each( obj
, function ( key
, conflicts
) {
250 adjustedConflicts
= {};
252 conflicts
.forEach( function ( conflict
) {
255 if ( conflict
.filter
) {
256 filterName
= model
.groups
[ conflict
.group
].getPrefixedName( conflict
.filter
);
257 filter
= model
.getItemByName( filterName
);
260 adjustedConflicts
[ filterName
] = $.extend(
269 // This conflict is for an entire group. Split it up to
270 // represent each filter
272 // Get the relevant group items
273 model
.groups
[ conflict
.group
].getItems().forEach( function ( groupItem
) {
274 // Rebuild the conflict
275 adjustedConflicts
[ groupItem
.getName() ] = $.extend(
279 filter
: groupItem
.getName(),
287 result
[ key
] = adjustedConflicts
;
299 filterGroups
= OO
.copy( filterGroups
);
301 // Normalize definition from the server
302 filterGroups
.forEach( function ( data
) {
304 // What's this information needs to be normalized
306 body
: data
.whatsThisBody
,
307 header
: data
.whatsThisHeader
,
308 linkText
: data
.whatsThisLinkText
,
309 url
: data
.whatsThisUrl
312 // Title is a msg-key
313 data
.title
= data
.title
? mw
.msg( data
.title
) : data
.name
;
315 // Filters are given to us with msg-keys, we need
316 // to translate those before we hand them off
317 for ( i
= 0; i
< data
.filters
.length
; i
++ ) {
318 data
.filters
[ i
].label
= data
.filters
[ i
].label
? mw
.msg( data
.filters
[ i
].label
) : data
.filters
[ i
].name
;
319 data
.filters
[ i
].description
= data
.filters
[ i
].description
? mw
.msg( data
.filters
[ i
].description
) : '';
324 allViews
= $.extend( true, {
326 title
: mw
.msg( 'rcfilters-filterlist-title' ),
332 // eslint-disable-next-line no-jquery/no-each-util
333 $.each( allViews
, function ( viewName
, viewData
) {
335 model
.views
[ viewName
] = {
337 title
: viewData
.title
,
338 trigger
: viewData
.trigger
342 viewData
.groups
.forEach( function ( groupData
) {
343 var group
= groupData
.name
;
345 if ( !model
.groups
[ group
] ) {
346 model
.groups
[ group
] = new FilterGroup(
348 $.extend( true, {}, groupData
, { view
: viewName
} )
352 model
.groups
[ group
].initializeFilters( groupData
.filters
, groupData
.default );
353 items
= items
.concat( model
.groups
[ group
].getItems() );
356 if ( groupData
.conflicts
) {
358 groupConflictMap
[ group
] = groupData
.conflicts
;
361 groupData
.filters
.forEach( function ( itemData
) {
362 var filterItem
= model
.groups
[ group
].getItemByParamName( itemData
.name
);
364 if ( itemData
.conflicts
) {
365 filterConflictMap
[ filterItem
.getName() ] = itemData
.conflicts
;
371 // Add item references to the model, for lookup
372 this.addItems( items
);
375 groupConflictResult
= expandConflictDefinitions( groupConflictMap
);
376 filterConflictResult
= expandConflictDefinitions( filterConflictMap
);
378 // Set conflicts for groups
379 // eslint-disable-next-line no-jquery/no-each-util
380 $.each( groupConflictResult
, function ( group
, conflicts
) {
381 model
.groups
[ group
].setConflicts( conflicts
);
384 // Set conflicts for items
385 // eslint-disable-next-line no-jquery/no-each-util
386 $.each( filterConflictResult
, function ( filterName
, conflicts
) {
387 var filterItem
= model
.getItemByName( filterName
);
388 // set conflicts for items in the group
389 filterItem
.setConflicts( conflicts
);
392 // Create a map between known parameters and their models
393 // eslint-disable-next-line no-jquery/no-each-util
394 $.each( this.groups
, function ( group
, groupModel
) {
396 groupModel
.getType() === 'send_unselected_if_any' ||
397 groupModel
.getType() === 'boolean' ||
398 groupModel
.getType() === 'any_value'
400 // Individual filters
401 groupModel
.getItems().forEach( function ( filterItem
) {
402 model
.parameterMap
[ filterItem
.getParamName() ] = filterItem
;
405 groupModel
.getType() === 'string_options' ||
406 groupModel
.getType() === 'single_option'
409 model
.parameterMap
[ groupModel
.getName() ] = groupModel
;
413 this.setSearch( '' );
415 this.updateHighlightedState();
417 // Finish initialization
418 this.emit( 'initialize' );
422 * Update filter view model state based on a parameter object
424 * @param {Object} params Parameters object
426 FiltersViewModel
.prototype.updateStateFromParams = function ( params
) {
428 // For arbitrary numeric single_option values make sure the values
429 // are normalized to fit within the limits
430 // eslint-disable-next-line no-jquery/no-each-util
431 $.each( this.getFilterGroups(), function ( groupName
, groupModel
) {
432 params
[ groupName
] = groupModel
.normalizeArbitraryValue( params
[ groupName
] );
435 // Update filter values
436 filtersValue
= this.getFiltersFromParameters( params
);
437 Object
.keys( filtersValue
).forEach( function ( filterName
) {
438 this.getItemByName( filterName
).setValue( filtersValue
[ filterName
] );
441 // Update highlight state
442 this.getItemsSupportingHighlights().forEach( function ( filterItem
) {
443 var color
= params
[ filterItem
.getName() + '_color' ];
445 filterItem
.setHighlightColor( color
);
447 filterItem
.clearHighlightColor();
450 this.updateHighlightedState();
452 // Check all filter interactions
453 this.reassessFilterInteractions();
457 * Get a representation of an empty (falsey) parameter state
459 * @return {Object} Empty parameter state
461 FiltersViewModel
.prototype.getEmptyParameterState = function () {
462 if ( !this.emptyParameterState
) {
463 this.emptyParameterState
= $.extend(
466 this.getParametersFromFilters( {} ),
467 this.getEmptyHighlightParameters()
470 return this.emptyParameterState
;
474 * Get a representation of only the non-falsey parameters
476 * @param {Object} [parameters] A given parameter state to minimize. If not given the current
477 * state of the system will be used.
478 * @return {Object} Empty parameter state
480 FiltersViewModel
.prototype.getMinimizedParamRepresentation = function ( parameters
) {
483 parameters
= parameters
? $.extend( true, {}, parameters
) : this.getCurrentParameterState();
486 // eslint-disable-next-line no-jquery/no-each-util
487 $.each( this.getEmptyParameterState(), function ( param
, value
) {
488 if ( parameters
[ param
] !== undefined && parameters
[ param
] !== value
) {
489 result
[ param
] = parameters
[ param
];
494 Object
.keys( this.getEmptyHighlightParameters() ).forEach( function ( param
) {
495 if ( parameters
[ param
] ) {
496 // If a highlight parameter is not undefined and not null
497 // add it to the result
498 result
[ param
] = parameters
[ param
];
506 * Get a representation of the full parameter list, including all base values
508 * @return {Object} Full parameter representation
510 FiltersViewModel
.prototype.getExpandedParamRepresentation = function () {
514 this.getEmptyParameterState(),
515 this.getCurrentParameterState()
520 * Get a parameter representation of the current state of the model
522 * @param {boolean} [removeStickyParams] Remove sticky filters from final result
523 * @return {Object} Parameter representation of the current state of the model
525 FiltersViewModel
.prototype.getCurrentParameterState = function ( removeStickyParams
) {
526 var state
= this.getMinimizedParamRepresentation( $.extend(
529 this.getParametersFromFilters( this.getSelectedState() ),
530 this.getHighlightParameters()
533 if ( removeStickyParams
) {
534 state
= this.removeStickyParams( state
);
541 * Delete sticky parameters from given object.
543 * @param {Object} paramState Parameter state
544 * @return {Object} Parameter state without sticky parameters
546 FiltersViewModel
.prototype.removeStickyParams = function ( paramState
) {
547 this.getStickyParams().forEach( function ( paramName
) {
548 delete paramState
[ paramName
];
555 * Turn the highlight feature on or off
557 FiltersViewModel
.prototype.updateHighlightedState = function () {
558 this.toggleHighlight( this.getHighlightedItems().length
> 0 );
562 * Get the object that defines groups by their name.
564 * @return {Object} Filter groups
566 FiltersViewModel
.prototype.getFilterGroups = function () {
571 * Get the object that defines groups that match a certain view by their name.
573 * @param {string} [view] Requested view. If not given, uses current view
574 * @return {Object} Filter groups matching a display group
576 FiltersViewModel
.prototype.getFilterGroupsByView = function ( view
) {
579 view
= view
|| this.getCurrentView();
581 // eslint-disable-next-line no-jquery/no-each-util
582 $.each( this.groups
, function ( groupName
, groupModel
) {
583 if ( groupModel
.getView() === view
) {
584 result
[ groupName
] = groupModel
;
592 * Get an array of filters matching the given display group.
594 * @param {string} [view] Requested view. If not given, uses current view
595 * @return {mw.rcfilters.dm.FilterItem} Filter items matching the group
597 FiltersViewModel
.prototype.getFiltersByView = function ( view
) {
601 view
= view
|| this.getCurrentView();
603 groups
= this.getFilterGroupsByView( view
);
605 // eslint-disable-next-line no-jquery/no-each-util
606 $.each( groups
, function ( groupName
, groupModel
) {
607 result
= result
.concat( groupModel
.getItems() );
614 * Get the trigger for the requested view.
616 * @param {string} view View name
617 * @return {string} View trigger, if exists
619 FiltersViewModel
.prototype.getViewTrigger = function ( view
) {
620 return ( this.views
[ view
] && this.views
[ view
].trigger
) || '';
624 * Get the value of a specific parameter
626 * @param {string} name Parameter name
627 * @return {number|string} Parameter value
629 FiltersViewModel
.prototype.getParamValue = function ( name
) {
630 return this.parameters
[ name
];
634 * Get the current selected state of the filters
636 * @param {boolean} [onlySelected] return an object containing only the filters with a value
637 * @return {Object} Filters selected state
639 FiltersViewModel
.prototype.getSelectedState = function ( onlySelected
) {
641 items
= this.getItems(),
644 for ( i
= 0; i
< items
.length
; i
++ ) {
645 if ( !onlySelected
|| items
[ i
].getValue() ) {
646 result
[ items
[ i
].getName() ] = items
[ i
].getValue();
654 * Get the current full state of the filters
656 * @return {Object} Filters full state
658 FiltersViewModel
.prototype.getFullState = function () {
660 items
= this.getItems(),
663 for ( i
= 0; i
< items
.length
; i
++ ) {
664 result
[ items
[ i
].getName() ] = {
665 selected
: items
[ i
].isSelected(),
666 conflicted
: items
[ i
].isConflicted(),
667 included
: items
[ i
].isIncluded()
675 * Get an object representing default parameters state
677 * @return {Object} Default parameter values
679 FiltersViewModel
.prototype.getDefaultParams = function () {
682 // Get default filter state
683 // eslint-disable-next-line no-jquery/no-each-util
684 $.each( this.groups
, function ( name
, model
) {
685 if ( !model
.isSticky() ) {
686 $.extend( true, result
, model
.getDefaultParams() );
694 * Get a parameter representation of all sticky parameters
696 * @return {Object} Sticky parameter values
698 FiltersViewModel
.prototype.getStickyParams = function () {
701 // eslint-disable-next-line no-jquery/no-each-util
702 $.each( this.groups
, function ( name
, model
) {
703 if ( model
.isSticky() ) {
704 if ( model
.isPerGroupRequestParameter() ) {
707 // Each filter is its own param
708 result
= result
.concat( model
.getItems().map( function ( filterItem
) {
709 return filterItem
.getParamName();
719 * Get a parameter representation of all sticky parameters
721 * @return {Object} Sticky parameter values
723 FiltersViewModel
.prototype.getStickyParamsValues = function () {
726 // eslint-disable-next-line no-jquery/no-each-util
727 $.each( this.groups
, function ( name
, model
) {
728 if ( model
.isSticky() ) {
729 $.extend( true, result
, model
.getParamRepresentation() );
737 * Analyze the groups and their filters and output an object representing
738 * the state of the parameters they represent.
740 * @param {Object} [filterDefinition] An object defining the filter values,
741 * keyed by filter names.
742 * @return {Object} Parameter state object
744 FiltersViewModel
.prototype.getParametersFromFilters = function ( filterDefinition
) {
745 var groupItemDefinition
,
747 groupItems
= this.getFilterGroups();
749 if ( filterDefinition
) {
750 groupItemDefinition
= {};
751 // Filter definition is "flat", but in effect
752 // each group needs to tell us its result based
753 // on the values in it. We need to split this list
754 // back into groupings so we can "feed" it to the
755 // loop below, and we need to expand it so it includes
756 // all filters (set to false)
757 this.getItems().forEach( function ( filterItem
) {
758 groupItemDefinition
[ filterItem
.getGroupName() ] = groupItemDefinition
[ filterItem
.getGroupName() ] || {};
759 groupItemDefinition
[ filterItem
.getGroupName() ][ filterItem
.getName() ] = filterItem
.coerceValue( filterDefinition
[ filterItem
.getName() ] );
763 // eslint-disable-next-line no-jquery/no-each-util
764 $.each( groupItems
, function ( group
, model
) {
767 model
.getParamRepresentation(
768 groupItemDefinition
?
769 groupItemDefinition
[ group
] : null
778 * This is the opposite of the #getParametersFromFilters method; this goes over
779 * the given parameters and translates into a selected/unselected value in the filters.
781 * @param {Object} params Parameters query object
782 * @return {Object} Filter state object
784 FiltersViewModel
.prototype.getFiltersFromParameters = function ( params
) {
789 // Go over the given parameters, break apart to groupings
790 // The resulting object represents the group with its parameter
791 // values. For example:
798 // group2: "param4|param5"
800 // eslint-disable-next-line no-jquery/no-each-util
801 $.each( params
, function ( paramName
, paramValue
) {
803 itemOrGroup
= model
.parameterMap
[ paramName
];
806 groupName
= itemOrGroup
instanceof FilterItem
?
807 itemOrGroup
.getGroupName() : itemOrGroup
.getName();
809 groupMap
[ groupName
] = groupMap
[ groupName
] || {};
810 groupMap
[ groupName
][ paramName
] = paramValue
;
814 // Go over all groups, so we make sure we get the complete output
815 // even if the parameters don't include a certain group
816 // eslint-disable-next-line no-jquery/no-each-util
817 $.each( this.groups
, function ( groupName
, groupModel
) {
818 result
= $.extend( true, {}, result
, groupModel
.getFilterRepresentation( groupMap
[ groupName
] ) );
825 * Get the highlight parameters based on current filter configuration
827 * @return {Object} Object where keys are `<filter name>_color` and values
828 * are the selected highlight colors.
830 FiltersViewModel
.prototype.getHighlightParameters = function () {
831 var highlightEnabled
= this.isHighlightEnabled(),
834 this.getItems().forEach( function ( filterItem
) {
835 if ( filterItem
.isHighlightSupported() ) {
836 result
[ filterItem
.getName() + '_color' ] = highlightEnabled
&& filterItem
.isHighlighted() ?
837 filterItem
.getHighlightColor() :
846 * Get an object representing the complete empty state of highlights
848 * @return {Object} Object containing all the highlight parameters set to their negative value
850 FiltersViewModel
.prototype.getEmptyHighlightParameters = function () {
853 this.getItems().forEach( function ( filterItem
) {
854 if ( filterItem
.isHighlightSupported() ) {
855 result
[ filterItem
.getName() + '_color' ] = null;
863 * Get an array of currently applied highlight colors
865 * @return {string[]} Currently applied highlight colors
867 FiltersViewModel
.prototype.getCurrentlyUsedHighlightColors = function () {
870 if ( this.isHighlightEnabled() ) {
871 this.getHighlightedItems().forEach( function ( filterItem
) {
872 var color
= filterItem
.getHighlightColor();
874 if ( result
.indexOf( color
) === -1 ) {
875 result
.push( color
);
884 * Sanitize value group of a string_option groups type
885 * Remove duplicates and make sure to only use valid
889 * @param {string} groupName Group name
890 * @param {string[]} valueArray Array of values
891 * @return {string[]} Array of valid values
893 FiltersViewModel
.prototype.sanitizeStringOptionGroup = function ( groupName
, valueArray
) {
894 var validNames
= this.getGroupFilters( groupName
).map( function ( filterItem
) {
895 return filterItem
.getParamName();
898 return mw
.rcfilters
.utils
.normalizeParamOptions( valueArray
, validNames
);
902 * Check whether no visible filter is selected.
904 * Filter groups that are hidden or sticky are not shown in the
905 * active filters area and therefore not included in this check.
907 * @return {boolean} No visible filter is selected
909 FiltersViewModel
.prototype.areVisibleFiltersEmpty = function () {
910 // Check if there are either any selected items or any items
911 // that have highlight enabled
912 return !this.getItems().some( function ( filterItem
) {
913 var visible
= !filterItem
.getGroupModel().isSticky() && !filterItem
.getGroupModel().isHidden(),
914 active
= ( filterItem
.isSelected() || filterItem
.isHighlighted() );
915 return visible
&& active
;
920 * Check whether the invert state is a valid one. A valid invert state is one where
921 * there are actual namespaces selected.
923 * This is done to compare states to previous ones that may have had the invert model
924 * selected but effectively had no namespaces, so are not effectively different than
925 * ones where invert is not selected.
927 * @return {boolean} Invert is effectively selected
929 FiltersViewModel
.prototype.areNamespacesEffectivelyInverted = function () {
930 return this.getInvertModel().isSelected() &&
931 this.findSelectedItems().some( function ( itemModel
) {
932 return itemModel
.getGroupModel().getName() === 'namespace';
937 * Get the item that matches the given name
939 * @param {string} name Filter name
940 * @return {mw.rcfilters.dm.FilterItem} Filter item
942 FiltersViewModel
.prototype.getItemByName = function ( name
) {
943 return this.getItems().filter( function ( item
) {
944 return name
=== item
.getName();
949 * Set all filters to false or empty/all
950 * This is equivalent to display all.
952 FiltersViewModel
.prototype.emptyAllFilters = function () {
953 this.getItems().forEach( function ( filterItem
) {
954 if ( !filterItem
.getGroupModel().isSticky() ) {
955 this.toggleFilterSelected( filterItem
.getName(), false );
961 * Toggle selected state of one item
963 * @param {string} name Name of the filter item
964 * @param {boolean} [isSelected] Filter selected state
966 FiltersViewModel
.prototype.toggleFilterSelected = function ( name
, isSelected
) {
967 var item
= this.getItemByName( name
);
970 item
.toggleSelected( isSelected
);
975 * Toggle selected state of items by their names
977 * @param {Object} filterDef Filter definitions
979 FiltersViewModel
.prototype.toggleFiltersSelected = function ( filterDef
) {
980 Object
.keys( filterDef
).forEach( function ( name
) {
981 this.toggleFilterSelected( name
, filterDef
[ name
] );
986 * Get a group model from its name
988 * @param {string} groupName Group name
989 * @return {mw.rcfilters.dm.FilterGroup} Group model
991 FiltersViewModel
.prototype.getGroup = function ( groupName
) {
992 return this.groups
[ groupName
];
996 * Get all filters within a specified group by its name
998 * @param {string} groupName Group name
999 * @return {mw.rcfilters.dm.FilterItem[]} Filters belonging to this group
1001 FiltersViewModel
.prototype.getGroupFilters = function ( groupName
) {
1002 return ( this.getGroup( groupName
) && this.getGroup( groupName
).getItems() ) || [];
1006 * Find items whose labels match the given string
1008 * @param {string} query Search string
1009 * @param {boolean} [returnFlat] Return a flat array. If false, the result
1010 * is an object whose keys are the group names and values are an array of
1011 * filters per group. If set to true, returns an array of filters regardless
1013 * @return {Object} An object of items to show
1014 * arranged by their group names
1016 FiltersViewModel
.prototype.findMatches = function ( query
, returnFlat
) {
1017 var i
, searchIsEmpty
,
1021 view
= this.getViewByTrigger( query
.substr( 0, 1 ) ),
1022 items
= this.getFiltersByView( view
);
1024 // Normalize so we can search strings regardless of case and view
1025 query
= query
.trim().toLowerCase();
1026 if ( view
!== 'default' ) {
1027 query
= query
.substr( 1 );
1029 // Trim again to also intercept cases where the spaces were after the trigger
1031 query
= query
.trim();
1033 // Check if the search if actually empty; this can be a problem when
1034 // we use prefixes to denote different views
1035 searchIsEmpty
= query
.length
=== 0;
1037 // item label starting with the query string
1038 for ( i
= 0; i
< items
.length
; i
++ ) {
1041 items
[ i
].getLabel().toLowerCase().indexOf( query
) === 0 ||
1043 // For tags, we want the parameter name to be included in the search
1045 items
[ i
].getParamName().toLowerCase().indexOf( query
) > -1
1048 result
[ items
[ i
].getGroupName() ] = result
[ items
[ i
].getGroupName() ] || [];
1049 result
[ items
[ i
].getGroupName() ].push( items
[ i
] );
1050 flatResult
.push( items
[ i
] );
1054 if ( $.isEmptyObject( result
) ) {
1055 // item containing the query string in their label, description, or group title
1056 for ( i
= 0; i
< items
.length
; i
++ ) {
1057 groupTitle
= items
[ i
].getGroupModel().getTitle();
1060 items
[ i
].getLabel().toLowerCase().indexOf( query
) > -1 ||
1061 items
[ i
].getDescription().toLowerCase().indexOf( query
) > -1 ||
1062 groupTitle
.toLowerCase().indexOf( query
) > -1 ||
1064 // For tags, we want the parameter name to be included in the search
1066 items
[ i
].getParamName().toLowerCase().indexOf( query
) > -1
1069 result
[ items
[ i
].getGroupName() ] = result
[ items
[ i
].getGroupName() ] || [];
1070 result
[ items
[ i
].getGroupName() ].push( items
[ i
] );
1071 flatResult
.push( items
[ i
] );
1076 return returnFlat
? flatResult
: result
;
1080 * Get items that are highlighted
1082 * @return {mw.rcfilters.dm.FilterItem[]} Highlighted items
1084 FiltersViewModel
.prototype.getHighlightedItems = function () {
1085 return this.getItems().filter( function ( filterItem
) {
1086 return filterItem
.isHighlightSupported() &&
1087 filterItem
.getHighlightColor();
1092 * Get items that allow highlights even if they're not currently highlighted
1094 * @return {mw.rcfilters.dm.FilterItem[]} Items supporting highlights
1096 FiltersViewModel
.prototype.getItemsSupportingHighlights = function () {
1097 return this.getItems().filter( function ( filterItem
) {
1098 return filterItem
.isHighlightSupported();
1103 * Get all selected items
1105 * @return {mw.rcfilters.dm.FilterItem[]} Selected items
1107 FiltersViewModel
.prototype.findSelectedItems = function () {
1108 var allSelected
= [];
1110 // eslint-disable-next-line no-jquery/no-each-util
1111 $.each( this.getFilterGroups(), function ( groupName
, groupModel
) {
1112 allSelected
= allSelected
.concat( groupModel
.findSelectedItems() );
1119 * Get the current view
1121 * @return {string} Current view
1123 FiltersViewModel
.prototype.getCurrentView = function () {
1124 return this.currentView
;
1128 * Get the label for the current view
1130 * @param {string} viewName View name
1131 * @return {string} Label for the current view
1133 FiltersViewModel
.prototype.getViewTitle = function ( viewName
) {
1134 viewName
= viewName
|| this.getCurrentView();
1136 return this.views
[ viewName
] && this.views
[ viewName
].title
;
1140 * Get the view that fits the given trigger
1142 * @param {string} trigger Trigger
1143 * @return {string} Name of view
1145 FiltersViewModel
.prototype.getViewByTrigger = function ( trigger
) {
1146 var result
= 'default';
1148 // eslint-disable-next-line no-jquery/no-each-util
1149 $.each( this.views
, function ( name
, data
) {
1150 if ( data
.trigger
=== trigger
) {
1159 * Return a version of the given string that is without any
1162 * @param {string} str Given string
1163 * @return {string} Result
1165 FiltersViewModel
.prototype.removeViewTriggers = function ( str
) {
1166 if ( this.getViewFromString( str
) !== 'default' ) {
1167 str
= str
.substr( 1 );
1174 * Get the view from the given string by a trigger, if it exists
1176 * @param {string} str Given string
1177 * @return {string} View name
1179 FiltersViewModel
.prototype.getViewFromString = function ( str
) {
1180 return this.getViewByTrigger( str
.substr( 0, 1 ) );
1184 * Set the current search for the system.
1185 * This also dictates what items and groups are visible according
1186 * to the search in #findMatches
1188 * @param {string} searchQuery Search query, including triggers
1189 * @fires searchChange
1191 FiltersViewModel
.prototype.setSearch = function ( searchQuery
) {
1192 var visibleGroups
, visibleGroupNames
;
1194 if ( this.searchQuery
!== searchQuery
) {
1195 // Check if the view changed
1196 this.switchView( this.getViewFromString( searchQuery
) );
1198 visibleGroups
= this.findMatches( searchQuery
);
1199 visibleGroupNames
= Object
.keys( visibleGroups
);
1201 // Update visibility of items and groups
1202 // eslint-disable-next-line no-jquery/no-each-util
1203 $.each( this.getFilterGroups(), function ( groupName
, groupModel
) {
1204 // Check if the group is visible at all
1205 groupModel
.toggleVisible( visibleGroupNames
.indexOf( groupName
) !== -1 );
1206 groupModel
.setVisibleItems( visibleGroups
[ groupName
] || [] );
1209 this.searchQuery
= searchQuery
;
1210 this.emit( 'searchChange', this.searchQuery
);
1215 * Get the current search
1217 * @return {string} Current search query
1219 FiltersViewModel
.prototype.getSearch = function () {
1220 return this.searchQuery
;
1224 * Switch the current view
1227 * @param {string} view View name
1229 FiltersViewModel
.prototype.switchView = function ( view
) {
1230 if ( this.views
[ view
] && this.currentView
!== view
) {
1231 this.currentView
= view
;
1236 * Toggle the highlight feature on and off.
1237 * Propagate the change to filter items.
1239 * @param {boolean} enable Highlight should be enabled
1240 * @fires highlightChange
1242 FiltersViewModel
.prototype.toggleHighlight = function ( enable
) {
1243 enable
= enable
=== undefined ? !this.highlightEnabled
: enable
;
1245 if ( this.highlightEnabled
!== enable
) {
1246 this.highlightEnabled
= enable
;
1247 this.emit( 'highlightChange', this.highlightEnabled
);
1252 * Check if the highlight feature is enabled
1255 FiltersViewModel
.prototype.isHighlightEnabled = function () {
1256 return !!this.highlightEnabled
;
1260 * Toggle the inverted namespaces property on and off.
1261 * Propagate the change to namespace filter items.
1263 * @param {boolean} enable Inverted property is enabled
1265 FiltersViewModel
.prototype.toggleInvertedNamespaces = function ( enable
) {
1266 this.toggleFilterSelected( this.getInvertModel().getName(), enable
);
1270 * Get the model object that represents the 'invert' filter
1272 * @return {mw.rcfilters.dm.FilterItem}
1274 FiltersViewModel
.prototype.getInvertModel = function () {
1275 return this.getGroup( 'invertGroup' ).getItemByParamName( 'invert' );
1279 * Set highlight color for a specific filter item
1281 * @param {string} filterName Name of the filter item
1282 * @param {string} color Selected color
1284 FiltersViewModel
.prototype.setHighlightColor = function ( filterName
, color
) {
1285 this.getItemByName( filterName
).setHighlightColor( color
);
1289 * Clear highlight for a specific filter item
1291 * @param {string} filterName Name of the filter item
1293 FiltersViewModel
.prototype.clearHighlightColor = function ( filterName
) {
1294 this.getItemByName( filterName
).clearHighlightColor();
1297 module
.exports
= FiltersViewModel
;