-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
98 lines (83 loc) · 3.17 KB
/
index.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
module.exports = function picker(config) {
return new Promise(( resolve, reject ) => {
var developerKey = config.developerKey;
// The Client ID obtained from the Google API Console. Replace with your own Client ID.
var clientId = config.clientId;
// Replace with your own project number from console.developers.google.com.
// See "Project number" under "IAM & Admin" > "Settings"
var appId = config.appId;
// Scope to use to access user's Drive items.
var scope = config.scope || ['https://www.googleapis.com/auth/drive.file'];
var mimeTypes = config.mimeTypes || "";
var pickerApiLoaded = false;
var oauthToken;
// Use the Google API Loader script to load the google.picker script.
var loadPicker = function() {
gapi.load('auth', {'callback': onAuthApiLoad});
gapi.load('picker', {'callback': onPickerApiLoad});
}
loadPickerAPIScript();
function loadPickerAPIScript() {
const src = `https://apis.google.com/js/api.js?key=${developerKey}`;
let script = document.body.querySelector( `script[src="${src}"]` );
if ( script ) {
resolve( loadPicker );
} else {
script = document.createElement('script');
script.src = src;
script.async = true;
script.defer = true;
script.onload = () => {
resolve( loadPicker );
};
script.onerror = reject;
document.body.appendChild( script );
}
}
function onAuthApiLoad() {
window.gapi.auth.authorize(
{
'client_id': clientId,
'scope': scope,
'immediate': false
},
handleAuthResult );
}
function onPickerApiLoad() {
pickerApiLoaded = true;
createPicker();
}
function handleAuthResult(authResult) {
if (authResult && !authResult.error) {
oauthToken = authResult.access_token;
createPicker();
}
}
// Create and render a Picker object for searching images.
function createPicker() {
if(this.picker) {
this.picker.setVisible(true);
} else if (pickerApiLoaded && oauthToken) {
var view = new google.picker.View(google.picker.ViewId.DOCS);
view.setMimeTypes(mimeTypes);
var picker = new google.picker.PickerBuilder()
.enableFeature(google.picker.Feature.NAV_HIDDEN)
.enableFeature(google.picker.Feature.MULTISELECT_ENABLED)
.setAppId(appId)
.setOAuthToken(oauthToken)
.addView(view)
.addView(new google.picker.DocsUploadView())
.setDeveloperKey(developerKey)
.setCallback(pickerCallback)
.build();
picker.setVisible(true);
this.picker = picker;
}
}
function pickerCallback(data) {
if (data.action == google.picker.Action.PICKED) {
config.onpick( data );
}
}
})
};