-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathregistry.js
166 lines (140 loc) · 4.56 KB
/
registry.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
/**
* Copyright 2012-2017, 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('./lib');
var basePlotAttributes = require('./plots/attributes');
exports.modules = {};
exports.allCategories = {};
exports.allTypes = [];
exports.subplotsRegistry = {};
exports.transformsRegistry = {};
exports.componentsRegistry = {};
exports.layoutArrayContainers = [];
/**
* register a module as the handler for a trace type
*
* @param {object} _module the module that will handle plotting this trace type
* @param {string} thisType
* @param {array of strings} categoriesIn all the categories this type is in,
* tested by calls: traceIs(trace, oneCategory)
* @param {object} meta meta information about the trace type
*/
exports.register = function(_module, thisType, categoriesIn, meta) {
if(exports.modules[thisType]) {
Lib.log('Type ' + thisType + ' already registered');
return;
}
var categoryObj = {};
for(var i = 0; i < categoriesIn.length; i++) {
categoryObj[categoriesIn[i]] = true;
exports.allCategories[categoriesIn[i]] = true;
}
exports.modules[thisType] = {
_module: _module,
categories: categoryObj
};
if(meta && Object.keys(meta).length) {
exports.modules[thisType].meta = meta;
}
exports.allTypes.push(thisType);
};
/**
* register a subplot type
*
* @param {object} _module subplot module:
*
* @param {string or array of strings} attr
* attribute name in traces and layout
* @param {string or array of strings} idRoot
* root of id (setting the possible value for attrName)
* @param {object} attributes
* attribute(s) for traces of this subplot type
*
* In trace objects `attr` is the object key taking a valid `id` as value
* (the set of all valid ids is generated below and stored in idRegex).
*
* In the layout object, a or several valid `attr` name(s) can be keys linked
* to a nested attribute objects
* (the set of all valid attr names is generated below and stored in attrRegex).
*/
exports.registerSubplot = function(_module) {
var plotType = _module.name;
if(exports.subplotsRegistry[plotType]) {
Lib.log('Plot type ' + plotType + ' already registered.');
return;
}
// not sure what's best for the 'cartesian' type at this point
exports.subplotsRegistry[plotType] = _module;
};
exports.registerComponent = function(_module) {
var name = _module.name;
exports.componentsRegistry[name] = _module;
if(_module.layoutAttributes && _module.layoutAttributes._isLinkedToArray) {
Lib.pushUnique(exports.layoutArrayContainers, name);
}
};
/**
* Get registered module using trace object or trace type
*
* @param {object||string} trace
* trace object with prop 'type' or trace type as a string
* @return {object}
* module object corresponding to trace type
*/
exports.getModule = function(trace) {
if(trace.r !== undefined) {
Lib.warn('Tried to put a polar trace ' +
'on an incompatible graph of cartesian ' +
'data. Ignoring this dataset.', trace
);
return false;
}
var _module = exports.modules[getTraceType(trace)];
if(!_module) return false;
return _module._module;
};
/**
* Determine if this trace type is in a given category
*
* @param {object||string} traceType
* a trace (object) or trace type (string)
* @param {string} category
* category in question
* @return {boolean}
*/
exports.traceIs = function(traceType, category) {
traceType = getTraceType(traceType);
// old plot.ly workspace hack, nothing to see here
if(traceType === 'various') return false;
var _module = exports.modules[traceType];
if(!_module) {
if(traceType && traceType !== 'area') {
Lib.log('Unrecognized trace type ' + traceType + '.');
}
_module = exports.modules[basePlotAttributes.type.dflt];
}
return !!_module.categories[category];
};
/**
* Retrieve component module method
*
* @param {string} name
* name of component (as declared in component module)
* @param {string} method
* name of component module method
* @return {function}
*/
exports.getComponentMethod = function(name, method) {
var _module = exports.componentsRegistry[name];
if(!_module) return Lib.noop;
return _module[method];
};
function getTraceType(traceType) {
if(typeof traceType === 'object') traceType = traceType.type;
return traceType;
}