-
Notifications
You must be signed in to change notification settings - Fork 590
/
parse.ts
149 lines (130 loc) · 5.03 KB
/
parse.ts
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import {parseSelector} from 'vega-event-selector';
import {array, isObject, isString, stringValue} from 'vega-util';
import {selectionCompilers, SelectionComponent, STORE} from '.';
import {warn} from '../../log';
import {BaseSelectionConfig, SelectionParameter, ParameterExtent} from '../../selection';
import {Dict, duplicate, entries, replacePathInField, varName} from '../../util';
import {DataFlowNode, OutputNode} from '../data/dataflow';
import {FilterNode} from '../data/filter';
import {Model} from '../model';
import {UnitModel} from '../unit';
import {DataSourceType} from '../../data';
import {ParameterPredicate} from '../../predicate';
export function parseUnitSelection(model: UnitModel, selDefs: SelectionParameter[]) {
const selCmpts: Dict<SelectionComponent<any /* this has to be "any" so typing won't fail in test files*/>> = {};
const selectionConfig = model.config.selection;
if (!selDefs || !selDefs.length) return selCmpts;
for (const def of selDefs) {
const name = varName(def.name);
const selDef = def.select;
const type = isString(selDef) ? selDef : selDef.type;
const defaults: BaseSelectionConfig = isObject(selDef) ? duplicate(selDef) : {type};
// Set default values from config if a property hasn't been specified,
// or if it is true. E.g., "translate": true should use the default
// event handlers for translate. However, true may be a valid value for
// a property (e.g., "nearest": true).
const cfg = selectionConfig[type];
for (const key in cfg) {
// Project transform applies its defaults.
if (key === 'fields' || key === 'encodings') {
continue;
}
if (key === 'mark') {
defaults[key] = {...cfg[key], ...defaults[key]};
}
if (defaults[key] === undefined || defaults[key] === true) {
defaults[key] = cfg[key] ?? defaults[key];
}
}
const selCmpt: SelectionComponent<any> = (selCmpts[name] = {
...defaults,
name,
type,
init: def.value,
bind: def.bind,
events: isString(defaults.on) ? parseSelector(defaults.on, 'scope') : array(duplicate(defaults.on))
} as any);
for (const c of selectionCompilers) {
if (c.defined(selCmpt) && c.parse) {
c.parse(model, selCmpt, def);
}
}
}
return selCmpts;
}
export function parseSelectionPredicate(
model: Model,
pred: ParameterPredicate,
dfnode?: DataFlowNode,
datum = 'datum'
): string {
const name = isString(pred) ? pred : pred.param;
const vname = varName(name);
const store = stringValue(vname + STORE);
let selCmpt;
try {
selCmpt = model.getSelectionComponent(vname, name);
} catch (e) {
// If a selection isn't found, treat as a variable parameter and coerce to boolean.
return `!!${vname}`;
}
if (selCmpt.project.timeUnit) {
const child = dfnode ?? model.component.data.raw;
const tunode = selCmpt.project.timeUnit.clone();
if (child.parent) {
tunode.insertAsParentOf(child);
} else {
child.parent = tunode;
}
}
const fn = selCmpt.project.hasSelectionId ? 'vlSelectionIdTest(' : 'vlSelectionTest(';
const resolve = selCmpt.resolve === 'global' ? ')' : `, ${stringValue(selCmpt.resolve)})`;
const test = `${fn}${store}, ${datum}${resolve}`;
const length = `length(data(${store}))`;
return pred.empty === false ? `${length} && ${test}` : `!${length} || ${test}`;
}
export function parseSelectionExtent(model: Model, name: string, extent: ParameterExtent) {
const vname = varName(name);
const encoding = extent['encoding'];
let field = extent['field'];
let selCmpt;
try {
selCmpt = model.getSelectionComponent(vname, name);
} catch (e) {
// If a selection isn't found, treat it as a variable parameter.
return vname;
}
if (!encoding && !field) {
field = selCmpt.project.items[0].field;
if (selCmpt.project.items.length > 1) {
warn(
'A "field" or "encoding" must be specified when using a selection as a scale domain. ' +
`Using "field": ${stringValue(field)}.`
);
}
} else if (encoding && !field) {
const encodings = selCmpt.project.items.filter(p => p.channel === encoding);
if (!encodings.length || encodings.length > 1) {
field = selCmpt.project.items[0].field;
warn(
(!encodings.length ? 'No ' : 'Multiple ') +
`matching ${stringValue(encoding)} encoding found for selection ${stringValue(extent.param)}. ` +
`Using "field": ${stringValue(field)}.`
);
} else {
field = encodings[0].field;
}
}
return `${selCmpt.name}[${stringValue(replacePathInField(field))}]`;
}
export function materializeSelections(model: UnitModel, main: OutputNode) {
for (const [selection, selCmpt] of entries(model.component.selection ?? {})) {
const lookupName = model.getName(`lookup_${selection}`);
model.component.data.outputNodes[lookupName] = selCmpt.materialized = new OutputNode(
new FilterNode(main, model, {param: selection}),
lookupName,
DataSourceType.Lookup,
model.component.data.outputNodeRefCounts
);
}
}