diff --git a/build.cmd b/build.cmd index 444874f..afe61f5 100644 --- a/build.cmd +++ b/build.cmd @@ -1,4 +1,4 @@ -call uglifyjs .\js\openfb.js .\js\opengplus.js .\js\libs\ua-parser.min.js -o .\js\common.min.js +call uglifyjs .\js\libs\hello.min.js .\js\libs\ua-parser.min.js -o .\js\common.min.js REM node r.js -o app.build_dev.js node r.js -o app.build_prod.js REM call cleancss -o .\css\loadingStyle.min.css .\css\loadingStyle.css diff --git a/css/_star.png b/css/_star.png new file mode 100644 index 0000000..b0c5942 Binary files /dev/null and b/css/_star.png differ diff --git a/index.php b/index.php index 13a3a57..b8ca29f 100644 --- a/index.php +++ b/index.php @@ -18,22 +18,10 @@ - - +
diff --git a/js/app.js b/js/app.js index fd9be39..73dbf3b 100644 --- a/js/app.js +++ b/js/app.js @@ -1,29 +1,78 @@ -define(['jquery','underscore', 'backbone', 'router', 'classes/profile'],function -($, _, Backbone,Router, Profile) { - 'use strict'; - var init=function(){ +define(['jquery', 'underscore', 'backbone', 'router', 'classes/profile', 'classes/cache'], function + ($, _, Backbone, Router, Profile, Cache) { + 'use strict'; + var init = function () { //create backbone router - var router=new Router(); + var router = new Router(); Backbone.history.start(); Backbone.emulateJSON = true; - var origin = location.protocol + '//' + location.hostname + (location.port ? ':' + location.port : '') + (location.pathname?location.pathname:''); - openFB.init('1466942266882168', origin, window.localStorage); - openGPlus.init('319140243944-qk0lfhgmi8shi4go97r83l69f1k0gdf4.apps.googleusercontent.com', origin, window.localStorage); - //console.log("Backbone history start"); - // Trigger 'route' event on router instance. - router.on('route', function(name, args) { - //alert(name); - }); - if( Profile.isConnected()) { + hello.init({ + facebook: '1466942266882168', + google: '319140243944-qk0lfhgmi8shi4go97r83l69f1k0gdf4.apps.googleusercontent.com' + }, {display: 'page', scope: 'email'}); + + //console.log("Backbone history start"); + // Trigger 'route' event on router instance. + router.on('route', function (name, args) { + //alert(name); + }); + + Cache.checkCacheFromServer(); + + if (Profile.isConnected()) { Profile.loadProfile(); } + hello.on('auth.login', function (auth) { + if (!Profile.isConnected()) + { + hello(auth.network).api("me").then(function (json) { + var id = ""; + var displayName = ""; + var email = ""; + var gender = ""; + + if (auth.network === "google") + { + id = json.id; + displayName = json.displayName; + email = json.email; + gender = json.gender; + } + else if (auth.network === "facebook") + { + id = json.id; + displayName = json.name; + email = json.email; + gender = json.gender; + } + Profile.internalLoadProfile(id, function(retVal){ + if(retVal === false) + { + Profile.createProfile( + id, + auth.network, + displayName, + email, + gender, + function () { + window.location.href = "#syncView/tokenFound"; + } + ); + } + else + window.location.href = "#syncView/tokenFound"; + }); + }); + } + }); + $('body').css('background-color', ''); $('#loadingFrm').remove(); - }; - - return { - initialize:init + }; + + return { + initialize: init }; }); \ No newline at end of file diff --git a/js/classes/cache.js b/js/classes/cache.js new file mode 100644 index 0000000..c703ecc --- /dev/null +++ b/js/classes/cache.js @@ -0,0 +1,66 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +define(['jquery', 'backbone'], + function ($, Backbone) { + + var Cache = { + convertToDate: function (mysql_string) + { + if (typeof mysql_string === 'string') + { + var t = mysql_string.split(/[- :]/); + + //when t[3], t[4] and t[5] are missing they defaults to zero + return new Date(t[0], t[1] - 1, t[2], t[3] || 0, t[4] || 0, t[5] || 0); + } + + return null; + }, + prefix: "cacheItem", + get: function (name) { + var item = window.localStorage.getItem(this.prefix + name); + if(item === null) + return null; + item = JSON.parse(item); + return item.storeValue; + }, + set: function (name, value) { + window.localStorage.setItem(this.prefix + name, JSON.stringify({setTime: new Date(), storeValue: value})); + }, + isExpired: function (name, timeDiff) { + var item = window.localStorage.getItem(this.prefix + name); + if(item === null) + return true; + + item = JSON.parse(item); + var time = item.setTime; + if(timeDiff.getTime() > Date.parse(time)) + return true; + return false; + }, + checkCacheFromServer: function () + { + var self = this; + $.ajax({ + url: myManifest.Settings.DefaultURL + "/api/profile/statusCache", + //dataType: "json", + type: 'GET', + success: function (msg) { + $(msg).each(function (index, element) { + var elemName = "Category" + element.ID_CATEGORIA; + if (self.isExpired(elemName, self.convertToDate(element.LastDateModified))) + { + self.set(elemName, null); + } + }); + } + }); + } + }; + + return Cache; + }); \ No newline at end of file diff --git a/js/classes/cachedCollection.js b/js/classes/cachedCollection.js new file mode 100644 index 0000000..3ab0865 --- /dev/null +++ b/js/classes/cachedCollection.js @@ -0,0 +1,54 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +define(['jquery', 'underscore', 'backbone', 'classes/cache'], + function ($, _, Backbone, Cache) { + + var CachedCollection = Backbone.Collection.extend({ + cacheName: null, + fetch: function (options) { + options = _.defaults(options || {}, {parse: true}); + var deferred = new $.Deferred(), + self = this; + + var cachedItem = Cache.get(this.cacheName); + + if (cachedItem !== null) + { + window.setTimeout(function () { + self[options.reset ? 'reset' : 'set'](false, options); + deferred.resolve(cachedItem); + self.push(cachedItem); + self.trigger('sync', self, false, options); + if (_.isFunction(options.success)) { + options.success(self, false, options); + } + return deferred; + }, 0); + } + else + { + // Delegate to the actual fetch method and store the attributes in the cache + var jqXHR = Backbone.Collection.prototype.fetch.apply(this, arguments); + // resolve the returned promise when the AJAX call completes + jqXHR.done(_.bind(deferred.resolve, this, this)) + // Set the new data in the cache + .done(_.bind( + function (a, b, c) { + Cache.set(self.cacheName, a); + } + , null, this, options)) + // Reject the promise on fail + .fail(_.bind(deferred.reject, this, this)); + + deferred.abort = jqXHR.abort; + // return a promise which provides the same methods as a jqXHR object + return deferred; + } + } + }); + return CachedCollection; + }); diff --git a/js/classes/profile.js b/js/classes/profile.js index 5ecb682..0ce5b68 100644 --- a/js/classes/profile.js +++ b/js/classes/profile.js @@ -73,12 +73,12 @@ define(['jquery', 'backbone'], }, isGPlusConnected: function(){ - if(this.data.type == "gplus") + if(this.data.type === "google") return true; return false; }, isFbConnected: function(){ - if(this.data.type == "fb") + if(this.data.type === "facebook") return true; return false; }, diff --git a/js/common.min.js b/js/common.min.js new file mode 100644 index 0000000..7afff58 --- /dev/null +++ b/js/common.min.js @@ -0,0 +1,3 @@ +var hello=function(e){return hello.use(e)};hello.utils={extend:function(e){for(var t=Array.prototype.slice.call(arguments,1),n=0;n