From b55bd0119a7b14738448693029661c39785beeae Mon Sep 17 00:00:00 2001 From: Max Guglielmi Date: Tue, 15 Mar 2016 18:18:51 +1100 Subject: [PATCH] Continued stateful class --- dist/tablefilter/style/colsVisibility.css | 2 +- dist/tablefilter/style/filtersVisibility.css | 2 +- dist/tablefilter/style/tablefilter.css | 2 +- .../style/themes/default/default.css | 2 +- .../style/themes/mytheme/mytheme.css | 2 +- .../style/themes/skyblue/skyblue.css | 2 +- dist/tablefilter/tablefilter.js | 126 ++++++++++++++---- dist/tablefilter/tf-1.js | 4 +- src/modules/state.js | 71 ---------- src/modules/stateful.js | 126 ++++++++++++++++++ src/tablefilter.js | 8 +- static/templates/pagination.html | 7 +- 12 files changed, 239 insertions(+), 115 deletions(-) delete mode 100644 src/modules/state.js create mode 100644 src/modules/stateful.js diff --git a/dist/tablefilter/style/colsVisibility.css b/dist/tablefilter/style/colsVisibility.css index 7eb5ff0e..dc4a5435 100644 --- a/dist/tablefilter/style/colsVisibility.css +++ b/dist/tablefilter/style/colsVisibility.css @@ -1,6 +1,6 @@ /** * tablefilter v0.2.0 by Max Guglielmi - * build date: 2016-03-14T05:50:30.144Z + * build date: 2016-03-15T05:28:00.528Z * MIT License */ span.colVisSpan{text-align:left;}span.colVisSpan a.colVis{display:inline-block;padding:7px 5px 0;font-size:inherit;font-weight:inherit;vertical-align:top}div.colVisCont{position:relative;background:#fff;-webkit-box-shadow:3px 3px 2px #888;-moz-box-shadow:3px 3px 2px #888;box-shadow:3px 3px 2px #888;position:absolute;display:none;border:1px solid #ccc;height:auto;width:250px;background-color:#fff;margin:35px 0 0 -100px;z-index:10000;padding:10px 10px 10px 10px;text-align:left;font-size:12px;}div.colVisCont:after,div.colVisCont:before{bottom:100%;left:50%;border:solid transparent;content:" ";height:0;width:0;position:absolute;pointer-events:none}div.colVisCont:after{border-color:rgba(255,255,255,0);border-bottom-color:#fff;border-width:10px;margin-left:-10px}div.colVisCont:before{border-color:rgba(255,255,255,0);border-bottom-color:#ccc;border-width:12px;margin-left:-12px}div.colVisCont p{margin:6px auto 6px auto}div.colVisCont a.colVis{display:initial;font-weight:inherit}ul.cols_checklist{padding:0;margin:0;list-style:none;}ul.cols_checklist label{display:block}ul.cols_checklist input{vertical-align:middle;margin:2px 5px 2px 1px}li.cols_checklist_item{padding:4px;margin:0;}li.cols_checklist_item:hover{background-color:#335ea8;color:#fff}.cols_checklist_slc_item{background-color:#335ea8;color:#fff} \ No newline at end of file diff --git a/dist/tablefilter/style/filtersVisibility.css b/dist/tablefilter/style/filtersVisibility.css index 77bf4db1..892de3c7 100644 --- a/dist/tablefilter/style/filtersVisibility.css +++ b/dist/tablefilter/style/filtersVisibility.css @@ -1,6 +1,6 @@ /** * tablefilter v0.2.0 by Max Guglielmi - * build date: 2016-03-14T05:50:30.144Z + * build date: 2016-03-15T05:28:00.528Z * MIT License */ span.expClpFlt a.btnExpClpFlt{width:35px;height:35px;display:inline-block;}span.expClpFlt a.btnExpClpFlt:hover{background-color:#f4f4f4}span.expClpFlt img{padding:8px 11px 11px 11px} \ No newline at end of file diff --git a/dist/tablefilter/style/tablefilter.css b/dist/tablefilter/style/tablefilter.css index 072d71d3..84b23b18 100644 --- a/dist/tablefilter/style/tablefilter.css +++ b/dist/tablefilter/style/tablefilter.css @@ -1,6 +1,6 @@ /** * tablefilter v0.2.0 by Max Guglielmi - * build date: 2016-03-14T05:50:30.144Z + * build date: 2016-03-15T05:28:00.528Z * MIT License */ .activeHeader{background-color:#66afe9 !important;color:#fff !important} diff --git a/dist/tablefilter/style/themes/default/default.css b/dist/tablefilter/style/themes/default/default.css index 6604a774..83100806 100644 --- a/dist/tablefilter/style/themes/default/default.css +++ b/dist/tablefilter/style/themes/default/default.css @@ -1,6 +1,6 @@ /** * tablefilter v0.2.0 by Max Guglielmi - * build date: 2016-03-14T05:50:30.144Z + * build date: 2016-03-15T05:28:00.528Z * MIT License */ table.TF{border-left:1px solid #ccc;border-top:none;border-right:none;border-bottom:none;}table.TF th{background:#ebecee url("images/bg_th.jpg") left top repeat-x;border-bottom:1px solid #d0d0d0;border-right:1px solid #d0d0d0;border-left:1px solid #fff;border-top:1px solid #fff;color:#333}table.TF td{border-bottom:1px dotted #999;padding:5px}.fltrow{background-color:#ebecee !important;}.fltrow th,.fltrow td{border-bottom:1px dotted #666 !important;padding:1px 3px 1px 3px !important}.flt,select.flt,select.flt_multi,.flt_s,.single_flt,.div_checklist{border:1px solid #999 !important}input.flt{width:99% !important}.inf{height:$min-height;background:#d7d7d7 url("images/bg_infDiv.jpg") 0 0 repeat-x !important}input.reset{background:transparent url("images/btn_eraser.gif") center center no-repeat !important}.helpBtn:hover{background-color:transparent}.nextPage{background:transparent url("images/btn_next_page.gif") center center no-repeat !important;}.nextPage:hover{background:transparent url("images/btn_over_next_page.gif") center center no-repeat !important}.previousPage{background:transparent url("images/btn_previous_page.gif") center center no-repeat !important;}.previousPage:hover{background:transparent url("images/btn_over_previous_page.gif") center center no-repeat !important}.firstPage{background:transparent url("images/btn_first_page.gif") center center no-repeat !important;}.firstPage:hover{background:transparent url("images/btn_over_first_page.gif") center center no-repeat !important}.lastPage{background:transparent url("images/btn_last_page.gif") center center no-repeat !important;}.lastPage:hover{background:transparent url("images/btn_over_last_page.gif") center center no-repeat !important}div.grd_Cont{background-color:#ebecee !important;border:1px solid #ccc !important;padding:0 !important;}div.grd_Cont .even{background-color:#fff}div.grd_Cont .odd{background-color:#d5d5d5}div.grd_headTblCont{background-color:#ebecee !important;border-bottom:none !important;}div.grd_headTblCont table{border-right:none !important}div.grd_tblCont table th,div.grd_headTblCont table th,div.grd_headTblCont table td{background:#ebecee url("images/bg_th.jpg") left top repeat-x !important;border-bottom:1px solid #d0d0d0 !important;border-right:1px solid #d0d0d0 !important;border-left:1px solid #fff !important;border-top:1px solid #fff !important}div.grd_tblCont table td{border-bottom:1px solid #999 !important}.grd_inf{background:#d7d7d7 url("images/bg_infDiv.jpg") 0 0 repeat-x !important;border-top:1px solid #d0d0d0 !important}.loader{border:1px solid #999}.defaultLoader{width:32px;height:32px;background:transparent url("images/img_loading.gif") 0 0 no-repeat !important}.even{background-color:#fff}.odd{background-color:#d5d5d5}span.expClpFlt a.btnExpClpFlt:hover{background-color:transparent !important}.activeHeader{background:#999 !important} \ No newline at end of file diff --git a/dist/tablefilter/style/themes/mytheme/mytheme.css b/dist/tablefilter/style/themes/mytheme/mytheme.css index b4dad713..e1cc1d5d 100644 --- a/dist/tablefilter/style/themes/mytheme/mytheme.css +++ b/dist/tablefilter/style/themes/mytheme/mytheme.css @@ -1,6 +1,6 @@ /** * tablefilter v0.2.0 by Max Guglielmi - * build date: 2016-03-14T05:50:30.144Z + * build date: 2016-03-15T05:28:00.528Z * MIT License */ table.TF{border-left:1px dotted #81963b;border-top:none;border-right:0;border-bottom:none;}table.TF th{background:#39424b url("images/bg_headers.jpg") left top repeat-x;border-bottom:0;border-right:1px dotted #d0d0d0;border-left:0;border-top:0;color:#fff}table.TF td{border-bottom:1px dotted #81963b;border-right:1px dotted #81963b;padding:5px}.fltrow{background-color:#81963b !important;}.fltrow th,.fltrow td{border-bottom:1px dotted #39424b !important;border-right:1px dotted #fff !important;border-left:0 !important;border-top:0 !important;padding:1px 3px 1px 3px !important}.flt,select.flt,select.flt_multi,.flt_s,.single_flt,.div_checklist{border:1px solid #687830 !important}input.flt{width:99% !important}.inf{background:#d8d8d8;height:$min-height}input.reset{width:53px;background:transparent url("images/btn_filter.png") center center no-repeat !important}.helpBtn:hover{background-color:transparent}.nextPage{background:transparent url("images/btn_next_page.gif") center center no-repeat !important}.previousPage{background:transparent url("images/btn_previous_page.gif") center center no-repeat !important}.firstPage{background:transparent url("images/btn_first_page.gif") center center no-repeat !important}.lastPage{background:transparent url("images/btn_last_page.gif") center center no-repeat !important}div.grd_Cont{background:#81963b url("images/bg_headers.jpg") left top repeat-x !important;border:1px solid #ccc !important;padding:0 1px 1px 1px !important;}div.grd_Cont .even{background-color:#bccd83}div.grd_Cont .odd{background-color:#fff}div.grd_headTblCont{background-color:#ebecee !important;border-bottom:none !important}div.grd_tblCont table{border-right:none !important;}div.grd_tblCont table td{border-bottom:1px dotted #81963b;border-right:1px dotted #81963b}div.grd_tblCont table th,div.grd_headTblCont table th{background:transparent url("images/bg_headers.jpg") 0 0 repeat-x !important;border-bottom:0 !important;border-right:1px dotted #d0d0d0 !important;border-left:0 !important;border-top:0 !important;padding:0 4px 0 4px !important;color:#fff !important;height:35px !important}div.grd_headTblCont table td{border-bottom:1px dotted #39424b !important;border-right:1px dotted #fff !important;border-left:0 !important;border-top:0 !important;background-color:#81963b !important;padding:1px 3px 1px 3px !important}.grd_inf{background-color:#d8d8d8;border-top:1px solid #d0d0d0 !important}.loader{border:0 !important;background:#81963b !important}.defaultLoader{width:32px;height:32px;background:transparent url("images/img_loading.gif") 0 0 no-repeat !important}.even{background-color:#bccd83}.odd{background-color:#fff}span.expClpFlt a.btnExpClpFlt:hover{background-color:transparent !important}.activeHeader{background:#81963b !important} \ No newline at end of file diff --git a/dist/tablefilter/style/themes/skyblue/skyblue.css b/dist/tablefilter/style/themes/skyblue/skyblue.css index e90231aa..b3b543eb 100644 --- a/dist/tablefilter/style/themes/skyblue/skyblue.css +++ b/dist/tablefilter/style/themes/skyblue/skyblue.css @@ -1,6 +1,6 @@ /** * tablefilter v0.2.0 by Max Guglielmi - * build date: 2016-03-14T05:50:30.144Z + * build date: 2016-03-15T05:28:00.528Z * MIT License */ table.TF{padding:0;color:#000;border-right:1px solid #a4bed4;border-top:1px solid #a4bed4;border-left:1px solid #a4bed4;border-bottom:0;}table.TF th{margin:0;color:inherit;background:#d1e5fe url("images/bg_skyblue.gif") 0 0 repeat-x;border-color:#fdfdfd #a4bed4 #a4bed4 #fdfdfd;border-width:1px;border-style:solid}table.TF td{margin:0;padding:5px;color:inherit;border-bottom:1px solid #a4bed4;border-left:0;border-top:0;border-right:0}.fltrow{background-color:#d1e5fe !important;}.fltrow th,.fltrow td{padding:1px 3px 1px 3px !important}.flt,select.flt,select.flt_multi,.flt_s,.single_flt,.div_checklist{border:1px solid #a4bed4 !important}input.flt{width:99% !important}.inf{background-color:#e3efff !important;border:1px solid #a4bed4;height:$min-height;color:#004a6f}div.tot,div.status{border-right:0 !important}.helpBtn:hover{background-color:transparent}input.reset{background:transparent url("images/icn_clear_filters.png") center center no-repeat !important}.nextPage{background:transparent url("images/btn_next_page.gif") center center no-repeat !important;border:1px solid transparent !important;}.nextPage:hover{background:#ffe4ab url("images/btn_next_page.gif") center center no-repeat !important;border:1px solid #ffb552 !important}.previousPage{background:transparent url("images/btn_prev_page.gif") center center no-repeat !important;border:1px solid transparent !important;}.previousPage:hover{background:#ffe4ab url("images/btn_prev_page.gif") center center no-repeat !important;border:1px solid #ffb552 !important}.firstPage{background:transparent url("images/btn_first_page.gif") center center no-repeat !important;border:1px solid transparent !important;}.firstPage:hover{background:#ffe4ab url("images/btn_first_page.gif") center center no-repeat !important;border:1px solid #ffb552 !important}.lastPage{background:transparent url("images/btn_last_page.gif") center center no-repeat !important;border:1px solid transparent !important;}.lastPage:hover{background:#ffe4ab url("images/btn_last_page.gif") center center no-repeat !important;border:1px solid #ffb552 !important}.activeHeader{background:#ffe4ab !important;border:1px solid #ffb552 !important;color:inherit !important}div.grd_Cont{background-color:#d9eaed !important;border:1px solid #9cc !important;padding:0 !important;}div.grd_Cont .even{background-color:#fff}div.grd_Cont .odd{background-color:#e3efff}div.grd_headTblCont{background-color:#d9eaed !important;border-bottom:none !important}div.grd_tblCont table{border-right:none !important}div.grd_tblCont table th,div.grd_headTblCont table th,div.grd_headTblCont table td{background:#d9eaed url("images/bg_skyblue.gif") left top repeat-x;border-bottom:1px solid #a4bed4;border-right:1px solid #a4bed4 !important;border-left:1px solid #fff !important;border-top:1px solid #fff !important}div.grd_tblCont table td{border-bottom:1px solid #a4bed4 !important;border-right:0 !important;border-left:0 !important;border-top:0 !important}.grd_inf{background-color:#cce2fe;color:#004a6f;border-top:1px solid #9cc !important;}.grd_inf a{text-decoration:none;font-weight:bold}.loader{background-color:#2d8eef;border:1px solid #cce2fe;border-radius:5px}.even{background-color:#fff}.odd{background-color:#e3efff}span.expClpFlt a.btnExpClpFlt:hover{background-color:transparent !important}.ezActiveRow{background-color:#ffdc61 !important;color:inherit}.ezSelectedRow{background-color:#ffe4ab !important;color:inherit}.ezActiveCell{background-color:#fff !important;color:#000 !important;font-weight:bold}.ezETSelectedCell{background-color:#fff !important;font-weight:bold;color:#000 !important} \ No newline at end of file diff --git a/dist/tablefilter/tablefilter.js b/dist/tablefilter/tablefilter.js index 06f8d7dc..948fbd65 100644 --- a/dist/tablefilter/tablefilter.js +++ b/dist/tablefilter/tablefilter.js @@ -173,7 +173,7 @@ return /******/ (function(modules) { // webpackBootstrap var _noResults = __webpack_require__(25); - var _state = __webpack_require__(26); + var _stateful = __webpack_require__(26); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } @@ -668,10 +668,10 @@ return /******/ (function(modules) { // webpackBootstrap } if (this.isStateful) { - if (!Mod.state) { - Mod.state = new _state.State(tf); + if (!Mod.stateful) { + Mod.stateful = new _stateful.Stateful(tf); } - Mod.state.init(); + Mod.stateful.init(); } if (this.hasPersistence) { @@ -7695,12 +7695,18 @@ return /******/ (function(modules) { // webpackBootstrap Object.defineProperty(exports, "__esModule", { value: true }); - exports.State = undefined; + exports.Stateful = undefined; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _feature = __webpack_require__(11); + var _string = __webpack_require__(3); + + var _string2 = _interopRequireDefault(_string); + + 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 _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } @@ -7708,36 +7714,40 @@ return /******/ (function(modules) { // webpackBootstrap function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } var global = window; + var JSON = global.JSON; + var location = global.location; var hasHashChange = function hasHashChange() { var docMode = global.documentMode; return 'onhashchange' in global && (docMode === undefined || docMode > 7); }; - var State = exports.State = function (_Feature) { - _inherits(State, _Feature); + var Stateful = exports.Stateful = function (_Feature) { + _inherits(Stateful, _Feature); /** - * Makes selected features stateful via URL hash + * Makes features stateful via URL hash * @param {Object} tf TableFilter instance */ - function State(tf) { - _classCallCheck(this, State); + function Stateful(tf) { + _classCallCheck(this, Stateful); - //configuration object - - var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(State).call(this, tf, 'state')); + var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(Stateful).call(this, tf, 'stateful')); var cfg = _this.config.stateful; - _this.filters = cfg.filters === false ? false : true; + _this.persistFilters = cfg.filters === false ? false : true; + _this.persistPageNumber = Boolean(cfg.page_number); + _this.pageNb = null; _this.lastHash = null; - _this.hashObj = {}; + _this.state = {}; + _this.prfxCol = 'col_'; + _this.prfxPageNb = 'page'; return _this; } - _createClass(State, [{ + _createClass(Stateful, [{ key: 'init', value: function init() { var _this2 = this; @@ -7746,59 +7756,117 @@ return /******/ (function(modules) { // webpackBootstrap return; } - // subscribe to after-filtering event + this.emitter.on(['initialized'], function () { + return _this2.sync(); + }); this.emitter.on(['after-filtering'], function () { return _this2.update(); }); + this.emitter.on(['after-changing-page'], function () { + return _this2.update(); + }); this.initialized = true; } }, { - key: 'formatHash', - value: function formatHash() { + key: 'format', + value: function format() { var _this3 = this; - if (this.filters) { - var filterValues = this.tf.getFiltersValue(); + var tf = this.tf; + + if (this.persistFilters) { + var filterValues = tf.getFiltersValue(); filterValues.forEach(function (val, idx) { - _this3.hashObj['col_' + idx] = _this3.hashObj['col_' + idx] || {}; - _this3.hashObj['col_' + idx]['flt'] = val; + var key = '' + _this3.prfxCol + idx; + + if (_string2.default.isEmpty(val)) { + if (_this3.state.hasOwnProperty(key)) { + _this3.state[key] = undefined; + } + } else { + _this3.state[key] = _this3.state[key] || {}; + _this3.state[key].flt = val; + } }); } - return '#' + JSON.stringify(this.hashObj); + if (this.persistPageNumber) { + var pageNumber = tf.feature('paging').getPage(); + this.state[this.prfxPageNb] = pageNumber; + } + + return '#' + JSON.stringify(this.state); + } + }, { + key: 'parse', + value: function parse(hash) { + if (hash.indexOf('#') === 0) { + hash = hash.substr(1); + } else { + hash = '{}'; + } + return JSON.parse(hash); } }, { key: 'update', value: function update() { - var hash = this.formatHash(); + var hash = this.format(); console.log(hash, this.lastHash, this.lastHash === hash); if (this.lastHash === hash) { return; } - global.location.hash = hash; + location.hash = hash; this.lastHash = hash; } + }, { + key: 'sync', + value: function sync() { + var _this4 = this; + + var state = this.parse(location.hash); + var tf = this.tf; + + if (this.persistFilters) { + Object.keys(state).forEach(function (key) { + if (key.indexOf(_this4.prfxCol) !== -1) { + var colIdx = parseInt(key.replace(_this4.prfxCol, '')); + var val = state[key].flt; + tf.setFilterValue(colIdx, val); + } + }); + + tf.filter(); + } + + if (this.persistPageNumber) { + var pageNumber = state[this.prfxPageNb]; + tf.feature('paging').setPage(pageNumber); + } + } }, { key: 'destroy', value: function destroy() { - var _this4 = this; + var _this5 = this; if (!this.initialized) { return; } + this.emitter.on(['initialized'], function () { + return _this5.sync(); + }); this.emitter.off(['after-filtering'], function () { - return _this4.update(); + return _this5.update(); }); this.initialized = false; } }]); - return State; + return Stateful; }(_feature.Feature); /***/ } diff --git a/dist/tablefilter/tf-1.js b/dist/tablefilter/tf-1.js index 64888aa3..6d551761 100644 --- a/dist/tablefilter/tf-1.js +++ b/dist/tablefilter/tf-1.js @@ -58,8 +58,8 @@ webpackJsonp([1],{ "./modules/popupFilter.js": 14, "./modules/rowsCounter": 19, "./modules/rowsCounter.js": 19, - "./modules/state": 26, - "./modules/state.js": 26, + "./modules/stateful": 26, + "./modules/stateful.js": 26, "./modules/statusBar": 20, "./modules/statusBar.js": 20, "./modules/store": 8, diff --git a/src/modules/state.js b/src/modules/state.js deleted file mode 100644 index 59a2f0b5..00000000 --- a/src/modules/state.js +++ /dev/null @@ -1,71 +0,0 @@ -import {Feature} from './feature'; - -const global = window; -const hasHashChange = ()=> { - var docMode = global.documentMode; - return ('onhashchange' in global) && (docMode === undefined || docMode > 7); -}; - -export class State extends Feature{ - - /** - * Makes selected features stateful via URL hash - * @param {Object} tf TableFilter instance - */ - constructor(tf){ - super(tf, 'state'); - - //configuration object - let cfg = this.config.stateful; - - this.filters = cfg.filters === false ? false : true; - - this.lastHash = null; - this.hashObj = {}; - } - - init(){ - if(this.initialized || !hasHashChange()){ - return; - } - - // subscribe to after-filtering event - this.emitter.on(['after-filtering'], ()=> this.update()); - - this.initialized = true; - } - - formatHash(){ - if(this.filters){ - let filterValues = this.tf.getFiltersValue(); - - filterValues.forEach((val, idx)=> { - this.hashObj['col_' + idx] = this.hashObj['col_' + idx] || {}; - this.hashObj['col_' + idx]['flt'] = val; - }); - } - - return `#${JSON.stringify(this.hashObj)}`; - } - - update(){ - let hash = this.formatHash(); - console.log(hash, this.lastHash, this.lastHash === hash); - if(this.lastHash === hash){ - return; - } - - global.location.hash = hash; - this.lastHash = hash; - } - - destroy(){ - if(!this.initialized){ - return; - } - - this.emitter.off(['after-filtering'], ()=> this.update()); - - this.initialized = false; - } -} diff --git a/src/modules/stateful.js b/src/modules/stateful.js new file mode 100644 index 00000000..86fb6833 --- /dev/null +++ b/src/modules/stateful.js @@ -0,0 +1,126 @@ +import {Feature} from './feature'; +import Str from '../string'; + +const global = window; +const JSON = global.JSON; +const location = global.location; +const hasHashChange = ()=> { + var docMode = global.documentMode; + return ('onhashchange' in global) && (docMode === undefined || docMode > 7); +}; + +export class Stateful extends Feature{ + + /** + * Makes features stateful via URL hash + * @param {Object} tf TableFilter instance + */ + constructor(tf){ + super(tf, 'stateful'); + + let cfg = this.config.stateful; + + this.persistFilters = cfg.filters === false ? false : true; + this.persistPageNumber = Boolean(cfg.page_number); + + this.pageNb = null; + this.lastHash = null; + this.state = {}; + this.prfxCol = 'col_'; + this.prfxPageNb = 'page'; + } + + init(){ + if(this.initialized || !hasHashChange()){ + return; + } + + this.emitter.on(['initialized'], ()=> this.sync()); + this.emitter.on(['after-filtering'], ()=> this.update()); + this.emitter.on(['after-changing-page'], ()=> this.update()); + + this.initialized = true; + } + + format(){ + let tf = this.tf; + + if(this.persistFilters){ + let filterValues = tf.getFiltersValue(); + + filterValues.forEach((val, idx)=> { + let key = `${this.prfxCol}${idx}`; + + if(Str.isEmpty(val)){ + if(this.state.hasOwnProperty(key)){ + this.state[key] = undefined; + } + } else { + this.state[key] = this.state[key] || {}; + this.state[key].flt = val; + } + + }); + } + + if(this.persistPageNumber){ + let pageNumber = tf.feature('paging').getPage(); + this.state[this.prfxPageNb] = pageNumber; + } + + return `#${JSON.stringify(this.state)}`; + } + + parse(hash){ + if(hash.indexOf('#') === 0){ + hash = hash.substr(1); + } else { + hash = '{}'; + } + return JSON.parse(hash); + } + + update(){ + let hash = this.format(); + console.log(hash, this.lastHash, this.lastHash === hash); + if(this.lastHash === hash){ + return; + } + + location.hash = hash; + this.lastHash = hash; + } + + sync(){ + let state = this.parse(location.hash); + 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, '')); + let val = state[key].flt; + tf.setFilterValue(colIdx, val); + } + }); + + tf.filter(); + } + + if(this.persistPageNumber){ + let pageNumber = state[this.prfxPageNb]; + tf.feature('paging').setPage(pageNumber); + } + } + + destroy(){ + if(!this.initialized){ + return; + } + + this.emitter.on(['initialized'], ()=> this.sync()); + this.emitter.off(['after-filtering'], ()=> this.update()); + + this.initialized = false; + } +} diff --git a/src/tablefilter.js b/src/tablefilter.js index e8d953b5..ffa4537d 100644 --- a/src/tablefilter.js +++ b/src/tablefilter.js @@ -21,7 +21,7 @@ import {ClearButton} from './modules/clearButton'; import {Help} from './modules/help'; import {AlternateRows} from './modules/alternateRows'; import {NoResults} from './modules/noResults'; -import {State} from './modules/state'; +import {Stateful} from './modules/stateful'; let global = window, doc = global.document; @@ -503,10 +503,10 @@ export class TableFilter { } if(this.isStateful){ - if(!Mod.state){ - Mod.state = new State(tf); + if(!Mod.stateful){ + Mod.stateful = new Stateful(tf); } - Mod.state.init(); + Mod.stateful.init(); } if(this.hasPersistence){ diff --git a/static/templates/pagination.html b/static/templates/pagination.html index 38f71cea..93f37e1d 100644 --- a/static/templates/pagination.html +++ b/static/templates/pagination.html @@ -30,11 +30,12 @@