1
0
Fork 0
mirror of https://github.com/koalyptus/TableFilter.git synced 2024-06-20 06:35:24 +02:00
TableFilter/src/modules/state.js

193 lines
4.7 KiB
JavaScript
Raw Normal View History

2016-03-15 08:18:51 +01:00
import {Feature} from './feature';
2016-03-19 15:10:59 +01:00
import {Hash} from './hash';
2016-03-15 08:18:51 +01:00
import Str from '../string';
2016-03-18 07:58:32 +01:00
import Types from '../types';
2016-03-15 08:18:51 +01:00
/**
* Reflects the state of features to be persisted via hash, localStorage or
* cookie
*
* @export
* @class State
* @extends {Feature}
*/
2016-03-19 15:10:59 +01:00
export class State extends Feature {
2016-03-15 08:18:51 +01:00
/**
2016-03-19 15:10:59 +01:00
* Creates an instance of State
2016-03-18 07:58:32 +01:00
*
* @param tf TableFilter instance
2016-03-15 08:18:51 +01:00
*/
constructor(tf) {
2016-03-19 15:10:59 +01:00
super(tf, 'state');
2016-03-15 08:18:51 +01:00
2016-03-19 15:10:59 +01:00
let cfg = this.config.state;
2016-03-15 08:18:51 +01:00
2016-03-20 04:51:08 +01:00
this.enableHash = cfg.type && cfg.type.indexOf('hash') !== -1;
2016-03-15 08:18:51 +01:00
this.persistFilters = cfg.filters === false ? false : true;
this.persistPageNumber = Boolean(cfg.page_number);
this.persistPageLength = Boolean(cfg.page_length);
2016-03-15 08:18:51 +01:00
2016-03-19 15:10:59 +01:00
this.hash = null;
2016-03-15 08:18:51 +01:00
this.pageNb = null;
2016-03-18 07:58:32 +01:00
this.pageLength = null;
2016-03-15 08:18:51 +01:00
this.state = {};
this.prfxCol = 'col_';
this.pageNbKey = 'page';
2016-03-20 04:51:08 +01:00
this.pageLengthKey = 'page_length';
2016-03-15 08:18:51 +01:00
}
/**
* Initialize features state
*/
init() {
2016-03-19 15:10:59 +01:00
if (this.initialized) {
2016-03-15 08:18:51 +01:00
return;
}
2016-03-18 07:58:32 +01:00
this.emitter.on(['after-filtering'], () => this.update());
this.emitter.on(['after-page-change'],
(tf, pageNb) => this.updatePage(pageNb));
this.emitter.on(['after-page-length-change'],
(tf, index) => this.updatePageLength(index));
2016-03-19 15:10:59 +01:00
if(this.enableHash){
this.hash = new Hash(this);
this.hash.init();
}
2016-03-15 08:18:51 +01:00
this.initialized = true;
}
/**
* Update state field based on current features state
*/
2016-03-19 15:10:59 +01:00
update() {
2016-03-20 04:51:08 +01:00
if(!this.isEnabled()){
return;
}
2016-03-15 08:18:51 +01:00
let tf = this.tf;
if (this.persistFilters) {
2016-03-15 08:18:51 +01:00
let filterValues = tf.getFiltersValue();
filterValues.forEach((val, idx) => {
2016-03-15 08:18:51 +01:00
let key = `${this.prfxCol}${idx}`;
if (Types.isString(val) && Str.isEmpty(val)) {
if (this.state.hasOwnProperty(key)) {
2016-03-15 08:18:51 +01:00
this.state[key] = undefined;
}
} else {
this.state[key] = this.state[key] || {};
this.state[key].flt = val;
}
});
}
if (this.persistPageNumber) {
2016-03-18 07:58:32 +01:00
if(Types.isNull(this.pageNb)){
this.state[this.pageNbKey] = undefined;
} else {
this.state[this.pageNbKey] = this.pageNb;
}
}
if (this.persistPageLength) {
2016-03-18 07:58:32 +01:00
if(Types.isNull(this.pageLength)){
this.state[this.pageLengthKey] = undefined;
} else {
this.state[this.pageLengthKey] = this.pageLength;
}
2016-03-15 08:18:51 +01:00
}
2016-03-20 04:51:08 +01:00
2016-03-19 15:10:59 +01:00
this.emitter.emit('state-changed', tf, this.state);
2016-03-15 08:18:51 +01:00
}
/**
* Refresh page number field on page number change
*
* @param pageNb Current page number
*/
2016-03-18 07:58:32 +01:00
updatePage(pageNb){
this.pageNb = pageNb;
this.update();
}
/**
* Refresh page length field on page length change
*
* @param pageLength Current page length value
*/
2016-03-18 07:58:32 +01:00
updatePageLength(pageLength){
this.pageLength = pageLength;
this.update();
}
/**
* Override state field
*
* @param state State object
*/
2016-03-20 04:51:08 +01:00
override(state){
this.state = state;
}
/**
* Apply current features state
*/
sync() {
2016-03-19 15:10:59 +01:00
let state = this.state;
2016-03-15 08:18:51 +01:00
let tf = this.tf;
if (this.persistFilters) {
Object.keys(state).forEach((key) => {
if (key.indexOf(this.prfxCol) !== -1) {
let colIdx = parseInt(key.replace(this.prfxCol, ''), 10);
2016-03-15 08:18:51 +01:00
let val = state[key].flt;
tf.setFilterValue(colIdx, val);
}
});
tf.filter();
}
if (this.persistPageNumber) {
let pageNumber = state[this.pageNbKey];
2016-03-18 07:58:32 +01:00
this.emitter.emit('change-page', this.tf, pageNumber);
}
if(this.persistPageLength){
let pageLength = state[this.pageLengthKey];
this.emitter.emit('change-page-results', this.tf, pageLength);
2016-03-15 08:18:51 +01:00
}
}
/**
* Destroy State instance
*/
destroy() {
if (!this.initialized) {
2016-03-15 08:18:51 +01:00
return;
}
this.state = {};
2016-03-18 07:58:32 +01:00
this.emitter.off(['after-filtering'], () => this.update());
this.emitter.off(['after-page-change'],
(tf, pageNb) => this.updatePage(pageNb));
this.emitter.off(['after-page-length-change'],
(tf, index) => this.updatePageLength(index));
2016-03-19 15:10:59 +01:00
if(this.enableHash){
this.hash.destroy();
this.hash = null;
}
2016-03-15 08:18:51 +01:00
this.initialized = false;
}
}