From cb29888e4cc2e2d34b416eb797280ad6ce9e94a8 Mon Sep 17 00:00:00 2001 From: Unknown Date: Wed, 7 Feb 2018 14:27:30 +0200 Subject: [PATCH 1/5] serialize, deserialize for autocomplete --- cosmoz-omnitable-column-autocomplete.html | 28 +++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/cosmoz-omnitable-column-autocomplete.html b/cosmoz-omnitable-column-autocomplete.html index eb291e3e..4ff87f74 100644 --- a/cosmoz-omnitable-column-autocomplete.html +++ b/cosmoz-omnitable-column-autocomplete.html @@ -168,6 +168,34 @@ } return true; }); + }, + + _serializeFilter: function (obj = this.filter) { + if (!(Array.isArray(obj) && obj.length > 0)) { + return null; + } + return obj.map(item => { + const property = this.valueProperty; + if (!property) { + return item; + } + return this.get(property, item); + }).join('~'); + }, + + _deserializeFilter: function (obj) { + if (obj == null || obj === '') { + return null; + } + return obj.split('~').map(item => this.values.find(v => { + const property = this.valueProperty; + if (!property) { + const vv = typeof v === 'number' ? v.toString() : v; + const ii = typeof item === 'number' ? item.toString() : item; + return vv === ii; + } + return this.get(property, v) === item; + })); } }); From 2b941ed0cf54396da31a6b0bb738c60fb72041f7 Mon Sep 17 00:00:00 2001 From: Unknown Date: Wed, 7 Feb 2018 14:28:04 +0200 Subject: [PATCH 2/5] tests for autocomplete serialize, deserialize --- test/autocomplete.html | 87 ++++++++++++++++++++++++++++++++++++++++++ test/hash-param.html | 6 +-- test/index.html | 2 +- 3 files changed, 91 insertions(+), 4 deletions(-) create mode 100644 test/autocomplete.html diff --git a/test/autocomplete.html b/test/autocomplete.html new file mode 100644 index 00000000..920b5e02 --- /dev/null +++ b/test/autocomplete.html @@ -0,0 +1,87 @@ + + + + + + Autocomplete tests + + + + + + + + + + + + + + + + + + + diff --git a/test/hash-param.html b/test/hash-param.html index 24f3f7ee..ab6ceb10 100644 --- a/test/hash-param.html +++ b/test/hash-param.html @@ -89,11 +89,11 @@ }); test('updates filter from url hash', function (done) { - location.hash = '#/#test-filter--id=["1"]'; + location.hash = '#/#test-filter--id=1'; instantiate(function () { assert.isArray(omnitable.columns[0].filter); assert.lengthOf(omnitable.columns[0].filter, 1); - assert.include(omnitable.columns[0].filter, '1'); + assert.include(omnitable.columns[0].filter, 1); done(); }); }); @@ -163,7 +163,7 @@ hash = omnitable._routeHash; column.filter = [0, 1]; Polymer.Base.async(function () { - assert.equal(hash['test-filter--id'], '[0,1]'); + assert.equal(hash['test-filter--id'], '0~1'); column.resetFilter(); Polymer.Base.async(function () { assert.equal(hash['test-filter--id'], null); diff --git a/test/index.html b/test/index.html index df409ef7..56d4d71d 100644 --- a/test/index.html +++ b/test/index.html @@ -13,8 +13,8 @@ 'hash-param.html', 'xlsx-export.html', 'range.html', - 'range-date.html', 'fit-dropdowns.html' + 'autocomplete.html' ], suites = [], i; From 56036f7a38d24def72dcbab3c3a31c2442cce6d2 Mon Sep 17 00:00:00 2001 From: Unknown Date: Tue, 13 Feb 2018 13:27:58 +0200 Subject: [PATCH 3/5] Move deserialize part of _routeHashFilterChanged to columns --- cosmoz-omnitable-column-behavior.html | 8 ++++++++ cosmoz-omnitable.js | 8 +------- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/cosmoz-omnitable-column-behavior.html b/cosmoz-omnitable-column-behavior.html index 296570b0..980ff81a 100644 --- a/cosmoz-omnitable-column-behavior.html +++ b/cosmoz-omnitable-column-behavior.html @@ -457,6 +457,14 @@ return this.deserialize(obj, type); }, + setFilterFromHash(value) { + const deserialized = this._deserializeFilter(value); + + if (deserialized === null) { + return this.resetFilter(); + } + this.set('filter', deserialized); + } }; diff --git a/cosmoz-omnitable.js b/cosmoz-omnitable.js index 76784435..fea43db8 100644 --- a/cosmoz-omnitable.js +++ b/cosmoz-omnitable.js @@ -1152,13 +1152,7 @@ return; } - let deserialized = column._deserializeFilter(value); - - if (deserialized === null) { - column.resetFilter(); - return; - } - column.set('filter', deserialized); + column.setFilterFromHash(value); }, _computeRouteHashKeyRule(hashParam) { if (!hashParam) { From 27216a0d812dc7aaffd5bfe82b8c303b815b384e Mon Sep 17 00:00:00 2001 From: Unknown Date: Tue, 13 Feb 2018 13:31:19 +0200 Subject: [PATCH 4/5] Refactor hash serialize/deserialize for autocomplete column --- cosmoz-omnitable-column-autocomplete.html | 62 +++++++++++++++-------- 1 file changed, 42 insertions(+), 20 deletions(-) diff --git a/cosmoz-omnitable-column-autocomplete.html b/cosmoz-omnitable-column-autocomplete.html index 4ff87f74..8090902d 100644 --- a/cosmoz-omnitable-column-autocomplete.html +++ b/cosmoz-omnitable-column-autocomplete.html @@ -92,6 +92,9 @@ notify: true }, }, + observers: [ + '_valuesForHashChanged(values.*)' + ], getComparableValue: function (item, valuePath = this.valuePath) { return this.getString(item, valuePath); @@ -170,33 +173,52 @@ }); }, - _serializeFilter: function (obj = this.filter) { - if (!(Array.isArray(obj) && obj.length > 0)) { + _normalizeValue(value) { + if (value == null) { + return value; + } + + return isNaN(value) ? value : Number(value); + }, + + _serializeFilter(filter = this.filter) { + if (!Array.isArray(filter) || filter.length === 0) { return null; } - return obj.map(item => { - const property = this.valueProperty; - if (!property) { - return item; - } - return this.get(property, item); - }).join('~'); + const property = this.valueProperty; + return filter + .map(item => property ? this.get(property, item) : item) + .join('~'); }, - _deserializeFilter: function (obj) { - if (obj == null || obj === '') { + _deserializeFilter(filterString) { + if (filterString == null || filterString === '') { return null; } - return obj.split('~').map(item => this.values.find(v => { - const property = this.valueProperty; - if (!property) { - const vv = typeof v === 'number' ? v.toString() : v; - const ii = typeof item === 'number' ? item.toString() : item; - return vv === ii; - } - return this.get(property, v) === item; - })); + const property = this.valueProperty; + return filterString + .split('~') + .map(item => this.values + .find(v => this._normalizeValue(property ? this.get(property, v) : v) === this._normalizeValue(item)) + ).filter(i => i != null); + }, + + _valuesForHashChanged() { + if (!Array.isArray(this.values) || this._pendingFilterString) { + return; + } + this.setFilterFromHash(this._pendingFilterString); + this._pendingFilterString = null; + }, + + setFilterFromHash(value) { + if (!Array.isArray(this.values)) { + this._pendingFilterString = value; + return; + } + Cosmoz.OmnitableColumnBehavior.setFilterFromHash.call(this, value); } + }); From 39fc3d8b2781a6e50fa5b09d9dd7c1f99b8c40d7 Mon Sep 17 00:00:00 2001 From: Andrei Alexandru Date: Thu, 1 Mar 2018 12:56:18 +0200 Subject: [PATCH 5/5] fix tests after rebase --- test/index.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/index.html b/test/index.html index 56d4d71d..26546dce 100644 --- a/test/index.html +++ b/test/index.html @@ -13,7 +13,7 @@ 'hash-param.html', 'xlsx-export.html', 'range.html', - 'fit-dropdowns.html' + 'fit-dropdowns.html', 'autocomplete.html' ], suites = [],