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

295 lines
7.0 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 = [];
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(() => {
if (this.$stateParams.categoryId)
this.categoryId = this.$stateParams.categoryId;
if (this.$stateParams.typeId)
this.typeId = this.$stateParams.typeId;
});
}
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();
}
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);
}
onSearchById(event) {
const hasValue = this.tags.length > 0 || this.itemId || this.typeId;
if (event.key === 'Enter' && hasValue)
this.applyFilters();
}
onSearchByTag(event) {
if (event.key !== 'Enter' || !this.value) return;
this.tags.push({
value: this.value,
});
this.$.search.value = null;
this.applyFilters();
}
remove(index) {
this.tags.splice(index, 1);
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);
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;
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
});