-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathregister.js
93 lines (71 loc) · 2.62 KB
/
register.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
/**
* 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 Registry = require('../registry');
var Lib = require('../lib');
module.exports = function register(_modules) {
if(!_modules) {
throw new Error('No argument passed to Plotly.register.');
}
else if(_modules && !Array.isArray(_modules)) {
_modules = [_modules];
}
for(var i = 0; i < _modules.length; i++) {
var newModule = _modules[i];
if(!newModule) {
throw new Error('Invalid module was attempted to be registered!');
}
switch(newModule.moduleType) {
case 'trace':
registerTraceModule(newModule);
break;
case 'transform':
registerTransformModule(newModule);
break;
case 'component':
registerComponentModule(newModule);
break;
default:
throw new Error('Invalid module was attempted to be registered!');
}
}
};
function registerTraceModule(newModule) {
Registry.register(newModule, newModule.name, newModule.categories, newModule.meta);
if(!Registry.subplotsRegistry[newModule.basePlotModule.name]) {
Registry.registerSubplot(newModule.basePlotModule);
}
}
function registerTransformModule(newModule) {
if(typeof newModule.name !== 'string') {
throw new Error('Transform module *name* must be a string.');
}
var prefix = 'Transform module ' + newModule.name;
var hasTransform = typeof newModule.transform === 'function',
hasCalcTransform = typeof newModule.calcTransform === 'function';
if(!hasTransform && !hasCalcTransform) {
throw new Error(prefix + ' is missing a *transform* or *calcTransform* method.');
}
if(hasTransform && hasCalcTransform) {
Lib.log([
prefix + ' has both a *transform* and *calcTransform* methods.',
'Please note that all *transform* methods are executed',
'before all *calcTransform* methods.'
].join(' '));
}
if(!Lib.isPlainObject(newModule.attributes)) {
Lib.log(prefix + ' registered without an *attributes* object.');
}
if(typeof newModule.supplyDefaults !== 'function') {
Lib.log(prefix + ' registered without a *supplyDefaults* method.');
}
Registry.transformsRegistry[newModule.name] = newModule;
}
function registerComponentModule(newModule) {
Registry.componentsRegistry[newModule.name] = newModule;
}