3 * View model for the filters selection and display
5 * @mixins OO.EventEmitter
6 * @mixins OO.EmitterList
10 mw
.rcfilters
.dm
.FiltersViewModel
= function MwRcfiltersDmFiltersViewModel() {
12 OO
.EventEmitter
.call( this );
13 OO
.EmitterList
.call( this );
16 this.defaultParams
= {};
17 this.highlightEnabled
= false;
18 this.parameterMap
= {};
19 this.emptyParameterState
= null;
22 this.currentView
= 'default';
23 this.searchQuery
= null;
26 this.aggregate( { update
: 'filterItemUpdate' } );
27 this.connect( this, { filterItemUpdate
: [ 'emit', 'itemUpdate' ] } );
31 OO
.initClass( mw
.rcfilters
.dm
.FiltersViewModel
);
32 OO
.mixinClass( mw
.rcfilters
.dm
.FiltersViewModel
, OO
.EventEmitter
);
33 OO
.mixinClass( mw
.rcfilters
.dm
.FiltersViewModel
, OO
.EmitterList
);
40 * Filter list is initialized
46 * Model has been updated
51 * @param {mw.rcfilters.dm.FilterItem} item Filter item updated
53 * Filter item has changed
57 * @event highlightChange
58 * @param {boolean} Highlight feature is enabled
60 * Highlight feature has been toggled enabled or disabled
66 * Re-assess the states of filter items based on the interactions between them
68 * @param {mw.rcfilters.dm.FilterItem} [item] Changed item. If not given, the
69 * method will go over the state of all items
71 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.reassessFilterInteractions = function ( item
) {
74 iterationItems
= item
!== undefined ? [ item
] : this.getItems();
76 iterationItems
.forEach( function ( checkedItem
) {
77 var allCheckedItems
= checkedItem
.getSubset().concat( [ checkedItem
.getName() ] ),
78 groupModel
= checkedItem
.getGroupModel();
80 // Check for subsets (included filters) plus the item itself:
81 allCheckedItems
.forEach( function ( filterItemName
) {
82 var itemInSubset
= model
.getItemByName( filterItemName
);
84 itemInSubset
.toggleIncluded(
85 // If any of itemInSubset's supersets are selected, this item
87 itemInSubset
.getSuperset().some( function ( supersetName
) {
88 return ( model
.getItemByName( supersetName
).isSelected() );
93 // Update coverage for the changed group
94 if ( groupModel
.isFullCoverage() ) {
95 allSelected
= groupModel
.areAllSelected();
96 groupModel
.getItems().forEach( function ( filterItem
) {
97 filterItem
.toggleFullyCovered( allSelected
);
102 // Check for conflicts
103 // In this case, we must go over all items, since
104 // conflicts are bidirectional and depend not only on
105 // individual items, but also on the selected states of
106 // the groups they're in.
107 this.getItems().forEach( function ( filterItem
) {
108 var inConflict
= false,
109 filterItemGroup
= filterItem
.getGroupModel();
111 // For each item, see if that item is still conflicting
112 $.each( model
.groups
, function ( groupName
, groupModel
) {
113 if ( filterItem
.getGroupName() === groupName
) {
114 // Check inside the group
115 inConflict
= groupModel
.areAnySelectedInConflictWith( filterItem
);
117 // According to the spec, if two items conflict from two different
118 // groups, the conflict only lasts if the groups **only have selected
119 // items that are conflicting**. If a group has selected items that
120 // are conflicting and non-conflicting, the scope of the result has
121 // expanded enough to completely remove the conflict.
123 // For example, see two groups with conflicts:
126 // name: 'experienced',
127 // conflicts: [ 'unregistered' ]
132 // name: 'registered',
135 // name: 'unregistered',
138 // If we select 'experienced', then 'unregistered' is in conflict (and vice versa),
139 // because, inherently, 'experienced' filter only includes registered users, and so
140 // both filters are in conflict with one another.
141 // However, the minute we select 'registered', the scope of our results
142 // has expanded to no longer have a conflict with 'experienced' filter, and
143 // so the conflict is removed.
145 // In our case, we need to check if the entire group conflicts with
146 // the entire item's group, so we follow the above spec
148 // The foreign group is in conflict with this item
149 groupModel
.areAllSelectedInConflictWith( filterItem
) &&
150 // Every selected member of the item's own group is also
151 // in conflict with the other group
152 filterItemGroup
.getSelectedItems().every( function ( otherGroupItem
) {
153 return groupModel
.areAllSelectedInConflictWith( otherGroupItem
);
158 // If we're in conflict, this will return 'false' which
159 // will break the loop. Otherwise, we're not in conflict
160 // and the loop continues
164 // Toggle the item state
165 filterItem
.toggleConflicted( inConflict
);
170 * Get whether the model has any conflict in its items
172 * @return {boolean} There is a conflict
174 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.hasConflict = function () {
175 return this.getItems().some( function ( filterItem
) {
176 return filterItem
.isSelected() && filterItem
.isConflicted();
181 * Get the first item with a current conflict
183 * @return {mw.rcfilters.dm.FilterItem} Conflicted item
185 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFirstConflictedItem = function () {
188 $.each( this.getItems(), function ( index
, filterItem
) {
189 if ( filterItem
.isSelected() && filterItem
.isConflicted() ) {
190 conflictedItem
= filterItem
;
195 return conflictedItem
;
199 * Set filters and preserve a group relationship based on
200 * the definition given by an object
202 * @param {Array} filterGroups Filters definition
203 * @param {Object} [views] Extra views definition
204 * Expected in the following format:
207 * label: 'namespaces', // Message key
212 * name: 'namespaces' // Parameter name
213 * title: 'namespaces' // Message key
214 * type: 'string_options',
216 * labelPrefixKey: { 'default': 'rcfilters-tag-prefix-namespace', inverted: 'rcfilters-tag-prefix-namespace-inverted' },
224 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.initializeFilters = function ( filterGroups
, views
) {
225 var filterConflictResult
, groupConflictResult
,
229 groupConflictMap
= {},
230 filterConflictMap
= {},
232 * Expand a conflict definition from group name to
233 * the list of all included filters in that group.
234 * We do this so that the direct relationship in the
235 * models are consistently item->items rather than
236 * mixing item->group with item->item.
238 * @param {Object} obj Conflict definition
239 * @return {Object} Expanded conflict definition
241 expandConflictDefinitions = function ( obj
) {
244 $.each( obj
, function ( key
, conflicts
) {
246 adjustedConflicts
= {};
248 conflicts
.forEach( function ( conflict
) {
251 if ( conflict
.filter
) {
252 filterName
= model
.groups
[ conflict
.group
].getPrefixedName( conflict
.filter
);
253 filter
= model
.getItemByName( filterName
);
256 adjustedConflicts
[ filterName
] = $.extend(
265 // This conflict is for an entire group. Split it up to
266 // represent each filter
268 // Get the relevant group items
269 model
.groups
[ conflict
.group
].getItems().forEach( function ( groupItem
) {
270 // Rebuild the conflict
271 adjustedConflicts
[ groupItem
.getName() ] = $.extend(
275 filter
: groupItem
.getName(),
283 result
[ key
] = adjustedConflicts
;
295 filterGroups
= OO
.copy( filterGroups
);
297 // Normalize definition from the server
298 filterGroups
.forEach( function ( data
) {
300 // What's this information needs to be normalized
302 body
: data
.whatsThisBody
,
303 header
: data
.whatsThisHeader
,
304 linkText
: data
.whatsThisLinkText
,
305 url
: data
.whatsThisUrl
308 // Title is a msg-key
309 data
.title
= data
.title
? mw
.msg( data
.title
) : data
.name
;
311 // Filters are given to us with msg-keys, we need
312 // to translate those before we hand them off
313 for ( i
= 0; i
< data
.filters
.length
; i
++ ) {
314 data
.filters
[ i
].label
= data
.filters
[ i
].label
? mw
.msg( data
.filters
[ i
].label
) : data
.filters
[ i
].name
;
315 data
.filters
[ i
].description
= data
.filters
[ i
].description
? mw
.msg( data
.filters
[ i
].description
) : '';
320 allViews
= $.extend( true, {
322 title
: mw
.msg( 'rcfilters-filterlist-title' ),
328 $.each( allViews
, function ( viewName
, viewData
) {
330 model
.views
[ viewName
] = {
332 title
: viewData
.title
,
333 trigger
: viewData
.trigger
337 viewData
.groups
.forEach( function ( groupData
) {
338 var group
= groupData
.name
;
340 if ( !model
.groups
[ group
] ) {
341 model
.groups
[ group
] = new mw
.rcfilters
.dm
.FilterGroup(
343 $.extend( true, {}, groupData
, { view
: viewName
} )
347 model
.groups
[ group
].initializeFilters( groupData
.filters
, groupData
.default );
348 items
= items
.concat( model
.groups
[ group
].getItems() );
351 if ( groupData
.conflicts
) {
353 groupConflictMap
[ group
] = groupData
.conflicts
;
356 groupData
.filters
.forEach( function ( itemData
) {
357 var filterItem
= model
.groups
[ group
].getItemByParamName( itemData
.name
);
359 if ( itemData
.conflicts
) {
360 filterConflictMap
[ filterItem
.getName() ] = itemData
.conflicts
;
366 // Add item references to the model, for lookup
367 this.addItems( items
);
370 groupConflictResult
= expandConflictDefinitions( groupConflictMap
);
371 filterConflictResult
= expandConflictDefinitions( filterConflictMap
);
373 // Set conflicts for groups
374 $.each( groupConflictResult
, function ( group
, conflicts
) {
375 model
.groups
[ group
].setConflicts( conflicts
);
378 // Set conflicts for items
379 $.each( filterConflictResult
, function ( filterName
, conflicts
) {
380 var filterItem
= model
.getItemByName( filterName
);
381 // set conflicts for items in the group
382 filterItem
.setConflicts( conflicts
);
385 // Create a map between known parameters and their models
386 $.each( this.groups
, function ( group
, groupModel
) {
388 groupModel
.getType() === 'send_unselected_if_any' ||
389 groupModel
.getType() === 'boolean' ||
390 groupModel
.getType() === 'any_value'
392 // Individual filters
393 groupModel
.getItems().forEach( function ( filterItem
) {
394 model
.parameterMap
[ filterItem
.getParamName() ] = filterItem
;
397 groupModel
.getType() === 'string_options' ||
398 groupModel
.getType() === 'single_option'
401 model
.parameterMap
[ groupModel
.getName() ] = groupModel
;
405 this.setSearch( '' );
407 this.updateHighlightedState();
409 // Finish initialization
410 this.emit( 'initialize' );
414 * Update filter view model state based on a parameter object
416 * @param {Object} params Parameters object
418 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.updateStateFromParams = function ( params
) {
420 // For arbitrary numeric single_option values make sure the values
421 // are normalized to fit within the limits
422 $.each( this.getFilterGroups(), function ( groupName
, groupModel
) {
423 params
[ groupName
] = groupModel
.normalizeArbitraryValue( params
[ groupName
] );
426 // Update filter values
427 filtersValue
= this.getFiltersFromParameters( params
);
428 Object
.keys( filtersValue
).forEach( function ( filterName
) {
429 this.getItemByName( filterName
).setValue( filtersValue
[ filterName
] );
432 // Update highlight state
433 this.getItemsSupportingHighlights().forEach( function ( filterItem
) {
434 var color
= params
[ filterItem
.getName() + '_color' ];
436 filterItem
.setHighlightColor( color
);
438 filterItem
.clearHighlightColor();
441 this.updateHighlightedState();
443 // Check all filter interactions
444 this.reassessFilterInteractions();
448 * Get a representation of an empty (falsey) parameter state
450 * @return {Object} Empty parameter state
452 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getEmptyParameterState = function () {
453 if ( !this.emptyParameterState
) {
454 this.emptyParameterState
= $.extend(
457 this.getParametersFromFilters( {} ),
458 this.getEmptyHighlightParameters()
461 return this.emptyParameterState
;
465 * Get a representation of only the non-falsey parameters
467 * @param {Object} [parameters] A given parameter state to minimize. If not given the current
468 * state of the system will be used.
469 * @return {Object} Empty parameter state
471 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getMinimizedParamRepresentation = function ( parameters
) {
474 parameters
= parameters
? $.extend( true, {}, parameters
) : this.getCurrentParameterState();
477 $.each( this.getEmptyParameterState(), function ( param
, value
) {
478 if ( parameters
[ param
] !== undefined && parameters
[ param
] !== value
) {
479 result
[ param
] = parameters
[ param
];
484 Object
.keys( this.getEmptyHighlightParameters() ).forEach( function ( param
) {
485 if ( parameters
[ param
] ) {
486 // If a highlight parameter is not undefined and not null
487 // add it to the result
488 result
[ param
] = parameters
[ param
];
496 * Get a representation of the full parameter list, including all base values
498 * @return {Object} Full parameter representation
500 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getExpandedParamRepresentation = function () {
504 this.getEmptyParameterState(),
505 this.getCurrentParameterState()
510 * Get a parameter representation of the current state of the model
512 * @param {boolean} [removeStickyParams] Remove sticky filters from final result
513 * @return {Object} Parameter representation of the current state of the model
515 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getCurrentParameterState = function ( removeStickyParams
) {
516 var state
= this.getMinimizedParamRepresentation( $.extend(
519 this.getParametersFromFilters( this.getSelectedState() ),
520 this.getHighlightParameters()
523 if ( removeStickyParams
) {
524 state
= this.removeStickyParams( state
);
531 * Delete sticky parameters from given object.
533 * @param {Object} paramState Parameter state
534 * @return {Object} Parameter state without sticky parameters
536 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.removeStickyParams = function ( paramState
) {
537 this.getStickyParams().forEach( function ( paramName
) {
538 delete paramState
[ paramName
];
545 * Turn the highlight feature on or off
547 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.updateHighlightedState = function () {
548 this.toggleHighlight( this.getHighlightedItems().length
> 0 );
552 * Get the object that defines groups by their name.
554 * @return {Object} Filter groups
556 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFilterGroups = function () {
561 * Get the object that defines groups that match a certain view by their name.
563 * @param {string} [view] Requested view. If not given, uses current view
564 * @return {Object} Filter groups matching a display group
566 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFilterGroupsByView = function ( view
) {
569 view
= view
|| this.getCurrentView();
571 $.each( this.groups
, function ( groupName
, groupModel
) {
572 if ( groupModel
.getView() === view
) {
573 result
[ groupName
] = groupModel
;
581 * Get an array of filters matching the given display group.
583 * @param {string} [view] Requested view. If not given, uses current view
584 * @return {mw.rcfilters.dm.FilterItem} Filter items matching the group
586 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFiltersByView = function ( view
) {
590 view
= view
|| this.getCurrentView();
592 groups
= this.getFilterGroupsByView( view
);
594 $.each( groups
, function ( groupName
, groupModel
) {
595 result
= result
.concat( groupModel
.getItems() );
602 * Get the trigger for the requested view.
604 * @param {string} view View name
605 * @return {string} View trigger, if exists
607 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getViewTrigger = function ( view
) {
608 return ( this.views
[ view
] && this.views
[ view
].trigger
) || '';
612 * Get the value of a specific parameter
614 * @param {string} name Parameter name
615 * @return {number|string} Parameter value
617 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getParamValue = function ( name
) {
618 return this.parameters
[ name
];
622 * Get the current selected state of the filters
624 * @param {boolean} [onlySelected] return an object containing only the filters with a value
625 * @return {Object} Filters selected state
627 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getSelectedState = function ( onlySelected
) {
629 items
= this.getItems(),
632 for ( i
= 0; i
< items
.length
; i
++ ) {
633 if ( !onlySelected
|| items
[ i
].getValue() ) {
634 result
[ items
[ i
].getName() ] = items
[ i
].getValue();
642 * Get the current full state of the filters
644 * @return {Object} Filters full state
646 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFullState = function () {
648 items
= this.getItems(),
651 for ( i
= 0; i
< items
.length
; i
++ ) {
652 result
[ items
[ i
].getName() ] = {
653 selected
: items
[ i
].isSelected(),
654 conflicted
: items
[ i
].isConflicted(),
655 included
: items
[ i
].isIncluded()
663 * Get an object representing default parameters state
665 * @return {Object} Default parameter values
667 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getDefaultParams = function () {
670 // Get default filter state
671 $.each( this.groups
, function ( name
, model
) {
672 if ( !model
.isSticky() ) {
673 $.extend( true, result
, model
.getDefaultParams() );
681 * Get a parameter representation of all sticky parameters
683 * @return {Object} Sticky parameter values
685 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getStickyParams = function () {
688 $.each( this.groups
, function ( name
, model
) {
689 if ( model
.isSticky() ) {
690 if ( model
.isPerGroupRequestParameter() ) {
693 // Each filter is its own param
694 result
= result
.concat( model
.getItems().map( function ( filterItem
) {
695 return filterItem
.getParamName();
705 * Get a parameter representation of all sticky parameters
707 * @return {Object} Sticky parameter values
709 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getStickyParamsValues = function () {
712 $.each( this.groups
, function ( name
, model
) {
713 if ( model
.isSticky() ) {
714 $.extend( true, result
, model
.getParamRepresentation() );
722 * Analyze the groups and their filters and output an object representing
723 * the state of the parameters they represent.
725 * @param {Object} [filterDefinition] An object defining the filter values,
726 * keyed by filter names.
727 * @return {Object} Parameter state object
729 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getParametersFromFilters = function ( filterDefinition
) {
730 var groupItemDefinition
,
732 groupItems
= this.getFilterGroups();
734 if ( filterDefinition
) {
735 groupItemDefinition
= {};
736 // Filter definition is "flat", but in effect
737 // each group needs to tell us its result based
738 // on the values in it. We need to split this list
739 // back into groupings so we can "feed" it to the
740 // loop below, and we need to expand it so it includes
741 // all filters (set to false)
742 this.getItems().forEach( function ( filterItem
) {
743 groupItemDefinition
[ filterItem
.getGroupName() ] = groupItemDefinition
[ filterItem
.getGroupName() ] || {};
744 groupItemDefinition
[ filterItem
.getGroupName() ][ filterItem
.getName() ] = filterItem
.coerceValue( filterDefinition
[ filterItem
.getName() ] );
748 $.each( groupItems
, function ( group
, model
) {
751 model
.getParamRepresentation(
752 groupItemDefinition
?
753 groupItemDefinition
[ group
] : null
762 * This is the opposite of the #getParametersFromFilters method; this goes over
763 * the given parameters and translates into a selected/unselected value in the filters.
765 * @param {Object} params Parameters query object
766 * @return {Object} Filter state object
768 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFiltersFromParameters = function ( params
) {
773 // Go over the given parameters, break apart to groupings
774 // The resulting object represents the group with its parameter
775 // values. For example:
782 // group2: "param4|param5"
784 $.each( params
, function ( paramName
, paramValue
) {
786 itemOrGroup
= model
.parameterMap
[ paramName
];
789 groupName
= itemOrGroup
instanceof mw
.rcfilters
.dm
.FilterItem
?
790 itemOrGroup
.getGroupName() : itemOrGroup
.getName();
792 groupMap
[ groupName
] = groupMap
[ groupName
] || {};
793 groupMap
[ groupName
][ paramName
] = paramValue
;
797 // Go over all groups, so we make sure we get the complete output
798 // even if the parameters don't include a certain group
799 $.each( this.groups
, function ( groupName
, groupModel
) {
800 result
= $.extend( true, {}, result
, groupModel
.getFilterRepresentation( groupMap
[ groupName
] ) );
807 * Get the highlight parameters based on current filter configuration
809 * @return {Object} Object where keys are `<filter name>_color` and values
810 * are the selected highlight colors.
812 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getHighlightParameters = function () {
813 var highlightEnabled
= this.isHighlightEnabled(),
816 this.getItems().forEach( function ( filterItem
) {
817 if ( filterItem
.isHighlightSupported() ) {
818 result
[ filterItem
.getName() + '_color' ] = highlightEnabled
&& filterItem
.isHighlighted() ?
819 filterItem
.getHighlightColor() :
828 * Get an object representing the complete empty state of highlights
830 * @return {Object} Object containing all the highlight parameters set to their negative value
832 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getEmptyHighlightParameters = function () {
835 this.getItems().forEach( function ( filterItem
) {
836 if ( filterItem
.isHighlightSupported() ) {
837 result
[ filterItem
.getName() + '_color' ] = null;
845 * Get an array of currently applied highlight colors
847 * @return {string[]} Currently applied highlight colors
849 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getCurrentlyUsedHighlightColors = function () {
852 if ( this.isHighlightEnabled() ) {
853 this.getHighlightedItems().forEach( function ( filterItem
) {
854 var color
= filterItem
.getHighlightColor();
856 if ( result
.indexOf( color
) === -1 ) {
857 result
.push( color
);
866 * Sanitize value group of a string_option groups type
867 * Remove duplicates and make sure to only use valid
871 * @param {string} groupName Group name
872 * @param {string[]} valueArray Array of values
873 * @return {string[]} Array of valid values
875 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.sanitizeStringOptionGroup = function ( groupName
, valueArray
) {
876 var validNames
= this.getGroupFilters( groupName
).map( function ( filterItem
) {
877 return filterItem
.getParamName();
880 return mw
.rcfilters
.utils
.normalizeParamOptions( valueArray
, validNames
);
884 * Check whether no visible filter is selected.
886 * Filter groups that are hidden or sticky are not shown in the
887 * active filters area and therefore not included in this check.
889 * @return {boolean} No visible filter is selected
891 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.areVisibleFiltersEmpty = function () {
892 // Check if there are either any selected items or any items
893 // that have highlight enabled
894 return !this.getItems().some( function ( filterItem
) {
895 var visible
= !filterItem
.getGroupModel().isSticky() && !filterItem
.getGroupModel().isHidden(),
896 active
= ( filterItem
.isSelected() || filterItem
.isHighlighted() );
897 return visible
&& active
;
902 * Check whether the invert state is a valid one. A valid invert state is one where
903 * there are actual namespaces selected.
905 * This is done to compare states to previous ones that may have had the invert model
906 * selected but effectively had no namespaces, so are not effectively different than
907 * ones where invert is not selected.
909 * @return {boolean} Invert is effectively selected
911 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.areNamespacesEffectivelyInverted = function () {
912 return this.getInvertModel().isSelected() &&
913 this.getSelectedItems().some( function ( itemModel
) {
914 return itemModel
.getGroupModel().getView() === 'namespace';
919 * Get the item that matches the given name
921 * @param {string} name Filter name
922 * @return {mw.rcfilters.dm.FilterItem} Filter item
924 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getItemByName = function ( name
) {
925 return this.getItems().filter( function ( item
) {
926 return name
=== item
.getName();
931 * Set all filters to false or empty/all
932 * This is equivalent to display all.
934 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.emptyAllFilters = function () {
935 this.getItems().forEach( function ( filterItem
) {
936 if ( !filterItem
.getGroupModel().isSticky() ) {
937 this.toggleFilterSelected( filterItem
.getName(), false );
943 * Toggle selected state of one item
945 * @param {string} name Name of the filter item
946 * @param {boolean} [isSelected] Filter selected state
948 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.toggleFilterSelected = function ( name
, isSelected
) {
949 var item
= this.getItemByName( name
);
952 item
.toggleSelected( isSelected
);
957 * Toggle selected state of items by their names
959 * @param {Object} filterDef Filter definitions
961 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.toggleFiltersSelected = function ( filterDef
) {
962 Object
.keys( filterDef
).forEach( function ( name
) {
963 this.toggleFilterSelected( name
, filterDef
[ name
] );
968 * Get a group model from its name
970 * @param {string} groupName Group name
971 * @return {mw.rcfilters.dm.FilterGroup} Group model
973 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getGroup = function ( groupName
) {
974 return this.groups
[ groupName
];
978 * Get all filters within a specified group by its name
980 * @param {string} groupName Group name
981 * @return {mw.rcfilters.dm.FilterItem[]} Filters belonging to this group
983 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getGroupFilters = function ( groupName
) {
984 return ( this.getGroup( groupName
) && this.getGroup( groupName
).getItems() ) || [];
988 * Find items whose labels match the given string
990 * @param {string} query Search string
991 * @param {boolean} [returnFlat] Return a flat array. If false, the result
992 * is an object whose keys are the group names and values are an array of
993 * filters per group. If set to true, returns an array of filters regardless
995 * @return {Object} An object of items to show
996 * arranged by their group names
998 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.findMatches = function ( query
, returnFlat
) {
999 var i
, searchIsEmpty
,
1003 view
= this.getViewByTrigger( query
.substr( 0, 1 ) ),
1004 items
= this.getFiltersByView( view
);
1006 // Normalize so we can search strings regardless of case and view
1007 query
= query
.trim().toLowerCase();
1008 if ( view
!== 'default' ) {
1009 query
= query
.substr( 1 );
1011 // Trim again to also intercept cases where the spaces were after the trigger
1013 query
= query
.trim();
1015 // Check if the search if actually empty; this can be a problem when
1016 // we use prefixes to denote different views
1017 searchIsEmpty
= query
.length
=== 0;
1019 // item label starting with the query string
1020 for ( i
= 0; i
< items
.length
; i
++ ) {
1023 items
[ i
].getLabel().toLowerCase().indexOf( query
) === 0 ||
1025 // For tags, we want the parameter name to be included in the search
1027 items
[ i
].getParamName().toLowerCase().indexOf( query
) > -1
1030 result
[ items
[ i
].getGroupName() ] = result
[ items
[ i
].getGroupName() ] || [];
1031 result
[ items
[ i
].getGroupName() ].push( items
[ i
] );
1032 flatResult
.push( items
[ i
] );
1036 if ( $.isEmptyObject( result
) ) {
1037 // item containing the query string in their label, description, or group title
1038 for ( i
= 0; i
< items
.length
; i
++ ) {
1039 groupTitle
= items
[ i
].getGroupModel().getTitle();
1042 items
[ i
].getLabel().toLowerCase().indexOf( query
) > -1 ||
1043 items
[ i
].getDescription().toLowerCase().indexOf( query
) > -1 ||
1044 groupTitle
.toLowerCase().indexOf( query
) > -1 ||
1046 // For tags, we want the parameter name to be included in the search
1048 items
[ i
].getParamName().toLowerCase().indexOf( query
) > -1
1051 result
[ items
[ i
].getGroupName() ] = result
[ items
[ i
].getGroupName() ] || [];
1052 result
[ items
[ i
].getGroupName() ].push( items
[ i
] );
1053 flatResult
.push( items
[ i
] );
1058 return returnFlat
? flatResult
: result
;
1062 * Get items that are highlighted
1064 * @return {mw.rcfilters.dm.FilterItem[]} Highlighted items
1066 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getHighlightedItems = function () {
1067 return this.getItems().filter( function ( filterItem
) {
1068 return filterItem
.isHighlightSupported() &&
1069 filterItem
.getHighlightColor();
1074 * Get items that allow highlights even if they're not currently highlighted
1076 * @return {mw.rcfilters.dm.FilterItem[]} Items supporting highlights
1078 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getItemsSupportingHighlights = function () {
1079 return this.getItems().filter( function ( filterItem
) {
1080 return filterItem
.isHighlightSupported();
1085 * Get all selected items
1087 * @return {mw.rcfilters.dm.FilterItem[]} Selected items
1089 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getSelectedItems = function () {
1090 var allSelected
= [];
1092 $.each( this.getFilterGroups(), function ( groupName
, groupModel
) {
1093 allSelected
= allSelected
.concat( groupModel
.getSelectedItems() );
1100 * Get the current view
1102 * @return {string} Current view
1104 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getCurrentView = function () {
1105 return this.currentView
;
1109 * Get the label for the current view
1111 * @param {string} viewName View name
1112 * @return {string} Label for the current view
1114 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getViewTitle = function ( viewName
) {
1115 viewName
= viewName
|| this.getCurrentView();
1117 return this.views
[ viewName
] && this.views
[ viewName
].title
;
1121 * Get the view that fits the given trigger
1123 * @param {string} trigger Trigger
1124 * @return {string} Name of view
1126 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getViewByTrigger = function ( trigger
) {
1127 var result
= 'default';
1129 $.each( this.views
, function ( name
, data
) {
1130 if ( data
.trigger
=== trigger
) {
1139 * Return a version of the given string that is without any
1142 * @param {string} str Given string
1143 * @return {string} Result
1145 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.removeViewTriggers = function ( str
) {
1146 if ( this.getViewFromString( str
) !== 'default' ) {
1147 str
= str
.substr( 1 );
1154 * Get the view from the given string by a trigger, if it exists
1156 * @param {string} str Given string
1157 * @return {string} View name
1159 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getViewFromString = function ( str
) {
1160 return this.getViewByTrigger( str
.substr( 0, 1 ) );
1164 * Set the current search for the system.
1165 * This also dictates what items and groups are visible according
1166 * to the search in #findMatches
1168 * @param {string} searchQuery Search query, including triggers
1169 * @fires searchChange
1171 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.setSearch = function ( searchQuery
) {
1172 var visibleGroups
, visibleGroupNames
;
1174 if ( this.searchQuery
!== searchQuery
) {
1175 // Check if the view changed
1176 this.switchView( this.getViewFromString( searchQuery
) );
1178 visibleGroups
= this.findMatches( searchQuery
);
1179 visibleGroupNames
= Object
.keys( visibleGroups
);
1181 // Update visibility of items and groups
1182 $.each( this.getFilterGroups(), function ( groupName
, groupModel
) {
1183 // Check if the group is visible at all
1184 groupModel
.toggleVisible( visibleGroupNames
.indexOf( groupName
) !== -1 );
1185 groupModel
.setVisibleItems( visibleGroups
[ groupName
] || [] );
1188 this.searchQuery
= searchQuery
;
1189 this.emit( 'searchChange', this.searchQuery
);
1194 * Get the current search
1196 * @return {string} Current search query
1198 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getSearch = function () {
1199 return this.searchQuery
;
1203 * Switch the current view
1206 * @param {string} view View name
1208 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.switchView = function ( view
) {
1209 if ( this.views
[ view
] && this.currentView
!== view
) {
1210 this.currentView
= view
;
1215 * Toggle the highlight feature on and off.
1216 * Propagate the change to filter items.
1218 * @param {boolean} enable Highlight should be enabled
1219 * @fires highlightChange
1221 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.toggleHighlight = function ( enable
) {
1222 enable
= enable
=== undefined ? !this.highlightEnabled
: enable
;
1224 if ( this.highlightEnabled
!== enable
) {
1225 this.highlightEnabled
= enable
;
1226 this.emit( 'highlightChange', this.highlightEnabled
);
1231 * Check if the highlight feature is enabled
1234 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.isHighlightEnabled = function () {
1235 return !!this.highlightEnabled
;
1239 * Toggle the inverted namespaces property on and off.
1240 * Propagate the change to namespace filter items.
1242 * @param {boolean} enable Inverted property is enabled
1244 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.toggleInvertedNamespaces = function ( enable
) {
1245 this.toggleFilterSelected( this.getInvertModel().getName(), enable
);
1249 * Get the model object that represents the 'invert' filter
1251 * @return {mw.rcfilters.dm.FilterItem}
1253 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getInvertModel = function () {
1254 return this.getGroup( 'invertGroup' ).getItemByParamName( 'invert' );
1258 * Set highlight color for a specific filter item
1260 * @param {string} filterName Name of the filter item
1261 * @param {string} color Selected color
1263 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.setHighlightColor = function ( filterName
, color
) {
1264 this.getItemByName( filterName
).setHighlightColor( color
);
1268 * Clear highlight for a specific filter item
1270 * @param {string} filterName Name of the filter item
1272 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.clearHighlightColor = function ( filterName
) {
1273 this.getItemByName( filterName
).clearHighlightColor();
1276 }( mediaWiki
, jQuery
) );