-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogle-drive-picker.js
151 lines (132 loc) · 4.06 KB
/
google-drive-picker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/* GoogleDrivePicker v0.0.2, @license MIT, (c) 2015 Bennett Goble */
(function() {
/**
* Creates an instance of GoogleDrivePicker
*
* @constructor
* @this {GoogleDrivePicker}
* @param {Object} opts - Options for Picker
* @param {string} opts.apiKey - The API Key from Google's API console
* @param {string} opts.clientId - The client ID from Google's API console
* @param {Function} opts.config - Configuration handler
*/
function GoogleDrivePicker(opts) {
opts = opts || {};
this.opts = opts;
this._gapi = opts.gapi || window.gapi;
this._oauthToken = null;
this._initialized = false;
this._attempts = 0; // Initialization attempts
}
/**
* Primary private initialization method, loads APIs
*
* @returns {Object} Promise
*/
GoogleDrivePicker.prototype._initialize = function() {
this._attempts++;
return Promise.all([this._initializeAuth(), this._loadPicker()]).then(this._setInitialized.bind(this));
};
/**
* Load auth API then authorize
*
* @returns {Object} Promise
*/
GoogleDrivePicker.prototype._initializeAuth = function() {
return this._loadAuth().then(this._authorize.bind(this));
}
/**
* Load auth API
*
* @returns {Object} Promise
*/
GoogleDrivePicker.prototype._loadAuth = function() {
return new Promise(function(resolve, reject) {
this._gapi.load('auth', {callback: resolve});
}.bind(this));
};
/**
* Tell the API to ask for Read-only Drive permissions when Picker opens, called after _loadAuth
*
* @returns {Object} Promise
*/
GoogleDrivePicker.prototype._authorize = function() {
return new Promise(function(resolve, reject) {
this._gapi.auth.authorize({
client_id: this.opts.clientId,
scope: ['https://www.googleapis.com/auth/drive.readonly'],
immediate: false
}, function(data) {
this._oauthToken = data.access_token;
resolve();
}.bind(this));
}.bind(this));
};
/**
* Load picker API
* @returns {Object} Promise
*/
GoogleDrivePicker.prototype._loadPicker = function() {
return new Promise(function(resolve, reject) {
this._gapi.load('picker', {callback: resolve});
}.bind(this));
};
/**
* APIs have been loaded and initialized
*/
GoogleDrivePicker.prototype._setInitialized = function() {
this._initialized = true;
}
/**
* Create a picker
*
* @param {string} locale - Google drive locale
* @param {string} view - Google drive view
* @param {Function} configFn - Configuration function
* @param {Function} cb - Callback
* @returns {Object}
*/
GoogleDrivePicker.prototype._createPicker = function(configFn, cb) {
var builder = new google.picker.PickerBuilder();
if (configFn) {
// Allow manual configuration by yielding the builder to configFn
configFn(builder);
}
else {
// Defaults
builder.addView(google.picker.ViewId.DOCS).setLocale('en');
}
builder.setOAuthToken(this._oauthToken)
.setDeveloperKey(this.opts.apiKey)
.setCallback(cb);
return builder.build();
};
/**
* Choose from Google Drive
*
* @param {Object} opts - Options (optional)
* @param {Function} opts.config - Configuration handler
* @returns {Object} Promise
*/
GoogleDrivePicker.prototype.pick = function(opts) {
opts = opts || {};
opts.config = opts.config || this.opts.config;
// Initialize and try again
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(resolve, reject) {
var picker = this._createPicker(opts.config, function(data) {
if (data.action == google.picker.Action.PICKED) {
resolve(data);
}
});
picker.setVisible(true);
}.bind(this));
};
this.GoogleDrivePicker = GoogleDrivePicker;
}());