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
|
|
|
*/
|
2015-12-28 07:53:03 +01:00
|
|
|
constructor(tf){
|
2015-02-21 04:39:09 +01:00
|
|
|
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;
|
2015-12-28 07:53:03 +01:00
|
|
|
this.emitter = tf.emitter;
|
|
|
|
}
|
|
|
|
|
|
|
|
init(){
|
2016-01-03 03:49:04 +01:00
|
|
|
this.emitter.on(['after-filtering'],
|
2015-12-28 07:53:03 +01:00
|
|
|
()=> this.saveFilterValues(this.tf.fltsValuesCookie));
|
2016-01-18 07:11:33 +01:00
|
|
|
this.emitter.on(['after-clearing-filters'], ()=> this.clearCookies());
|
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++){
|
2015-02-21 04:39:09 +01:00
|
|
|
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
|
|
|
}
|
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,
|
2015-06-07 17:31:15 +02:00
|
|
|
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,
|
2015-06-07 17:31:15 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-01-18 07:11:33 +01:00
|
|
|
/**
|
|
|
|
* Remove all cookies
|
|
|
|
*/
|
|
|
|
clearCookies(){
|
|
|
|
Cookie.remove(this.tf.fltsValuesCookie);
|
|
|
|
Cookie.remove(this.tf.pgLenCookie);
|
|
|
|
Cookie.remove(this.tf.pgNbCookie);
|
|
|
|
}
|
|
|
|
|
2015-12-28 07:53:03 +01:00
|
|
|
destroy(){
|
2016-01-03 03:49:04 +01:00
|
|
|
this.emitter.off(['after-filtering'],
|
2015-12-28 07:53:03 +01:00
|
|
|
()=> this.saveFilterValues(this.tf.fltsValuesCookie));
|
2016-01-18 07:11:33 +01:00
|
|
|
this.emitter.off(['after-clearing-filters'], ()=> this.clearCookies());
|
2015-12-28 07:53:03 +01:00
|
|
|
}
|
2014-12-05 02:10:00 +01:00
|
|
|
}
|