new searchBar

This commit is contained in:
Daniel Herrero 2017-11-16 15:05:45 +01:00
parent 5874c80e85
commit ea35c1feda
7 changed files with 110 additions and 58 deletions

View File

@ -7,7 +7,6 @@
index="index"
on-search="$ctrl.search(index)"
advanced="true"
search="$ctrl.model.search"
popover="vn-client-search-panel">
</vn-searchbar>
</vn-horizontal>

View File

@ -7,7 +7,6 @@ export default class Controller {
this.model = {};
}
search(index) {
index.filter.search = this.model.search;
index.accept();
}
}

View File

@ -1,28 +1,16 @@
import ngModule from '../module';
export default class Controller {
constructor(sessionStorage) {
this.sessionStorage = sessionStorage;
constructor() {
// onSubmit() is defined by @vnSearchbar
this.onSubmit = () => {};
}
onSearch() {
this.setStorageValue();
this.onSubmit(this.filter);
}
$onChanges() {
var value = this.sessionStorage.get('filter');
if (value !== undefined)
this.filter = value;
}
setStorageValue() {
this.sessionStorage.set('filter', this.filter);
}
}
Controller.$inject = ['sessionStorage'];
Controller.$inject = [];
ngModule.component('vnClientSearchPanel', {
template: require('./search-panel.html'),

View File

@ -1,10 +1,10 @@
<form ng-submit="$ctrl.onSubmit()">
<vn-horizontal>
<vn-textfield vn-one label="Search" model="$ctrl.search"></vn-textfield>
<vn-textfield vn-one label="Search" model="$ctrl.stringSearch"></vn-textfield>
<vn-icon
pad-medium-top
ng-if="$ctrl.advanced"
ng-click="$ctrl.onClick($event)"
ng-click="$ctrl.onpenFilters($event)"
icon="keyboard_arrow_down"
style="cursor: pointer;">
</vn-icon>

View File

@ -1,21 +1,66 @@
import ngModule from '../../module';
export default class Controller {
constructor($element, $scope, $document, $compile, vnPopover, sessionStorage, $timeout) {
constructor($element, $scope, $document, $compile, vnPopover, $timeout) {
this.element = $element[0];
this.$scope = $scope;
this.$document = $document;
this.$compile = $compile;
this.vnPopover = vnPopover;
this.sessionStorage = sessionStorage;
this.$timeout = $timeout;
this.stringSearch = '';
}
clearFilter() {
this.index.filter = {};
this.sessionStorage.remove('filter');
this.index.filter = {
page: 1,
size: 20
};
}
// string search to json filter
getFiltersFromSearch() {
let toFind = this.stringSearch;
let find;
if (this.stringSearch) {
// find pattern key:value or key:(extra value) and returns array
find = toFind.match(/((([\w_]+):([\w_]+))|([\w_]+):\(([\w_ ]+)\))/gi);
// remove pattern key:value or key:(extra value) from string
this.index.filter.search = (toFind.replace(/((([\w_]+):([\w_]+))|([\w_]+):\(([\w_ ]+)\))/gi, '')).trim();
if (find)
for (let i = 0; i < find.length; i++) {
let aux = find[i].split(':');
let property = aux[0];
let value = aux[1].replace(/\(|\)/g, '');
this.index.filter[property] = value.trim();
}
}
}
// json filter to string search
createFilterSearch(filter) {
let search = [];
let keys = Object.keys(filter);
if (keys.length) {
keys.forEach(k => {
if (k !== 'page' && k !== 'size' && k !== 'search') {
let value = filter[k];
if (typeof value === 'string' && value.indexOf(' ') !== -1) {
search.push(`${k}:(${value})`);
} else if (typeof value !== 'object') {
search.push(`${k}:${value}`);
}
}
});
}
if (this.index.filter.search) {
search.push(this.index.filter.search);
}
delete this.index.filter.search;
this.stringSearch = (search.length) ? search.join(' ') : '';
}
onpenFilters(event) {
if (this.stringSearch) {
this.getFiltersFromSearch();
}
onClick(event) {
this.search = null;
this.child = this.vnPopover.showComponent(this.popover, this.$scope, this.element);
@ -27,12 +72,21 @@ export default class Controller {
event.preventDefault();
}
onChildSubmit(filter) {
// this.vnPopover.hide();
Object.assign(this.index.filter, filter);
this.createFilterSearch(filter);
this.index.filter = {
page: 1,
size: 20
};
this.$timeout(() => {
this.onSubmit();
});
}
onSubmit() {
if (this.stringSearch) {
this.getFiltersFromSearch();
}
if (this.onSearch)
this.onSearch();
@ -47,13 +101,12 @@ export default class Controller {
this.clearFilter();
}
}
Controller.$inject = ['$element', '$scope', '$document', '$compile', 'vnPopover', 'sessionStorage', '$timeout'];
Controller.$inject = ['$element', '$scope', '$document', '$compile', 'vnPopover', '$timeout'];
ngModule.component('vnSearchbar', {
template: require('./searchbar.html'),
bindings: {
index: '<',
search: '=',
onSearch: '&',
advanced: '=',
popover: '@',

View File

@ -2,26 +2,6 @@ module.exports = function(Client) {
Client.installMethod('filter', filterClients);
function filterClients(params) {
if (params.search)
return searchWhere(params);
return andWhere(params);
}
function searchWhere(params) {
return {
where: {
or: [
{id: params.search},
{name: {regexp: params.search}}
]
},
skip: (params.page - 1) * params.size,
limit: params.size
};
}
function andWhere(params) {
let filters = {
where: {},
skip: (params.page - 1) * params.size,
@ -31,18 +11,51 @@ module.exports = function(Client) {
delete params.page;
delete params.size;
if (params.search) {
hasAnd = true;
filters.where.and = [
{
or: [
{id: params.search},
{name: {regexp: params.search}}
]
}
];
delete params.search;
}
if (params.phone) {
filters.where.or = [
let phones = [
{phone: params.phone},
{mobile: params.phone}
];
delete params.phone;
}
Object.keys(params).forEach(
key => {
filters.where[key] = (key === 'postcode' || key === 'fi') ? params[key] : {regexp: params[key]};
if (filters.where.and) {
filters.where.and.push(
{
or: phones
}
);
} else {
filters.where.or = phones;
}
delete params.phone;
}
let keys = Object.keys(params);
if (keys.length) {
keys.forEach(
key => {
if (filters.where.and) {
let filter = {};
filter[key] = (key === 'postcode' || key === 'fi' || key === 'id') ? params[key] : {regexp: params[key]};
filters.where.and.push(filter);
} else {
filters.where[key] = (key === 'postcode' || key === 'fi' || key === 'id') ? params[key] : {regexp: params[key]};
}
}
);
}
return filters;
}
};