Blame view

frontend/web/js/artbox_basket.ts 1.7 KB
cc658b4c   Yarik   Big commit
1
2
3
4
5
6
7
8
9
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
  class ArtboxBasket {
      private _items;
      get items() {
          return this._items;
      }
      constructor() {
          this.init(true, true);
      }
      public init(update_modal, update_cart) {
          $.get('/basket', 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('Basket cannot be init');
          });
      }
      public add(product_variant_id) {
          $.post('/basket/add?product_variant_id='+product_variant_id, function (data) {
              this._items = data.basket;
              this.updateModal(data.modal, data.cart, true);
              showForm();
          }.bind(this), 'json').fail(function (xhr, status, error) {
              console.error(error);
          });
      }
      public remove(product_variant_id) {
          $.post('/basket/remove?product_variant_id='+product_variant_id, function (data) {
              this._items = data.basket;
              this.updateCart(data.cart);
              // this.updateModal(data.modal, data.cart, true);
          }.bind(this), 'json').fail(function (xhr, status, error) {
              console.error(error);
          });
      }
      public updateModal(modal, cart_html, show = false)
      {
          var modalBox = $('#modal_form-2');
          modalBox.html(modal);
          if(cart_html) {
              this.updateCart(cart_html);
          }
      }
      public updateCart(cart_html) {
          var cart = $('.question-form ');
          cart.html(cart_html);
      }
      get count(): number {
          return Object.keys(this._items).length;
      }
  }