1
0
Fork 0
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:
Max Guglielmi 2016-04-07 18:34:25 +02:00
commit 2656435d00
10 changed files with 117 additions and 27 deletions

View file

@ -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;

View file

@ -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;
}
}