2016-05-14 12:04:00 +02:00
|
|
|
import {Feature} from '../feature';
|
2016-03-19 15:10:59 +01:00
|
|
|
import {Hash} from './hash';
|
2016-04-07 12:13:54 +02:00
|
|
|
import {Storage} from './storage';
|
2016-05-20 10:08:39 +02:00
|
|
|
import {isEmpty} from '../string';
|
2016-05-15 04:56:12 +02:00
|
|
|
import {isArray, isNull, isString, isUndef} from '../types';
|
2017-07-09 07:11:51 +02:00
|
|
|
import {defaultsBool, defaultsNb} from '../settings';
|
2016-03-15 08:18:51 +01:00
|
|
|
|
2016-03-20 06:15:15 +01:00
|
|
|
/**
|
2016-08-06 11:46:26 +02:00
|
|
|
* Features state object persistable with localStorage, cookie or URL hash
|
2016-03-20 06:15:15 +01:00
|
|
|
*
|
|
|
|
* @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-20 09:56:18 +01:00
|
|
|
* @param {TableFilter} tf TableFilter instance
|
2016-03-15 08:18:51 +01:00
|
|
|
*/
|
2016-03-16 08:08:26 +01:00
|
|
|
constructor(tf) {
|
2019-02-09 14:27:55 +01:00
|
|
|
super(tf, State);
|
2016-03-15 08:18:51 +01:00
|
|
|
|
2017-07-02 13:53:40 +02:00
|
|
|
let cfg = this.config.state || {};
|
2016-03-15 08:18:51 +01:00
|
|
|
|
2016-08-06 11:46:26 +02:00
|
|
|
/**
|
|
|
|
* Determines whether state is persisted with URL hash
|
|
|
|
* @type {Boolean}
|
|
|
|
*/
|
2016-04-08 17:43:55 +02:00
|
|
|
this.enableHash = cfg === true ||
|
2016-05-15 04:56:12 +02:00
|
|
|
(isArray(cfg.types) && cfg.types.indexOf('hash') !== -1);
|
2016-08-06 11:46:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines whether state is persisted with localStorage
|
|
|
|
* @type {Boolean}
|
|
|
|
*/
|
2016-05-15 04:56:12 +02:00
|
|
|
this.enableLocalStorage = isArray(cfg.types) &&
|
2016-04-07 12:13:54 +02:00
|
|
|
cfg.types.indexOf('local_storage') !== -1;
|
2016-08-06 11:46:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines whether state is persisted with localStorage
|
|
|
|
* @type {Boolean}
|
|
|
|
*/
|
2016-05-15 04:56:12 +02:00
|
|
|
this.enableCookie = isArray(cfg.types) &&
|
2016-04-08 17:43:55 +02:00
|
|
|
cfg.types.indexOf('cookie') !== -1;
|
2016-08-06 11:46:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Persist filters values, enabled by default
|
|
|
|
* @type {Boolean}
|
|
|
|
*/
|
2017-07-09 07:11:51 +02:00
|
|
|
this.persistFilters = defaultsBool(cfg.filters, true);
|
2016-08-06 11:46:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Persist current page number when paging is enabled
|
|
|
|
* @type {Boolean}
|
|
|
|
*/
|
2016-03-15 08:18:51 +01:00
|
|
|
this.persistPageNumber = Boolean(cfg.page_number);
|
2016-08-06 11:46:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Persist page length when paging is enabled
|
|
|
|
* @type {Boolean}
|
|
|
|
*/
|
2016-03-16 08:08:26 +01:00
|
|
|
this.persistPageLength = Boolean(cfg.page_length);
|
2016-08-06 11:46:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Persist column sorting
|
|
|
|
* @type {Boolean}
|
|
|
|
*/
|
2016-04-12 09:49:54 +02:00
|
|
|
this.persistSort = Boolean(cfg.sort);
|
2016-08-06 11:46:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Persist columns visibility
|
|
|
|
* @type {Boolean}
|
|
|
|
*/
|
2016-04-18 11:04:50 +02:00
|
|
|
this.persistColsVisibility = Boolean(cfg.columns_visibility);
|
2016-08-06 11:46:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Persist filters row visibility
|
|
|
|
* @type {Boolean}
|
|
|
|
*/
|
2016-04-24 17:12:45 +02:00
|
|
|
this.persistFiltersVisibility = Boolean(cfg.filters_visibility);
|
2016-08-06 11:46:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Cookie duration in hours
|
|
|
|
* @type {Boolean}
|
|
|
|
*/
|
2017-07-09 07:11:51 +02:00
|
|
|
this.cookieDuration = defaultsNb(parseInt(cfg.cookie_duration, 10),
|
|
|
|
87600);
|
2016-03-15 08:18:51 +01:00
|
|
|
|
2016-08-06 11:46:26 +02:00
|
|
|
/**
|
|
|
|
* Enable Storage if localStorage or cookie is required
|
|
|
|
* @type {Boolean}
|
|
|
|
* @private
|
|
|
|
*/
|
2016-04-13 18:28:12 +02:00
|
|
|
this.enableStorage = this.enableLocalStorage || this.enableCookie;
|
2016-08-06 11:46:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Storage instance if storage is required
|
|
|
|
* @type {Storage}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this.storage = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hash instance if URL hash is required
|
|
|
|
* @type {Boolean}
|
|
|
|
* @private
|
|
|
|
*/
|
2016-03-19 15:10:59 +01:00
|
|
|
this.hash = null;
|
2016-08-06 11:46:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Current page number
|
|
|
|
* @type {Number}
|
|
|
|
* @private
|
|
|
|
*/
|
2016-03-15 08:18:51 +01:00
|
|
|
this.pageNb = null;
|
2016-08-06 11:46:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Current page length
|
|
|
|
* @type {Number}
|
|
|
|
* @private
|
|
|
|
*/
|
2016-03-18 07:58:32 +01:00
|
|
|
this.pageLength = null;
|
2016-08-06 11:46:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Current column sorting
|
|
|
|
* @type {Object}
|
|
|
|
* @private
|
|
|
|
*/
|
2016-04-12 18:39:20 +02:00
|
|
|
this.sort = null;
|
2016-08-06 11:46:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Current hidden columns
|
|
|
|
* @type {Object}
|
|
|
|
* @private
|
|
|
|
*/
|
2016-04-18 11:04:50 +02:00
|
|
|
this.hiddenCols = null;
|
2016-08-06 11:46:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Filters row visibility
|
|
|
|
* @type {Boolean}
|
|
|
|
* @private
|
|
|
|
*/
|
2016-04-24 17:12:45 +02:00
|
|
|
this.filtersVisibility = null;
|
2016-03-18 07:58:32 +01:00
|
|
|
|
2016-08-06 11:46:26 +02:00
|
|
|
/**
|
|
|
|
* State object
|
|
|
|
* @type {Object}
|
|
|
|
* @private
|
|
|
|
*/
|
2016-03-15 08:18:51 +01:00
|
|
|
this.state = {};
|
2016-08-06 11:46:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Prefix for column ID
|
|
|
|
* @type {String}
|
|
|
|
* @private
|
|
|
|
*/
|
2016-03-15 08:18:51 +01:00
|
|
|
this.prfxCol = 'col_';
|
2016-08-06 11:46:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Prefix for page number ID
|
|
|
|
* @type {String}
|
|
|
|
* @private
|
|
|
|
*/
|
2016-03-16 08:08:26 +01:00
|
|
|
this.pageNbKey = 'page';
|
2016-08-06 11:46:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Prefix for page length ID
|
|
|
|
* @type {String}
|
|
|
|
* @private
|
|
|
|
*/
|
2016-03-20 04:51:08 +01:00
|
|
|
this.pageLengthKey = 'page_length';
|
2016-08-06 11:46:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Prefix for filters visibility ID
|
|
|
|
* @type {String}
|
|
|
|
* @private
|
|
|
|
*/
|
2016-04-24 17:12:45 +02:00
|
|
|
this.filtersVisKey = 'filters_visibility';
|
2016-03-15 08:18:51 +01:00
|
|
|
}
|
|
|
|
|
2016-03-20 06:15:15 +01:00
|
|
|
/**
|
2016-08-06 11:46:26 +02:00
|
|
|
* Initializes State instance
|
2016-03-20 06:15:15 +01:00
|
|
|
*/
|
2016-03-16 08:08:26 +01:00
|
|
|
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());
|
2016-04-10 10:46:20 +02:00
|
|
|
this.emitter.on(['after-page-change', 'after-clearing-filters'],
|
2016-03-18 07:58:32 +01:00
|
|
|
(tf, pageNb) => this.updatePage(pageNb));
|
|
|
|
this.emitter.on(['after-page-length-change'],
|
2016-04-12 18:39:20 +02:00
|
|
|
(tf, pageLength) => this.updatePageLength(pageLength));
|
2016-04-12 09:49:54 +02:00
|
|
|
this.emitter.on(['column-sorted'],
|
2016-04-12 18:39:20 +02:00
|
|
|
(tf, index, descending) => this.updateSort(index, descending));
|
2016-04-13 18:28:12 +02:00
|
|
|
this.emitter.on(['sort-initialized'], () => this._syncSort());
|
2016-04-19 15:28:37 +02:00
|
|
|
this.emitter.on(['columns-visibility-initialized'],
|
2016-04-18 11:04:50 +02:00
|
|
|
() => this._syncColsVisibility());
|
|
|
|
this.emitter.on(['column-shown', 'column-hidden'], (tf, feature,
|
|
|
|
colIndex, hiddenCols) => this.updateColsVisibility(hiddenCols));
|
2016-04-24 17:12:45 +02:00
|
|
|
this.emitter.on(['filters-visibility-initialized'],
|
|
|
|
() => this._syncFiltersVisibility());
|
|
|
|
this.emitter.on(['filters-toggled'],
|
|
|
|
(tf, extension, visible) => this.updateFiltersVisibility(visible));
|
2016-03-16 08:08:26 +01:00
|
|
|
|
2017-12-27 13:13:44 +01:00
|
|
|
if (this.enableHash) {
|
|
|
|
this.hash = new Hash(this);
|
|
|
|
this.hash.init();
|
|
|
|
}
|
2018-01-03 03:07:15 +01:00
|
|
|
if (this.enableStorage) {
|
|
|
|
this.storage = new Storage(this);
|
|
|
|
this.storage.init();
|
|
|
|
}
|
2016-08-06 11:46:26 +02:00
|
|
|
|
2017-12-27 13:13:44 +01:00
|
|
|
/** @inherited */
|
2016-03-15 08:18:51 +01:00
|
|
|
this.initialized = true;
|
|
|
|
}
|
|
|
|
|
2016-03-20 06:15:15 +01:00
|
|
|
|
|
|
|
/**
|
2016-04-18 11:04:50 +02:00
|
|
|
* Update state object based on current features state
|
2016-03-20 06:15:15 +01:00
|
|
|
*/
|
2016-03-19 15:10:59 +01:00
|
|
|
update() {
|
2016-03-20 12:09:08 +01:00
|
|
|
if (!this.isEnabled()) {
|
2016-03-20 04:51:08 +01:00
|
|
|
return;
|
|
|
|
}
|
2016-04-13 18:28:12 +02:00
|
|
|
let state = this.state;
|
2016-03-15 08:18:51 +01:00
|
|
|
let tf = this.tf;
|
|
|
|
|
2016-03-16 08:08:26 +01:00
|
|
|
if (this.persistFilters) {
|
2016-03-15 08:18:51 +01:00
|
|
|
let filterValues = tf.getFiltersValue();
|
|
|
|
|
2016-03-16 08:08:26 +01:00
|
|
|
filterValues.forEach((val, idx) => {
|
2016-03-15 08:18:51 +01:00
|
|
|
let key = `${this.prfxCol}${idx}`;
|
|
|
|
|
2016-05-20 10:08:39 +02:00
|
|
|
if (isString(val) && isEmpty(val)) {
|
2016-04-13 18:28:12 +02:00
|
|
|
if (state.hasOwnProperty(key)) {
|
2016-04-18 11:04:50 +02:00
|
|
|
state[key].flt = undefined;
|
2016-03-15 08:18:51 +01:00
|
|
|
}
|
|
|
|
} else {
|
2016-04-13 18:28:12 +02:00
|
|
|
state[key] = state[key] || {};
|
|
|
|
state[key].flt = val;
|
2016-03-15 08:18:51 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-03-16 08:08:26 +01:00
|
|
|
if (this.persistPageNumber) {
|
2016-05-15 04:56:12 +02:00
|
|
|
if (isNull(this.pageNb)) {
|
2016-04-13 18:28:12 +02:00
|
|
|
state[this.pageNbKey] = undefined;
|
2016-03-18 07:58:32 +01:00
|
|
|
} else {
|
2016-04-13 18:28:12 +02:00
|
|
|
state[this.pageNbKey] = this.pageNb;
|
2016-03-18 07:58:32 +01:00
|
|
|
}
|
2016-03-16 08:08:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this.persistPageLength) {
|
2016-05-15 04:56:12 +02:00
|
|
|
if (isNull(this.pageLength)) {
|
2016-04-13 18:28:12 +02:00
|
|
|
state[this.pageLengthKey] = undefined;
|
2016-03-18 07:58:32 +01:00
|
|
|
} else {
|
2016-04-13 18:28:12 +02:00
|
|
|
state[this.pageLengthKey] = this.pageLength;
|
2016-03-18 07:58:32 +01:00
|
|
|
}
|
2016-03-15 08:18:51 +01:00
|
|
|
}
|
2016-03-20 04:51:08 +01:00
|
|
|
|
2016-04-13 18:28:12 +02:00
|
|
|
if (this.persistSort) {
|
2016-05-15 04:56:12 +02:00
|
|
|
if (!isNull(this.sort)) {
|
2016-04-13 18:28:12 +02:00
|
|
|
// Remove previuosly sorted column
|
|
|
|
Object.keys(state).forEach((key) => {
|
|
|
|
if (key.indexOf(this.prfxCol) !== -1 && state[key]) {
|
|
|
|
state[key].sort = undefined;
|
2016-04-12 18:39:20 +02:00
|
|
|
}
|
|
|
|
});
|
2016-04-13 18:28:12 +02:00
|
|
|
|
2016-04-12 18:39:20 +02:00
|
|
|
let key = `${this.prfxCol}${this.sort.column}`;
|
2016-04-13 18:28:12 +02:00
|
|
|
state[key] = state[key] || {};
|
|
|
|
state[key].sort = { descending: this.sort.descending };
|
2016-04-12 18:39:20 +02:00
|
|
|
}
|
2016-04-12 09:49:54 +02:00
|
|
|
}
|
|
|
|
|
2016-04-19 15:28:37 +02:00
|
|
|
if (this.persistColsVisibility) {
|
2016-05-15 04:56:12 +02:00
|
|
|
if (!isNull(this.hiddenCols)) {
|
2016-04-19 15:28:37 +02:00
|
|
|
// Clear previuosly hidden columns
|
2016-04-18 11:04:50 +02:00
|
|
|
Object.keys(state).forEach((key) => {
|
|
|
|
if (key.indexOf(this.prfxCol) !== -1 && state[key]) {
|
|
|
|
state[key].hidden = undefined;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.hiddenCols.forEach((colIdx) => {
|
|
|
|
let key = `${this.prfxCol}${colIdx}`;
|
|
|
|
state[key] = state[key] || {};
|
|
|
|
state[key].hidden = true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-24 17:12:45 +02:00
|
|
|
if (this.persistFiltersVisibility) {
|
2016-05-15 04:56:12 +02:00
|
|
|
if (isNull(this.filtersVisibility)) {
|
2016-04-24 17:12:45 +02:00
|
|
|
state[this.filtersVisKey] = undefined;
|
|
|
|
} else {
|
|
|
|
state[this.filtersVisKey] = this.filtersVisibility;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-13 18:28:12 +02:00
|
|
|
this.emitter.emit('state-changed', tf, state);
|
2016-03-15 08:18:51 +01:00
|
|
|
}
|
|
|
|
|
2016-03-20 06:15:15 +01:00
|
|
|
/**
|
2016-04-19 15:28:37 +02:00
|
|
|
* Refresh page number field on page number changes
|
2016-03-20 06:15:15 +01:00
|
|
|
*
|
2016-04-24 18:47:01 +02:00
|
|
|
* @param {Number} pageNb Current page number
|
2016-03-20 06:15:15 +01:00
|
|
|
*/
|
2016-03-20 12:09:08 +01:00
|
|
|
updatePage(pageNb) {
|
2016-03-18 07:58:32 +01:00
|
|
|
this.pageNb = pageNb;
|
|
|
|
this.update();
|
|
|
|
}
|
|
|
|
|
2016-03-20 06:15:15 +01:00
|
|
|
/**
|
2016-04-19 15:28:37 +02:00
|
|
|
* Refresh page length field on page length changes
|
2016-03-20 06:15:15 +01:00
|
|
|
*
|
2016-04-24 18:47:01 +02:00
|
|
|
* @param {Number} pageLength Current page length value
|
2016-03-20 06:15:15 +01:00
|
|
|
*/
|
2016-03-20 12:09:08 +01:00
|
|
|
updatePageLength(pageLength) {
|
2016-03-18 07:58:32 +01:00
|
|
|
this.pageLength = pageLength;
|
|
|
|
this.update();
|
|
|
|
}
|
|
|
|
|
2016-04-13 18:28:12 +02:00
|
|
|
/**
|
2016-04-19 15:28:37 +02:00
|
|
|
* Refresh column sorting information on sort changes
|
2016-04-13 18:28:12 +02:00
|
|
|
*
|
|
|
|
* @param index {Number} Column index
|
2016-04-24 18:47:01 +02:00
|
|
|
* @param {Boolean} descending Descending manner
|
2016-04-13 18:28:12 +02:00
|
|
|
*/
|
|
|
|
updateSort(index, descending) {
|
2016-04-12 18:39:20 +02:00
|
|
|
this.sort = {
|
|
|
|
column: index,
|
|
|
|
descending: descending
|
|
|
|
};
|
2016-04-12 09:49:54 +02:00
|
|
|
this.update();
|
|
|
|
}
|
|
|
|
|
2016-04-19 15:28:37 +02:00
|
|
|
/**
|
|
|
|
* Refresh hidden columns information on columns visibility changes
|
|
|
|
*
|
2016-04-24 18:47:01 +02:00
|
|
|
* @param {Array} hiddenCols Columns indexes
|
2016-04-19 15:28:37 +02:00
|
|
|
*/
|
|
|
|
updateColsVisibility(hiddenCols) {
|
2016-04-18 11:04:50 +02:00
|
|
|
this.hiddenCols = hiddenCols;
|
|
|
|
this.update();
|
|
|
|
}
|
|
|
|
|
2016-04-24 18:47:01 +02:00
|
|
|
/**
|
|
|
|
* Refresh filters visibility on filters visibility change
|
|
|
|
*
|
|
|
|
* @param {Boolean} visible Visibility flad
|
|
|
|
*/
|
2016-04-24 17:12:45 +02:00
|
|
|
updateFiltersVisibility(visible) {
|
|
|
|
this.filtersVisibility = visible;
|
|
|
|
this.update();
|
|
|
|
}
|
|
|
|
|
2016-03-20 06:15:15 +01:00
|
|
|
/**
|
|
|
|
* Override state field
|
|
|
|
*
|
|
|
|
* @param state State object
|
|
|
|
*/
|
2016-03-20 12:09:08 +01:00
|
|
|
override(state) {
|
2016-03-20 04:51:08 +01:00
|
|
|
this.state = state;
|
2018-01-07 12:48:35 +01:00
|
|
|
this.emitter.emit('state-changed', this.tf, state);
|
2016-03-20 04:51:08 +01:00
|
|
|
}
|
|
|
|
|
2016-03-20 06:15:15 +01:00
|
|
|
/**
|
2016-04-13 18:28:12 +02:00
|
|
|
* Sync stored features state
|
2016-03-20 06:15:15 +01:00
|
|
|
*/
|
2016-03-16 08:08:26 +01:00
|
|
|
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;
|
|
|
|
|
2016-04-13 18:28:12 +02:00
|
|
|
this._syncFilters();
|
2016-03-15 08:18:51 +01:00
|
|
|
|
2016-03-16 08:08:26 +01:00
|
|
|
if (this.persistPageNumber) {
|
|
|
|
let pageNumber = state[this.pageNbKey];
|
2016-04-13 18:28:12 +02:00
|
|
|
this.emitter.emit('change-page', tf, pageNumber);
|
2016-03-18 07:58:32 +01:00
|
|
|
}
|
|
|
|
|
2016-03-20 12:09:08 +01:00
|
|
|
if (this.persistPageLength) {
|
2016-03-18 07:58:32 +01:00
|
|
|
let pageLength = state[this.pageLengthKey];
|
2016-04-13 18:28:12 +02:00
|
|
|
this.emitter.emit('change-page-results', tf, pageLength);
|
2016-03-15 08:18:51 +01:00
|
|
|
}
|
2016-04-12 18:39:20 +02:00
|
|
|
|
2016-04-13 18:28:12 +02:00
|
|
|
this._syncSort();
|
2016-04-18 11:04:50 +02:00
|
|
|
this._syncColsVisibility();
|
2016-04-24 17:12:45 +02:00
|
|
|
this._syncFiltersVisibility();
|
2016-03-15 08:18:51 +01:00
|
|
|
}
|
|
|
|
|
2016-04-08 17:43:55 +02:00
|
|
|
/**
|
|
|
|
* Override current state with passed one and sync features
|
|
|
|
*
|
2016-04-24 19:00:36 +02:00
|
|
|
* @param {Object} state State object
|
2016-04-08 17:43:55 +02:00
|
|
|
*/
|
|
|
|
overrideAndSync(state) {
|
|
|
|
// To prevent state to react to features changes, state is temporarily
|
|
|
|
// disabled
|
|
|
|
this.disable();
|
|
|
|
// State is overriden with passed state object
|
|
|
|
this.override(state);
|
|
|
|
// New hash state is applied to features
|
|
|
|
this.sync();
|
|
|
|
// State is re-enabled
|
|
|
|
this.enable();
|
|
|
|
}
|
|
|
|
|
2016-04-13 18:28:12 +02:00
|
|
|
/**
|
|
|
|
* Sync filters with stored values and filter table
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_syncFilters() {
|
|
|
|
if (!this.persistFilters) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let state = this.state;
|
|
|
|
let tf = this.tf;
|
|
|
|
|
2018-01-07 12:48:35 +01:00
|
|
|
// clear all filters
|
|
|
|
// TODO: use tf.clearFilters() once it allows to not filter the table
|
|
|
|
tf.eachCol((colIdx) => tf.setFilterValue(colIdx, ''));
|
|
|
|
|
2016-04-13 18:28:12 +02:00
|
|
|
Object.keys(state).forEach((key) => {
|
|
|
|
if (key.indexOf(this.prfxCol) !== -1) {
|
|
|
|
let colIdx = parseInt(key.replace(this.prfxCol, ''), 10);
|
|
|
|
let val = state[key].flt;
|
|
|
|
tf.setFilterValue(colIdx, val);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
tf.filter();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sync sorted column with stored sorting information and sort table
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_syncSort() {
|
|
|
|
if (!this.persistSort) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let state = this.state;
|
|
|
|
let tf = this.tf;
|
|
|
|
|
|
|
|
Object.keys(state).forEach((key) => {
|
|
|
|
if (key.indexOf(this.prfxCol) !== -1) {
|
|
|
|
let colIdx = parseInt(key.replace(this.prfxCol, ''), 10);
|
2016-05-15 04:56:12 +02:00
|
|
|
if (!isUndef(state[key].sort)) {
|
2016-04-13 18:28:12 +02:00
|
|
|
let sort = state[key].sort;
|
|
|
|
this.emitter.emit('sort', tf, colIdx, sort.descending);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-04-19 15:28:37 +02:00
|
|
|
/**
|
|
|
|
* Sync hidden columns with stored information
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_syncColsVisibility() {
|
|
|
|
if (!this.persistColsVisibility) {
|
2016-04-18 11:04:50 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
let state = this.state;
|
|
|
|
let tf = this.tf;
|
2016-04-19 15:28:37 +02:00
|
|
|
let hiddenCols = [];
|
2016-04-18 11:04:50 +02:00
|
|
|
|
|
|
|
Object.keys(state).forEach((key) => {
|
|
|
|
if (key.indexOf(this.prfxCol) !== -1) {
|
|
|
|
let colIdx = parseInt(key.replace(this.prfxCol, ''), 10);
|
2016-05-15 04:56:12 +02:00
|
|
|
if (!isUndef(state[key].hidden)) {
|
2016-04-19 15:28:37 +02:00
|
|
|
hiddenCols.push(colIdx);
|
2016-04-18 11:04:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2016-04-19 15:28:37 +02:00
|
|
|
|
|
|
|
hiddenCols.forEach((colIdx) => {
|
|
|
|
this.emitter.emit('hide-column', tf, colIdx);
|
|
|
|
});
|
2016-04-18 11:04:50 +02:00
|
|
|
}
|
|
|
|
|
2016-04-24 18:47:01 +02:00
|
|
|
/**
|
|
|
|
* Sync filters visibility with stored information
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
*/
|
2016-04-26 11:22:02 +02:00
|
|
|
_syncFiltersVisibility() {
|
2016-04-24 17:12:45 +02:00
|
|
|
if (!this.persistFiltersVisibility) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let state = this.state;
|
|
|
|
let tf = this.tf;
|
|
|
|
let filtersVisibility = state[this.filtersVisKey];
|
|
|
|
|
2016-04-26 11:22:02 +02:00
|
|
|
this.filtersVisibility = filtersVisibility;
|
2016-04-24 18:47:01 +02:00
|
|
|
this.emitter.emit('show-filters', tf, filtersVisibility);
|
2016-04-24 17:12:45 +02:00
|
|
|
}
|
|
|
|
|
2016-03-20 06:15:15 +01:00
|
|
|
/**
|
|
|
|
* Destroy State instance
|
|
|
|
*/
|
2016-03-16 08:08:26 +01:00
|
|
|
destroy() {
|
|
|
|
if (!this.initialized) {
|
2016-03-15 08:18:51 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-20 06:15:15 +01:00
|
|
|
this.state = {};
|
|
|
|
|
2016-03-18 07:58:32 +01:00
|
|
|
this.emitter.off(['after-filtering'], () => this.update());
|
2016-04-10 10:46:20 +02:00
|
|
|
this.emitter.off(['after-page-change', 'after-clearing-filters'],
|
2016-03-18 07:58:32 +01:00
|
|
|
(tf, pageNb) => this.updatePage(pageNb));
|
|
|
|
this.emitter.off(['after-page-length-change'],
|
|
|
|
(tf, index) => this.updatePageLength(index));
|
2016-04-12 09:49:54 +02:00
|
|
|
this.emitter.off(['column-sorted'],
|
2016-04-12 18:39:20 +02:00
|
|
|
(tf, index, descending) => this.updateSort(index, descending));
|
2016-04-13 18:28:12 +02:00
|
|
|
this.emitter.off(['sort-initialized'], () => this._syncSort());
|
2016-04-19 15:28:37 +02:00
|
|
|
this.emitter.off(['columns-visibility-initialized'],
|
2016-04-18 11:04:50 +02:00
|
|
|
() => this._syncColsVisibility());
|
|
|
|
this.emitter.off(['column-shown', 'column-hidden'], (tf, feature,
|
|
|
|
colIndex, hiddenCols) => this.updateColsVisibility(hiddenCols));
|
2016-04-24 17:12:45 +02:00
|
|
|
this.emitter.off(['filters-visibility-initialized'],
|
|
|
|
() => this._syncFiltersVisibility());
|
|
|
|
this.emitter.off(['filters-toggled'],
|
|
|
|
(tf, extension, visible) => this.updateFiltersVisibility(visible));
|
2016-03-16 08:08:26 +01:00
|
|
|
|
2016-03-20 12:09:08 +01:00
|
|
|
if (this.enableHash) {
|
2016-03-19 15:10:59 +01:00
|
|
|
this.hash.destroy();
|
|
|
|
this.hash = null;
|
|
|
|
}
|
|
|
|
|
2016-04-13 18:28:12 +02:00
|
|
|
if (this.enableStorage) {
|
2016-04-07 12:13:54 +02:00
|
|
|
this.storage.destroy();
|
|
|
|
this.storage = null;
|
|
|
|
}
|
|
|
|
|
2016-03-15 08:18:51 +01:00
|
|
|
this.initialized = false;
|
|
|
|
}
|
|
|
|
}
|