persistentState.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. 'use strict';
  2. exports.__esModule = true;
  3. var _pluginHooks = require('./../../pluginHooks');
  4. var _pluginHooks2 = _interopRequireDefault(_pluginHooks);
  5. var _plugins = require('./../../plugins');
  6. var _object = require('./../../helpers/object');
  7. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  8. function Storage(prefix) {
  9. var savedKeys;
  10. var saveSavedKeys = function saveSavedKeys() {
  11. window.localStorage[prefix + '__persistentStateKeys'] = JSON.stringify(savedKeys);
  12. };
  13. var loadSavedKeys = function loadSavedKeys() {
  14. var keysJSON = window.localStorage[prefix + '__persistentStateKeys'];
  15. var keys = typeof keysJSON == 'string' ? JSON.parse(keysJSON) : void 0;
  16. savedKeys = keys ? keys : [];
  17. };
  18. var clearSavedKeys = function clearSavedKeys() {
  19. savedKeys = [];
  20. saveSavedKeys();
  21. };
  22. loadSavedKeys();
  23. this.saveValue = function (key, value) {
  24. window.localStorage[prefix + '_' + key] = JSON.stringify(value);
  25. if (savedKeys.indexOf(key) == -1) {
  26. savedKeys.push(key);
  27. saveSavedKeys();
  28. }
  29. };
  30. this.loadValue = function (key, defaultValue) {
  31. key = typeof key === 'undefined' ? defaultValue : key;
  32. var value = window.localStorage[prefix + '_' + key];
  33. return typeof value == 'undefined' ? void 0 : JSON.parse(value);
  34. };
  35. this.reset = function (key) {
  36. window.localStorage.removeItem(prefix + '_' + key);
  37. };
  38. this.resetAll = function () {
  39. for (var index = 0; index < savedKeys.length; index++) {
  40. window.localStorage.removeItem(prefix + '_' + savedKeys[index]);
  41. }
  42. clearSavedKeys();
  43. };
  44. }
  45. /**
  46. * @private
  47. * @class PersistentState
  48. * @plugin PersistentState
  49. */
  50. function HandsontablePersistentState() {
  51. var plugin = this;
  52. this.init = function () {
  53. var instance = this,
  54. pluginSettings = instance.getSettings().persistentState;
  55. plugin.enabled = !!pluginSettings;
  56. if (!plugin.enabled) {
  57. removeHooks.call(instance);
  58. return;
  59. }
  60. if (!instance.storage) {
  61. instance.storage = new Storage(instance.rootElement.id);
  62. }
  63. instance.resetState = plugin.resetValue;
  64. addHooks.call(instance);
  65. };
  66. this.saveValue = function (key, value) {
  67. var instance = this;
  68. instance.storage.saveValue(key, value);
  69. };
  70. this.loadValue = function (key, saveTo) {
  71. var instance = this;
  72. saveTo.value = instance.storage.loadValue(key);
  73. };
  74. this.resetValue = function (key) {
  75. var instance = this;
  76. if (typeof key === 'undefined') {
  77. instance.storage.resetAll();
  78. } else {
  79. instance.storage.reset(key);
  80. }
  81. };
  82. var hooks = {
  83. persistentStateSave: plugin.saveValue,
  84. persistentStateLoad: plugin.loadValue,
  85. persistentStateReset: plugin.resetValue
  86. };
  87. for (var hookName in hooks) {
  88. if ((0, _object.hasOwnProperty)(hooks, hookName)) {
  89. _pluginHooks2.default.getSingleton().register(hookName);
  90. }
  91. }
  92. function addHooks() {
  93. var instance = this;
  94. for (var hookName in hooks) {
  95. if ((0, _object.hasOwnProperty)(hooks, hookName)) {
  96. instance.addHook(hookName, hooks[hookName]);
  97. }
  98. }
  99. }
  100. function removeHooks() {
  101. var instance = this;
  102. for (var hookName in hooks) {
  103. if ((0, _object.hasOwnProperty)(hooks, hookName)) {
  104. instance.removeHook(hookName, hooks[hookName]);
  105. }
  106. }
  107. }
  108. }
  109. var htPersistentState = new HandsontablePersistentState();
  110. _pluginHooks2.default.getSingleton().add('beforeInit', htPersistentState.init);
  111. _pluginHooks2.default.getSingleton().add('afterUpdateSettings', htPersistentState.init);
  112. exports.default = HandsontablePersistentState;