plugins.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. 'use strict';
  2. exports.__esModule = true;
  3. exports.getPluginName = exports.getRegistredPluginNames = exports.getPlugin = exports.registerPlugin = undefined;
  4. var _pluginHooks = require('./pluginHooks');
  5. var _pluginHooks2 = _interopRequireDefault(_pluginHooks);
  6. var _object = require('./helpers/object');
  7. var _string = require('./helpers/string');
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  9. var registeredPlugins = new WeakMap();
  10. /**
  11. * Registers plugin under given name
  12. *
  13. * @param {String} pluginName
  14. * @param {Function} PluginClass
  15. */
  16. /**
  17. * Utility to register plugins and common namespace for keeping reference to all plugins classes
  18. */
  19. function registerPlugin(pluginName, PluginClass) {
  20. pluginName = (0, _string.toUpperCaseFirst)(pluginName);
  21. _pluginHooks2.default.getSingleton().add('construct', function () {
  22. var holder = void 0;
  23. if (!registeredPlugins.has(this)) {
  24. registeredPlugins.set(this, {});
  25. }
  26. holder = registeredPlugins.get(this);
  27. if (!holder[pluginName]) {
  28. holder[pluginName] = new PluginClass(this);
  29. }
  30. });
  31. _pluginHooks2.default.getSingleton().add('afterDestroy', function () {
  32. if (registeredPlugins.has(this)) {
  33. var pluginsHolder = registeredPlugins.get(this);
  34. (0, _object.objectEach)(pluginsHolder, function (plugin) {
  35. return plugin.destroy();
  36. });
  37. registeredPlugins.delete(this);
  38. }
  39. });
  40. }
  41. /**
  42. * @param {Object} instance
  43. * @param {String|Function} pluginName
  44. * @returns {Function} pluginClass Returns plugin instance if exists or `undefined` if not exists.
  45. */
  46. function getPlugin(instance, pluginName) {
  47. if (typeof pluginName != 'string') {
  48. throw Error('Only strings can be passed as "plugin" parameter');
  49. }
  50. var _pluginName = (0, _string.toUpperCaseFirst)(pluginName);
  51. if (!registeredPlugins.has(instance) || !registeredPlugins.get(instance)[_pluginName]) {
  52. return void 0;
  53. }
  54. return registeredPlugins.get(instance)[_pluginName];
  55. }
  56. /**
  57. * Get all registred plugins names for concrete Handsontable instance.
  58. *
  59. * @param {Object} hotInstance
  60. * @returns {Array}
  61. */
  62. function getRegistredPluginNames(hotInstance) {
  63. return registeredPlugins.has(hotInstance) ? Object.keys(registeredPlugins.get(hotInstance)) : [];
  64. }
  65. /**
  66. * Get plugin name.
  67. *
  68. * @param {Object} hotInstance
  69. * @param {Object} plugin
  70. * @returns {String|null}
  71. */
  72. function getPluginName(hotInstance, plugin) {
  73. var pluginName = null;
  74. if (registeredPlugins.has(hotInstance)) {
  75. (0, _object.objectEach)(registeredPlugins.get(hotInstance), function (pluginInstance, name) {
  76. if (pluginInstance === plugin) {
  77. pluginName = name;
  78. }
  79. });
  80. }
  81. return pluginName;
  82. }
  83. exports.registerPlugin = registerPlugin;
  84. exports.getPlugin = getPlugin;
  85. exports.getRegistredPluginNames = getRegistredPluginNames;
  86. exports.getPluginName = getPluginName;