diff --git a/dist/google-picker.js b/dist/google-picker.js index 38b04b8..644f2c6 100644 --- a/dist/google-picker.js +++ b/dist/google-picker.js @@ -61,8 +61,8 @@ /** * Load required modules */ - function instanciate () { - gapi.load('auth', { 'callback': onApiAuthLoad }); + function instantiate () { + gapi.load('auth2', { 'callback': onApiAuthLoad }); gapi.load('picker'); } @@ -71,17 +71,18 @@ * If user is already logged in, then open the Picker modal */ function onApiAuthLoad () { - var authToken = gapi.auth.getToken(); - - if (authToken) { - handleAuthResult(authToken); - } else { - gapi.auth.authorize({ - 'client_id' : lkGoogleSettings.clientId, - 'scope' : lkGoogleSettings.scopes, - 'immediate' : false - }, handleAuthResult); - } + gapi.auth2.init({ + 'client_id' : lkGoogleSettings.clientId, + 'scope' : lkGoogleSettings.scopes + }) + .then(function (authInstance) { + return authInstance.signIn(); + }) + .then(function (guser) { + var response = guser.getAuthResponse(); + accessToken = response.access_token; + openDialog(); + }); } /** @@ -139,11 +140,9 @@ }); } - gapi.load('auth'); - gapi.load('picker'); - element.bind('click', function (e) { - instanciate(); + /* dynamically load dependencies only on click */ + instantiate(); }); } } diff --git a/dist/google-picker.min.js b/dist/google-picker.min.js index cab4192..643ef3e 100644 --- a/dist/google-picker.min.js +++ b/dist/google-picker.min.js @@ -1 +1 @@ -!function(){angular.module("lk-google-picker",[]).provider("lkGoogleSettings",function(){this.apiKey=null,this.clientId=null,this.scopes=["https://www.googleapis.com/auth/drive"],this.features=["MULTISELECT_ENABLED"],this.views=["DocsView().setIncludeFolders(true)","DocsUploadView().setIncludeFolders(true)"],this.locale="en",this.$get=["$window",function(e){return{apiKey:this.apiKey,clientId:this.clientId,scopes:this.scopes,features:this.features,views:this.views,locale:this.locale,origin:this.origin||e.location.protocol+"//"+e.location.host}}],this.configure=function(e){for(var o in e)this[o]=e[o]}}).directive("lkGooglePicker",["lkGoogleSettings",function(lkGoogleSettings){return{restrict:"A",scope:{onLoaded:"&",onCancel:"&",onPicked:"&"},link:function(scope,element,attrs){function instanciate(){gapi.load("auth",{callback:onApiAuthLoad}),gapi.load("picker")}function onApiAuthLoad(){var e=gapi.auth.getToken();e?handleAuthResult(e):gapi.auth.authorize({client_id:lkGoogleSettings.clientId,scope:lkGoogleSettings.scopes,immediate:!1},handleAuthResult)}function handleAuthResult(e){e&&!e.error&&(accessToken=e.access_token,openDialog())}function openDialog(){var picker=(new google.picker.PickerBuilder).setLocale(lkGoogleSettings.locale).setOAuthToken(accessToken).setCallback(pickerResponse).setOrigin(lkGoogleSettings.origin);lkGoogleSettings.features.length>0&&angular.forEach(lkGoogleSettings.features,function(e,o){picker.enableFeature(google.picker.Feature[e])}),lkGoogleSettings.views.length>0&&angular.forEach(lkGoogleSettings.views,function(view,key){view=eval("new google.picker."+view),picker.addView(view)}),picker.build().setVisible(!0)}function pickerResponse(e){gapi.client.load("drive","v2",function(){e.action==google.picker.Action.LOADED&&scope.onLoaded&&(scope.onLoaded||angular.noop)(),e.action==google.picker.Action.CANCEL&&scope.onCancel&&(scope.onCancel||angular.noop)(),e.action==google.picker.Action.PICKED&&scope.onPicked&&(scope.onPicked||angular.noop)({docs:e.docs}),scope.$apply()})}var accessToken=null;gapi.load("auth"),gapi.load("picker"),element.bind("click",function(e){instanciate()})}}}])}(); \ No newline at end of file +!function(){angular.module("lk-google-picker",[]).provider("lkGoogleSettings",function(){this.apiKey=null,this.clientId=null,this.scopes=["https://www.googleapis.com/auth/drive"],this.features=["MULTISELECT_ENABLED"],this.views=["DocsView().setIncludeFolders(true)","DocsUploadView().setIncludeFolders(true)"],this.locale="en",this.$get=["$window",function(e){return{apiKey:this.apiKey,clientId:this.clientId,scopes:this.scopes,features:this.features,views:this.views,locale:this.locale,origin:this.origin||e.location.protocol+"//"+e.location.host}}],this.configure=function(e){for(var o in e)this[o]=e[o]}}).directive("lkGooglePicker",["lkGoogleSettings",function(lkGoogleSettings){return{restrict:"A",scope:{onLoaded:"&",onCancel:"&",onPicked:"&"},link:function(scope,element,attrs){function instantiate(){gapi.load("auth2",{callback:onApiAuthLoad}),gapi.load("picker")}function onApiAuthLoad(){gapi.auth2.init({client_id:lkGoogleSettings.clientId,scope:lkGoogleSettings.scopes}).then(function(e){return e.signIn()}).then(function(e){var o=e.getAuthResponse();accessToken=o.access_token,openDialog()})}function handleAuthResult(e){e&&!e.error&&(accessToken=e.access_token,openDialog())}function openDialog(){var picker=(new google.picker.PickerBuilder).setLocale(lkGoogleSettings.locale).setOAuthToken(accessToken).setCallback(pickerResponse).setOrigin(lkGoogleSettings.origin);lkGoogleSettings.features.length>0&&angular.forEach(lkGoogleSettings.features,function(e,o){picker.enableFeature(google.picker.Feature[e])}),lkGoogleSettings.views.length>0&&angular.forEach(lkGoogleSettings.views,function(view,key){view=eval("new google.picker."+view),picker.addView(view)}),picker.build().setVisible(!0)}function pickerResponse(e){gapi.client.load("drive","v2",function(){e.action==google.picker.Action.LOADED&&scope.onLoaded&&(scope.onLoaded||angular.noop)(),e.action==google.picker.Action.CANCEL&&scope.onCancel&&(scope.onCancel||angular.noop)(),e.action==google.picker.Action.PICKED&&scope.onPicked&&(scope.onPicked||angular.noop)({docs:e.docs}),scope.$apply()})}var accessToken=null;element.bind("click",function(e){instantiate()})}}}])}(); \ No newline at end of file diff --git a/example/google-picker.min.js b/example/google-picker.min.js index cab4192..643ef3e 100644 --- a/example/google-picker.min.js +++ b/example/google-picker.min.js @@ -1 +1 @@ -!function(){angular.module("lk-google-picker",[]).provider("lkGoogleSettings",function(){this.apiKey=null,this.clientId=null,this.scopes=["https://www.googleapis.com/auth/drive"],this.features=["MULTISELECT_ENABLED"],this.views=["DocsView().setIncludeFolders(true)","DocsUploadView().setIncludeFolders(true)"],this.locale="en",this.$get=["$window",function(e){return{apiKey:this.apiKey,clientId:this.clientId,scopes:this.scopes,features:this.features,views:this.views,locale:this.locale,origin:this.origin||e.location.protocol+"//"+e.location.host}}],this.configure=function(e){for(var o in e)this[o]=e[o]}}).directive("lkGooglePicker",["lkGoogleSettings",function(lkGoogleSettings){return{restrict:"A",scope:{onLoaded:"&",onCancel:"&",onPicked:"&"},link:function(scope,element,attrs){function instanciate(){gapi.load("auth",{callback:onApiAuthLoad}),gapi.load("picker")}function onApiAuthLoad(){var e=gapi.auth.getToken();e?handleAuthResult(e):gapi.auth.authorize({client_id:lkGoogleSettings.clientId,scope:lkGoogleSettings.scopes,immediate:!1},handleAuthResult)}function handleAuthResult(e){e&&!e.error&&(accessToken=e.access_token,openDialog())}function openDialog(){var picker=(new google.picker.PickerBuilder).setLocale(lkGoogleSettings.locale).setOAuthToken(accessToken).setCallback(pickerResponse).setOrigin(lkGoogleSettings.origin);lkGoogleSettings.features.length>0&&angular.forEach(lkGoogleSettings.features,function(e,o){picker.enableFeature(google.picker.Feature[e])}),lkGoogleSettings.views.length>0&&angular.forEach(lkGoogleSettings.views,function(view,key){view=eval("new google.picker."+view),picker.addView(view)}),picker.build().setVisible(!0)}function pickerResponse(e){gapi.client.load("drive","v2",function(){e.action==google.picker.Action.LOADED&&scope.onLoaded&&(scope.onLoaded||angular.noop)(),e.action==google.picker.Action.CANCEL&&scope.onCancel&&(scope.onCancel||angular.noop)(),e.action==google.picker.Action.PICKED&&scope.onPicked&&(scope.onPicked||angular.noop)({docs:e.docs}),scope.$apply()})}var accessToken=null;gapi.load("auth"),gapi.load("picker"),element.bind("click",function(e){instanciate()})}}}])}(); \ No newline at end of file +!function(){angular.module("lk-google-picker",[]).provider("lkGoogleSettings",function(){this.apiKey=null,this.clientId=null,this.scopes=["https://www.googleapis.com/auth/drive"],this.features=["MULTISELECT_ENABLED"],this.views=["DocsView().setIncludeFolders(true)","DocsUploadView().setIncludeFolders(true)"],this.locale="en",this.$get=["$window",function(e){return{apiKey:this.apiKey,clientId:this.clientId,scopes:this.scopes,features:this.features,views:this.views,locale:this.locale,origin:this.origin||e.location.protocol+"//"+e.location.host}}],this.configure=function(e){for(var o in e)this[o]=e[o]}}).directive("lkGooglePicker",["lkGoogleSettings",function(lkGoogleSettings){return{restrict:"A",scope:{onLoaded:"&",onCancel:"&",onPicked:"&"},link:function(scope,element,attrs){function instantiate(){gapi.load("auth2",{callback:onApiAuthLoad}),gapi.load("picker")}function onApiAuthLoad(){gapi.auth2.init({client_id:lkGoogleSettings.clientId,scope:lkGoogleSettings.scopes}).then(function(e){return e.signIn()}).then(function(e){var o=e.getAuthResponse();accessToken=o.access_token,openDialog()})}function handleAuthResult(e){e&&!e.error&&(accessToken=e.access_token,openDialog())}function openDialog(){var picker=(new google.picker.PickerBuilder).setLocale(lkGoogleSettings.locale).setOAuthToken(accessToken).setCallback(pickerResponse).setOrigin(lkGoogleSettings.origin);lkGoogleSettings.features.length>0&&angular.forEach(lkGoogleSettings.features,function(e,o){picker.enableFeature(google.picker.Feature[e])}),lkGoogleSettings.views.length>0&&angular.forEach(lkGoogleSettings.views,function(view,key){view=eval("new google.picker."+view),picker.addView(view)}),picker.build().setVisible(!0)}function pickerResponse(e){gapi.client.load("drive","v2",function(){e.action==google.picker.Action.LOADED&&scope.onLoaded&&(scope.onLoaded||angular.noop)(),e.action==google.picker.Action.CANCEL&&scope.onCancel&&(scope.onCancel||angular.noop)(),e.action==google.picker.Action.PICKED&&scope.onPicked&&(scope.onPicked||angular.noop)({docs:e.docs}),scope.$apply()})}var accessToken=null;element.bind("click",function(e){instantiate()})}}}])}(); \ No newline at end of file diff --git a/src/google-picker.js b/src/google-picker.js index 5268ab9..644f2c6 100644 --- a/src/google-picker.js +++ b/src/google-picker.js @@ -62,7 +62,7 @@ * Load required modules */ function instantiate () { - gapi.load('auth', { 'callback': onApiAuthLoad }); + gapi.load('auth2', { 'callback': onApiAuthLoad }); gapi.load('picker'); } @@ -71,17 +71,18 @@ * If user is already logged in, then open the Picker modal */ function onApiAuthLoad () { - var authToken = gapi.auth.getToken(); - - if (authToken) { - handleAuthResult(authToken); - } else { - gapi.auth.authorize({ - 'client_id' : lkGoogleSettings.clientId, - 'scope' : lkGoogleSettings.scopes, - 'immediate' : false - }, handleAuthResult); - } + gapi.auth2.init({ + 'client_id' : lkGoogleSettings.clientId, + 'scope' : lkGoogleSettings.scopes + }) + .then(function (authInstance) { + return authInstance.signIn(); + }) + .then(function (guser) { + var response = guser.getAuthResponse(); + accessToken = response.access_token; + openDialog(); + }); } /**