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
|
var CollectionView = ScrollableView.extend({
el: "#collection",
template: $("#collection .template").html(),
loaded: false,
data: null,
filtered_items: null,
items: {},
events: {
"touchstart .item": "touchstart",
"touchmove .item": "touchmove",
"touchend .item": "touchend",
"mousedown .item": "touchstart",
"mousemove .item": "touchmove",
"mouseup .item": "touchend",
"touchstart h1": "showDepartmentSelector",
},
initialize: function(){
this.$title = this.$("h1")
this.$content = this.$(".content")
this.$loader = this.$(".loader")
this.scroller = ScrollFactory('#collection', app.iscroll_options)
this.departmentFilterView = new DepartmentFilter ({ parent: this })
this.sizeFilterView = new SizeFilter ({ parent: this })
},
show: function(){
if (! navigator.onLine) {
app.closed.showElement()
app.closed.setMessage("PLEASE GO ONLINE TO BROWSE<br>THE STONE ISLAND STORE.", "")
return
}
if (sdk.env !== "test" && app.closed.storeIsClosed) {
return app.closed.show()
}
// if (sdk.env === 'test' && this.data.SearchResponseFull.Results.Items.length < 4) {
// var items = this.data.SearchResponseFull.Results.Items
// items = items.concat(items).concat(items).concat(items)
// items = items.concat(items).concat(items).concat(items)
// this.data.SearchResponseFull.Results.Items = items
// }
if (this.data && this.data.SearchResponseFull.Results.Items.length < 4) {
app.footer.hide()
}
if (app.store.FilterBy !== "none") {
if (app.store.FilterBy === "category") {
app.footer.show("FILTER")
}
else {
app.footer.show("FILTER BY " + app.store.FilterBy.toUpperCase())
}
}
document.body.className = "collection"
if (this.loaded) {
console.log("collection this loaded")
return this.populate(this.data)
}
else {
this.fetch()
}
},
// called when footer is tapped - filter by (x)
save: function(){
switch (app.store.FilterBy) {
case 'none':
this.departmentFilterView.filter()
break
case 'size':
this.sizeFilterView.filter()
break
}
},
fetch: function(){
console.log("collection fetch")
if (this.loaded) {
console.log("collection loaded")
return
}
this.$loader.show()
if (!app.department_id) {
console.log("no department ID, store is closed")
}
console.log("fetching", app.department_id)
sdk.product.collection({
department_id: app.department_id,
success: this.populate.bind(this)
})
},
refresh: function(){
this.loaded = false
this.fetch()
},
populate: function(data){
if (this.loaded && ! data) {
console.log("populate 1")
data = this.data
}
else {
console.log("populate 2")
this.data = data
this.loaded = false
// console.log(data)
}
console.log(">>>>>>>> YES ")
if (! this.loaded) {
console.log("populate 3", data.SearchResponseFull.Results.Items.length)
this.loaded = true
this.$loader.hide()
this.$content.empty()
// DefaultCode10
// data.SearchResponseFull.Results.Items.length = 1
var is_single_product = (data.SearchResponseFull.Results.Items.length < 4)
this.$el.toggleClass("single", is_single_product)
if (is_single_product) {
console.log("IS SINGLE PRODUCT")
var item = data.SearchResponseFull.Results.Items[0]
// console.log(data.SearchResponseFull.Results)
if (item) {
var url = sdk.image(item['DefaultCode10'], '13_f')
console.log(url)
var img = new Image ()
img.src = url
}
else {
console.log("no products at all!")
}
}
// if (data.SearchResponseFull.Results.Items.length == 1) {
// app.footer.hide()
// }
// else {
// app.footer.show("FILTER")
// }
console.log( data.SearchResponseFull.Results.Items.length )
var items = this.filtered_items || data.SearchResponseFull.Results.Items
if (app.store.GroupBy === 'size') {
this.groupBySize( items, is_single_product )
}
else {
this.appendItems( items, is_single_product )
}
// this.restoreScroll()
// this.deferScrollToTop()
}
this.afterFetchCallback && this.afterFetchCallback()
this.deferRefresh()
this.restoreScroll()
},
groupBySize: function(items, is_single_product){
var groups = {}
items.forEach(function(item){
if (! item.Sizes.length) return
var size = item.Sizes[0]
var id = size['Text']
if ( ! (id in groups) ) {
groups[id] = {
label: SIZE_LOOKUP[ id ],
items: [],
}
}
groups[id].items.push( item )
}.bind(this))
Object.keys(groups).sort(function(a,b){
var ao = SIZE_ORDER.indexOf( a )
var bo = SIZE_ORDER.indexOf( b )
return ao - bo
}).forEach(function(id){
var size = groups[id]
var $el = $("<div>")
$el.addClass("product-group")
$el.html( size.label )
this.$content.append($el)
this.appendItems( size.items, is_single_product )
}.bind(this))
},
appendItems: function(items, is_single_product){
items.forEach(function(item){
this.append(item, is_single_product)
}.bind(this))
},
append: function(item, is_single_product){
this.items[ item['Code8'] ] = item
console.log("TITLE: " + item['ModelNames'])
var t = this.template.replace(/{{image}}/, sdk.image(item['DefaultCode10'], is_single_product ? '13_f' : '11_f'))
.replace(/{{code8}}/, item['Code8'])
.replace(/{{title}}/g, stonewash(item['ModelNames'] + ' ' + item['MicroCategory']))
var $t = $(t)
if (app.store.ShowProductNameOnCollectionPage) {
var $title = $("<span>")
$title.addClass("product-title")
$title.html( item['ModelNames'] )
$t.append($title)
}
this.$content.append($t)
},
pick: function(e){
var code = $(e.currentTarget).data("code")
var data = this.items[code]
this.saveScroll()
app.product.load(code, data)
},
collectionName: "STONE ISLAND",
setCollectionName: function(name){
this.collectionName = name
this.$title.html(this.collectionName)
this.$title.attr('aria-label', stonewash(this.collectionName))
},
// filter by department
showDepartmentSelector: function(){
if (this.$("h1").hasClass("single-dept")) {
this.departmentFilterView.filter()
}
},
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)
}
},
})
|