forked from plotly/plotly.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_template.js
More file actions
138 lines (111 loc) · 3.85 KB
/
Copy pathplot_template.js
File metadata and controls
138 lines (111 loc) · 3.85 KB
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
/**
* 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 Lib = require('@src/lib');
var PlotSchema = require('@src/plot_api/plot_schema');
var Plots = require('@src/plots/plots');
exports.makeTemplate = function(figure) {
var template = {};
var data = figure.data || [];
var layout = figure.layout || {};
var graphDiv = {
data: data,
layout: layout,
_context: {locales: {}, locale: 'en-US'}
};
Plots.supplyDefaults(graphDiv);
data.forEach(function(trace, index) {
if(!template.data) {
template.data = [];
}
// TODO: What if no style info is extracted for this trace. We may
// not want an empty object as the null value.
var traceTemplate = template.data[index] = {};
// TODO: you can't assume data[index] will match to fullData[index]
// which this next index operation is assuming.
walkStyleKeys(trace, getTraceInfo.bind(null, trace), function(attr, path) {
// NOTE: we are extracting the value from fullData so templates
// will be validated to some degree by a supplyDefaults step.
// TODO: Keep this behaviour or copy over the user data?
var value = Lib.nestedProperty(trace, path).get();
var templateProp = Lib.nestedProperty(traceTemplate, path);
templateProp.set(value);
});
});
walkStyleKeys(layout, getLayoutInfo.bind(null, layout), function(attr, path) {
var layoutTemplate = template.layout;
if(!template.layout) {
layoutTemplate = template.layout = {};
}
// NOTE: we are extracting the value from fullData so templates
// will be validated to some degree by a supplyDefaults step.
// TODO: Keep this behaviour or copy over the user data?
var value = Lib.nestedProperty(layout, path).get();
var templateProp = Lib.nestedProperty(layoutTemplate, path);
templateProp.set(value);
});
return template;
};
exports.applyTemplate = function(figure, template) {
var data = figure.data || [];
var layout = figure.layout || {};
var graphDiv = {
data: data,
layout: layout,
_context: {locales: {}, locale: 'en-US'}
};
Plots.supplyDefaults(graphDiv);
var fullData = graphDiv._fullData;
var fullLayout = graphDiv._fullLayout;
var results = {};
results.figure = Lib.extendDeepNoArrays(figure);
return results;
};
function walkStyleKeys(parent, getAttributeInfo, callback, path) {
Object.keys(parent).forEach(function(key) {
var child = parent[key];
var nextPath = getNextPath(parent, key, path);
var attr = getAttributeInfo(nextPath);
if(!attr) {
return;
}
if(attr.valType === 'data_array' ||
(attr.arrayOk && Array.isArray(child))) {
return;
}
if(Lib.isPlainObject(child) || Array.isArray(child)) {
walkStyleKeys(child, getAttributeInfo, callback, nextPath);
return;
}
if(attr.role !== 'style') {
return;
}
callback(attr, nextPath);
});
}
function getLayoutInfo(fullLayout, path) {
return PlotSchema.getLayoutValObject(
fullLayout, Lib.nestedProperty({}, path).parts
);
}
function getTraceInfo(fullTrace, path) {
return PlotSchema.getTraceValObject(
fullTrace, Lib.nestedProperty({}, path).parts
);
}
function getNextPath(parent, key, path) {
var nextPath;
if(!path || path.length === 0) {
nextPath = key;
} else if(Array.isArray(parent)) {
nextPath = path + '[' + key + ']';
} else {
nextPath = path + '.' + key;
}
return nextPath;
}