Blame view

web/js/artbox_basket.js 6.03 KB
ed038e99   Yarik   Order
1
  ///<reference path="node_modules/@types/jquery/index.d.ts" />
0b0b1232   Anastasia   bonuses in basket
2
  var ArtboxBasket = /** @class */ (function () {
11ecfb79   Yarik   Basket
3
4
5
6
7
8
9
      function ArtboxBasket(settings) {
          if (settings === void 0) { settings = {}; }
          this._defaults = {
              language: 'ru',
              url: 'basket',
              initError: 'Basket cannot be init',
              modalSelector: '.basket_modal',
0b0b1232   Anastasia   bonuses in basket
10
11
              cartSelector: '.basket_wrapper',
              smallSelector: '.small_basket_page'
11ecfb79   Yarik   Basket
12
13
14
          };
          this._settings = {};
          this._settings = ArtboxBasket.mergeObjects(this._defaults, settings);
20743c4e   Yarik   Basket
15
16
17
          if (settings['language'] !== undefined) {
              this._language = settings['language'];
          }
11ecfb79   Yarik   Basket
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
          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) {
0b0b1232   Anastasia   bonuses in basket
50
          this.deleteBonus();
20743c4e   Yarik   Basket
51
          $.get(this.getLanguagePath() + this._settings['url'], function (data) {
11ecfb79   Yarik   Basket
52
53
54
55
56
57
58
59
60
61
62
63
              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) {
20743c4e   Yarik   Basket
64
          return $.post(this.getLanguagePath() + this._settings['url'] + '/add?variant_id=' + variant_id + '&count=' + count, function (data) {
11ecfb79   Yarik   Basket
65
66
67
68
69
70
71
              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) {
20743c4e   Yarik   Basket
72
          return $.post(this.getLanguagePath() + this._settings['url'] + '/set?variant_id=' + variant_id + '&count=' + count, function (data) {
11ecfb79   Yarik   Basket
73
74
75
76
77
78
79
              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) {
20743c4e   Yarik   Basket
80
          return $.post(this.getLanguagePath() + this._settings['url'] + '/remove?variant_id=' + variant_id, function (data) {
11ecfb79   Yarik   Basket
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
              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);
      };
0b0b1232   Anastasia   bonuses in basket
105
106
107
108
      ArtboxBasket.prototype.updateSmall = function (small_html) {
          var cart = $(this._settings['smallSelector']);
          cart.html(small_html);
      };
11ecfb79   Yarik   Basket
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
      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;
      };
20743c4e   Yarik   Basket
138
139
140
141
142
143
144
145
      ArtboxBasket.prototype.getLanguagePath = function () {
          if (this.language) {
              return '/' + this.language + '/';
          }
          else {
              return '/';
          }
      };
0b0b1232   Anastasia   bonuses in basket
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
      ArtboxBasket.prototype.setBonus = function (bonus) {
          return $.post(this.getLanguagePath() + this._settings['url'] + '/add-bonus?bonus=' + bonus, function (data) {
              this._items = data.basket;
              this.updateModal(data.modal, data.cart, true);
              this.updateSmall(data.small);
          }.bind(this), 'json').fail(function (xhr, status, error) {
              console.error(error);
          });
      };
      ArtboxBasket.prototype.deleteBonus = function () {
          return $.post(this.getLanguagePath() + this._settings['url'] + '/delete-bonus', function (data) {
              this._items = data.basket;
              this.updateModal(data.modal, data.cart, true);
              this.updateSmall(data.small);
          }.bind(this), 'json').fail(function (xhr, status, error) {
              console.error(error);
          });
      };
11ecfb79   Yarik   Basket
164
165
      return ArtboxBasket;
  }());