From 9156bdbb9d10f228d2c1395c01f0dcdc9bdb4f14 Mon Sep 17 00:00:00 2001 From: Max Guglielmi Date: Wed, 13 Apr 2016 18:28:12 +0200 Subject: [PATCH] Finalised sort persistence --- 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 | 146 ++++++++++++------ dist/tablefilter/tf-1.js | 20 ++- src/extensions/sort/adapterSortabletable.js | 6 + src/modules/state.js | 132 ++++++++++------ 10 files changed, 208 insertions(+), 108 deletions(-) diff --git a/dist/tablefilter/style/colsVisibility.css b/dist/tablefilter/style/colsVisibility.css index 42e6387a..fc0b3836 100644 --- a/dist/tablefilter/style/colsVisibility.css +++ b/dist/tablefilter/style/colsVisibility.css @@ -1,6 +1,6 @@ /** * tablefilter v0.2.11 by Max Guglielmi - * build date: 2016-04-12T13:40:06.860Z + * build date: 2016-04-13T14:24:11.741Z * 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 31403131..2aeac36b 100644 --- a/dist/tablefilter/style/filtersVisibility.css +++ b/dist/tablefilter/style/filtersVisibility.css @@ -1,6 +1,6 @@ /** * tablefilter v0.2.11 by Max Guglielmi - * build date: 2016-04-12T13:40:06.860Z + * build date: 2016-04-13T14:24:11.741Z * 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 5e363c42..e7fc0c83 100644 --- a/dist/tablefilter/style/tablefilter.css +++ b/dist/tablefilter/style/tablefilter.css @@ -1,6 +1,6 @@ /** * tablefilter v0.2.11 by Max Guglielmi - * build date: 2016-04-12T13:40:06.860Z + * build date: 2016-04-13T14:24:11.741Z * 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 f6d22e1b..098424a9 100644 --- a/dist/tablefilter/style/themes/default/default.css +++ b/dist/tablefilter/style/themes/default/default.css @@ -1,6 +1,6 @@ /** * tablefilter v0.2.11 by Max Guglielmi - * build date: 2016-04-12T13:40:06.860Z + * build date: 2016-04-13T14:24:11.741Z * 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 2c04365f..ff3e98c1 100644 --- a/dist/tablefilter/style/themes/mytheme/mytheme.css +++ b/dist/tablefilter/style/themes/mytheme/mytheme.css @@ -1,6 +1,6 @@ /** * tablefilter v0.2.11 by Max Guglielmi - * build date: 2016-04-12T13:40:06.860Z + * build date: 2016-04-13T14:24:11.741Z * 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 73c8a2c0..4a71dec0 100644 --- a/dist/tablefilter/style/themes/skyblue/skyblue.css +++ b/dist/tablefilter/style/themes/skyblue/skyblue.css @@ -1,6 +1,6 @@ /** * tablefilter v0.2.11 by Max Guglielmi - * build date: 2016-04-12T13:40:06.860Z + * build date: 2016-04-13T14:24:11.741Z * 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 cb65a4b8..8d97dd98 100644 --- a/dist/tablefilter/tablefilter.js +++ b/dist/tablefilter/tablefilter.js @@ -7614,6 +7614,7 @@ return /******/ (function(modules) { // webpackBootstrap _this.persistSort = Boolean(cfg.sort); _this.cookieDuration = !isNaN(cfg.cookie_duration) ? parseInt(cfg.cookie_duration, 10) : 87600; + _this.enableStorage = _this.enableLocalStorage || _this.enableCookie; _this.hash = null; _this.pageNb = null; _this.pageLength = null; @@ -7650,12 +7651,15 @@ return /******/ (function(modules) { // webpackBootstrap this.emitter.on(['column-sorted'], function (tf, index, descending) { return _this2.updateSort(index, descending); }); + this.emitter.on(['sort-initialized'], function () { + return _this2._syncSort(); + }); if (this.enableHash) { this.hash = new _hash.Hash(this); this.hash.init(); } - if (this.enableLocalStorage || this.enableCookie) { + if (this.enableStorage) { this.storage = new _storage.Storage(this); this.storage.init(); } @@ -7673,6 +7677,7 @@ return /******/ (function(modules) { // webpackBootstrap if (!this.isEnabled()) { return; } + var state = this.state; var tf = this.tf; if (this.persistFilters) { @@ -7682,47 +7687,48 @@ return /******/ (function(modules) { // webpackBootstrap var key = '' + _this3.prfxCol + idx; if (_types2.default.isString(val) && _string2.default.isEmpty(val)) { - if (_this3.state.hasOwnProperty(key)) { - _this3.state[key] = undefined; + if (state.hasOwnProperty(key)) { + state[key] = undefined; } } else { - _this3.state[key] = _this3.state[key] || {}; - _this3.state[key].flt = val; + state[key] = state[key] || {}; + state[key].flt = val; } }); } if (this.persistPageNumber) { if (_types2.default.isNull(this.pageNb)) { - this.state[this.pageNbKey] = undefined; + state[this.pageNbKey] = undefined; } else { - this.state[this.pageNbKey] = this.pageNb; + state[this.pageNbKey] = this.pageNb; } } if (this.persistPageLength) { if (_types2.default.isNull(this.pageLength)) { - this.state[this.pageLengthKey] = undefined; + state[this.pageLengthKey] = undefined; } else { - this.state[this.pageLengthKey] = this.pageLength; + state[this.pageLengthKey] = this.pageLength; } } if (this.persistSort) { if (!_types2.default.isNull(this.sort)) { - // Remove any sorted column - Object.keys(this.state).forEach(function (key) { - if (key.indexOf(_this3.prfxCol) !== -1 && _this3.state[key]) { - _this3.state[key].sort = undefined; + // Remove previuosly sorted column + Object.keys(state).forEach(function (key) { + if (key.indexOf(_this3.prfxCol) !== -1 && state[key]) { + state[key].sort = undefined; } }); + var key = '' + this.prfxCol + this.sort.column; - this.state[key] = this.state[key] || {}; - this.state[key].sort = { descending: this.sort.descending }; + state[key] = state[key] || {}; + state[key].sort = { descending: this.sort.descending }; } } - this.emitter.emit('state-changed', tf, this.state); + this.emitter.emit('state-changed', tf, state); }; /** @@ -7749,6 +7755,14 @@ return /******/ (function(modules) { // webpackBootstrap this.update(); }; + /** + * Refresh column sorting information on sort change + * + * @param index {Number} Column index + * @param descending {Boolean} Descending manner + */ + + State.prototype.updateSort = function updateSort(index, descending) { this.sort = { column: index, @@ -7769,48 +7783,27 @@ return /******/ (function(modules) { // webpackBootstrap }; /** - * Apply current features state + * Sync stored features state */ State.prototype.sync = function sync() { - var _this4 = this; - var state = this.state; 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, ''), 10); - if (_this4.persistFilters) { - var val = state[key].flt; - tf.setFilterValue(colIdx, val); - } - if (_this4.persistSort) { - if (state[key].hasOwnProperty('sort')) {} - var _val = state[key].sort; - var sort = tf.extension('sort'); - sort.sortByColumnIndex(colIdx, _val.descending); - } - } - }); - - if (this.persistFilters) { - tf.filter(); - } + this._syncFilters(); if (this.persistPageNumber) { var pageNumber = state[this.pageNbKey]; - this.emitter.emit('change-page', this.tf, pageNumber); + this.emitter.emit('change-page', tf, pageNumber); } if (this.persistPageLength) { var pageLength = state[this.pageLengthKey]; - this.emitter.emit('change-page-results', this.tf, pageLength); + this.emitter.emit('change-page-results', tf, pageLength); } - if (this.persistSort) {} + this._syncSort(); }; /** @@ -7832,13 +7825,67 @@ return /******/ (function(modules) { // webpackBootstrap this.enable(); }; + /** + * Sync filters with stored values and filter table + * + * @private + */ + + + State.prototype._syncFilters = function _syncFilters() { + var _this4 = this; + + if (!this.persistFilters) { + return; + } + var state = this.state; + var tf = this.tf; + + Object.keys(state).forEach(function (key) { + if (key.indexOf(_this4.prfxCol) !== -1) { + var colIdx = parseInt(key.replace(_this4.prfxCol, ''), 10); + var val = state[key].flt; + tf.setFilterValue(colIdx, val); + } + }); + + tf.filter(); + }; + + /** + * Sync sorted column with stored sorting information and sort table + * + * @private + */ + + + State.prototype._syncSort = function _syncSort() { + var _this5 = this; + + if (!this.persistSort) { + return; + } + var state = this.state; + var tf = this.tf; + + Object.keys(state).forEach(function (key) { + if (key.indexOf(_this5.prfxCol) !== -1) { + var colIdx = parseInt(key.replace(_this5.prfxCol, ''), 10); + if (!_types2.default.isUndef(state[key].sort)) { + var sort = state[key].sort; + _this5.emitter.emit('sort', tf, colIdx, sort.descending); + } + } + }); + }; + /** * Destroy State instance */ State.prototype.destroy = function destroy() { - var _this5 = this; + var _this6 = this; if (!this.initialized) { return; @@ -7847,16 +7894,19 @@ return /******/ (function(modules) { // webpackBootstrap this.state = {}; this.emitter.off(['after-filtering'], function () { - return _this5.update(); + return _this6.update(); }); this.emitter.off(['after-page-change', 'after-clearing-filters'], function (tf, pageNb) { - return _this5.updatePage(pageNb); + return _this6.updatePage(pageNb); }); this.emitter.off(['after-page-length-change'], function (tf, index) { - return _this5.updatePageLength(index); + return _this6.updatePageLength(index); }); this.emitter.off(['column-sorted'], function (tf, index, descending) { - return _this5.updateSort(index, descending); + return _this6.updateSort(index, descending); + }); + this.emitter.off(['sort-initialized'], function () { + return _this6._syncSort(); }); if (this.enableHash) { @@ -7864,7 +7914,7 @@ return /******/ (function(modules) { // webpackBootstrap this.hash = null; } - if (this.enableLocalStorage || this.enableCookie) { + if (this.enableStorage) { this.storage.destroy(); this.storage = null; } diff --git a/dist/tablefilter/tf-1.js b/dist/tablefilter/tf-1.js index 3081c701..2662948c 100644 --- a/dist/tablefilter/tf-1.js +++ b/dist/tablefilter/tf-1.js @@ -1793,6 +1793,8 @@ webpackJsonp([1],{ } AdapterSortableTable.prototype.init = function init() { + var _this = this; + var tf = this.tf; var adpt = this; @@ -1845,7 +1847,12 @@ webpackJsonp([1],{ adpt.emitter.emit('column-sorted', tf, adpt.stt.sortColumn, adpt.stt.descending); }; + this.emitter.on(['sort'], function (tf, colIdx, desc) { + return _this.sortByColumnIndex(colIdx, desc); + }); + this.initialized = true; + this.emitter.emit('sort-initialized', tf, this); }; /** @@ -2019,7 +2026,7 @@ webpackJsonp([1],{ }; AdapterSortableTable.prototype.setSortTypes = function setSortTypes() { - var _this = this; + var _this2 = this; var tf = this.tf, sortTypes = this.sortTypes, @@ -2066,7 +2073,7 @@ webpackJsonp([1],{ /*** external table headers adapter ***/ if (this.asyncSort && this.triggerIds.length > 0) { (function () { - var triggers = _this.triggerIds; + var triggers = _this2.triggerIds; for (var j = 0; j < triggers.length; j++) { if (triggers[j] === null) { continue; @@ -2077,10 +2084,10 @@ webpackJsonp([1],{ _event2.default.add(trigger, 'click', function (evt) { var elm = evt.target; - if (!_this.tf.sort) { + if (!_this2.tf.sort) { return; } - _this.stt.asyncSort(triggers.indexOf(elm.id)); + _this2.stt.asyncSort(triggers.indexOf(elm.id)); }); trigger.setAttribute('_sortType', _sortTypes[j]); } @@ -2095,7 +2102,12 @@ webpackJsonp([1],{ AdapterSortableTable.prototype.destroy = function destroy() { + var _this3 = this; + var tf = this.tf; + this.emitter.off(['sort'], function (tf, colIdx, desc) { + return _this3.sortByColumnIndex(colIdx, desc); + }); this.sorted = false; this.initialized = false; this.stt.destroy(); diff --git a/src/extensions/sort/adapterSortabletable.js b/src/extensions/sort/adapterSortabletable.js index 36570593..cb33be6a 100644 --- a/src/extensions/sort/adapterSortabletable.js +++ b/src/extensions/sort/adapterSortabletable.js @@ -104,7 +104,11 @@ export default class AdapterSortableTable{ adpt.stt.descending); }; + this.emitter.on(['sort'], + (tf, colIdx, desc)=> this.sortByColumnIndex(colIdx, desc)); + this.initialized = true; + this.emitter.emit('sort-initialized', tf, this); } /** @@ -350,6 +354,8 @@ export default class AdapterSortableTable{ */ destroy(){ let tf = this.tf; + this.emitter.off(['sort'], + (tf, colIdx, desc)=> this.sortByColumnIndex(colIdx, desc)); this.sorted = false; this.initialized = false; this.stt.destroy(); diff --git a/src/modules/state.js b/src/modules/state.js index 35d3c129..4b90ed58 100644 --- a/src/modules/state.js +++ b/src/modules/state.js @@ -37,6 +37,7 @@ export class State extends Feature { this.cookieDuration = !isNaN(cfg.cookie_duration) ? parseInt(cfg.cookie_duration, 10) : 87600; + this.enableStorage = this.enableLocalStorage || this.enableCookie; this.hash = null; this.pageNb = null; this.pageLength = null; @@ -63,12 +64,13 @@ export class State extends Feature { (tf, pageLength) => this.updatePageLength(pageLength)); this.emitter.on(['column-sorted'], (tf, index, descending) => this.updateSort(index, descending)); + this.emitter.on(['sort-initialized'], () => this._syncSort()); if (this.enableHash) { this.hash = new Hash(this); this.hash.init(); } - if (this.enableLocalStorage || this.enableCookie) { + if (this.enableStorage) { this.storage = new Storage(this); this.storage.init(); } @@ -83,6 +85,7 @@ export class State extends Feature { if (!this.isEnabled()) { return; } + let state = this.state; let tf = this.tf; if (this.persistFilters) { @@ -92,49 +95,48 @@ export class State extends Feature { let key = `${this.prfxCol}${idx}`; if (Types.isString(val) && Str.isEmpty(val)) { - if (this.state.hasOwnProperty(key)) { - this.state[key] = undefined; + if (state.hasOwnProperty(key)) { + state[key] = undefined; } } else { - this.state[key] = this.state[key] || {}; - this.state[key].flt = val; + state[key] = state[key] || {}; + state[key].flt = val; } - }); } if (this.persistPageNumber) { if (Types.isNull(this.pageNb)) { - this.state[this.pageNbKey] = undefined; + state[this.pageNbKey] = undefined; } else { - this.state[this.pageNbKey] = this.pageNb; + state[this.pageNbKey] = this.pageNb; } } if (this.persistPageLength) { if (Types.isNull(this.pageLength)) { - this.state[this.pageLengthKey] = undefined; + state[this.pageLengthKey] = undefined; } else { - this.state[this.pageLengthKey] = this.pageLength; + state[this.pageLengthKey] = this.pageLength; } } - if(this.persistSort){ - if(!Types.isNull(this.sort)) { - // Remove any sorted column - Object.keys(this.state).forEach((key) => { - if (key.indexOf(this.prfxCol) !== -1 && - this.state[key]) { - this.state[key].sort = undefined; + if (this.persistSort) { + if (!Types.isNull(this.sort)) { + // Remove previuosly sorted column + Object.keys(state).forEach((key) => { + if (key.indexOf(this.prfxCol) !== -1 && state[key]) { + state[key].sort = undefined; } }); + let key = `${this.prfxCol}${this.sort.column}`; - this.state[key] = this.state[key] || {}; - this.state[key].sort = { descending: this.sort.descending }; + state[key] = state[key] || {}; + state[key].sort = { descending: this.sort.descending }; } } - this.emitter.emit('state-changed', tf, this.state); + this.emitter.emit('state-changed', tf, state); } /** @@ -157,7 +159,13 @@ export class State extends Feature { this.update(); } - updateSort(index, descending){ + /** + * Refresh column sorting information on sort change + * + * @param index {Number} Column index + * @param descending {Boolean} Descending manner + */ + updateSort(index, descending) { this.sort = { column: index, descending: descending @@ -175,48 +183,25 @@ export class State extends Feature { } /** - * Apply current features state + * Sync stored features state */ sync() { let state = this.state; 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); - if (this.persistFilters) { - let val = state[key].flt; - tf.setFilterValue(colIdx, val); - } - if(this.persistSort){ - if(state[key].hasOwnProperty('sort')){ - - } - let val = state[key].sort; - let sort = tf.extension('sort'); - sort.sortByColumnIndex(colIdx, val.descending); - } - } - }); - - if (this.persistFilters) { - tf.filter(); - } + this._syncFilters(); if (this.persistPageNumber) { let pageNumber = state[this.pageNbKey]; - this.emitter.emit('change-page', this.tf, pageNumber); + this.emitter.emit('change-page', tf, pageNumber); } if (this.persistPageLength) { let pageLength = state[this.pageLengthKey]; - this.emitter.emit('change-page-results', this.tf, pageLength); + this.emitter.emit('change-page-results', tf, pageLength); } - if(this.persistSort){ - - } + this._syncSort(); } /** @@ -236,6 +221,52 @@ export class State extends Feature { this.enable(); } + /** + * Sync filters with stored values and filter table + * + * @private + */ + _syncFilters() { + if (!this.persistFilters) { + 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); + 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); + if (!Types.isUndef(state[key].sort)) { + let sort = state[key].sort; + this.emitter.emit('sort', tf, colIdx, sort.descending); + } + } + }); + } + /** * Destroy State instance */ @@ -253,13 +284,14 @@ export class State extends Feature { (tf, index) => this.updatePageLength(index)); this.emitter.off(['column-sorted'], (tf, index, descending) => this.updateSort(index, descending)); + this.emitter.off(['sort-initialized'], () => this._syncSort()); if (this.enableHash) { this.hash.destroy(); this.hash = null; } - if (this.enableLocalStorage || this.enableCookie) { + if (this.enableStorage) { this.storage.destroy(); this.storage = null; }