salix/modules/order/front/catalog/index.js

378 lines
9.1 KiB
JavaScript
Raw Normal View History

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._tagGroups = [];
2018-10-31 13:56:54 +00:00
// Static autocomplete data
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
];
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
];
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() {
this.getData().then(() => {
if (this.order && this.order.isConfirmed)
this.$state.go('order.card.line');
});
}
getData() {
return this.$http.get(`Orders/${this.$params.id}`)
.then(res => this.order = res.data);
2020-01-30 12:53:14 +00:00
}
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-15 08:48:34 +00:00
if (this.$params.tagGroups)
this.tagGroups = JSON.parse(this.$params.tagGroups);
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-09-16 07:54:04 +00:00
this.fetchResultTags(value);
this.buildOrderFilter();
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-15 12:14:13 +00:00
if (this.tagGroups.length > 0)
this.applyFilters();
2020-01-30 12:53:14 +00:00
if (value)
this.updateItemTypes();
}
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-15 12:14:13 +00:00
if (value || this.tagGroups.length > 0)
2020-09-08 12:19:21 +00:00
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();
}
2018-10-31 13:56:54 +00:00
/**
* Get order way ASC/DESC
*/
get orderWay() {
return this._orderWay;
2018-07-24 07:35:23 +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-15 12:14:13 +00:00
if (this.typeId || this.tagGroups.length > 0)
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);
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;
this.$.searchbar.doSearch({}, 'bar');
2020-03-25 12:44:51 +00:00
}
removeItemName() {
this.itemName = null;
this.$.searchbar.doSearch({}, 'bar');
2020-03-25 12:44:51 +00:00
}
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
}
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);
}
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();
}
}
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-15 08:48:34 +00:00
params.tagGroups = undefined;
2020-09-15 12:14:13 +00:00
if (this.tagGroups && this.tagGroups.length)
params.tagGroups = JSON.stringify(this.sanitizedTagGroupParam());
2020-02-19 10:57:39 +00:00
2020-09-15 12:14:13 +00:00
this.$state.go(this.$state.current.name, params);
}
sanitizedTagGroupParam() {
const tagGroups = [];
for (let tagGroup of this.tagGroups) {
const tagParam = {values: []};
2020-09-15 08:48:34 +00:00
2020-09-15 12:14:13 +00:00
for (let tagValue of tagGroup.values)
tagParam.values.push({value: tagValue.value});
2020-09-15 08:48:34 +00:00
2020-09-15 12:14:13 +00:00
if (tagGroup.tagFk)
tagParam.tagFk = tagGroup.tagFk;
2020-02-19 10:57:39 +00:00
2020-09-15 12:14:13 +00:00
if (tagGroup.tagSelection) {
tagParam.tagSelection = {
name: tagGroup.tagSelection.name
};
2020-02-19 10:57:39 +00:00
}
2020-09-15 12:14:13 +00:00
tagGroups.push(tagParam);
2020-09-14 05:42:43 +00:00
}
2020-02-19 10:57:39 +00:00
2020-09-15 12:14:13 +00:00
return tagGroups;
}
2020-02-11 13:41:59 +00:00
2020-09-16 07:54:04 +00:00
fetchResultTags(items) {
const resultTags = [];
for (let item of items) {
for (let itemTag of item.tags) {
const alreadyAdded = resultTags.findIndex(tag => {
return tag.tagFk == itemTag.tagFk;
2020-02-11 13:41:59 +00:00
});
if (alreadyAdded == -1)
2020-09-16 07:54:04 +00:00
resultTags.push({...itemTag, priority: 1});
else
resultTags[alreadyAdded].priority += 1;
}
}
this.resultTags = resultTags;
2020-02-11 13:41:59 +00:00
}
2020-09-16 07:54:04 +00:00
buildOrderFilter() {
const filter = [].concat(this.defaultOrderFields);
for (let tag of this.resultTags)
filter.push({...tag, field: tag.id, isTag: true});
2020-02-11 13:41:59 +00:00
2020-09-16 07:54:04 +00:00
this.orderFields = filter;
2020-02-11 13:41:59 +00:00
}
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
}
ngModule.vnComponent('vnOrderCatalog', {
2018-07-24 07:35:23 +00:00
template: require('./index.html'),
controller: Controller
2018-07-24 07:35:23 +00:00
});