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.highlightEnabled
= false;
18 this.parameterMap
= {};
19 this.emptyParameterState
= null;
22 this.currentView
= 'default';
23 this.searchQuery
= null;
26 this.aggregate( { update
: 'filterItemUpdate' } );
27 this.connect( this, { filterItemUpdate
: [ 'emit', 'itemUpdate' ] } );
31 OO
.initClass( mw
.rcfilters
.dm
.FiltersViewModel
);
32 OO
.mixinClass( mw
.rcfilters
.dm
.FiltersViewModel
, OO
.EventEmitter
);
33 OO
.mixinClass( mw
.rcfilters
.dm
.FiltersViewModel
, OO
.EmitterList
);
40 * Filter list is initialized
46 * Model has been updated
51 * @param {mw.rcfilters.dm.FilterItem} item Filter item updated
53 * Filter item has changed
57 * @event highlightChange
58 * @param {boolean} Highlight feature is enabled
60 * Highlight feature has been toggled enabled or disabled
66 * Re-assess the states of filter items based on the interactions between them
68 * @param {mw.rcfilters.dm.FilterItem} [item] Changed item. If not given, the
69 * method will go over the state of all items
71 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.reassessFilterInteractions = function ( item
) {
74 iterationItems
= item
!== undefined ? [ item
] : this.getItems();
76 iterationItems
.forEach( function ( checkedItem
) {
77 var allCheckedItems
= checkedItem
.getSubset().concat( [ checkedItem
.getName() ] ),
78 groupModel
= checkedItem
.getGroupModel();
80 // Check for subsets (included filters) plus the item itself:
81 allCheckedItems
.forEach( function ( filterItemName
) {
82 var itemInSubset
= model
.getItemByName( filterItemName
);
84 itemInSubset
.toggleIncluded(
85 // If any of itemInSubset's supersets are selected, this item
87 itemInSubset
.getSuperset().some( function ( supersetName
) {
88 return ( model
.getItemByName( supersetName
).isSelected() );
93 // Update coverage for the changed group
94 if ( groupModel
.isFullCoverage() ) {
95 allSelected
= groupModel
.areAllSelected();
96 groupModel
.getItems().forEach( function ( filterItem
) {
97 filterItem
.toggleFullyCovered( allSelected
);
102 // Check for conflicts
103 // In this case, we must go over all items, since
104 // conflicts are bidirectional and depend not only on
105 // individual items, but also on the selected states of
106 // the groups they're in.
107 this.getItems().forEach( function ( filterItem
) {
108 var inConflict
= false,
109 filterItemGroup
= filterItem
.getGroupModel();
111 // For each item, see if that item is still conflicting
112 // eslint-disable-next-line jquery/no-each-util
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 // eslint-disable-next-line jquery/no-each-util
246 $.each( obj
, function ( key
, conflicts
) {
248 adjustedConflicts
= {};
250 conflicts
.forEach( function ( conflict
) {
253 if ( conflict
.filter
) {
254 filterName
= model
.groups
[ conflict
.group
].getPrefixedName( conflict
.filter
);
255 filter
= model
.getItemByName( filterName
);
258 adjustedConflicts
[ filterName
] = $.extend(
267 // This conflict is for an entire group. Split it up to
268 // represent each filter
270 // Get the relevant group items
271 model
.groups
[ conflict
.group
].getItems().forEach( function ( groupItem
) {
272 // Rebuild the conflict
273 adjustedConflicts
[ groupItem
.getName() ] = $.extend(
277 filter
: groupItem
.getName(),
285 result
[ key
] = adjustedConflicts
;
297 filterGroups
= OO
.copy( filterGroups
);
299 // Normalize definition from the server
300 filterGroups
.forEach( function ( data
) {
302 // What's this information needs to be normalized
304 body
: data
.whatsThisBody
,
305 header
: data
.whatsThisHeader
,
306 linkText
: data
.whatsThisLinkText
,
307 url
: data
.whatsThisUrl
310 // Title is a msg-key
311 data
.title
= data
.title
? mw
.msg( data
.title
) : data
.name
;
313 // Filters are given to us with msg-keys, we need
314 // to translate those before we hand them off
315 for ( i
= 0; i
< data
.filters
.length
; i
++ ) {
316 data
.filters
[ i
].label
= data
.filters
[ i
].label
? mw
.msg( data
.filters
[ i
].label
) : data
.filters
[ i
].name
;
317 data
.filters
[ i
].description
= data
.filters
[ i
].description
? mw
.msg( data
.filters
[ i
].description
) : '';
322 allViews
= $.extend( true, {
324 title
: mw
.msg( 'rcfilters-filterlist-title' ),
330 // eslint-disable-next-line jquery/no-each-util
331 $.each( allViews
, function ( viewName
, viewData
) {
333 model
.views
[ viewName
] = {
335 title
: viewData
.title
,
336 trigger
: viewData
.trigger
340 viewData
.groups
.forEach( function ( groupData
) {
341 var group
= groupData
.name
;
343 if ( !model
.groups
[ group
] ) {
344 model
.groups
[ group
] = new mw
.rcfilters
.dm
.FilterGroup(
346 $.extend( true, {}, groupData
, { view
: viewName
} )
350 model
.groups
[ group
].initializeFilters( groupData
.filters
, groupData
.default );
351 items
= items
.concat( model
.groups
[ group
].getItems() );
354 if ( groupData
.conflicts
) {
356 groupConflictMap
[ group
] = groupData
.conflicts
;
359 groupData
.filters
.forEach( function ( itemData
) {
360 var filterItem
= model
.groups
[ group
].getItemByParamName( itemData
.name
);
362 if ( itemData
.conflicts
) {
363 filterConflictMap
[ filterItem
.getName() ] = itemData
.conflicts
;
369 // Add item references to the model, for lookup
370 this.addItems( items
);
373 groupConflictResult
= expandConflictDefinitions( groupConflictMap
);
374 filterConflictResult
= expandConflictDefinitions( filterConflictMap
);
376 // Set conflicts for groups
377 // eslint-disable-next-line jquery/no-each-util
378 $.each( groupConflictResult
, function ( group
, conflicts
) {
379 model
.groups
[ group
].setConflicts( conflicts
);
382 // Set conflicts for items
383 // eslint-disable-next-line jquery/no-each-util
384 $.each( filterConflictResult
, function ( filterName
, conflicts
) {
385 var filterItem
= model
.getItemByName( filterName
);
386 // set conflicts for items in the group
387 filterItem
.setConflicts( conflicts
);
390 // Create a map between known parameters and their models
391 // eslint-disable-next-line jquery/no-each-util
392 $.each( this.groups
, function ( group
, groupModel
) {
394 groupModel
.getType() === 'send_unselected_if_any' ||
395 groupModel
.getType() === 'boolean' ||
396 groupModel
.getType() === 'any_value'
398 // Individual filters
399 groupModel
.getItems().forEach( function ( filterItem
) {
400 model
.parameterMap
[ filterItem
.getParamName() ] = filterItem
;
403 groupModel
.getType() === 'string_options' ||
404 groupModel
.getType() === 'single_option'
407 model
.parameterMap
[ groupModel
.getName() ] = groupModel
;
411 this.setSearch( '' );
413 this.updateHighlightedState();
415 // Finish initialization
416 this.emit( 'initialize' );
420 * Update filter view model state based on a parameter object
422 * @param {Object} params Parameters object
424 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.updateStateFromParams = function ( params
) {
426 // For arbitrary numeric single_option values make sure the values
427 // are normalized to fit within the limits
428 // eslint-disable-next-line jquery/no-each-util
429 $.each( this.getFilterGroups(), function ( groupName
, groupModel
) {
430 params
[ groupName
] = groupModel
.normalizeArbitraryValue( params
[ groupName
] );
433 // Update filter values
434 filtersValue
= this.getFiltersFromParameters( params
);
435 Object
.keys( filtersValue
).forEach( function ( filterName
) {
436 this.getItemByName( filterName
).setValue( filtersValue
[ filterName
] );
439 // Update highlight state
440 this.getItemsSupportingHighlights().forEach( function ( filterItem
) {
441 var color
= params
[ filterItem
.getName() + '_color' ];
443 filterItem
.setHighlightColor( color
);
445 filterItem
.clearHighlightColor();
448 this.updateHighlightedState();
450 // Check all filter interactions
451 this.reassessFilterInteractions();
455 * Get a representation of an empty (falsey) parameter state
457 * @return {Object} Empty parameter state
459 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getEmptyParameterState = function () {
460 if ( !this.emptyParameterState
) {
461 this.emptyParameterState
= $.extend(
464 this.getParametersFromFilters( {} ),
465 this.getEmptyHighlightParameters()
468 return this.emptyParameterState
;
472 * Get a representation of only the non-falsey parameters
474 * @param {Object} [parameters] A given parameter state to minimize. If not given the current
475 * state of the system will be used.
476 * @return {Object} Empty parameter state
478 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getMinimizedParamRepresentation = function ( parameters
) {
481 parameters
= parameters
? $.extend( true, {}, parameters
) : this.getCurrentParameterState();
484 // eslint-disable-next-line jquery/no-each-util
485 $.each( this.getEmptyParameterState(), function ( param
, value
) {
486 if ( parameters
[ param
] !== undefined && parameters
[ param
] !== value
) {
487 result
[ param
] = parameters
[ param
];
492 Object
.keys( this.getEmptyHighlightParameters() ).forEach( function ( param
) {
493 if ( parameters
[ param
] ) {
494 // If a highlight parameter is not undefined and not null
495 // add it to the result
496 result
[ param
] = parameters
[ param
];
504 * Get a representation of the full parameter list, including all base values
506 * @return {Object} Full parameter representation
508 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getExpandedParamRepresentation = function () {
512 this.getEmptyParameterState(),
513 this.getCurrentParameterState()
518 * Get a parameter representation of the current state of the model
520 * @param {boolean} [removeStickyParams] Remove sticky filters from final result
521 * @return {Object} Parameter representation of the current state of the model
523 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getCurrentParameterState = function ( removeStickyParams
) {
524 var state
= this.getMinimizedParamRepresentation( $.extend(
527 this.getParametersFromFilters( this.getSelectedState() ),
528 this.getHighlightParameters()
531 if ( removeStickyParams
) {
532 state
= this.removeStickyParams( state
);
539 * Delete sticky parameters from given object.
541 * @param {Object} paramState Parameter state
542 * @return {Object} Parameter state without sticky parameters
544 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.removeStickyParams = function ( paramState
) {
545 this.getStickyParams().forEach( function ( paramName
) {
546 delete paramState
[ paramName
];
553 * Turn the highlight feature on or off
555 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.updateHighlightedState = function () {
556 this.toggleHighlight( this.getHighlightedItems().length
> 0 );
560 * Get the object that defines groups by their name.
562 * @return {Object} Filter groups
564 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFilterGroups = function () {
569 * Get the object that defines groups that match a certain view by their name.
571 * @param {string} [view] Requested view. If not given, uses current view
572 * @return {Object} Filter groups matching a display group
574 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFilterGroupsByView = function ( view
) {
577 view
= view
|| this.getCurrentView();
579 // eslint-disable-next-line jquery/no-each-util
580 $.each( this.groups
, function ( groupName
, groupModel
) {
581 if ( groupModel
.getView() === view
) {
582 result
[ groupName
] = groupModel
;
590 * Get an array of filters matching the given display group.
592 * @param {string} [view] Requested view. If not given, uses current view
593 * @return {mw.rcfilters.dm.FilterItem} Filter items matching the group
595 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFiltersByView = function ( view
) {
599 view
= view
|| this.getCurrentView();
601 groups
= this.getFilterGroupsByView( view
);
603 // eslint-disable-next-line jquery/no-each-util
604 $.each( groups
, function ( groupName
, groupModel
) {
605 result
= result
.concat( groupModel
.getItems() );
612 * Get the trigger for the requested view.
614 * @param {string} view View name
615 * @return {string} View trigger, if exists
617 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getViewTrigger = function ( view
) {
618 return ( this.views
[ view
] && this.views
[ view
].trigger
) || '';
622 * Get the value of a specific parameter
624 * @param {string} name Parameter name
625 * @return {number|string} Parameter value
627 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getParamValue = function ( name
) {
628 return this.parameters
[ name
];
632 * Get the current selected state of the filters
634 * @param {boolean} [onlySelected] return an object containing only the filters with a value
635 * @return {Object} Filters selected state
637 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getSelectedState = function ( onlySelected
) {
639 items
= this.getItems(),
642 for ( i
= 0; i
< items
.length
; i
++ ) {
643 if ( !onlySelected
|| items
[ i
].getValue() ) {
644 result
[ items
[ i
].getName() ] = items
[ i
].getValue();
652 * Get the current full state of the filters
654 * @return {Object} Filters full state
656 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFullState = function () {
658 items
= this.getItems(),
661 for ( i
= 0; i
< items
.length
; i
++ ) {
662 result
[ items
[ i
].getName() ] = {
663 selected
: items
[ i
].isSelected(),
664 conflicted
: items
[ i
].isConflicted(),
665 included
: items
[ i
].isIncluded()
673 * Get an object representing default parameters state
675 * @return {Object} Default parameter values
677 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getDefaultParams = function () {
680 // Get default filter state
681 // eslint-disable-next-line jquery/no-each-util
682 $.each( this.groups
, function ( name
, model
) {
683 if ( !model
.isSticky() ) {
684 $.extend( true, result
, model
.getDefaultParams() );
692 * Get a parameter representation of all sticky parameters
694 * @return {Object} Sticky parameter values
696 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getStickyParams = function () {
699 // eslint-disable-next-line jquery/no-each-util
700 $.each( this.groups
, function ( name
, model
) {
701 if ( model
.isSticky() ) {
702 if ( model
.isPerGroupRequestParameter() ) {
705 // Each filter is its own param
706 result
= result
.concat( model
.getItems().map( function ( filterItem
) {
707 return filterItem
.getParamName();
717 * Get a parameter representation of all sticky parameters
719 * @return {Object} Sticky parameter values
721 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getStickyParamsValues = function () {
724 // eslint-disable-next-line jquery/no-each-util
725 $.each( this.groups
, function ( name
, model
) {
726 if ( model
.isSticky() ) {
727 $.extend( true, result
, model
.getParamRepresentation() );
735 * Analyze the groups and their filters and output an object representing
736 * the state of the parameters they represent.
738 * @param {Object} [filterDefinition] An object defining the filter values,
739 * keyed by filter names.
740 * @return {Object} Parameter state object
742 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getParametersFromFilters = function ( filterDefinition
) {
743 var groupItemDefinition
,
745 groupItems
= this.getFilterGroups();
747 if ( filterDefinition
) {
748 groupItemDefinition
= {};
749 // Filter definition is "flat", but in effect
750 // each group needs to tell us its result based
751 // on the values in it. We need to split this list
752 // back into groupings so we can "feed" it to the
753 // loop below, and we need to expand it so it includes
754 // all filters (set to false)
755 this.getItems().forEach( function ( filterItem
) {
756 groupItemDefinition
[ filterItem
.getGroupName() ] = groupItemDefinition
[ filterItem
.getGroupName() ] || {};
757 groupItemDefinition
[ filterItem
.getGroupName() ][ filterItem
.getName() ] = filterItem
.coerceValue( filterDefinition
[ filterItem
.getName() ] );
761 // eslint-disable-next-line jquery/no-each-util
762 $.each( groupItems
, function ( group
, model
) {
765 model
.getParamRepresentation(
766 groupItemDefinition
?
767 groupItemDefinition
[ group
] : null
776 * This is the opposite of the #getParametersFromFilters method; this goes over
777 * the given parameters and translates into a selected/unselected value in the filters.
779 * @param {Object} params Parameters query object
780 * @return {Object} Filter state object
782 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFiltersFromParameters = function ( params
) {
787 // Go over the given parameters, break apart to groupings
788 // The resulting object represents the group with its parameter
789 // values. For example:
796 // group2: "param4|param5"
798 // eslint-disable-next-line jquery/no-each-util
799 $.each( params
, function ( paramName
, paramValue
) {
801 itemOrGroup
= model
.parameterMap
[ paramName
];
804 groupName
= itemOrGroup
instanceof mw
.rcfilters
.dm
.FilterItem
?
805 itemOrGroup
.getGroupName() : itemOrGroup
.getName();
807 groupMap
[ groupName
] = groupMap
[ groupName
] || {};
808 groupMap
[ groupName
][ paramName
] = paramValue
;
812 // Go over all groups, so we make sure we get the complete output
813 // even if the parameters don't include a certain group
814 // eslint-disable-next-line jquery/no-each-util
815 $.each( this.groups
, function ( groupName
, groupModel
) {
816 result
= $.extend( true, {}, result
, groupModel
.getFilterRepresentation( groupMap
[ groupName
] ) );
823 * Get the highlight parameters based on current filter configuration
825 * @return {Object} Object where keys are `<filter name>_color` and values
826 * are the selected highlight colors.
828 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getHighlightParameters = function () {
829 var highlightEnabled
= this.isHighlightEnabled(),
832 this.getItems().forEach( function ( filterItem
) {
833 if ( filterItem
.isHighlightSupported() ) {
834 result
[ filterItem
.getName() + '_color' ] = highlightEnabled
&& filterItem
.isHighlighted() ?
835 filterItem
.getHighlightColor() :
844 * Get an object representing the complete empty state of highlights
846 * @return {Object} Object containing all the highlight parameters set to their negative value
848 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getEmptyHighlightParameters = function () {
851 this.getItems().forEach( function ( filterItem
) {
852 if ( filterItem
.isHighlightSupported() ) {
853 result
[ filterItem
.getName() + '_color' ] = null;
861 * Get an array of currently applied highlight colors
863 * @return {string[]} Currently applied highlight colors
865 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getCurrentlyUsedHighlightColors = function () {
868 if ( this.isHighlightEnabled() ) {
869 this.getHighlightedItems().forEach( function ( filterItem
) {
870 var color
= filterItem
.getHighlightColor();
872 if ( result
.indexOf( color
) === -1 ) {
873 result
.push( color
);
882 * Sanitize value group of a string_option groups type
883 * Remove duplicates and make sure to only use valid
887 * @param {string} groupName Group name
888 * @param {string[]} valueArray Array of values
889 * @return {string[]} Array of valid values
891 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.sanitizeStringOptionGroup = function ( groupName
, valueArray
) {
892 var validNames
= this.getGroupFilters( groupName
).map( function ( filterItem
) {
893 return filterItem
.getParamName();
896 return mw
.rcfilters
.utils
.normalizeParamOptions( valueArray
, validNames
);
900 * Check whether no visible filter is selected.
902 * Filter groups that are hidden or sticky are not shown in the
903 * active filters area and therefore not included in this check.
905 * @return {boolean} No visible filter is selected
907 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.areVisibleFiltersEmpty = function () {
908 // Check if there are either any selected items or any items
909 // that have highlight enabled
910 return !this.getItems().some( function ( filterItem
) {
911 var visible
= !filterItem
.getGroupModel().isSticky() && !filterItem
.getGroupModel().isHidden(),
912 active
= ( filterItem
.isSelected() || filterItem
.isHighlighted() );
913 return visible
&& active
;
918 * Check whether the invert state is a valid one. A valid invert state is one where
919 * there are actual namespaces selected.
921 * This is done to compare states to previous ones that may have had the invert model
922 * selected but effectively had no namespaces, so are not effectively different than
923 * ones where invert is not selected.
925 * @return {boolean} Invert is effectively selected
927 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.areNamespacesEffectivelyInverted = function () {
928 return this.getInvertModel().isSelected() &&
929 this.findSelectedItems().some( function ( itemModel
) {
930 return itemModel
.getGroupModel().getName() === 'namespace';
935 * Get the item that matches the given name
937 * @param {string} name Filter name
938 * @return {mw.rcfilters.dm.FilterItem} Filter item
940 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getItemByName = function ( name
) {
941 return this.getItems().filter( function ( item
) {
942 return name
=== item
.getName();
947 * Set all filters to false or empty/all
948 * This is equivalent to display all.
950 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.emptyAllFilters = function () {
951 this.getItems().forEach( function ( filterItem
) {
952 if ( !filterItem
.getGroupModel().isSticky() ) {
953 this.toggleFilterSelected( filterItem
.getName(), false );
959 * Toggle selected state of one item
961 * @param {string} name Name of the filter item
962 * @param {boolean} [isSelected] Filter selected state
964 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.toggleFilterSelected = function ( name
, isSelected
) {
965 var item
= this.getItemByName( name
);
968 item
.toggleSelected( isSelected
);
973 * Toggle selected state of items by their names
975 * @param {Object} filterDef Filter definitions
977 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.toggleFiltersSelected = function ( filterDef
) {
978 Object
.keys( filterDef
).forEach( function ( name
) {
979 this.toggleFilterSelected( name
, filterDef
[ name
] );
984 * Get a group model from its name
986 * @param {string} groupName Group name
987 * @return {mw.rcfilters.dm.FilterGroup} Group model
989 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getGroup = function ( groupName
) {
990 return this.groups
[ groupName
];
994 * Get all filters within a specified group by its name
996 * @param {string} groupName Group name
997 * @return {mw.rcfilters.dm.FilterItem[]} Filters belonging to this group
999 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getGroupFilters = function ( groupName
) {
1000 return ( this.getGroup( groupName
) && this.getGroup( groupName
).getItems() ) || [];
1004 * Find items whose labels match the given string
1006 * @param {string} query Search string
1007 * @param {boolean} [returnFlat] Return a flat array. If false, the result
1008 * is an object whose keys are the group names and values are an array of
1009 * filters per group. If set to true, returns an array of filters regardless
1011 * @return {Object} An object of items to show
1012 * arranged by their group names
1014 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.findMatches = function ( query
, returnFlat
) {
1015 var i
, searchIsEmpty
,
1019 view
= this.getViewByTrigger( query
.substr( 0, 1 ) ),
1020 items
= this.getFiltersByView( view
);
1022 // Normalize so we can search strings regardless of case and view
1023 query
= query
.trim().toLowerCase();
1024 if ( view
!== 'default' ) {
1025 query
= query
.substr( 1 );
1027 // Trim again to also intercept cases where the spaces were after the trigger
1029 query
= query
.trim();
1031 // Check if the search if actually empty; this can be a problem when
1032 // we use prefixes to denote different views
1033 searchIsEmpty
= query
.length
=== 0;
1035 // item label starting with the query string
1036 for ( i
= 0; i
< items
.length
; i
++ ) {
1039 items
[ i
].getLabel().toLowerCase().indexOf( query
) === 0 ||
1041 // For tags, we want the parameter name to be included in the search
1043 items
[ i
].getParamName().toLowerCase().indexOf( query
) > -1
1046 result
[ items
[ i
].getGroupName() ] = result
[ items
[ i
].getGroupName() ] || [];
1047 result
[ items
[ i
].getGroupName() ].push( items
[ i
] );
1048 flatResult
.push( items
[ i
] );
1052 if ( $.isEmptyObject( result
) ) {
1053 // item containing the query string in their label, description, or group title
1054 for ( i
= 0; i
< items
.length
; i
++ ) {
1055 groupTitle
= items
[ i
].getGroupModel().getTitle();
1058 items
[ i
].getLabel().toLowerCase().indexOf( query
) > -1 ||
1059 items
[ i
].getDescription().toLowerCase().indexOf( query
) > -1 ||
1060 groupTitle
.toLowerCase().indexOf( query
) > -1 ||
1062 // For tags, we want the parameter name to be included in the search
1064 items
[ i
].getParamName().toLowerCase().indexOf( query
) > -1
1067 result
[ items
[ i
].getGroupName() ] = result
[ items
[ i
].getGroupName() ] || [];
1068 result
[ items
[ i
].getGroupName() ].push( items
[ i
] );
1069 flatResult
.push( items
[ i
] );
1074 return returnFlat
? flatResult
: result
;
1078 * Get items that are highlighted
1080 * @return {mw.rcfilters.dm.FilterItem[]} Highlighted items
1082 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getHighlightedItems = function () {
1083 return this.getItems().filter( function ( filterItem
) {
1084 return filterItem
.isHighlightSupported() &&
1085 filterItem
.getHighlightColor();
1090 * Get items that allow highlights even if they're not currently highlighted
1092 * @return {mw.rcfilters.dm.FilterItem[]} Items supporting highlights
1094 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getItemsSupportingHighlights = function () {
1095 return this.getItems().filter( function ( filterItem
) {
1096 return filterItem
.isHighlightSupported();
1101 * Get all selected items
1103 * @return {mw.rcfilters.dm.FilterItem[]} Selected items
1105 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.findSelectedItems = function () {
1106 var allSelected
= [];
1108 // eslint-disable-next-line jquery/no-each-util
1109 $.each( this.getFilterGroups(), function ( groupName
, groupModel
) {
1110 allSelected
= allSelected
.concat( groupModel
.findSelectedItems() );
1117 * Get the current view
1119 * @return {string} Current view
1121 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getCurrentView = function () {
1122 return this.currentView
;
1126 * Get the label for the current view
1128 * @param {string} viewName View name
1129 * @return {string} Label for the current view
1131 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getViewTitle = function ( viewName
) {
1132 viewName
= viewName
|| this.getCurrentView();
1134 return this.views
[ viewName
] && this.views
[ viewName
].title
;
1138 * Get the view that fits the given trigger
1140 * @param {string} trigger Trigger
1141 * @return {string} Name of view
1143 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getViewByTrigger = function ( trigger
) {
1144 var result
= 'default';
1146 // eslint-disable-next-line jquery/no-each-util
1147 $.each( this.views
, function ( name
, data
) {
1148 if ( data
.trigger
=== trigger
) {
1157 * Return a version of the given string that is without any
1160 * @param {string} str Given string
1161 * @return {string} Result
1163 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.removeViewTriggers = function ( str
) {
1164 if ( this.getViewFromString( str
) !== 'default' ) {
1165 str
= str
.substr( 1 );
1172 * Get the view from the given string by a trigger, if it exists
1174 * @param {string} str Given string
1175 * @return {string} View name
1177 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getViewFromString = function ( str
) {
1178 return this.getViewByTrigger( str
.substr( 0, 1 ) );
1182 * Set the current search for the system.
1183 * This also dictates what items and groups are visible according
1184 * to the search in #findMatches
1186 * @param {string} searchQuery Search query, including triggers
1187 * @fires searchChange
1189 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.setSearch = function ( searchQuery
) {
1190 var visibleGroups
, visibleGroupNames
;
1192 if ( this.searchQuery
!== searchQuery
) {
1193 // Check if the view changed
1194 this.switchView( this.getViewFromString( searchQuery
) );
1196 visibleGroups
= this.findMatches( searchQuery
);
1197 visibleGroupNames
= Object
.keys( visibleGroups
);
1199 // Update visibility of items and groups
1200 // eslint-disable-next-line jquery/no-each-util
1201 $.each( this.getFilterGroups(), function ( groupName
, groupModel
) {
1202 // Check if the group is visible at all
1203 groupModel
.toggleVisible( visibleGroupNames
.indexOf( groupName
) !== -1 );
1204 groupModel
.setVisibleItems( visibleGroups
[ groupName
] || [] );
1207 this.searchQuery
= searchQuery
;
1208 this.emit( 'searchChange', this.searchQuery
);
1213 * Get the current search
1215 * @return {string} Current search query
1217 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getSearch = function () {
1218 return this.searchQuery
;
1222 * Switch the current view
1225 * @param {string} view View name
1227 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.switchView = function ( view
) {
1228 if ( this.views
[ view
] && this.currentView
!== view
) {
1229 this.currentView
= view
;
1234 * Toggle the highlight feature on and off.
1235 * Propagate the change to filter items.
1237 * @param {boolean} enable Highlight should be enabled
1238 * @fires highlightChange
1240 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.toggleHighlight = function ( enable
) {
1241 enable
= enable
=== undefined ? !this.highlightEnabled
: enable
;
1243 if ( this.highlightEnabled
!== enable
) {
1244 this.highlightEnabled
= enable
;
1245 this.emit( 'highlightChange', this.highlightEnabled
);
1250 * Check if the highlight feature is enabled
1253 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.isHighlightEnabled = function () {
1254 return !!this.highlightEnabled
;
1258 * Toggle the inverted namespaces property on and off.
1259 * Propagate the change to namespace filter items.
1261 * @param {boolean} enable Inverted property is enabled
1263 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.toggleInvertedNamespaces = function ( enable
) {
1264 this.toggleFilterSelected( this.getInvertModel().getName(), enable
);
1268 * Get the model object that represents the 'invert' filter
1270 * @return {mw.rcfilters.dm.FilterItem}
1272 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getInvertModel = function () {
1273 return this.getGroup( 'invertGroup' ).getItemByParamName( 'invert' );
1277 * Set highlight color for a specific filter item
1279 * @param {string} filterName Name of the filter item
1280 * @param {string} color Selected color
1282 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.setHighlightColor = function ( filterName
, color
) {
1283 this.getItemByName( filterName
).setHighlightColor( color
);
1287 * Clear highlight for a specific filter item
1289 * @param {string} filterName Name of the filter item
1291 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.clearHighlightColor = function ( filterName
) {
1292 this.getItemByName( filterName
).clearHighlightColor();