-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
325 lines (275 loc) · 9.31 KB
/
script.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
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
let nav = 0; // current month - 1
let clicked = null; // clicked date
let dateTimeEvents = [];
let dateEvents = [];
let tasks = [];
let year, month, date;
let client;
let access_token;
let googleResponse;
const dayGrid = document.getElementById("dayGrid");
const newEventModal = document.getElementById("newEventModal");
const deleteEventModal = document.getElementById("deleteEventModal");
const backDrop = document.getElementById("modalBackDrop");
const eventTitleInput = document.getElementById("eventTitleInput");
const weekdays = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
];
const CLIENT_ID =
"1039492430904-8q81bvrg1bt8fslph292sfiqkp58a9t3.apps.googleusercontent.com";
// Authorization scopes required by the API; multiple scopes can be
// included, separated by spaces.
const SCOPES = "https://www.googleapis.com/auth/calendar.readonly";
function initClient() {
client = google.accounts.oauth2.initTokenClient({
client_id: CLIENT_ID,
scope: SCOPES,
callback: (tokenResponse) => {
access_token = tokenResponse.access_token;
localStorage.setItem("access_token", access_token);
console.log("got access token");
loadCalendar();
},
auto_select: true,
});
}
function getToken() {
client.requestAccessToken();
}
function revokeToken() {
google.accounts.oauth2.revoke(access_token, () => {
console.log("access token revoked");
});
}
function updateDate() {
const dt = new Date();
if (nav !== 0) {
dt.setMonth(new Date().getMonth() + nav);
}
day = dt.getDate();
month = dt.getMonth();
year = dt.getFullYear();
}
function loadCalendar() {
if (localStorage.getItem("access_token")) {
access_token = localStorage.getItem("access_token");
}
if (client === undefined || access_token === undefined) {
if (client === undefined) console.log("Client not set.");
if (access_token === undefined) console.log("access token not set.");
return;
}
let xhr = new XMLHttpRequest();
let timeMin = new Date(year, month - 1, 1).toISOString();
let timeMax = new Date(year, month + 2, 0).toISOString();
// URL에 쿼리 파라미터 추가
let url = `https://www.googleapis.com/calendar/v3/calendars/primary/events?timeMin=${encodeURIComponent(
timeMin
)}&timeMax=${encodeURIComponent(
timeMax
)}&orderBy=startTime&showDeleted=false&singleEvents=true`; //!!!!!!!!
console.log(url);
xhr.open("GET", url);
xhr.setRequestHeader("Authorization", "Bearer " + access_token);
xhr.onreadystatechange = function () {
// 요청이 완료되었는지 확인
if (xhr.readyState === XMLHttpRequest.DONE) {
// HTTP 요청이 성공적으로 완료되었는지 확인
if (xhr.status === 200) {
// 성공적으로 응답을 받았을 경우, 응답을 파싱하여 처리
let response = JSON.parse(xhr.responseText);
console.log("Calendar events:", response);
const googleEvents = response.items;
googleResponse = response.items;
if (!googleEvents || googleEvents.length == 0) {
// document.getElementById('content').innerText = 'No googleEvents found.';
return;
}
let modifiedEvents = googleEvents.map((event) => ({
title: event.summary,
isAllDay: event.start.dateTime === undefined,
startDate: !event.start.dateTime
? new Date(event.start.date).toLocaleDateString("en-us")
: new Date(event.start.dateTime).toLocaleDateString("en-us"),
endDate: !event.start.dateTime
? new Date(event.end.date).toLocaleDateString("en-us")
: new Date(event.end.dateTime).toLocaleDateString("en-us"),
startDateTime: event.start.dateTime
? new Date(event.end.dateTime).toLocaleString("en-us")
: undefined,
endDateTime: event.start.dateTime
? new Date(event.end.dateTime).toLocaleString("en-us")
: undefined,
}));
for (let key in modifiedEvents) {
if (modifiedEvents[key].isAllDay) {
dateEvents.push(modifiedEvents[key]);
} else {
dateTimeEvents.push(modifiedEvents[key]);
}
}
load();
} else {
console.error("Failed to load calendar events, status: " + xhr.status);
}
}
};
xhr.send();
}
// function openModal(date) {
// clicked = date;
// const eventForDay = events.find((e) => e.date == clicked);
// if (eventForDay) {
// document.getElementById("eventText").innerText = eventForDay.title;
// deleteEventModal.style.display = "block";
// } else {
// newEventModal.style.display = "block";
// }
// backDrop.style.display = "block";
// }
function load() {
const dt = new Date(year, month, day);
const firstDayOfMonth = new Date(year, month, 1);
const daysInThisMonth = new Date(year, month + 1, 0).getDate();
const daysInLastMonth = new Date(year, month, 0).getDate();
document.getElementById("monthDisplay").innerText = `${dt.toLocaleDateString(
"en-us",
{ month: "short" }
)} ${year}`;
const paddingDays = weekdays.indexOf(
firstDayOfMonth.toLocaleDateString("en-us", {
weekday: "long",
})
);
dayGrid.innerHTML = "";
let dayDisplayed = 0;
/* last month */
for (; dayDisplayed < paddingDays; dayDisplayed++) {
const daySquare = document.createElement("div");
daySquare.classList.add("paddingDay");
daySquare.innerText = daysInLastMonth - paddingDays + dayDisplayed + 1;
dayGrid.appendChild(daySquare);
}
/* this month */
for (; dayDisplayed - paddingDays < daysInThisMonth; dayDisplayed++) {
const thisDay = dayDisplayed - paddingDays + 1;
const daySquare = document.createElement("div");
daySquare.classList.add("day");
daySquare.innerText = thisDay;
if (dayDisplayed - paddingDays === day && nav === 0) {
daySquare.id = "currentDay";
}
const thisDateString = `${month + 1}/${thisDay}/${year}`;
const dateEventsForDay = dateEvents.filter(
(e) => e.startDate <= thisDateString && thisDateString < e.endDate
);
// const dateTimeString = `${month + 1}/${thisDay}/${year}`;
const dateTimeEventsForDay = dateTimeEvents.filter(
(e) => e.startDate <= thisDateString && thisDateString <= e.endDate
);
let eventsDisplayed = 0;
let eventsNotDisplayed = 0;
for (const key in dateEventsForDay) {
if (eventsDisplayed >= 4) {
++eventsNotDisplayed;
} else {
const eventDiv = document.createElement("div");
eventDiv.classList.add("dateEvent");
eventDiv.innerText = dateEventsForDay[key].title;
daySquare.appendChild(eventDiv);
++eventsDisplayed;
}
}
for (const key in dateTimeEventsForDay) {
if (eventsDisplayed >= 4) {
++eventsNotDisplayed;
} else {
const eventDiv = document.createElement("div");
eventDiv.classList.add("dateTimeEvent");
eventDiv.innerText = dateTimeEventsForDay[key].title;
daySquare.appendChild(eventDiv);
++eventsDisplayed;
}
}
if (eventsNotDisplayed) {
const eventDiv = document.createElement("div");
eventDiv.classList.add("moreEvent");
eventDiv.innerText = "+ " + eventsNotDisplayed + " more";
daySquare.appendChild(eventDiv);
}
// daySquare.addEventListener("click", () =>
// openModal(`${month + 1}/${thisDay}/${year}`)
// );
dayGrid.appendChild(daySquare);
}
/* next month */
for (; dayDisplayed % 7 != 0; dayDisplayed++) {
const daySquare = document.createElement("div");
daySquare.classList.add("paddingDay");
daySquare.innerText = dayDisplayed % 7;
dayGrid.appendChild(daySquare);
}
}
// function saveEvent() {
// if (eventTitleInput.value) {
// eventTitleInput.classList.remove("error");
// events.push({
// date: clicked,
// title: eventTitleInput.value,
// });
// localStorage.setItem("events", JSON.stringify(events));
// closeModal();
// } else {
// eventTitleInput.classList.add("error");
// }
// }
// function closeModal() {
// eventTitleInput.classList.remove("error");
// newEventModal.style.display = "none";
// deleteEventModal.style.display = "none";
// backDrop.style.display = "none";
// eventTitleInput.value = "";
// clicked = null;
// load();
// }
// function deleteEvent() {
// events = events.filter((e) => e.date !== clicked);
// localStorage.setItem("events", JSON.stringify(events));
// closeModal();
// }
function initButtons() {
document.getElementById("nextButton").addEventListener("click", () => {
nav++;
dateTimeEvents = [];
dateEvents = [];
updateDate();
loadCalendar();
load();
});
document.getElementById("backButton").addEventListener("click", () => {
nav--;
dateTimeEvents = [];
dateEvents = [];
updateDate();
loadCalendar();
load();
});
document.getElementById("loginButton").addEventListener("click", getToken);
// document.getElementById("saveButton").addEventListener("click", saveEvent);
// document.getElementById("cancelButton").addEventListener("click", closeModal);
// document
// .getElementById("deleteButton")
// .addEventListener("click", deleteEvent);
// document.getElementById("closeButton").addEventListener("click", closeModal);
}
initClient();
initButtons();
updateDate();
load();
loadCalendar();