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';
25 this.aggregate( { update
: 'filterItemUpdate' } );
26 this.connect( this, { filterItemUpdate
: [ 'emit', 'itemUpdate' ] } );
30 OO
.initClass( mw
.rcfilters
.dm
.FiltersViewModel
);
31 OO
.mixinClass( mw
.rcfilters
.dm
.FiltersViewModel
, OO
.EventEmitter
);
32 OO
.mixinClass( mw
.rcfilters
.dm
.FiltersViewModel
, OO
.EmitterList
);
39 * Filter list is initialized
45 * Model has been updated
50 * @param {mw.rcfilters.dm.FilterItem} item Filter item updated
52 * Filter item has changed
56 * @event highlightChange
57 * @param {boolean} Highlight feature is enabled
59 * Highlight feature has been toggled enabled or disabled
65 * Re-assess the states of filter items based on the interactions between them
67 * @param {mw.rcfilters.dm.FilterItem} [item] Changed item. If not given, the
68 * method will go over the state of all items
70 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.reassessFilterInteractions = function ( item
) {
73 iterationItems
= item
!== undefined ? [ item
] : this.getItems();
75 iterationItems
.forEach( function ( checkedItem
) {
76 var allCheckedItems
= checkedItem
.getSubset().concat( [ checkedItem
.getName() ] ),
77 groupModel
= checkedItem
.getGroupModel();
79 // Check for subsets (included filters) plus the item itself:
80 allCheckedItems
.forEach( function ( filterItemName
) {
81 var itemInSubset
= model
.getItemByName( filterItemName
);
83 itemInSubset
.toggleIncluded(
84 // If any of itemInSubset's supersets are selected, this item
86 itemInSubset
.getSuperset().some( function ( supersetName
) {
87 return ( model
.getItemByName( supersetName
).isSelected() );
92 // Update coverage for the changed group
93 if ( groupModel
.isFullCoverage() ) {
94 allSelected
= groupModel
.areAllSelected();
95 groupModel
.getItems().forEach( function ( filterItem
) {
96 filterItem
.toggleFullyCovered( allSelected
);
101 // Check for conflicts
102 // In this case, we must go over all items, since
103 // conflicts are bidirectional and depend not only on
104 // individual items, but also on the selected states of
105 // the groups they're in.
106 this.getItems().forEach( function ( filterItem
) {
107 var inConflict
= false,
108 filterItemGroup
= filterItem
.getGroupModel();
110 // For each item, see if that item is still conflicting
111 $.each( model
.groups
, function ( groupName
, groupModel
) {
112 if ( filterItem
.getGroupName() === groupName
) {
113 // Check inside the group
114 inConflict
= groupModel
.areAnySelectedInConflictWith( filterItem
);
116 // According to the spec, if two items conflict from two different
117 // groups, the conflict only lasts if the groups **only have selected
118 // items that are conflicting**. If a group has selected items that
119 // are conflicting and non-conflicting, the scope of the result has
120 // expanded enough to completely remove the conflict.
122 // For example, see two groups with conflicts:
125 // name: 'experienced',
126 // conflicts: [ 'unregistered' ]
131 // name: 'registered',
134 // name: 'unregistered',
137 // If we select 'experienced', then 'unregistered' is in conflict (and vice versa),
138 // because, inherently, 'experienced' filter only includes registered users, and so
139 // both filters are in conflict with one another.
140 // However, the minute we select 'registered', the scope of our results
141 // has expanded to no longer have a conflict with 'experienced' filter, and
142 // so the conflict is removed.
144 // In our case, we need to check if the entire group conflicts with
145 // the entire item's group, so we follow the above spec
147 // The foreign group is in conflict with this item
148 groupModel
.areAllSelectedInConflictWith( filterItem
) &&
149 // Every selected member of the item's own group is also
150 // in conflict with the other group
151 filterItemGroup
.getSelectedItems().every( function ( otherGroupItem
) {
152 return groupModel
.areAllSelectedInConflictWith( otherGroupItem
);
157 // If we're in conflict, this will return 'false' which
158 // will break the loop. Otherwise, we're not in conflict
159 // and the loop continues
163 // Toggle the item state
164 filterItem
.toggleConflicted( inConflict
);
169 * Get whether the model has any conflict in its items
171 * @return {boolean} There is a conflict
173 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.hasConflict = function () {
174 return this.getItems().some( function ( filterItem
) {
175 return filterItem
.isSelected() && filterItem
.isConflicted();
180 * Get the first item with a current conflict
182 * @return {mw.rcfilters.dm.FilterItem} Conflicted item
184 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFirstConflictedItem = function () {
187 $.each( this.getItems(), function ( index
, filterItem
) {
188 if ( filterItem
.isSelected() && filterItem
.isConflicted() ) {
189 conflictedItem
= filterItem
;
194 return conflictedItem
;
198 * Set filters and preserve a group relationship based on
199 * the definition given by an object
201 * @param {Array} filterGroups Filters definition
202 * @param {Object} [views] Extra views definition
203 * Expected in the following format:
206 * label: 'namespaces', // Message key
211 * name: 'namespaces' // Parameter name
212 * title: 'namespaces' // Message key
213 * type: 'string_options',
215 * labelPrefixKey: { 'default': 'rcfilters-tag-prefix-namespace', inverted: 'rcfilters-tag-prefix-namespace-inverted' },
223 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.initializeFilters = function ( filterGroups
, views
) {
224 var filterConflictResult
, groupConflictResult
,
228 groupConflictMap
= {},
229 filterConflictMap
= {},
231 * Expand a conflict definition from group name to
232 * the list of all included filters in that group.
233 * We do this so that the direct relationship in the
234 * models are consistently item->items rather than
235 * mixing item->group with item->item.
237 * @param {Object} obj Conflict definition
238 * @return {Object} Expanded conflict definition
240 expandConflictDefinitions = function ( obj
) {
243 $.each( obj
, function ( key
, conflicts
) {
245 adjustedConflicts
= {};
247 conflicts
.forEach( function ( conflict
) {
250 if ( conflict
.filter
) {
251 filterName
= model
.groups
[ conflict
.group
].getPrefixedName( conflict
.filter
);
252 filter
= model
.getItemByName( filterName
);
255 adjustedConflicts
[ filterName
] = $.extend(
264 // This conflict is for an entire group. Split it up to
265 // represent each filter
267 // Get the relevant group items
268 model
.groups
[ conflict
.group
].getItems().forEach( function ( groupItem
) {
269 // Rebuild the conflict
270 adjustedConflicts
[ groupItem
.getName() ] = $.extend(
274 filter
: groupItem
.getName(),
282 result
[ key
] = adjustedConflicts
;
294 filterGroups
= OO
.copy( filterGroups
);
296 // Normalize definition from the server
297 filterGroups
.forEach( function ( data
) {
299 // What's this information needs to be normalized
301 body
: data
.whatsThisBody
,
302 header
: data
.whatsThisHeader
,
303 linkText
: data
.whatsThisLinkText
,
304 url
: data
.whatsThisUrl
307 // Title is a msg-key
308 data
.title
= data
.title
? mw
.msg( data
.title
) : data
.name
;
310 // Filters are given to us with msg-keys, we need
311 // to translate those before we hand them off
312 for ( i
= 0; i
< data
.filters
.length
; i
++ ) {
313 data
.filters
[ i
].label
= data
.filters
[ i
].label
? mw
.msg( data
.filters
[ i
].label
) : data
.filters
[ i
].name
;
314 data
.filters
[ i
].description
= data
.filters
[ i
].description
? mw
.msg( data
.filters
[ i
].description
) : '';
319 allViews
= $.extend( true, {
321 title
: mw
.msg( 'rcfilters-filterlist-title' ),
327 $.each( allViews
, function ( viewName
, viewData
) {
329 model
.views
[ viewName
] = {
331 title
: viewData
.title
,
332 trigger
: viewData
.trigger
336 viewData
.groups
.forEach( function ( groupData
) {
337 var group
= groupData
.name
;
339 if ( !model
.groups
[ group
] ) {
340 model
.groups
[ group
] = new mw
.rcfilters
.dm
.FilterGroup(
342 $.extend( true, {}, groupData
, { view
: viewName
} )
346 model
.groups
[ group
].initializeFilters( groupData
.filters
, groupData
.default );
347 items
= items
.concat( model
.groups
[ group
].getItems() );
350 if ( groupData
.conflicts
) {
352 groupConflictMap
[ group
] = groupData
.conflicts
;
355 groupData
.filters
.forEach( function ( itemData
) {
356 var filterItem
= model
.groups
[ group
].getItemByParamName( itemData
.name
);
358 if ( itemData
.conflicts
) {
359 filterConflictMap
[ filterItem
.getName() ] = itemData
.conflicts
;
365 // Add item references to the model, for lookup
366 this.addItems( items
);
369 groupConflictResult
= expandConflictDefinitions( groupConflictMap
);
370 filterConflictResult
= expandConflictDefinitions( filterConflictMap
);
372 // Set conflicts for groups
373 $.each( groupConflictResult
, function ( group
, conflicts
) {
374 model
.groups
[ group
].setConflicts( conflicts
);
377 // Set conflicts for items
378 $.each( filterConflictResult
, function ( filterName
, conflicts
) {
379 var filterItem
= model
.getItemByName( filterName
);
380 // set conflicts for items in the group
381 filterItem
.setConflicts( conflicts
);
384 // Create a map between known parameters and their models
385 $.each( this.groups
, function ( group
, groupModel
) {
387 groupModel
.getType() === 'send_unselected_if_any' ||
388 groupModel
.getType() === 'boolean' ||
389 groupModel
.getType() === 'any_value'
391 // Individual filters
392 groupModel
.getItems().forEach( function ( filterItem
) {
393 model
.parameterMap
[ filterItem
.getParamName() ] = filterItem
;
396 groupModel
.getType() === 'string_options' ||
397 groupModel
.getType() === 'single_option'
400 model
.parameterMap
[ groupModel
.getName() ] = groupModel
;
404 this.currentView
= 'default';
406 this.updateHighlightedState();
408 // Finish initialization
409 this.emit( 'initialize' );
413 * Update filter view model state based on a parameter object
415 * @param {Object} params Parameters object
417 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.updateStateFromParams = function ( params
) {
419 // For arbitrary numeric single_option values make sure the values
420 // are normalized to fit within the limits
421 $.each( this.getFilterGroups(), function ( groupName
, groupModel
) {
422 params
[ groupName
] = groupModel
.normalizeArbitraryValue( params
[ groupName
] );
425 // Update filter values
426 filtersValue
= this.getFiltersFromParameters( params
);
427 Object
.keys( filtersValue
).forEach( function ( filterName
) {
428 this.getItemByName( filterName
).setValue( filtersValue
[ filterName
] );
431 // Update highlight state
432 this.getItemsSupportingHighlights().forEach( function ( filterItem
) {
433 var color
= params
[ filterItem
.getName() + '_color' ];
435 filterItem
.setHighlightColor( color
);
437 filterItem
.clearHighlightColor();
440 this.updateHighlightedState();
442 // Check all filter interactions
443 this.reassessFilterInteractions();
447 * Get a representation of an empty (falsey) parameter state
449 * @return {Object} Empty parameter state
451 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getEmptyParameterState = function () {
452 if ( !this.emptyParameterState
) {
453 this.emptyParameterState
= $.extend(
456 this.getParametersFromFilters( {} ),
457 this.getEmptyHighlightParameters()
460 return this.emptyParameterState
;
464 * Get a representation of only the non-falsey parameters
466 * @param {Object} [parameters] A given parameter state to minimize. If not given the current
467 * state of the system will be used.
468 * @return {Object} Empty parameter state
470 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getMinimizedParamRepresentation = function ( parameters
) {
473 parameters
= parameters
? $.extend( true, {}, parameters
) : this.getCurrentParameterState();
476 $.each( this.getEmptyParameterState(), function ( param
, value
) {
477 if ( parameters
[ param
] !== undefined && parameters
[ param
] !== value
) {
478 result
[ param
] = parameters
[ param
];
483 Object
.keys( this.getEmptyHighlightParameters() ).forEach( function ( param
) {
484 if ( parameters
[ param
] ) {
485 // If a highlight parameter is not undefined and not null
486 // add it to the result
487 result
[ param
] = parameters
[ param
];
495 * Get a representation of the full parameter list, including all base values
497 * @return {Object} Full parameter representation
499 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getExpandedParamRepresentation = function () {
503 this.getEmptyParameterState(),
504 this.getCurrentParameterState()
509 * Get a parameter representation of the current state of the model
511 * @param {boolean} [removeStickyParams] Remove sticky filters from final result
512 * @return {Object} Parameter representation of the current state of the model
514 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getCurrentParameterState = function ( removeStickyParams
) {
515 var state
= this.getMinimizedParamRepresentation( $.extend(
518 this.getParametersFromFilters( this.getSelectedState() ),
519 this.getHighlightParameters()
522 if ( removeStickyParams
) {
523 state
= this.removeStickyParams( state
);
530 * Delete sticky parameters from given object.
532 * @param {Object} paramState Parameter state
533 * @return {Object} Parameter state without sticky parameters
535 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.removeStickyParams = function ( paramState
) {
536 this.getStickyParams().forEach( function ( paramName
) {
537 delete paramState
[ paramName
];
544 * Turn the highlight feature on or off
546 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.updateHighlightedState = function () {
547 this.toggleHighlight( this.getHighlightedItems().length
> 0 );
551 * Get the object that defines groups by their name.
553 * @return {Object} Filter groups
555 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFilterGroups = function () {
560 * Get the object that defines groups that match a certain view by their name.
562 * @param {string} [view] Requested view. If not given, uses current view
563 * @return {Object} Filter groups matching a display group
565 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFilterGroupsByView = function ( view
) {
568 view
= view
|| this.getCurrentView();
570 $.each( this.groups
, function ( groupName
, groupModel
) {
571 if ( groupModel
.getView() === view
) {
572 result
[ groupName
] = groupModel
;
580 * Get an array of filters matching the given display group.
582 * @param {string} [view] Requested view. If not given, uses current view
583 * @return {mw.rcfilters.dm.FilterItem} Filter items matching the group
585 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFiltersByView = function ( view
) {
589 view
= view
|| this.getCurrentView();
591 groups
= this.getFilterGroupsByView( view
);
593 $.each( groups
, function ( groupName
, groupModel
) {
594 result
= result
.concat( groupModel
.getItems() );
601 * Get the trigger for the requested view.
603 * @param {string} view View name
604 * @return {string} View trigger, if exists
606 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getViewTrigger = function ( view
) {
607 return ( this.views
[ view
] && this.views
[ view
].trigger
) || '';
611 * Get the value of a specific parameter
613 * @param {string} name Parameter name
614 * @return {number|string} Parameter value
616 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getParamValue = function ( name
) {
617 return this.parameters
[ name
];
621 * Get the current selected state of the filters
623 * @param {boolean} [onlySelected] return an object containing only the filters with a value
624 * @return {Object} Filters selected state
626 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getSelectedState = function ( onlySelected
) {
628 items
= this.getItems(),
631 for ( i
= 0; i
< items
.length
; i
++ ) {
632 if ( !onlySelected
|| items
[ i
].getValue() ) {
633 result
[ items
[ i
].getName() ] = items
[ i
].getValue();
641 * Get the current full state of the filters
643 * @return {Object} Filters full state
645 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFullState = function () {
647 items
= this.getItems(),
650 for ( i
= 0; i
< items
.length
; i
++ ) {
651 result
[ items
[ i
].getName() ] = {
652 selected
: items
[ i
].isSelected(),
653 conflicted
: items
[ i
].isConflicted(),
654 included
: items
[ i
].isIncluded()
662 * Get an object representing default parameters state
664 * @return {Object} Default parameter values
666 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getDefaultParams = function () {
669 // Get default filter state
670 $.each( this.groups
, function ( name
, model
) {
671 if ( !model
.isSticky() ) {
672 $.extend( true, result
, model
.getDefaultParams() );
680 * Get a parameter representation of all sticky parameters
682 * @return {Object} Sticky parameter values
684 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getStickyParams = function () {
687 $.each( this.groups
, function ( name
, model
) {
688 if ( model
.isSticky() ) {
689 if ( model
.isPerGroupRequestParameter() ) {
692 // Each filter is its own param
693 result
= result
.concat( model
.getItems().map( function ( filterItem
) {
694 return filterItem
.getParamName();
704 * Get a parameter representation of all sticky parameters
706 * @return {Object} Sticky parameter values
708 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getStickyParamsValues = function () {
711 $.each( this.groups
, function ( name
, model
) {
712 if ( model
.isSticky() ) {
713 $.extend( true, result
, model
.getDefaultParams() );
721 * Analyze the groups and their filters and output an object representing
722 * the state of the parameters they represent.
724 * @param {Object} [filterDefinition] An object defining the filter values,
725 * keyed by filter names.
726 * @return {Object} Parameter state object
728 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getParametersFromFilters = function ( filterDefinition
) {
729 var groupItemDefinition
,
731 groupItems
= this.getFilterGroups();
733 if ( filterDefinition
) {
734 groupItemDefinition
= {};
735 // Filter definition is "flat", but in effect
736 // each group needs to tell us its result based
737 // on the values in it. We need to split this list
738 // back into groupings so we can "feed" it to the
739 // loop below, and we need to expand it so it includes
740 // all filters (set to false)
741 this.getItems().forEach( function ( filterItem
) {
742 groupItemDefinition
[ filterItem
.getGroupName() ] = groupItemDefinition
[ filterItem
.getGroupName() ] || {};
743 groupItemDefinition
[ filterItem
.getGroupName() ][ filterItem
.getName() ] = filterItem
.coerceValue( filterDefinition
[ filterItem
.getName() ] );
747 $.each( groupItems
, function ( group
, model
) {
750 model
.getParamRepresentation(
751 groupItemDefinition
?
752 groupItemDefinition
[ group
] : null
761 * This is the opposite of the #getParametersFromFilters method; this goes over
762 * the given parameters and translates into a selected/unselected value in the filters.
764 * @param {Object} params Parameters query object
765 * @return {Object} Filter state object
767 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFiltersFromParameters = function ( params
) {
772 // Go over the given parameters, break apart to groupings
773 // The resulting object represents the group with its parameter
774 // values. For example:
781 // group2: "param4|param5"
783 $.each( params
, function ( paramName
, paramValue
) {
785 itemOrGroup
= model
.parameterMap
[ paramName
];
788 groupName
= itemOrGroup
instanceof mw
.rcfilters
.dm
.FilterItem
?
789 itemOrGroup
.getGroupName() : itemOrGroup
.getName();
791 groupMap
[ groupName
] = groupMap
[ groupName
] || {};
792 groupMap
[ groupName
][ paramName
] = paramValue
;
796 // Go over all groups, so we make sure we get the complete output
797 // even if the parameters don't include a certain group
798 $.each( this.groups
, function ( groupName
, groupModel
) {
799 result
= $.extend( true, {}, result
, groupModel
.getFilterRepresentation( groupMap
[ groupName
] ) );
806 * Get the highlight parameters based on current filter configuration
808 * @return {Object} Object where keys are `<filter name>_color` and values
809 * are the selected highlight colors.
811 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getHighlightParameters = function () {
812 var highlightEnabled
= this.isHighlightEnabled(),
815 this.getItems().forEach( function ( filterItem
) {
816 if ( filterItem
.isHighlightSupported() ) {
817 result
[ filterItem
.getName() + '_color' ] = highlightEnabled
&& filterItem
.isHighlighted() ?
818 filterItem
.getHighlightColor() :
827 * Get an object representing the complete empty state of highlights
829 * @return {Object} Object containing all the highlight parameters set to their negative value
831 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getEmptyHighlightParameters = function () {
834 this.getItems().forEach( function ( filterItem
) {
835 if ( filterItem
.isHighlightSupported() ) {
836 result
[ filterItem
.getName() + '_color' ] = null;
844 * Get an array of currently applied highlight colors
846 * @return {string[]} Currently applied highlight colors
848 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getCurrentlyUsedHighlightColors = function () {
851 if ( this.isHighlightEnabled() ) {
852 this.getHighlightedItems().forEach( function ( filterItem
) {
853 var color
= filterItem
.getHighlightColor();
855 if ( result
.indexOf( color
) === -1 ) {
856 result
.push( color
);
865 * Sanitize value group of a string_option groups type
866 * Remove duplicates and make sure to only use valid
870 * @param {string} groupName Group name
871 * @param {string[]} valueArray Array of values
872 * @return {string[]} Array of valid values
874 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.sanitizeStringOptionGroup = function ( groupName
, valueArray
) {
875 var validNames
= this.getGroupFilters( groupName
).map( function ( filterItem
) {
876 return filterItem
.getParamName();
879 return mw
.rcfilters
.utils
.normalizeParamOptions( valueArray
, validNames
);
883 * Check whether no visible filter is selected.
885 * Filter groups that are hidden or sticky are not shown in the
886 * active filters area and therefore not included in this check.
888 * @return {boolean} No visible filter is selected
890 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.areVisibleFiltersEmpty = function () {
891 // Check if there are either any selected items or any items
892 // that have highlight enabled
893 return !this.getItems().some( function ( filterItem
) {
894 var visible
= !filterItem
.getGroupModel().isSticky() && !filterItem
.getGroupModel().isHidden(),
895 active
= ( filterItem
.isSelected() || filterItem
.isHighlighted() );
896 return visible
&& active
;
901 * Check whether the invert state is a valid one. A valid invert state is one where
902 * there are actual namespaces selected.
904 * This is done to compare states to previous ones that may have had the invert model
905 * selected but effectively had no namespaces, so are not effectively different than
906 * ones where invert is not selected.
908 * @return {boolean} Invert is effectively selected
910 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.areNamespacesEffectivelyInverted = function () {
911 return this.getInvertModel().isSelected() &&
912 this.getSelectedItems().some( function ( itemModel
) {
913 return itemModel
.getGroupModel().getView() === 'namespace';
918 * Get the item that matches the given name
920 * @param {string} name Filter name
921 * @return {mw.rcfilters.dm.FilterItem} Filter item
923 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getItemByName = function ( name
) {
924 return this.getItems().filter( function ( item
) {
925 return name
=== item
.getName();
930 * Set all filters to false or empty/all
931 * This is equivalent to display all.
933 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.emptyAllFilters = function () {
934 this.getItems().forEach( function ( filterItem
) {
935 if ( !filterItem
.getGroupModel().isSticky() ) {
936 this.toggleFilterSelected( filterItem
.getName(), false );
942 * Toggle selected state of one item
944 * @param {string} name Name of the filter item
945 * @param {boolean} [isSelected] Filter selected state
947 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.toggleFilterSelected = function ( name
, isSelected
) {
948 var item
= this.getItemByName( name
);
951 item
.toggleSelected( isSelected
);
956 * Toggle selected state of items by their names
958 * @param {Object} filterDef Filter definitions
960 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.toggleFiltersSelected = function ( filterDef
) {
961 Object
.keys( filterDef
).forEach( function ( name
) {
962 this.toggleFilterSelected( name
, filterDef
[ name
] );
967 * Get a group model from its name
969 * @param {string} groupName Group name
970 * @return {mw.rcfilters.dm.FilterGroup} Group model
972 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getGroup = function ( groupName
) {
973 return this.groups
[ groupName
];
977 * Get all filters within a specified group by its name
979 * @param {string} groupName Group name
980 * @return {mw.rcfilters.dm.FilterItem[]} Filters belonging to this group
982 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getGroupFilters = function ( groupName
) {
983 return ( this.getGroup( groupName
) && this.getGroup( groupName
).getItems() ) || [];
987 * Find items whose labels match the given string
989 * @param {string} query Search string
990 * @param {boolean} [returnFlat] Return a flat array. If false, the result
991 * is an object whose keys are the group names and values are an array of
992 * filters per group. If set to true, returns an array of filters regardless
994 * @return {Object} An object of items to show
995 * arranged by their group names
997 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.findMatches = function ( query
, returnFlat
) {
998 var i
, searchIsEmpty
,
1002 view
= this.getViewByTrigger( query
.substr( 0, 1 ) ),
1003 items
= this.getFiltersByView( view
);
1005 // Normalize so we can search strings regardless of case and view
1006 query
= query
.trim().toLowerCase();
1007 if ( view
!== 'default' ) {
1008 query
= query
.substr( 1 );
1010 // Trim again to also intercept cases where the spaces were after the trigger
1012 query
= query
.trim();
1014 // Check if the search if actually empty; this can be a problem when
1015 // we use prefixes to denote different views
1016 searchIsEmpty
= query
.length
=== 0;
1018 // item label starting with the query string
1019 for ( i
= 0; i
< items
.length
; i
++ ) {
1022 items
[ i
].getLabel().toLowerCase().indexOf( query
) === 0 ||
1024 // For tags, we want the parameter name to be included in the search
1026 items
[ i
].getParamName().toLowerCase().indexOf( query
) > -1
1029 result
[ items
[ i
].getGroupName() ] = result
[ items
[ i
].getGroupName() ] || [];
1030 result
[ items
[ i
].getGroupName() ].push( items
[ i
] );
1031 flatResult
.push( items
[ i
] );
1035 if ( $.isEmptyObject( result
) ) {
1036 // item containing the query string in their label, description, or group title
1037 for ( i
= 0; i
< items
.length
; i
++ ) {
1038 groupTitle
= items
[ i
].getGroupModel().getTitle();
1041 items
[ i
].getLabel().toLowerCase().indexOf( query
) > -1 ||
1042 items
[ i
].getDescription().toLowerCase().indexOf( query
) > -1 ||
1043 groupTitle
.toLowerCase().indexOf( query
) > -1 ||
1045 // For tags, we want the parameter name to be included in the search
1047 items
[ i
].getParamName().toLowerCase().indexOf( query
) > -1
1050 result
[ items
[ i
].getGroupName() ] = result
[ items
[ i
].getGroupName() ] || [];
1051 result
[ items
[ i
].getGroupName() ].push( items
[ i
] );
1052 flatResult
.push( items
[ i
] );
1057 return returnFlat
? flatResult
: result
;
1061 * Get items that are highlighted
1063 * @return {mw.rcfilters.dm.FilterItem[]} Highlighted items
1065 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getHighlightedItems = function () {
1066 return this.getItems().filter( function ( filterItem
) {
1067 return filterItem
.isHighlightSupported() &&
1068 filterItem
.getHighlightColor();
1073 * Get items that allow highlights even if they're not currently highlighted
1075 * @return {mw.rcfilters.dm.FilterItem[]} Items supporting highlights
1077 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getItemsSupportingHighlights = function () {
1078 return this.getItems().filter( function ( filterItem
) {
1079 return filterItem
.isHighlightSupported();
1084 * Get all selected items
1086 * @return {mw.rcfilters.dm.FilterItem[]} Selected items
1088 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getSelectedItems = function () {
1089 var allSelected
= [];
1091 $.each( this.getFilterGroups(), function ( groupName
, groupModel
) {
1092 allSelected
= allSelected
.concat( groupModel
.getSelectedItems() );
1099 * Switch the current view
1101 * @param {string} view View name
1104 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.switchView = function ( view
) {
1105 if ( this.views
[ view
] && this.currentView
!== view
) {
1106 this.currentView
= view
;
1107 this.emit( 'update' );
1112 * Get the current view
1114 * @return {string} Current view
1116 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getCurrentView = function () {
1117 return this.currentView
;
1121 * Get the label for the current view
1123 * @param {string} viewName View name
1124 * @return {string} Label for the current view
1126 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getViewTitle = function ( viewName
) {
1127 viewName
= viewName
|| this.getCurrentView();
1129 return this.views
[ viewName
] && this.views
[ viewName
].title
;
1133 * Get the view that fits the given trigger
1135 * @param {string} trigger Trigger
1136 * @return {string} Name of view
1138 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getViewByTrigger = function ( trigger
) {
1139 var result
= 'default';
1141 $.each( this.views
, function ( name
, data
) {
1142 if ( data
.trigger
=== trigger
) {
1151 * Toggle the highlight feature on and off.
1152 * Propagate the change to filter items.
1154 * @param {boolean} enable Highlight should be enabled
1155 * @fires highlightChange
1157 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.toggleHighlight = function ( enable
) {
1158 enable
= enable
=== undefined ? !this.highlightEnabled
: enable
;
1160 if ( this.highlightEnabled
!== enable
) {
1161 this.highlightEnabled
= enable
;
1162 this.emit( 'highlightChange', this.highlightEnabled
);
1167 * Check if the highlight feature is enabled
1170 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.isHighlightEnabled = function () {
1171 return !!this.highlightEnabled
;
1175 * Toggle the inverted namespaces property on and off.
1176 * Propagate the change to namespace filter items.
1178 * @param {boolean} enable Inverted property is enabled
1180 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.toggleInvertedNamespaces = function ( enable
) {
1181 this.toggleFilterSelected( this.getInvertModel().getName(), enable
);
1185 * Get the model object that represents the 'invert' filter
1187 * @return {mw.rcfilters.dm.FilterItem}
1189 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getInvertModel = function () {
1190 return this.getGroup( 'invertGroup' ).getItemByParamName( 'invert' );
1194 * Set highlight color for a specific filter item
1196 * @param {string} filterName Name of the filter item
1197 * @param {string} color Selected color
1199 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.setHighlightColor = function ( filterName
, color
) {
1200 this.getItemByName( filterName
).setHighlightColor( color
);
1204 * Clear highlight for a specific filter item
1206 * @param {string} filterName Name of the filter item
1208 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.clearHighlightColor = function ( filterName
) {
1209 this.getItemByName( filterName
).clearHighlightColor();
1213 * Return a version of the given string that is without any
1216 * @param {string} str Given string
1217 * @return {string} Result
1219 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.removeViewTriggers = function ( str
) {
1220 if ( this.getViewByTrigger( str
.substr( 0, 1 ) ) !== 'default' ) {
1221 str
= str
.substr( 1 );
1226 }( mediaWiki
, jQuery
) );