2018-06-07 21:47:19 +00:00
|
|
|
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';
|
2018-06-07 21:47:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
this.$compile = $compile;
|
|
|
|
this.$state = $state;
|
|
|
|
|
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;
|
2018-06-07 21:47:19 +00:00
|
|
|
this.searchString = '';
|
2018-11-21 13:30:32 +00:00
|
|
|
this.autoLoad = false;
|
2018-06-07 21:47:19 +00:00
|
|
|
}
|
|
|
|
|
2018-12-19 07:42:07 +00:00
|
|
|
$postLink() {
|
|
|
|
if (this.filter === null)
|
|
|
|
this.onStateChange();
|
|
|
|
}
|
2018-06-07 21:47:19 +00:00
|
|
|
|
2018-12-19 07:42:07 +00:00
|
|
|
set filter(value) {
|
|
|
|
this._filter = value;
|
2019-01-16 14:29:01 +00:00
|
|
|
this.$state.go('.', {q: JSON.stringify(value)});
|
2018-12-19 07:42:07 +00:00
|
|
|
}
|
2018-11-21 13:30:32 +00:00
|
|
|
|
2018-12-19 07:42:07 +00:00
|
|
|
get filter() {
|
|
|
|
return this._filter;
|
2018-06-07 21:47:19 +00:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
} catch (e) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.doSearch();
|
2018-06-07 21:47:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
openPanel(event) {
|
|
|
|
if (event.defaultPrevented) return;
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
this.$panel = this.$compile(`<${this.panel}/>`)(this.$.$new());
|
|
|
|
let panel = this.$panel.isolateScope().$ctrl;
|
2018-12-19 07:42:07 +00:00
|
|
|
panel.filter = this._filter;
|
2018-06-07 21:47:19 +00:00
|
|
|
panel.onSubmit = filter => this.onPanelSubmit(filter);
|
|
|
|
|
|
|
|
this.$.popover.parent = this.element;
|
|
|
|
this.$.popover.child = this.$panel[0];
|
|
|
|
this.$.popover.show();
|
|
|
|
}
|
|
|
|
|
|
|
|
onPopoverClose() {
|
|
|
|
this.$panel.scope().$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 : {};
|
|
|
|
this.focus();
|
2018-06-07 21:47:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
onSubmit() {
|
|
|
|
this.filter = this.getObjectFromString(this.searchString);
|
2019-01-16 14:29:01 +00:00
|
|
|
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);
|
2018-06-07 21:47:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
doSearch() {
|
2018-12-19 07:42:07 +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 = {};
|
|
|
|
|
|
|
|
this.searchString = this.getStringFromObject(filter);
|
2018-06-07 21:47:19 +00:00
|
|
|
|
|
|
|
if (this.onSearch)
|
2018-12-19 07:42:07 +00:00
|
|
|
this.onSearch({$params: filter});
|
2018-06-07 21:47:19 +00:00
|
|
|
|
|
|
|
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-06-07 21:47:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-19 07:42:07 +00:00
|
|
|
exprBuilder(param, value) {
|
|
|
|
return {[param]: value};
|
2018-06-07 21:47:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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();
|
2018-11-21 13:30:32 +00:00
|
|
|
if (findPattern) {
|
2018-06-07 21:47:19 +00:00
|
|
|
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();
|
|
|
|
}
|
2018-11-21 13:30:32 +00:00
|
|
|
}
|
2018-06-07 21:47:19 +00:00
|
|
|
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();
|
2018-11-21 13:30:32 +00:00
|
|
|
else {
|
2018-06-07 21:47:19 +00:00
|
|
|
switch (typeof value) {
|
|
|
|
case 'number':
|
|
|
|
case 'string':
|
|
|
|
case 'boolean':
|
|
|
|
valueString = `${value}`;
|
|
|
|
}
|
2018-11-21 13:30:32 +00:00
|
|
|
}
|
2018-06-07 21:47:19 +00:00
|
|
|
|
|
|
|
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: '<?',
|
2018-10-30 12:58:02 +00:00
|
|
|
onSearch: '&?',
|
2018-06-07 21:47:19 +00:00
|
|
|
panel: '@',
|
|
|
|
model: '<?',
|
2018-09-05 11:01:21 +00:00
|
|
|
exprBuilder: '&?',
|
2018-11-21 13:30:32 +00:00
|
|
|
paramBuilder: '&?',
|
|
|
|
autoLoad: '<?'
|
2018-06-07 21:47:19 +00:00
|
|
|
},
|
|
|
|
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;
|
|
|
|
}
|