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
= 'default';
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 Filters definition
210 * @param {Object} [views] Extra views definition
211 * Expected in the following format:
214 * label: 'namespaces', // Message key
219 * name: 'namespaces' // Parameter name
221 * title: 'namespaces' // Message key
222 * type: 'string_options',
224 * labelPrefixKey: { 'default': 'rcfilters-tag-prefix-namespace', inverted: 'rcfilters-tag-prefix-namespace-inverted' },
233 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.initializeFilters = function ( filters
, views
) {
234 var filterItem
, filterConflictResult
, groupConflictResult
,
237 groupConflictMap
= {},
238 filterConflictMap
= {},
240 * Expand a conflict definition from group name to
241 * the list of all included filters in that group.
242 * We do this so that the direct relationship in the
243 * models are consistently item->items rather than
244 * mixing item->group with item->item.
246 * @param {Object} obj Conflict definition
247 * @return {Object} Expanded conflict definition
249 expandConflictDefinitions = function ( obj
) {
252 $.each( obj
, function ( key
, conflicts
) {
254 adjustedConflicts
= {};
256 conflicts
.forEach( function ( conflict
) {
259 if ( conflict
.filter
) {
260 filterName
= model
.groups
[ conflict
.group
].getPrefixedName( conflict
.filter
);
261 filter
= model
.getItemByName( filterName
);
264 adjustedConflicts
[ filterName
] = $.extend(
273 // This conflict is for an entire group. Split it up to
274 // represent each filter
276 // Get the relevant group items
277 model
.groups
[ conflict
.group
].getItems().forEach( function ( groupItem
) {
278 // Rebuild the conflict
279 adjustedConflicts
[ groupItem
.getName() ] = $.extend(
283 filter
: groupItem
.getName(),
291 result
[ key
] = adjustedConflicts
;
305 this.views
.default = { name
: 'default', label
: mw
.msg( 'rcfilters-filterlist-title' ) };
306 filters
.forEach( function ( data
) {
310 if ( !model
.groups
[ group
] ) {
311 model
.groups
[ group
] = new mw
.rcfilters
.dm
.FilterGroup( group
, {
313 title
: data
.title
? mw
.msg( data
.title
) : group
,
314 separator
: data
.separator
,
315 fullCoverage
: !!data
.fullCoverage
,
317 body
: data
.whatsThisBody
,
318 header
: data
.whatsThisHeader
,
319 linkText
: data
.whatsThisLinkText
,
320 url
: data
.whatsThisUrl
325 // Filters are given to us with msg-keys, we need
326 // to translate those before we hand them off
327 for ( i
= 0; i
< data
.filters
.length
; i
++ ) {
328 data
.filters
[ i
].label
= data
.filters
[ i
].label
? mw
.msg( data
.filters
[ i
].label
) : data
.filters
[ i
].name
;
329 data
.filters
[ i
].description
= data
.filters
[ i
].description
? mw
.msg( data
.filters
[ i
].description
) : '';
332 model
.groups
[ group
].initializeFilters( data
.filters
, data
.default );
333 items
= items
.concat( model
.groups
[ group
].getItems() );
336 if ( data
.conflicts
) {
338 groupConflictMap
[ group
] = data
.conflicts
;
341 for ( i
= 0; i
< data
.filters
.length
; i
++ ) {
343 if ( data
.filters
[ i
].conflicts
) {
344 filterItem
= model
.groups
[ group
].getItemByParamName( data
.filters
[ i
].name
);
345 filterConflictMap
[ filterItem
.getName() ] = data
.filters
[ i
].conflicts
;
350 if ( mw
.config
.get( 'wgStructuredChangeFiltersEnableExperimentalViews' ) ) {
351 $.each( views
, function ( viewName
, viewData
) {
352 model
.views
[ viewName
] = {
354 title
: viewData
.title
,
355 trigger
: viewData
.trigger
359 viewData
.groups
.forEach( function ( groupData
) {
360 model
.groups
[ groupData
.name
] = new mw
.rcfilters
.dm
.FilterGroup(
362 $.extend( true, {}, groupData
.definition
, { view
: viewName
} )
366 model
.groups
[ groupData
.name
].initializeFilters( groupData
.items
);
368 // Add to global search list
369 items
= items
.concat( model
.groups
[ groupData
.name
].getItems() );
374 // Add item references to the model, for lookup
375 this.addItems( items
);
378 groupConflictResult
= expandConflictDefinitions( groupConflictMap
);
379 filterConflictResult
= expandConflictDefinitions( filterConflictMap
);
381 // Set conflicts for groups
382 $.each( groupConflictResult
, function ( group
, conflicts
) {
383 model
.groups
[ group
].setConflicts( conflicts
);
386 // Set conflicts for items
387 $.each( filterConflictResult
, function ( filterName
, conflicts
) {
388 var filterItem
= model
.getItemByName( filterName
);
389 // set conflicts for items in the group
390 filterItem
.setConflicts( conflicts
);
393 // Create a map between known parameters and their models
394 $.each( this.groups
, function ( group
, groupModel
) {
395 if ( groupModel
.getType() === 'send_unselected_if_any' ) {
396 // Individual filters
397 groupModel
.getItems().forEach( function ( filterItem
) {
398 model
.parameterMap
[ filterItem
.getParamName() ] = filterItem
;
400 } else if ( groupModel
.getType() === 'string_options' ) {
402 model
.parameterMap
[ groupModel
.getName() ] = groupModel
;
406 this.currentView
= 'default';
408 // Finish initialization
409 this.emit( 'initialize' );
413 * Get the names of all available filters
415 * @return {string[]} An array of filter names
417 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFilterNames = function () {
418 return this.getItems().map( function ( item
) { return item
.getName(); } );
422 * Get the object that defines groups by their name.
424 * @return {Object} Filter groups
426 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFilterGroups = function () {
431 * Get the object that defines groups that match a certain view by their name.
433 * @param {string} [view] Requested view. If not given, uses current view
434 * @return {Object} Filter groups matching a display group
436 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFilterGroupsByView = function ( view
) {
439 view
= view
|| this.getCurrentView();
441 $.each( this.groups
, function ( groupName
, groupModel
) {
442 if ( groupModel
.getView() === view
) {
443 result
[ groupName
] = groupModel
;
451 * Get an array of filters matching the given display group.
453 * @param {string} [view] Requested view. If not given, uses current view
454 * @return {mw.rcfilters.dm.FilterItem} Filter items matching the group
456 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFiltersByView = function ( view
) {
460 view
= view
|| this.getCurrentView();
462 groups
= this.getFilterGroupsByView( view
);
464 $.each( groups
, function ( groupName
, groupModel
) {
465 result
= result
.concat( groupModel
.getItems() );
472 * Get the trigger for the requested view.
474 * @param {string} view View name
475 * @return {string} View trigger, if exists
477 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getViewTrigger = function ( view
) {
478 return ( this.views
[ view
] && this.views
[ view
].trigger
) || '';
481 * Get the value of a specific parameter
483 * @param {string} name Parameter name
484 * @return {number|string} Parameter value
486 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getParamValue = function ( name
) {
487 return this.parameters
[ name
];
491 * Get the current selected state of the filters
493 * @return {Object} Filters selected state
495 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getSelectedState = function () {
497 items
= this.getItems(),
500 for ( i
= 0; i
< items
.length
; i
++ ) {
501 result
[ items
[ i
].getName() ] = items
[ i
].isSelected();
508 * Get the current full state of the filters
510 * @return {Object} Filters full state
512 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFullState = function () {
514 items
= this.getItems(),
517 for ( i
= 0; i
< items
.length
; i
++ ) {
518 result
[ items
[ i
].getName() ] = {
519 selected
: items
[ i
].isSelected(),
520 conflicted
: items
[ i
].isConflicted(),
521 included
: items
[ i
].isIncluded()
529 * Get an object representing default parameters state
531 * @return {Object} Default parameter values
533 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getDefaultParams = function () {
536 // Get default filter state
537 $.each( this.groups
, function ( name
, model
) {
538 $.extend( true, result
, model
.getDefaultParams() );
545 * Analyze the groups and their filters and output an object representing
546 * the state of the parameters they represent.
548 * @param {Object} [filterDefinition] An object defining the filter values,
549 * keyed by filter names.
550 * @return {Object} Parameter state object
552 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getParametersFromFilters = function ( filterDefinition
) {
553 var groupItemDefinition
,
555 groupItems
= this.getFilterGroups();
557 if ( filterDefinition
) {
558 groupItemDefinition
= {};
559 // Filter definition is "flat", but in effect
560 // each group needs to tell us its result based
561 // on the values in it. We need to split this list
562 // back into groupings so we can "feed" it to the
563 // loop below, and we need to expand it so it includes
564 // all filters (set to false)
565 this.getItems().forEach( function ( filterItem
) {
566 groupItemDefinition
[ filterItem
.getGroupName() ] = groupItemDefinition
[ filterItem
.getGroupName() ] || {};
567 groupItemDefinition
[ filterItem
.getGroupName() ][ filterItem
.getName() ] = !!filterDefinition
[ filterItem
.getName() ];
571 $.each( groupItems
, function ( group
, model
) {
574 model
.getParamRepresentation(
575 groupItemDefinition
?
576 groupItemDefinition
[ group
] : null
585 * This is the opposite of the #getParametersFromFilters method; this goes over
586 * the given parameters and translates into a selected/unselected value in the filters.
588 * @param {Object} params Parameters query object
589 * @return {Object} Filter state object
591 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFiltersFromParameters = function ( params
) {
596 // Go over the given parameters, break apart to groupings
597 // The resulting object represents the group with its parameter
598 // values. For example:
605 // group2: "param4|param5"
607 $.each( params
, function ( paramName
, paramValue
) {
608 var itemOrGroup
= model
.parameterMap
[ paramName
];
610 if ( itemOrGroup
instanceof mw
.rcfilters
.dm
.FilterItem
) {
611 groupMap
[ itemOrGroup
.getGroupName() ] = groupMap
[ itemOrGroup
.getGroupName() ] || {};
612 groupMap
[ itemOrGroup
.getGroupName() ][ itemOrGroup
.getParamName() ] = paramValue
;
613 } else if ( itemOrGroup
instanceof mw
.rcfilters
.dm
.FilterGroup
) {
614 // This parameter represents a group (values are the filters)
615 // this is equivalent to checking if the group is 'string_options'
616 groupMap
[ itemOrGroup
.getName() ] = groupMap
[ itemOrGroup
.getName() ] || {};
617 groupMap
[ itemOrGroup
.getName() ] = paramValue
;
621 // Go over all groups, so we make sure we get the complete output
622 // even if the parameters don't include a certain group
623 $.each( this.groups
, function ( groupName
, groupModel
) {
624 result
= $.extend( true, {}, result
, groupModel
.getFilterRepresentation( groupMap
[ groupName
] ) );
631 * Get the highlight parameters based on current filter configuration
633 * @return {Object} Object where keys are "<filter name>_color" and values
634 * are the selected highlight colors.
636 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getHighlightParameters = function () {
639 this.getItems().forEach( function ( filterItem
) {
640 result
[ filterItem
.getName() + '_color' ] = filterItem
.getHighlightColor() || null;
642 result
.highlight
= String( Number( this.isHighlightEnabled() ) );
648 * Extract the highlight values from given object. Since highlights are
649 * the same for filter and parameters, it doesn't matter which one is
650 * given; values will be returned with a full list of the highlights
651 * with colors or null values.
653 * @param {Object} representation Object containing representation of
654 * some or all highlight values
655 * @return {Object} Object where keys are "<filter name>_color" and values
656 * are the selected highlight colors. The returned object
657 * contains all available filters either with a color value
660 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.extractHighlightValues = function ( representation
) {
663 this.getItems().forEach( function ( filterItem
) {
664 var highlightName
= filterItem
.getName() + '_color';
665 result
[ highlightName
] = representation
[ highlightName
] || null;
672 * Sanitize value group of a string_option groups type
673 * Remove duplicates and make sure to only use valid
677 * @param {string} groupName Group name
678 * @param {string[]} valueArray Array of values
679 * @return {string[]} Array of valid values
681 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.sanitizeStringOptionGroup = function ( groupName
, valueArray
) {
682 var validNames
= this.getGroupFilters( groupName
).map( function ( filterItem
) {
683 return filterItem
.getParamName();
686 return mw
.rcfilters
.utils
.normalizeParamOptions( valueArray
, validNames
);
690 * Check whether the current filter state is set to all false.
692 * @return {boolean} Current filters are all empty
694 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.areCurrentFiltersEmpty = function () {
695 // Check if there are either any selected items or any items
696 // that have highlight enabled
697 return !this.getItems().some( function ( filterItem
) {
698 return filterItem
.isSelected() || filterItem
.isHighlighted();
703 * Check whether the default values of the filters are all false.
705 * @return {boolean} Default filters are all false
707 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.areDefaultFiltersEmpty = function () {
710 if ( this.defaultFiltersEmpty
!== null ) {
711 // We only need to do this test once,
712 // because defaults are set once per session
713 defaultFilters
= this.getFiltersFromParameters( this.getDefaultParams() );
714 this.defaultFiltersEmpty
= Object
.keys( defaultFilters
).every( function ( filterName
) {
715 return !defaultFilters
[ filterName
];
719 return this.defaultFiltersEmpty
;
723 * Get the item that matches the given name
725 * @param {string} name Filter name
726 * @return {mw.rcfilters.dm.FilterItem} Filter item
728 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getItemByName = function ( name
) {
729 return this.getItems().filter( function ( item
) {
730 return name
=== item
.getName();
735 * Set all filters to false or empty/all
736 * This is equivalent to display all.
738 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.emptyAllFilters = function () {
739 this.getItems().forEach( function ( filterItem
) {
740 this.toggleFilterSelected( filterItem
.getName(), false );
745 * Toggle selected state of one item
747 * @param {string} name Name of the filter item
748 * @param {boolean} [isSelected] Filter selected state
750 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.toggleFilterSelected = function ( name
, isSelected
) {
751 var item
= this.getItemByName( name
);
754 item
.toggleSelected( isSelected
);
759 * Toggle selected state of items by their names
761 * @param {Object} filterDef Filter definitions
763 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.toggleFiltersSelected = function ( filterDef
) {
764 Object
.keys( filterDef
).forEach( function ( name
) {
765 this.toggleFilterSelected( name
, filterDef
[ name
] );
770 * Get a group model from its name
772 * @param {string} groupName Group name
773 * @return {mw.rcfilters.dm.FilterGroup} Group model
775 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getGroup = function ( groupName
) {
776 return this.groups
[ groupName
];
780 * Get all filters within a specified group by its name
782 * @param {string} groupName Group name
783 * @return {mw.rcfilters.dm.FilterItem[]} Filters belonging to this group
785 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getGroupFilters = function ( groupName
) {
786 return ( this.getGroup( groupName
) && this.getGroup( groupName
).getItems() ) || [];
790 * Find items whose labels match the given string
792 * @param {string} query Search string
793 * @param {boolean} [returnFlat] Return a flat array. If false, the result
794 * is an object whose keys are the group names and values are an array of
795 * filters per group. If set to true, returns an array of filters regardless
797 * @return {Object} An object of items to show
798 * arranged by their group names
800 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.findMatches = function ( query
, returnFlat
) {
801 var i
, searchIsEmpty
,
805 view
= this.getViewByTrigger( query
.substr( 0, 1 ) ),
806 items
= this.getFiltersByView( view
);
808 // Normalize so we can search strings regardless of case and view
809 query
= query
.toLowerCase();
810 if ( view
!== 'default' ) {
811 query
= query
.substr( 1 );
814 // Check if the search if actually empty; this can be a problem when
815 // we use prefixes to denote different views
816 searchIsEmpty
= query
.length
=== 0;
818 // item label starting with the query string
819 for ( i
= 0; i
< items
.length
; i
++ ) {
822 items
[ i
].getLabel().toLowerCase().indexOf( query
) === 0 ||
824 // For tags, we want the parameter name to be included in the search
826 items
[ i
].getParamName().toLowerCase().indexOf( query
) > -1
829 result
[ items
[ i
].getGroupName() ] = result
[ items
[ i
].getGroupName() ] || [];
830 result
[ items
[ i
].getGroupName() ].push( items
[ i
] );
831 flatResult
.push( items
[ i
] );
835 if ( $.isEmptyObject( result
) ) {
836 // item containing the query string in their label, description, or group title
837 for ( i
= 0; i
< items
.length
; i
++ ) {
838 groupTitle
= items
[ i
].getGroupModel().getTitle();
841 items
[ i
].getLabel().toLowerCase().indexOf( query
) > -1 ||
842 items
[ i
].getDescription().toLowerCase().indexOf( query
) > -1 ||
843 groupTitle
.toLowerCase().indexOf( query
) > -1 ||
845 // For tags, we want the parameter name to be included in the search
847 items
[ i
].getParamName().toLowerCase().indexOf( query
) > -1
850 result
[ items
[ i
].getGroupName() ] = result
[ items
[ i
].getGroupName() ] || [];
851 result
[ items
[ i
].getGroupName() ].push( items
[ i
] );
852 flatResult
.push( items
[ i
] );
857 return returnFlat
? flatResult
: result
;
861 * Get items that are highlighted
863 * @return {mw.rcfilters.dm.FilterItem[]} Highlighted items
865 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getHighlightedItems = function () {
866 return this.getItems().filter( function ( filterItem
) {
867 return filterItem
.isHighlightSupported() &&
868 filterItem
.getHighlightColor();
873 * Get items that allow highlights even if they're not currently highlighted
875 * @return {mw.rcfilters.dm.FilterItem[]} Items supporting highlights
877 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getItemsSupportingHighlights = function () {
878 return this.getItems().filter( function ( filterItem
) {
879 return filterItem
.isHighlightSupported();
884 * Switch the current view
886 * @param {string} view View name
889 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.switchView = function ( view
) {
890 if ( this.views
[ view
] && this.currentView
!== view
) {
891 this.currentView
= view
;
892 this.emit( 'update' );
897 * Get the current view
899 * @return {string} Current view
901 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getCurrentView = function () {
902 return this.currentView
;
906 * Get the label for the current view
908 * @return {string} Label for the current view
910 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getCurrentViewLabel = function () {
911 return this.views
[ this.getCurrentView() ].title
;
915 * Get an array of all available view names
917 * @return {string} Available view names
919 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getAvailableViews = function () {
920 return Object
.keys( this.views
);
924 * Get the view that fits the given trigger
926 * @param {string} trigger Trigger
927 * @return {string} Name of view
929 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getViewByTrigger = function ( trigger
) {
930 var result
= 'default';
932 $.each( this.views
, function ( name
, data
) {
933 if ( data
.trigger
=== trigger
) {
942 * Toggle the highlight feature on and off.
943 * Propagate the change to filter items.
945 * @param {boolean} enable Highlight should be enabled
946 * @fires highlightChange
948 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.toggleHighlight = function ( enable
) {
949 enable
= enable
=== undefined ? !this.highlightEnabled
: enable
;
951 if ( this.highlightEnabled
!== enable
) {
952 this.highlightEnabled
= enable
;
954 this.getItems().forEach( function ( filterItem
) {
955 filterItem
.toggleHighlight( this.highlightEnabled
);
958 this.emit( 'highlightChange', this.highlightEnabled
);
963 * Check if the highlight feature is enabled
966 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.isHighlightEnabled = function () {
967 return !!this.highlightEnabled
;
971 * Toggle the inverted namespaces property on and off.
972 * Propagate the change to namespace filter items.
974 * @param {boolean} enable Inverted property is enabled
975 * @fires invertChange
977 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.toggleInvertedNamespaces = function ( enable
) {
978 enable
= enable
=== undefined ? !this.invertedNamespaces
: enable
;
980 if ( this.invertedNamespaces
!== enable
) {
981 this.invertedNamespaces
= enable
;
983 this.getFiltersByView( 'namespaces' ).forEach( function ( filterItem
) {
984 filterItem
.toggleInverted( this.invertedNamespaces
);
987 this.emit( 'invertChange', this.invertedNamespaces
);
992 * Check if the namespaces selection is set to be inverted
995 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.areNamespacesInverted = function () {
996 return !!this.invertedNamespaces
;
1000 * Set highlight color for a specific filter item
1002 * @param {string} filterName Name of the filter item
1003 * @param {string} color Selected color
1005 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.setHighlightColor = function ( filterName
, color
) {
1006 this.getItemByName( filterName
).setHighlightColor( color
);
1010 * Clear highlight for a specific filter item
1012 * @param {string} filterName Name of the filter item
1014 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.clearHighlightColor = function ( filterName
) {
1015 this.getItemByName( filterName
).clearHighlightColor();
1019 * Clear highlight for all filter items
1021 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.clearAllHighlightColors = function () {
1022 this.getItems().forEach( function ( filterItem
) {
1023 filterItem
.clearHighlightColor();
1028 * Return a version of the given string that is without any
1031 * @param {string} str Given string
1032 * @return {string} Result
1034 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.removeViewTriggers = function ( str
) {
1035 if ( this.getViewByTrigger( str
.substr( 0, 1 ) ) !== 'default' ) {
1036 str
= str
.substr( 1 );
1041 }( mediaWiki
, jQuery
) );