Blame view

frontend/web/js/artbox_basket.ts 2.01 KB
fe7b6960   Yarik   Basket
1
2
3
4
5
6
7
8
  class ArtboxBasket {
      private _items;
      get items() {
          return this._items;
      }
      constructor() {
          $.get('/basket', function (data) {
              this._items = data.basket;
258f7054   Yarik   Preload basket
9
              this.updateCart();
fe7b6960   Yarik   Basket
10
11
12
13
14
15
16
17
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
50
51
52
53
54
55
56
57
58
          }.bind(this), 'json').fail(function() {
              console.error('Basket cannot be init');
          });
      }
      public add(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, true);
          }.bind(this), 'json').fail(function (xhr, status, error) {
              console.error(error);
          });
      }
      public set(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);
          }.bind(this), 'json').fail(function (xhr, status, error) {
              console.error(error);
          });
      }
      public updateModal(modal, show = false)
      {
          var modalBox = $('#buyForm');
          modalBox.html(modal);
          if(show) {
              modalBox.modal('show');
          }
          this.updateCart();
      }
      public updateCart() {
          var cart = $('#top-cart-content');
          var count = this.count;
          if(count > 0) {
              $(cart).html('<a href="ordering.htm"><span class="in_the_cart">В корзине</span><span id="in_cart_col">' + count + ' товар' + ((count > 4) ? 'ов' : ((count > 1) ? 'а' : '')) + '</span><br/><span id="in_cart_sum">на ' + this.sum + ' </span><br/></a><div id="in_cart_a"><a class="btn" href="ordering.htm">оформить покупку</a></div>');
          } else {
  
          }
      }
      get count(): number {
          return Object.keys(this._items).length;
      }
      get sum(): number {
          var sum = 0;
          $.each(this._items, function(index, value) {
              sum += value.price * value.count;
          });
          return sum;
      }
  }