9b37b11a0e3f1d678b7725cc33ce892e1b59de391572e6bb78db65bf7829fd0442e4eac5d362f270084fa426416303bc544b70cf9ed5abca60255e21a8947d 4.1 KB

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