2018-07-24 07:35:23 +00:00
|
|
|
import ngModule from '../module';
|
2020-03-25 08:20:26 +00:00
|
|
|
import Section from 'salix/components/section';
|
2018-09-14 07:10:30 +00:00
|
|
|
import './style.scss';
|
2018-07-24 07:35:23 +00:00
|
|
|
|
2020-03-25 08:20:26 +00:00
|
|
|
class Controller extends Section {
|
2020-03-17 14:18:02 +00:00
|
|
|
constructor($element, $) {
|
2020-03-25 08:20:26 +00:00
|
|
|
super($element, $);
|
2020-01-30 12:53:14 +00:00
|
|
|
this.itemTypes = [];
|
2020-09-08 12:19:21 +00:00
|
|
|
// this._tags = [];
|
|
|
|
this._tagGroups = [];
|
2018-10-31 13:56:54 +00:00
|
|
|
|
|
|
|
// Static autocomplete data
|
2019-10-28 16:31:33 +00:00
|
|
|
this.orderWays = [
|
2018-10-31 13:56:54 +00:00
|
|
|
{way: 'ASC', name: 'Ascendant'},
|
|
|
|
{way: 'DESC', name: 'Descendant'},
|
2018-09-14 07:10:30 +00:00
|
|
|
];
|
2019-10-28 16:31:33 +00:00
|
|
|
this.defaultOrderFields = [
|
2020-09-02 08:06:25 +00:00
|
|
|
{field: 'relevancy DESC, name', name: 'Relevancy', priority: 999},
|
|
|
|
{field: 'showOrder, price', name: 'Color and price', priority: 999},
|
|
|
|
{field: 'name', name: 'Name', priority: 999},
|
|
|
|
{field: 'price', name: 'Price', priority: 999}
|
2018-10-31 13:56:54 +00:00
|
|
|
];
|
2019-10-28 16:31:33 +00:00
|
|
|
this.orderFields = [].concat(this.defaultOrderFields);
|
|
|
|
this._orderWay = this.orderWays[0].way;
|
2020-01-09 10:21:54 +00:00
|
|
|
this.orderField = this.orderFields[0].field;
|
2018-07-24 07:35:23 +00:00
|
|
|
}
|
|
|
|
|
2020-01-30 12:53:14 +00:00
|
|
|
$onChanges() {
|
|
|
|
if (this.order && this.order.isConfirmed)
|
|
|
|
this.$state.go('order.card.line');
|
|
|
|
}
|
|
|
|
|
2018-10-31 13:56:54 +00:00
|
|
|
/**
|
|
|
|
* Fills order autocomplete with tags
|
|
|
|
* obtained from last filtered
|
|
|
|
*/
|
2020-01-30 12:53:14 +00:00
|
|
|
get order() {
|
|
|
|
return this._order;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets filter values from state params
|
|
|
|
*
|
|
|
|
* @param {Object} value - Order data
|
|
|
|
*/
|
|
|
|
set order(value) {
|
|
|
|
this._order = value;
|
|
|
|
|
|
|
|
if (!value) return;
|
|
|
|
|
|
|
|
this.$.$applyAsync(() => {
|
2020-03-25 08:20:26 +00:00
|
|
|
if (this.$params.categoryId)
|
|
|
|
this.categoryId = parseInt(this.$params.categoryId);
|
2020-02-19 10:57:39 +00:00
|
|
|
|
2020-03-25 08:20:26 +00:00
|
|
|
if (this.$params.typeId)
|
|
|
|
this.typeId = parseInt(this.$params.typeId);
|
2020-01-30 12:53:14 +00:00
|
|
|
|
2020-09-08 12:19:21 +00:00
|
|
|
/* if (this.$params.tags)
|
|
|
|
this.tags = JSON.parse(this.$params.tags); */
|
2020-01-30 12:53:14 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
get items() {
|
|
|
|
return this._items;
|
|
|
|
}
|
2018-10-31 13:56:54 +00:00
|
|
|
|
2020-01-30 12:53:14 +00:00
|
|
|
set items(value) {
|
|
|
|
this._items = value;
|
|
|
|
|
|
|
|
if (!value) return;
|
|
|
|
|
2020-02-11 13:41:59 +00:00
|
|
|
this.buildTagsFilter(value);
|
|
|
|
this.buildOrderFilter(value);
|
2018-07-24 07:35:23 +00:00
|
|
|
}
|
|
|
|
|
2020-01-30 12:53:14 +00:00
|
|
|
get categoryId() {
|
|
|
|
return this._categoryId;
|
|
|
|
}
|
|
|
|
|
2020-02-06 12:42:28 +00:00
|
|
|
set categoryId(value = null) {
|
2020-01-30 12:53:14 +00:00
|
|
|
this._categoryId = value;
|
|
|
|
this.itemTypes = [];
|
|
|
|
this.typeId = null;
|
|
|
|
|
|
|
|
this.updateStateParams();
|
|
|
|
|
2020-09-08 12:19:21 +00:00
|
|
|
/* if (this.tags.length > 0)
|
|
|
|
this.applyFilters(); */
|
2020-01-30 12:53:14 +00:00
|
|
|
|
|
|
|
if (value)
|
|
|
|
this.updateItemTypes();
|
|
|
|
}
|
|
|
|
|
2020-02-06 11:46:44 +00:00
|
|
|
changeCategory(id) {
|
|
|
|
if (this._categoryId == id) id = null;
|
|
|
|
this.categoryId = id;
|
|
|
|
}
|
|
|
|
|
2020-01-30 12:53:14 +00:00
|
|
|
get typeId() {
|
|
|
|
return this._typeId;
|
|
|
|
}
|
|
|
|
|
|
|
|
set typeId(value) {
|
|
|
|
this._typeId = value;
|
|
|
|
|
|
|
|
this.updateStateParams();
|
|
|
|
|
2020-09-08 12:19:21 +00:00
|
|
|
/* if (value || this.tags.length > 0)
|
|
|
|
this.applyFilters(); */
|
|
|
|
if (value)
|
|
|
|
this.applyFilters();
|
|
|
|
}
|
|
|
|
|
|
|
|
get tagGroups() {
|
|
|
|
return this._tagGroups;
|
|
|
|
}
|
|
|
|
|
|
|
|
set tagGroups(value) {
|
|
|
|
this._tagGroups = value;
|
|
|
|
|
|
|
|
this.updateStateParams();
|
|
|
|
|
|
|
|
if (value.length)
|
2020-01-30 12:53:14 +00:00
|
|
|
this.applyFilters();
|
|
|
|
}
|
|
|
|
|
2020-09-08 12:19:21 +00:00
|
|
|
/* get tags() {
|
2020-02-19 10:57:39 +00:00
|
|
|
return this._tags;
|
|
|
|
}
|
|
|
|
|
|
|
|
set tags(value) {
|
|
|
|
this._tags = value;
|
|
|
|
|
|
|
|
this.updateStateParams();
|
|
|
|
|
|
|
|
if (value.length)
|
|
|
|
this.applyFilters();
|
2020-09-08 12:19:21 +00:00
|
|
|
} */
|
2020-02-19 10:57:39 +00:00
|
|
|
|
2018-10-31 13:56:54 +00:00
|
|
|
/**
|
|
|
|
* Get order way ASC/DESC
|
|
|
|
*/
|
2019-10-28 16:31:33 +00:00
|
|
|
get orderWay() {
|
|
|
|
return this._orderWay;
|
2018-07-24 07:35:23 +00:00
|
|
|
}
|
|
|
|
|
2019-10-28 16:31:33 +00:00
|
|
|
set orderWay(value) {
|
|
|
|
this._orderWay = value;
|
2019-04-24 07:21:24 +00:00
|
|
|
if (value) this.applyOrder();
|
2018-10-31 13:56:54 +00:00
|
|
|
}
|
|
|
|
|
2020-01-30 12:53:14 +00:00
|
|
|
/**
|
|
|
|
* Returns the order way selection
|
|
|
|
*/
|
2020-01-09 10:21:54 +00:00
|
|
|
get orderSelection() {
|
|
|
|
return this._orderSelection;
|
2018-10-31 13:56:54 +00:00
|
|
|
}
|
|
|
|
|
2020-01-09 10:21:54 +00:00
|
|
|
set orderSelection(value) {
|
|
|
|
this._orderSelection = value;
|
|
|
|
|
2019-04-24 07:21:24 +00:00
|
|
|
if (value) this.applyOrder();
|
2018-10-31 13:56:54 +00:00
|
|
|
}
|
|
|
|
|
2020-01-30 12:53:14 +00:00
|
|
|
/**
|
|
|
|
* Apply order to model
|
|
|
|
*/
|
|
|
|
applyOrder() {
|
2020-09-08 12:19:21 +00:00
|
|
|
/* if (this.typeId || this.tags.length > 0)
|
|
|
|
this.$.model.addFilter(null, {orderBy: this.getOrderBy()}); */
|
|
|
|
if (this.typeId)
|
2020-01-30 12:53:14 +00:00
|
|
|
this.$.model.addFilter(null, {orderBy: this.getOrderBy()});
|
|
|
|
}
|
|
|
|
|
2018-10-31 13:56:54 +00:00
|
|
|
/**
|
|
|
|
* Returns order param
|
|
|
|
*
|
|
|
|
* @return {Object} - Order param
|
|
|
|
*/
|
|
|
|
getOrderBy() {
|
2020-01-09 10:21:54 +00:00
|
|
|
const isTag = !!(this.orderSelection && this.orderSelection.isTag);
|
2019-10-28 16:31:33 +00:00
|
|
|
return {
|
|
|
|
field: this.orderField,
|
|
|
|
way: this.orderWay,
|
2020-01-09 10:21:54 +00:00
|
|
|
isTag: isTag
|
2018-10-31 13:56:54 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-30 12:53:14 +00:00
|
|
|
* Refreshes item type dropdown data
|
2018-10-31 13:56:54 +00:00
|
|
|
*/
|
2020-01-30 12:53:14 +00:00
|
|
|
updateItemTypes() {
|
|
|
|
let params = {
|
|
|
|
itemCategoryId: this.categoryId
|
|
|
|
};
|
|
|
|
|
|
|
|
const query = `Orders/${this.order.id}/getItemTypeAvailable`;
|
|
|
|
this.$http.get(query, {params}).then(res =>
|
|
|
|
this.itemTypes = res.data);
|
|
|
|
}
|
|
|
|
|
2020-02-19 10:57:39 +00:00
|
|
|
/**
|
|
|
|
* Search by tag value
|
|
|
|
* @param {object} event
|
|
|
|
*/
|
2020-01-30 12:53:14 +00:00
|
|
|
onSearchByTag(event) {
|
2020-02-19 10:57:39 +00:00
|
|
|
const value = this.$.search.value;
|
|
|
|
if (event.key !== 'Enter' || !value) return;
|
2020-09-14 05:42:43 +00:00
|
|
|
this.tagGroups.push({values: [{value: value}]});
|
2020-01-30 12:53:14 +00:00
|
|
|
this.$.search.value = null;
|
2020-02-19 10:57:39 +00:00
|
|
|
this.updateStateParams();
|
2020-01-30 12:53:14 +00:00
|
|
|
this.applyFilters();
|
|
|
|
}
|
|
|
|
|
|
|
|
remove(index) {
|
2020-09-14 05:42:43 +00:00
|
|
|
this.tagGroups.splice(index, 1);
|
2020-02-19 10:57:39 +00:00
|
|
|
this.updateStateParams();
|
2020-01-30 12:53:14 +00:00
|
|
|
|
2020-09-14 05:42:43 +00:00
|
|
|
if (this.tagGroups.length >= 0 || this.itemId || this.typeId)
|
2020-01-30 12:53:14 +00:00
|
|
|
this.applyFilters();
|
|
|
|
}
|
|
|
|
|
2020-03-25 12:44:51 +00:00
|
|
|
removeItemId() {
|
|
|
|
this.itemId = null;
|
2020-05-21 12:18:24 +00:00
|
|
|
this.$.searchbar.doSearch({}, 'bar');
|
2020-03-25 12:44:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
removeItemName() {
|
|
|
|
this.itemName = null;
|
|
|
|
this.applyFilters();
|
|
|
|
}
|
|
|
|
|
|
|
|
applyFilters(filter = {}) {
|
2020-01-30 12:53:14 +00:00
|
|
|
let newParams = {};
|
2020-03-25 12:44:51 +00:00
|
|
|
let newFilter = Object.assign({}, filter);
|
2020-01-30 12:53:14 +00:00
|
|
|
const model = this.$.model;
|
|
|
|
|
|
|
|
if (this.categoryId)
|
|
|
|
newFilter.categoryFk = this.categoryId;
|
|
|
|
|
|
|
|
if (this.typeId)
|
|
|
|
newFilter.typeFk = this.typeId;
|
2020-09-14 05:42:43 +00:00
|
|
|
|
2020-01-30 12:53:14 +00:00
|
|
|
newParams = {
|
2020-03-25 08:20:26 +00:00
|
|
|
orderFk: this.$params.id,
|
2020-01-30 12:53:14 +00:00
|
|
|
orderBy: this.getOrderBy(),
|
2020-09-08 12:19:21 +00:00
|
|
|
tagGroups: this.tagGroups,
|
2020-01-30 12:53:14 +00:00
|
|
|
};
|
|
|
|
|
2020-03-25 12:44:51 +00:00
|
|
|
return model.applyFilter({where: newFilter}, newParams);
|
2018-07-24 07:35:23 +00:00
|
|
|
}
|
2018-08-20 07:01:38 +00:00
|
|
|
|
2020-01-30 12:53:14 +00:00
|
|
|
openPanel(event) {
|
|
|
|
if (event.defaultPrevented) return;
|
2018-08-21 11:38:16 +00:00
|
|
|
event.preventDefault();
|
2020-01-30 12:53:14 +00:00
|
|
|
|
|
|
|
this.panelFilter = {};
|
|
|
|
this.$.popover.show(this.$.search.element);
|
2018-08-20 07:01:38 +00:00
|
|
|
}
|
|
|
|
|
2020-01-30 12:53:14 +00:00
|
|
|
onPanelSubmit(filter) {
|
2020-09-08 12:19:21 +00:00
|
|
|
this.$.popover.hide();
|
2020-09-14 05:42:43 +00:00
|
|
|
const values = filter.values;
|
|
|
|
const nonEmptyValues = values.filter(tagValue => {
|
|
|
|
return tagValue.value;
|
|
|
|
});
|
|
|
|
|
|
|
|
filter.values = nonEmptyValues;
|
|
|
|
|
|
|
|
if (filter.tagFk && nonEmptyValues.length) {
|
|
|
|
this.tagGroups.push(filter);
|
2020-09-08 12:19:21 +00:00
|
|
|
this.updateStateParams();
|
|
|
|
this.applyFilters();
|
|
|
|
}
|
2018-08-20 07:01:38 +00:00
|
|
|
}
|
2018-09-18 07:37:54 +00:00
|
|
|
|
2020-01-30 12:53:14 +00:00
|
|
|
/**
|
|
|
|
* Updates url state params from filter values
|
|
|
|
*/
|
|
|
|
updateStateParams() {
|
|
|
|
const params = {};
|
|
|
|
|
2020-02-19 11:34:33 +00:00
|
|
|
params.categoryId = undefined;
|
2020-01-30 12:53:14 +00:00
|
|
|
if (this.categoryId)
|
|
|
|
params.categoryId = this.categoryId;
|
|
|
|
|
2020-02-19 11:34:33 +00:00
|
|
|
params.typeId = undefined;
|
2020-01-30 12:53:14 +00:00
|
|
|
if (this.typeId)
|
|
|
|
params.typeId = this.typeId;
|
|
|
|
|
2020-09-14 05:42:43 +00:00
|
|
|
params.tagGrops = undefined;
|
|
|
|
if (this.tagGrops.length) {
|
|
|
|
const tagGrops = [];
|
|
|
|
for (let tag of this.tagGrops) {
|
2020-02-19 10:57:39 +00:00
|
|
|
const tagParam = {value: tag.value};
|
|
|
|
|
|
|
|
if (tag.tagSelection) {
|
|
|
|
tagParam.tagFk = tag.tagFk;
|
|
|
|
tagParam.tagSelection = {
|
|
|
|
name: tag.tagSelection.name
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
tags.push(tagParam);
|
|
|
|
}
|
|
|
|
|
|
|
|
params.tags = JSON.stringify(tags);
|
2020-09-14 05:42:43 +00:00
|
|
|
}
|
2020-02-19 10:57:39 +00:00
|
|
|
|
2020-01-30 12:53:14 +00:00
|
|
|
this.$state.go(this.$state.current.name, params);
|
2018-09-18 07:37:54 +00:00
|
|
|
}
|
2020-02-11 13:41:59 +00:00
|
|
|
|
|
|
|
buildTagsFilter(items) {
|
|
|
|
const tagValues = [];
|
|
|
|
items.forEach(item => {
|
|
|
|
item.tags.forEach(itemTag => {
|
|
|
|
const alreadyAdded = tagValues.findIndex(tag => {
|
|
|
|
return tag.value == itemTag.value;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (alreadyAdded == -1)
|
|
|
|
tagValues.push(itemTag);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
this.tagValues = tagValues;
|
|
|
|
}
|
|
|
|
|
|
|
|
buildOrderFilter(items) {
|
|
|
|
const tags = [];
|
|
|
|
items.forEach(item => {
|
|
|
|
item.tags.forEach(itemTag => {
|
|
|
|
const alreadyAdded = tags.findIndex(tag => {
|
|
|
|
return tag.field == itemTag.tagFk;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (alreadyAdded == -1) {
|
|
|
|
tags.push({
|
|
|
|
name: itemTag.name,
|
|
|
|
field: itemTag.tagFk,
|
2020-09-02 08:06:25 +00:00
|
|
|
isTag: true,
|
|
|
|
priority: 1
|
2020-02-11 13:41:59 +00:00
|
|
|
});
|
2020-09-02 08:06:25 +00:00
|
|
|
} else
|
|
|
|
tags[alreadyAdded].priority += 1;
|
2020-02-11 13:41:59 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
let newFilterList = [].concat(this.defaultOrderFields);
|
|
|
|
newFilterList = newFilterList.concat(tags);
|
|
|
|
this.orderFields = newFilterList;
|
|
|
|
}
|
2020-03-25 08:20:26 +00:00
|
|
|
|
|
|
|
onSearch(params) {
|
2020-03-25 12:44:51 +00:00
|
|
|
if (!params) return;
|
2020-05-21 12:22:22 +00:00
|
|
|
|
2020-03-25 12:44:51 +00:00
|
|
|
this.itemId = null;
|
|
|
|
this.itemName = null;
|
|
|
|
|
2020-03-25 08:20:26 +00:00
|
|
|
if (params.search) {
|
|
|
|
if (/^\d+$/.test(params.search)) {
|
|
|
|
this.itemId = params.search;
|
2020-03-25 12:44:51 +00:00
|
|
|
return this.applyFilters({
|
|
|
|
'i.id': params.search
|
|
|
|
});
|
2020-03-25 08:20:26 +00:00
|
|
|
} else {
|
|
|
|
this.itemName = params.search;
|
2020-03-25 12:44:51 +00:00
|
|
|
return this.applyFilters({
|
|
|
|
'i.name': {like: `%${params.search}%`}
|
|
|
|
});
|
2020-03-25 08:20:26 +00:00
|
|
|
}
|
2020-03-25 12:44:51 +00:00
|
|
|
} else return this.applyFilters();
|
2020-03-25 08:20:26 +00:00
|
|
|
}
|
2020-09-14 05:42:43 +00:00
|
|
|
|
|
|
|
formatTooltip(tagGroup) {
|
|
|
|
const tagValues = tagGroup.values;
|
|
|
|
|
|
|
|
let title = '';
|
|
|
|
if (tagGroup.tagFk) {
|
|
|
|
const tagName = tagGroup.tagSelection.name;
|
|
|
|
title += `${tagName}: `;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let [i, tagValue] of tagValues.entries()) {
|
|
|
|
if (i > 0) title += ', ';
|
|
|
|
title += `"${tagValue.value}"`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return `${title}`;
|
|
|
|
}
|
2018-07-24 07:35:23 +00:00
|
|
|
}
|
|
|
|
|
2020-07-24 12:22:30 +00:00
|
|
|
ngModule.vnComponent('vnOrderCatalog', {
|
2018-07-24 07:35:23 +00:00
|
|
|
template: require('./index.html'),
|
|
|
|
controller: Controller,
|
|
|
|
bindings: {
|
2020-01-30 12:53:14 +00:00
|
|
|
order: '<'
|
2019-01-30 22:47:06 +00:00
|
|
|
}
|
2018-07-24 07:35:23 +00:00
|
|
|
});
|