-
Notifications
You must be signed in to change notification settings - Fork 3k
/
persistentState.js
150 lines (130 loc) · 4.21 KB
/
persistentState.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import { BasePlugin } from '../base';
import Storage from './storage';
import Hooks from '../../pluginHooks';
Hooks.getSingleton().register('persistentStateSave');
Hooks.getSingleton().register('persistentStateLoad');
Hooks.getSingleton().register('persistentStateReset');
export const PLUGIN_KEY = 'persistentState';
export const PLUGIN_PRIORITY = 0;
/* eslint-disable jsdoc/require-description-complete-sentence */
/**
* @plugin PersistentState
* @class PersistentState
*
* @description
* Save the state of column sorting, column positions and column sizes in local storage to preserve table state
* between page reloads.
*
* In order to enable data storage mechanism, {@link Options#persistentState} option must be set to `true`.
*
* When persistentState is enabled it exposes 3 hooks:
* - {@link Hooks#persistentStateSave} - Saves value under given key in browser local storage.
* - {@link Hooks#persistentStateLoad} - Loads value, saved under given key, from browser local storage. The loaded
* value will be saved in `saveTo.value`.
* - {@link Hooks#persistentStateReset} - Clears the value saved under key. If no key is given, all values associated
* with table will be cleared.
*
* __Note:__ The main reason behind using `persistentState` hooks rather than regular LocalStorage API is that it
* ensures separation of data stored by multiple Handsontable instances. In other words, if you have two (or more)
* instances of Handsontable on one page, data saved by one instance won't be accessible by the second instance.
* Those two instances can store data under the same key and no data would be overwritten.
*
* __Important:__ In order for the data separation to work properly, make sure that each instance of Handsontable has a unique `id`.
*
*/
export class PersistentState extends BasePlugin {
static get PLUGIN_KEY() {
return PLUGIN_KEY;
}
static get PLUGIN_PRIORITY() {
return PLUGIN_PRIORITY;
}
constructor(hotInstance) {
super(hotInstance);
/**
* Instance of {@link Storage}.
*
* @private
* @type {Storage}
*/
this.storage = void 0;
}
/**
* Checks if the plugin is enabled in the handsontable settings. This method is executed in {@link Hooks#beforeInit}
* hook and if it returns `true` then the {@link PersistentState#enablePlugin} method is called.
*
* @returns {boolean}
*/
isEnabled() {
return !!this.hot.getSettings()[PLUGIN_KEY];
}
/**
* Enables the plugin functionality for this Handsontable instance.
*/
enablePlugin() {
if (this.enabled) {
return;
}
if (!this.storage) {
this.storage = new Storage(this.hot.rootElement.id, this.hot.rootWindow);
}
this.addHook('persistentStateSave', (key, value) => this.saveValue(key, value));
this.addHook('persistentStateLoad', (key, saveTo) => this.loadValue(key, saveTo));
this.addHook('persistentStateReset', () => this.resetValue());
super.enablePlugin();
}
/**
* Disables the plugin functionality for this Handsontable instance.
*/
disablePlugin() {
this.storage = void 0;
super.disablePlugin();
}
/**
* Updates the plugin's state.
*
* This method is executed when [`updateSettings()`](@/api/core.md#updatesettings) is invoked with any of the following configuration options:
* - [`persistentState`](@/api/options.md#persistentstate)
*/
updatePlugin() {
this.disablePlugin();
this.enablePlugin();
super.updatePlugin();
}
/**
* Loads the value from local storage.
*
* @param {string} key Storage key.
* @param {object} saveTo Saved value from local storage.
*/
loadValue(key, saveTo) {
saveTo.value = this.storage.loadValue(key);
}
/**
* Saves the data to local storage.
*
* @param {string} key Storage key.
* @param {Mixed} value Value to save.
*/
saveValue(key, value) {
this.storage.saveValue(key, value);
}
/**
* Resets the data or all data from local storage.
*
* @param {string} key [optional] Storage key.
*/
resetValue(key) {
if (typeof key === 'undefined') {
this.storage.resetAll();
} else {
this.storage.reset(key);
}
}
/**
* Destroys the plugin instance.
*/
destroy() {
super.destroy();
}
}