-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.js
354 lines (303 loc) · 12.3 KB
/
main.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
const { jsonc } = require('jsonc');
const moment = require('moment');
const ics = require("ics");
const rrule = require("./docs/rrule.min.js");
const RRule = rrule.RRule;
const RRuleSet = rrule.RRuleSet;
const FileSaver = require("file-saver");
const DATA_FOLDER = "sem-data/";
const SCHEDULE_FILE = DATA_FOLDER + "ScheduleSem2_2023-24.jsonc"
const SLOTTING_FILE = DATA_FOLDER + "SlottingPattern.jsonc"
const DATE_FORMAT = "DD/MM/YYYY";
const TIME_FORMAT = "hh:mmA"
const TIMEZONE = "Asia/Calcutta";
const PROD_ID = "arpit-saxena/schedule-maker"
let scheduleJSON, slottingJSON;
const setExcludedDates = (scheduleJSON) => {
let excludedDates = [];
for (const interval of scheduleJSON.excludedDates) {
if (interval.length == 1) {
excludedDates.push(moment.utc(interval[0], DATE_FORMAT));
} else { // interval.length == 2
const date1 = moment.utc(interval[0], DATE_FORMAT);
const date2 = moment.utc(interval[1], DATE_FORMAT);
for (let date = date1; date.diff(date2, 'days') <= 0; date.add(1, 'days')) {
excludedDates.push(date.clone());
}
}
}
scheduleJSON.excludedDates = excludedDates;
};
const initSchedule = async () => {
const scheduleJSON = await getJSONC(SCHEDULE_FILE);
scheduleJSON.startingDate = moment.utc(scheduleJSON.startingDate, DATE_FORMAT);
scheduleJSON.endingDate = moment.utc(scheduleJSON.endingDate, DATE_FORMAT);
setExcludedDates(scheduleJSON);
for (const date of scheduleJSON.extraDays) {
date[0] = moment.utc(date[0], DATE_FORMAT);
}
return scheduleJSON;
}
// Copies date, month, year from date2 to date1
const setDate = (date1, date2) => {
date1.date(date2.date());
date1.month(date2.month());
date1.year(date2.year());
}
const initSlotting = async () => {
const slottingJSON = await getJSONC(SLOTTING_FILE);
for (const slot in slottingJSON) {
const schedule = slottingJSON[slot];
for (const sameTimeSchedule of schedule) {
for (let i = 0; i < sameTimeSchedule[0].length; i++) {
sameTimeSchedule[0][i] = getDayEnum(sameTimeSchedule[0][i]);
}
sameTimeSchedule[1][0] = moment.utc(sameTimeSchedule[1][0], TIME_FORMAT);
setDate(sameTimeSchedule[1][0], scheduleJSON.startingDate);
sameTimeSchedule[1][1] = moment.utc(sameTimeSchedule[1][1], TIME_FORMAT);
setDate(sameTimeSchedule[1][1], scheduleJSON.startingDate);
}
}
return slottingJSON;
}
const getDayEnum = (day) => {
return RRule[day.toUpperCase()];
}
const getRRule = (days, startingTime, scheduleJSON) => {
const rruleSet = new RRuleSet();
rruleSet.rrule(new RRule({
freq: RRule.WEEKLY,
dtstart: scheduleJSON.startingDate.toDate(),
until: scheduleJSON.endingDate.toDate(),
byweekday: days,
tzid: TIMEZONE,
}));
for (const date of scheduleJSON.excludedDates) {
const dateTime = date.clone();
dateTime.hour(startingTime.hour());
dateTime.minute(startingTime.minute());
rruleSet.exdate(dateTime.toDate());
}
for (const date of scheduleJSON.extraDays) {
if (days.indexOf(getDayEnum(date[1])) != -1) {
const dateTime = date[0].clone();
dateTime.hour(startingTime.hour());
dateTime.minute(startingTime.minute());
rruleSet.rdate(dateTime.utc().toDate());
}
}
return rruleSet.toString();
}
const getJSONC = async (fileURL) => {
const response = await fetch(fileURL);
const text = await response.text();
return jsonc.parse(text);
};
const momentToArray = (mom) => {
let arr = mom.toArray().slice(0, 5);
arr[1] += 1; // convert month to 1-based index
return arr;
}
events = [];
const addCustomEvent = (name, days, startMoment, endMoment) => {
// Fix the date of the first event
// Assumes that first day in the days array is the earliest
// day.
// Also assumes the semester starts on Monday.
// FIXME: Fix this mess. (Probably will never get fixed)
let startMomentActual = startMoment.clone();
startMomentActual.add(days[0].weekday, 'days');
let endMomentActual = endMoment.clone();
endMomentActual.add(days[0].weekday, 'days');
events.push({
start: momentToArray(startMomentActual),
end: momentToArray(endMomentActual),
productId: PROD_ID,
title: name,
recurrenceRule: getRRule(days, startMoment, scheduleJSON)
});
}
const addSlotEvent = (name, slot) => {
for (const sameTimeSchedule of slottingJSON[slot]) {
addCustomEvent(
name,
sameTimeSchedule[0],
sameTimeSchedule[1][0],
sameTimeSchedule[1][1],
);
}
}
const getWeekdaySelectRow = (idx) => {
const row = document.createElement("div");
row.classList.add("row", "p-2", "weekdays");
const span = document.createElement("span");
span.textContent = "Repeat days:";
span.classList.add("col-sm");
row.appendChild(span);
let shortNames = ["Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"];
for (let i = 0; i < 7; i++) {
let checkbox = document.createElement("input");
checkbox.type = "checkbox";
checkbox.id = shortNames[i] + "-checkbox-" + idx;
checkbox.classList.add("btn-check");
row.appendChild(checkbox);
let label = document.createElement("label");
label.htmlFor = checkbox.id;
label.classList.add("btn", "btn-outline-primary", "col", "m-1");
label.innerText = shortNames[i];
row.appendChild(label);
}
return row;
}
const getTimeInput = (className, idx, labelText) => {
const div = document.createElement("div");
div.classList.add("form-group", "col-sm");
const input = document.createElement("input");
input.type = "time";
input.classList.add("form-control", className);
input.id = className + "-" + idx;
div.appendChild(input);
const startTimeLabel = document.createElement("label");
startTimeLabel.htmlFor = input.id;
startTimeLabel.textContent = labelText;
div.appendChild(startTimeLabel);
return [input, div];
}
const getCourseSlotInput = (idx) => {
const div = document.createElement("div");
div.classList.add("course-input");
const row1 = document.createElement("div");
row1.classList.add("row", "g-3", "m-1");
div.appendChild(row1);
const customSlotDiv = document.createElement("div");
customSlotDiv.classList.add("form-check", "form-switch", "col-sm-auto", "align-middle");
const customSlotCheckbox = document.createElement("input");
customSlotCheckbox.type = "checkbox";
customSlotCheckbox.id = "slot-checkbox-" + idx;
customSlotCheckbox.classList.add("form-check-input", "custom-slot-checkbox");
customSlotDiv.appendChild(customSlotCheckbox);
const customSlotLabel = document.createElement("label");
customSlotLabel.htmlFor = customSlotCheckbox.id;
customSlotLabel.classList.add("form-check-label");
customSlotLabel.innerText = "Custom Slot";
customSlotDiv.appendChild(customSlotLabel);
row1.appendChild(customSlotDiv);
const courseNameDiv = document.createElement("div");
courseNameDiv.classList.add("form-group", "col-sm");
const courseNameInput = document.createElement("input");
courseNameInput.type = "text";
courseNameInput.classList.add("form-control", "course-name");
courseNameInput.placeholder = "Course name";
courseNameInput.required = true;
courseNameDiv.appendChild(courseNameInput);
row1.appendChild(courseNameDiv);
const courseSlotDiv = document.createElement("div");
courseSlotDiv.classList.add("form-group", "col-sm");
const courseSlotInput = document.createElement("input");
courseSlotInput.type = "text";
courseSlotInput.classList.add("form-control", "slot");
courseSlotInput.placeholder = "Slot name";
courseSlotInput.required = true;
courseSlotInput.pattern = "A|B|C|D|E|F|H|J|K|L|M|AA|AB|AC|AD";
courseSlotDiv.appendChild(courseSlotInput);
row1.appendChild(courseSlotDiv);
const [customStartTimeInput, customStartTimeDiv] = getTimeInput("start-time", idx, "Start Time");
const [customEndTimeInput, customEndTimeDiv] = getTimeInput("end-time", idx, "End Time");
const row2 = getWeekdaySelectRow(idx);
const customSlotCheckboxListener = (_) => {
if (customSlotCheckbox.checked) {
row1.removeChild(courseSlotDiv);
row1.appendChild(customStartTimeDiv);
row1.appendChild(customEndTimeDiv);
div.appendChild(row2);
courseSlotInput.required = false;
customStartTimeInput.required = true;
customEndTimeInput.required = true;
} else {
row1.removeChild(customStartTimeDiv);
row1.removeChild(customEndTimeDiv);
div.removeChild(row2);
row1.appendChild(courseSlotDiv);
courseSlotInput.required = true;
customStartTimeInput.required = false;
customEndTimeInput.required = false;
}
}
customSlotCheckbox.addEventListener("change", customSlotCheckboxListener);
return div;
}
const addNumCoursesCallback = () => {
const numCoursesInput = document.getElementById("num-courses");
const inputDiv = document.getElementById("course-slots")
let deletedElements = [];
const callback = (event) => {
const numCourses = numCoursesInput.value;
const currRows = inputDiv.children.length;
if (numCourses == currRows) return;
if (numCourses > currRows) {
for (let i = currRows; i < numCourses; i++) {
if (deletedElements.length > 0) {
inputDiv.appendChild(deletedElements.pop());
} else {
inputDiv.appendChild(getCourseSlotInput(i));
}
}
} else {
while (inputDiv.children.length > numCourses) {
deletedElements.push(inputDiv.lastChild);
inputDiv.lastChild.remove();
}
}
};
numCoursesInput.addEventListener("change", callback);
numCoursesInput.addEventListener("keyup", callback);
numCoursesInput.addEventListener("focusout", callback);
callback();
}
const formCallback = (event) => {
event.preventDefault();
const coursesInput = document.querySelectorAll("#course-slots .course-input");;
for (let i = 0; i < coursesInput.length; i++) {
const courseName = coursesInput[i].querySelector(".course-name");
const slot = coursesInput[i].querySelector(".slot");
const customSlotCheckbox = coursesInput[i].querySelector(".custom-slot-checkbox");
const startTime = coursesInput[i].querySelector(".start-time");
const endTime = coursesInput[i].querySelector(".end-time");
const weekDays = coursesInput[i].querySelector(".weekdays");
if (customSlotCheckbox.checked) {
let startMoment = moment.utc(startTime.value, "HH:mm");
setDate(startMoment, scheduleJSON.startingDate);
let endMoment = moment.utc(endTime.value, "HH:mm");
setDate(endMoment, scheduleJSON.startingDate);
let days = [];
weekDays.querySelectorAll("input").forEach(input => {
if (!input.checked) return;
days.push(input.nextElementSibling.innerText);
});
days = days.map(getDayEnum);
if (days.length == 0) {
alert("Please select days for " + courseName.value);
return;
}
addCustomEvent(courseName.value, days, startMoment, endMoment);
} else {
addSlotEvent(courseName.value, slot.value);
}
}
const { err, value } = ics.createEvents(events);
events = [];
if (err) {
console.error(err);
alert("Error occurred in generation of ics file. See console for details");
} else {
const cal = value.replace(/^RRULE:DTSTART;.*$/gm, '').replace(/\n/gm, '\n\r');
const blob = new Blob([cal], {type: "text/plain;charset=utf-8"});
FileSaver.saveAs(blob, "schedule.ics");
}
}
document.addEventListener("DOMContentLoaded", async () => {
scheduleJSON = await initSchedule();
slottingJSON = await initSlotting();
addNumCoursesCallback();
document.getElementById("slots-form").addEventListener("submit", formCallback);
});