-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.js
More file actions
326 lines (299 loc) · 9.77 KB
/
main.js
File metadata and controls
326 lines (299 loc) · 9.77 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
let handleClientLoad=( function(){
var GoogleAuth;
var SCOPE = 'https://www.googleapis.com/auth/calendar';
function handleClientLoad() {
// Load the API's client and auth2 modules.
// Call the initClient function after the modules load.
gapi.load('client:auth2', initClient);
}
function initClient() {
// Retrieve the discovery document for version 3 of Google Drive API.
// In practice, your app can retrieve one or more discovery documents.
var discoveryUrl = 'https://www.googleapis.com/discovery/v1/apis/calendar/v3/rest';
$.ajax({
dataType: "json",
url: discoveryUrl,
success: (responce)=>{
discoveryAPI=responce;
initializeForms();
}
});
// Initialize the gapi.client object, which app uses to make API requests.
// Get API key and client ID from API Console.
// 'scope' field specifies space-delimited list of access scopes.
gapi.client.init({
'apiKey': 'AIzaSyAIeh9UbKwZvh2rDzIHneD0afADrH0GFSI',
'discoveryDocs': [discoveryUrl],
'clientId': '873022610329-t3o1o1h6oj80rqpai86p94t1h849177d.apps.googleusercontent.com',
'scope': SCOPE
}).then(function () {
GoogleAuth = gapi.auth2.getAuthInstance();
// Listen for sign-in state changes.
GoogleAuth.isSignedIn.listen(updateSigninStatus);
// Handle initial sign-in state. (Determine if user is already signed in.)
var user = GoogleAuth.currentUser.get();
setSigninStatus();
// Call handleAuthClick function when user clicks on
// "Sign In/Authorize" button.
$('#sign-in-or-out-button').click(function() {
handleAuthClick();
});
$('#revoke-access-button').click(function() {
revokeAccess();
});
});
}
function handleAuthClick() {
if (GoogleAuth.isSignedIn.get()) {
// User is authorized and has clicked 'Sign out' button.
GoogleAuth.signOut();
} else {
// User is not signed in. Start Google auth flow.
GoogleAuth.signIn();
}
}
function revokeAccess() {
GoogleAuth.disconnect();
}
var colorSelect=null
function setSigninStatus(isSignedIn) {
var user = GoogleAuth.currentUser.get();
var isAuthorized = user.hasGrantedScopes(SCOPE);
if (isAuthorized) {
$('#sign-in-or-out-button').html('Sign out');
$('#revoke-access-button').css('display', 'inline-block');
$('#auth-status').html('You are currently signed in and have granted ' +
'access to this app.');
populateCalendarList();
gapi.client.calendar.colors.get()
.then(resp=>{
colorSelect=document.createElement('select')
let op=document.createElement('option')
op.innerHTML='unchanged'
colorSelect.appendChild(op)
for(let color in resp.result.event){
let op=document.createElement('option')
op.style.color=resp.result.event[color].background
op.value=color
op.innerHTML='⬤'
colorSelect.appendChild(op)
}
colorSelect.addEventListener('change',updateColorOfSelectOnChange)
for(let input of document.querySelectorAll('input[name=colorId]')){
var newSel=colorSelect.cloneNode(true)
newSel.name=input.name
newSel.addEventListener('change',updateColorOfSelectOnChange)
newSel.selectedIndex=-1;
input.replaceWith(newSel)
}
})
} else {
$('#sign-in-or-out-button').html('Sign In/Authorize');
$('#revoke-access-button').css('display', 'none');
$('#auth-status').html('You have not authorized this app or you are ' +
'signed out.');
}
}
function updateSigninStatus(isSignedIn) {
setSigninStatus();
}
var populateCalendarList=function(){
gapi.client.calendar.calendarList.list()
.then(function(responce){
if(!responce.result){
return;
}
var sel=document.forms.search['calendarId'];
for(var i in responce.result.items){
var opt=document.createElement('option');
opt.text=responce.result.items[i].summary;
opt.value=responce.result.items[i].id;
sel.appendChild(opt);
}
});
}
var searchResults=null;
document.forms.search.addEventListener('submit',function(e){
var obj=prep_form_to_send(e,discoveryAPI.resources.events.methods.list.parameters)
gapi.client.calendar.events.list(obj).then(gotSearchResults.bind(null,obj),console.error);
return false;
})
var patch_body=null
document.forms.patch.addEventListener('submit',function(e){
if(remaining_to_patch.length){
e.preventDefault();
console.error('attempted patch while already patching')
return false;
}
patch_body=prep_form_to_send(e,discoveryAPI.schemas.Event.properties)
remaining_to_patch=searchResults.responce.items.slice()
progress_bar.max=remaining_to_patch.length
progress_bar.value=+progress_bar.max-remaining_to_patch.length
document.body.appendChild(progress_bar)
doPatch()
return false;
})
var progress_bar=document.createElement('progress')
var remaining_to_patch=[]
function doPatch(){
let next=remaining_to_patch.pop()
progress_bar.value=+progress_bar.max-remaining_to_patch.length
if(next){
return gapi.client.calendar.events.patch({
calendarId:searchResults.calendarId,
eventId:next.id
},patch_body)
.then(doPatch,console.error)
}
progress_bar.remove()
}
function prep_form_to_send(e,fields){
e.preventDefault();
var obj={}
var types={};
$(e.target).serializeArray().forEach(function(item){
if(item.value){
let format=fields[item.name].format;
switch(format){
case "date-time":
obj[item.name]=(new Date(item.value)).toISOString();
break;
case undefined:
obj[item.name]=item.value;
break;
default:
types[format]=true;
obj[item.name]=item.value;
}
}
})
if(types.length){
console.warn(types)
}
return obj
}
var searchResults=null;
function gotSearchResults(searchobj,results){
searchResults=searchobj
searchResults.responce=results.result;
document.getElementById('found_count').innerText=searchResults.responce.items.length
}
var initializeForms=function(){
var addStuff=createFormTable(discoveryAPI.resources.events.methods.list.parameters,listEventParams);
document.forms.search.replaceChild(addStuff,document.forms.search.calendarId.nextElementSibling);
var edit=createFormTable(discoveryAPI.schemas.Event.properties,editEventParams)
document.forms.patch.replaceChild(edit,document.forms.patch.firstElementChild)
}
var listEventParams={
calendarId:"",
orderBy:"",
q:"Search Text",
showDeleted:"Include Deleted",
showHiddenInvitations:"Include Hidden Invitations",
timeMax:"Starts Before",
timeMin:"Ends After",
updatedMin:"Modified Since"
}
var editEventParams={
id:"",
updated:"",
created:"",
privateCopy:"",
locked:"",
recurringEventId:"",
kind:"",
etag:"",
organizer:"",
iCalUID:"",
originalStartTime:"",
creator:"",
start:"",
end:"",
summary:"Summary",
description:"Description",
location:"Location",
colorId:"Color",
visibility:"Visibility"
}
var autoPrefix=0;
var createFormTable=function(parameters,labels,prefix){
prefix=prefix|| "p"+(autoPrefix++);
var mainDiv=document.createElement('div');
var table=document.createElement('table');
for(var name in labels){
if(parameters[name] && labels[name]){
table.appendChild(createRow(name,parameters[name],labels[name],prefix));
}
}
mainDiv.appendChild(table);
mainDiv.appendChild(document.createElement('label'));
mainDiv.lastChild.innerText="Show Advanced Parameters";
mainDiv.lastChild.htmlFor=prefix+"ShowAllCheckbox";
mainDiv.appendChild(document.createElement('input'));
mainDiv.lastChild.type='checkbox';
mainDiv.lastChild.className='show-hide-next';
mainDiv.lastChild.id=prefix+"ShowAllCheckbox";
table=document.createElement('table');
for(var name in parameters){
if(typeof labels[name] == 'undefined'){
table.appendChild(createRow(name, parameters[name],name,prefix));
}
}
mainDiv.appendChild(table);
return mainDiv;
}
var createRow=function(name, parameter,label,prefix){
var row=document.createElement('tr');
var cell=document.createElement('td');
cell.appendChild(document.createElement('input'));
switch(parameter.type){
case "boolean":
cell.firstChild.type='checkbox';
break;
case "integer":
cell.firstChild.type='number';
if(parameter.min){
cell.firstChild.min=parameter.min;
}
if(parameter.max){
cell.firstChild.max=parameter.max;
}
break;
case "string":
if(parameter.enum){
cell.replaceChild(document.createElement('select'),cell.firstChild);
cell.firstChild.appendChild(document.createElement('option'));
for(var i in parameter.enum){
cell.firstChild.appendChild(document.createElement('option'));
cell.firstChild.lastChild.innerText=parameter.enum[i];
}
}else if(parameter.format){
switch(parameter.format){
case "date-time":
cell.firstChild.type="datetime-local";
}
}
break;
default:
console.warn("Unknown Parameter type:"+parameter.type+' for '+name);
}
cell.firstChild.id=prefix+name;
cell.firstChild.name=name;
cell.title=parameter.description;
row.appendChild(cell);
cell=document.createElement('td');
cell.appendChild(document.createElement('label'));
cell.firstChild.innerText=label;
cell.firstChild.htmlFor=prefix+name;
row.insertBefore(cell,row.firstChild);
return row;
}
function updateColorOfSelectOnChange(e){
let sel=e.target
sel.style.color=sel.selectedOptions[0].style.color
if(sel.selectedIndex==0){
sel.selectedIndex=-1;
}
}
return handleClientLoad;
})();