-
Notifications
You must be signed in to change notification settings - Fork 2
/
gapiLoader.js
190 lines (176 loc) · 5.22 KB
/
gapiLoader.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
let gapiPickerInited = false;
let gapiClientInited = false;
let gisInited = false;
// set this to the right server origin when pushed to Github
const SERVER_ORIGIN = "https://mra9407.pythonanywhere.com";
let tokenClient;
let pickedFile;
let fileId;
/**
* Callback after api script is loaded.
*/
function gapiLoaded(libraryName, callback) {
gapi.load(libraryName, callback);
}
/**
* Callback after Google Identity Services are loaded.
*/
function gisLoaded() {
axios
.get(`${SERVER_ORIGIN}/get_gis_credentials`)
.then((rsp) => {
let credentials = rsp.data;
tokenClient = google.accounts.oauth2.initTokenClient({
client_id: credentials.client_id,
scope: credentials.scope.join(' '),
callback: "", // defined later
});
gisInited = true;
maybeEnableAuthButton();
})
.catch((err) => {
console.log(err);
});
}
/**
* Callback after the API client is loaded.
*/
function initializeGapiClient() {
axios
.get(`${SERVER_ORIGIN}/get_gapi_credentials`)
.then((rsp) => {
let credentials = rsp.data;
gapi.client
.init({
apiKey: credentials.apiKey,
discoveryDocs: credentials.discoveryDocs,
})
.then(() => {
gapiClientInited = true;
maybeEnableAuthButton();
})
.catch((err) => {
console.log(err);
});
})
.catch((err) => {
console.log(err);
});
}
function initializeGapiPicker() {
gapiPickerInited = true;
}
function showPicker(accessToken, callback) {
axios
.get(`${SERVER_ORIGIN}/get_gapi_credentials`)
.then((rsp) => {
let credentials = rsp.data;
let view = new google.picker.DocsView(google.picker.ViewId.DOCS).setParent('root').setIncludeFolders(true);
// view.setMimeTypes("text/plain");
view.setMode(google.picker.DocsViewMode.LIST);
let picker = new google.picker.PickerBuilder()
.addView(view)
.setOAuthToken(accessToken)
.setDeveloperKey(credentials.apiKey)
.setCallback(callback)
.setAppId((credentials.clientId).split('-')[0])
.build();
picker.setVisible(true);
})
.catch((err) => {
console.log(err);
});
}
function showSaveAsPicker(accessToken, callback) {
axios
.get(`${SERVER_ORIGIN}/get_gapi_credentials`)
.then((rsp) => {
let credentials = rsp.data;
let view = new google.picker.DocsView(google.picker.ViewId.FOLDERS)
.setParent('root')
.setIncludeFolders(true)
.setSelectFolderEnabled(true);
view.setMimeTypes('application/vnd.google-apps.folder');
view.setMode(google.picker.DocsViewMode.LIST);
let picker = new google.picker.PickerBuilder()
.addView(view)
.setOAuthToken(accessToken)
.setDeveloperKey(credentials.apiKey)
.setCallback(callback)
.setAppId((credentials.clientId).split('-')[0])
.build();
picker.setVisible(true);
})
.catch((err) => {
console.log(err);
});
}
function pickerCallback(data) {
// if the user picked a file
// need to check if the file is a conllu file
if (data[google.picker.Response.ACTION] == google.picker.Action.PICKED) {
let doc = data[google.picker.Response.DOCUMENTS][0];
let fileNameParts = doc.name.split(".");
if (
fileNameParts[fileNameParts.length - 1] !== "conllu" &&
fileNameParts[fileNameParts.length - 1] !== "conllx"
) {
alert(
"File does not end with the .conllu/conllx extension, please upload a ConllU/X file."
);
return;
}
// need to add extension for conllx files as well
let url =
"https://www.googleapis.com/drive/v3/files/" + doc.id + "?alt=media";
axios
.get(url, {
headers: {
Authorization: "Bearer " + gapi.client.getToken().access_token,
},
})
.then((rsp) => {
// show name of picked file
// set global pickedFile variable
document.getElementById("picked_filename").innerHTML = doc.name;
pickedFile = new File([rsp.data], doc.name, { type: doc.mimeType });
fileId = doc.id;
})
.catch((err) => {
console.log(err);
alert("Failed to retrieve file!");
});
}
}
function saveAsPickerCallback(data) {
if (data[google.picker.Response.ACTION] == google.picker.Action.PICKED) {
let doc = data[google.picker.Response.DOCUMENTS][0];
saveTreeRemote(remoteLocation=doc.id, saveType="saveAs")
}
}
function enableBrowseButton() {
let browseBtn = document.getElementById("browse_btn");
browseBtn.style.display = "inline-block";
browseBtn.onclick = () => {
showPicker(gapi.client.getToken().access_token, pickerCallback);
};
}
function saveAsPicker() {
let browseBtn = document.getElementById("save_as_option");
browseBtn.onclick = () => {
showSaveAsPicker(gapi.client.getToken().access_token, saveAsPickerCallback);
};
}
/**
* Enables user interaction after all libraries are loaded.
*/
function maybeEnableAuthButton() {
if (gapiClientInited && gapiPickerInited && gisInited && !isAuthenticated) {
$(".toolbar [id='auth_btn']").show();
}
}
function maybeEnableSaveRemoteButton() {
if (gapiClientInited && gisInited) {
$("#save_remote").show();
}
}