observeChanges.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } };
  5. var _base = require('./../_base');
  6. var _base2 = _interopRequireDefault(_base);
  7. var _jsonPatchDuplex = require('./../../../lib/jsonpatch/json-patch-duplex');
  8. var _jsonPatchDuplex2 = _interopRequireDefault(_jsonPatchDuplex);
  9. var _dataObserver = require('./dataObserver');
  10. var _dataObserver2 = _interopRequireDefault(_dataObserver);
  11. var _array = require('./../../helpers/array');
  12. var _plugins = require('./../../plugins');
  13. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  14. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  15. function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
  16. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
  17. // Handsontable.hooks.register('afterChangesObserved');
  18. /**
  19. * @plugin ObserveChanges
  20. *
  21. * @description
  22. * This plugin allows to observe data source changes.
  23. *
  24. * By default, the plugin is declared as `undefined`, which makes it disabled.
  25. * Enabling this plugin switches the table into one-way data binding where changes are applied into the data source (outside from the table)
  26. * will be automatically reflected in the table.
  27. *
  28. * ```js
  29. * ...
  30. * // as a boolean
  31. * observeChanges: true,
  32. * ...
  33. * ```
  34. *
  35. * To configure this plugin see {@link Options#observeChanges}.
  36. */
  37. var ObserveChanges = function (_BasePlugin) {
  38. _inherits(ObserveChanges, _BasePlugin);
  39. function ObserveChanges(hotInstance) {
  40. _classCallCheck(this, ObserveChanges);
  41. /**
  42. * Instance of {@link DataObserver}.
  43. *
  44. * @type {DataObserver}
  45. */
  46. var _this = _possibleConstructorReturn(this, (ObserveChanges.__proto__ || Object.getPrototypeOf(ObserveChanges)).call(this, hotInstance));
  47. _this.observer = null;
  48. return _this;
  49. }
  50. /**
  51. * Check if the plugin is enabled in the handsontable settings.
  52. *
  53. * @returns {Boolean}
  54. */
  55. _createClass(ObserveChanges, [{
  56. key: 'isEnabled',
  57. value: function isEnabled() {
  58. return this.hot.getSettings().observeChanges;
  59. }
  60. /**
  61. * Enable plugin for this Handsontable instance.
  62. */
  63. }, {
  64. key: 'enablePlugin',
  65. value: function enablePlugin() {
  66. var _this2 = this;
  67. if (this.enabled) {
  68. return;
  69. }
  70. if (!this.observer) {
  71. this.observer = new _dataObserver2.default(this.hot.getSourceData());
  72. this._exposePublicApi();
  73. }
  74. this.observer.addLocalHook('change', function (patches) {
  75. return _this2.onDataChange(patches);
  76. });
  77. this.addHook('afterCreateRow', function () {
  78. return _this2.onAfterTableAlter();
  79. });
  80. this.addHook('afterRemoveRow', function () {
  81. return _this2.onAfterTableAlter();
  82. });
  83. this.addHook('afterCreateCol', function () {
  84. return _this2.onAfterTableAlter();
  85. });
  86. this.addHook('afterRemoveCol', function () {
  87. return _this2.onAfterTableAlter();
  88. });
  89. this.addHook('afterChange', function (changes, source) {
  90. return _this2.onAfterTableAlter(source);
  91. });
  92. this.addHook('afterLoadData', function (firstRun) {
  93. return _this2.onAfterLoadData(firstRun);
  94. });
  95. _get(ObserveChanges.prototype.__proto__ || Object.getPrototypeOf(ObserveChanges.prototype), 'enablePlugin', this).call(this);
  96. }
  97. /**
  98. * Disable plugin for this Handsontable instance.
  99. */
  100. }, {
  101. key: 'disablePlugin',
  102. value: function disablePlugin() {
  103. if (this.observer) {
  104. this.observer.destroy();
  105. this.observer = null;
  106. this._deletePublicApi();
  107. }
  108. _get(ObserveChanges.prototype.__proto__ || Object.getPrototypeOf(ObserveChanges.prototype), 'disablePlugin', this).call(this);
  109. }
  110. /**
  111. * Data change observer.
  112. *
  113. * @private
  114. * @param {Array} patches An array of objects which every item defines coordinates where data was changed.
  115. */
  116. }, {
  117. key: 'onDataChange',
  118. value: function onDataChange(patches) {
  119. var _this3 = this;
  120. if (!this.observer.isPaused()) {
  121. var sourceName = this.pluginName + '.change';
  122. var actions = {
  123. add: function add(patch) {
  124. if (isNaN(patch.col)) {
  125. _this3.hot.runHooks('afterCreateRow', patch.row, 1, sourceName);
  126. } else {
  127. _this3.hot.runHooks('afterCreateCol', patch.col, 1, sourceName);
  128. }
  129. },
  130. remove: function remove(patch) {
  131. if (isNaN(patch.col)) {
  132. _this3.hot.runHooks('afterRemoveRow', patch.row, 1, sourceName);
  133. } else {
  134. _this3.hot.runHooks('afterRemoveCol', patch.col, 1, sourceName);
  135. }
  136. },
  137. replace: function replace(patch) {
  138. _this3.hot.runHooks('afterChange', [patch.row, patch.col, null, patch.value], sourceName);
  139. }
  140. };
  141. (0, _array.arrayEach)(patches, function (patch) {
  142. if (actions[patch.op]) {
  143. actions[patch.op](patch);
  144. }
  145. });
  146. this.hot.render();
  147. }
  148. this.hot.runHooks('afterChangesObserved');
  149. }
  150. /**
  151. * On after table alter listener. Prevents infinity loop between internal and external data changing.
  152. *
  153. * @private
  154. * @param source
  155. */
  156. }, {
  157. key: 'onAfterTableAlter',
  158. value: function onAfterTableAlter(source) {
  159. var _this4 = this;
  160. if (source !== 'loadData') {
  161. this.observer.pause();
  162. this.hot.addHookOnce('afterChangesObserved', function () {
  163. return _this4.observer.resume();
  164. });
  165. }
  166. }
  167. /**
  168. * On after load data listener.
  169. *
  170. * @private
  171. * @param {Boolean} firstRun `true` if event was fired first time.
  172. */
  173. }, {
  174. key: 'onAfterLoadData',
  175. value: function onAfterLoadData(firstRun) {
  176. if (!firstRun) {
  177. this.observer.setObservedData(this.hot.getSourceData());
  178. }
  179. }
  180. /**
  181. * Destroy plugin instance.
  182. */
  183. }, {
  184. key: 'destroy',
  185. value: function destroy() {
  186. if (this.observer) {
  187. this.observer.destroy();
  188. this._deletePublicApi();
  189. }
  190. _get(ObserveChanges.prototype.__proto__ || Object.getPrototypeOf(ObserveChanges.prototype), 'destroy', this).call(this);
  191. }
  192. /**
  193. * Expose plugins methods to the core.
  194. *
  195. * @private
  196. */
  197. }, {
  198. key: '_exposePublicApi',
  199. value: function _exposePublicApi() {
  200. var _this5 = this;
  201. var hot = this.hot;
  202. hot.pauseObservingChanges = function () {
  203. return _this5.observer.pause();
  204. };
  205. hot.resumeObservingChanges = function () {
  206. return _this5.observer.resume();
  207. };
  208. hot.isPausedObservingChanges = function () {
  209. return _this5.observer.isPaused();
  210. };
  211. }
  212. /**
  213. * Delete all previously exposed methods.
  214. *
  215. * @private
  216. */
  217. }, {
  218. key: '_deletePublicApi',
  219. value: function _deletePublicApi() {
  220. var hot = this.hot;
  221. delete hot.pauseObservingChanges;
  222. delete hot.resumeObservingChanges;
  223. delete hot.isPausedObservingChanges;
  224. }
  225. }]);
  226. return ObserveChanges;
  227. }(_base2.default);
  228. exports.default = ObserveChanges;
  229. (0, _plugins.registerPlugin)('observeChanges', ObserveChanges);