@quantlab/handsontable
Version:
Spreadsheet-like data grid editor that provides copy/paste functionality compatible with Excel/Google Docs
153 lines (112 loc) • 3.67 kB
JavaScript
;
exports.__esModule = true;
var _pluginHooks = require('./../../pluginHooks');
var _pluginHooks2 = _interopRequireDefault(_pluginHooks);
var _plugins = require('./../../plugins');
var _object = require('./../../helpers/object');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function Storage(prefix) {
var savedKeys;
var saveSavedKeys = function saveSavedKeys() {
window.localStorage[prefix + '__persistentStateKeys'] = JSON.stringify(savedKeys);
};
var loadSavedKeys = function loadSavedKeys() {
var keysJSON = window.localStorage[prefix + '__persistentStateKeys'];
var keys = typeof keysJSON == 'string' ? JSON.parse(keysJSON) : void 0;
savedKeys = keys ? keys : [];
};
var clearSavedKeys = function clearSavedKeys() {
savedKeys = [];
saveSavedKeys();
};
loadSavedKeys();
this.saveValue = function (key, value) {
window.localStorage[prefix + '_' + key] = JSON.stringify(value);
if (savedKeys.indexOf(key) == -1) {
savedKeys.push(key);
saveSavedKeys();
}
};
this.loadValue = function (key, defaultValue) {
key = typeof key === 'undefined' ? defaultValue : key;
var value = window.localStorage[prefix + '_' + key];
return typeof value == 'undefined' ? void 0 : JSON.parse(value);
};
this.reset = function (key) {
window.localStorage.removeItem(prefix + '_' + key);
};
this.resetAll = function () {
for (var index = 0; index < savedKeys.length; index++) {
window.localStorage.removeItem(prefix + '_' + savedKeys[index]);
}
clearSavedKeys();
};
}
/**
* @private
* @class PersistentState
* @plugin PersistentState
*/
function HandsontablePersistentState() {
var plugin = this;
this.init = function () {
var instance = this,
pluginSettings = instance.getSettings().persistentState;
plugin.enabled = !!pluginSettings;
if (!plugin.enabled) {
removeHooks.call(instance);
return;
}
if (!instance.storage) {
instance.storage = new Storage(instance.rootElement.id);
}
instance.resetState = plugin.resetValue;
addHooks.call(instance);
};
this.saveValue = function (key, value) {
var instance = this;
instance.storage.saveValue(key, value);
};
this.loadValue = function (key, saveTo) {
var instance = this;
saveTo.value = instance.storage.loadValue(key);
};
this.resetValue = function (key) {
var instance = this;
if (typeof key === 'undefined') {
instance.storage.resetAll();
} else {
instance.storage.reset(key);
}
};
var hooks = {
persistentStateSave: plugin.saveValue,
persistentStateLoad: plugin.loadValue,
persistentStateReset: plugin.resetValue
};
for (var hookName in hooks) {
if ((0, _object.hasOwnProperty)(hooks, hookName)) {
_pluginHooks2.default.getSingleton().register(hookName);
}
}
function addHooks() {
var instance = this;
for (var hookName in hooks) {
if ((0, _object.hasOwnProperty)(hooks, hookName)) {
instance.addHook(hookName, hooks[hookName]);
}
}
}
function removeHooks() {
var instance = this;
for (var hookName in hooks) {
if ((0, _object.hasOwnProperty)(hooks, hookName)) {
instance.removeHook(hookName, hooks[hookName]);
}
}
}
}
var htPersistentState = new HandsontablePersistentState();
_pluginHooks2.default.getSingleton().add('beforeInit', htPersistentState.init);
_pluginHooks2.default.getSingleton().add('afterUpdateSettings', htPersistentState.init);
exports.default = HandsontablePersistentState;