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} filterGroups 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
220 * title: 'namespaces' // Message key
221 * type: 'string_options',
223 * labelPrefixKey: { 'default': 'rcfilters-tag-prefix-namespace', inverted: 'rcfilters-tag-prefix-namespace-inverted' },
231 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.initializeFilters = function ( filterGroups
, views
) {
232 var filterConflictResult
, groupConflictResult
,
236 groupConflictMap
= {},
237 filterConflictMap
= {},
239 * Expand a conflict definition from group name to
240 * the list of all included filters in that group.
241 * We do this so that the direct relationship in the
242 * models are consistently item->items rather than
243 * mixing item->group with item->item.
245 * @param {Object} obj Conflict definition
246 * @return {Object} Expanded conflict definition
248 expandConflictDefinitions = function ( obj
) {
251 $.each( obj
, function ( key
, conflicts
) {
253 adjustedConflicts
= {};
255 conflicts
.forEach( function ( conflict
) {
258 if ( conflict
.filter
) {
259 filterName
= model
.groups
[ conflict
.group
].getPrefixedName( conflict
.filter
);
260 filter
= model
.getItemByName( filterName
);
263 adjustedConflicts
[ filterName
] = $.extend(
272 // This conflict is for an entire group. Split it up to
273 // represent each filter
275 // Get the relevant group items
276 model
.groups
[ conflict
.group
].getItems().forEach( function ( groupItem
) {
277 // Rebuild the conflict
278 adjustedConflicts
[ groupItem
.getName() ] = $.extend(
282 filter
: groupItem
.getName(),
290 result
[ key
] = adjustedConflicts
;
302 filterGroups
= OO
.copy( filterGroups
);
304 // Normalize definition from the server
305 filterGroups
.forEach( function ( data
) {
307 // What's this information needs to be normalized
309 body
: data
.whatsThisBody
,
310 header
: data
.whatsThisHeader
,
311 linkText
: data
.whatsThisLinkText
,
312 url
: data
.whatsThisUrl
315 // Title is a msg-key
316 data
.title
= data
.title
? mw
.msg( data
.title
) : data
.name
;
318 // Filters are given to us with msg-keys, we need
319 // to translate those before we hand them off
320 for ( i
= 0; i
< data
.filters
.length
; i
++ ) {
321 data
.filters
[ i
].label
= data
.filters
[ i
].label
? mw
.msg( data
.filters
[ i
].label
) : data
.filters
[ i
].name
;
322 data
.filters
[ i
].description
= data
.filters
[ i
].description
? mw
.msg( data
.filters
[ i
].description
) : '';
329 title
: mw
.msg( 'rcfilters-filterlist-title' ),
334 if ( views
&& mw
.config
.get( 'wgStructuredChangeFiltersEnableExperimentalViews' ) ) {
335 // If we have extended views, add them in
336 $.extend( true, allViews
, views
);
340 $.each( allViews
, function ( viewName
, viewData
) {
342 model
.views
[ viewName
] = {
344 title
: viewData
.title
,
345 trigger
: viewData
.trigger
349 viewData
.groups
.forEach( function ( groupData
) {
350 var group
= groupData
.name
;
352 if ( !model
.groups
[ group
] ) {
353 model
.groups
[ group
] = new mw
.rcfilters
.dm
.FilterGroup(
355 $.extend( true, {}, groupData
, { view
: viewName
} )
359 model
.groups
[ group
].initializeFilters( groupData
.filters
, groupData
.default );
360 items
= items
.concat( model
.groups
[ group
].getItems() );
363 if ( groupData
.conflicts
) {
365 groupConflictMap
[ group
] = groupData
.conflicts
;
368 groupData
.filters
.forEach( function ( itemData
) {
369 var filterItem
= model
.groups
[ group
].getItemByParamName( itemData
.name
);
371 if ( itemData
.conflicts
) {
372 filterConflictMap
[ filterItem
.getName() ] = itemData
.conflicts
;
378 // Add item references to the model, for lookup
379 this.addItems( items
);
382 groupConflictResult
= expandConflictDefinitions( groupConflictMap
);
383 filterConflictResult
= expandConflictDefinitions( filterConflictMap
);
385 // Set conflicts for groups
386 $.each( groupConflictResult
, function ( group
, conflicts
) {
387 model
.groups
[ group
].setConflicts( conflicts
);
390 // Set conflicts for items
391 $.each( filterConflictResult
, function ( filterName
, conflicts
) {
392 var filterItem
= model
.getItemByName( filterName
);
393 // set conflicts for items in the group
394 filterItem
.setConflicts( conflicts
);
397 // Create a map between known parameters and their models
398 $.each( this.groups
, function ( group
, groupModel
) {
399 if ( groupModel
.getType() === 'send_unselected_if_any' ) {
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.currentView
= 'default';
415 // Finish initialization
416 this.emit( 'initialize' );
420 * Get the names of all available filters
422 * @return {string[]} An array of filter names
424 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFilterNames = function () {
425 return this.getItems().map( function ( item
) { return item
.getName(); } );
429 * Get the object that defines groups by their name.
431 * @return {Object} Filter groups
433 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFilterGroups = function () {
438 * Get the object that defines groups that match a certain view by their name.
440 * @param {string} [view] Requested view. If not given, uses current view
441 * @return {Object} Filter groups matching a display group
443 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFilterGroupsByView = function ( view
) {
446 view
= view
|| this.getCurrentView();
448 $.each( this.groups
, function ( groupName
, groupModel
) {
449 if ( groupModel
.getView() === view
) {
450 result
[ groupName
] = groupModel
;
458 * Get an array of filters matching the given display group.
460 * @param {string} [view] Requested view. If not given, uses current view
461 * @return {mw.rcfilters.dm.FilterItem} Filter items matching the group
463 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFiltersByView = function ( view
) {
467 view
= view
|| this.getCurrentView();
469 groups
= this.getFilterGroupsByView( view
);
471 $.each( groups
, function ( groupName
, groupModel
) {
472 result
= result
.concat( groupModel
.getItems() );
479 * Get the trigger for the requested view.
481 * @param {string} view View name
482 * @return {string} View trigger, if exists
484 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getViewTrigger = function ( view
) {
485 return ( this.views
[ view
] && this.views
[ view
].trigger
) || '';
488 * Get the value of a specific parameter
490 * @param {string} name Parameter name
491 * @return {number|string} Parameter value
493 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getParamValue = function ( name
) {
494 return this.parameters
[ name
];
498 * Get the current selected state of the filters
500 * @return {Object} Filters selected state
502 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getSelectedState = function () {
504 items
= this.getItems(),
507 for ( i
= 0; i
< items
.length
; i
++ ) {
508 result
[ items
[ i
].getName() ] = items
[ i
].isSelected();
515 * Get the current full state of the filters
517 * @return {Object} Filters full state
519 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFullState = function () {
521 items
= this.getItems(),
524 for ( i
= 0; i
< items
.length
; i
++ ) {
525 result
[ items
[ i
].getName() ] = {
526 selected
: items
[ i
].isSelected(),
527 conflicted
: items
[ i
].isConflicted(),
528 included
: items
[ i
].isIncluded()
536 * Get an object representing default parameters state
538 * @return {Object} Default parameter values
540 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getDefaultParams = function () {
543 // Get default filter state
544 $.each( this.groups
, function ( name
, model
) {
545 $.extend( true, result
, model
.getDefaultParams() );
552 * Analyze the groups and their filters and output an object representing
553 * the state of the parameters they represent.
555 * @param {Object} [filterDefinition] An object defining the filter values,
556 * keyed by filter names.
557 * @return {Object} Parameter state object
559 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getParametersFromFilters = function ( filterDefinition
) {
560 var groupItemDefinition
,
562 groupItems
= this.getFilterGroups();
564 if ( filterDefinition
) {
565 groupItemDefinition
= {};
566 // Filter definition is "flat", but in effect
567 // each group needs to tell us its result based
568 // on the values in it. We need to split this list
569 // back into groupings so we can "feed" it to the
570 // loop below, and we need to expand it so it includes
571 // all filters (set to false)
572 this.getItems().forEach( function ( filterItem
) {
573 groupItemDefinition
[ filterItem
.getGroupName() ] = groupItemDefinition
[ filterItem
.getGroupName() ] || {};
574 groupItemDefinition
[ filterItem
.getGroupName() ][ filterItem
.getName() ] = !!filterDefinition
[ filterItem
.getName() ];
578 $.each( groupItems
, function ( group
, model
) {
581 model
.getParamRepresentation(
582 groupItemDefinition
?
583 groupItemDefinition
[ group
] : null
592 * This is the opposite of the #getParametersFromFilters method; this goes over
593 * the given parameters and translates into a selected/unselected value in the filters.
595 * @param {Object} params Parameters query object
596 * @return {Object} Filter state object
598 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFiltersFromParameters = function ( params
) {
603 // Go over the given parameters, break apart to groupings
604 // The resulting object represents the group with its parameter
605 // values. For example:
612 // group2: "param4|param5"
614 $.each( params
, function ( paramName
, paramValue
) {
615 var itemOrGroup
= model
.parameterMap
[ paramName
];
617 if ( itemOrGroup
instanceof mw
.rcfilters
.dm
.FilterItem
) {
618 groupMap
[ itemOrGroup
.getGroupName() ] = groupMap
[ itemOrGroup
.getGroupName() ] || {};
619 groupMap
[ itemOrGroup
.getGroupName() ][ itemOrGroup
.getParamName() ] = paramValue
;
620 } else if ( itemOrGroup
instanceof mw
.rcfilters
.dm
.FilterGroup
) {
621 // This parameter represents a group (values are the filters)
622 // this is equivalent to checking if the group is 'string_options'
623 groupMap
[ itemOrGroup
.getName() ] = groupMap
[ itemOrGroup
.getName() ] || {};
624 groupMap
[ itemOrGroup
.getName() ] = paramValue
;
628 // Go over all groups, so we make sure we get the complete output
629 // even if the parameters don't include a certain group
630 $.each( this.groups
, function ( groupName
, groupModel
) {
631 result
= $.extend( true, {}, result
, groupModel
.getFilterRepresentation( groupMap
[ groupName
] ) );
638 * Get the highlight parameters based on current filter configuration
640 * @return {Object} Object where keys are "<filter name>_color" and values
641 * are the selected highlight colors.
643 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getHighlightParameters = function () {
646 this.getItems().forEach( function ( filterItem
) {
647 result
[ filterItem
.getName() + '_color' ] = filterItem
.getHighlightColor() || null;
649 result
.highlight
= String( Number( this.isHighlightEnabled() ) );
655 * Extract the highlight values from given object. Since highlights are
656 * the same for filter and parameters, it doesn't matter which one is
657 * given; values will be returned with a full list of the highlights
658 * with colors or null values.
660 * @param {Object} representation Object containing representation of
661 * some or all highlight values
662 * @return {Object} Object where keys are "<filter name>_color" and values
663 * are the selected highlight colors. The returned object
664 * contains all available filters either with a color value
667 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.extractHighlightValues = function ( representation
) {
670 this.getItems().forEach( function ( filterItem
) {
671 var highlightName
= filterItem
.getName() + '_color';
672 result
[ highlightName
] = representation
[ highlightName
] || null;
679 * Sanitize value group of a string_option groups type
680 * Remove duplicates and make sure to only use valid
684 * @param {string} groupName Group name
685 * @param {string[]} valueArray Array of values
686 * @return {string[]} Array of valid values
688 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.sanitizeStringOptionGroup = function ( groupName
, valueArray
) {
689 var validNames
= this.getGroupFilters( groupName
).map( function ( filterItem
) {
690 return filterItem
.getParamName();
693 return mw
.rcfilters
.utils
.normalizeParamOptions( valueArray
, validNames
);
697 * Check whether the current filter state is set to all false.
699 * @return {boolean} Current filters are all empty
701 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.areCurrentFiltersEmpty = function () {
702 // Check if there are either any selected items or any items
703 // that have highlight enabled
704 return !this.getItems().some( function ( filterItem
) {
705 return !filterItem
.getGroupModel().isHidden() && ( filterItem
.isSelected() || filterItem
.isHighlighted() );
710 * Check whether the default values of the filters are all false.
712 * @return {boolean} Default filters are all false
714 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.areDefaultFiltersEmpty = function () {
717 if ( this.defaultFiltersEmpty
!== null ) {
718 // We only need to do this test once,
719 // because defaults are set once per session
720 defaultFilters
= this.getFiltersFromParameters( this.getDefaultParams() );
721 this.defaultFiltersEmpty
= Object
.keys( defaultFilters
).every( function ( filterName
) {
722 return !defaultFilters
[ filterName
];
726 return this.defaultFiltersEmpty
;
730 * Get the item that matches the given name
732 * @param {string} name Filter name
733 * @return {mw.rcfilters.dm.FilterItem} Filter item
735 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getItemByName = function ( name
) {
736 return this.getItems().filter( function ( item
) {
737 return name
=== item
.getName();
742 * Set all filters to false or empty/all
743 * This is equivalent to display all.
745 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.emptyAllFilters = function () {
746 this.getItems().forEach( function ( filterItem
) {
747 this.toggleFilterSelected( filterItem
.getName(), false );
752 * Toggle selected state of one item
754 * @param {string} name Name of the filter item
755 * @param {boolean} [isSelected] Filter selected state
757 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.toggleFilterSelected = function ( name
, isSelected
) {
758 var item
= this.getItemByName( name
);
761 item
.toggleSelected( isSelected
);
766 * Toggle selected state of items by their names
768 * @param {Object} filterDef Filter definitions
770 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.toggleFiltersSelected = function ( filterDef
) {
771 Object
.keys( filterDef
).forEach( function ( name
) {
772 this.toggleFilterSelected( name
, filterDef
[ name
] );
777 * Get a group model from its name
779 * @param {string} groupName Group name
780 * @return {mw.rcfilters.dm.FilterGroup} Group model
782 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getGroup = function ( groupName
) {
783 return this.groups
[ groupName
];
787 * Get all filters within a specified group by its name
789 * @param {string} groupName Group name
790 * @return {mw.rcfilters.dm.FilterItem[]} Filters belonging to this group
792 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getGroupFilters = function ( groupName
) {
793 return ( this.getGroup( groupName
) && this.getGroup( groupName
).getItems() ) || [];
797 * Find items whose labels match the given string
799 * @param {string} query Search string
800 * @param {boolean} [returnFlat] Return a flat array. If false, the result
801 * is an object whose keys are the group names and values are an array of
802 * filters per group. If set to true, returns an array of filters regardless
804 * @return {Object} An object of items to show
805 * arranged by their group names
807 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.findMatches = function ( query
, returnFlat
) {
808 var i
, searchIsEmpty
,
812 view
= this.getViewByTrigger( query
.substr( 0, 1 ) ),
813 items
= this.getFiltersByView( view
);
815 // Normalize so we can search strings regardless of case and view
816 query
= query
.toLowerCase();
817 if ( view
!== 'default' ) {
818 query
= query
.substr( 1 );
821 // Check if the search if actually empty; this can be a problem when
822 // we use prefixes to denote different views
823 searchIsEmpty
= query
.length
=== 0;
825 // item label starting with the query string
826 for ( i
= 0; i
< items
.length
; i
++ ) {
829 items
[ i
].getLabel().toLowerCase().indexOf( query
) === 0 ||
831 // For tags, we want the parameter name to be included in the search
833 items
[ i
].getParamName().toLowerCase().indexOf( query
) > -1
836 result
[ items
[ i
].getGroupName() ] = result
[ items
[ i
].getGroupName() ] || [];
837 result
[ items
[ i
].getGroupName() ].push( items
[ i
] );
838 flatResult
.push( items
[ i
] );
842 if ( $.isEmptyObject( result
) ) {
843 // item containing the query string in their label, description, or group title
844 for ( i
= 0; i
< items
.length
; i
++ ) {
845 groupTitle
= items
[ i
].getGroupModel().getTitle();
848 items
[ i
].getLabel().toLowerCase().indexOf( query
) > -1 ||
849 items
[ i
].getDescription().toLowerCase().indexOf( query
) > -1 ||
850 groupTitle
.toLowerCase().indexOf( query
) > -1 ||
852 // For tags, we want the parameter name to be included in the search
854 items
[ i
].getParamName().toLowerCase().indexOf( query
) > -1
857 result
[ items
[ i
].getGroupName() ] = result
[ items
[ i
].getGroupName() ] || [];
858 result
[ items
[ i
].getGroupName() ].push( items
[ i
] );
859 flatResult
.push( items
[ i
] );
864 return returnFlat
? flatResult
: result
;
868 * Get items that are highlighted
870 * @return {mw.rcfilters.dm.FilterItem[]} Highlighted items
872 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getHighlightedItems = function () {
873 return this.getItems().filter( function ( filterItem
) {
874 return filterItem
.isHighlightSupported() &&
875 filterItem
.getHighlightColor();
880 * Get items that allow highlights even if they're not currently highlighted
882 * @return {mw.rcfilters.dm.FilterItem[]} Items supporting highlights
884 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getItemsSupportingHighlights = function () {
885 return this.getItems().filter( function ( filterItem
) {
886 return filterItem
.isHighlightSupported();
891 * Get all selected items
893 * @return {mw.rcfilters.dm.FilterItem[]} Selected items
895 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getSelectedItems = function () {
896 var allSelected
= [];
898 $.each( this.getFilterGroups(), function ( groupName
, groupModel
) {
899 allSelected
= allSelected
.concat( groupModel
.getSelectedItems() );
905 * Switch the current view
907 * @param {string} view View name
910 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.switchView = function ( view
) {
911 if ( this.views
[ view
] && this.currentView
!== view
) {
912 this.currentView
= view
;
913 this.emit( 'update' );
918 * Get the current view
920 * @return {string} Current view
922 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getCurrentView = function () {
923 return this.currentView
;
927 * Get the label for the current view
929 * @param {string} viewName View name
930 * @return {string} Label for the current view
932 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getViewTitle = function ( viewName
) {
933 viewName
= viewName
|| this.getCurrentView();
935 return this.views
[ viewName
] && this.views
[ viewName
].title
;
939 * Get an array of all available view names
941 * @return {string} Available view names
943 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getAvailableViews = function () {
944 return Object
.keys( this.views
);
948 * Get the view that fits the given trigger
950 * @param {string} trigger Trigger
951 * @return {string} Name of view
953 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getViewByTrigger = function ( trigger
) {
954 var result
= 'default';
956 $.each( this.views
, function ( name
, data
) {
957 if ( data
.trigger
=== trigger
) {
966 * Toggle the highlight feature on and off.
967 * Propagate the change to filter items.
969 * @param {boolean} enable Highlight should be enabled
970 * @fires highlightChange
972 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.toggleHighlight = function ( enable
) {
973 enable
= enable
=== undefined ? !this.highlightEnabled
: enable
;
975 if ( this.highlightEnabled
!== enable
) {
976 this.highlightEnabled
= enable
;
978 this.getItems().forEach( function ( filterItem
) {
979 filterItem
.toggleHighlight( this.highlightEnabled
);
982 this.emit( 'highlightChange', this.highlightEnabled
);
987 * Check if the highlight feature is enabled
990 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.isHighlightEnabled = function () {
991 return !!this.highlightEnabled
;
995 * Toggle the inverted namespaces property on and off.
996 * Propagate the change to namespace filter items.
998 * @param {boolean} enable Inverted property is enabled
999 * @fires invertChange
1001 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.toggleInvertedNamespaces = function ( enable
) {
1002 enable
= enable
=== undefined ? !this.invertedNamespaces
: enable
;
1004 if ( this.invertedNamespaces
!== enable
) {
1005 this.invertedNamespaces
= enable
;
1007 this.getFiltersByView( 'namespaces' ).forEach( function ( filterItem
) {
1008 filterItem
.toggleInverted( this.invertedNamespaces
);
1011 this.emit( 'invertChange', this.invertedNamespaces
);
1016 * Check if the namespaces selection is set to be inverted
1019 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.areNamespacesInverted = function () {
1020 return !!this.invertedNamespaces
;
1024 * Set highlight color for a specific filter item
1026 * @param {string} filterName Name of the filter item
1027 * @param {string} color Selected color
1029 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.setHighlightColor = function ( filterName
, color
) {
1030 this.getItemByName( filterName
).setHighlightColor( color
);
1034 * Clear highlight for a specific filter item
1036 * @param {string} filterName Name of the filter item
1038 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.clearHighlightColor = function ( filterName
) {
1039 this.getItemByName( filterName
).clearHighlightColor();
1043 * Clear highlight for all filter items
1045 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.clearAllHighlightColors = function () {
1046 this.getItems().forEach( function ( filterItem
) {
1047 filterItem
.clearHighlightColor();
1052 * Return a version of the given string that is without any
1055 * @param {string} str Given string
1056 * @return {string} Result
1058 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.removeViewTriggers = function ( str
) {
1059 if ( this.getViewByTrigger( str
.substr( 0, 1 ) ) !== 'default' ) {
1060 str
= str
.substr( 1 );
1065 }( mediaWiki
, jQuery
) );