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'
390 // Individual filters
391 groupModel
.getItems().forEach( function ( filterItem
) {
392 model
.parameterMap
[ filterItem
.getParamName() ] = filterItem
;
395 groupModel
.getType() === 'string_options' ||
396 groupModel
.getType() === 'single_option'
399 model
.parameterMap
[ groupModel
.getName() ] = groupModel
;
403 this.currentView
= 'default';
405 this.updateHighlightedState();
407 // Finish initialization
408 this.emit( 'initialize' );
412 * Update filter view model state based on a parameter object
414 * @param {Object} params Parameters object
416 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.updateStateFromParams = function ( params
) {
417 // For arbitrary numeric single_option values make sure the values
418 // are normalized to fit within the limits
419 $.each( this.getFilterGroups(), function ( groupName
, groupModel
) {
420 params
[ groupName
] = groupModel
.normalizeArbitraryValue( params
[ groupName
] );
423 // Update filter states
424 this.toggleFiltersSelected(
425 this.getFiltersFromParameters(
430 // Update highlight state
431 this.getItemsSupportingHighlights().forEach( function ( filterItem
) {
432 var color
= params
[ filterItem
.getName() + '_color' ];
434 filterItem
.setHighlightColor( color
);
436 filterItem
.clearHighlightColor();
439 this.updateHighlightedState();
441 // Check all filter interactions
442 this.reassessFilterInteractions();
446 * Get a representation of an empty (falsey) parameter state
448 * @return {Object} Empty parameter state
450 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getEmptyParameterState = function () {
451 if ( !this.emptyParameterState
) {
452 this.emptyParameterState
= $.extend(
455 this.getParametersFromFilters( {} ),
456 this.getEmptyHighlightParameters()
459 return this.emptyParameterState
;
463 * Get a representation of only the non-falsey parameters
465 * @param {Object} [parameters] A given parameter state to minimize. If not given the current
466 * state of the system will be used.
467 * @return {Object} Empty parameter state
469 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getMinimizedParamRepresentation = function ( parameters
) {
472 parameters
= parameters
? $.extend( true, {}, parameters
) : this.getCurrentParameterState();
475 $.each( this.getEmptyParameterState(), function ( param
, value
) {
476 if ( parameters
[ param
] !== undefined && parameters
[ param
] !== value
) {
477 result
[ param
] = parameters
[ param
];
482 Object
.keys( this.getEmptyHighlightParameters() ).forEach( function ( param
) {
483 if ( parameters
[ param
] ) {
484 // If a highlight parameter is not undefined and not null
485 // add it to the result
486 result
[ param
] = parameters
[ param
];
494 * Get a representation of the full parameter list, including all base values
496 * @return {Object} Full parameter representation
498 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getExpandedParamRepresentation = function () {
502 this.getEmptyParameterState(),
503 this.getCurrentParameterState()
508 * Get a parameter representation of the current state of the model
510 * @param {boolean} [removeStickyParams] Remove sticky filters from final result
511 * @return {Object} Parameter representation of the current state of the model
513 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getCurrentParameterState = function ( removeStickyParams
) {
514 var state
= this.getMinimizedParamRepresentation( $.extend(
517 this.getParametersFromFilters( this.getSelectedState() ),
518 this.getHighlightParameters()
521 if ( removeStickyParams
) {
522 state
= this.removeStickyParams( state
);
529 * Delete sticky parameters from given object.
531 * @param {Object} paramState Parameter state
532 * @return {Object} Parameter state without sticky parameters
534 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.removeStickyParams = function ( paramState
) {
535 this.getStickyParams().forEach( function ( paramName
) {
536 delete paramState
[ paramName
];
543 * Turn the highlight feature on or off
545 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.updateHighlightedState = function () {
546 this.toggleHighlight( this.getHighlightedItems().length
> 0 );
550 * Get the object that defines groups by their name.
552 * @return {Object} Filter groups
554 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFilterGroups = function () {
559 * Get the object that defines groups that match a certain view by their name.
561 * @param {string} [view] Requested view. If not given, uses current view
562 * @return {Object} Filter groups matching a display group
564 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFilterGroupsByView = function ( view
) {
567 view
= view
|| this.getCurrentView();
569 $.each( this.groups
, function ( groupName
, groupModel
) {
570 if ( groupModel
.getView() === view
) {
571 result
[ groupName
] = groupModel
;
579 * Get an array of filters matching the given display group.
581 * @param {string} [view] Requested view. If not given, uses current view
582 * @return {mw.rcfilters.dm.FilterItem} Filter items matching the group
584 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFiltersByView = function ( view
) {
588 view
= view
|| this.getCurrentView();
590 groups
= this.getFilterGroupsByView( view
);
592 $.each( groups
, function ( groupName
, groupModel
) {
593 result
= result
.concat( groupModel
.getItems() );
600 * Get the trigger for the requested view.
602 * @param {string} view View name
603 * @return {string} View trigger, if exists
605 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getViewTrigger = function ( view
) {
606 return ( this.views
[ view
] && this.views
[ view
].trigger
) || '';
610 * Get the value of a specific parameter
612 * @param {string} name Parameter name
613 * @return {number|string} Parameter value
615 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getParamValue = function ( name
) {
616 return this.parameters
[ name
];
620 * Get the current selected state of the filters
622 * @param {boolean} onlySelected return an object containing only the selected filters
623 * @return {Object} Filters selected state
625 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getSelectedState = function ( onlySelected
) {
627 items
= this.getItems(),
630 for ( i
= 0; i
< items
.length
; i
++ ) {
631 if ( !onlySelected
|| items
[ i
].isSelected() ) {
632 result
[ items
[ i
].getName() ] = items
[ i
].isSelected();
640 * Get the current full state of the filters
642 * @return {Object} Filters full state
644 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFullState = function () {
646 items
= this.getItems(),
649 for ( i
= 0; i
< items
.length
; i
++ ) {
650 result
[ items
[ i
].getName() ] = {
651 selected
: items
[ i
].isSelected(),
652 conflicted
: items
[ i
].isConflicted(),
653 included
: items
[ i
].isIncluded()
661 * Get an object representing default parameters state
663 * @return {Object} Default parameter values
665 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getDefaultParams = function () {
668 // Get default filter state
669 $.each( this.groups
, function ( name
, model
) {
670 if ( !model
.isSticky() ) {
671 $.extend( true, result
, model
.getDefaultParams() );
679 * Get a parameter representation of all sticky parameters
681 * @return {Object} Sticky parameter values
683 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getStickyParams = function () {
686 $.each( this.groups
, function ( name
, model
) {
687 if ( model
.isSticky() ) {
688 if ( model
.isPerGroupRequestParameter() ) {
691 // Each filter is its own param
692 result
= result
.concat( model
.getItems().map( function ( filterItem
) {
693 return filterItem
.getParamName();
703 * Get a parameter representation of all sticky parameters
705 * @return {Object} Sticky parameter values
707 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getStickyParamsValues = function () {
710 $.each( this.groups
, function ( name
, model
) {
711 if ( model
.isSticky() ) {
712 $.extend( true, result
, model
.getDefaultParams() );
720 * Analyze the groups and their filters and output an object representing
721 * the state of the parameters they represent.
723 * @param {Object} [filterDefinition] An object defining the filter values,
724 * keyed by filter names.
725 * @return {Object} Parameter state object
727 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getParametersFromFilters = function ( filterDefinition
) {
728 var groupItemDefinition
,
730 groupItems
= this.getFilterGroups();
732 if ( filterDefinition
) {
733 groupItemDefinition
= {};
734 // Filter definition is "flat", but in effect
735 // each group needs to tell us its result based
736 // on the values in it. We need to split this list
737 // back into groupings so we can "feed" it to the
738 // loop below, and we need to expand it so it includes
739 // all filters (set to false)
740 this.getItems().forEach( function ( filterItem
) {
741 groupItemDefinition
[ filterItem
.getGroupName() ] = groupItemDefinition
[ filterItem
.getGroupName() ] || {};
742 groupItemDefinition
[ filterItem
.getGroupName() ][ filterItem
.getName() ] = !!filterDefinition
[ filterItem
.getName() ];
746 $.each( groupItems
, function ( group
, model
) {
749 model
.getParamRepresentation(
750 groupItemDefinition
?
751 groupItemDefinition
[ group
] : null
760 * This is the opposite of the #getParametersFromFilters method; this goes over
761 * the given parameters and translates into a selected/unselected value in the filters.
763 * @param {Object} params Parameters query object
764 * @return {Object} Filter state object
766 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFiltersFromParameters = function ( params
) {
771 // Go over the given parameters, break apart to groupings
772 // The resulting object represents the group with its parameter
773 // values. For example:
780 // group2: "param4|param5"
782 $.each( params
, function ( paramName
, paramValue
) {
784 itemOrGroup
= model
.parameterMap
[ paramName
];
787 groupName
= itemOrGroup
instanceof mw
.rcfilters
.dm
.FilterItem
?
788 itemOrGroup
.getGroupName() : itemOrGroup
.getName();
790 groupMap
[ groupName
] = groupMap
[ groupName
] || {};
791 groupMap
[ groupName
][ paramName
] = paramValue
;
795 // Go over all groups, so we make sure we get the complete output
796 // even if the parameters don't include a certain group
797 $.each( this.groups
, function ( groupName
, groupModel
) {
798 result
= $.extend( true, {}, result
, groupModel
.getFilterRepresentation( groupMap
[ groupName
] ) );
805 * Get the highlight parameters based on current filter configuration
807 * @return {Object} Object where keys are `<filter name>_color` and values
808 * are the selected highlight colors.
810 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getHighlightParameters = function () {
811 var highlightEnabled
= this.isHighlightEnabled(),
814 this.getItems().forEach( function ( filterItem
) {
815 if ( filterItem
.isHighlightSupported() ) {
816 result
[ filterItem
.getName() + '_color' ] = highlightEnabled
&& filterItem
.isHighlighted() ?
817 filterItem
.getHighlightColor() :
826 * Get an object representing the complete empty state of highlights
828 * @return {Object} Object containing all the highlight parameters set to their negative value
830 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getEmptyHighlightParameters = function () {
833 this.getItems().forEach( function ( filterItem
) {
834 if ( filterItem
.isHighlightSupported() ) {
835 result
[ filterItem
.getName() + '_color' ] = null;
843 * Get an array of currently applied highlight colors
845 * @return {string[]} Currently applied highlight colors
847 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getCurrentlyUsedHighlightColors = function () {
850 if ( this.isHighlightEnabled() ) {
851 this.getHighlightedItems().forEach( function ( filterItem
) {
852 var color
= filterItem
.getHighlightColor();
854 if ( result
.indexOf( color
) === -1 ) {
855 result
.push( color
);
864 * Sanitize value group of a string_option groups type
865 * Remove duplicates and make sure to only use valid
869 * @param {string} groupName Group name
870 * @param {string[]} valueArray Array of values
871 * @return {string[]} Array of valid values
873 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.sanitizeStringOptionGroup = function ( groupName
, valueArray
) {
874 var validNames
= this.getGroupFilters( groupName
).map( function ( filterItem
) {
875 return filterItem
.getParamName();
878 return mw
.rcfilters
.utils
.normalizeParamOptions( valueArray
, validNames
);
882 * Check whether no visible filter is selected.
884 * Filter groups that are hidden or sticky are not shown in the
885 * active filters area and therefore not included in this check.
887 * @return {boolean} No visible filter is selected
889 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.areVisibleFiltersEmpty = function () {
890 // Check if there are either any selected items or any items
891 // that have highlight enabled
892 return !this.getItems().some( function ( filterItem
) {
893 var visible
= !filterItem
.getGroupModel().isSticky() && !filterItem
.getGroupModel().isHidden(),
894 active
= ( filterItem
.isSelected() || filterItem
.isHighlighted() );
895 return visible
&& active
;
900 * Check whether the invert state is a valid one. A valid invert state is one where
901 * there are actual namespaces selected.
903 * This is done to compare states to previous ones that may have had the invert model
904 * selected but effectively had no namespaces, so are not effectively different than
905 * ones where invert is not selected.
907 * @return {boolean} Invert is effectively selected
909 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.areNamespacesEffectivelyInverted = function () {
910 return this.getInvertModel().isSelected() &&
911 this.getSelectedItems().some( function ( itemModel
) {
912 return itemModel
.getGroupModel().getView() === 'namespace';
917 * Get the item that matches the given name
919 * @param {string} name Filter name
920 * @return {mw.rcfilters.dm.FilterItem} Filter item
922 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getItemByName = function ( name
) {
923 return this.getItems().filter( function ( item
) {
924 return name
=== item
.getName();
929 * Set all filters to false or empty/all
930 * This is equivalent to display all.
932 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.emptyAllFilters = function () {
933 this.getItems().forEach( function ( filterItem
) {
934 if ( !filterItem
.getGroupModel().isSticky() ) {
935 this.toggleFilterSelected( filterItem
.getName(), false );
941 * Toggle selected state of one item
943 * @param {string} name Name of the filter item
944 * @param {boolean} [isSelected] Filter selected state
946 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.toggleFilterSelected = function ( name
, isSelected
) {
947 var item
= this.getItemByName( name
);
950 item
.toggleSelected( isSelected
);
955 * Toggle selected state of items by their names
957 * @param {Object} filterDef Filter definitions
959 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.toggleFiltersSelected = function ( filterDef
) {
960 Object
.keys( filterDef
).forEach( function ( name
) {
961 this.toggleFilterSelected( name
, filterDef
[ name
] );
966 * Get a group model from its name
968 * @param {string} groupName Group name
969 * @return {mw.rcfilters.dm.FilterGroup} Group model
971 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getGroup = function ( groupName
) {
972 return this.groups
[ groupName
];
976 * Get all filters within a specified group by its name
978 * @param {string} groupName Group name
979 * @return {mw.rcfilters.dm.FilterItem[]} Filters belonging to this group
981 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getGroupFilters = function ( groupName
) {
982 return ( this.getGroup( groupName
) && this.getGroup( groupName
).getItems() ) || [];
986 * Find items whose labels match the given string
988 * @param {string} query Search string
989 * @param {boolean} [returnFlat] Return a flat array. If false, the result
990 * is an object whose keys are the group names and values are an array of
991 * filters per group. If set to true, returns an array of filters regardless
993 * @return {Object} An object of items to show
994 * arranged by their group names
996 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.findMatches = function ( query
, returnFlat
) {
997 var i
, searchIsEmpty
,
1001 view
= this.getViewByTrigger( query
.substr( 0, 1 ) ),
1002 items
= this.getFiltersByView( view
);
1004 // Normalize so we can search strings regardless of case and view
1005 query
= query
.trim().toLowerCase();
1006 if ( view
!== 'default' ) {
1007 query
= query
.substr( 1 );
1009 // Trim again to also intercept cases where the spaces were after the trigger
1011 query
= query
.trim();
1013 // Check if the search if actually empty; this can be a problem when
1014 // we use prefixes to denote different views
1015 searchIsEmpty
= query
.length
=== 0;
1017 // item label starting with the query string
1018 for ( i
= 0; i
< items
.length
; i
++ ) {
1021 items
[ i
].getLabel().toLowerCase().indexOf( query
) === 0 ||
1023 // For tags, we want the parameter name to be included in the search
1025 items
[ i
].getParamName().toLowerCase().indexOf( query
) > -1
1028 result
[ items
[ i
].getGroupName() ] = result
[ items
[ i
].getGroupName() ] || [];
1029 result
[ items
[ i
].getGroupName() ].push( items
[ i
] );
1030 flatResult
.push( items
[ i
] );
1034 if ( $.isEmptyObject( result
) ) {
1035 // item containing the query string in their label, description, or group title
1036 for ( i
= 0; i
< items
.length
; i
++ ) {
1037 groupTitle
= items
[ i
].getGroupModel().getTitle();
1040 items
[ i
].getLabel().toLowerCase().indexOf( query
) > -1 ||
1041 items
[ i
].getDescription().toLowerCase().indexOf( query
) > -1 ||
1042 groupTitle
.toLowerCase().indexOf( query
) > -1 ||
1044 // For tags, we want the parameter name to be included in the search
1046 items
[ i
].getParamName().toLowerCase().indexOf( query
) > -1
1049 result
[ items
[ i
].getGroupName() ] = result
[ items
[ i
].getGroupName() ] || [];
1050 result
[ items
[ i
].getGroupName() ].push( items
[ i
] );
1051 flatResult
.push( items
[ i
] );
1056 return returnFlat
? flatResult
: result
;
1060 * Get items that are highlighted
1062 * @return {mw.rcfilters.dm.FilterItem[]} Highlighted items
1064 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getHighlightedItems = function () {
1065 return this.getItems().filter( function ( filterItem
) {
1066 return filterItem
.isHighlightSupported() &&
1067 filterItem
.getHighlightColor();
1072 * Get items that allow highlights even if they're not currently highlighted
1074 * @return {mw.rcfilters.dm.FilterItem[]} Items supporting highlights
1076 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getItemsSupportingHighlights = function () {
1077 return this.getItems().filter( function ( filterItem
) {
1078 return filterItem
.isHighlightSupported();
1083 * Get all selected items
1085 * @return {mw.rcfilters.dm.FilterItem[]} Selected items
1087 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getSelectedItems = function () {
1088 var allSelected
= [];
1090 $.each( this.getFilterGroups(), function ( groupName
, groupModel
) {
1091 allSelected
= allSelected
.concat( groupModel
.getSelectedItems() );
1098 * Switch the current view
1100 * @param {string} view View name
1103 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.switchView = function ( view
) {
1104 if ( this.views
[ view
] && this.currentView
!== view
) {
1105 this.currentView
= view
;
1106 this.emit( 'update' );
1111 * Get the current view
1113 * @return {string} Current view
1115 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getCurrentView = function () {
1116 return this.currentView
;
1120 * Get the label for the current view
1122 * @param {string} viewName View name
1123 * @return {string} Label for the current view
1125 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getViewTitle = function ( viewName
) {
1126 viewName
= viewName
|| this.getCurrentView();
1128 return this.views
[ viewName
] && this.views
[ viewName
].title
;
1132 * Get the view that fits the given trigger
1134 * @param {string} trigger Trigger
1135 * @return {string} Name of view
1137 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getViewByTrigger = function ( trigger
) {
1138 var result
= 'default';
1140 $.each( this.views
, function ( name
, data
) {
1141 if ( data
.trigger
=== trigger
) {
1150 * Toggle the highlight feature on and off.
1151 * Propagate the change to filter items.
1153 * @param {boolean} enable Highlight should be enabled
1154 * @fires highlightChange
1156 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.toggleHighlight = function ( enable
) {
1157 enable
= enable
=== undefined ? !this.highlightEnabled
: enable
;
1159 if ( this.highlightEnabled
!== enable
) {
1160 this.highlightEnabled
= enable
;
1161 this.emit( 'highlightChange', this.highlightEnabled
);
1166 * Check if the highlight feature is enabled
1169 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.isHighlightEnabled = function () {
1170 return !!this.highlightEnabled
;
1174 * Toggle the inverted namespaces property on and off.
1175 * Propagate the change to namespace filter items.
1177 * @param {boolean} enable Inverted property is enabled
1179 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.toggleInvertedNamespaces = function ( enable
) {
1180 this.toggleFilterSelected( this.getInvertModel().getName(), enable
);
1184 * Get the model object that represents the 'invert' filter
1186 * @return {mw.rcfilters.dm.FilterItem}
1188 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getInvertModel = function () {
1189 return this.getGroup( 'invertGroup' ).getItemByParamName( 'invert' );
1193 * Set highlight color for a specific filter item
1195 * @param {string} filterName Name of the filter item
1196 * @param {string} color Selected color
1198 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.setHighlightColor = function ( filterName
, color
) {
1199 this.getItemByName( filterName
).setHighlightColor( color
);
1203 * Clear highlight for a specific filter item
1205 * @param {string} filterName Name of the filter item
1207 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.clearHighlightColor = function ( filterName
) {
1208 this.getItemByName( filterName
).clearHighlightColor();
1212 * Return a version of the given string that is without any
1215 * @param {string} str Given string
1216 * @return {string} Result
1218 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.removeViewTriggers = function ( str
) {
1219 if ( this.getViewByTrigger( str
.substr( 0, 1 ) ) !== 'default' ) {
1220 str
= str
.substr( 1 );
1225 }( mediaWiki
, jQuery
) );