Blame view

orders/web/js/artbox_basket.ts 3.52 KB
8ad6fbc1   Alexey Boroda   first 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
  ///<reference path="node_modules/@types/jquery/index.d.ts" />
  class ArtboxBasket {
      private _items;
      private _language;
      private _defaults: Object = {
          language: 'ru',
          url: 'basket',
          initError: 'Basket cannot be init',
          modalSelector: '.basket_modal',
          cartSelector: '.basket_wrapper',
          initCallback: function () {
          }
      };
      private _settings: Object = {};
  
      get items() {
          return this._items;
      }
  
      get language() {
          if (this._language === undefined) {
              let language_attr = $('html').attr('lang');
              if (language_attr !== undefined) {
                  let 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;
      }
  
      constructor(settings: Object = {},) {
          this._settings = ArtboxBasket.mergeObjects(this._defaults, settings);
          if (settings['language'] !== undefined) {
              this._language = settings['language'];
          }
          this.init(this._settings['initCallback']);
      }
  
      public init(callback) {
          $.get(this.getLanguagePath() + this._settings['url'], function (data) {
              this._items = data.basket;
              console.log(callback);
              callback(data);
          }.bind(this), 'json').fail(function () {
              console.error(this._settings['initError']);
          }.bind(this));
      }
  
      public add(variant_id, count, callback): JQueryPromise<JQueryXHR> {
          return $.post(this.getLanguagePath() + this._settings['url'] + '/add?variant_id=' + variant_id + '&count=' + count, function (data) {
              this._items = data.basket;
              if (callback !== undefined) {
                  callback(data);
              }
          }.bind(this), 'json').fail(function (xhr, status, error) {
              console.error(error);
          });
      }
  
      public set(variant_id, count, callback): JQueryPromise<JQueryXHR> {
          return $.post(this.getLanguagePath() + this._settings['url'] + '/set?variant_id=' + variant_id + '&count=' + count, function (data) {
              this._items = data.basket;
              if (callback !== undefined) {
                  callback(data);
              }
          }.bind(this), 'json').fail(function (xhr, status, error) {
              console.error(error);
          });
      }
  
      public remove(variant_id, callback): JQueryPromise<JQueryXHR> {
          return $.post(this.getLanguagePath() + this._settings['url'] + '/remove?variant_id=' + variant_id, function (data) {
              this._items = data.basket;
              if (callback !== undefined) {
                  callback(data);
              }
          }.bind(this), 'json').fail(function (xhr, status, error) {
              console.error(error);
          });
      }
  
  
  
      get count(): number {
          return Object.keys(this._items).length;
      }
  
      private static mergeObjects(obj1: Object, obj2: Object): Object {
          let obj = {};
          for (let attribute in obj1) {
              obj[attribute] = obj1[attribute];
          }
          for (let attribute in obj2) {
              obj[attribute] = obj2[attribute];
          }
          return obj;
      }
  
      private getLanguagePath(): string {
          if (this.language) {
              return '/' + this.language + '/';
          } else {
              return '/';
          }
      }
  }