3a6efe2b1dd5643f7ed7ecef85750748bb6cf9f3
3 * View model for the filters selection and display
5 * @mixins OO.EventEmitter
6 * @mixins OO.EmitterList
10 mw
.rcfilters
.dm
.FiltersViewModel
= function MwRcfiltersDmFiltersViewModel() {
12 OO
.EventEmitter
.call( this );
13 OO
.EmitterList
.call( this );
16 this.defaultParams
= {};
17 this.defaultFiltersEmpty
= null;
18 this.highlightEnabled
= false;
19 this.parameterMap
= {};
20 this.emptyParameterState
= null;
23 this.currentView
= 'default';
26 this.aggregate( { update
: 'filterItemUpdate' } );
27 this.connect( this, { filterItemUpdate
: [ 'emit', 'itemUpdate' ] } );
31 OO
.initClass( mw
.rcfilters
.dm
.FiltersViewModel
);
32 OO
.mixinClass( mw
.rcfilters
.dm
.FiltersViewModel
, OO
.EventEmitter
);
33 OO
.mixinClass( mw
.rcfilters
.dm
.FiltersViewModel
, OO
.EmitterList
);
40 * Filter list is initialized
46 * Model has been updated
51 * @param {mw.rcfilters.dm.FilterItem} item Filter item updated
53 * Filter item has changed
57 * @event highlightChange
58 * @param {boolean} Highlight feature is enabled
60 * Highlight feature has been toggled enabled or disabled
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'
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
) {
418 // For arbitrary numeric single_option values make sure the values
419 // are normalized to fit within the limits
420 $.each( this.getFilterGroups(), function ( groupName
, groupModel
) {
421 params
[ groupName
] = groupModel
.normalizeArbitraryValue( params
[ groupName
] );
424 // Update filter states
425 this.toggleFiltersSelected(
426 this.getFiltersFromParameters(
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 * @param {Object} [parameters] A given parameter state to minimize. If not given the current
498 * state of the system will be used.
499 * @param {boolean} [removeExcluded] Remove excluded and sticky parameters
500 * @return {Object} Full parameter representation
502 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getExpandedParamRepresentation = function ( parameters
, removeExcluded
) {
505 parameters
= parameters
? $.extend( true, {}, parameters
) : this.getCurrentParameterState();
510 this.getEmptyParameterState(),
514 if ( removeExcluded
) {
515 result
= this.removeExcludedParams( result
);
522 * Get a parameter representation of the current state of the model
524 * @param {boolean} [removeExcludedParams] Remove excluded filters from final result
525 * @return {Object} Parameter representation of the current state of the model
527 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getCurrentParameterState = function ( removeExcludedParams
) {
529 state
= this.getMinimizedParamRepresentation( $.extend(
532 this.getParametersFromFilters( this.getSelectedState() ),
533 this.getHighlightParameters()
536 if ( removeExcludedParams
) {
537 excludedParams
= this.getExcludedParams();
538 // Delete all excluded filters
539 $.each( state
, function ( param
) {
540 if ( excludedParams
.indexOf( param
) > -1 ) {
541 delete state
[ param
];
550 * Delete excluded and sticky filters from given object. If object isn't given, output
551 * the current filter state without the excluded values
553 * @param {Object} [filterState] Filter state
554 * @return {Object} Filter state without excluded filters
556 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.removeExcludedFilters = function ( filterState
) {
557 filterState
= filterState
!== undefined ?
558 $.extend( true, {}, filterState
) :
559 this.getFiltersFromParameters();
561 // Remove excluded filters
562 Object
.keys( this.getExcludedFiltersState() ).forEach( function ( filterName
) {
563 delete filterState
[ filterName
];
566 // Remove sticky filters
567 Object
.keys( this.getStickyFiltersState() ).forEach( function ( filterName
) {
568 delete filterState
[ filterName
];
574 * Delete excluded and sticky parameters from given object. If object isn't given, output
575 * the current param state without the excluded values
577 * @param {Object} [paramState] Parameter state
578 * @return {Object} Parameter state without excluded filters
580 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.removeExcludedParams = function ( paramState
) {
581 paramState
= paramState
!== undefined ?
582 $.extend( true, {}, paramState
) :
583 this.getCurrentParameterState();
585 // Remove excluded filters
586 this.getExcludedParams().forEach( function ( paramName
) {
587 delete paramState
[ paramName
];
590 // Remove sticky filters
591 this.getStickyParams().forEach( function ( paramName
) {
592 delete paramState
[ paramName
];
599 * Get the names of all available filters
601 * @return {string[]} An array of filter names
603 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFilterNames = function () {
604 return this.getItems().map( function ( item
) { return item
.getName(); } );
608 * Turn the highlight feature on or off
610 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.updateHighlightedState = function () {
611 this.toggleHighlight( this.getHighlightedItems().length
> 0 );
615 * Get the object that defines groups by their name.
617 * @return {Object} Filter groups
619 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFilterGroups = function () {
624 * Get the object that defines groups that match a certain view by their name.
626 * @param {string} [view] Requested view. If not given, uses current view
627 * @return {Object} Filter groups matching a display group
629 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFilterGroupsByView = function ( view
) {
632 view
= view
|| this.getCurrentView();
634 $.each( this.groups
, function ( groupName
, groupModel
) {
635 if ( groupModel
.getView() === view
) {
636 result
[ groupName
] = groupModel
;
644 * Get an array of filters matching the given display group.
646 * @param {string} [view] Requested view. If not given, uses current view
647 * @return {mw.rcfilters.dm.FilterItem} Filter items matching the group
649 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFiltersByView = function ( view
) {
653 view
= view
|| this.getCurrentView();
655 groups
= this.getFilterGroupsByView( view
);
657 $.each( groups
, function ( groupName
, groupModel
) {
658 result
= result
.concat( groupModel
.getItems() );
665 * Get the trigger for the requested view.
667 * @param {string} view View name
668 * @return {string} View trigger, if exists
670 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getViewTrigger = function ( view
) {
671 return ( this.views
[ view
] && this.views
[ view
].trigger
) || '';
674 * Get the value of a specific parameter
676 * @param {string} name Parameter name
677 * @return {number|string} Parameter value
679 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getParamValue = function ( name
) {
680 return this.parameters
[ name
];
684 * Get the current selected state of the filters
686 * @return {Object} Filters selected state
688 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getSelectedState = function () {
690 items
= this.getItems(),
693 for ( i
= 0; i
< items
.length
; i
++ ) {
694 result
[ items
[ i
].getName() ] = items
[ i
].isSelected();
701 * Get the current full state of the filters
703 * @return {Object} Filters full state
705 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFullState = function () {
707 items
= this.getItems(),
710 for ( i
= 0; i
< items
.length
; i
++ ) {
711 result
[ items
[ i
].getName() ] = {
712 selected
: items
[ i
].isSelected(),
713 conflicted
: items
[ i
].isConflicted(),
714 included
: items
[ i
].isIncluded()
722 * Get an object representing default parameters state
724 * @param {boolean} [excludeHiddenParams] Exclude hidden and sticky params
725 * @return {Object} Default parameter values
727 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getDefaultParams = function ( excludeHiddenParams
) {
730 // Get default filter state
731 $.each( this.groups
, function ( name
, model
) {
732 $.extend( true, result
, model
.getDefaultParams() );
735 if ( excludeHiddenParams
) {
736 Object
.keys( this.getDefaultHiddenParams() ).forEach( function ( paramName
) {
737 delete result
[ paramName
];
745 * Get an object representing defaults for the hidden parameters state
747 * @return {Object} Default values for hidden parameters
749 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getDefaultHiddenParams = function () {
752 // Get default filter state
753 $.each( this.groups
, function ( name
, model
) {
754 if ( model
.isHidden() ) {
755 $.extend( true, result
, model
.getDefaultParams() );
763 * Get a parameter representation of all sticky parameters
765 * @return {Object} Sticky parameter values
767 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getStickyParams = function () {
770 $.each( this.groups
, function ( name
, model
) {
771 if ( model
.isSticky() ) {
772 if ( model
.isPerGroupRequestParameter() ) {
775 // Each filter is its own param
776 result
= result
.concat( model
.getItems().map( function ( filterItem
) {
777 return filterItem
.getParamName();
787 * Get a parameter representation of all sticky parameters
789 * @return {Object} Sticky parameter values
791 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getStickyParamsValues = function () {
794 $.each( this.groups
, function ( name
, model
) {
795 if ( model
.isSticky() ) {
796 $.extend( true, result
, model
.getDefaultParams() );
804 * Get a filter representation of all sticky parameters
806 * @return {Object} Sticky filters values
808 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getStickyFiltersState = function () {
811 $.each( this.groups
, function ( name
, model
) {
812 if ( model
.isSticky() ) {
813 $.extend( true, result
, model
.getSelectedState() );
821 * Get a filter representation of all parameters that are marked
822 * as being excluded from saved query.
824 * @return {Object} Excluded filters values
826 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getExcludedFiltersState = function () {
829 $.each( this.groups
, function ( name
, model
) {
830 if ( model
.isExcludedFromSavedQueries() ) {
831 $.extend( true, result
, model
.getSelectedState() );
839 * Get the parameter names that represent filters that are excluded
840 * from saved queries.
842 * @return {string[]} Parameter names
844 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getExcludedParams = function () {
847 $.each( this.groups
, function ( name
, model
) {
848 if ( model
.isExcludedFromSavedQueries() ) {
849 if ( model
.isPerGroupRequestParameter() ) {
852 // Each filter is its own param
853 result
= result
.concat( model
.getItems().map( function ( filterItem
) {
854 return filterItem
.getParamName();
864 * Analyze the groups and their filters and output an object representing
865 * the state of the parameters they represent.
867 * @param {Object} [filterDefinition] An object defining the filter values,
868 * keyed by filter names.
869 * @return {Object} Parameter state object
871 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getParametersFromFilters = function ( filterDefinition
) {
872 var groupItemDefinition
,
874 groupItems
= this.getFilterGroups();
876 if ( filterDefinition
) {
877 groupItemDefinition
= {};
878 // Filter definition is "flat", but in effect
879 // each group needs to tell us its result based
880 // on the values in it. We need to split this list
881 // back into groupings so we can "feed" it to the
882 // loop below, and we need to expand it so it includes
883 // all filters (set to false)
884 this.getItems().forEach( function ( filterItem
) {
885 groupItemDefinition
[ filterItem
.getGroupName() ] = groupItemDefinition
[ filterItem
.getGroupName() ] || {};
886 groupItemDefinition
[ filterItem
.getGroupName() ][ filterItem
.getName() ] = !!filterDefinition
[ filterItem
.getName() ];
890 $.each( groupItems
, function ( group
, model
) {
893 model
.getParamRepresentation(
894 groupItemDefinition
?
895 groupItemDefinition
[ group
] : null
904 * This is the opposite of the #getParametersFromFilters method; this goes over
905 * the given parameters and translates into a selected/unselected value in the filters.
907 * @param {Object} params Parameters query object
908 * @return {Object} Filter state object
910 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFiltersFromParameters = function ( params
) {
915 // Go over the given parameters, break apart to groupings
916 // The resulting object represents the group with its parameter
917 // values. For example:
924 // group2: "param4|param5"
926 $.each( params
, function ( paramName
, paramValue
) {
928 itemOrGroup
= model
.parameterMap
[ paramName
];
931 groupName
= itemOrGroup
instanceof mw
.rcfilters
.dm
.FilterItem
?
932 itemOrGroup
.getGroupName() : itemOrGroup
.getName();
934 groupMap
[ groupName
] = groupMap
[ groupName
] || {};
935 groupMap
[ groupName
][ paramName
] = paramValue
;
939 // Go over all groups, so we make sure we get the complete output
940 // even if the parameters don't include a certain group
941 $.each( this.groups
, function ( groupName
, groupModel
) {
942 result
= $.extend( true, {}, result
, groupModel
.getFilterRepresentation( groupMap
[ groupName
] ) );
949 * Get the highlight parameters based on current filter configuration
951 * @return {Object} Object where keys are `<filter name>_color` and values
952 * are the selected highlight colors.
954 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getHighlightParameters = function () {
955 var highlightEnabled
= this.isHighlightEnabled(),
958 this.getItems().forEach( function ( filterItem
) {
959 if ( filterItem
.isHighlightSupported() ) {
960 result
[ filterItem
.getName() + '_color' ] = highlightEnabled
&& filterItem
.isHighlighted() ?
961 filterItem
.getHighlightColor() :
970 * Get an object representing the complete empty state of highlights
972 * @return {Object} Object containing all the highlight parameters set to their negative value
974 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getEmptyHighlightParameters = function () {
977 this.getItems().forEach( function ( filterItem
) {
978 if ( filterItem
.isHighlightSupported() ) {
979 result
[ filterItem
.getName() + '_color' ] = null;
987 * Get an array of currently applied highlight colors
989 * @return {string[]} Currently applied highlight colors
991 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getCurrentlyUsedHighlightColors = function () {
994 if ( this.isHighlightEnabled() ) {
995 this.getHighlightedItems().forEach( function ( filterItem
) {
996 var color
= filterItem
.getHighlightColor();
998 if ( result
.indexOf( color
) === -1 ) {
999 result
.push( color
);
1008 * Sanitize value group of a string_option groups type
1009 * Remove duplicates and make sure to only use valid
1013 * @param {string} groupName Group name
1014 * @param {string[]} valueArray Array of values
1015 * @return {string[]} Array of valid values
1017 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.sanitizeStringOptionGroup = function ( groupName
, valueArray
) {
1018 var validNames
= this.getGroupFilters( groupName
).map( function ( filterItem
) {
1019 return filterItem
.getParamName();
1022 return mw
.rcfilters
.utils
.normalizeParamOptions( valueArray
, validNames
);
1026 * Check whether the current filter state is set to all false.
1028 * @return {boolean} Current filters are all empty
1030 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.areCurrentFiltersEmpty = function () {
1031 // Check if there are either any selected items or any items
1032 // that have highlight enabled
1033 return !this.getItems().some( function ( filterItem
) {
1034 return !filterItem
.getGroupModel().isHidden() && ( filterItem
.isSelected() || filterItem
.isHighlighted() );
1039 * Get the item that matches the given name
1041 * @param {string} name Filter name
1042 * @return {mw.rcfilters.dm.FilterItem} Filter item
1044 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getItemByName = function ( name
) {
1045 return this.getItems().filter( function ( item
) {
1046 return name
=== item
.getName();
1051 * Set all filters to false or empty/all
1052 * This is equivalent to display all.
1054 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.emptyAllFilters = function () {
1055 this.getItems().forEach( function ( filterItem
) {
1056 if ( !filterItem
.getGroupModel().isSticky() ) {
1057 this.toggleFilterSelected( filterItem
.getName(), false );
1063 * Toggle selected state of one item
1065 * @param {string} name Name of the filter item
1066 * @param {boolean} [isSelected] Filter selected state
1068 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.toggleFilterSelected = function ( name
, isSelected
) {
1069 var item
= this.getItemByName( name
);
1072 item
.toggleSelected( isSelected
);
1077 * Toggle selected state of items by their names
1079 * @param {Object} filterDef Filter definitions
1081 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.toggleFiltersSelected = function ( filterDef
) {
1082 Object
.keys( filterDef
).forEach( function ( name
) {
1083 this.toggleFilterSelected( name
, filterDef
[ name
] );
1088 * Get a group model from its name
1090 * @param {string} groupName Group name
1091 * @return {mw.rcfilters.dm.FilterGroup} Group model
1093 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getGroup = function ( groupName
) {
1094 return this.groups
[ groupName
];
1098 * Get all filters within a specified group by its name
1100 * @param {string} groupName Group name
1101 * @return {mw.rcfilters.dm.FilterItem[]} Filters belonging to this group
1103 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getGroupFilters = function ( groupName
) {
1104 return ( this.getGroup( groupName
) && this.getGroup( groupName
).getItems() ) || [];
1108 * Find items whose labels match the given string
1110 * @param {string} query Search string
1111 * @param {boolean} [returnFlat] Return a flat array. If false, the result
1112 * is an object whose keys are the group names and values are an array of
1113 * filters per group. If set to true, returns an array of filters regardless
1115 * @return {Object} An object of items to show
1116 * arranged by their group names
1118 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.findMatches = function ( query
, returnFlat
) {
1119 var i
, searchIsEmpty
,
1123 view
= this.getViewByTrigger( query
.substr( 0, 1 ) ),
1124 items
= this.getFiltersByView( view
);
1126 // Normalize so we can search strings regardless of case and view
1127 query
= query
.trim().toLowerCase();
1128 if ( view
!== 'default' ) {
1129 query
= query
.substr( 1 );
1131 // Trim again to also intercept cases where the spaces were after the trigger
1133 query
= query
.trim();
1135 // Check if the search if actually empty; this can be a problem when
1136 // we use prefixes to denote different views
1137 searchIsEmpty
= query
.length
=== 0;
1139 // item label starting with the query string
1140 for ( i
= 0; i
< items
.length
; i
++ ) {
1143 items
[ i
].getLabel().toLowerCase().indexOf( query
) === 0 ||
1145 // For tags, we want the parameter name to be included in the search
1147 items
[ i
].getParamName().toLowerCase().indexOf( query
) > -1
1150 result
[ items
[ i
].getGroupName() ] = result
[ items
[ i
].getGroupName() ] || [];
1151 result
[ items
[ i
].getGroupName() ].push( items
[ i
] );
1152 flatResult
.push( items
[ i
] );
1156 if ( $.isEmptyObject( result
) ) {
1157 // item containing the query string in their label, description, or group title
1158 for ( i
= 0; i
< items
.length
; i
++ ) {
1159 groupTitle
= items
[ i
].getGroupModel().getTitle();
1162 items
[ i
].getLabel().toLowerCase().indexOf( query
) > -1 ||
1163 items
[ i
].getDescription().toLowerCase().indexOf( query
) > -1 ||
1164 groupTitle
.toLowerCase().indexOf( query
) > -1 ||
1166 // For tags, we want the parameter name to be included in the search
1168 items
[ i
].getParamName().toLowerCase().indexOf( query
) > -1
1171 result
[ items
[ i
].getGroupName() ] = result
[ items
[ i
].getGroupName() ] || [];
1172 result
[ items
[ i
].getGroupName() ].push( items
[ i
] );
1173 flatResult
.push( items
[ i
] );
1178 return returnFlat
? flatResult
: result
;
1182 * Get items that are highlighted
1184 * @return {mw.rcfilters.dm.FilterItem[]} Highlighted items
1186 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getHighlightedItems = function () {
1187 return this.getItems().filter( function ( filterItem
) {
1188 return filterItem
.isHighlightSupported() &&
1189 filterItem
.getHighlightColor();
1194 * Get items that allow highlights even if they're not currently highlighted
1196 * @return {mw.rcfilters.dm.FilterItem[]} Items supporting highlights
1198 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getItemsSupportingHighlights = function () {
1199 return this.getItems().filter( function ( filterItem
) {
1200 return filterItem
.isHighlightSupported();
1205 * Get all selected items
1207 * @return {mw.rcfilters.dm.FilterItem[]} Selected items
1209 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getSelectedItems = function () {
1210 var allSelected
= [];
1212 $.each( this.getFilterGroups(), function ( groupName
, groupModel
) {
1213 allSelected
= allSelected
.concat( groupModel
.getSelectedItems() );
1219 * Switch the current view
1221 * @param {string} view View name
1224 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.switchView = function ( view
) {
1225 if ( this.views
[ view
] && this.currentView
!== view
) {
1226 this.currentView
= view
;
1227 this.emit( 'update' );
1232 * Get the current view
1234 * @return {string} Current view
1236 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getCurrentView = function () {
1237 return this.currentView
;
1241 * Get the label for the current view
1243 * @param {string} viewName View name
1244 * @return {string} Label for the current view
1246 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getViewTitle = function ( viewName
) {
1247 viewName
= viewName
|| this.getCurrentView();
1249 return this.views
[ viewName
] && this.views
[ viewName
].title
;
1253 * Get an array of all available view names
1255 * @return {string} Available view names
1257 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getAvailableViews = function () {
1258 return Object
.keys( this.views
);
1262 * Get the view that fits the given trigger
1264 * @param {string} trigger Trigger
1265 * @return {string} Name of view
1267 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getViewByTrigger = function ( trigger
) {
1268 var result
= 'default';
1270 $.each( this.views
, function ( name
, data
) {
1271 if ( data
.trigger
=== trigger
) {
1280 * Toggle the highlight feature on and off.
1281 * Propagate the change to filter items.
1283 * @param {boolean} enable Highlight should be enabled
1284 * @fires highlightChange
1286 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.toggleHighlight = function ( enable
) {
1287 enable
= enable
=== undefined ? !this.highlightEnabled
: enable
;
1289 if ( this.highlightEnabled
!== enable
) {
1290 this.highlightEnabled
= enable
;
1291 this.emit( 'highlightChange', this.highlightEnabled
);
1296 * Check if the highlight feature is enabled
1299 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.isHighlightEnabled = function () {
1300 return !!this.highlightEnabled
;
1304 * Toggle the inverted namespaces property on and off.
1305 * Propagate the change to namespace filter items.
1307 * @param {boolean} enable Inverted property is enabled
1309 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.toggleInvertedNamespaces = function ( enable
) {
1310 this.toggleFilterSelected( this.getInvertModel().getName(), enable
);
1314 * Get the model object that represents the 'invert' filter
1316 * @return {mw.rcfilters.dm.FilterItem}
1318 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getInvertModel = function () {
1319 return this.getGroup( 'invertGroup' ).getItemByParamName( 'invert' );
1323 * Set highlight color for a specific filter item
1325 * @param {string} filterName Name of the filter item
1326 * @param {string} color Selected color
1328 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.setHighlightColor = function ( filterName
, color
) {
1329 this.getItemByName( filterName
).setHighlightColor( color
);
1333 * Clear highlight for a specific filter item
1335 * @param {string} filterName Name of the filter item
1337 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.clearHighlightColor = function ( filterName
) {
1338 this.getItemByName( filterName
).clearHighlightColor();
1342 * Clear highlight for all filter items
1344 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.clearAllHighlightColors = function () {
1345 this.getItems().forEach( function ( filterItem
) {
1346 filterItem
.clearHighlightColor();
1351 * Return a version of the given string that is without any
1354 * @param {string} str Given string
1355 * @return {string} Result
1357 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.removeViewTriggers = function ( str
) {
1358 if ( this.getViewByTrigger( str
.substr( 0, 1 ) ) !== 'default' ) {
1359 str
= str
.substr( 1 );
1364 }( mediaWiki
, jQuery
) );