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

329 lines
7.0 KiB
JavaScript

Hedera.Catalog = new Class({
Extends: Hedera.Form
,_menuShown: false
,open: function() {
this.close();
this.isOpen = true;
if (!localStorage.getItem('hederaGuest')) {
Hedera.BasketChecker.check(this.conn, this.hash,
this.onBasketCheck.bind(this));
} else {
var query = 'CALL mybasket_configureForGuest';
this.conn.execQuery(query, this.loadUi.bind(this));
}
}
,onBasketCheck: function(isOk) {
if (isOk)
this.loadUi();
}
,activate: function() {
document.body.appendChild(this.$.rightPanel);
this.$.items.setInfo('i', 'item', 'vn', ['id']);
if (localStorage.getItem('hederaView'))
this.setView(parseInt(localStorage.getItem('hederaView')));
else
this.setView(Hedera.Catalog.View.GRID);
this.onFilterChange();
}
,deactivate: function() {
this.hideMenu();
this.gui.$.topBar.style.backgroundColor = '';
Vn.Node.remove(this.$.rightPanel);
}
,getFilter(params, tags, currentTag) {
if (params.search == null && params.type == null)
return null;
const $ = this.$;
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);
if (!idSearch) {
filter.push($.searchOp);
params.search = `%${params.search}%`;
} else
filter.push($.idOp);
}
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;
}
,onFilterChange: function() {
const $ = this.$;
const params = $.params.$;
this.refreshTitle();
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)
$[`${tag}s`].lot = this.getFilter(params, tags, tag);
if (lot) this.hideMenu();
}
,refreshTitle: function() {
const hash = this.hash.$;
const types = this.$.types;
const realms = this.$.realms;
const type = hash.type;
const realm = hash.realm;
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);
}
,setView: function(view) {
if (view === Hedera.Catalog.View.GRID) {
this.$.viewButton.setProperties({
icon: 'view_list',
tip: _('List view')
});
this.view = Hedera.Catalog.View.GRID;
var className = 'grid-view';
} else {
this.$.viewButton.setProperties({
icon: 'grid_on',
tip: _('Grid view')
});
this.view = Hedera.Catalog.View.LIST;
var className = 'list-view';
}
var node = this.$.gridView.node;
node.className = className;
localStorage.setItem('hederaView', this.view);
}
,onSwitchViewClick: function() {
this.setView(this.view === Hedera.Catalog.View.LIST ?
Hedera.Catalog.View.GRID : Hedera.Catalog.View.LIST);
}
,onItemsChange: function(model, status) {
if (status !== Db.Model.Status.CLEAN)
this.$.order.style.display = 'block';
else
this.$.order.style.display = 'none';
}
,onOrderChange: function(e) {
var value = e.target.value;
var sortField = value.substr(2);
var sortWay = value.charAt(0) === 'A' ?
Db.Model.SortWay.ASC : Db.Model.SortWay.DESC;
if (sortField)
this.$.items.sortByName(sortField, sortWay);
this.hideMenu();
}
,realmRenderer: function(builder, form) {
var link = builder.$.link;
link.href = this.hash.make({
form: this.hash.$.form,
realm: form.$.id
});
}
,onRightPanelClick: function(event) {
event.stopPropagation();
}
,onShowMenuClick: function(event) {
this._menuShown = true;
event.stopPropagation();
this.gui.showBackground();
Vn.Node.addClass(this.$.rightPanel, 'show');
this.hideMenuCallback = this.hideMenu.bind(this);
document.addEventListener('click', this.hideMenuCallback);
}
,hideMenu: function() {
if (!this._menuShown)
return;
this.gui.hideBackground();
Vn.Node.removeClass(this.$.rightPanel, 'show');
document.removeEventListener('click', this.hideMenuCallback);
this.hideMenuCallback = null;
}
,isGuest: function() {
if (localStorage.getItem('hederaGuest')) {
Htk.Toast.showError(_('YouMustBeLoggedIn'));
return true;
}
return false;
}
,onBasketClick: function() {
if (this.isGuest())
return;
this.hash.setAll({form: 'ecomerce/basket'});
}
,onConfigureClick: function() {
if (this.isGuest())
return;
this.hash.setAll({form: 'ecomerce/checkout'});
}
,onAddItemClick: function(event, form) {
if (event.defaultPrevented) return;
event.preventDefault();
if (this.isGuest()) return;
this.onEraseClick();
this.$.$card.row = form.row;
this.$.cardLot.assign({item: form.$.id});
this.$.cardPopup.show(event.currentTarget);
}
,onAddLotClick: function(column, value, row) {
var model = this.$.itemLots;
var grouping = model.get(row, 'grouping');
var warehouse = model.get(row, 'warehouseFk');
var available = model.get(row, 'available');
var lotAmount = this.items[warehouse];
if (lotAmount === undefined)
lotAmount = 0;
if (lotAmount < available) {
var newAmount = lotAmount + grouping;
if (newAmount > available)
newAmount = available;
this.items[warehouse] = newAmount;
this.$.amount.value += newAmount - lotAmount;
} else
Htk.Toast.showError(_('NoMoreAmountAvailable'));
}
,onConfirmClick: function() {
var sql = '';
var query = new Sql.String({query: 'CALL myBasket_addItem(#warehouse, #item, #amount);'});
var amountSum = 0;
for (var warehouse in this.items) {
var amount = this.items[warehouse];
amountSum += amount;
const params = {
warehouse: warehouse,
item: this.$.cardLot.$.item,
amount: amount
}
sql += query.render(params);
}
if (amountSum > 0) {
this.conn.execQuery(sql);
var itemName = this.$.$card.get('item');
Htk.Toast.showMessage(
Vn.Value.sprintf(_('Added%dOf%s'), amountSum, itemName));
}
this.$.cardPopup.hide();
}
,onEraseClick: function() {
this.$.amount.value = 0;
this.items = {};
}
,onPopupClose: function() {
this.onEraseClick();
this.$.$card.row = -1;
this.$.cardLot.value = undefined;
}
,onCardLoad: function() {
this.$.cardPopup.reset();
}
});
Hedera.Catalog.extend({
View: {
LIST: 0,
GRID: 1
}
});