/// var ArtboxBasket = (function () { function ArtboxBasket(settings) { if (settings === void 0) { settings = {}; } this._defaults = { language: 'ru', url: 'basket', initError: 'Basket cannot be init', modalSelector: '.basket_modal', cartSelector: '.basket_wrapper' }; this._settings = {}; this._settings = ArtboxBasket.mergeObjects(this._defaults, settings); this.init(true, true); } Object.defineProperty(ArtboxBasket.prototype, "items", { get: function () { return this._items; }, enumerable: true, configurable: true }); Object.defineProperty(ArtboxBasket.prototype, "language", { get: function () { if (this._language === undefined) { var language_attr = $('html').attr('lang'); if (language_attr !== undefined) { var language = language_attr.substr(0, 2); if (language.length == 2) { this._language = language; } else { this._language = this._settings['language']; } } else { this._language = this._settings['language']; } } return this._language; }, enumerable: true, configurable: true }); ArtboxBasket.prototype.init = function (update_modal, update_cart) { $.get('/' + this.language + '/' + this._settings['url'], function (data) { this._items = data.basket; if (update_modal) { this.updateModal(data.modal, false); } if (update_cart) { this.updateCart(data.cart); } }.bind(this), 'json').fail(function () { console.error(this._settings['initError']); }.bind(this)); }; ArtboxBasket.prototype.add = function (variant_id, count) { return $.post('/' + this.language + '/' + this._settings['url'] + '/add?variant_id=' + 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 (variant_id, count) { return $.post('/' + this.language + '/' + this._settings['url'] + '/set?variant_id=' + 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.remove = function (variant_id) { return $.post('/' + this.language + '/' + this._settings['url'] + '/remove?variant_id=' + variant_id, 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.updateModal = function (modal, cart_html, show) { if (show === void 0) { show = false; } var modalBox = $(this._settings['modalSelector']); modalBox.html(modal); if (cart_html) { this.updateCart(cart_html); } if (this.count < 1) { this.hideBasket(); } if (show) { return show; } }; ArtboxBasket.prototype.updateCart = function (cart_html) { var cart = $(this._settings['cartSelector']); cart.html(cart_html); }; ArtboxBasket.prototype.hideBasket = function () { $(this._settings['modalSelector']) .animate({ opacity: 0, top: '0' }, 200, function () { $(this) .css('display', 'none'); $('#overlay') .fadeOut(400); }); }; Object.defineProperty(ArtboxBasket.prototype, "count", { get: function () { return Object.keys(this._items).length; }, enumerable: true, configurable: true }); ArtboxBasket.mergeObjects = function (obj1, obj2) { var obj = {}; for (var attribute in obj1) { obj[attribute] = obj1[attribute]; } for (var attribute in obj2) { obj[attribute] = obj2[attribute]; } return obj; }; return ArtboxBasket; }());