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

361 lines
8.5 KiB
JavaScript
Raw Normal View History

2018-07-24 07:35:23 +00:00
import ngModule from '../module';
2018-09-14 07:10:30 +00:00
import './style.scss';
2018-07-24 07:35:23 +00:00
class Controller {
2020-01-30 12:53:14 +00:00
constructor($http, $scope, $state, $compile, $transitions) {
this.$http = $http;
this.$ = $scope;
this.$state = $state;
this.$stateParams = $state.params;
2020-01-30 12:53:14 +00:00
this.$compile = $compile;
this.$transitions = $transitions;
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-02-19 10:57:39 +00:00
if (this.$stateParams.itemId)
this.itemId = parseInt(this.$stateParams.itemId);
2020-01-30 12:53:14 +00:00
if (this.$stateParams.categoryId)
2020-02-19 10:57:39 +00:00
this.categoryId = parseInt(this.$stateParams.categoryId);
2020-01-30 12:53:14 +00:00
if (this.$stateParams.typeId)
2020-02-19 10:57:39 +00:00
this.typeId = parseInt(this.$stateParams.typeId);
if (this.$stateParams.tags)
this.tags = JSON.parse(this.$stateParams.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 itemId() {
return this._itemId;
}
set itemId(value) {
this._itemId = value;
this.updateStateParams();
this.applyFilters();
}
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 item id filter
* @param {object} event
*/
2020-01-30 12:53:14 +00:00
onSearchById(event) {
2020-02-19 10:57:39 +00:00
const value = this.$.itemId.value;
if (event.key === 'Enter' && value) {
this.itemId = value;
this.$.itemId.value = null;
}
2020-01-30 12:53:14 +00:00
}
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();
}
applyFilters() {
let newParams = {};
let newFilter = {};
const model = this.$.model;
if (this.categoryId)
newFilter.categoryFk = this.categoryId;
if (this.typeId)
newFilter.typeFk = this.typeId;
if (this.itemId)
newFilter = {'i.id': this.itemId};
newParams = {
orderFk: this.order.id,
orderBy: this.getOrderBy(),
tags: this.tags,
};
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 = {};
if (this.categoryId)
params.categoryId = this.categoryId;
else params.categoryId = undefined;
if (this.typeId)
params.typeId = this.typeId;
else params.typeId = undefined;
2020-02-19 10:57:39 +00:00
if (this.itemId)
params.itemId = this.itemId;
else params.itemId = undefined;
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);
} else params.tags = undefined;
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;
}
2018-07-24 07:35:23 +00:00
}
2020-01-30 12:53:14 +00:00
Controller.$inject = ['$http', '$scope', '$state', '$compile', '$transitions'];
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
});