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
) : '';
327 allViews
= $.extend( true, {
329 title
: mw
.msg( 'rcfilters-filterlist-title' ),
335 $.each( allViews
, function ( viewName
, viewData
) {
337 model
.views
[ viewName
] = {
339 title
: viewData
.title
,
340 trigger
: viewData
.trigger
344 viewData
.groups
.forEach( function ( groupData
) {
345 var group
= groupData
.name
;
347 if ( !model
.groups
[ group
] ) {
348 model
.groups
[ group
] = new mw
.rcfilters
.dm
.FilterGroup(
350 $.extend( true, {}, groupData
, { view
: viewName
} )
354 model
.groups
[ group
].initializeFilters( groupData
.filters
, groupData
.default );
355 items
= items
.concat( model
.groups
[ group
].getItems() );
358 if ( groupData
.conflicts
) {
360 groupConflictMap
[ group
] = groupData
.conflicts
;
363 groupData
.filters
.forEach( function ( itemData
) {
364 var filterItem
= model
.groups
[ group
].getItemByParamName( itemData
.name
);
366 if ( itemData
.conflicts
) {
367 filterConflictMap
[ filterItem
.getName() ] = itemData
.conflicts
;
373 // Add item references to the model, for lookup
374 this.addItems( items
);
377 groupConflictResult
= expandConflictDefinitions( groupConflictMap
);
378 filterConflictResult
= expandConflictDefinitions( filterConflictMap
);
380 // Set conflicts for groups
381 $.each( groupConflictResult
, function ( group
, conflicts
) {
382 model
.groups
[ group
].setConflicts( conflicts
);
385 // Set conflicts for items
386 $.each( filterConflictResult
, function ( filterName
, conflicts
) {
387 var filterItem
= model
.getItemByName( filterName
);
388 // set conflicts for items in the group
389 filterItem
.setConflicts( conflicts
);
392 // Create a map between known parameters and their models
393 $.each( this.groups
, function ( group
, groupModel
) {
395 groupModel
.getType() === 'send_unselected_if_any' ||
396 groupModel
.getType() === 'boolean'
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.currentView
= 'default';
413 // Finish initialization
414 this.emit( 'initialize' );
418 * Get the names of all available filters
420 * @return {string[]} An array of filter names
422 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFilterNames = function () {
423 return this.getItems().map( function ( item
) { return item
.getName(); } );
427 * Get the object that defines groups by their name.
429 * @return {Object} Filter groups
431 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFilterGroups = function () {
436 * Get the object that defines groups that match a certain view by their name.
438 * @param {string} [view] Requested view. If not given, uses current view
439 * @return {Object} Filter groups matching a display group
441 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFilterGroupsByView = function ( view
) {
444 view
= view
|| this.getCurrentView();
446 $.each( this.groups
, function ( groupName
, groupModel
) {
447 if ( groupModel
.getView() === view
) {
448 result
[ groupName
] = groupModel
;
456 * Get an array of filters matching the given display group.
458 * @param {string} [view] Requested view. If not given, uses current view
459 * @return {mw.rcfilters.dm.FilterItem} Filter items matching the group
461 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFiltersByView = function ( view
) {
465 view
= view
|| this.getCurrentView();
467 groups
= this.getFilterGroupsByView( view
);
469 $.each( groups
, function ( groupName
, groupModel
) {
470 result
= result
.concat( groupModel
.getItems() );
477 * Get the trigger for the requested view.
479 * @param {string} view View name
480 * @return {string} View trigger, if exists
482 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getViewTrigger = function ( view
) {
483 return ( this.views
[ view
] && this.views
[ view
].trigger
) || '';
486 * Get the value of a specific parameter
488 * @param {string} name Parameter name
489 * @return {number|string} Parameter value
491 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getParamValue = function ( name
) {
492 return this.parameters
[ name
];
496 * Get the current selected state of the filters
498 * @return {Object} Filters selected state
500 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getSelectedState = function () {
502 items
= this.getItems(),
505 for ( i
= 0; i
< items
.length
; i
++ ) {
506 result
[ items
[ i
].getName() ] = items
[ i
].isSelected();
513 * Get the current full state of the filters
515 * @return {Object} Filters full state
517 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFullState = function () {
519 items
= this.getItems(),
522 for ( i
= 0; i
< items
.length
; i
++ ) {
523 result
[ items
[ i
].getName() ] = {
524 selected
: items
[ i
].isSelected(),
525 conflicted
: items
[ i
].isConflicted(),
526 included
: items
[ i
].isIncluded()
534 * Get an object representing default parameters state
536 * @return {Object} Default parameter values
538 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getDefaultParams = function () {
541 // Get default filter state
542 $.each( this.groups
, function ( name
, model
) {
543 $.extend( true, result
, model
.getDefaultParams() );
550 * Get a parameter representation of all sticky parameters
552 * @return {Object} Sticky parameter values
554 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getStickyParams = function () {
557 $.each( this.groups
, function ( name
, model
) {
558 if ( model
.isSticky() ) {
559 $.extend( true, result
, model
.getDefaultParams() );
567 * Get a filter representation of all sticky parameters
569 * @return {Object} Sticky filters values
571 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getStickyFiltersState = function () {
574 $.each( this.groups
, function ( name
, model
) {
575 if ( model
.isSticky() ) {
576 $.extend( true, result
, model
.getSelectedState() );
584 * Analyze the groups and their filters and output an object representing
585 * the state of the parameters they represent.
587 * @param {Object} [filterDefinition] An object defining the filter values,
588 * keyed by filter names.
589 * @return {Object} Parameter state object
591 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getParametersFromFilters = function ( filterDefinition
) {
592 var groupItemDefinition
,
594 groupItems
= this.getFilterGroups();
596 if ( filterDefinition
) {
597 groupItemDefinition
= {};
598 // Filter definition is "flat", but in effect
599 // each group needs to tell us its result based
600 // on the values in it. We need to split this list
601 // back into groupings so we can "feed" it to the
602 // loop below, and we need to expand it so it includes
603 // all filters (set to false)
604 this.getItems().forEach( function ( filterItem
) {
605 groupItemDefinition
[ filterItem
.getGroupName() ] = groupItemDefinition
[ filterItem
.getGroupName() ] || {};
606 groupItemDefinition
[ filterItem
.getGroupName() ][ filterItem
.getName() ] = !!filterDefinition
[ filterItem
.getName() ];
610 $.each( groupItems
, function ( group
, model
) {
613 model
.getParamRepresentation(
614 groupItemDefinition
?
615 groupItemDefinition
[ group
] : null
624 * This is the opposite of the #getParametersFromFilters method; this goes over
625 * the given parameters and translates into a selected/unselected value in the filters.
627 * @param {Object} params Parameters query object
628 * @return {Object} Filter state object
630 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFiltersFromParameters = function ( params
) {
635 // Go over the given parameters, break apart to groupings
636 // The resulting object represents the group with its parameter
637 // values. For example:
644 // group2: "param4|param5"
646 $.each( params
, function ( paramName
, paramValue
) {
648 itemOrGroup
= model
.parameterMap
[ paramName
];
651 groupName
= itemOrGroup
instanceof mw
.rcfilters
.dm
.FilterItem
?
652 itemOrGroup
.getGroupName() : itemOrGroup
.getName();
654 groupMap
[ groupName
] = groupMap
[ groupName
] || {};
655 groupMap
[ groupName
][ paramName
] = paramValue
;
659 // Go over all groups, so we make sure we get the complete output
660 // even if the parameters don't include a certain group
661 $.each( this.groups
, function ( groupName
, groupModel
) {
662 result
= $.extend( true, {}, result
, groupModel
.getFilterRepresentation( groupMap
[ groupName
] ) );
669 * Get the highlight parameters based on current filter configuration
671 * @return {Object} Object where keys are `<filter name>_color` and values
672 * are the selected highlight colors.
674 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getHighlightParameters = function () {
677 this.getItems().forEach( function ( filterItem
) {
678 result
[ filterItem
.getName() + '_color' ] = filterItem
.getHighlightColor() || null;
680 result
.highlight
= String( Number( this.isHighlightEnabled() ) );
686 * Extract the highlight values from given object. Since highlights are
687 * the same for filter and parameters, it doesn't matter which one is
688 * given; values will be returned with a full list of the highlights
689 * with colors or null values.
691 * @param {Object} representation Object containing representation of
692 * some or all highlight values
693 * @return {Object} Object where keys are `<filter name>_color` and values
694 * are the selected highlight colors. The returned object
695 * contains all available filters either with a color value
698 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.extractHighlightValues = function ( representation
) {
701 this.getItems().forEach( function ( filterItem
) {
702 var highlightName
= filterItem
.getName() + '_color';
703 result
[ highlightName
] = representation
[ highlightName
] || null;
710 * Sanitize value group of a string_option groups type
711 * Remove duplicates and make sure to only use valid
715 * @param {string} groupName Group name
716 * @param {string[]} valueArray Array of values
717 * @return {string[]} Array of valid values
719 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.sanitizeStringOptionGroup = function ( groupName
, valueArray
) {
720 var validNames
= this.getGroupFilters( groupName
).map( function ( filterItem
) {
721 return filterItem
.getParamName();
724 return mw
.rcfilters
.utils
.normalizeParamOptions( valueArray
, validNames
);
728 * Check whether the current filter state is set to all false.
730 * @return {boolean} Current filters are all empty
732 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.areCurrentFiltersEmpty = function () {
733 // Check if there are either any selected items or any items
734 // that have highlight enabled
735 return !this.getItems().some( function ( filterItem
) {
736 return !filterItem
.getGroupModel().isHidden() && ( filterItem
.isSelected() || filterItem
.isHighlighted() );
741 * Check whether the default values of the filters are all false.
743 * @return {boolean} Default filters are all false
745 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.areDefaultFiltersEmpty = function () {
748 if ( this.defaultFiltersEmpty
!== null ) {
749 // We only need to do this test once,
750 // because defaults are set once per session
751 defaultFilters
= this.getFiltersFromParameters( this.getDefaultParams() );
752 this.defaultFiltersEmpty
= Object
.keys( defaultFilters
).every( function ( filterName
) {
753 return !defaultFilters
[ filterName
];
757 return this.defaultFiltersEmpty
;
761 * Get the item that matches the given name
763 * @param {string} name Filter name
764 * @return {mw.rcfilters.dm.FilterItem} Filter item
766 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getItemByName = function ( name
) {
767 return this.getItems().filter( function ( item
) {
768 return name
=== item
.getName();
773 * Set all filters to false or empty/all
774 * This is equivalent to display all.
776 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.emptyAllFilters = function () {
777 this.getItems().forEach( function ( filterItem
) {
778 this.toggleFilterSelected( filterItem
.getName(), false );
783 * Toggle selected state of one item
785 * @param {string} name Name of the filter item
786 * @param {boolean} [isSelected] Filter selected state
788 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.toggleFilterSelected = function ( name
, isSelected
) {
789 var item
= this.getItemByName( name
);
792 item
.toggleSelected( isSelected
);
797 * Toggle selected state of items by their names
799 * @param {Object} filterDef Filter definitions
801 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.toggleFiltersSelected = function ( filterDef
) {
802 Object
.keys( filterDef
).forEach( function ( name
) {
803 this.toggleFilterSelected( name
, filterDef
[ name
] );
808 * Get a group model from its name
810 * @param {string} groupName Group name
811 * @return {mw.rcfilters.dm.FilterGroup} Group model
813 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getGroup = function ( groupName
) {
814 return this.groups
[ groupName
];
818 * Get all filters within a specified group by its name
820 * @param {string} groupName Group name
821 * @return {mw.rcfilters.dm.FilterItem[]} Filters belonging to this group
823 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getGroupFilters = function ( groupName
) {
824 return ( this.getGroup( groupName
) && this.getGroup( groupName
).getItems() ) || [];
828 * Find items whose labels match the given string
830 * @param {string} query Search string
831 * @param {boolean} [returnFlat] Return a flat array. If false, the result
832 * is an object whose keys are the group names and values are an array of
833 * filters per group. If set to true, returns an array of filters regardless
835 * @return {Object} An object of items to show
836 * arranged by their group names
838 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.findMatches = function ( query
, returnFlat
) {
839 var i
, searchIsEmpty
,
843 view
= this.getViewByTrigger( query
.substr( 0, 1 ) ),
844 items
= this.getFiltersByView( view
);
846 // Normalize so we can search strings regardless of case and view
847 query
= query
.trim().toLowerCase();
848 if ( view
!== 'default' ) {
849 query
= query
.substr( 1 );
852 // Check if the search if actually empty; this can be a problem when
853 // we use prefixes to denote different views
854 searchIsEmpty
= query
.length
=== 0;
856 // item label starting with the query string
857 for ( i
= 0; i
< items
.length
; i
++ ) {
860 items
[ i
].getLabel().toLowerCase().indexOf( query
) === 0 ||
862 // For tags, we want the parameter name to be included in the search
864 items
[ i
].getParamName().toLowerCase().indexOf( query
) > -1
867 result
[ items
[ i
].getGroupName() ] = result
[ items
[ i
].getGroupName() ] || [];
868 result
[ items
[ i
].getGroupName() ].push( items
[ i
] );
869 flatResult
.push( items
[ i
] );
873 if ( $.isEmptyObject( result
) ) {
874 // item containing the query string in their label, description, or group title
875 for ( i
= 0; i
< items
.length
; i
++ ) {
876 groupTitle
= items
[ i
].getGroupModel().getTitle();
879 items
[ i
].getLabel().toLowerCase().indexOf( query
) > -1 ||
880 items
[ i
].getDescription().toLowerCase().indexOf( query
) > -1 ||
881 groupTitle
.toLowerCase().indexOf( query
) > -1 ||
883 // For tags, we want the parameter name to be included in the search
885 items
[ i
].getParamName().toLowerCase().indexOf( query
) > -1
888 result
[ items
[ i
].getGroupName() ] = result
[ items
[ i
].getGroupName() ] || [];
889 result
[ items
[ i
].getGroupName() ].push( items
[ i
] );
890 flatResult
.push( items
[ i
] );
895 return returnFlat
? flatResult
: result
;
899 * Get items that are highlighted
901 * @return {mw.rcfilters.dm.FilterItem[]} Highlighted items
903 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getHighlightedItems = function () {
904 return this.getItems().filter( function ( filterItem
) {
905 return filterItem
.isHighlightSupported() &&
906 filterItem
.getHighlightColor();
911 * Get items that allow highlights even if they're not currently highlighted
913 * @return {mw.rcfilters.dm.FilterItem[]} Items supporting highlights
915 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getItemsSupportingHighlights = function () {
916 return this.getItems().filter( function ( filterItem
) {
917 return filterItem
.isHighlightSupported();
922 * Get all selected items
924 * @return {mw.rcfilters.dm.FilterItem[]} Selected items
926 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getSelectedItems = function () {
927 var allSelected
= [];
929 $.each( this.getFilterGroups(), function ( groupName
, groupModel
) {
930 allSelected
= allSelected
.concat( groupModel
.getSelectedItems() );
936 * Switch the current view
938 * @param {string} view View name
941 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.switchView = function ( view
) {
942 if ( this.views
[ view
] && this.currentView
!== view
) {
943 this.currentView
= view
;
944 this.emit( 'update' );
949 * Get the current view
951 * @return {string} Current view
953 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getCurrentView = function () {
954 return this.currentView
;
958 * Get the label for the current view
960 * @param {string} viewName View name
961 * @return {string} Label for the current view
963 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getViewTitle = function ( viewName
) {
964 viewName
= viewName
|| this.getCurrentView();
966 return this.views
[ viewName
] && this.views
[ viewName
].title
;
970 * Get an array of all available view names
972 * @return {string} Available view names
974 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getAvailableViews = function () {
975 return Object
.keys( this.views
);
979 * Get the view that fits the given trigger
981 * @param {string} trigger Trigger
982 * @return {string} Name of view
984 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getViewByTrigger = function ( trigger
) {
985 var result
= 'default';
987 $.each( this.views
, function ( name
, data
) {
988 if ( data
.trigger
=== trigger
) {
997 * Toggle the highlight feature on and off.
998 * Propagate the change to filter items.
1000 * @param {boolean} enable Highlight should be enabled
1001 * @fires highlightChange
1003 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.toggleHighlight = function ( enable
) {
1004 enable
= enable
=== undefined ? !this.highlightEnabled
: enable
;
1006 if ( this.highlightEnabled
!== enable
) {
1007 this.highlightEnabled
= enable
;
1009 this.getItems().forEach( function ( filterItem
) {
1010 filterItem
.toggleHighlight( this.highlightEnabled
);
1013 this.emit( 'highlightChange', this.highlightEnabled
);
1018 * Check if the highlight feature is enabled
1021 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.isHighlightEnabled = function () {
1022 return !!this.highlightEnabled
;
1026 * Toggle the inverted namespaces property on and off.
1027 * Propagate the change to namespace filter items.
1029 * @param {boolean} enable Inverted property is enabled
1030 * @fires invertChange
1032 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.toggleInvertedNamespaces = function ( enable
) {
1033 enable
= enable
=== undefined ? !this.invertedNamespaces
: enable
;
1035 if ( this.invertedNamespaces
!== enable
) {
1036 this.invertedNamespaces
= enable
;
1038 this.getFiltersByView( 'namespaces' ).forEach( function ( filterItem
) {
1039 filterItem
.toggleInverted( this.invertedNamespaces
);
1042 this.emit( 'invertChange', this.invertedNamespaces
);
1047 * Check if the namespaces selection is set to be inverted
1050 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.areNamespacesInverted = function () {
1051 return !!this.invertedNamespaces
;
1055 * Set highlight color for a specific filter item
1057 * @param {string} filterName Name of the filter item
1058 * @param {string} color Selected color
1060 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.setHighlightColor = function ( filterName
, color
) {
1061 this.getItemByName( filterName
).setHighlightColor( color
);
1065 * Clear highlight for a specific filter item
1067 * @param {string} filterName Name of the filter item
1069 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.clearHighlightColor = function ( filterName
) {
1070 this.getItemByName( filterName
).clearHighlightColor();
1074 * Clear highlight for all filter items
1076 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.clearAllHighlightColors = function () {
1077 this.getItems().forEach( function ( filterItem
) {
1078 filterItem
.clearHighlightColor();
1083 * Return a version of the given string that is without any
1086 * @param {string} str Given string
1087 * @return {string} Result
1089 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.removeViewTriggers = function ( str
) {
1090 if ( this.getViewByTrigger( str
.substr( 0, 1 ) ) !== 'default' ) {
1091 str
= str
.substr( 1 );
1096 }( mediaWiki
, jQuery
) );