1
0
Fork 0
mirror of https://github.com/koalyptus/TableFilter.git synced 2024-05-21 15:56:40 +02:00
TableFilter/src/modules/store.js
2016-01-18 17:11:33 +11:00

131 lines
3 KiB
JavaScript

import Cookie from '../cookie';
export class Store{
/**
* Store, persistence manager
* @param {Object} tf TableFilter instance
*
* TODO: use localStorage and fallback to cookie persistence
*/
constructor(tf){
var f = tf.config();
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.tf.fltsValuesCookie));
this.emitter.on(['after-clearing-filters'], ()=> this.clearCookies());
}
/**
* Store filters' values in cookie
* @param {String} cookie name
*/
saveFilterValues(name){
var tf = this.tf;
var fltValues = [];
if(!tf.rememberGridValues){
return;
}
//store filters' values
for(var i=0; i<tf.fltIds.length; i++){
var value = tf.getFilterValue(i);
if (value === ''){
value = ' ';
}
fltValues.push(value);
}
//writes cookie
Cookie.write(
name,
fltValues.join(tf.separator),
this.duration
);
}
/**
* Retrieve filters' values from cookie
* @param {String} cookie name
* @return {Array}
*/
getFilterValues(name){
var flts = Cookie.read(name);
var rgx = new RegExp(this.tf.separator, 'g');
// filters' values array
return flts.split(rgx);
}
/**
* Store page number in cookie
* @param {String} cookie name
*/
savePageNb(name){
if(!this.tf.rememberPageNb){
return;
}
Cookie.write(
name,
this.tf.feature('paging').currentPageNb,
this.duration
);
}
/**
* Retrieve page number from cookie
* @param {String} cookie name
* @return {String}
*/
getPageNb(name){
return Cookie.read(name);
}
/**
* Store page length in cookie
* @param {String} cookie name
*/
savePageLength(name){
if(!this.tf.rememberPageLen){
return;
}
Cookie.write(
name,
this.tf.feature('paging').resultsPerPageSlc.selectedIndex,
this.duration
);
}
/**
* Retrieve page length from cookie
* @param {String} cookie name
* @return {String}
*/
getPageLength(name){
return Cookie.read(name);
}
/**
* Remove all cookies
*/
clearCookies(){
Cookie.remove(this.tf.fltsValuesCookie);
Cookie.remove(this.tf.pgLenCookie);
Cookie.remove(this.tf.pgNbCookie);
}
destroy(){
this.emitter.off(['after-filtering'],
()=> this.saveFilterValues(this.tf.fltsValuesCookie));
this.emitter.off(['after-clearing-filters'], ()=> this.clearCookies());
}
}