-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathplot_schema.js
324 lines (249 loc) · 9.12 KB
/
plot_schema.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
/**
* Copyright 2012-2016, 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 Plots = require('../plots/plots');
var Lib = require('../lib');
var extendFlat = Lib.extendFlat;
var extendDeep = Lib.extendDeep;
var extendDeepAll = Lib.extendDeepAll;
var NESTED_MODULE = '_nestedModules',
COMPOSED_MODULE = '_composedModules',
IS_SUBPLOT_OBJ = '_isSubplotObj',
IS_LINKED_TO_ARRAY = '_isLinkedToArray',
DEPRECATED = '_deprecated';
// list of underscore attributes to keep in schema as is
var UNDERSCORE_ATTRS = [IS_SUBPLOT_OBJ, IS_LINKED_TO_ARRAY, DEPRECATED];
var plotSchema = {
traces: {},
layout: {},
defs: {}
};
// FIXME polar attribute are not part of Plotly yet
var polarAreaAttrs = require('../plots/polar/area_attributes'),
polarAxisAttrs = require('../plots/polar/axis_attributes');
var PlotSchema = module.exports = {};
PlotSchema.get = function() {
Plots.allTypes
.concat('area') // FIXME polar 'area' attributes
.forEach(getTraceAttributes);
getLayoutAttributes();
getDefs();
return plotSchema;
};
PlotSchema.crawl = function(attrs, callback) {
Object.keys(attrs).forEach(function(attrName) {
var attr = attrs[attrName];
if(UNDERSCORE_ATTRS.indexOf(attrName) !== -1) return;
callback(attr, attrName, attrs);
if(PlotSchema.isValObject(attr)) return;
if(Lib.isPlainObject(attr)) PlotSchema.crawl(attr, callback);
});
};
PlotSchema.isValObject = function(obj) {
return obj && obj.valType !== undefined;
};
function getTraceAttributes(type) {
var globalAttributes = Plots.attributes,
_module = getModule({type: type}),
meta = getMeta(type),
subplotRegistry = getSubplotRegistry(type);
var attributes = {},
layoutAttributes = {};
// make 'type' the first attribute in the object
attributes.type = null;
// global attributes (same for all trace types)
extendDeep(attributes, globalAttributes);
// module attributes (+ nested + composed)
attributes = coupleAttrs(
_module.attributes, attributes, 'attributes', type
);
// subplot attributes
if(subplotRegistry.attributes !== undefined) {
extendDeep(attributes, subplotRegistry.attributes);
}
// 'type' gets overwritten by globalAttributes; reset it here
attributes.type = type;
attributes = removeUnderscoreAttrs(attributes);
mergeValTypeAndRole(attributes);
plotSchema.traces[type] = extendFlat({},
meta,
{ attributes: attributes }
);
// trace-specific layout attributes
if(_module.layoutAttributes !== undefined) {
layoutAttributes = coupleAttrs(
_module.layoutAttributes, layoutAttributes, 'layoutAttributes', type
);
mergeValTypeAndRole(layoutAttributes);
plotSchema.traces[type].layoutAttributes = layoutAttributes;
}
}
function getLayoutAttributes() {
var globalLayoutAttributes = Plots.layoutAttributes,
layoutAttributes = {};
// layout module attributes (+ nested + composed)
layoutAttributes = coupleAttrs(
globalLayoutAttributes, layoutAttributes, 'layoutAttributes', '*'
);
// FIXME polar layout attributes
layoutAttributes = assignPolarLayoutAttrs(layoutAttributes);
// add IS_SUBPLOT_OBJ attribute
layoutAttributes = handleSubplotObjs(layoutAttributes);
layoutAttributes = removeUnderscoreAttrs(layoutAttributes);
mergeValTypeAndRole(layoutAttributes);
// generate IS_LINKED_TO_ARRAY structure
handleLinkedToArray(layoutAttributes);
plotSchema.layout = { layoutAttributes: layoutAttributes };
}
function getDefs() {
plotSchema.defs = {
valObjects: Lib.valObjects,
metaKeys: UNDERSCORE_ATTRS.concat(['description', 'role'])
};
}
function coupleAttrs(attrsIn, attrsOut, whichAttrs, type) {
var nestedModule, nestedAttrs, nestedReference,
composedModule, composedAttrs;
Object.keys(attrsIn).forEach(function(k) {
if(k === NESTED_MODULE) {
Object.keys(attrsIn[k]).forEach(function(kk) {
nestedModule = getModule({module: attrsIn[k][kk]});
if(nestedModule === undefined) return;
nestedAttrs = nestedModule[whichAttrs];
nestedReference = coupleAttrs(
nestedAttrs, {}, whichAttrs, type
);
Lib.nestedProperty(attrsOut, kk)
.set(extendDeep({}, nestedReference));
});
return;
}
if(k === COMPOSED_MODULE) {
Object.keys(attrsIn[k]).forEach(function(kk) {
if(kk !== type) return;
composedModule = getModule({module: attrsIn[k][kk]});
if(composedModule === undefined) return;
composedAttrs = composedModule[whichAttrs];
composedAttrs = coupleAttrs(
composedAttrs, {}, whichAttrs, type
);
extendDeepAll(attrsOut, composedAttrs);
});
return;
}
attrsOut[k] = Lib.isPlainObject(attrsIn[k]) ?
extendDeepAll({}, attrsIn[k]) :
attrsIn[k];
});
return attrsOut;
}
function mergeValTypeAndRole(attrs) {
function makeSrcAttr(attrName) {
return {
valType: 'string',
role: 'info',
description: [
'Sets the source reference on plot.ly for ',
attrName, '.'
].join(' ')
};
}
function callback(attr, attrName, attrs) {
if(PlotSchema.isValObject(attr)) {
if(attr.valType === 'data_array') {
// all 'data_array' attrs have role 'data'
attr.role = 'data';
// all 'data_array' attrs have a corresponding 'src' attr
attrs[attrName + 'src'] = makeSrcAttr(attrName);
}
else if(attr.arrayOk === true) {
// all 'arrayOk' attrs have a corresponding 'src' attr
attrs[attrName + 'src'] = makeSrcAttr(attrName);
}
}
else if(Lib.isPlainObject(attr)) {
// all attrs container objects get role 'object'
attr.role = 'object';
}
}
PlotSchema.crawl(attrs, callback);
}
// helper methods
function getModule(arg) {
if('type' in arg) {
return (arg.type === 'area') ? // FIXME
{ attributes: polarAreaAttrs } :
Plots.getModule({type: arg.type});
}
var subplotsRegistry = Plots.subplotsRegistry,
_module = arg.module;
if(subplotsRegistry[_module]) return subplotsRegistry[_module];
else if('module' in arg) return Plotly[_module];
}
function removeUnderscoreAttrs(attributes) {
Object.keys(attributes).forEach(function(k) {
if(k.charAt(0) === '_' &&
UNDERSCORE_ATTRS.indexOf(k) === -1) delete attributes[k];
});
return attributes;
}
function getMeta(type) {
if(type === 'area') return {}; // FIXME
return Plots.modules[type].meta || {};
}
function assignPolarLayoutAttrs(layoutAttributes) {
extendFlat(layoutAttributes, {
radialaxis: polarAxisAttrs.radialaxis,
angularaxis: polarAxisAttrs.angularaxis
});
extendFlat(layoutAttributes, polarAxisAttrs.layout);
return layoutAttributes; // FIXME
}
function getSubplotRegistry(traceType) {
if(traceType === 'area') return {}; // FIXME
var subplotsRegistry = Plots.subplotsRegistry,
subplotType = Object.keys(subplotsRegistry).filter(function(subplotType) {
return Plots.traceIs({type: traceType}, subplotType);
})[0];
if(subplotType === undefined) return {};
return subplotsRegistry[subplotType];
}
function handleSubplotObjs(layoutAttributes) {
var subplotsRegistry = Plots.subplotsRegistry;
Object.keys(layoutAttributes).forEach(function(k) {
Object.keys(subplotsRegistry).forEach(function(subplotType) {
var subplotRegistry = subplotsRegistry[subplotType],
isSubplotObj;
if(!subplotRegistry.attrRegex) return;
if(subplotType === 'cartesian' || subplotType === 'gl2d') {
isSubplotObj = (
subplotRegistry.attrRegex.x.test(k) ||
subplotRegistry.attrRegex.y.test(k)
);
}
else {
isSubplotObj = subplotRegistry.attrRegex.test(k);
}
if(isSubplotObj) layoutAttributes[k][IS_SUBPLOT_OBJ] = true;
});
});
return layoutAttributes;
}
function handleLinkedToArray(layoutAttributes) {
function callback(attr, attrName, attrs) {
if(attr[IS_LINKED_TO_ARRAY] !== true) return;
// TODO more robust logic
var itemName = attrName.substr(0, attrName.length - 1);
delete attr[IS_LINKED_TO_ARRAY];
attrs[attrName] = { items: {} };
attrs[attrName].items[itemName] = attr;
attrs[attrName].role = 'object';
}
PlotSchema.crawl(layoutAttributes, callback);
}