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(new Date).getTime()/1e3){var f=t.diff(c.scope||[],n.qs.state.scope||[]);if(0===f.length)return e.emitAfter("complete success login",{unchanged:!0,network:n.network,authResponse:c}),e}if("page"===i.display&&i.page_uri&&(n.qs.state.page_uri=t.url(i.page_uri).href),"login"in a&&"function"==typeof a.login&&a.login(n),("token"!==u||parseInt(a.oauth.version,10)<2||"none"===i.display&&a.oauth.grant&&c&&c.refresh_token)&&(n.qs.state.oauth=a.oauth,n.qs.state.oauth_proxy=i.oauth_proxy),n.qs.state=JSON.stringify(n.qs.state),1===parseInt(a.oauth.version,10)?o=t.qs(i.oauth_proxy,n.qs):"none"===i.display&&a.oauth.grant&&c&&c.refresh_token?(n.qs.refresh_token=c.refresh_token,o=t.qs(i.oauth_proxy,n.qs)):o=t.qs(a.oauth.auth,n.qs),e.emit("notice","Authorization URL "+o),"none"===i.display)t.iframe(o);else if("popup"===i.display)var p=t.popup(o,l,i.window_width||500,i.window_height||550),m=setInterval(function(){if((!p||p.closed)&&(clearInterval(m),!r)){var t={code:"cancelled",message:"Login has been cancelled"};p||(t={code:"blocked",message:"Popup was blocked"}),e.emit("complete failed error",{error:t,network:n.network})}},100);else window.location=o;return e},logout:function(){var e=this.use(),t=e.utils,n=t.args({name:"s",options:"o",callback:"f"},arguments);if(n.options=n.options||{},e.on("complete",n.callback),n.name=n.name||e.settings.default_service,!n.name||n.name in e.services)if(n.name&&t.store(n.name)){var o=function(t){e.utils.store(n.name,""),e.emitAfter("complete logout success auth.logout auth",hello.utils.merge({network:n.name},t||{}))},i={};if(n.options.force){var a=e.services[n.name].logout;if(a)if("function"==typeof a&&(a=a(o)),"string"==typeof a)t.iframe(a),i.force=null,i.message="Logout success on providers site was indeterminate";else if(void 0===a)return e}o(i)}else if(n.name)e.emitAfter("complete error",{error:{code:"invalid_session",message:"There was no session to remove"}});else{for(var r in e.services)e.services.hasOwnProperty(r)&&e.logout(r);e.service(!1)}else e.emitAfter("complete error",{error:{code:"invalid_network",message:"The network was unrecognized"}});return e},getAuthResponse:function(e){return e=e||this.settings.default_service,e&&e in this.services?this.utils.store(e)||null:(this.emit("complete error",{error:{code:"invalid_network",message:"The network was unrecognized"}}),null)},events:{}}),hello.utils.extend(hello.utils,{qs:function(e,t){if(t){var n;for(var o in t)if(e.indexOf(o)>-1){var i="[\\?\\&]"+o+"=[^\\&]*";n=new RegExp(i),e=e.replace(n,"")}}return e+(this.isEmpty(t)?"":(e.indexOf("?")>-1?"&":"?")+this.param(t))},param:function(e){var t,n,o={};if("string"==typeof e){if(n=e.replace(/^[\#\?]/,"").match(/([^=\/\&]+)=([^\&]+)/g))for(var i=0;i-1&&"string"===i||e[a].indexOf("o")>-1&&"object"===i||e[a].indexOf("i")>-1&&"number"===i||e[a].indexOf("a")>-1&&"object"===i||e[a].indexOf("f")>-1&&"function"===i))n[a]=t[o++];else if("string"==typeof e[a]&&e[a].indexOf("!")>-1)return!1;return n},url:function(e){if(e){if(window.URL&&URL instanceof Function&&0!==URL.length)return new URL(e,window.location);var t=document.createElement("a");return t.href=e,t}return window.location},diff:function(e,t){for(var n=[],o=0;o0)return!1;if(e&&0===e.length)return!0;for(var t in e)if(e.hasOwnProperty(t))return!1;return!0},objectCreate:function(){function e(){}return Object.create?Object.create:function(t){if(1!=arguments.length)throw new Error("Object.create implementation only accepts one parameter.");return e.prototype=t,new e}}(),Event:function(){var e=/[\s\,]+/;return this.parent={events:this.events,findEvents:this.findEvents,parent:this.parent,utils:this.utils},this.events={},this.on=function(t,n){if(n&&"function"==typeof n)for(var o=t.split(e),i=0;i-1)for(var a=0;a0&&u.append(l,o[l].files[0]):o[l]instanceof Blob?u.append(l,o[l],o.name):u.append(l,o[l]));o=u}if(r.open(e,t,!0),s&&("responseType"in r?r.responseType=s:r.overrideMimeType("text/plain; charset=x-user-defined")),n)for(l in n)r.setRequestHeader(l,n[l]);return r.send(o),r},jsonp:function(e,t,n,o){var i,a,r=this,s=0,l=document.getElementsByTagName("head")[0],u={error:{message:"server_error",code:"server_error"}},c=function(){s++||window.setTimeout(function(){t(u),l.removeChild(a)},0)};n=r.globalEvent(function(e){return u=e,!0},n),e=e.replace(new RegExp("=\\?(&|$)"),"="+n+"$1"),a=r.append("script",{id:n,name:n,src:e,async:!0,onload:c,onerror:c,onreadystatechange:function(){/loaded|complete/i.test(this.readyState)&&c()}}),window.navigator.userAgent.toLowerCase().indexOf("opera")>-1&&(i=r.append("script",{text:"document.getElementById('"+cb_name+"').onerror();"}),a.async=!1),o&&window.setTimeout(function(){u={error:{message:"timeout",code:"timeout"}},c()},o),l.appendChild(a),i&&l.appendChild(i)},post:function(e,t,n,o,i,a){var r,s=this,l=document,u=null,c=[],d=0,f=null,p=0,m=function(e){p++||o(e)};s.globalEvent(m,i);var h;try{h=l.createElement('