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",
"touchstart h1": "showDepartmentSelector",
},
initialize: function(){
this.$title = this.$("h1")
this.$content = this.$(".content")
this.$loader = this.$(".loader")
this.scroller = new IScroll('#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
THE STONE ISLAND STORE.", "")
return
}
if (sdk.env !== "test" && app.closed.storeIsClosed) {
return app.closed.show()
}
if (this.data && this.data.SearchResponseFull.Results.Items.length < 4) {
app.footer.hide()
}
else 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()
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]
var url = sdk.image(item['DefaultCode10'], '13_f')
console.log(url)
var img = new Image ()
img.src = url
}
// if (data.SearchResponseFull.Results.Items.length == 1) {
// app.footer.hide()
// }
// else {
// app.footer.show("FILTER")
// }
console.log( data.SearchResponseFull.Results.Items.length )
data.SearchResponseFull.Results.Items.forEach(function(item){
console.log(">>> ITEM")
this.append(item, is_single_product)
}.bind(this))
this.deferScrollToTop()
}
this.afterFetchCallback && this.afterFetchCallback()
app.collection.deferRefresh()
},
append: function(item, is_single_product){
this.items[ item['Code8'] ] = item
var t = this.template.replace(/{{image}}/, sdk.image(item['DefaultCode10'], is_single_product ? '13_f' : '11_f'))
.replace(/{{code8}}/, item['Code8'])
var $t = $(t)
if (app.store.ShowProductNameOnCollectionPage) {
var $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]
app.product.load(code, data)
},
collectionName: "STONE ISLAND",
setCollectionName: function(name){
this.collectionName = name
this.$title.html(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)
}
},
})