-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogle-drive-picker.min.js
2 lines (2 loc) · 1.7 KB
/
google-drive-picker.min.js
1
2
/* GoogleDrivePicker v0.0.2, @license MIT, (c) 2015 Bennett Goble */
(function(){function i(i){i=i||{};this.opts=i;this._gapi=i.gapi||window.gapi;this._oauthToken=null;this._initialized=false;this._attempts=0}i.prototype._initialize=function(){this._attempts++;return Promise.all([this._initializeAuth(),this._loadPicker()]).then(this._setInitialized.bind(this))};i.prototype._initializeAuth=function(){return this._loadAuth().then(this._authorize.bind(this))};i.prototype._loadAuth=function(){return new Promise(function(i,t){this._gapi.load("auth",{callback:i})}.bind(this))};i.prototype._authorize=function(){return new Promise(function(i,t){this._gapi.auth.authorize({client_id:this.opts.clientId,scope:["https://www.googleapis.com/auth/drive.readonly"],immediate:false},function(t){this._oauthToken=t.access_token;i()}.bind(this))}.bind(this))};i.prototype._loadPicker=function(){return new Promise(function(i,t){this._gapi.load("picker",{callback:i})}.bind(this))};i.prototype._setInitialized=function(){this._initialized=true};i.prototype._createPicker=function(i,t){var e=new google.picker.PickerBuilder;if(i){i(e)}else{e.addView(google.picker.ViewId.DOCS).setLocale("en")}e.setOAuthToken(this._oauthToken).setDeveloperKey(this.opts.apiKey).setCallback(t);return e.build()};i.prototype.pick=function(i){i=i||{};i.config=i.config||this.opts.config;if(!this._initialized){if(this._attempts>2){throw"Unable to initialize Google Picker API, attempted 3 times"}return this._initialize().then(function(){this.pick.apply(this,arguments)}.bind(this))}return new Promise(function(t,e){var n=this._createPicker(i.config,function(i){if(i.action==google.picker.Action.PICKED){t(i)}});n.setVisible(true)}.bind(this))};this.GoogleDrivePicker=i})();