-
Notifications
You must be signed in to change notification settings - Fork 14
/
apply.js
205 lines (187 loc) · 7.12 KB
/
apply.js
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
const util = require('../util');
const vega = require('vega');
const vl = require('vega-lite')
const {parsePredicateFilter} = require('./trans');
const { OPS, LOGIC_OPS } = require('../constants');
function apply (sSpec, eSpec, editOps) {
checkApplyingEditOps(editOps);
let resultSpec = editOps.reduce((resultSpec, editOp) => {
if (editOp.type === "mark") {
resultSpec = applyMarkEditOp(resultSpec, eSpec, editOp);
} else if (editOp.type === "transform") {
resultSpec = applyTransformEditOp(resultSpec, eSpec, editOp);
} else if (editOp.type === "encoding") {
resultSpec = applyEncodingEditOp(resultSpec, eSpec, editOp);
}
return resultSpec;
}, util.duplicate(sSpec))//an intermediate spec by applying edit operations on the sSpec
checkSpec(resultSpec);
return resultSpec;
}
exports.apply = apply
function applyMarkEditOp(targetSpec, eSpec, editOp) {
let resultSpec = util.duplicate(targetSpec);
resultSpec.mark = eSpec.mark
return resultSpec;
}
exports.applyMarkEditOp = applyMarkEditOp;
function applyTransformEditOp(targetSpec, eSpec, editOp){
let resultSpec = util.duplicate(targetSpec);
const transformType = editOp.name.toLowerCase();
const details = !util.isArray(editOp.detail) ? [editOp.detail] : editOp.detail;
if (transformType.indexOf("filter") >= 0) {
if (editOp.name === "REMOVE_FILTER" || editOp.name === "MODIFY_FILTER") {
resultSpec.transform.filter(tfm => {
return tfm.filter && ((parsePredicateFilter(tfm.filter)[0].id === editOp.detail.id))
}).forEach(filter => {
if (resultSpec.transform) {
let i = resultSpec.transform.findIndex(trsfm => util.deepEqual(trsfm, filter))
resultSpec.transform.splice(i, 1);
}
})
}
if (editOp.name === "ADD_FILTER" || editOp.name === "MODIFY_FILTER") {
eSpec.transform.filter(tfm => {
return tfm.filter && ((parsePredicateFilter(tfm.filter)[0].id === editOp.detail.id))
}).forEach(filter => {
if (!resultSpec.transform) {
resultSpec.transform = [filter];
} else if (!resultSpec.transform.find(trsfm => util.deepEqual(filter, trsfm))) {
resultSpec.transform.push(filter);
}
})
}
} else {
details.forEach(detail => {
let fieldDef = resultSpec.encoding[detail.channel];
if (fieldDef) {
//Todo: cannot apply SCALE if the channel has a different type.
if (detail.how === "removed"){
delete fieldDef[transformType]
} else {
// console.log(fieldDef.type, detail.fieldType)
if (transformType === "scale" && fieldDef.type !== detail.fieldType) {
throw new UnapplicableEditOPError(`Cannot apply ${editOp.name} since it requires "${detail.fieldType}" field instead of "${fieldDef.type}".`)
}
fieldDef[transformType] = eSpec.encoding[detail.channel][transformType]
}
} else {
throw new UnapplicableEditOPError(`Cannot apply ${editOp.name} since there is no "${detail.channel}" channel.`)
}
})
}
return resultSpec;
}
exports.applyTransformEditOp = applyTransformEditOp;
function applyEncodingEditOp(targetSpec, eSpec, editOp){
let resultSpec = util.duplicate(targetSpec);
if (editOp.name.indexOf("REMOVE") === 0) {
let channel = editOp.detail.before.channel;
if (resultSpec.encoding[channel]) {
delete resultSpec.encoding[channel];
} else {
throw new UnapplicableEditOPError(`Cannot apply ${editOp.name} since there is no "${channel}" channel.`);
}
} else if (editOp.name.indexOf("ADD") === 0) {
let channel = editOp.detail.after.channel;
if (resultSpec.encoding[channel]) {
throw new UnapplicableEditOPError(`Cannot apply ${editOp.name} since "${channel}" already exists.`);
} else {
resultSpec.encoding[channel] = util.duplicate(eSpec.encoding[channel]);
}
} else if (editOp.name.indexOf("MOVE") === 0) {
let sChannel = editOp.detail.before.channel,
dChannel = editOp.detail.after.channel;
if (!resultSpec.encoding[sChannel]) {
throw new UnapplicableEditOPError(`Cannot apply ${editOp.name} since there is no "${sChannel}" channel.`);
} else if (resultSpec.encoding[dChannel]) {
throw new UnapplicableEditOPError(`Cannot apply ${editOp.name} since "${dChannel}" already exists.`);
} else {
resultSpec.encoding[dChannel] = util.duplicate(resultSpec.encoding[sChannel])
delete resultSpec.encoding[sChannel];
}
} else if (editOp.name.indexOf("MODIFY") === 0) {
let channel = editOp.detail.before.channel,
field = editOp.detail.after.field,
type = editOp.detail.after.type;
if (!resultSpec.encoding[channel]) {
throw new UnapplicableEditOPError(`Cannot apply ${editOp.name} since there is no "${channel}" channel.`);
} else {
resultSpec.encoding[channel].field = field;
resultSpec.encoding[channel].type = type;
}
} else if (editOp.name.indexOf("SWAP_X_Y") === 0) {
if (!resultSpec.encoding.x || !resultSpec.encoding.y) {
throw new UnapplicableEditOPError(`Cannot apply ${editOp.name} since there is no "x" and "y" channels.`);
} else {
let temp = util.duplicate(resultSpec.encoding.y);
resultSpec.encoding.y = util.duplicate(resultSpec.encoding.x);
resultSpec.encoding.x = temp;
}
}
return resultSpec;
}
exports.applyEncodingEditOp = applyEncodingEditOp;
function checkSpec(spec) {
let lg = vega.logger();
const warnings = [], errors = [];
lg.warn = (m) => {
warnings.push(m);
}
lg.error = (m) => {
errors.push(m);
}
vl.compile(spec, {logger: lg})
let hasAggregate = false;
for (const key in spec.encoding) {
if (spec.encoding.hasOwnProperty(key)) {
const fieldDef = spec.encoding[key];
if (fieldDef.aggregate) {
hasAggregate = true;
}
if (fieldDef.field === "*" && !fieldDef.aggregate) {
warnings.push("'*' field should innclude aggregate.")
}
}
}
if (hasAggregate) {
const hasNoAggOnQField = Object.keys(spec.encoding)
.filter(ch => {
return spec.encoding[ch].type === "quantitative" && !spec.encoding[ch].aggregate
}).length > 0
if (hasNoAggOnQField) {
warnings.push("Aggregate should be applied on all quantitative fields.")
}
}
if ((warnings.length > 0) || (errors.length > 0)) {
throw new InvalidVLSpecError(`The resulted spec is not valid Vega-Lite Spec.`, {warnings, errors})
}
}
function checkApplyingEditOps(editOps) {
// _COUNT encodig should be applied with AGGREGATE
if (
editOps.find(eo => eo.name.indexOf("_COUNT") >= 0) &&
!editOps.find(eo => eo.name === "AGGREGATE")
) {
throw new UnapplicableEditOpsError("_COUNT encoding edit operations cannot be applied without AGGREGATE.");
}
}
class UnapplicableEditOPError extends Error {
constructor(message) {
super(message);
this.name = "UnapplicableEditOPError"
}
}
class InvalidVLSpecError extends Error {
constructor(message, info) {
super(message);
this.name = "InvalidVLSpecError"
this.info = info;
}
}
class UnapplicableEditOpsError extends Error {
constructor(message) {
super(message);
this.name = "UnapplicableEditOpsError"
}
}