persistentState.js 3.3 KB

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