mirror of
https://github.com/koalyptus/TableFilter.git
synced 2026-03-17 08:05:44 +01:00
Continued storage class
This commit is contained in:
parent
680e13840d
commit
2656435d00
10 changed files with 117 additions and 27 deletions
|
|
@ -24,9 +24,7 @@ export class State extends Feature {
|
|||
|
||||
let cfg = this.config.state;
|
||||
|
||||
// hash enabled by default if state setting is simply set true
|
||||
this.enableHash = (cfg.types && cfg.types.indexOf('hash') !== -1) ||
|
||||
tf.state === true;
|
||||
this.enableHash = cfg.types && cfg.types.indexOf('hash') !== -1;
|
||||
this.enableLocalStorage = cfg.types &&
|
||||
cfg.types.indexOf('local_storage') !== -1;
|
||||
this.enableCookie = cfg.types && cfg.types.indexOf('cookie') !== -1;
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
|
||||
const global = window;
|
||||
// const JSON = global.JSON;
|
||||
const JSON = global.JSON;
|
||||
const localStorage = global.localStorage;
|
||||
|
||||
export const hasStorage = () => {
|
||||
return 'Storage' in global;
|
||||
|
|
@ -10,22 +11,64 @@ export class Storage {
|
|||
|
||||
constructor(state){
|
||||
this.state = state;
|
||||
this.tf = state.tf;
|
||||
this.enableLocalStorage = state.enableLocalStorage && hasStorage();
|
||||
this.enableCookie = state.enableCookie;
|
||||
this.emitter = state.emitter;
|
||||
|
||||
this.prxStorage = 'store';
|
||||
}
|
||||
|
||||
|
||||
init(){
|
||||
this.emitter.on(['state-changed'], (tf, state) => this.update(state));
|
||||
// this.emitter.on(['initialized'], () => this.sync());
|
||||
this.emitter.on(['state-changed'], (tf, state) => this.save(state));
|
||||
this.emitter.on(['initialized'], () => this.sync());
|
||||
}
|
||||
|
||||
update(state){
|
||||
console.log(this.enableLocalStorage, this.enableCookie, state);
|
||||
}
|
||||
|
||||
// save(state){
|
||||
|
||||
// update(state){
|
||||
// console.log(this.enableLocalStorage, this.enableCookie, state);
|
||||
// }
|
||||
|
||||
save(state){
|
||||
if(this.enableLocalStorage) {
|
||||
localStorage[this.getKey()] = JSON.stringify(state);
|
||||
}
|
||||
}
|
||||
|
||||
delete(){
|
||||
if(this.enableLocalStorage) {
|
||||
localStorage.removeItem(this.getKey());
|
||||
}
|
||||
}
|
||||
|
||||
sync() {
|
||||
let state = JSON.parse(localStorage[this.getKey()]);
|
||||
if (!state) {
|
||||
return;
|
||||
}
|
||||
|
||||
// To prevent state to react to features changes, state is temporarily
|
||||
// disabled
|
||||
this.state.disable();
|
||||
// State is overriden with hash state object
|
||||
this.state.override(state);
|
||||
// New hash state is applied to features
|
||||
this.state.sync();
|
||||
// State is re-enabled
|
||||
this.state.enable();
|
||||
}
|
||||
|
||||
getKey(){
|
||||
return `${this.tf.prfxTf}_${this.tf.id}`;
|
||||
}
|
||||
|
||||
destroy(){
|
||||
this.emitter.off(['state-changed'], (tf, state) => this.save(state));
|
||||
this.emitter.off(['initialized'], () => this.sync());
|
||||
|
||||
this.delete();
|
||||
|
||||
this.state = null;
|
||||
this.emitter = null;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue