-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcalendar.js
117 lines (109 loc) · 3.48 KB
/
calendar.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
var clientId = config.CLIENT_ID;
var apiKey = config.API_KEY;
var userEmail = config.CALENDAR;
var userTimeZone = "New_York";
var maxRows = 2;
var calName = "test";
var scopes = 'https://www.googleapis.com/auth/calendar';
function padNum(num) {
if (num <= 9) {
return "0" + num;
}
return num;
}
function monthString(num) {
if (num === "01") { return "JAN"; }
else if (num === "02") { return "FEB"; }
else if (num === "03") { return "MAR"; }
else if (num === "04") { return "APR"; }
else if (num === "05") { return "MAJ"; }
else if (num === "06") { return "JUN"; }
else if (num === "07") { return "JUL"; }
else if (num === "08") { return "AUG"; }
else if (num === "09") { return "SEP"; }
else if (num === "10") { return "OCT"; }
else if (num === "11") { return "NOV"; }
else if (num === "12") { return "DEC"; }
}
function dayString(num){
if (num == "1") { return "Monday" }
else if (num == "2") { return "Tuesday" }
else if (num == "3") { return "Wednesday" }
else if (num == "4") { return "Thursday" }
else if (num == "5") { return "Friday" }
else if (num == "6") { return "Saturday" }
else if (num == "0") { return "Sunday" }
}
function handleClientLoad() {
gapi.client.setApiKey(apiKey);
checkAuth();
}
function checkAuth() {
gapi.auth.authorize({
client_id: clientId,
scope: scopes,
immediate: true
}, handleAuthResult);
}
function handleAuthResult(authResult) {
if (authResult) {
makeApiCall();
}
}
function makeApiCall() {
var today = new Date(); //today date
gapi.client.load('calendar', 'v3', function() {
var request = gapi.client.calendar.events.list({
'calendarId': userEmail,
'timeZone': userTimeZone,
'singleEvents': true,
'timeMin': today.toISOString(), //gathers only events not happened yet
'maxResults': maxRows,
'orderBy': 'startTime'
});
request.execute(function(resp) {
for (var i = 0; i < resp.items.length; i++) {
var li = document.createElement('li');
var item = resp.items[i];
var classes = [];
var allDay = item.start.date ? true : false;
var startDT = allDay ? item.start.date : item.start.dateTime;
var dateTime = startDT.split("T"); //split date from time
var date = dateTime[0].split("-"); //split yyyy mm dd
var startYear = date[0];
var startMonth = monthString(date[1]);
var startDay = date[2];
var startDateISO = new Date(startMonth + " " + startDay + ", " + startYear + " 00:00:00");
var startDayWeek = dayString(startDateISO.getDay());
if (allDay == true) {
var str = [
'<span class="event">',
item.summary, '<span class="event-info">',
startDayWeek, ' ',
startMonth, ' ',
startDay, ' ',
startYear,
'</span></span>'
];
} else {
var time = dateTime[1].split(":"); //split hh ss etc...
var startHour = time[0];
var startMin = time[1];
var str = [
'<span class="event">',
item.summary, '<span class="event-info">',
startDayWeek, ' ',
startMonth, ' ',
startDay, ', ',
startYear, ' - ',
startHour, ':', startMin,
'</span></span>'
];
}
li.innerHTML = str.join('');
li.setAttribute('class', classes.join(' '));
document.getElementById('calendar').appendChild(li);
}
});
});
}