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

354 lines
8.3 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-02-19 10:57:39 +00:00
this._tags = [];
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 = [
{field: 'relevancy DESC, name', name: 'Relevancy'},
2020-02-11 13:41:59 +00:00
{field: 'showOrder, price', name: 'Color and price'},
2019-02-19 14:13:27 +00:00
{field: 'name', name: 'Name'},
2018-10-31 13:56:54 +00:00
{field: 'price', name: 'Price'}
];
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-03-25 08:20:26 +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();
if (this.tags.length > 0)
this.applyFilters();
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();
if (value || this.tags.length > 0)
this.applyFilters();
}
2020-02-19 10:57:39 +00:00
get tags() {
return this._tags;
}
set tags(value) {
this._tags = value;
this.updateStateParams();
if (value.length)
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() {
if (this.typeId || this.tags.length > 0)
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-01-30 12:53:14 +00:00
this.tags.push({
2020-02-19 10:57:39 +00:00
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) {
this.tags.splice(index, 1);
2020-02-19 10:57:39 +00:00
this.updateStateParams();
2020-01-30 12:53:14 +00:00
if (this.tags.length >= 0 || this.itemId || this.typeId)
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.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;
newParams = {
2020-03-25 08:20:26 +00:00
orderFk: this.$params.id,
2020-01-30 12:53:14 +00:00
orderBy: this.getOrderBy(),
tags: this.tags,
};
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) {
this.$.popover.hide();
this.tags.push(filter);
2020-02-19 10:57:39 +00:00
this.updateStateParams();
2020-01-30 12:53:14 +00:00
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-02-19 11:34:33 +00:00
params.tags = undefined;
2020-02-19 10:57:39 +00:00
if (this.tags.length) {
const tags = [];
for (let tag of this.tags) {
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-02-19 11:34:33 +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);
}
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,
isTag: true
});
}
});
});
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
}
2018-07-24 07:35:23 +00:00
}
2018-08-21 11:38:16 +00:00
ngModule.component('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
});