summaryrefslogtreecommitdiff
path: root/StoneIsland/www/js/lib/cart/CartShipping.js
blob: dfc5cb3a99820af64442ecaeda3ab568af602338 (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
var CartShipping = FormView.extend({
  
  el: "#cart_shipping",
  
  action: sdk.cart.set_shipping_address,
  
  list_mode: true,
  data: {},
  
  template: $("#cart_shipping .template").html(),
  
  events: {
    "click .dropdown-wrapper": "toggle_dropdown",
  },

  initialize: function(opt){
    this.parent = opt.parent
    this.$form = this.$("form")
    this.$dropdown_wrapper = this.$(".dropdown-wrapper")
    this.$address_list = this.$(".address_list")
    this.$address_form = this.$(".address")
    this.$msg = this.$(".msg")
    this.address = new AddressView ({ parent: this })
    this.scroller = new IScroll('#cart_shipping', app.iscroll_options)
    this.address.disabled = true
  },

  show: function(){
    document.body.className = "cart"
    app.cart.el.className = "shipping"
    app.footer.show("PAYMENT >", "CANCEL")
    window.location.hash = "#/cart/shipping"
    this.populate()
    this.deferScrollToTop()
  },
  
  populate: function(){
    // id checked name address city state zip
    this.$(".save_as_default").show()
    this.$address_list.empty()
    if (! app.account.addresses.length) {
      this.toggle_dropdown(false)
      return
    }
    app.account.addresses.forEach(function(address){
      var t = this.template.replace(/{{id}}/g, address.Id)
                           .replace(/{{checked}}/g, address.IsDefault ? "checked" : "")
                           .replace(/{{name}}/g, address.Name + " " + address.Surname)
                           .replace(/{{address}}/g, address.Address.replace(/\n$/,"").replace("\n", "<br>"))
                           .replace(/{{city}}/g, address.City)
                           .replace(/{{state}}/g, address.Province)
                           .replace(/{{zip}}/g, address.ZipCode)
      this.$address_list.append(t)
    }.bind(this))
  },
  
  load_form: function(cart_data){
    var data = cart_data.Cart.Receiver
    var addy = data.StreetWithNumber.split("\n")
    data.Address1 = addy[0] || ""
    data.Address2 = addy[1] || ""
    data.ZipCode = data.PostalCode
    data.Province = data.Region
    this.load_data(data)
    
    if (cart_data.DeliveryMethod.Selected && cart_data.DeliveryMethod.Type) {
      $("#standard-shipping").prop("checked", cart_data.DeliveryMethod.Type == 1)
      $("#express-shipping").prop("checked", cart_data.DeliveryMethod.Type == 2)
    }
  },
  
  toggle_dropdown: function(state){
    if (! app.account.addresses.length) {
      state = false
    }
    this.list_mode = typeof state == "boolean" ? state : ! this.list_mode
    this.$dropdown_wrapper.toggle( !! app.account.addresses.length )
    this.address.disabled = this.list_mode
    this.$address_form.toggle(! this.list_mode)
    this.$address_list.toggle(this.list_mode)
  },
  
  // sdk.cart.set_shipping_address
  // sdk.shipping.get_delivery_types
  // sdk.shipping.set_delivery_type
  
  shipping_types: {
    Standard: 1,
    Express: 2,
  },
  
  finalize: function(data){
    var shipping_info = {}, address_data, address_id
    var shipping_type = $("[name=ShippingType]").filter(function(){ return $(this).prop("checked") }).val()

    sdk.shipping.set_delivery_type({
      id: this.shipping_types[shipping_type],
      success: function(data){ console.log("set shipping type", data) },
      error: function(data){ console.log("didnt set shipping type", data) },
    })
    
    if (this.list_mode) {
      address_id = this.$("[name=AddressId]").filter(function(){ return $(this).prop("checked") }).val()
      address_data = app.account.addressLookup[ address_id ]
    }
    else {
      address_data = data
    }
    
    shipping_info.Name = address_data.Name
    shipping_info.Surname = address_data.Surname
    shipping_info.Email = auth.user.Email
    shipping_info.Phone = address_data.Phone
    shipping_info.Mobile = address_data.Phone
    shipping_info.StreetWithNumber = address_data.Address
    shipping_info.PostalCode = address_data.ZipCode
    shipping_info.City = address_data.City
    shipping_info.Province = address_data.Province
    shipping_info.Region = address_data.Province
    shipping_info.CountryCode = CANADIAN_LOOKUP[ address_data.Province ] ? "CA" : "US"
    
    this.data = shipping_info
    
    return shipping_info
  },
  
  success: function(){
    app.router.go('cart/payment')
  },
  
  cancel: function(){
    app.router.go('cart/summary')
  },

})