Vn.Catalog = new Class ({ Extends: Vn.Module ,open: function () { if (this.basketChecked) this.parent (); } ,activate: function () { if (!Vn.Url.getQuery ('guest')) { Vn.BasketChecker.check (this.conn, this.onBasketCheck.bind (this)); } else { var query = 'CALL basket_configure_for_guest ()'; this.conn.execQuery (query, this.onBasketForGuest.bind (this)); } } ,onBasketForGuest: function () { this.onBasketCheck (true); } ,onBasketCheck: function (isOk) { if (!isOk) return; this.basketChecked = true; this.open (); this.popup = new Htk.Popup (); this.popup.setChildNode (this.$('lots-popup')); } ,typeRenderer: function (builder, form) { var link = builder.$('link'); link.href = this.hash.make ({'type': form.get ('tipo_id')}, true); } ,realmRenderer: function (builder, form) { var link = builder.$('link'); link.href = this.hash.make ({ 'form': this.hash.get ('form'), 'realm': form.get ('id') }); var img = builder.$('image'); img.src = 'image/family/'+ form.get ('id') +'.svg'; img.title = form.get ('reino'); img.alt = img.title; } ,onTypeChange: function () { if (Vn.isMobile () && this.$('type').value) this.hideMenu (); var realms = this.$('realms-model'); if (!realms.ready) return; this.refreshTitle (); var color = null; if (this.$('type').value) { var row = realms.search ('id', this.$('realm').value); if (row != -1) color = '#'+ realms.get (row, 'color'); } this.$('header').style.backgroundColor = color; var tr = this.$('items-grid').getNode () .getElementsByTagName ('thead')[0] .getElementsByTagName ('tr')[0]; //.querySelector ('thead tr'); tr.style.backgroundColor = color; } ,refreshTitle: function (title) { var types = this.$('types-model'); if (!types.ready) return; var title = _('Catalog'); if (this.$('type').value) { var row = types.search ('tipo_id', this.$('type').value); if (row != -1) title = types.get (row, 'Tipo'); } else if (this.$('search-entry').value) title = _('SearchResults'); Vn.Node.setText (this.$('title'), title); } ,onSearch: function (event) { var searchTags = this.$('search-entry').value; searchTags = searchTags != '' ? searchTags : undefined; var batch = this.$('batch'); batch.block (); this.$('search').value = searchTags; if (searchTags) { this.$('type').value = undefined; this.$('realm').value = undefined; } batch.unblock (); batch.changed (); } ,onMenuClick: function (event) { event.stopPropagation (); } ,onShowMenuClick: function (event) { event.stopPropagation (); this.gui.showBackground (); this.$('menu').style.display = 'block'; this.hideMenuCallback = this.hideMenu.bind (this); document.addEventListener ('click', this.hideMenuCallback); } ,hideMenu: function () { this.gui.hideBackground (); this.$('menu').style.display = 'none'; document.removeEventListener ('click', this.hideMenuCallback); this.hideMenuCallback = null; } ,onBasketClick: function () { if (!Vn.Url.getQuery ('guest')) this.hash.set ({'form': 'ecomerce/basket'}); else Htk.Toast.showError (_('YouMustBeLoggedIn')); } ,featuresRender: function (renderer, form) { renderer.value = form.get ('Medida') +' '+ form.get ('Categoria') +' '+ form.get ('Color'); } ,amountRender: function (renderer, form) { var amount = form.get ('amount'); var grouping = form.get ('grouping'); if (amount && grouping) renderer.value = amount / grouping; else renderer.value = null; } ,amountChanged: function (renderer, row, newValue) { var model = this.$('items-model'); model.set (row, 'amount', newValue * model.get (row, 'grouping')); } ,catRenderer: function (column, form) { switch (form.get ('Categoria')) { case 'A2': column.td.className = 'second-category'; break; case 'B1': column.td.className = 'third-category'; break; default: column.td.className = ''; } } ,onAddItemClick: function (column, value, row, button) { if (Vn.Url.getQuery ('guest')) { Htk.Toast.showError (_('YouMustBeLoggedIn')); return; } var itemId = this.$('items-model').get (row, 'item_id') this.$('lots-batch').addValue ('item', itemId); this.itemRow = row; this.onEraseClick (); this.popup.show (button); } ,onAddLotClick: function (column, value, row, button) { var model = this.$('item-lots'); var grouping = model.get (row, 'grouping'); var warehouse = model.get (row, 'warehouse_id'); var lotAmount = this.items[warehouse]; if (lotAmount === undefined) lotAmount = 0; lotAmount += grouping; if (lotAmount <= model.get (row, 'available')) { this.items[warehouse] = lotAmount; this.$('amount').value += grouping; } else Htk.Toast.showError (_('NoMoreAmountAvailable')); } ,onEraseClick: function () { this.$('amount').value = 0; this.items = {}; } ,onConfirmClick: function () { var model = this.$('items-model'); var sql = ''; var batch = new Sql.Batch (); var query = new Sql.String ({query: 'CALL basket_item_add (#warehouse, #item, #amount);'}); var amountSum = 0; for (var warehouse in this.items) { var amount = this.items[warehouse]; amountSum += amount; batch.addValue ('warehouse', warehouse); batch.addValue ('item', model.get (this.itemRow, 'item_id')); batch.addValue ('amount', amount); sql += query.render (batch); } if (amountSum > 0) { this.conn.execQuery (sql); var itemName = model.get (this.itemRow, 'Article'); Htk.Toast.showMessage ( sprintf (_('Added%dOf%s'), amountSum, itemName)); } this.popup.hide (); } ,onStatusChange: function (model) { if (this.popup) this.popup.reset (); } });