Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve autocomplete serialize, deserialize #149

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions cosmoz-omnitable-column-autocomplete.html
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@
notify: true
},
},
observers: [
'_valuesForHashChanged(values.*)'
],

getComparableValue: function (item, valuePath = this.valuePath) {
return this.getString(item, valuePath);
Expand Down Expand Up @@ -168,7 +171,54 @@
}
return true;
});
},

_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;
}
const property = this.valueProperty;
return filter
.map(item => property ? this.get(property, item) : item)
.join('~');
},

_deserializeFilter(filterString) {
if (filterString == null || filterString === '') {
return null;
}
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_pendingFilterString can only be falsy? Intended? Where does that property even come from? New in this PR?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nomego The problem is that when we set filter (from deserializeFilter) values might not be set if data property is not set so it can't set selection in paper-autocomplete-chips.
Because of that I set the param from url as _pendingFilterString and next time values are set it will update filter.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another solution would be to able to set an array of values in cosmoz-autocomplete-chips.

this._pendingFilterString = null;
},

setFilterFromHash(value) {
if (!Array.isArray(this.values)) {
this._pendingFilterString = value;
return;
}
Cosmoz.OmnitableColumnBehavior.setFilterFromHash.call(this, value);
}

});
</script>
</dom-module>
8 changes: 8 additions & 0 deletions cosmoz-omnitable-column-behavior.html
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};

</script>
8 changes: 1 addition & 7 deletions cosmoz-omnitable.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
87 changes: 87 additions & 0 deletions test/autocomplete.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes">
<title>Autocomplete tests</title>

<script src="../../webcomponentsjs/webcomponents-lite.js"></script>
<script src="../../web-component-tester/browser.js"></script>
<script src="../../test-fixture/test-fixture-mocha.js"></script>

<link rel="import" href="../../test-fixture/test-fixture.html">
<link rel="import" href="../../iron-test-helpers/iron-test-helpers.html">

<link rel="import" href="../cosmoz-omnitable.html">
<link rel="import" href="../cosmoz-omnitable-columns.html">
<link rel="import" href="../demo/table-demo-behavior.html">
</head>
<body>
<test-fixture id="default">
<template>
<cosmoz-omnitable id="omnitable" class="flex" selection-enabled>
<cosmoz-omnitable-column-autocomplete name="elementName" title="name" value-path="element" text-property="name" value-property="id">
</cosmoz-omnitable-column-autocomplete>
</cosmoz-omnitable>
</template>
</test-fixture>
<script>
/*global sinon chai flush */
(function () {
'use strict';

sinon.assert.expose(chai.assert, { prefix: '' });

suite('basic', function () {
var omnitable,
data = [
{ element: { id: 'a', name: 'aa' } },
{ element: { id: 'b', name: 'bb' } },
{ element: { id: 'c', name: 'cc' } },
{ element: { id: 'd', name: 'dd' } },
{ element: { id: 'e', name: 'ee' } },
{ element: { id: 'f', name: 'ff' } },
{ element: { id: 'g', name: 'gg' } }
];

setup(function (done) {
omnitable = fixture('default');
omnitable.data = data;

flush(function () {
Polymer.Base.async(done, 90);
});
});

test('serialize and deserialize one filter', function (done) {
assert.equal(omnitable.is, 'cosmoz-omnitable');
const column = omnitable.columns[0];
column.filter = [{ id: 'c', name: 'cc' }];

const serialized = column._serializeFilter(column.filter);
assert.equal(serialized, 'c', 'Expected valueProperty to be serialized');

const deserialized = column._deserializeFilter(serialized);
assert.deepEqual(deserialized[0], { id: 'c', name: 'cc' });
done();
});

test('serialize and deserialize two filters', function (done) {
assert.equal(omnitable.is, 'cosmoz-omnitable');
const column = omnitable.columns[0];
column.filter = [{ id: 'b', name: 'bb' }, { id: 'g', name: 'gg' }];

const serialized = column._serializeFilter(column.filter);
assert.equal(serialized, 'b~g', 'Expected valueProperty to be serialized');

const deserialized = column._deserializeFilter(serialized);
assert.deepEqual(deserialized[0], { id: 'b', name: 'bb' });
assert.deepEqual(deserialized[1], { id: 'g', name: 'gg' });
done();
});
});
}());

</script>
</body>
</html>
6 changes: 3 additions & 3 deletions test/hash-param.html
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
'hash-param.html',
'xlsx-export.html',
'range.html',
'range-date.html',
'fit-dropdowns.html'
'fit-dropdowns.html',
'autocomplete.html'
],
suites = [],
i;
Expand Down