_base.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. 'use strict';
  2. exports.__esModule = true;
  3. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  4. var _object = require('./../helpers/object');
  5. var _array = require('./../helpers/array');
  6. var _recordTranslator = require('./../utils/recordTranslator');
  7. var _plugins = require('./../plugins');
  8. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  9. var privatePool = new WeakMap();
  10. var initializedPlugins = null;
  11. /**
  12. * @private
  13. */
  14. var BasePlugin = function () {
  15. /**
  16. * @param {Object} hotInstance Handsontable instance.
  17. */
  18. function BasePlugin(hotInstance) {
  19. var _this = this;
  20. _classCallCheck(this, BasePlugin);
  21. /**
  22. * Handsontable instance.
  23. *
  24. * @type {Core}
  25. */
  26. (0, _object.defineGetter)(this, 'hot', hotInstance, {
  27. writable: false
  28. });
  29. (0, _object.defineGetter)(this, 't', (0, _recordTranslator.getTranslator)(hotInstance), {
  30. writable: false
  31. });
  32. privatePool.set(this, { hooks: {} });
  33. initializedPlugins = null;
  34. this.pluginName = null;
  35. this.pluginsInitializedCallbacks = [];
  36. this.isPluginsReady = false;
  37. this.enabled = false;
  38. this.initialized = false;
  39. this.hot.addHook('afterPluginsInitialized', function () {
  40. return _this.onAfterPluginsInitialized();
  41. });
  42. this.hot.addHook('afterUpdateSettings', function () {
  43. return _this.onUpdateSettings();
  44. });
  45. this.hot.addHook('beforeInit', function () {
  46. return _this.init();
  47. });
  48. }
  49. _createClass(BasePlugin, [{
  50. key: 'init',
  51. value: function init() {
  52. this.pluginName = (0, _plugins.getPluginName)(this.hot, this);
  53. if (this.isEnabled && this.isEnabled()) {
  54. this.enablePlugin();
  55. }
  56. if (!initializedPlugins) {
  57. initializedPlugins = (0, _plugins.getRegistredPluginNames)(this.hot);
  58. }
  59. if (initializedPlugins.indexOf(this.pluginName) >= 0) {
  60. initializedPlugins.splice(initializedPlugins.indexOf(this.pluginName), 1);
  61. }
  62. if (!initializedPlugins.length) {
  63. this.hot.runHooks('afterPluginsInitialized');
  64. }
  65. this.initialized = true;
  66. }
  67. /**
  68. * Enable plugin for this Handsontable instance.
  69. */
  70. }, {
  71. key: 'enablePlugin',
  72. value: function enablePlugin() {
  73. this.enabled = true;
  74. }
  75. /**
  76. * Disable plugin for this Handsontable instance.
  77. */
  78. }, {
  79. key: 'disablePlugin',
  80. value: function disablePlugin() {
  81. if (this.eventManager) {
  82. this.eventManager.clear();
  83. }
  84. this.clearHooks();
  85. this.enabled = false;
  86. }
  87. /**
  88. * Add listener to plugin hooks system.
  89. *
  90. * @param {String} name
  91. * @param {Function} callback
  92. */
  93. }, {
  94. key: 'addHook',
  95. value: function addHook(name, callback) {
  96. privatePool.get(this).hooks[name] = privatePool.get(this).hooks[name] || [];
  97. var hooks = privatePool.get(this).hooks[name];
  98. this.hot.addHook(name, callback);
  99. hooks.push(callback);
  100. privatePool.get(this).hooks[name] = hooks;
  101. }
  102. /**
  103. * Remove all hooks listeners by hook name.
  104. *
  105. * @param {String} name
  106. */
  107. }, {
  108. key: 'removeHooks',
  109. value: function removeHooks(name) {
  110. var _this2 = this;
  111. (0, _array.arrayEach)(privatePool.get(this).hooks[name] || [], function (callback) {
  112. _this2.hot.removeHook(name, callback);
  113. });
  114. }
  115. /**
  116. * Clear all hooks.
  117. */
  118. }, {
  119. key: 'clearHooks',
  120. value: function clearHooks() {
  121. var _this3 = this;
  122. var hooks = privatePool.get(this).hooks;
  123. (0, _object.objectEach)(hooks, function (callbacks, name) {
  124. return _this3.removeHooks(name);
  125. });
  126. hooks.length = 0;
  127. }
  128. /**
  129. * Register function which will be immediately called after all plugins initialized.
  130. *
  131. * @param {Function} callback
  132. */
  133. }, {
  134. key: 'callOnPluginsReady',
  135. value: function callOnPluginsReady(callback) {
  136. if (this.isPluginsReady) {
  137. callback();
  138. } else {
  139. this.pluginsInitializedCallbacks.push(callback);
  140. }
  141. }
  142. /**
  143. * On after plugins initialized listener.
  144. *
  145. * @private
  146. */
  147. }, {
  148. key: 'onAfterPluginsInitialized',
  149. value: function onAfterPluginsInitialized() {
  150. (0, _array.arrayEach)(this.pluginsInitializedCallbacks, function (callback) {
  151. return callback();
  152. });
  153. this.pluginsInitializedCallbacks.length = 0;
  154. this.isPluginsReady = true;
  155. }
  156. /**
  157. * On update settings listener.
  158. *
  159. * @private
  160. */
  161. }, {
  162. key: 'onUpdateSettings',
  163. value: function onUpdateSettings() {
  164. if (this.isEnabled) {
  165. if (this.enabled && !this.isEnabled()) {
  166. this.disablePlugin();
  167. }
  168. if (!this.enabled && this.isEnabled()) {
  169. this.enablePlugin();
  170. }
  171. if (this.enabled && this.isEnabled()) {
  172. this.updatePlugin();
  173. }
  174. }
  175. }
  176. /**
  177. * Updates the plugin to use the latest options you have specified.
  178. *
  179. * @private
  180. */
  181. }, {
  182. key: 'updatePlugin',
  183. value: function updatePlugin() {}
  184. /**
  185. * Destroy plugin.
  186. */
  187. }, {
  188. key: 'destroy',
  189. value: function destroy() {
  190. var _this4 = this;
  191. if (this.eventManager) {
  192. this.eventManager.destroy();
  193. }
  194. this.clearHooks();
  195. (0, _object.objectEach)(this, function (value, property) {
  196. if (property !== 'hot' && property !== 't') {
  197. _this4[property] = null;
  198. }
  199. });
  200. delete this.t;
  201. delete this.hot;
  202. }
  203. }]);
  204. return BasePlugin;
  205. }();
  206. exports.default = BasePlugin;