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

122 lines
2.7 KiB
JavaScript
Raw Normal View History

2015-05-30 14:23:33 +02:00
import Cookie from '../cookie';
2014-11-30 11:38:40 +01:00
export class Store{
2014-12-05 02:10:00 +01:00
/**
* Store, persistence manager
* @param {Object} tf TableFilter instance
2015-05-16 11:58:56 +02:00
*
* TODO: use localStorage and fallback to cookie persistence
2014-12-05 02:10:00 +01:00
*/
constructor(tf){
var f = tf.config();
2014-11-30 11:38:40 +01:00
this.duration = !isNaN(f.set_cookie_duration) ?
parseInt(f.set_cookie_duration, 10) : 100000;
this.tf = tf;
this.emitter = tf.emitter;
}
init(){
2016-01-03 03:49:04 +01:00
this.emitter.on(['after-filtering'],
()=> this.saveFilterValues(this.tf.fltsValuesCookie));
2014-11-30 11:38:40 +01:00
}
2014-12-05 02:10:00 +01:00
/**
* Store filters' values in cookie
* @param {String} cookie name
*/
2014-11-30 11:38:40 +01:00
saveFilterValues(name){
var tf = this.tf;
2014-12-05 02:10:00 +01:00
var fltValues = [];
2016-01-09 10:22:57 +01:00
if(!tf.rememberGridValues){
return;
}
2014-11-30 11:38:40 +01:00
//store filters' values
for(var i=0; i<tf.fltIds.length; i++){
var value = tf.getFilterValue(i);
2014-11-30 11:38:40 +01:00
if (value === ''){
value = ' ';
}
2014-12-05 02:10:00 +01:00
fltValues.push(value);
2014-11-30 11:38:40 +01:00
}
//adds array size
2016-01-13 07:45:44 +01:00
//fltValues.push(tf.fltIds.length);
2014-12-05 02:10:00 +01:00
2014-11-30 11:38:40 +01:00
//writes cookie
Cookie.write(
name,
2014-12-05 02:10:00 +01:00
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){
2016-01-09 10:22:57 +01:00
if(!this.tf.rememberPageNb){
return;
}
2014-12-05 02:10:00 +01:00
Cookie.write(
name,
this.tf.feature('paging').currentPageNb,
2014-11-30 11:38:40 +01:00
this.duration
);
}
2014-12-05 02:10:00 +01:00
/**
* 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){
2016-01-09 10:22:57 +01:00
if(!this.tf.rememberPageLen){
return;
}
2014-12-05 02:10:00 +01:00
Cookie.write(
name,
this.tf.feature('paging').resultsPerPageSlc.selectedIndex,
2014-12-05 02:10:00 +01:00
this.duration
);
}
/**
* Retrieve page length from cookie
* @param {String} cookie name
* @return {String}
*/
getPageLength(name){
return Cookie.read(name);
}
destroy(){
2016-01-03 03:49:04 +01:00
this.emitter.off(['after-filtering'],
()=> this.saveFilterValues(this.tf.fltsValuesCookie));
}
2014-12-05 02:10:00 +01:00
}