hedera-web/forms/ecomerce/catalog/catalog.js

329 lines
7.0 KiB
JavaScript
Raw Normal View History

2019-05-21 14:16:27 +00:00
Hedera.Catalog = new Class({
2016-09-26 09:28:47 +00:00
Extends: Hedera.Form
2015-09-16 16:11:15 +00:00
,_menuShown: false
2019-05-21 14:16:27 +00:00
,open: function() {
this.close();
2015-12-10 13:48:43 +00:00
this.isOpen = true;
2022-05-30 01:30:33 +00:00
if (!localStorage.getItem('hederaGuest')) {
Hedera.BasketChecker.check(this.conn, this.hash,
2019-05-21 14:16:27 +00:00
this.onBasketCheck.bind(this));
} else {
var query = 'CALL mybasket_configureForGuest';
this.conn.execQuery(query, this.loadUi.bind(this));
}
}
2019-05-21 14:16:27 +00:00
,onBasketCheck: function(isOk) {
2015-12-10 13:48:43 +00:00
if (isOk)
2019-05-21 14:16:27 +00:00
this.loadUi();
2015-12-10 13:48:43 +00:00
}
2019-05-21 14:16:27 +00:00
,activate: function() {
2022-05-28 01:18:06 +00:00
document.body.appendChild(this.$.rightPanel);
2022-05-28 19:27:36 +00:00
this.$.items.setInfo('i', 'item', 'vn', ['id']);
2015-12-10 13:48:43 +00:00
2019-05-21 14:16:27 +00:00
if (localStorage.getItem('hederaView'))
this.setView(parseInt(localStorage.getItem('hederaView')));
2015-12-10 13:48:43 +00:00
else
2019-05-21 14:16:27 +00:00
this.setView(Hedera.Catalog.View.GRID);
2022-05-28 19:31:43 +00:00
2022-06-06 08:53:59 +00:00
this.onFilterChange();
2015-12-10 13:48:43 +00:00
}
2019-05-21 14:16:27 +00:00
,deactivate: function() {
this.hideMenu();
2022-05-28 01:18:06 +00:00
this.gui.$.topBar.style.backgroundColor = '';
Vn.Node.remove(this.$.rightPanel);
}
2022-05-28 19:27:36 +00:00
2022-06-06 08:53:59 +00:00
,getFilter(params, tags, currentTag) {
if (params.search == null && params.type == null)
return null;
2022-05-28 19:27:36 +00:00
const $ = this.$;
2022-06-06 08:53:59 +00:00
params = Object.assign({}, params);
const filter = new Sql.Operation({
type: Sql.Operation.Type.AND
});
let idSearch = false;
if (params.search != null) {
idSearch = /^\d+$/.test(params.search);
2022-05-28 19:27:36 +00:00
if (!idSearch) {
2022-06-06 08:53:59 +00:00
filter.push($.searchOp);
params.search = `%${params.search}%`;
} else
filter.push($.idOp);
}
2022-05-28 19:27:36 +00:00
2022-06-06 08:53:59 +00:00
if (!idSearch) {
if (params.realm != null)
filter.push($.realmOp);
if (params.type != null)
filter.push($.typeOp);
for (const tag of tags)
if (tag != currentTag && params[tag] != null)
filter.push($[`${tag}Op`]);
}
params.filter = filter;
const lot = new Vn.Lot();
lot.setAll(params);
return lot;
2022-05-28 19:27:36 +00:00
}
2022-06-06 08:53:59 +00:00
,onFilterChange: function() {
const $ = this.$;
const params = $.params.$;
2022-05-28 19:27:36 +00:00
this.refreshTitle();
2022-06-06 08:53:59 +00:00
const hasRealm = params.realm != null;
$.filters.style.display = hasRealm ? 'block' : 'none';
$.realmMsg.style.display = hasRealm ? 'none' : 'block';
const tags = [
'color',
'origin',
'category',
'producer'
];
const lot = this.getFilter(params, tags);
if (lot) {
$.items.lot = lot;
$.items.refresh();
} else
$.items.clean();
for (const tag of tags)
2022-06-06 16:02:17 +00:00
$[`${tag}s`].lot = this.getFilter(params, tags, tag);
2022-06-07 08:19:29 +00:00
if (lot) this.hideMenu();
2022-05-28 19:27:36 +00:00
}
,refreshTitle: function() {
2022-06-06 08:53:59 +00:00
const hash = this.hash.$;
2022-05-28 19:27:36 +00:00
const types = this.$.types;
const realms = this.$.realms;
2022-06-06 08:53:59 +00:00
const type = hash.type;
const realm = hash.realm;
2022-05-28 19:27:36 +00:00
let typeName;
let realmName;
if (type && types.ready) {
var row = types.search('id', type);
if (row != -1) typeName = types.get(row, 'name');
}
if (realm && realms.ready) {
var row = realms.search('id', realm);
if (row != -1) realmName = realms.get(row, 'name');
}
let title = _('Catalog');
let subtitle = '';
if (typeName) {
title = typeName;
subtitle = realmName;
} else if (realmName) {
title = realmName;
}
Vn.Node.setText(this.$.titleText, title);
Vn.Node.setText(this.$.subtitle, subtitle);
}
2016-04-26 10:54:12 +00:00
2019-05-21 14:16:27 +00:00
,setView: function(view) {
if (view === Hedera.Catalog.View.GRID) {
2022-05-28 01:18:06 +00:00
this.$.viewButton.setProperties({
2022-05-25 18:04:16 +00:00
icon: 'view_list',
tip: _('List view')
});
2016-09-26 09:28:47 +00:00
this.view = Hedera.Catalog.View.GRID;
var className = 'grid-view';
2019-05-21 14:16:27 +00:00
} else {
2022-05-28 01:18:06 +00:00
this.$.viewButton.setProperties({
2022-05-25 18:04:16 +00:00
icon: 'grid_on',
tip: _('Grid view')
});
2016-09-26 09:28:47 +00:00
this.view = Hedera.Catalog.View.LIST;
var className = 'list-view';
2015-09-22 07:20:47 +00:00
}
2022-05-28 01:18:06 +00:00
var node = this.$.gridView.node;
node.className = className;
2019-05-21 14:16:27 +00:00
localStorage.setItem('hederaView', this.view);
2015-12-10 13:48:43 +00:00
}
2019-05-21 14:16:27 +00:00
,onSwitchViewClick: function() {
this.setView(this.view === Hedera.Catalog.View.LIST ?
2016-09-26 09:28:47 +00:00
Hedera.Catalog.View.GRID : Hedera.Catalog.View.LIST);
}
2015-02-08 15:38:38 +00:00
2019-05-21 14:16:27 +00:00
,onItemsChange: function(model, status) {
if (status !== Db.Model.Status.CLEAN)
2022-05-28 01:18:06 +00:00
this.$.order.style.display = 'block';
else
2022-05-28 01:18:06 +00:00
this.$.order.style.display = 'none';
}
2019-05-21 14:16:27 +00:00
,onOrderChange: function(e) {
var value = e.target.value;
2019-05-21 14:16:27 +00:00
var sortField = value.substr(2);
var sortWay = value.charAt(0) === 'A' ?
Db.Model.SortWay.ASC : Db.Model.SortWay.DESC;
if (sortField)
2022-05-28 19:27:36 +00:00
this.$.items.sortByName(sortField, sortWay);
2019-05-21 14:16:27 +00:00
this.hideMenu();
}
2019-05-21 14:16:27 +00:00
,realmRenderer: function(builder, form) {
2022-05-28 01:18:06 +00:00
var link = builder.$.link;
2019-05-21 14:16:27 +00:00
link.href = this.hash.make({
2022-05-28 15:49:46 +00:00
form: this.hash.$.form,
realm: form.$.id
2015-07-23 15:58:48 +00:00
});
2015-02-08 15:38:38 +00:00
}
2019-05-21 14:16:27 +00:00
,onRightPanelClick: function(event) {
event.stopPropagation();
2015-02-08 15:38:38 +00:00
}
2019-05-21 14:16:27 +00:00
,onShowMenuClick: function(event) {
2015-09-16 16:11:15 +00:00
this._menuShown = true;
2019-05-21 14:16:27 +00:00
event.stopPropagation();
this.gui.showBackground();
2022-05-28 01:18:06 +00:00
Vn.Node.addClass(this.$.rightPanel, 'show');
2019-05-21 14:16:27 +00:00
this.hideMenuCallback = this.hideMenu.bind(this);
document.addEventListener('click', this.hideMenuCallback);
2015-02-08 15:38:38 +00:00
}
2019-05-21 14:16:27 +00:00
,hideMenu: function() {
2015-09-16 16:11:15 +00:00
if (!this._menuShown)
return;
2019-05-21 14:16:27 +00:00
this.gui.hideBackground();
2022-05-28 01:18:06 +00:00
Vn.Node.removeClass(this.$.rightPanel, 'show');
2019-05-21 14:16:27 +00:00
document.removeEventListener('click', this.hideMenuCallback);
2015-02-08 15:38:38 +00:00
this.hideMenuCallback = null;
}
2019-05-21 14:16:27 +00:00
,isGuest: function() {
if (localStorage.getItem('hederaGuest')) {
Htk.Toast.showError(_('YouMustBeLoggedIn'));
2016-05-04 14:36:51 +00:00
return true;
}
return false;
}
2019-05-21 14:16:27 +00:00
,onBasketClick: function() {
if (this.isGuest())
2016-05-04 14:36:51 +00:00
return;
2022-05-30 01:30:33 +00:00
this.hash.setAll({form: 'ecomerce/basket'});
}
2016-05-04 14:36:51 +00:00
2019-05-21 14:16:27 +00:00
,onConfigureClick: function() {
if (this.isGuest())
2016-05-04 14:36:51 +00:00
return;
2015-11-19 13:57:23 +00:00
2022-05-30 01:30:33 +00:00
this.hash.setAll({form: 'ecomerce/checkout'});
2015-11-19 13:57:23 +00:00
}
2022-05-28 00:37:24 +00:00
,onAddItemClick: function(event, form) {
if (event.defaultPrevented) return;
event.preventDefault();
if (this.isGuest()) return;
2019-05-21 14:16:27 +00:00
this.onEraseClick();
2022-06-18 21:04:34 +00:00
this.$.$card.row = form.row;
2022-06-06 16:19:43 +00:00
this.$.cardLot.assign({item: form.$.id});
2022-05-28 01:18:06 +00:00
this.$.cardPopup.show(event.currentTarget);
2015-07-07 15:27:47 +00:00
}
2019-05-21 14:16:27 +00:00
,onAddLotClick: function(column, value, row) {
2022-05-28 01:18:06 +00:00
var model = this.$.itemLots;
2019-05-21 14:16:27 +00:00
var grouping = model.get(row, 'grouping');
var warehouse = model.get(row, 'warehouseFk');
var available = model.get(row, 'available');
2015-07-21 14:16:07 +00:00
var lotAmount = this.items[warehouse];
2015-07-21 14:16:07 +00:00
if (lotAmount === undefined)
lotAmount = 0;
2019-05-21 14:16:27 +00:00
if (lotAmount < available) {
var newAmount = lotAmount + grouping;
if (newAmount > available)
newAmount = available;
this.items[warehouse] = newAmount;
2022-05-28 01:18:06 +00:00
this.$.amount.value += newAmount - lotAmount;
2019-05-21 14:16:27 +00:00
} else
Htk.Toast.showError(_('NoMoreAmountAvailable'));
2015-07-07 15:27:47 +00:00
}
2019-05-21 14:16:27 +00:00
,onConfirmClick: function() {
2015-07-21 14:16:07 +00:00
var sql = '';
2019-05-21 14:16:27 +00:00
var query = new Sql.String({query: 'CALL myBasket_addItem(#warehouse, #item, #amount);'});
var amountSum = 0;
2015-07-21 14:16:07 +00:00
2019-05-21 14:16:27 +00:00
for (var warehouse in this.items) {
var amount = this.items[warehouse];
amountSum += amount;
2022-05-30 01:30:33 +00:00
const params = {
warehouse: warehouse,
2022-06-06 16:19:43 +00:00
item: this.$.cardLot.$.item,
2022-05-30 01:30:33 +00:00
amount: amount
}
sql += query.render(params);
2015-07-21 14:16:07 +00:00
}
2015-07-07 15:27:47 +00:00
2019-05-21 14:16:27 +00:00
if (amountSum > 0) {
this.conn.execQuery(sql);
2015-07-07 15:27:47 +00:00
2022-06-18 21:04:34 +00:00
var itemName = this.$.$card.get('item');
2019-05-21 14:16:27 +00:00
Htk.Toast.showMessage(
2022-06-06 16:19:43 +00:00
Vn.Value.sprintf(_('Added%dOf%s'), amountSum, itemName));
}
2022-05-28 01:18:06 +00:00
this.$.cardPopup.hide();
}
2015-07-21 14:16:07 +00:00
2019-05-21 14:16:27 +00:00
,onEraseClick: function() {
2022-05-28 01:18:06 +00:00
this.$.amount.value = 0;
2016-05-04 14:36:51 +00:00
this.items = {};
}
2019-05-21 14:16:27 +00:00
,onPopupClose: function() {
this.onEraseClick();
2022-06-18 21:04:34 +00:00
this.$.$card.row = -1;
2022-06-06 08:53:59 +00:00
this.$.cardLot.value = undefined;
}
2015-11-19 13:57:23 +00:00
2019-05-21 14:16:27 +00:00
,onCardLoad: function() {
2022-05-28 01:18:06 +00:00
this.$.cardPopup.reset();
2015-07-21 14:16:07 +00:00
}
});
2019-05-21 14:16:27 +00:00
Hedera.Catalog.extend({
View: {
LIST: 0,
GRID: 1
}
});