UNPKG

2.8 kBJavaScriptView Raw
1import {Transform} from 'vega-dataflow';
2import {falsy, inherits, isArray} from 'vega-util';
3
4/**
5 * Invokes encoding functions for visual items.
6 * @constructor
7 * @param {object} params - The parameters to the encoding functions. This
8 * parameter object will be passed through to all invoked encoding functions.
9 * @param {object} [params.mod=false] - Flag indicating if tuples in the input
10 * mod set that are unmodified by encoders should be included in the output.
11 * @param {object} param.encoders - The encoding functions
12 * @param {function(object, object): boolean} [param.encoders.update] - Update encoding set
13 * @param {function(object, object): boolean} [param.encoders.enter] - Enter encoding set
14 * @param {function(object, object): boolean} [param.encoders.exit] - Exit encoding set
15 */
16export default function Encode(params) {
17 Transform.call(this, null, params);
18}
19
20var prototype = inherits(Encode, Transform);
21
22prototype.transform = function(_, pulse) {
23 var out = pulse.fork(pulse.ADD_REM),
24 fmod = _.mod || false,
25 encoders = _.encoders,
26 encode = pulse.encode;
27
28 // if an array, the encode directive includes additional sets
29 // that must be defined in order for the primary set to be invoked
30 // e.g., only run the update set if the hover set is defined
31 if (isArray(encode)) {
32 if (out.changed() || encode.every(function(e) { return encoders[e]; })) {
33 encode = encode[0];
34 out.encode = null; // consume targeted encode directive
35 } else {
36 return pulse.StopPropagation;
37 }
38 }
39
40 // marshall encoder functions
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};