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.excludedByMap
= {};
17 this.defaultParams
= {};
18 this.defaultFiltersEmpty
= null;
21 this.aggregate( { update
: 'filterItemUpdate' } );
22 this.connect( this, { filterItemUpdate
: 'onFilterItemUpdate' } );
26 OO
.initClass( mw
.rcfilters
.dm
.FiltersViewModel
);
27 OO
.mixinClass( mw
.rcfilters
.dm
.FiltersViewModel
, OO
.EventEmitter
);
28 OO
.mixinClass( mw
.rcfilters
.dm
.FiltersViewModel
, OO
.EmitterList
);
35 * Filter list is initialized
40 * @param {mw.rcfilters.dm.FilterItem} item Filter item updated
42 * Filter item has changed
48 * Respond to filter item change.
50 * @param {mw.rcfilters.dm.FilterItem} item Updated filter
53 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.onFilterItemUpdate = function ( item
) {
54 // Reapply the active state of filters
55 this.reapplyActiveFilters( item
);
57 // Recheck group activity state
58 this.getGroup( item
.getGroup() ).checkActive();
60 this.emit( 'itemUpdate', item
);
64 * Calculate the active state of the filters, based on selected filters in the group.
66 * @param {mw.rcfilters.dm.FilterItem} item Changed item
68 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.reapplyActiveFilters = function ( item
) {
69 var selectedItemsCount
,
70 group
= item
.getGroup(),
73 !this.getGroup( group
).getExclusionType() ||
74 this.getGroup( group
).getExclusionType() === 'default'
77 // If any parameter is selected, but:
78 // - If there are unselected items in the group, they are inactive
79 // - If the entire group is selected, all are inactive
81 // Check what's selected in the group
82 selectedItemsCount
= this.getGroupFilters( group
).filter( function ( filterItem
) {
83 return filterItem
.isSelected();
86 this.getGroupFilters( group
).forEach( function ( filterItem
) {
87 filterItem
.toggleActive(
88 selectedItemsCount
> 0 ?
89 // If some items are selected
91 selectedItemsCount
=== model
.groups
[ group
].getItemCount() ?
92 // If **all** items are selected, they're all inactive
94 // If not all are selected, then the selected are active
95 // and the unselected are inactive
96 filterItem
.isSelected()
98 // No item is selected, everything is active
102 } else if ( this.getGroup( group
).getExclusionType() === 'explicit' ) {
104 // - Go over the list of excluded filters to change their
105 // active states accordingly
107 // For each item in the list, see if there are other selected
108 // filters that also exclude it. If it does, it will still be
111 item
.getExcludedFilters().forEach( function ( filterName
) {
112 var filterItem
= model
.getItemByName( filterName
);
114 // Note to reduce confusion:
115 // - item is the filter whose state changed and should exclude the other filters
116 // in its list of exclusions
117 // - filterItem is the filter that is potentially being excluded by the current item
118 // - anotherExcludingFilter is any other filter that excludes filterItem; we must check
119 // if that filter is selected, because if it is, we should not touch the excluded item
121 // Check if there are any filters (other than the current one)
122 // that also exclude the filterName
123 !model
.excludedByMap
[ filterName
].some( function ( anotherExcludingFilterName
) {
124 var anotherExcludingFilter
= model
.getItemByName( anotherExcludingFilterName
);
127 anotherExcludingFilterName
!== item
.getName() &&
128 anotherExcludingFilter
.isSelected()
132 // Only change the state for filters that aren't
133 // also affected by other excluding selected filters
134 filterItem
.toggleActive( !item
.isSelected() );
141 * Set filters and preserve a group relationship based on
142 * the definition given by an object
144 * @param {Object} filters Filter group definition
146 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.initializeFilters = function ( filters
) {
147 var i
, filterItem
, selectedFilterNames
, excludedFilters
,
150 addToMap = function ( excludedFilters
) {
151 excludedFilters
.forEach( function ( filterName
) {
152 model
.excludedByMap
[ filterName
] = model
.excludedByMap
[ filterName
] || [];
153 model
.excludedByMap
[ filterName
].push( filterItem
.getName() );
160 this.excludedByMap
= {};
162 $.each( filters
, function ( group
, data
) {
163 if ( !model
.groups
[ group
] ) {
164 model
.groups
[ group
] = new mw
.rcfilters
.dm
.FilterGroup( {
167 separator
: data
.separator
,
168 exclusionType
: data
.exclusionType
172 selectedFilterNames
= [];
173 for ( i
= 0; i
< data
.filters
.length
; i
++ ) {
174 excludedFilters
= data
.filters
[ i
].excludes
|| [];
176 filterItem
= new mw
.rcfilters
.dm
.FilterItem( data
.filters
[ i
].name
, {
178 label
: data
.filters
[ i
].label
,
179 description
: data
.filters
[ i
].description
,
180 selected
: data
.filters
[ i
].selected
,
181 excludes
: excludedFilters
,
182 'default': data
.filters
[ i
].default
185 // Map filters and what excludes them
186 addToMap( excludedFilters
);
188 if ( data
.type
=== 'send_unselected_if_any' ) {
189 // Store the default parameter state
190 // For this group type, parameter values are direct
191 model
.defaultParams
[ data
.filters
[ i
].name
] = Number( !!data
.filters
[ i
].default );
193 data
.type
=== 'string_options' &&
194 data
.filters
[ i
].default
196 selectedFilterNames
.push( data
.filters
[ i
].name
);
199 model
.groups
[ group
].addItems( filterItem
);
200 items
.push( filterItem
);
203 if ( data
.type
=== 'string_options' ) {
204 // Store the default parameter group state
205 // For this group, the parameter is group name and value is the names
207 model
.defaultParams
[ group
] = model
.sanitizeStringOptionGroup( group
, selectedFilterNames
).join( model
.groups
[ group
].getSeparator() );
211 this.addItems( items
);
213 this.emit( 'initialize' );
217 * Get the names of all available filters
219 * @return {string[]} An array of filter names
221 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFilterNames = function () {
222 return this.getItems().map( function ( item
) { return item
.getName(); } );
226 * Get the object that defines groups by their name.
228 * @return {Object} Filter groups
230 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFilterGroups = function () {
235 * Update the representation of the parameters. These are the back-end
236 * parameters representing the filters, but they represent the given
237 * current state regardless of validity.
239 * This should only run after filters are already set.
241 * @param {Object} params Parameter state
243 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.updateParameters = function ( params
) {
246 $.each( params
, function ( name
, value
) {
247 // Only store the parameters that exist in the system
248 if ( model
.getItemByName( name
) ) {
249 model
.parameters
[ name
] = value
;
255 * Get the value of a specific parameter
257 * @param {string} name Parameter name
258 * @return {number|string} Parameter value
260 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getParamValue = function ( name
) {
261 return this.parameters
[ name
];
265 * Get the current selected state of the filters
267 * @return {Object} Filters selected state
269 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getSelectedState = function () {
271 items
= this.getItems(),
274 for ( i
= 0; i
< items
.length
; i
++ ) {
275 result
[ items
[ i
].getName() ] = items
[ i
].isSelected();
282 * Get the current full state of the filters
284 * @return {Object} Filters full state
286 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFullState = function () {
288 items
= this.getItems(),
291 for ( i
= 0; i
< items
.length
; i
++ ) {
292 result
[ items
[ i
].getName() ] = {
293 selected
: items
[ i
].isSelected(),
294 active
: items
[ i
].isActive()
302 * Get the default parameters object
304 * @return {Object} Default parameter values
306 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getDefaultParams = function () {
307 return this.defaultParams
;
311 * Set all filter states to default values
313 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.setFiltersToDefaults = function () {
314 var defaultFilterStates
= this.getFiltersFromParameters( this.getDefaultParams() );
316 this.updateFilters( defaultFilterStates
);
320 * Analyze the groups and their filters and output an object representing
321 * the state of the parameters they represent.
323 * @param {Object} [filterGroups] An object defining the filter groups to
324 * translate to parameters. Its structure must follow that of this.groups
325 * see #getFilterGroups
326 * @return {Object} Parameter state object
328 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getParametersFromFilters = function ( filterGroups
) {
329 var i
, filterItems
, anySelected
, values
,
331 groupItems
= filterGroups
|| this.getFilterGroups();
333 $.each( groupItems
, function ( group
, model
) {
334 filterItems
= model
.getItems();
336 if ( model
.getType() === 'send_unselected_if_any' ) {
337 // First, check if any of the items are selected at all.
338 // If none is selected, we're treating it as if they are
340 anySelected
= filterItems
.some( function ( filterItem
) {
341 return filterItem
.isSelected();
344 // Go over the items and define the correct values
345 for ( i
= 0; i
< filterItems
.length
; i
++ ) {
346 result
[ filterItems
[ i
].getName() ] = anySelected
?
347 Number( !filterItems
[ i
].isSelected() ) : 0;
349 } else if ( model
.getType() === 'string_options' ) {
351 for ( i
= 0; i
< filterItems
.length
; i
++ ) {
352 if ( filterItems
[ i
].isSelected() ) {
353 values
.push( filterItems
[ i
].getName() );
357 if ( values
.length
=== 0 || values
.length
=== filterItems
.length
) {
358 result
[ group
] = 'all';
360 result
[ group
] = values
.join( model
.getSeparator() );
369 * Sanitize value group of a string_option groups type
370 * Remove duplicates and make sure to only use valid
374 * @param {string} groupName Group name
375 * @param {string[]} valueArray Array of values
376 * @return {string[]} Array of valid values
378 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.sanitizeStringOptionGroup = function( groupName
, valueArray
) {
380 validNames
= this.getGroupFilters( groupName
).map( function ( filterItem
) {
381 return filterItem
.getName();
384 if ( valueArray
.indexOf( 'all' ) > -1 ) {
385 // If anywhere in the values there's 'all', we
386 // treat it as if only 'all' was selected.
387 // Example: param=valid1,valid2,all
392 // Get rid of any dupe and invalid parameter, only output
394 // Example: param=valid1,valid2,invalid1,valid1
395 // Result: param=valid1,valid2
396 valueArray
.forEach( function ( value
) {
398 validNames
.indexOf( value
) > -1 &&
399 result
.indexOf( value
) === -1
401 result
.push( value
);
409 * Check whether the current filter state is set to all false.
411 * @return {boolean} Current filters are all empty
413 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.areCurrentFiltersEmpty = function () {
414 var currFilters
= this.getSelectedState();
416 return Object
.keys( currFilters
).every( function ( filterName
) {
417 return !currFilters
[ filterName
];
422 * Check whether the default values of the filters are all false.
424 * @return {boolean} Default filters are all false
426 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.areDefaultFiltersEmpty = function () {
429 if ( this.defaultFiltersEmpty
!== null ) {
430 // We only need to do this test once,
431 // because defaults are set once per session
432 defaultFilters
= this.getFiltersFromParameters();
433 this.defaultFiltersEmpty
= Object
.keys( defaultFilters
).every( function ( filterName
) {
434 return !defaultFilters
[ filterName
];
438 return this.defaultFiltersEmpty
;
442 * This is the opposite of the #getParametersFromFilters method; this goes over
443 * the given parameters and translates into a selected/unselected value in the filters.
445 * @param {Object} params Parameters query object
446 * @return {Object} Filter state object
448 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getFiltersFromParameters = function ( params
) {
452 base
= this.getDefaultParams(),
455 params
= $.extend( {}, base
, params
);
457 $.each( params
, function ( paramName
, paramValue
) {
458 // Find the filter item
459 filterItem
= model
.getItemByName( paramName
);
460 // Ignore if no filter item exists
462 groupMap
[ filterItem
.getGroup() ] = groupMap
[ filterItem
.getGroup() ] || {};
464 // Mark the group if it has any items that are selected
465 groupMap
[ filterItem
.getGroup() ].hasSelected
= (
466 groupMap
[ filterItem
.getGroup() ].hasSelected
||
467 !!Number( paramValue
)
470 // Add the relevant filter into the group map
471 groupMap
[ filterItem
.getGroup() ].filters
= groupMap
[ filterItem
.getGroup() ].filters
|| [];
472 groupMap
[ filterItem
.getGroup() ].filters
.push( filterItem
);
473 } else if ( model
.groups
.hasOwnProperty( paramName
) ) {
474 // This parameter represents a group (values are the filters)
475 // this is equivalent to checking if the group is 'string_options'
476 groupMap
[ paramName
] = { filters
: model
.groups
[ paramName
].getItems() };
480 // Now that we know the groups' selection states, we need to go over
481 // the filters in the groups and mark their selected states appropriately
482 $.each( groupMap
, function ( group
, data
) {
483 var paramValues
, filterItem
,
484 allItemsInGroup
= data
.filters
;
486 if ( model
.groups
[ group
].getType() === 'send_unselected_if_any' ) {
487 for ( i
= 0; i
< allItemsInGroup
.length
; i
++ ) {
488 filterItem
= allItemsInGroup
[ i
];
490 result
[ filterItem
.getName() ] = data
.hasSelected
?
491 // Flip the definition between the parameter
492 // state and the filter state
493 // This is what the 'toggleSelected' value of the filter is
494 !Number( params
[ filterItem
.getName() ] ) :
495 // Otherwise, there are no selected items in the
496 // group, which means the state is false
499 } else if ( model
.groups
[ group
].getType() === 'string_options' ) {
500 paramValues
= model
.sanitizeStringOptionGroup( group
, params
[ group
].split( model
.groups
[ group
].getSeparator() ) );
502 for ( i
= 0; i
< allItemsInGroup
.length
; i
++ ) {
503 filterItem
= allItemsInGroup
[ i
];
505 result
[ filterItem
.getName() ] = (
506 // If it is the word 'all'
507 paramValues
.length
=== 1 && paramValues
[ 0 ] === 'all' ||
508 // All values are written
509 paramValues
.length
=== model
.groups
[ group
].getItemCount()
511 // All true (either because all values are written or the term 'all' is written)
512 // is the same as all filters set to false
514 // Otherwise, the filter is selected only if it appears in the parameter values
515 paramValues
.indexOf( filterItem
.getName() ) > -1;
523 * Get the item that matches the given name
525 * @param {string} name Filter name
526 * @return {mw.rcfilters.dm.FilterItem} Filter item
528 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getItemByName = function ( name
) {
529 return this.getItems().filter( function ( item
) {
530 return name
=== item
.getName();
535 * Set all filters to false or empty/all
536 * This is equivalent to display all.
538 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.emptyAllFilters = function () {
541 this.getItems().forEach( function ( filterItem
) {
542 filters
[ filterItem
.getName() ] = false;
546 this.updateFilters( filters
);
550 * Toggle selected state of items by their names
552 * @param {Object} filterDef Filter definitions
554 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.updateFilters = function ( filterDef
) {
555 var name
, filterItem
;
557 for ( name
in filterDef
) {
558 filterItem
= this.getItemByName( name
);
559 filterItem
.toggleSelected( filterDef
[ name
] );
564 * Get a group model from its name
566 * @param {string} groupName Group name
567 * @return {mw.rcfilters.dm.FilterGroup} Group model
569 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getGroup = function ( groupName
) {
570 return this.groups
[ groupName
];
574 * Get all filters within a specified group by its name
576 * @param {string} groupName Group name
577 * @return {mw.rcfilters.dm.FilterItem[]} Filters belonging to this group
579 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.getGroupFilters = function ( groupName
) {
580 return ( this.getGroup( groupName
) && this.getGroup( groupName
).getItems() ) || [];
584 * Find items whose labels match the given string
586 * @param {string} str Search string
587 * @return {Object} An object of items to show
588 * arranged by their group names
590 mw
.rcfilters
.dm
.FiltersViewModel
.prototype.findMatches = function ( str
) {
593 items
= this.getItems();
595 // Normalize so we can search strings regardless of case
596 str
= str
.toLowerCase();
597 for ( i
= 0; i
< items
.length
; i
++ ) {
598 if ( items
[ i
].getLabel().toLowerCase().indexOf( str
) > -1 ) {
599 result
[ items
[ i
].getGroup() ] = result
[ items
[ i
].getGroup() ] || [];
600 result
[ items
[ i
].getGroup() ].push( items
[ i
] );
606 }( mediaWiki
, jQuery
) );