import Cookie from '../cookie'; import Types from '../types'; export class Store{ /** * Store, persistence manager * @param {Object} tf TableFilter instance * * TODO: use localStorage and fallback to cookie persistence */ constructor(tf){ let f = tf.config(); //cookie storing filter values this.fltsValuesCookie = tf.prfxCookieFltsValues + tf.id; //cookie storing page nb this.pgNbCookie = tf.prfxCookiePageNb + tf.id; //cookie storing page length this.pgLenCookie = tf.prfxCookiePageLen + tf.id; this.duration = !isNaN(f.set_cookie_duration) ? parseInt(f.set_cookie_duration, 10) : 100000; this.tf = tf; this.emitter = tf.emitter; } init(){ this.emitter.on(['after-filtering'], ()=> this.saveFilterValues()); this.emitter.on(['after-clearing-filters'], ()=> this.clearCookies()); this.emitter.on(['after-page-change'], (tf, index)=> this.savePageNb(index)); this.emitter.on(['after-page-length-change'], (tf, index)=> this.savePageLength(index)); } /** * Store filters' values in cookie */ saveFilterValues(){ let tf = this.tf; let fltValues = []; if(!tf.rememberGridValues){ return; } //store filters' values for(let i=0; i this.saveFilterValues()); this.emitter.off(['after-clearing-filters'], ()=> this.clearCookies()); this.emitter.off(['after-page-change'], (tf, index)=> this.savePageNb(index)); this.emitter.off(['after-page-length-change'], (tf, index)=> this.savePageLength(index)); } }