summaryrefslogtreecommitdiff
path: root/StoneIsland/platforms/ios/www/js/lib/cart/CartSummary.js
blob: 72c44405c8f990c5eff747deeb1141e6f752eeb7 (plain)
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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
var CartSummary = ScrollableView.extend({
  
  el: "#cart_summary",
  
  template: $("#cart_summary .template").html(),
  
  events: {
    "click .remove": "remove_item",
  },
  
  data: null,
  
  initialize: function(opt){
    this.parent = opt.parent
    this.$loader = this.$(".loader")
    this.$cart_body = this.$(".cart_body")
    this.$cart_empty = this.$(".cart_empty")
    this.scroller = new IScroll('#cart_summary', app.iscroll_options)

    this.$rows = this.$(".rows")
    this.$subtotal = this.$(".subtotal")
    this.$shipping = this.$(".shipping")
    this.$tax = this.$(".tax")
    this.$total = this.$(".total")
  },
  
  show: function(){
    document.body.className = "cart"
    app.cart.el.className = "summary"
    window.location.hash = "#/cart/summary"
    if (auth.has_cart()) {
      this.load()
    }
    else {
      this.empty()
    }
  },
  
  load: function(){
    this.el.className = "loading"
    app.footer.show("SHIPPING >", "CANCEL")
    sdk.cart.get_status({
      success: this.populate.bind(this),
      error: this.empty.bind(this),
    })
  },
    
  populate: function(data){
    this.data = data

    console.log("CART", data)
    
    if (! data.Cart.Items || data.Cart.Items.length == 0) {
      return this.empty()
    }

    this.updateCounts()
    
    this.$rows.empty()
    
    data.Cart.Items.forEach(function(item){
      var code_ten = item['Code10']
      var code = code_ten.substr(0, 8)
      var size_id = item['Size']

      var $el = $("<div>").addClass("cart_item_row")
      $el.html("<img src='img/spinner.gif'>")
      $el.data({
        code: code_ten,
        size: size_id,
      })
      this.$rows.append($el)
      app.product.find(code, function(data, details){
        // console.log(data, details)
        
        var descriptions = app.product.get_descriptions( details )
        // console.log(descriptions)

        var name_partz = descriptions['ModelNames'].split(' ')
        var num = name_partz.shift()
        var title = name_partz.join(' ')
        var type = title_case( descriptions['MicroCategory'] )
        
        var color_name, size_name
        // console.log(code)
        details.Item.ModelColors.some(function(color){
          if (color['Code10'] == code_ten) {
            color_name = color['ColorDescription']
            // console.log(color)
            return true
          }
          return false
        })
        details.Item.ModelSizes.some(function(size){
          if (size['SizeId'] == size_id) {
            // console.log(size)
            size_name = size['Default']['Text']
            size_name = SIZE_LOOKUP[ size_name ] || size_name
            if (! size_name && ! size['Default']['Labeled']) {
              size_name = size['Default']['Text'] + " " + size['Default']['ClassFamily']
            }

            return true
          }
          return false
        })

        var t = this.template
                    .replace(/{{image}}/, sdk.image(item['Code10'], '11_f'))
                    .replace(/{{sku}}/, num)
                    .replace(/{{title}}/, title)
                    .replace(/{{type}}/, type)
                    .replace(/{{size}}/, size_name)
                    .replace(/{{color}}/, color_name)
                    .replace(/{{quantity}}/, 1)
                    .replace(/{{price}}/, as_cash(details.Item.Price.DiscountedPrice))
        $el.html(t)
        this.refreshScroller()
      }.bind(this))
    }.bind(this))
    
    this.updateTotals()

    this.el.className = "full"
    this.refreshScroller()
  },
  
  updateCounts: function(){
    app.header.set_cart_count( this.data.Cart.Items.length )
    this.parent.setHeaderCount( this.data.Cart.Items.length )
  },
  
  updateTotals: function(){
    var subtotal = this.data.Cart.Totals.TotalWithoutPromotions
    var shipping_cost = this.data.Cart.DeliveryMethod.Selected.Amount.Total
    var tax = 0
    var total = this.data.Cart.Totals.TotalToPay
    
    this.$subtotal.html( as_cash(subtotal) )
    this.$shipping.html( as_cash(shipping_cost) )
    this.$tax.html( as_cash(tax) )
    this.$total.html( as_cash(total) )
  },
  
  empty: function(){
    app.footer.hide()
    app.header.set_cart_count(0)
    this.parent.setHeaderCount( 0 )
    this.parent.$itemcount.html("0 ITEMS")
    this.el.className = "empty"
  },

  save: function(){
    app.router.go('cart/shipping')
  },
  
  cancel: function(){
    app.router.go('intro')
  },
  
  remove_item: function(e){
    var $el = $( e.currentTarget ).closest(".cart_item_row")
    var data = $el.data()

    console.log(this.data.Cart)

    this.data.Cart.Totals.TotalWithoutPromotions -= data.price
    this.data.Cart.Totals.TotalToPay -= data.price    
    this.data.Cart.Items = this.data.Cart.Items.filter(function(item){
      return ( item['Code10'] !== data.code || item['Size'] !== data.size)
    })
    
    this.updateTotals()    
    this.updateCounts()
    $el.remove()
    this.refreshScroller()
    
    if (this.data.Cart.Items.length == 0) {
      this.empty()
    }
    
    app.curtain.show("loading")
    sdk.cart.delete_item({
      data: {
        Code10: data.code,
        Size: data.size,
      },
      success: function(){
        app.curtain.hide("loading")
      },
    })
  },

})