-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathflags.ts
More file actions
executable file
·62 lines (55 loc) · 2.01 KB
/
flags.ts
File metadata and controls
executable file
·62 lines (55 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import * as _ from 'lodash';
import utils from '../../index';
import { DataFormatTypes, IDataFormat, formatWarn, getPrefix } from '../common';
import { IDataSourcePlugin } from '../../../data-sources/plugins/DataSourcePlugin';
/**
* Turns a list of values into a list of flags
*
* Receives a list of filtering values (on the data source params variable):
* params: {
* values: [ 'value1', 'value2', 'value3' ]
* }
*
* And outputs the result in a consumable filter way:
* result: {
* "value1": false, (will be true if prefix-selected contains "value1")
* "value2": false,
* "value3": false,
* "prefix-filters": [ 'value 1', 'value 2', 'value 3' ],
* "prefix-selected": [ ],
* }
*
* "prefix-selected" will be able to hold the selected values from the filter component
*
* @param format 'filter' | {
* type: 'filter',
* args: {
* prefix: string - a prefix string for the exported variables (default to id).
* data: string - the state property holding the data (default is 'values').
* }
* }
* @param state Current received state from data source
* @param dependencies Dependencies for the plugin
* @param plugin The entire plugin (for id generation, params etc...)
* @param prevState The previous state to compare for changing filters
*/
export function flags(
format: string | IDataFormat,
state: any,
dependencies: IDictionary,
plugin: IDataSourcePlugin,
prevState: any) {
const prefix = getPrefix(format);
const args = typeof format !== 'string' && format.args || {};
const params = plugin.getParams();
if (!params || !Array.isArray(params.values)) {
return formatWarn('A paramerter "values" is expected as an array on "params" in the data source', 'filter', plugin);
}
if (!state) { return null; }
let values = params[args.data || 'values'];
let flagsobj = {};
values.forEach(key => { flagsobj[key] = state.selectedValue === key; });
flagsobj[prefix + 'values-all'] = values;
flagsobj[prefix + 'values-selected'] = state.selectedValue || [];
return flagsobj;
}