forked from maximepvrt/angular-google-gapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
angular-google-gapi.min.js
7 lines (6 loc) · 4.42 KB
/
angular-google-gapi.min.js
1
2
3
4
5
6
7
/**
* An AngularJS module for use all Google Apis and your Google Cloud Endpoints
* @version v0.1.3
* @link https://github.com/maximepvrt/angular-google-gapi
*/
angular.module("angular-google-gapi",[]),angular.module("angular-google-gapi").factory("GClient",["$document","$q","$timeout","$interval","$window",function(e,n,t,o,i){function r(o){var i=n.defer(),r=e[0].createElement("script");return r.onload=function(e){t(function(){i.resolve(e)})},r.onerror=function(e){t(function(){i.reject(e)})},r.src=o,e[0].body.appendChild(r),i.promise}function a(e){r(c).then(function(){var n=function(e){void 0!=i.gapi.client&&(e(),o.cancel(t))};n(e);var t=o(function(){n(e)},10);u=!0})}var u=!1,c="https://apis.google.com/js/client.js";return{get:function(e){u?e():a(e)}}}]),angular.module("angular-google-gapi").factory("GData",["$rootScope",function(e){e.gapi={};var n=!1,t=null;return{isLogin:function(t){return 0==arguments.length?n:(n=t,void(e.gapi.login=t))},getUser:function(n){return 0==arguments.length?t:(t=n,void(e.gapi.user=n))}}}]),angular.module("angular-google-gapi").factory("GAuth",["$rootScope","$q","GClient","GApi","GData","$interval","$window","$location",function(e,n,t,o,i,r,a){function u(e){if(0==s){var n=arguments.length;t.get(function(){a.gapi.client.load("oauth2","v2",function(){s=!0,1==n&&e()})})}else e()}function c(e,n){u(function(){a.gapi.auth.authorize({client_id:g,scope:p,immediate:e,response_type:d},n)})}function l(){function e(n){if("https://accounts.google.com"===n.origin){var i=JSON.parse(n.data);a.removeEventListener("message",e),i=t(i.a[0],"code"),void 0==i?o.reject():o.resolve(i)}}function t(e,n){n=n.replace(/[[]/,"[").replace(/[]]/,"]");var t=n+"=([^&#]*)",o=new RegExp(t),i=o.exec(e);return null==i?void 0:i[1]}var o=n.defer(),i=$location.protocol+"//"+$location.hostname;""!=$location.port&&(i=i+":"+$location.port);a.open("https://accounts.google.com/o/oauth2/auth?scope="+encodeURI(p)+"&redirect_uri=postmessage&response_type=code&client_id="+g+"&access_type=offline&approval_prompt=force&origin="+i,null,"width=800, height=600");return a.addEventListener("message",e),o.promise}function f(){var e=n.defer();return a.gapi.client.oauth2.userinfo.get().execute(function(n){if(n.code)e.reject();else{i.isLogin(!0),o.executeCallbacks();var t={};t.email=n.email,t.picture=n.picture,t.id=n.id,t.name=void 0==n.name?n.email:n.name,t.link=n.link,i.getUser(t),e.resolve()}}),e.promise}var g,s=!1,p="https://www.googleapis.com/auth/userinfo.email",d="token id_token";return{setClient:function(e){g=e},setScope:function(e){p=e},load:function(e){var n=arguments.length;t.get(function(){a.gapi.client.load("oauth2","v2",function(){1==n&&e()})})},checkAuth:function(){var e=n.defer();return c(!0,function(){f().then(function(){e.resolve()},function(){e.reject()})}),e.promise},login:function(){var e=n.defer();return c(!1,function(){f().then(function(){e.resolve()},function(){e.reject()})}),e.promise},setToken:function(e){var t=n.defer();return u(function(){a.gapi.auth.setToken(e),f().then(function(){t.resolve()},function(){t.reject()})}),t.promise},getToken:function(){var e=n.defer();return u(function(){e.resolve(a.gapi.auth.getToken())}),e.promise},logout:function(){var e=n.defer();return u(function(){a.gapi.auth.setToken(null),i.isLogin(!1),i.getUser(null),e.resolve()}),e.promise},offline:function(){var e=n.defer();return l().then(function(n){e.resolve(n)},function(){e.reject()}),e.promise}}}]),angular.module("angular-google-gapi").factory("GApi",["$q","GClient","GData","$window",function(e,n,t,o){function i(e,n,t,o,i){var r={};r.api=e,r.apiLoad=!1,r.method=n,r.params=t,r.auth=o,r.deferred=i,f.push(r)}function r(e,t,i){n.get(function(){o.gapi.client.load(e,t,function(){console.log(e+" "+t+" api loaded"),l.push(e),a(e)},i)})}function a(e){for(var n=e,o=0;o<f.length;o++){var i=f[o];i.api!=n&&!i.apiLoad||0!=i.auth&&1!=t.isLogin()?i.api==n&&(f[o].apiLoad=!0):(u(i.api,i.method,i.params,i.deferred),o>-1&&f.splice(o--,1))}}function u(e,n,t,i){for(var r=n.split("."),e=o.gapi.client[e],a=0;a<r.length;a++)e=e[r[a]];e(t).execute(function(e){e.error?i.reject(e):i.resolve(e)})}function c(n,t,o,r){var a=e.defer();return l.indexOf(n)>-1?u(n,t,o,a):i(n,t,o,r,a),a.promise}var l=[],f=[];return{executeCallbacks:function(){a()},load:function(e,n,t){r(e,n,t)},execute:function(e,n,t){return 3==arguments.length?c(e,n,t,!1):2==arguments.length?c(e,n,null,!1):void 0},executeAuth:function(e,n,t){return 3==arguments.length?c(e,n,t,!0):2==arguments.length?c(e,n,null,!0):void 0}}}]);