#938 Searchbar fixes

This commit is contained in:
Juan Ferrer 2019-01-16 15:29:01 +01:00
parent ecfe2c356f
commit 47b7596381
3 changed files with 52 additions and 41 deletions

View File

@ -38,7 +38,7 @@ export default class Controller extends Component {
set filter(value) {
this._filter = value;
this.pushFilterToState(value);
this.$state.go('.', {q: JSON.stringify(value)});
}
get filter() {
@ -75,30 +75,33 @@ export default class Controller extends Component {
this.$panel.scope().$destroy();
this.$panel.remove();
this.$panel = null;
let element = this.element.querySelector('vn-textfield[label=Search] input');
element.select();
element.focus();
}
onPanelSubmit(filter) {
this.$.popover.hide();
for (let param in filter) {
if (filter[param] == null)
delete filter[param];
}
this.filter = filter;
filter = compact(filter);
this.filter = filter != null ? filter : {};
this.focus();
}
onSubmit() {
this.filter = this.getObjectFromString(this.searchString);
this.focus();
}
focus() {
// FIXME: Timeout used because textfield blurs focus when Enter key is pressed
setTimeout(() => {
let element = this.element.querySelector('vn-textfield input');
element.select();
element.focus();
}, 200);
}
doSearch() {
let filter = this._filter;
if (filter === null && this.autoload)
if (filter == null && this.autoload)
filter = {};
this.searchString = this.getStringFromObject(filter);
@ -135,30 +138,6 @@ export default class Controller extends Component {
}
}
pushFilterToState(filter) {
let keys = Object.keys(filter);
let search = {};
if (keys.length) {
keys.forEach(key => {
let value = filter[key];
if (value instanceof Date)
search[key] = value;
else {
switch (typeof value) {
case 'boolean':
case 'number':
case 'string':
search[key] = value;
}
}
});
}
this.$state.go('.', {q: JSON.stringify(search)});
}
exprBuilder(param, value) {
return {[param]: value};
}
@ -249,3 +228,33 @@ ngModule.component('vnSearchbar', {
},
controller: Controller
});
/**
* Removes null, undefined, empty objects and empty arrays from an object.
* It also applies to nested objects/arrays.
*
* @param {*} obj The value to format
* @return {*} The formatted value
*/
function compact(obj) {
if (obj == null)
return undefined;
else if (Array.isArray(obj)) {
for (let i = obj.length - 1; i >= 0; i--) {
if (compact(obj[i]) === undefined)
obj.splice(i, 1);
}
if (obj.length == 0)
return undefined;
} else if (typeof obj == 'object' && obj.constructor == Object) {
let keys = Object.keys(obj);
for (let key of keys) {
if (key.substr(0, 2) == '$$' || compact(obj[key]) === undefined)
delete obj[key];
}
if (Object.keys(obj).length == 0)
return undefined;
}
return obj;
}

View File

@ -3,14 +3,14 @@
url="/client/api/receipts/filter"
params="$ctrl.params"
limit="20"
data="$ctrl.risks"
auto-load="false">
data="$ctrl.risks">
</vn-crud-model>
<vn-crud-model
vn-id="riskModel"
url="/client/api/ClientRisks"
filter="::$ctrl.filter"
data="riskTotal">
data="riskTotal"
auto-load="true">
</vn-crud-model>
<vn-vertical>
<vn-card pad-large>

View File

@ -3,8 +3,10 @@ import SearchPanel from 'core/components/searchbar/search-panel';
class Controller extends SearchPanel {
set filter(value) {
if (!value || !value.tags)
value = {tags: [{}]};
if (!value)
value = {};
if (!value.tags)
value.tags = [{}];
this.$.filter = value;
}