-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay-timetable.js
115 lines (94 loc) · 3.14 KB
/
display-timetable.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
var colors = ["accent-pink-gradient", "accent-orange-gradient", "accent-green-gradient", "accent-cyan-gradient", "accent-blue-gradient", "accent-purple-gradient"]
var nextCourseColor = 0
function loadCourses() {
let additionalInfo = "";
let tabTitleElement = document.getElementsByClassName("nav-item")[2].getElementsByTagName("a")[0];
tabTitleElement.textContent = "Визуализация на програма";
var url = new URL(window.location.href);
console.log(window.location.href);
var location = url.searchParams.get("location");
if(location) {
additionalInfo = '?location=' + location;
console.log("Search by location:" + location);
tabTitleElement.textContent += " на " + location;
}else{
var cookie = getCookie("currentlyLoggedInUserSpeciality");
if (!cookie || cookie === '') {
cookie = "SI";
}
additionalInfo = '?speciality=' + cookie;
console.log("Search by speciality:" + cookie);
tabTitleElement.textContent += " на " + cookie;
}
return fetch('./api/get-courses.php' + additionalInfo)
.then(res => res.json())
.then(courses => {
courses.forEach(course => {
fillTimetable(course);
});
})
.catch(err => {
console.error(err);
});
}
function getCookie(name) {
const value = `; ${document.cookie}`;
const parts = value.split(`; ${name}=`);
if (parts.length === 2) return parts.pop().split(';').shift();
}
let coursesColors = new Map();
function fillTimetable(course) {
var color;
if (coursesColors.has(course.title)) {
color = coursesColors.get(course.title);
} else {
color = getColor();
coursesColors.set(course.title, color);
}
var arrayOfStartTimes = getStartTimes(course.startTime.split(":")[0], course.endTime.split(":")[0]);
for (var i = 0; i < arrayOfStartTimes.length; i++) {
var block = document.getElementById(arrayOfStartTimes[i] + "-" + convertDayOfTheWeek(course.day));
var newSpot = document.createElement("div");
newSpot.classList.add(color);
newSpot.classList.add("center-text");
newSpot.textContent = course.title;
block.appendChild(newSpot)
}
}
function isValidHttpUrl(string) {
try {
let url = new URL(string);
} catch (_) {
return false;
}
return true;
}
function getStartTimes(startTime, endTime) {
var result = [];
for (var i = parseInt(startTime, 10); i < parseInt(endTime, 10); i++) {
result.push(i);
}
return result;
}
function getColor() {
var color = colors[nextCourseColor];
nextCourseColor = (nextCourseColor + 1) % 6;
return color;
}
function convertDayOfTheWeek(day) {
switch (day) {
case 'Понеделник':
return "monday";
case 'Вторник':
return "tuesday";
case 'Сряда':
return "wednesday";
case 'Четвъртък':
return "thursday";
case 'Петък':
return "friday";
}
}
(function() {
loadCourses();
})();