3 * View model for the filters selection and display
5 * @mixins OO.EventEmitter
6 * @mixins OO.EmitterList
10 mw
.rcfilters
.dm
.FiltersViewModel
= function MwRcfiltersDmFiltersViewModel() {
12 OO
.EventEmitter
.call( this );
13 OO
.EmitterList
.call( this );
16 this.defaultParams
= {};
17 this.defaultFiltersEmpty
= null;
18 this.highlightEnabled
= false;
19 this.invertedNamespaces
= false;
20 this.parameterMap
= {};
23 this.currentView
= 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
65 * @param {boolean} isInverted Namespace selected is inverted
67 * Namespace selection is inverted or straight forward
73 * Re-assess the states of filter items based on the interactions between them
75 * @param {mw.rcfilters.dm.FilterItem} [item] Changed item. If not given, the
76 * method will go over the state of all items
78 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.reassessFilterInteractions = function ( item
) {
81 iterationItems
= item
!== undefined ? [ item
] : this.getItems();
83 iterationItems
.forEach( function ( checkedItem
) {
84 var allCheckedItems
= checkedItem
.getSubset().concat( [ checkedItem
.getName() ] ),
85 groupModel
= checkedItem
.getGroupModel();
87 // Check for subsets (included filters) plus the item itself:
88 allCheckedItems
.forEach( function ( filterItemName
) {
89 var itemInSubset
= model
.getItemByName( filterItemName
);
91 itemInSubset
.toggleIncluded(
92 // If any of itemInSubset's supersets are selected, this item
94 itemInSubset
.getSuperset().some( function ( supersetName
) {
95 return ( model
.getItemByName( supersetName
).isSelected() );
100 // Update coverage for the changed group
101 if ( groupModel
.isFullCoverage() ) {
102 allSelected
= groupModel
.areAllSelected();
103 groupModel
.getItems().forEach( function ( filterItem
) {
104 filterItem
.toggleFullyCovered( allSelected
);
109 // Check for conflicts
110 // In this case, we must go over all items, since
111 // conflicts are bidirectional and depend not only on
112 // individual items, but also on the selected states of
113 // the groups they're in.
114 this.getItems().forEach( function ( filterItem
) {
115 var inConflict
= false,
116 filterItemGroup
= filterItem
.getGroupModel();
118 // For each item, see if that item is still conflicting
119 $.each( model
.groups
, function ( groupName
, groupModel
) {
120 if ( filterItem
.getGroupName() === groupName
) {
121 // Check inside the group
122 inConflict
= groupModel
.areAnySelectedInConflictWith( filterItem
);
124 // According to the spec, if two items conflict from two different
125 // groups, the conflict only lasts if the groups **only have selected
126 // items that are conflicting**. If a group has selected items that
127 // are conflicting and non-conflicting, the scope of the result has
128 // expanded enough to completely remove the conflict.
130 // For example, see two groups with conflicts:
133 // name: 'experienced',
134 // conflicts: [ 'unregistered' ]
139 // name: 'registered',
142 // name: 'unregistered',
145 // If we select 'experienced', then 'unregistered' is in conflict (and vice versa),
146 // because, inherently, 'experienced' filter only includes registered users, and so
147 // both filters are in conflict with one another.
148 // However, the minute we select 'registered', the scope of our results
149 // has expanded to no longer have a conflict with 'experienced' filter, and
150 // so the conflict is removed.
152 // In our case, we need to check if the entire group conflicts with
153 // the entire item's group, so we follow the above spec
155 // The foreign group is in conflict with this item
156 groupModel
.areAllSelectedInConflictWith( filterItem
) &&
157 // Every selected member of the item's own group is also
158 // in conflict with the other group
159 filterItemGroup
.getSelectedItems().every( function ( otherGroupItem
) {
160 return groupModel
.areAllSelectedInConflictWith( otherGroupItem
);
165 // If we're in conflict, this will return 'false' which
166 // will break the loop. Otherwise, we're not in conflict
167 // and the loop continues
171 // Toggle the item state
172 filterItem
.toggleConflicted( inConflict
);
177 * Get whether the model has any conflict in its items
179 * @return {boolean} There is a conflict
181 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.hasConflict = function () {
182 return this.getItems().some( function ( filterItem
) {
183 return filterItem
.isSelected() && filterItem
.isConflicted();
188 * Get the first item with a current conflict
190 * @return {mw.rcfilters.dm.FilterItem} Conflicted item
192 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFirstConflictedItem = function () {
195 $.each( this.getItems(), function ( index
, filterItem
) {
196 if ( filterItem
.isSelected() && filterItem
.isConflicted() ) {
197 conflictedItem
= filterItem
;
202 return conflictedItem
;
206 * Set filters and preserve a group relationship based on
207 * the definition given by an object
209 * @param {Array} filters Filter group definition
210 * @param {Object} [namespaces] Namespace definition
212 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.initializeFilters = function ( filters
, namespaces
) {
213 var filterItem
, filterConflictResult
, groupConflictResult
,
216 namespaceDefinition
= [],
217 groupConflictMap
= {},
218 filterConflictMap
= {},
220 * Expand a conflict definition from group name to
221 * the list of all included filters in that group.
222 * We do this so that the direct relationship in the
223 * models are consistently item->items rather than
224 * mixing item->group with item->item.
226 * @param {Object} obj Conflict definition
227 * @return {Object} Expanded conflict definition
229 expandConflictDefinitions = function ( obj
) {
232 $.each( obj
, function ( key
, conflicts
) {
234 adjustedConflicts
= {};
236 conflicts
.forEach( function ( conflict
) {
239 if ( conflict
.filter
) {
240 filterName
= model
.groups
[ conflict
.group
].getPrefixedName( conflict
.filter
);
241 filter
= model
.getItemByName( filterName
);
244 adjustedConflicts
[ filterName
] = $.extend(
253 // This conflict is for an entire group. Split it up to
254 // represent each filter
256 // Get the relevant group items
257 model
.groups
[ conflict
.group
].getItems().forEach( function ( groupItem
) {
258 // Rebuild the conflict
259 adjustedConflicts
[ groupItem
.getName() ] = $.extend(
263 filter
: groupItem
.getName(),
271 result
[ key
] = adjustedConflicts
;
283 this.views
.default = { name
: 'default', label
: mw
.msg( 'rcfilters-filterlist-title' ) };
284 filters
.forEach( function ( data
) {
288 if ( !model
.groups
[ group
] ) {
289 model
.groups
[ group
] = new mw
.rcfilters
.dm
.FilterGroup( group
, {
291 title
: data
.title
? mw
.msg( data
.title
) : group
,
292 separator
: data
.separator
,
293 fullCoverage
: !!data
.fullCoverage
,
295 body
: data
.whatsThisBody
,
296 header
: data
.whatsThisHeader
,
297 linkText
: data
.whatsThisLinkText
,
298 url
: data
.whatsThisUrl
303 // Filters are given to us with msg-keys, we need
304 // to translate those before we hand them off
305 for ( i
= 0; i
< data
.filters
.length
; i
++ ) {
306 data
.filters
[ i
].label
= data
.filters
[ i
].label
? mw
.msg( data
.filters
[ i
].label
) : data
.filters
[ i
].name
;
307 data
.filters
[ i
].description
= data
.filters
[ i
].description
? mw
.msg( data
.filters
[ i
].description
) : '';
310 model
.groups
[ group
].initializeFilters( data
.filters
, data
.default );
311 items
= items
.concat( model
.groups
[ group
].getItems() );
314 if ( data
.conflicts
) {
316 groupConflictMap
[ group
] = data
.conflicts
;
319 for ( i
= 0; i
< data
.filters
.length
; i
++ ) {
321 if ( data
.filters
[ i
].conflicts
) {
322 filterItem
= model
.groups
[ group
].getItemByParamName( data
.filters
[ i
].name
);
323 filterConflictMap
[ filterItem
.getName() ] = data
.filters
[ i
].conflicts
;
328 namespaces
= namespaces
|| {};
330 mw
.config
.get( 'wgStructuredChangeFiltersEnableExperimentalViews' ) &&
331 !$.isEmptyObject( namespaces
)
334 this.views
.namespaces
= { name
: 'namespaces', label
: mw
.msg( 'namespaces' ), trigger
: ':' };
335 $.each( namespaces
, function ( namespaceID
, label
) {
336 // Build and clean up the definition
337 namespaceDefinition
.push( {
339 label
: label
|| mw
.msg( 'blanknamespace' ),
342 ( namespaceID
< 0 || namespaceID
% 2 === 0 ) ?
345 cssClass
: 'mw-changeslist-ns-' + namespaceID
350 model
.groups
.namespace = new mw
.rcfilters
.dm
.FilterGroup(
351 'namespace', // Parameter name is singular
353 type
: 'string_options',
355 title
: 'namespaces', // Message key
357 labelPrefixKey
: { 'default': 'rcfilters-tag-prefix-namespace', inverted
: 'rcfilters-tag-prefix-namespace-inverted' },
361 // Add namespace items to group
362 model
.groups
.namespace.initializeFilters( namespaceDefinition
);
363 items
= items
.concat( model
.groups
.namespace.getItems() );
366 // Add item references to the model, for lookup
367 this.addItems( items
);
369 groupConflictResult
= expandConflictDefinitions( groupConflictMap
);
370 filterConflictResult
= expandConflictDefinitions( filterConflictMap
);
372 // Set conflicts for groups
373 $.each( groupConflictResult
, function ( group
, conflicts
) {
374 model
.groups
[ group
].setConflicts( conflicts
);
377 // Set conflicts for items
378 $.each( filterConflictResult
, function ( filterName
, conflicts
) {
379 var filterItem
= model
.getItemByName( filterName
);
380 // set conflicts for items in the group
381 filterItem
.setConflicts( conflicts
);
384 // Create a map between known parameters and their models
385 $.each( this.groups
, function ( group
, groupModel
) {
386 if ( groupModel
.getType() === 'send_unselected_if_any' ) {
387 // Individual filters
388 groupModel
.getItems().forEach( function ( filterItem
) {
389 model
.parameterMap
[ filterItem
.getParamName() ] = filterItem
;
391 } else if ( groupModel
.getType() === 'string_options' ) {
393 model
.parameterMap
[ groupModel
.getName() ] = groupModel
;
397 this.currentView
= 'default';
399 // Finish initialization
400 this.emit( 'initialize' );
404 * Get the names of all available filters
406 * @return {string[]} An array of filter names
408 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFilterNames = function () {
409 return this.getItems().map( function ( item
) { return item
.getName(); } );
413 * Get the object that defines groups by their name.
415 * @return {Object} Filter groups
417 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFilterGroups = function () {
422 * Get the object that defines groups that match a certain view by their name.
424 * @param {string} [view] Requested view. If not given, uses current view
425 * @return {Object} Filter groups matching a display group
427 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFilterGroupsByView = function ( view
) {
430 view
= view
|| this.getCurrentView();
432 $.each( this.groups
, function ( groupName
, groupModel
) {
433 if ( groupModel
.getView() === view
) {
434 result
[ groupName
] = groupModel
;
442 * Get an array of filters matching the given display group.
444 * @param {string} [view] Requested view. If not given, uses current view
445 * @return {mw.rcfilters.dm.FilterItem} Filter items matching the group
447 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFiltersByView = function ( view
) {
451 view
= view
|| this.getCurrentView();
453 groups
= this.getFilterGroupsByView( view
);
455 $.each( groups
, function ( groupName
, groupModel
) {
456 result
= result
.concat( groupModel
.getItems() );
463 * Get the trigger for the requested view.
465 * @param {string} view View name
466 * @return {string} View trigger, if exists
468 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getViewTrigger = function ( view
) {
469 return this.views
[ view
] && this.views
[ view
].trigger
;
472 * Get the value of a specific parameter
474 * @param {string} name Parameter name
475 * @return {number|string} Parameter value
477 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getParamValue = function ( name
) {
478 return this.parameters
[ name
];
482 * Get the current selected state of the filters
484 * @return {Object} Filters selected state
486 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getSelectedState = function () {
488 items
= this.getItems(),
491 for ( i
= 0; i
< items
.length
; i
++ ) {
492 result
[ items
[ i
].getName() ] = items
[ i
].isSelected();
499 * Get the current full state of the filters
501 * @return {Object} Filters full state
503 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFullState = function () {
505 items
= this.getItems(),
508 for ( i
= 0; i
< items
.length
; i
++ ) {
509 result
[ items
[ i
].getName() ] = {
510 selected
: items
[ i
].isSelected(),
511 conflicted
: items
[ i
].isConflicted(),
512 included
: items
[ i
].isIncluded()
520 * Get an object representing default parameters state
522 * @return {Object} Default parameter values
524 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getDefaultParams = function () {
527 // Get default filter state
528 $.each( this.groups
, function ( name
, model
) {
529 $.extend( true, result
, model
.getDefaultParams() );
536 * Analyze the groups and their filters and output an object representing
537 * the state of the parameters they represent.
539 * @param {Object} [filterDefinition] An object defining the filter values,
540 * keyed by filter names.
541 * @return {Object} Parameter state object
543 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getParametersFromFilters = function ( filterDefinition
) {
544 var groupItemDefinition
,
546 groupItems
= this.getFilterGroups();
548 if ( filterDefinition
) {
549 groupItemDefinition
= {};
550 // Filter definition is "flat", but in effect
551 // each group needs to tell us its result based
552 // on the values in it. We need to split this list
553 // back into groupings so we can "feed" it to the
554 // loop below, and we need to expand it so it includes
555 // all filters (set to false)
556 this.getItems().forEach( function ( filterItem
) {
557 groupItemDefinition
[ filterItem
.getGroupName() ] = groupItemDefinition
[ filterItem
.getGroupName() ] || {};
558 groupItemDefinition
[ filterItem
.getGroupName() ][ filterItem
.getName() ] = !!filterDefinition
[ filterItem
.getName() ];
562 $.each( groupItems
, function ( group
, model
) {
565 model
.getParamRepresentation(
566 groupItemDefinition
?
567 groupItemDefinition
[ group
] : null
576 * This is the opposite of the #getParametersFromFilters method; this goes over
577 * the given parameters and translates into a selected/unselected value in the filters.
579 * @param {Object} params Parameters query object
580 * @return {Object} Filter state object
582 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFiltersFromParameters = function ( params
) {
587 // Go over the given parameters, break apart to groupings
588 // The resulting object represents the group with its parameter
589 // values. For example:
596 // group2: "param4|param5"
598 $.each( params
, function ( paramName
, paramValue
) {
599 var itemOrGroup
= model
.parameterMap
[ paramName
];
601 if ( itemOrGroup
instanceof mw
.rcfilters
.dm
.FilterItem
) {
602 groupMap
[ itemOrGroup
.getGroupName() ] = groupMap
[ itemOrGroup
.getGroupName() ] || {};
603 groupMap
[ itemOrGroup
.getGroupName() ][ itemOrGroup
.getParamName() ] = paramValue
;
604 } else if ( itemOrGroup
instanceof mw
.rcfilters
.dm
.FilterGroup
) {
605 // This parameter represents a group (values are the filters)
606 // this is equivalent to checking if the group is 'string_options'
607 groupMap
[ itemOrGroup
.getName() ] = groupMap
[ itemOrGroup
.getName() ] || {};
608 groupMap
[ itemOrGroup
.getName() ] = paramValue
;
612 // Go over all groups, so we make sure we get the complete output
613 // even if the parameters don't include a certain group
614 $.each( this.groups
, function ( groupName
, groupModel
) {
615 result
= $.extend( true, {}, result
, groupModel
.getFilterRepresentation( groupMap
[ groupName
] ) );
622 * Get the highlight parameters based on current filter configuration
624 * @return {Object} Object where keys are "<filter name>_color" and values
625 * are the selected highlight colors.
627 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getHighlightParameters = function () {
630 this.getItems().forEach( function ( filterItem
) {
631 result
[ filterItem
.getName() + '_color' ] = filterItem
.getHighlightColor() || null;
633 result
.highlight
= String( Number( this.isHighlightEnabled() ) );
639 * Extract the highlight values from given object. Since highlights are
640 * the same for filter and parameters, it doesn't matter which one is
641 * given; values will be returned with a full list of the highlights
642 * with colors or null values.
644 * @param {Object} representation Object containing representation of
645 * some or all highlight values
646 * @return {Object} Object where keys are "<filter name>_color" and values
647 * are the selected highlight colors. The returned object
648 * contains all available filters either with a color value
651 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.extractHighlightValues = function ( representation
) {
654 this.getItems().forEach( function ( filterItem
) {
655 var highlightName
= filterItem
.getName() + '_color';
656 result
[ highlightName
] = representation
[ highlightName
] || null;
663 * Sanitize value group of a string_option groups type
664 * Remove duplicates and make sure to only use valid
668 * @param {string} groupName Group name
669 * @param {string[]} valueArray Array of values
670 * @return {string[]} Array of valid values
672 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.sanitizeStringOptionGroup = function ( groupName
, valueArray
) {
673 var validNames
= this.getGroupFilters( groupName
).map( function ( filterItem
) {
674 return filterItem
.getParamName();
677 return mw
.rcfilters
.utils
.normalizeParamOptions( valueArray
, validNames
);
681 * Check whether the current filter state is set to all false.
683 * @return {boolean} Current filters are all empty
685 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.areCurrentFiltersEmpty = function () {
686 // Check if there are either any selected items or any items
687 // that have highlight enabled
688 return !this.getItems().some( function ( filterItem
) {
689 return filterItem
.isSelected() || filterItem
.isHighlighted();
694 * Check whether the default values of the filters are all false.
696 * @return {boolean} Default filters are all false
698 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.areDefaultFiltersEmpty = function () {
701 if ( this.defaultFiltersEmpty
!== null ) {
702 // We only need to do this test once,
703 // because defaults are set once per session
704 defaultFilters
= this.getFiltersFromParameters( this.getDefaultParams() );
705 this.defaultFiltersEmpty
= Object
.keys( defaultFilters
).every( function ( filterName
) {
706 return !defaultFilters
[ filterName
];
710 return this.defaultFiltersEmpty
;
714 * Get the item that matches the given name
716 * @param {string} name Filter name
717 * @return {mw.rcfilters.dm.FilterItem} Filter item
719 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getItemByName = function ( name
) {
720 return this.getItems().filter( function ( item
) {
721 return name
=== item
.getName();
726 * Set all filters to false or empty/all
727 * This is equivalent to display all.
729 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.emptyAllFilters = function () {
730 this.getItems().forEach( function ( filterItem
) {
731 this.toggleFilterSelected( filterItem
.getName(), false );
736 * Toggle selected state of one item
738 * @param {string} name Name of the filter item
739 * @param {boolean} [isSelected] Filter selected state
741 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.toggleFilterSelected = function ( name
, isSelected
) {
742 var item
= this.getItemByName( name
);
745 item
.toggleSelected( isSelected
);
750 * Toggle selected state of items by their names
752 * @param {Object} filterDef Filter definitions
754 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.toggleFiltersSelected = function ( filterDef
) {
755 Object
.keys( filterDef
).forEach( function ( name
) {
756 this.toggleFilterSelected( name
, filterDef
[ name
] );
761 * Get a group model from its name
763 * @param {string} groupName Group name
764 * @return {mw.rcfilters.dm.FilterGroup} Group model
766 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getGroup = function ( groupName
) {
767 return this.groups
[ groupName
];
771 * Get all filters within a specified group by its name
773 * @param {string} groupName Group name
774 * @return {mw.rcfilters.dm.FilterItem[]} Filters belonging to this group
776 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getGroupFilters = function ( groupName
) {
777 return ( this.getGroup( groupName
) && this.getGroup( groupName
).getItems() ) || [];
781 * Find items whose labels match the given string
783 * @param {string} query Search string
784 * @param {boolean} [returnFlat] Return a flat array. If false, the result
785 * is an object whose keys are the group names and values are an array of
786 * filters per group. If set to true, returns an array of filters regardless
788 * @return {Object} An object of items to show
789 * arranged by their group names
791 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.findMatches = function ( query
, returnFlat
) {
792 var i
, searchIsEmpty
,
796 view
= query
.indexOf( this.getViewTrigger( 'namespaces' ) ) === 0 ? 'namespaces' : 'default',
797 items
= this.getFiltersByView( view
);
799 // Normalize so we can search strings regardless of case and view
800 query
= query
.toLowerCase();
801 if ( view
=== 'namespaces' ) {
802 query
= query
.substr( 1 );
805 // Check if the search if actually empty; this can be a problem when
806 // we use prefixes to denote different views
807 searchIsEmpty
= query
.length
=== 0;
809 // item label starting with the query string
810 for ( i
= 0; i
< items
.length
; i
++ ) {
813 items
[ i
].getLabel().toLowerCase().indexOf( query
) === 0
815 result
[ items
[ i
].getGroupName() ] = result
[ items
[ i
].getGroupName() ] || [];
816 result
[ items
[ i
].getGroupName() ].push( items
[ i
] );
817 flatResult
.push( items
[ i
] );
821 if ( $.isEmptyObject( result
) ) {
822 // item containing the query string in their label, description, or group title
823 for ( i
= 0; i
< items
.length
; i
++ ) {
824 groupTitle
= items
[ i
].getGroupModel().getTitle();
827 items
[ i
].getLabel().toLowerCase().indexOf( query
) > -1 ||
828 items
[ i
].getDescription().toLowerCase().indexOf( query
) > -1 ||
829 groupTitle
.toLowerCase().indexOf( query
) > -1
831 result
[ items
[ i
].getGroupName() ] = result
[ items
[ i
].getGroupName() ] || [];
832 result
[ items
[ i
].getGroupName() ].push( items
[ i
] );
833 flatResult
.push( items
[ i
] );
838 return returnFlat
? flatResult
: result
;
842 * Get items that are highlighted
844 * @return {mw.rcfilters.dm.FilterItem[]} Highlighted items
846 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getHighlightedItems = function () {
847 return this.getItems().filter( function ( filterItem
) {
848 return filterItem
.isHighlightSupported() &&
849 filterItem
.getHighlightColor();
854 * Get items that allow highlights even if they're not currently highlighted
856 * @return {mw.rcfilters.dm.FilterItem[]} Items supporting highlights
858 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getItemsSupportingHighlights = function () {
859 return this.getItems().filter( function ( filterItem
) {
860 return filterItem
.isHighlightSupported();
865 * Switch the current view
867 * @param {string} view View name
870 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.switchView = function ( view
) {
871 if ( this.views
[ view
] && this.currentView
!== view
) {
872 this.currentView
= view
;
873 this.emit( 'update' );
878 * Get the current view
880 * @return {string} Current view
882 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getCurrentView = function () {
883 return this.currentView
;
887 * Get the label for the current view
889 * @return {string} Label for the current view
891 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getCurrentViewLabel = function () {
892 return this.views
[ this.getCurrentView() ].label
;
896 * Toggle the highlight feature on and off.
897 * Propagate the change to filter items.
899 * @param {boolean} enable Highlight should be enabled
900 * @fires highlightChange
902 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.toggleHighlight = function ( enable
) {
903 enable
= enable
=== undefined ? !this.highlightEnabled
: enable
;
905 if ( this.highlightEnabled
!== enable
) {
906 this.highlightEnabled
= enable
;
908 this.getItems().forEach( function ( filterItem
) {
909 filterItem
.toggleHighlight( this.highlightEnabled
);
912 this.emit( 'highlightChange', this.highlightEnabled
);
917 * Check if the highlight feature is enabled
920 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.isHighlightEnabled = function () {
921 return !!this.highlightEnabled
;
925 * Toggle the inverted namespaces property on and off.
926 * Propagate the change to namespace filter items.
928 * @param {boolean} enable Inverted property is enabled
929 * @fires invertChange
931 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.toggleInvertedNamespaces = function ( enable
) {
932 enable
= enable
=== undefined ? !this.invertedNamespaces
: enable
;
934 if ( this.invertedNamespaces
!== enable
) {
935 this.invertedNamespaces
= enable
;
937 this.getFiltersByView( 'namespaces' ).forEach( function ( filterItem
) {
938 filterItem
.toggleInverted( this.invertedNamespaces
);
941 this.emit( 'invertChange', this.invertedNamespaces
);
946 * Check if the namespaces selection is set to be inverted
949 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.areNamespacesInverted = function () {
950 return !!this.invertedNamespaces
;
954 * Set highlight color for a specific filter item
956 * @param {string} filterName Name of the filter item
957 * @param {string} color Selected color
959 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.setHighlightColor = function ( filterName
, color
) {
960 this.getItemByName( filterName
).setHighlightColor( color
);
964 * Clear highlight for a specific filter item
966 * @param {string} filterName Name of the filter item
968 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.clearHighlightColor = function ( filterName
) {
969 this.getItemByName( filterName
).clearHighlightColor();
973 * Clear highlight for all filter items
975 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.clearAllHighlightColors = function () {
976 this.getItems().forEach( function ( filterItem
) {
977 filterItem
.clearHighlightColor();
980 }( mediaWiki
, jQuery
) );