-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathcommand.js
425 lines (358 loc) · 12.1 KB
/
command.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/**
* Copyright 2012-2018, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Plotly = require('../plotly');
var Lib = require('../lib');
/*
* Create or update an observer. This function is designed to be
* idempotent so that it can be called over and over as the component
* updates, and will attach and detach listeners as needed.
*
* @param {optional object} container
* An object on which the observer is stored. This is the mechanism
* by which it is idempotent. If it already exists, another won't be
* added. Each time it's called, the value lookup table is updated.
* @param {array} commandList
* An array of commands, following either `buttons` of `updatemenus`
* or `steps` of `sliders`.
* @param {function} onchange
* A listener called when the value is changed. Receives data object
* with information about the new state.
*/
exports.manageCommandObserver = function(gd, container, commandList, onchange) {
var ret = {};
var enabled = true;
if(container && container._commandObserver) {
ret = container._commandObserver;
}
if(!ret.cache) {
ret.cache = {};
}
// Either create or just recompute this:
ret.lookupTable = {};
var binding = exports.hasSimpleAPICommandBindings(gd, commandList, ret.lookupTable);
if(container && container._commandObserver) {
if(!binding) {
// If container exists and there are no longer any bindings,
// remove existing:
if(container._commandObserver.remove) {
container._commandObserver.remove();
container._commandObserver = null;
return ret;
}
} else {
// If container exists and there *are* bindings, then the lookup
// table should have been updated and check is already attached,
// so there's nothing to be done:
return ret;
}
}
// Determine whether there's anything to do for this binding:
if(binding) {
// Build the cache:
bindingValueHasChanged(gd, binding, ret.cache);
ret.check = function check() {
if(!enabled) return;
var update = bindingValueHasChanged(gd, binding, ret.cache);
if(update.changed && onchange) {
// Disable checks for the duration of this command in order to avoid
// infinite loops:
if(ret.lookupTable[update.value] !== undefined) {
ret.disable();
Promise.resolve(onchange({
value: update.value,
type: binding.type,
prop: binding.prop,
traces: binding.traces,
index: ret.lookupTable[update.value]
})).then(ret.enable, ret.enable);
}
}
return update.changed;
};
var checkEvents = [
'plotly_relayout',
'plotly_redraw',
'plotly_restyle',
'plotly_update',
'plotly_animatingframe',
'plotly_afterplot'
];
for(var i = 0; i < checkEvents.length; i++) {
gd._internalOn(checkEvents[i], ret.check);
}
ret.remove = function() {
for(var i = 0; i < checkEvents.length; i++) {
gd._removeInternalListener(checkEvents[i], ret.check);
}
};
} else {
// TODO: It'd be really neat to actually give a *reason* for this, but at least a warning
// is a start
Lib.log('Unable to automatically bind plot updates to API command');
ret.lookupTable = {};
ret.remove = function() {};
}
ret.disable = function disable() {
enabled = false;
};
ret.enable = function enable() {
enabled = true;
};
if(container) {
container._commandObserver = ret;
}
return ret;
};
/*
* This function checks to see if an array of objects containing
* method and args properties is compatible with automatic two-way
* binding. The criteria right now are that
*
* 1. multiple traces may be affected
* 2. only one property may be affected
* 3. the same property must be affected by all commands
*/
exports.hasSimpleAPICommandBindings = function(gd, commandList, bindingsByValue) {
var i;
var n = commandList.length;
var refBinding;
for(i = 0; i < n; i++) {
var binding;
var command = commandList[i];
var method = command.method;
var args = command.args;
if(!Array.isArray(args)) args = [];
// If any command has no method, refuse to bind:
if(!method) {
return false;
}
var bindings = exports.computeAPICommandBindings(gd, method, args);
// Right now, handle one and *only* one property being set:
if(bindings.length !== 1) {
return false;
}
if(!refBinding) {
refBinding = bindings[0];
if(Array.isArray(refBinding.traces)) {
refBinding.traces.sort();
}
} else {
binding = bindings[0];
if(binding.type !== refBinding.type) {
return false;
}
if(binding.prop !== refBinding.prop) {
return false;
}
if(Array.isArray(refBinding.traces)) {
if(Array.isArray(binding.traces)) {
binding.traces.sort();
for(var j = 0; j < refBinding.traces.length; j++) {
if(refBinding.traces[j] !== binding.traces[j]) {
return false;
}
}
} else {
return false;
}
} else {
if(binding.prop !== refBinding.prop) {
return false;
}
}
}
binding = bindings[0];
var value = binding.value;
if(Array.isArray(value)) {
if(value.length === 1) {
value = value[0];
} else {
return false;
}
}
if(bindingsByValue) {
bindingsByValue[value] = i;
}
}
return refBinding;
};
function bindingValueHasChanged(gd, binding, cache) {
var container, value, obj;
var changed = false;
if(binding.type === 'data') {
// If it's data, we need to get a trace. Based on the limited scope
// of what we cover, we can just take the first trace from the list,
// or otherwise just the first trace:
container = gd._fullData[binding.traces !== null ? binding.traces[0] : 0];
} else if(binding.type === 'layout') {
container = gd._fullLayout;
} else {
return false;
}
value = Lib.nestedProperty(container, binding.prop).get();
obj = cache[binding.type] = cache[binding.type] || {};
if(obj.hasOwnProperty(binding.prop)) {
if(obj[binding.prop] !== value) {
changed = true;
}
}
obj[binding.prop] = value;
return {
changed: changed,
value: value
};
}
/*
* Execute an API command. There's really not much to this; it just provides
* a common hook so that implementations don't need to be synchronized across
* multiple components with the ability to invoke API commands.
*
* @param {string} method
* The name of the plotly command to execute. Must be one of 'animate',
* 'restyle', 'relayout', 'update'.
* @param {array} args
* A list of arguments passed to the API command
*/
exports.executeAPICommand = function(gd, method, args) {
if(method === 'skip') return Promise.resolve();
var apiMethod = Plotly[method];
var allArgs = [gd];
if(!Array.isArray(args)) args = [];
for(var i = 0; i < args.length; i++) {
allArgs.push(args[i]);
}
return apiMethod.apply(null, allArgs).catch(function(err) {
Lib.warn('API call to Plotly.' + method + ' rejected.', err);
return Promise.reject(err);
});
};
exports.computeAPICommandBindings = function(gd, method, args) {
var bindings;
if(!Array.isArray(args)) args = [];
switch(method) {
case 'restyle':
bindings = computeDataBindings(gd, args);
break;
case 'relayout':
bindings = computeLayoutBindings(gd, args);
break;
case 'update':
bindings = computeDataBindings(gd, [args[0], args[2]])
.concat(computeLayoutBindings(gd, [args[1]]));
break;
case 'animate':
bindings = computeAnimateBindings(gd, args);
break;
default:
// This is the case where intelligent logic about what affects
// this command is not implemented. It causes no ill effects.
// For example, addFrames simply won't bind to a control component.
bindings = [];
}
return bindings;
};
function computeAnimateBindings(gd, args) {
// We'll assume that the only relevant modification an animation
// makes that's meaningfully tracked is the frame:
if(Array.isArray(args[0]) && args[0].length === 1 && ['string', 'number'].indexOf(typeof args[0][0]) !== -1) {
return [{type: 'layout', prop: '_currentFrame', value: args[0][0].toString()}];
} else {
return [];
}
}
function computeLayoutBindings(gd, args) {
var bindings = [];
var astr = args[0];
var aobj = {};
if(typeof astr === 'string') {
aobj[astr] = args[1];
} else if(Lib.isPlainObject(astr)) {
aobj = astr;
} else {
return bindings;
}
crawl(aobj, function(path, attrName, attr) {
bindings.push({type: 'layout', prop: path, value: attr});
}, '', 0);
return bindings;
}
function computeDataBindings(gd, args) {
var traces, astr, val, aobj;
var bindings = [];
// Logic copied from Plotly.restyle:
astr = args[0];
val = args[1];
traces = args[2];
aobj = {};
if(typeof astr === 'string') {
aobj[astr] = val;
} else if(Lib.isPlainObject(astr)) {
// the 3-arg form
aobj = astr;
if(traces === undefined) {
traces = val;
}
} else {
return bindings;
}
if(traces === undefined) {
// Explicitly assign this to null instead of undefined:
traces = null;
}
crawl(aobj, function(path, attrName, attr) {
var thisTraces;
if(Array.isArray(attr)) {
var nAttr = Math.min(attr.length, gd.data.length);
if(traces) {
nAttr = Math.min(nAttr, traces.length);
}
thisTraces = [];
for(var j = 0; j < nAttr; j++) {
thisTraces[j] = traces ? traces[j] : j;
}
} else {
thisTraces = traces ? traces.slice(0) : null;
}
// Convert [7] to just 7 when traces is null:
if(thisTraces === null) {
if(Array.isArray(attr)) {
attr = attr[0];
}
} else if(Array.isArray(thisTraces)) {
if(!Array.isArray(attr)) {
var tmp = attr;
attr = [];
for(var i = 0; i < thisTraces.length; i++) {
attr[i] = tmp;
}
}
attr.length = Math.min(thisTraces.length, attr.length);
}
bindings.push({
type: 'data',
prop: path,
traces: thisTraces,
value: attr
});
}, '', 0);
return bindings;
}
function crawl(attrs, callback, path, depth) {
Object.keys(attrs).forEach(function(attrName) {
var attr = attrs[attrName];
if(attrName[0] === '_') return;
var thisPath = path + (depth > 0 ? '.' : '') + attrName;
if(Lib.isPlainObject(attr)) {
crawl(attr, callback, thisPath, depth + 1);
} else {
// Only execute the callback on leaf nodes:
callback(thisPath, attrName, attr);
}
});
}