/* * @constructor */ jt.block.basket = function () { this.items_ = []; this.itemElements_ = $('.basket__item'); this.itemElements_.each($.proxy(function (index, item) { this.items_.push(new jt.block.basket_item(item, this)); }, this)); this.itemElements_.on('updatePrice', $.proxy(function (event, data) { for (var i = 0, l = this.items_.length; i < l; i++) { if (data.root === this.items_[i].root) { this.items_[i].price_cost_ = this.items_[i].root.find('.item__cost .price__value').text(); break; } } this.sumCost_(); }, this)); this.sumCost_(); } /* * @constructor */ jt.block.basket_item = function (item, cart) { var root, cart; this.cart = cart; this.root = root = $(item); this.price_cost_ = root.find('.item__cost .price__value').text(); new jt.block.basket_calculator(this.root); } /* * count number of items * @private */ /* jt.block.basket.prototype.count_ = function() { var words = ['товар', 'товара', 'товаров']; $('.basket_count').text(' ' + jt.utils.declension( count, words)); } */ /* * Sum and replace cost_total * @private */ jt.block.basket.prototype.sumCost_ = function () { var cost_total; cost_total = 0; for (var i = 0, l = this.items_.length; i < l; i++) { cost_total += this.items_[i].getCost(); } $('.basket__total-price') .find('.price').empty() .append(jt.block.Cart.ADD_PRICE_CONTAINER) .find('.price__value') .text(jt.utils.format_number(cost_total)) .after(jt.block.Cart.ADD_ROUBLE_SIGN); } jt.block.basket_item.prototype.getCost = function () { return jt.utils.clean_to_number(this.price_cost_); } /* * @constructor */ jt.block.basket_calculator = function (root) { var quantity; this.parent_ = root; this.resultPriceElem_ = this.parent_.find('.item__cost .price__value'); this.priceElem_ = this.parent_.find('.item__price .price__value'); this.initialPrice_ = jt.utils.clean_to_number(this.priceElem_.text()); quantity = this.parent_.find('.item__input'); this.input_ = new jt.block.IntegerInput(quantity, 9999, quantity.data('min'), undefined, true); this.input_.addEventListener(jt.block.IntegerInput.EventType.CHANGE, $.proxy(function () { this.calculate_(this.input_.getValue()); this.parent_.trigger('updatePrice', {root: this.parent_}); }, this)); this.action_delete = this.parent_.find('.del_from_js_basket'); this.action_delete.on('click', function () { var product_id = $(this).data('productId'); var delete_data = {'action': 'delete', 'id': product_id}; root.remove(); var items_count = $('ul.basket__list li').length; $('.products_counter a').text('Корзина (' + items_count + ')'); $.ajax({ type: 'GET', url: "/personal/cart/index.php", data: delete_data, success: function(){ window.location.href = window.location.href; } }); }); this.calculate_(this.input_.getValue()); } jt.block.basket_calculator.prototype.calculate_ = function (quantity) { var discount, price, resultPrice; price = this.initialPrice_; resultPrice = price * quantity; this.resultPriceElem_.text(jt.utils.format_number(resultPrice)); this.priceElem_.text(jt.utils.format_number(price)); } $(document).on('ready', function () { new jt.block.basket(); });