salix/front/core/components/searchbar/searchbar.js

262 lines
7.8 KiB
JavaScript
Raw Normal View History

import ngModule from '../../module';
import Component from '../../lib/component';
import './style.scss';
2018-12-27 11:54:16 +00:00
import {buildFilter} from 'vn-loopback/util/filter';
/**
* An input specialized to perform searches, it allows to use a panel
* for advanced searches when the panel property is defined.
* When model and exprBuilder properties are used, the model is updated
* automatically with an and-filter exprexion in which each operand is built
* by calling the exprBuilder function for each non-null parameter.
*
* @property {Object} filter A key-value object with filter parameters
* @property {Function} onSearch Function to call when search is submited
* @property {SearchPanel} panel The panel used for advanced searches
* @property {CrudModel} model The model used for searching
* @property {Function} exprBuilder If defined, is used to build each non-null param expresion
*/
export default class Controller extends Component {
constructor($element, $scope, $compile, $state, $transitions) {
super($element, $scope);
2019-04-12 12:33:08 +00:00
this.$element = $element;
this.$compile = $compile;
this.$state = $state;
2019-09-20 07:35:12 +00:00
this.$ = $scope;
2018-12-19 07:42:07 +00:00
let criteria = {to: this.$state.current.name};
this.deregisterCallback = $transitions.onSuccess(criteria,
() => this.onStateChange());
this._filter = null;
this.autoLoad = false;
}
2018-12-19 07:42:07 +00:00
$postLink() {
if (this.filter === null)
this.onStateChange();
}
2018-12-19 07:42:07 +00:00
set filter(value) {
this._filter = value;
this.$state.go('.', {q: JSON.stringify(value)}, {location: 'replace'});
2018-12-19 07:42:07 +00:00
}
2018-12-19 07:42:07 +00:00
get filter() {
return this._filter;
}
2018-12-19 07:42:07 +00:00
onStateChange() {
this._filter = null;
if (this.$state.params.q) {
try {
this._filter = JSON.parse(this.$state.params.q);
2019-09-20 07:35:12 +00:00
} catch (e) {
console.error(e);
}
2018-12-19 07:42:07 +00:00
}
this.doSearch();
}
2019-08-29 06:32:46 +00:00
get shownFilter() {
return this._filter != null ? this._filter : this.suggestedFilter;
}
openPanel(event) {
if (event.defaultPrevented) return;
2019-02-15 10:03:27 +00:00
this.$panelScope = this.$.$new();
this.$panel = this.$compile(`<${this.panel}/>`)(this.$panelScope);
2019-10-15 14:19:35 +00:00
let panel = this.$panel[0].$ctrl;
2019-08-29 06:32:46 +00:00
if (this.shownFilter)
panel.filter = JSON.parse(JSON.stringify(this.shownFilter));
panel.onSubmit = filter => this.onPanelSubmit(filter);
this.$.popover.parent = this.element;
this.$.popover.child = this.$panel[0];
this.$.popover.show();
}
onPopoverClose() {
2019-02-15 10:03:27 +00:00
this.$panelScope.$destroy();
this.$panel.remove();
this.$panel = null;
}
onPanelSubmit(filter) {
this.$.popover.hide();
2019-01-16 14:29:01 +00:00
filter = compact(filter);
this.filter = filter != null ? filter : {};
2019-01-27 23:26:01 +00:00
let element = this.element.querySelector('vn-textfield input');
element.select();
element.focus();
}
onSubmit() {
this.filter = this.getObjectFromString(this.searchString);
}
doSearch() {
2019-08-29 06:32:46 +00:00
this.searchString = this.getStringFromObject(this.shownFilter);
2018-12-19 07:42:07 +00:00
2019-08-29 06:32:46 +00:00
let filter = this._filter;
2019-01-16 14:29:01 +00:00
if (filter == null && this.autoload)
2018-12-19 07:42:07 +00:00
filter = {};
if (this.onSearch)
2018-12-19 07:42:07 +00:00
this.onSearch({$params: filter});
if (this.model) {
2018-12-19 07:42:07 +00:00
if (filter !== null) {
let where = buildFilter(filter,
(param, value) => this.exprBuilder({param, value}));
let userParams = {};
let hasParams = false;
if (this.paramBuilder) {
for (let param in filter) {
let value = filter[param];
if (value == null) continue;
let expr = this.paramBuilder({param, value});
if (expr) {
Object.assign(userParams, expr);
hasParams = true;
}
2018-09-05 11:01:21 +00:00
}
}
2018-12-19 07:42:07 +00:00
this.model.applyFilter(
where ? {where} : null,
hasParams ? userParams : null
);
} else
this.model.clear();
}
}
2018-12-19 07:42:07 +00:00
exprBuilder(param, value) {
return {[param]: value};
}
/**
* Finds pattern key:value or key:(extra value) and passes it to object.
*
* @param {String} searchString The search string
* @return {Object} The parsed object
*/
getObjectFromString(searchString) {
let result = {};
if (searchString) {
let regex = /((([\w_]+):([\w_]+))|([\w_]+):\(([\w_ ]+)\))/gi;
let findPattern = searchString.match(regex);
let remnantString = searchString.replace(regex, '').trim();
if (findPattern) {
for (let i = 0; i < findPattern.length; i++) {
let aux = findPattern[i].split(':');
let property = aux[0];
let value = aux[1].replace(/\(|\)/g, '');
result[property] = value.trim();
}
}
if (remnantString)
result.search = remnantString;
}
return result;
}
/**
* Passes an object to pattern key:value or key:(extra value).
*
* @param {Object} searchObject The search object
* @return {String} The passed string
*/
getStringFromObject(searchObject) {
let search = [];
if (searchObject) {
let keys = Object.keys(searchObject);
keys.forEach(key => {
if (key == 'search') return;
let value = searchObject[key];
let valueString;
if (typeof value === 'string' && value.indexOf(' ') !== -1)
valueString = `(${value})`;
else if (value instanceof Date)
valueString = value.toJSON();
else {
switch (typeof value) {
case 'number':
case 'string':
case 'boolean':
valueString = `${value}`;
}
}
if (valueString)
search.push(`${key}:${valueString}`);
});
if (searchObject.search)
search.unshift(searchObject.search);
}
return search.length ? search.join(' ') : '';
}
$onDestroy() {
this.deregisterCallback();
}
}
Controller.$inject = ['$element', '$scope', '$compile', '$state', '$transitions'];
ngModule.component('vnSearchbar', {
template: require('./searchbar.html'),
bindings: {
filter: '<?',
2019-08-29 06:32:46 +00:00
suggestedFilter: '<?',
onSearch: '&?',
panel: '@',
model: '<?',
2018-09-05 11:01:21 +00:00
exprBuilder: '&?',
paramBuilder: '&?',
2019-04-12 12:33:08 +00:00
autoLoad: '<?',
info: '@?'
},
controller: Controller
});
2019-01-16 14:29:01 +00:00
/**
* 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;
}