1
0
Fork 0
mirror of https://github.com/koalyptus/TableFilter.git synced 2024-06-10 18:03:07 +02:00

Added cookie persistence

This commit is contained in:
Max Guglielmi 2016-04-08 12:13:29 +02:00
parent 2656435d00
commit f731b6c79c
4 changed files with 60 additions and 22 deletions

View file

@ -7598,6 +7598,7 @@ return /******/ (function(modules) { // webpackBootstrap
_this.persistFilters = cfg.filters === false ? false : true; _this.persistFilters = cfg.filters === false ? false : true;
_this.persistPageNumber = Boolean(cfg.page_number); _this.persistPageNumber = Boolean(cfg.page_number);
_this.persistPageLength = Boolean(cfg.page_length); _this.persistPageLength = Boolean(cfg.page_length);
_this.cookieDuration = !isNaN(cfg.cookie_duration) ? parseInt(cfg.cookie_duration, 10) : 87600;
_this.hash = null; _this.hash = null;
_this.pageNb = null; _this.pageNb = null;
@ -7961,13 +7962,20 @@ return /******/ (function(modules) { // webpackBootstrap
/***/ }, /***/ },
/* 28 */ /* 28 */
/***/ function(module, exports) { /***/ function(module, exports, __webpack_require__) {
'use strict'; 'use strict';
Object.defineProperty(exports, "__esModule", { Object.defineProperty(exports, "__esModule", {
value: true value: true
}); });
exports.Storage = exports.hasStorage = undefined;
var _cookie = __webpack_require__(9);
var _cookie2 = _interopRequireDefault(_cookie);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
@ -7986,10 +7994,9 @@ return /******/ (function(modules) { // webpackBootstrap
this.state = state; this.state = state;
this.tf = state.tf; this.tf = state.tf;
this.enableLocalStorage = state.enableLocalStorage && hasStorage(); this.enableLocalStorage = state.enableLocalStorage && hasStorage();
this.enableCookie = state.enableCookie; this.enableCookie = state.enableCookie && !this.enableLocalStorage;
this.emitter = state.emitter; this.emitter = state.emitter;
this.duration = state.cookieDuration;
this.prxStorage = 'store';
} }
Storage.prototype.init = function init() { Storage.prototype.init = function init() {
@ -8003,24 +8010,38 @@ return /******/ (function(modules) { // webpackBootstrap
}); });
}; };
// update(state){
// console.log(this.enableLocalStorage, this.enableCookie, state);
// }
Storage.prototype.save = function save(state) { Storage.prototype.save = function save(state) {
if (this.enableLocalStorage) { if (this.enableLocalStorage) {
localStorage[this.getKey()] = JSON.stringify(state); localStorage[this.getKey()] = JSON.stringify(state);
} else {
_cookie2.default.write(this.getKey(), JSON.stringify(state), this.duration);
} }
}; };
Storage.prototype.delete = function _delete() { Storage.prototype.parse = function parse() {
var state = null;
if (this.enableLocalStorage) {
state = localStorage[this.getKey()];
} else {
state = _cookie2.default.read(this.getKey());
}
if (!state) {
return null;
}
return JSON.parse(state);
};
Storage.prototype.remove = function remove() {
if (this.enableLocalStorage) { if (this.enableLocalStorage) {
localStorage.removeItem(this.getKey()); localStorage.removeItem(this.getKey());
} else {
_cookie2.default.remove(this.getKey());
} }
}; };
Storage.prototype.sync = function sync() { Storage.prototype.sync = function sync() {
var state = JSON.parse(localStorage[this.getKey()]); var state = this.parse();
if (!state) { if (!state) {
return; return;
} }
@ -8050,7 +8071,7 @@ return /******/ (function(modules) { // webpackBootstrap
return _this2.sync(); return _this2.sync();
}); });
this.delete(); this.remove();
this.state = null; this.state = null;
this.emitter = null; this.emitter = null;

View file

@ -31,6 +31,8 @@ export class State extends Feature {
this.persistFilters = cfg.filters === false ? false : true; this.persistFilters = cfg.filters === false ? false : true;
this.persistPageNumber = Boolean(cfg.page_number); this.persistPageNumber = Boolean(cfg.page_number);
this.persistPageLength = Boolean(cfg.page_length); this.persistPageLength = Boolean(cfg.page_length);
this.cookieDuration = !isNaN(cfg.cookie_duration) ?
parseInt(cfg.cookie_duration, 10) : 87600;
this.hash = null; this.hash = null;
this.pageNb = null; this.pageNb = null;

View file

@ -1,4 +1,6 @@
import Cookie from '../cookie';
const global = window; const global = window;
const JSON = global.JSON; const JSON = global.JSON;
const localStorage = global.localStorage; const localStorage = global.localStorage;
@ -13,10 +15,9 @@ export class Storage {
this.state = state; this.state = state;
this.tf = state.tf; this.tf = state.tf;
this.enableLocalStorage = state.enableLocalStorage && hasStorage(); this.enableLocalStorage = state.enableLocalStorage && hasStorage();
this.enableCookie = state.enableCookie; this.enableCookie = state.enableCookie && !this.enableLocalStorage;
this.emitter = state.emitter; this.emitter = state.emitter;
this.duration = state.cookieDuration;
this.prxStorage = 'store';
} }
@ -25,24 +26,38 @@ export class Storage {
this.emitter.on(['initialized'], () => this.sync()); this.emitter.on(['initialized'], () => this.sync());
} }
// update(state){
// console.log(this.enableLocalStorage, this.enableCookie, state);
// }
save(state){ save(state){
if(this.enableLocalStorage) { if(this.enableLocalStorage) {
localStorage[this.getKey()] = JSON.stringify(state); localStorage[this.getKey()] = JSON.stringify(state);
} else {
Cookie.write(this.getKey(), JSON.stringify(state), this.duration);
} }
} }
delete(){ parse(){
let state = null;
if(this.enableLocalStorage) {
state = localStorage[this.getKey()];
} else {
state = Cookie.read(this.getKey());
}
if(!state){
return null;
}
return JSON.parse(state);
}
remove(){
if(this.enableLocalStorage) { if(this.enableLocalStorage) {
localStorage.removeItem(this.getKey()); localStorage.removeItem(this.getKey());
} else {
Cookie.remove(this.getKey());
} }
} }
sync() { sync() {
let state = JSON.parse(localStorage[this.getKey()]); let state = this.parse();
if (!state) { if (!state) {
return; return;
} }
@ -66,7 +81,7 @@ export class Storage {
this.emitter.off(['state-changed'], (tf, state) => this.save(state)); this.emitter.off(['state-changed'], (tf, state) => this.save(state));
this.emitter.off(['initialized'], () => this.sync()); this.emitter.off(['initialized'], () => this.sync());
this.delete(); this.remove();
this.state = null; this.state = null;
this.emitter = null; this.emitter = null;

View file

@ -37,7 +37,7 @@
var tfConfig = { var tfConfig = {
base_path: '../dist/tablefilter/', base_path: '../dist/tablefilter/',
state: { state: {
types: ['local_storage'/*, 'hash'*/], types: ['cookie'/*,'local_storage', 'hash'*/],
page_number: true, page_number: true,
page_length: true page_length: true
}, },