-
Notifications
You must be signed in to change notification settings - Fork 3k
/
base.js
309 lines (261 loc) · 7.93 KB
/
base.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
import { defineGetter, objectEach } from '../../helpers/object';
import { arrayEach } from '../../helpers/array';
import { getPluginsNames, hasPlugin } from '../registry';
import { hasCellType } from '../../cellTypes/registry';
import { hasEditor } from '../../editors/registry';
import { hasRenderer } from '../../renderers/registry';
import { hasValidator } from '../../validators/registry';
const DEPS_TYPE_CHECKERS = new Map([
['plugin', hasPlugin],
['cell-type', hasCellType],
['editor', hasEditor],
['renderer', hasRenderer],
['validator', hasValidator],
]);
export const PLUGIN_KEY = 'base';
const privatePool = new WeakMap();
const missingDependeciesMsgs = [];
let initializedPlugins = null;
/**
* @util
* @property {Core} hot Handsontable instance.
*/
export class BasePlugin {
static get PLUGIN_KEY() {
return PLUGIN_KEY;
}
/**
* The `SETTING_KEYS` getter defines the keys that, when present in the config object, trigger the plugin update
* after the `updateSettings` calls.
* - When it returns `true`, the plugin updates after all `updateSettings` calls, regardless of the contents of the
* config object.
* - When it returns `false`, the plugin never updates on `updateSettings` calls.
*
* @returns {string[] | boolean}
*/
static get SETTING_KEYS() {
return [
this.PLUGIN_KEY
];
}
/**
* @param {object} hotInstance Handsontable instance.
*/
constructor(hotInstance) {
/**
* Handsontable instance.
*
* @type {Core}
*/
defineGetter(this, 'hot', hotInstance, {
writable: false
});
privatePool.set(this, { hooks: {} });
initializedPlugins = null;
this.pluginName = null;
this.pluginsInitializedCallbacks = [];
this.isPluginsReady = false;
this.enabled = false;
this.initialized = false;
this.hot.addHook('afterPluginsInitialized', () => this.onAfterPluginsInitialized());
this.hot.addHook('afterUpdateSettings', newSettings => this.onUpdateSettings(newSettings));
this.hot.addHook('beforeInit', () => this.init());
}
init() {
this.pluginName = this.hot.getPluginName(this);
const pluginDeps = this.constructor.PLUGIN_DEPS;
const dependecies = Array.isArray(pluginDeps) ? pluginDeps : [];
if (dependecies.length > 0) {
const missingDependencies = [];
dependecies.forEach((dependency) => {
const [type, moduleName] = dependency.split(':');
if (!DEPS_TYPE_CHECKERS.has(type)) {
throw new Error(`Unknown plugin dependency type "${type}" was found.`);
}
if (!DEPS_TYPE_CHECKERS.get(type)(moduleName)) {
missingDependencies.push(` - ${moduleName} (${type})`);
}
});
if (missingDependencies.length > 0) {
const errorMsg = [
`The ${this.pluginName} plugin requires the following modules:\n`,
`${missingDependencies.join('\n')}\n`,
].join('');
missingDependeciesMsgs.push(errorMsg);
}
}
if (!initializedPlugins) {
initializedPlugins = getPluginsNames();
}
// Workaround for the UndoRedo plugin which, currently doesn't follow the plugin architecture.
// Without this line the `callOnPluginsReady` callback won't be triggered after all plugin
// initialization.
if (initializedPlugins.indexOf('UndoRedo') >= 0) {
initializedPlugins.splice(initializedPlugins.indexOf('UndoRedo'), 1);
}
if (initializedPlugins.indexOf(this.pluginName) >= 0) {
initializedPlugins.splice(initializedPlugins.indexOf(this.pluginName), 1);
}
this.hot.addHookOnce('afterPluginsInitialized', () => {
if (this.isEnabled && this.isEnabled()) {
this.enablePlugin();
}
});
const isAllPluginsAreInitialized = initializedPlugins.length === 0;
if (isAllPluginsAreInitialized) {
if (missingDependeciesMsgs.length > 0) {
const errorMsg = [
`${missingDependeciesMsgs.join('\n')}\n`,
'You have to import and register them manually.',
].join('');
throw new Error(errorMsg);
}
this.hot.runHooks('afterPluginsInitialized');
}
this.initialized = true;
}
/**
* Enable plugin for this Handsontable instance.
*/
enablePlugin() {
this.enabled = true;
}
/**
* Disable plugin for this Handsontable instance.
*/
disablePlugin() {
if (this.eventManager) {
this.eventManager.clear();
}
this.clearHooks();
this.enabled = false;
}
/**
* Add listener to plugin hooks system.
*
* @param {string} name The hook name.
* @param {Function} callback The listener function to add.
*/
addHook(name, callback) {
privatePool.get(this).hooks[name] = (privatePool.get(this).hooks[name] || []);
const hooks = privatePool.get(this).hooks[name];
this.hot.addHook(name, callback);
hooks.push(callback);
privatePool.get(this).hooks[name] = hooks;
}
/**
* Remove all hooks listeners by hook name.
*
* @param {string} name The hook name.
*/
removeHooks(name) {
arrayEach(privatePool.get(this).hooks[name] || [], (callback) => {
this.hot.removeHook(name, callback);
});
}
/**
* Clear all hooks.
*/
clearHooks() {
const hooks = privatePool.get(this).hooks;
objectEach(hooks, (callbacks, name) => this.removeHooks(name));
hooks.length = 0;
}
/**
* Register function which will be immediately called after all plugins initialized.
*
* @param {Function} callback The listener function to call.
*/
callOnPluginsReady(callback) {
if (this.isPluginsReady) {
callback();
} else {
this.pluginsInitializedCallbacks.push(callback);
}
}
/**
* Check if any of the keys defined in `SETTING_KEYS` configuration of the plugin is present in the provided
* config object, or if the `SETTING_KEYS` configuration states that the plugin is relevant to the config object
* regardless of its contents.
*
* @private
* @param {Handsontable.DefaultSettings} settings The config object passed to `updateSettings`.
* @returns {boolean}
*/
#isRelevantToSettings(settings) {
if (!settings) {
return false;
}
const settingKeys = this.constructor.SETTING_KEYS;
// If SETTING_KEYS is declared as `true` -> update the plugin regardless of the settings declared in
// `updateSettings`.
// If SETTING_KEYS is declared as `false` -> DON'T update the plugin regardless of the settings declared in
// `updateSettings`.
if (typeof settingKeys === 'boolean') {
return settingKeys;
}
for (let i = 0; i < settingKeys.length; i++) {
if (settings[settingKeys[i]] !== void 0) {
return true;
}
}
return false;
}
/**
* On after plugins initialized listener.
*
* @private
*/
onAfterPluginsInitialized() {
arrayEach(this.pluginsInitializedCallbacks, callback => callback());
this.pluginsInitializedCallbacks.length = 0;
this.isPluginsReady = true;
}
/**
* On update settings listener.
*
* @private
* @param {object} newSettings New set of settings passed to the `updateSettings` method.
*/
onUpdateSettings(newSettings) {
const relevantToSettings = this.#isRelevantToSettings(newSettings);
if (this.isEnabled) {
if (this.enabled && !this.isEnabled()) {
this.disablePlugin();
}
if (!this.enabled && this.isEnabled()) {
this.enablePlugin();
}
if (
this.enabled &&
this.isEnabled() &&
relevantToSettings
) {
this.updatePlugin(newSettings);
}
}
}
/**
* Updates the plugin to use the latest options you have specified.
*
* @private
*/
updatePlugin() {
}
/**
* Destroy plugin.
*/
destroy() {
if (this.eventManager) {
this.eventManager.destroy();
}
this.clearHooks();
objectEach(this, (value, property) => {
if (property !== 'hot') {
this[property] = null;
}
});
delete this.t;
delete this.hot;
}
}