summaryrefslogtreecommitdiff
path: root/StoneIsland/www/js/lib/products/CollectionView.js
blob: 652aa92bd9860c26385131e65f92435d8904f7b9 (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
var CollectionView = ScrollableView.extend({
  
  el: "#collection",
  template: $("#collection .template").html(),
  loaded: false,
  data: null,
  items: {},

  events: {
    "touchstart .item": "touchstart",
    "touchmove .item": "touchmove",
    "touchend .item": "touchend",
    "mousedown .item": "touchstart",
    "mousemove .item": "touchmove",
    "mouseup .item": "touchend",
  },
  
  initialize: function(){
    this.$content = this.$(".content")
    this.$loader = this.$(".loader")
    this.scroller = new IScroll('#collection', app.iscroll_options)
  },

  show: function(){
    app.footer.show("FILTER")
    document.body.className = "collection"
    if (this.loaded) {
      return this.populate(this.data)
    }
    this.fetch()
  },
  
  ok: function(){
    var cats = this.data.SearchResponseFull.Refinements.Filters.Categories.map(function(cat){
      return {
        id: cat.Id,
        label: cat.Value
      }
    })
    if (this.filter_choice) {
      cats.push({
        id: "__remove_filter",
        label: "REMOVE FILTER",
      })
    }
    app.selector.select(cats, this.filter.bind(this))
  },
  filter_choice: null,
  filter: function(choice){
    this.$content.empty()
    if (choice.id == "__remove_filter") {
      this.filter_choice = null
      this.data.SearchResponseFull.Results.Items.forEach(this.append.bind(this))
    }
    else {
      this.filter_choice = choice
      this.data.SearchResponseFull.Results.Items.filter(function(item){
        return item.MacroCategory == choice.label
      }).forEach(this.append.bind(this))
    }
    this.deferScrollToTop()
  },
  
  fetch: function(){
    if (this.loaded) return
    this.$loader.show()
    sdk.product.collection({
      gallery_id: 32780,
      success: this.populate.bind(this)
    })
  },

  populate: function(data){
    if (this.loaded && ! data) {
      data = this.data
    }
    else {
      this.data = data
    }
    if (! this.loaded) {
      this.loaded = true
      this.$loader.hide()
      this.$content.empty()
      // DefaultCode10
      data.SearchResponseFull.Results.Items.forEach(this.append.bind(this))
      this.deferScrollToTop()
    }
    this.afterFetchCallback && this.afterFetchCallback()
  },
  
  append: function(item){
    this.items[ item['Code8'] ] = item
    var t = this.template.replace(/{{image}}/, sdk.image(item['DefaultCode10'], '11_f'))
                         .replace(/{{code8}}/, item['Code8'])
    this.$content.append(t)
  },
  
  pick: function(e){
    var code = $(e.currentTarget).data("code")
    var data = this.items[code]
    app.product.load(code, data)
  },
  
  firstTouch: { x: 0, y: 0, id: "" },
  lastTouch: { x: 0, y: 0, id: "" },
  touchstart: function(e){
    var p = e.originalEvent.touches ? e.originalEvent.touches[0] : e.originalEvent
    this.firstTouch.x = this.lastTouch.x = p.pageX
    this.firstTouch.y = this.lastTouch.y = p.pageY
    this.firstTouch.id = e.currentTarget.dataset.id
  },
  touchmove: function(e){
    var p = e.originalEvent.touches ? e.originalEvent.touches[0] : e.originalEvent
    this.lastTouch.x = p.pageX
    this.lastTouch.y = p.pageY
    this.lastTouch.id = e.currentTarget.dataset.id
  },
  touchend: function(e){
    var first = app.collection.firstTouch
    var last = app.collection.lastTouch
    var distance = Math.sqrt( Math.pow(first.x - last.x, 2) + Math.pow(first.y - last.y, 2) )
    if (distance < 20) {
      this.pick(e)
    }
  },

})