-
Notifications
You must be signed in to change notification settings - Fork 11.9k
/
Copy pathcore.registry.js
186 lines (164 loc) · 4.41 KB
/
core.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import DatasetController from './core.datasetController.js';
import Element from './core.element.js';
import Scale from './core.scale.js';
import TypedRegistry from './core.typedRegistry.js';
import {each, callback as call, _capitalize} from '../helpers/helpers.core.js';
/**
* Please use the module's default export which provides a singleton instance
* Note: class is exported for typedoc
*/
export class Registry {
constructor() {
this.controllers = new TypedRegistry(DatasetController, 'datasets', true);
this.elements = new TypedRegistry(Element, 'elements');
this.plugins = new TypedRegistry(Object, 'plugins');
this.scales = new TypedRegistry(Scale, 'scales');
// Order is important, Scale has Element in prototype chain,
// so Scales must be before Elements. Plugins are a fallback, so not listed here.
this._typedRegistries = [this.controllers, this.scales, this.elements];
}
/**
* @param {...any} args
*/
add(...args) {
this._each('register', args);
}
remove(...args) {
this._each('unregister', args);
}
/**
* @param {...typeof DatasetController} args
*/
addControllers(...args) {
this._each('register', args, this.controllers);
}
/**
* @param {...typeof Element} args
*/
addElements(...args) {
this._each('register', args, this.elements);
}
/**
* @param {...any} args
*/
addPlugins(...args) {
this._each('register', args, this.plugins);
}
/**
* @param {...typeof Scale} args
*/
addScales(...args) {
this._each('register', args, this.scales);
}
/**
* @param {string} id
* @returns {typeof DatasetController}
*/
getController(id) {
return this._get(id, this.controllers, 'controller');
}
/**
* @param {string} id
* @returns {typeof Element}
*/
getElement(id) {
return this._get(id, this.elements, 'element');
}
/**
* @param {string} id
* @returns {object}
*/
getPlugin(id) {
return this._get(id, this.plugins, 'plugin');
}
/**
* @param {string} id
* @returns {typeof Scale}
*/
getScale(id) {
return this._get(id, this.scales, 'scale');
}
/**
* @param {...typeof DatasetController} args
*/
removeControllers(...args) {
this._each('unregister', args, this.controllers);
}
/**
* @param {...typeof Element} args
*/
removeElements(...args) {
this._each('unregister', args, this.elements);
}
/**
* @param {...any} args
*/
removePlugins(...args) {
this._each('unregister', args, this.plugins);
}
/**
* @param {...typeof Scale} args
*/
removeScales(...args) {
this._each('unregister', args, this.scales);
}
/**
* @private
*/
_each(method, args, typedRegistry) {
[...args].forEach(arg => {
const reg = typedRegistry || this._getRegistryForType(arg);
if (typedRegistry || reg.isForType(arg) || (reg === this.plugins && arg.id)) {
this._exec(method, reg, arg);
} else {
// Handle loopable args
// Use case:
// import * as plugins from './plugins.js';
// Chart.register(plugins);
each(arg, item => {
// If there are mixed types in the loopable, make sure those are
// registered in correct registry
// Use case: (treemap exporting controller, elements etc)
// import * as treemap from 'chartjs-chart-treemap.js';
// Chart.register(treemap);
const itemReg = typedRegistry || this._getRegistryForType(item);
this._exec(method, itemReg, item);
});
}
});
}
/**
* @private
*/
_exec(method, registry, component) {
const camelMethod = _capitalize(method);
call(component['before' + camelMethod], [], component); // beforeRegister / beforeUnregister
registry[method](component);
call(component['after' + camelMethod], [], component); // afterRegister / afterUnregister
}
/**
* @private
*/
_getRegistryForType(type) {
for (let i = 0; i < this._typedRegistries.length; i++) {
const reg = this._typedRegistries[i];
if (reg.isForType(type)) {
return reg;
}
}
// plugins is the fallback registry
return this.plugins;
}
/**
* @private
*/
_get(id, typedRegistry, type) {
const item = typedRegistry.get(id);
if (item === undefined) {
throw new Error('"' + id + '" is not a registered ' + type + '.');
}
return item;
}
}
// singleton instance
export default /* #__PURE__ */ new Registry();