artbox_basket.js 2.4 KB
var ArtboxBasket = (function () {
    function ArtboxBasket() {
        $.get('/basket/main', function (data) {
            this._items = data.basket;
            this.updateModal(data.modal, data.cart);
        }.bind(this), 'json').fail(function () {
            console.error('Basket cannot be init');
        });
    }
    Object.defineProperty(ArtboxBasket.prototype, "items", {
        get: function () {
            return this._items;
        },
        enumerable: true,
        configurable: true
    });
    ArtboxBasket.prototype.add = function (product_variant_id, count) {
        $.post('/basket/add?product_variant_id=' + product_variant_id + '&count=' + count, function (data) {
            this._items = data.basket;
            this.updateModal(data.modal, data.cart, true);
        }.bind(this), 'json').fail(function (xhr, status, error) {
            console.error(error);
        });
    };
    ArtboxBasket.prototype.set = function (product_variant_id, count) {
        $.post('/basket/set?product_variant_id=' + product_variant_id + '&count=' + count, function (data) {
            this._items = data.basket;
            this.updateModal(data.modal, data.cart);
        }.bind(this), 'json').fail(function (xhr, status, error) {
            console.error(error);
        });
    };
    ArtboxBasket.prototype.updateModal = function (modal, cart_html, show) {
        if (show === void 0) { show = false; }
        var modalBox = $('#buyForm');
        modalBox.html(modal);
        if (this.count < 1) {
            modalBox.modal('hide');
        }
        else if (show) {
            modalBox.modal('show');
        }
        this.updateCart(cart_html);
    };
    ArtboxBasket.prototype.updateCart = function (cart_html) {
        var cart = $('#top-cart-content');
        cart.html(cart_html);
    };
    Object.defineProperty(ArtboxBasket.prototype, "count", {
        get: function () {
            return Object.keys(this._items).length;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(ArtboxBasket.prototype, "sum", {
        get: function () {
            var sum = 0;
            $.each(this._items, function (index, value) {
                sum += value.price * value.count;
            });
            return sum.toFixed(2);
        },
        enumerable: true,
        configurable: true
    });
    return ArtboxBasket;
})();
//# sourceMappingURL=artbox_basket.js.map