/*
* @constructor
*/
jt.block.Cart = function () {
this.items_ = [];
this.itemElements_ = $('.cart__item');
this.itemElements_.each($.proxy(function (index, item) {
this.items_.push(new jt.block.CartItem(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('.cart__cell_cost .price__value').text();
break;
}
}
this.sumCost_();
}, this))
this.sumCost_();
}
/*
* @constructor
*/
jt.block.CartItem = function (item, cart) {
var root, cart;
this.cart = cart;
this.root = root = $(item);
this.isDelay_ = $(item).data('delay') === true ? true : false;
this.remover_ = root.find('.cart__item-remover');
this.price_cost_ = root.find('.cart__cell_cost .price__value').text();
// this.remover_.on('click', $.proxy(this.remove_, this));
new jt.block.ProductPriceCalculator(this.root);
};
/* constants */
jt.block.Cart.ADD_ROUBLE_SIGN = ' руб.';
jt.block.Cart.ADD_PRICE_CONTAINER = '';
jt.block.Cart.HIDE_CLASS = 'not-display';
/*
* count number of items
* @private
*/
jt.block.Cart.prototype.count_ = function () {
var words = ['товар', 'товара', 'товаров'];
this.countReady_ = 0;
this.countDelay_ = 0;
for (var i = 0, l = this.items_.length; i < l; i++) {
if (this.items_[i].isDelay_) {
this.countDelay_++;
} else {
this.countReady_++;
}
}
$('.cart__tabs label:first-child .extra').text(' ' + this.countReady_);
$('.cart__num').text(this.countReady_);
$('.cart__num-word').text(' ' + jt.utils.declension(this.countReady_, words));
$('.cart__tabs label:last-child .extra').text(' ' + this.countDelay_);
}
/*
* Sum and replace cost_total
* @private
*/
jt.block.Cart.prototype.sumCost_ = function () {
var cost_total;
cost_total = 0;
for (var i = 0, l = this.items_.length; i < l; i++) {
if (!this.items_[i].isDelay_) {
cost_total += this.items_[i].getCost();
}
}
$('.cart__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.CartItem.prototype.getCost = function () {
return jt.utils.clean_to_number(this.price_cost_);
}
/*
* @constructor
*/
jt.block.ProductPriceCalculator = function (root) {
var quantity;
this.parent_ = root;
this.resultPriceElem_ = this.parent_.find('.cart__cell_cost .price__value');
this.priceElem_ = this.parent_.find('.cart__cell_price-element .price__value');
this.initialPrice_ = jt.utils.clean_to_number(this.priceElem_.text());
quantity = this.parent_.find('.cart__cell_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.calculate_(this.input_.getValue());
}
/**
* Calculating price by quantity change
* @param {number} quantity Of products
* @private
*/
jt.block.ProductPriceCalculator.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.Cart();
// $('.sidebar__inner').affix();
$('.cart__item-remover').click(function () {
var product_id = $(this).data('id');
var delete_data = {'action': 'delete', 'id': product_id};
var item_index = $(this).parent().parent('.cart__item').index('tr');
$('ul.basket__list li:nth-child(' + item_index + ')').remove();
var items_count = $('ul.basket__list li').length;
$('.products_counter a').text('Корзина (' + items_count + ')');
$(this).parent().parent('.cart__item').remove();
$.ajax({
type: 'GET',
url: "index.php",
data: delete_data,
success: function(){
window.location.reload();
}
});
});
});