1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- /**
- * Utility to register plugins and common namespace for keeping reference to all plugins classes
- */
- import Hooks from './pluginHooks';
- import { objectEach } from './helpers/object';
- import { toUpperCaseFirst } from './helpers/string';
- var registeredPlugins = new WeakMap();
- /**
- * Registers plugin under given name
- *
- * @param {String} pluginName
- * @param {Function} PluginClass
- */
- function registerPlugin(pluginName, PluginClass) {
- pluginName = toUpperCaseFirst(pluginName);
- Hooks.getSingleton().add('construct', function () {
- var holder = void 0;
- if (!registeredPlugins.has(this)) {
- registeredPlugins.set(this, {});
- }
- holder = registeredPlugins.get(this);
- if (!holder[pluginName]) {
- holder[pluginName] = new PluginClass(this);
- }
- });
- Hooks.getSingleton().add('afterDestroy', function () {
- if (registeredPlugins.has(this)) {
- var pluginsHolder = registeredPlugins.get(this);
- objectEach(pluginsHolder, function (plugin) {
- return plugin.destroy();
- });
- registeredPlugins.delete(this);
- }
- });
- }
- /**
- * @param {Object} instance
- * @param {String|Function} pluginName
- * @returns {Function} pluginClass Returns plugin instance if exists or `undefined` if not exists.
- */
- function getPlugin(instance, pluginName) {
- if (typeof pluginName != 'string') {
- throw Error('Only strings can be passed as "plugin" parameter');
- }
- var _pluginName = toUpperCaseFirst(pluginName);
- if (!registeredPlugins.has(instance) || !registeredPlugins.get(instance)[_pluginName]) {
- return void 0;
- }
- return registeredPlugins.get(instance)[_pluginName];
- }
- /**
- * Get all registred plugins names for concrete Handsontable instance.
- *
- * @param {Object} hotInstance
- * @returns {Array}
- */
- function getRegistredPluginNames(hotInstance) {
- return registeredPlugins.has(hotInstance) ? Object.keys(registeredPlugins.get(hotInstance)) : [];
- }
- /**
- * Get plugin name.
- *
- * @param {Object} hotInstance
- * @param {Object} plugin
- * @returns {String|null}
- */
- function getPluginName(hotInstance, plugin) {
- var pluginName = null;
- if (registeredPlugins.has(hotInstance)) {
- objectEach(registeredPlugins.get(hotInstance), function (pluginInstance, name) {
- if (pluginInstance === plugin) {
- pluginName = name;
- }
- });
- }
- return pluginName;
- }
- export { registerPlugin, getPlugin, getRegistredPluginNames, getPluginName };
|