summaryrefslogtreecommitdiff
path: root/StoneIsland/www/js/lib/products/ProductView.js
blob: eacfe9891d0f94276c52c69dbf0db066b170df1e (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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
var ProductView = ScrollableView.extend({
  
  el: "#product",
  
  events: {
    "click .fit": "scroll_to_bottom",
    "click .size": "select_size",
    "click .color": "select_color",
    "click .share": "share",
  },
  
  initialize: function(){
    this.gallery = new GalleryView ()
    this.scroller = new IScroll('#product', app.iscroll_options)
    
    this.$num = this.$(".num")
    this.$title = this.$(".title")
    this.$type = this.$(".type")
    this.$price = this.$(".price")
    this.$size = this.$(".size")
    this.$color = this.$(".color")
    this.$body = this.$(".body")
    this.$fit = this.$(".fit")
    this.$sizing = this.$(".sizing")
  },

  show: function(){
    app.footer.show("ADD TO CART", "BUY NOW")
    document.body.className = "product"
  },
  hide: function(){
  },
  
  item: null,
  details: null,
  size: null,
  color: null,
  code: null,
  is_onesize: false,

  sizes: null,
  colors: null,
  
  cache: {},
  
  find: function(code, cb){
    data = app.collection.items[code]
    if (code in this.cache) {
      return cb(data, this.cache[code])
    }
    sdk.product.item({
      code: code
    }).done(function(details){
      this.cache[code] = details
      cb(data, details)
    }.bind(this))
  },

  load: function(code, data){
    this.gallery.reset()
    this.show()
    if (app.view && app.view.hide) {
      app.view.hide()
    }
    app.view = this
    app.header.set_back(true)
    
    if (! app.collection.loaded) {
      this.el.className = "loading"
      app.collection.afterFetchCallback = this.load.bind(this, code, data)
      app.collection.fetch()
      return
    }
    else {
      app.collection.afterFetchCallback = null
    }
    window.location.href = "#/store/" + code
    
    console.log(data)
    if (data) {
      app.collection.items[code] = data
    }
    
    this.el.className = "loading"
    this.find(code, this.populate.bind(this))
  },

  populate: function(data, details){
    this.el.className = ""

    console.log(data, details)

    var descriptions = this.get_descriptions(details)

    this.gallery.populate( data['Code8'], details['Item']['ImageTypes'] )
    
    var name_partz = data['ModelNames'].split(' ')
    var num = name_partz.shift()
    var title = name_partz.join(' ')
    var type = title_case( data['MicroCategory'] )
    var price = "$" + data['DiscountedPrice'] + ".00"
    var body = descriptions['EditorialDescription'].replace(/<br>/g, "<br><br>")

    var default_color_id = this.populate_selectors(data, details)

    var color = this.colors[default_color_id]
    var color_label = color.label
    var sizes = this.find_sizes_for_color(default_color_id)
    var size = sizes[0]
    var size_label = this.sizes[size].label
    
    this.is_onesize = !! this.sizes[1]

    // console.log(color, color_label, size, size_label)

    this.item = data
    this.details = details['Item']
    this.code = data['DefaultCode10']

    this.color = color
    this.size = size
    
    this.$num.html(num)
    this.$title.html(title)
    this.$type.html(type)
    this.$price.html(price)
    this.$body.html(body)
    
    this.$size.html(size_label)
    if (color_label) {
      this.$color.show().html(color_label)
    }
    else {
      this.$color.hide()
    }

    this.deferScrollToTop()
  },

  get_descriptions: function (details){
    var descriptions = {}
    details['Item']['Descriptions'].forEach(function(pair){
      descriptions[pair.Key] = pair.Value
    })
    return descriptions
  },
  find_sizes_for_color: function(color_id){
    return Object.keys( this.colors[color_id].sizes ).sort(function(a,b){
      var ao = SIZE_ORDER[ a.label ], bo = SIZE_ORDER[ b.label ]
      return ao<bo?-1:ao==bo?0:1
    })
  },
  find_colors_for_size: function(size_id){
    return Object.keys( this.sizes[size_id].colors )
  },
  
  populate_selectors: function(data, details){
    var sizes = {}, colors = {}, size_lookup = {}, default_color
    details['Item']['ModelColors'].forEach(function(color, index){
      if (! default_color || color['Code10'] == data['DefaultCode10']) {
        default_color = color['ColorId']
      }
      colors[ color['ColorId'] ] = {
        id: color['Code10'],
        code: color['Code10'],
        label: color['ColorDescription'].toUpperCase(),
        sizes: {},
      }
    })
    details['Item']['ModelSizes'].forEach(function(size){
      var label = SIZE_LOOKUP[ size['Default']['Text'] ]
      size_lookup[ label ] = size['SizeId']
      sizes[ size['SizeId'] ] = {
        id: label,
        label: label.toUpperCase(),
        colors: {},
      }
    })
    details['Item']['ModelColorSize'].forEach(function(cs){
      colors[ cs['IdColor'] ].sizes[ cs['IdSize'] ] = true
      sizes[ cs['IdSize'] ].colors[ cs['IdColor'] ] = true
    })
    
    this.sizes = sizes
    this.colors = colors
    return default_color
  },

  select_size: function(){
    if (this.is_onesize) { return this.select_color() }
    if (this.item['Sizes'].length == 0) { return }
    var sizes = Object.keys(this.sizes).map(function(key){
      return this.sizes[key]
    }.bind(this))
    app.selector.select(sizes, function(size){
      this.size = size.value
      this.$size.html(size.label)
    }.bind(this))
  },
  
  select_color: function(){
    if (this.item['Colors'].length == 0) { return }
    var colors = Object.keys(this.colors).map(function(key){
      return this.colors[key]
    }.bind(this))
    app.selector.select(colors, function(color){
      this.code = color.code
      this.$color.html(color.label)
    }.bind(this))
  },

  // ADD TO CART
  save: function(){
    this.add_to_cart({ route: false })
  },
  // BUY NOW
  cancel: function(){
    this.add_to_cart({ route: true })
  },
  
  add_to_cart: function(opt){
    auth.deferred_product = { Size: this.size, Code10: this.code }
    if ( ! auth.logged_in() ) {
      app.router.go("account/login")
      app.last_view = app.cart
    }
    else if ( ! auth.has_cart() ) {
      auth.create_cart(function(){
        auth.add_deferred_product_to_cart(function(){
          app.router.go("cart")
        })
      })
    }
    else {
      auth.add_deferred_product_to_cart(function(){
        if (opt.route) {
          app.router.go("cart")
        }
      })
    }
  },
  
  back: function(){
    app.router.go('store')
  },
  
  scroll_to_bottom: function(){
  },
  
  share: function(){
  },

})

var SIZE_LOOKUP = {
  "XS": "X-SMALL",
  "S": "SMALL",
  "M": "MEDIUM",
  "L": "LARGE",
  "XL": "X-LARGE",
  "XXL": "XX-LARGE",
  "3XL": "3X-LARGE",
  "OneSize": "ONESIZE",
}

var SIZE_ORDER = "XS S M L XL XXL 3XL".split(" ")

/*
{
  "Code8": "41504876",
  "BrandName": "STONE ISLAND",
  "DefaultCode10": "41504876MA",
  "MicroCategory": "Jacket",
  "MacroCategory": "COATS & JACKETS",
  "FullPrice": 728,
  "DiscountedPrice": 437,
  "PriceListId": 155702498,
  "ModelNames": "41764 FLOWING CAMO WATRO",
  "Sizes": [
    {
      "Id": 4,
      "Text": "S",
      "ClassFamily": "INT",
      "Labeled": true
    },
    {
      "Id": 6,
      "Text": "L",
      "ClassFamily": "INT",
      "Labeled": true
    },
    {
      "Id": 7,
      "Text": "XL",
      "ClassFamily": "INT",
      "Labeled": true
    },
    {
      "Id": 8,
      "Text": "XXL",
      "ClassFamily": "INT",
      "Labeled": true
    }
  ],
  "Colors": [
    {
      "Id": 3152,
      "Code10": {
        "Id": 6769575,
        "Value": "41504876MA"
      },
      "Description": "Green",
      "MacroColorId": 3152,
      "Rgb": "3C941F"
    }
  ],
  "SizeTypeId": 928,
  "HasFlipSide": false,
  "SeasonOfSale": "PE15",
  "SalesLineId": "126",
  "SalesLine": "18_STONE ISLAND",
  "MarketId": 19,
  "Criteria": {
    "Sizes": [
      "3",
      "5",
      "6",
      "7"
    ],
    "Looks": [],
    "Styles": [],
    "WashTypes": [],
    "WashStories": [],
    "WashCodes": [],
    "Waists": [],
    "Fabrics": [],
    "ColorTypes": [],
    "ModelNames": [],
    "Material": []
  },
  "NoveltyPoints": 0,
  "C10Attributes": [
    {
      "Key": "MFC",
      "Value": "621541764CC-6215-64V0050",
      "C10": "41504876MA"
    }
  ],
  "MacroCategoryId": 224,
  "MicroCategoryId": 1319
},

DESCRIPTIONS:

Age: "Adult"
Appliqué: "Logo detail"
ColoreFoto_ID: "3140"
Composition: "100% Cotton"
CustomsInvoiceDescr: "Woven"
Design: "Solid color"
EditorialDescription: "20081 DATA DRIP PIN<br>Short sleeve T-Shirt in cotton jersey. Garment dyed.<br>Stone Island Compass logo print on the front, made up of a series of numbers."
ItemDescription: "Logo detail<br>Jersey<br>Round collar<br>Solid color<br>"
KeywordDescription: "Logo detail Jersey Round collar Solid color Jersey Woven not made of fur "
Kind of fabric: "Woven"
MFC: "631520081CC-6315-81V0060"
MacroCategory: "POLO SHIRTS & T-SHIRTS"
MadeIn: "Made In Turkmenistan"
MadeInIsoCode: "TN"
MainMaterial: "Cotton"
Material Description: "Jersey"
MicroCategory: "Short sleeve t-shirt"
MicroCategoryPlural: "Short sleeve t-shirts"
ModelFabric: "631520081CC-6315-81"
ModelNames: "20081 DATA DRIP PIN"
Neckline: "Claudine or round collar"

*/