Files
IlLievitario-App/js/modules/item/item.js
T

190 lines
9.1 KiB
JavaScript

define(['jquery', 'underscore', 'backbone', 'text!modules/item/itemViewTemplate.html', 'modules/base/baseView', 'classes/profile', 'jQueryPlugins'],
function ($, _, Backbone, itemViewTemplate, BaseView, Profile) {
var ItemView = BaseView.extend({
collRicetta: null,
collIngredienti: null,
fetchedRicetta: false,
fetchedIngredienti: false,
IMG_WIDTH: 500,
currentImg: 0,
maxImages: 3,
speed: 500,
//initialize template
template: _.template(itemViewTemplate),
initialize: function (options) {
this.id = "ItemView";
var self = this;
ItemView.__super__.initialize.call(this);
this.collRicetta = options.collRicetta;
this.collIngredienti = options.collIngredienti;
this.collRicetta.fetch({success: function () {
self.fetchedRicetta = true;
self.render();
}});
this.collIngredienti.fetch({success: function () {
self.fetchedIngredienti = true;
self.render();
}});
},
isStayInPage: function () {
return false;
},
events: {
"click #favoriteBtn": "favBtn",
"click #backBtn": "backBtn"
},
backBtn: function (e) {
e.preventDefault();
window.history.back();
},
favBtn: function (e) {
e.preventDefault();
var self = this;
var ric = this.collRicetta.toJSON()[0];
if (!Profile.isConnected())
{
self.$el.find('#popupDialog').popup("open");
return;
}
Profile.addRicettaBloccoNote(ric.ricetta_id,
function (msg) {
var favBtn = self.$el.find('#favoriteBtn');
favBtn.addClass('ui-disabled');
}
);
},
onShowed: function(){
$('body').scrollTop(0);
this.$el.find("#Gallery").swipe({
triggerOnTouchEnd: true,
swipeStatus: this._swipeStatus,
allowPageScroll: "vertical",
threshold: 75
});
},
//render the content into div of view
render: function () {
if (this.fetchedIngredienti && this.fetchedRicetta)
{
ItemView.__super__.render.call(this);
var self = this;
var ric = self.collRicetta.toJSON()[0];
this.maxImages = ric.foto.length;
Profile.existRicettaBloccoNote(ric.ricetta_id,
function (exist) {
self.$el.append(self.template({
ricetta: self.collRicetta.toJSON()[0],
ingredienti: self.collIngredienti.toJSON()
}));
self.scrollPositions = [];
self.$arrayContent = self.$el.find('div[data-role="content"]');
self.$arrayNavBar = self.$el.find('div[data-role="navbar"] a');
for (var i = self.$arrayContent.length - 1; i >= 0; i--) {
self.scrollPositions.push(0);
};
var favBtn = self.$el.find('#favoriteBtn');
if (exist > 0)
favBtn.addClass('ui-disabled');
self.$arrayNavBar.on("click", function (e) {
e.preventDefault();
var index = jQuery.inArray(this, self.$arrayNavBar);
self.goToContent(index);
});
self.$arrayContent.on("swipeleft", function (e) {
var index = jQuery.inArray(this, self.$arrayContent);
self.goToContent(index + 1);
});
self.$arrayContent.on("swiperight", function (e) {
var index = jQuery.inArray(this, self.$arrayContent);
self.goToContent(index - 1);
});
self.trigger("renderCompleted:Item", self);
}
);
//this.el is the root element of Backbone.View. By default, it is a div.
//$el is cached jQuery object for the view's element.
//append the compiled template into view div container
//return to enable chained calls
return this;
}
},
/**
* Catch each phase of the swipe.
* move : we drag the div
* cancel : we animate back to where we were
* end : we animate to the next image
*/
_swipeStatus: function (event, phase, direction, distance) {
//If we are moving before swipe, and we are going L or R in X mode, or U or D in Y mode then drag.
if (phase == "move" && (direction == "left" || direction == "right")) {
var duration = 0;
if (direction == "left") {
scrollImages((this.IMG_WIDTH * this.currentImg) + distance, duration);
} else if (direction == "right") {
scrollImages((this.IMG_WIDTH * this.currentImg) - distance, duration);
}
} else if (phase == "cancel") {
scrollImages(this.IMG_WIDTH * this.currentImg, this.speed);
} else if (phase == "end") {
if (direction == "right") {
self._previousImage();
} else if (direction == "left") {
self._nextImage();
}
}
},
_previousImage: function () {
this.currentImg = Math.max(this.currentImg - 1, 0);
scrollImages(this.IMG_WIDTH * this.currentImg, this.speed);
},
_nextImage: function() {
this.currentImg = Math.min(this.currentImg + 1, this.maxImages - 1);
this._scrollImages(this.IMG_WIDTH * this.currentImg, this.speed);
},
/**
* Manually update the position of the imgs on drag
*/
_scrollImages: function (distance, duration) {
imgs.css("transition-duration", (duration / 1000).toFixed(1) + "s");
//inverse the number we set in the css
var value = (distance < 0 ? "" : "-") + Math.abs(distance).toString();
imgs.css("transform", "translate(" + value + "px,0)");
},
goToContent: function (newIndex)
{
if (this.$arrayContent.length > newIndex && newIndex >= 0)
{
this.scrollPositions[this.indexContent] = $('body').scrollTop();
this.indexContent = newIndex;
this.switchContent();
$('body').scrollTop(this.scrollPositions[this.indexContent]);
}
},
switchContent: function () {
this.$arrayContent.hide();
this.$arrayNavBar.removeClass('ui-btn-active');
this.$arrayNavBar.removeClass('ui-state-persist');
$(this.$arrayContent[this.indexContent]).show();
$(this.$arrayNavBar[this.indexContent]).addClass('ui-btn-active');
$(this.$arrayNavBar[this.indexContent]).addClass('ui-state-persist');
}
});
return ItemView;
});