1 | import {Transform} from 'vega-dataflow';
|
2 | import {falsy, inherits, isArray} from 'vega-util';
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | export default function Encode(params) {
|
17 | Transform.call(this, null, params);
|
18 | }
|
19 |
|
20 | var prototype = inherits(Encode, Transform);
|
21 |
|
22 | prototype.transform = function(_, pulse) {
|
23 | var out = pulse.fork(pulse.ADD_REM),
|
24 | fmod = _.mod || false,
|
25 | encoders = _.encoders,
|
26 | encode = pulse.encode;
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | if (isArray(encode)) {
|
32 | if (out.changed() || encode.every(function(e) { return encoders[e]; })) {
|
33 | encode = encode[0];
|
34 | out.encode = null;
|
35 | } else {
|
36 | return pulse.StopPropagation;
|
37 | }
|
38 | }
|
39 |
|
40 |
|
41 | var reenter = encode === 'enter',
|
42 | update = encoders.update || falsy,
|
43 | enter = encoders.enter || falsy,
|
44 | exit = encoders.exit || falsy,
|
45 | set = (encode && !reenter ? encoders[encode] : update) || falsy;
|
46 |
|
47 | if (pulse.changed(pulse.ADD)) {
|
48 | pulse.visit(pulse.ADD, function(t) { enter(t, _); update(t, _); });
|
49 | out.modifies(enter.output);
|
50 | out.modifies(update.output);
|
51 | if (set !== falsy && set !== update) {
|
52 | pulse.visit(pulse.ADD, function(t) { set(t, _); });
|
53 | out.modifies(set.output);
|
54 | }
|
55 | }
|
56 |
|
57 | if (pulse.changed(pulse.REM) && exit !== falsy) {
|
58 | pulse.visit(pulse.REM, function(t) { exit(t, _); });
|
59 | out.modifies(exit.output);
|
60 | }
|
61 |
|
62 | if (reenter || set !== falsy) {
|
63 | var flag = pulse.MOD | (_.modified() ? pulse.REFLOW : 0);
|
64 | if (reenter) {
|
65 | pulse.visit(flag, function(t) {
|
66 | var mod = enter(t, _) || fmod;
|
67 | if (set(t, _) || mod) out.mod.push(t);
|
68 | });
|
69 | if (out.mod.length) out.modifies(enter.output);
|
70 | } else {
|
71 | pulse.visit(flag, function(t) {
|
72 | if (set(t, _) || fmod) out.mod.push(t);
|
73 | });
|
74 | }
|
75 | if (out.mod.length) out.modifies(set.output);
|
76 | }
|
77 |
|
78 | return out.changed() ? out : pulse.StopPropagation;
|
79 | };
|