_base.js 6.0 KB

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