Skip to content

Commit

Permalink
Refactor hash serialize/deserialize for autocomplete column
Browse files Browse the repository at this point in the history
  • Loading branch information
megheaiulian authored and programmer4web committed Mar 1, 2018
1 parent ec29268 commit 3bba09d
Showing 1 changed file with 42 additions and 20 deletions.
62 changes: 42 additions & 20 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 @@ -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);
}

});
</script>
</dom-module>

0 comments on commit 3bba09d

Please sign in to comment.