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.onRealmChange(); } ,deactivate: function() { this.hideMenu(); this.gui.$.topBar.style.backgroundColor = ''; Vn.Node.remove(this.$.rightPanel); } ,onFilterChange: function() { const $ = this.$; const hash = this.hash; if (hash.search != null || hash.type != null) { const filter = new Sql.Operation({ type: Sql.Operation.Type.AND }); let idSearch = false; if (hash.search != null) { idSearch = /^\d+$/.test(hash.search); filter.push(idSearch ? $.idOp : $.nameOp); } if (!idSearch) { if (hash.realm != null) filter.push($.realmOp); if (hash.type != null) filter.push($.typeOp); const tags = [ 'color', 'origin', 'category', 'producer' ]; for (const tag of tags) if (hash[tag] != null) filter.push($[`${tag}Op`]); } const lot = new Vn.Lot(); lot.set('filter', filter); $.items.lot = lot; } else $.items.lot = null; } ,onRealmChange: function() { const hasRealm = this.$.realm.value != null; this.$.filters.style.display = hasRealm ? 'block' : 'none'; this.$.realmMsg.style.display = hasRealm ? 'none' : 'block'; this.refreshTitle(); } ,refreshTitle: function() { const types = this.$.types; const realms = this.$.realms; const type = this.$.type.value; const realm = this.$.realm.value; 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); } ,onBasketReady: function(form) { if (form.$.method != 'PICKUP') Vn.Node.setText(this.$.method, _('Agency')); else Vn.Node.setText(this.$.method, _('Warehouse')); } ,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.$.cardItem.value = 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.$.cardItem.value, amount: amount } sql += query.render(params); } if (amountSum > 0) { this.conn.execQuery(sql); var itemName = this.$.card.get('item'); Htk.Toast.showMessage( 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.$.cardItem.value = undefined; } ,onCardLoad: function() { this.$.cardPopup.reset(); } }); Hedera.Catalog.extend({ View: { LIST: 0, GRID: 1 } }); Vn.Filter = new Class({ Extends: Htk.Field ,Tag: 'vn-filter' ,Child: 'model' ,Properties: { model: { type: Db.Model ,set: function(x) { x.lot = this._lot; this._model = x; this._select.model = x; } ,get: function() { return this._model; } }, placeholder: { type: String ,set: function(x) { this._select.placeholder = x; this._placeholder = x; } ,get: function() { return this._placeholder; } }, filter: { type: Sql.Filter ,set: function(x) { this._filter = x; this._lot.set('filter', x); } ,get: function() { return this._filter; } } } ,_valueColumnIndex: 0 ,_showColumnIndex: 1 ,initialize: function(props) { var node = this.createRoot('div'); node.className = 'vn-filter'; this._select = new Htk.Select(); this._select.on('mousedown', this._onMouseDown, this); this._select.on('changed', this._onChange, this); this._select.on('ready', this._onReady, this); this.node.appendChild(this._select.node); this._ul = this.createElement('ul'); this.node.appendChild(this._ul); this._lot = new Vn.Lot(); this.parent(props); } ,_onMouseDown: function() { if (this._model && this._model.status === Db.Model.Status.CLEAN) this._model.refresh(); } ,_onCloseClick: function() { this._removeSelectionNode(); this._changeValue(undefined); } ,_removeSelectionNode: function() { if (this._lastLi) { Vn.Node.remove(this._lastLi); this._lastLi = null; this._label = null; } } ,_onChange: function() { if (this._select.value === null || this._select.value === undefined) return; this._realSetValue(this._select.value); } ,_onReady: function() { if (this._emptyLabel) this._refreshLabel(); } ,_changeValue: function(newValue) { this._lot.block(); this.value = newValue; this._lot.unblock(); } ,_onTimeout: function() { this._select.value = null; } ,putValue: function(value) { this._onMouseDown(); this._realSetValue(value); } ,_realSetValue: function(value) { this._removeSelectionNode(); if (value === null || value === undefined) return; var li = this._lastLi = this.createElement('li'); this._ul.appendChild(li); var button = this.createElement('button'); button.addEventListener('click', this._onCloseClick.bind(this, li)); li.appendChild(button); var icon = new Htk.Icon({ name: 'close', alt: _('Close') }); button.appendChild(icon.node); var text = this._label = this.createTextNode(''); li.appendChild(text); setTimeout(this._onTimeout.bind(this)); this._changeValue(value); this._refreshLabel(); } ,_refreshLabel: function() { if (!this._label) return; let row = -1; const model = this._model; let showValue = ''; this._emptyLabel = true; if (model) { if (model.ready) { row = model.searchByIndex(this._valueColumnIndex, this._value); if (row != -1) { var label = model.getByIndex(row, this._showColumnIndex); showValue = label; this._emptyLabel = false; } } else showValue = _('Loading...'); } this._label.nodeValue = showValue; } });