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
|
const API_ENDPOINT = "https://gimb.tk/test.php";
// const API_ENDPOINT = "http://localhost:5000/test.php";
var calendar_obj = null;
var transformed_storage = [];
function checkLogin() {
localforage.getItem("logged_in").then((value) => {
// This code runs once the value has been loaded
// from the offline store.
if (value !== true) {
window.location.replace("/index.html");
}
}).catch((err) => {
// This code runs if there were any errors
console.log(err);
});
}
// Set loading bar visibility
function setLoading(state) {
if (state) {
$("#loading-bar").removeClass("hidden");
} else {
$("#loading-bar").addClass("hidden");
}
}
// ----GET COLOR FROM STRING--------
function hashCode(str) { // java String#hashCode
var hash = 0;
for (var i = 0; i < str.length; i++) {
hash = str.charCodeAt(i) + ((hash << 5) - hash);
}
return hash;
}
function intToRGB(i) {
var c = (i & 0x00FFFFFF)
.toString(16)
.toUpperCase();
return "00000".substring(0, 6 - c.length) + c;
}
// http://www.w3.org/TR/AERT#color-contrast
function getForegroundFromBackground(background_color) {
let color_hex = background_color.replace("#", "");
let rgb = [
parseInt(color_hex.substring(0, 2), 16),
parseInt(color_hex.substring(2, 4), 16),
parseInt(color_hex.substring(4, 6), 16)
];
let o = Math.round(((parseInt(rgb[0]) * 299) + (parseInt(rgb[1]) * 587) + (parseInt(rgb[2]) * 114)) / 1000);
if (o > 180) {
return "#000000";
} else {
return "#ffffff";
}
}
function getHexColorFromString(str) {
return "#" + intToRGB(hashCode(str));
}
// --------------------------------------------------
// ---------DATE FUNCTION-------------
function getDateString(date) {
let year_str = date.getFullYear();
let month_str = date.getMonth() + 1
month_str = month_str.toString().padStart(2, "0");
let day_str = date.getDate();
day_str = day_str.toString().padStart(2, "0");
let date_string = year_str + "-" + month_str + "-" + day_str;
return date_string;
}
function getLastMonday(date_object) {
if (date_object.getDay() === 0) {
date_object.setDate(date_object.getDate() - 6);
} else {
date_object.setDate(date_object.getDate() - date_object.getDay() + 1);
}
return date_object;
}
// ----------------------------------
async function loadTimetable(date_object, force_refresh = false) {
setLoading(true);
let date_monday = getLastMonday(date_object);
let date_string = getDateString(date_monday);
// Load required data
let promises_to_run = [
localforage.getItem("username").then(function (value) {
username = value;
}),
localforage.getItem("password").then(function (value) {
password = value;
}),
localforage.getItem("timetable").then(function (value) {
timetable = value;
})
];
await Promise.all(promises_to_run);
if (force_refresh || timetable == null || !(date_string in timetable)) {
$.ajax({
url: API_ENDPOINT,
crossDomain: true,
data: {
"u": username,
"p": password,
"m": "fetchurnik",
"a": date_string
},
dataType: "json",
cache: false,
type: "GET",
success: (data) => {
// Check if operation was successful
if (data === null) {
UIAlert( D("noPeriods") );
setLoading(false);
} else {
// Populate the calendar & save data
if (timetable === null) {
timetable = {};
}
timetable[date_string] = data;
localforage.setItem("timetable", timetable).then(() => {
displayTimetable(data, date_monday);
setLoading(false);
});
}
},
error: () => {
UIAlert( D( "noInternetConnection" ) );
setLoading(false);
}
});
} else {
displayTimetable(timetable[date_string], date_monday);
setLoading(false);
}
}
function getLessonTimes(lesson_number) {
const lessonTimes = [
["07:10:00", "07:55:00"],
["08:00:00", "08:45:00"],
["08:50:00", "09:35:00"],
["09:40:00", "10:25:00"],
["10:55:00", "11:40:00"],
["11:45:00", "12:30:00"],
["12:35:00", "13:20:00"],
["13:25:00", "14:10:00"],
["14:15:00", "15:00:00"],
["15:05:00", "15:50:00"],
["15:55:00", "16:40:00"],
["16:45:00", "17:30:00"],
["17:35:00", "18:20:00"],
["18:25:00", "19:10:00"]
];
return lessonTimes[lesson_number];
}
function displayTimetable(weekly_timetable, date_object) {
let transformed_timetable = [];
let num_days = Object.keys(weekly_timetable).length;
for (let i = 0; i < num_days; i++) {
// Get date string (required by callendar)
let date_string = getDateString(date_object);
// Go to next day (for next loop iteration)
date_object.setDate(date_object.getDate() + 1);
let daily_timetable = weekly_timetable[i.toString()];
Object.keys(daily_timetable).forEach((lesson_number) => {
let lesson = daily_timetable[lesson_number];
let lesson_times = getLessonTimes(parseInt(lesson_number));
let bg_color = getHexColorFromString(lesson["kratica"]);
let fg_color = getForegroundFromBackground(bg_color);
let lesson_metadata = {
subject: lesson["predmet"],
class: lesson["razred"],
teacher: lesson["profesor"],
classroom: lesson["prostor"],
start: lesson_times[0].substring(0, 5),
end: lesson_times[1].substring(0, 5)
}
let lesson_object = {
id: JSON.stringify(lesson_metadata),
title: lesson["kratica"],
start: date_string + " " + lesson_times[0],
end: date_string + " " + lesson_times[1],
backgroundColor: bg_color,
textColor: fg_color
};
transformed_timetable.push(lesson_object);
});
}
// Update calendar
calendar_obj.removeAllEvents();
calendar_obj.addEventSource(transformed_timetable);
// Update stored value
transformed_storage = transformed_timetable;
}
function eventClickHandler(eventClickInfo) {
let lesson_metadata = JSON.parse(eventClickInfo.event.id);
let lesson_subject = lesson_metadata["subject"];
let lesson_teacher = lesson_metadata["teacher"];
let lesson_classroom = lesson_metadata["classroom"];
let lesson_class = lesson_metadata["class"];
let lesson_duration = lesson_metadata["start"] + " - " + lesson_metadata["end"];
document.getElementById("lesson-subject").innerText = lesson_subject;
document.getElementById("lesson-teacher").innerText = lesson_teacher;
document.getElementById("lesson-class").innerText = lesson_class;
document.getElementById("lesson-classroom").innerText = lesson_classroom;
document.getElementById("lesson-duration").innerText = lesson_duration;
const modal = document.querySelectorAll('.side-modal')[0];
M.Sidenav.getInstance(modal).open();
}
document.addEventListener("DOMContentLoaded", () => {
checkLogin();
let calendarEl = document.getElementById("calendar");
calendar_obj = new FullCalendar.Calendar(calendarEl, {
plugins: ["timeGrid"],
eventClick: eventClickHandler,
defaultView: "timeGridWeek",
contentHeight: "auto",
height: "auto",
width: "auto",
timeGridEventMinHeight: 35,
nowIndicator: true,
firstDay: 1,
weekends: false,
minTime: "07:10:00",
maxTime: "19:10:00"
});
calendar_obj.render();
loadTimetable(new Date());
// Handlers for next/prev/today buttons
$(".fc-today-button, .fc-prev-button, .fc-next-button").click(() => {
loadTimetable(calendar_obj.getDate());
});
// Setup refresh handler
$("#refresh-icon").click(() => {
loadTimetable(calendar_obj.getDate(), true);
});
// Setup side menu
const menus = document.querySelectorAll(".side-menu");
M.Sidenav.init(menus, { edge: "right", draggable: true });
// Setup side modal
const modals = document.querySelectorAll('.side-modal');
M.Sidenav.init(modals, { edge: 'left', draggable: false });
});
|