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

164 lines
6.3 KiB
JavaScript

define(['jquery', 'underscore', 'backbone','text!modules/sync/syncViewTemplate.html', 'modules/base/baseView', 'classes/profile'],
function($, _, Backbone, syncViewTemplate, BaseView, Profile){
var SyncView = BaseView.extend({
//initialize template
template:_.template(syncViewTemplate),
initialize: function (options) {
SyncView.__super__.initialize.call(this);
if(options &&
options.tokenFound == 1)
{
if(window.localStorage.getItem("tryLogin")=="fb")
{
this.fbAfterLogin();
}
else if(window.localStorage.getItem("tryLogin")=="gplus")
{
this.gPlusAfterLogin();
}
}
window.localStorage.setItem("tryLogin", null);
},
events: {
"click #backBtn": "backBtn",
"click #fblogin": "fbLogin",
"click #gpluslogin": "gplusLogin",
"click #resetBtn": "resetBtn"
},
resetBtn:function(e){
e.preventDefault();
Profile.setKeySave(null);
Backbone.history.loadUrl();
},
backBtn:function(e){
e.preventDefault();
window.history.back();
},
fbLogin: function(e){
var self = this;
e.preventDefault();
if(!Profile.isConnected()) {
window.localStorage.setItem("tryLogin", "fb");
openFB.login('email');
}
else if(Profile.isConnected() && Profile.isFbConnected())
openFB.revokePermissions(function(){
Profile.setKeySave(null);
self.updateLoginBtn();
});
},
fbAfterLogin: function()
{
var self = this;
openFB.api({
path: '/me',
success: function (data) {
Profile.internalLoadProfile(data.id, function(retVal){
if(retVal == false)
Profile.createProfile(
data.id,
"fb",
data.name,
data.email,
data.gender,
function(){
//Backbone.history.loadUrl();
});
self.updateLoginBtn();
});
}});
},
gplusLogin: function(e){
var self = this;
e.preventDefault();
if(!Profile.isConnected()) {
window.localStorage.setItem("tryLogin", "gplus");
openGPlus.login('email');
}
else if(Profile.isConnected() && Profile.isGPlusConnected())
openGPlus.revokePermissions(function(){
Profile.setKeySave(null);
self.updateLoginBtn();
});
},
gPlusAfterLogin: function()
{
var self = this;
openGPlus.api({
path: 'people/me',
success: function (data) {
Profile.internalLoadProfile(data.id, function(retVal){
if(retVal == false)
Profile.createProfile(
data.id,
"gplus",
data.displayName,
data.emails[0].value,
data.gender,
function(){
//Backbone.history.loadUrl();
}
);
self.updateLoginBtn();
});
}});
},
updateLoginBtn: function()
{
if(Profile.isConnected()) {
if (Profile.isFbConnected()) {
this.$el.find("#fblogin").show();
this.$el.find("#gpluslogin").hide();
this.$el.find("#fblogin").removeClass("loginfb");
this.$el.find("#fblogin").addClass("logoutfb");
}
if (Profile.isGPlusConnected()) {
this.$el.find("#gpluslogin").show();
this.$el.find("#fblogin").hide();
this.$el.find("#gpluslogin").removeClass("logingplus");
this.$el.find("#gpluslogin").addClass("logoutgplus");
}
}
else
{
this.$el.find("#fblogin").removeClass("logoutfb");
this.$el.find("#fblogin").addClass("loginfb");
this.$el.find("#gpluslogin").removeClass("logoutgplus");
this.$el.find("#gpluslogin").addClass("logingplus");
}
},
//render the content into div of view
render: function(){
var self = this;
//this.header.title = "pippo";
SyncView.__super__.render.call(this);
this.header.$el.append("<a id=\"backBtn\" href=\"\" data-role=\"button\" class=\"ui-btn-right\" data-icon=\"custom\">Indietro</a>");
this.$el.append(this.header.$el);
this.$el.append(this.template( { app: myManifest }));
this.$el.append(this.footer.$el);
this.$el.find("#moreBtn").addClass("ui-btn-active");
this.updateLoginBtn();
return this;
}
});
return SyncView;
});