This repository has been archived by the owner on Mar 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlegend.js
115 lines (110 loc) · 3.73 KB
/
legend.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 legend, timelist, dynamicStyle;
function initLegend() {
legend = document.getElementById("legend");
timelist = document.getElementById("timelist");
dynamicStyle = document.getElementById("dynamic-style");
initDays();
initTime();
}
function initDays() {
var days = document.getElementById("days");
for (var day of days.children) {
day.addEventListener("click", function () {
for (var that of days.children) {
that.classList.remove("selected");
}
this.classList.add("selected");
today = this.dataset.day;
initTimeline();
});
}
days.children[new Date().getDay()].click();
}
function getTime() {
var date = new Date();
var time = date.getHours() + date.getMinutes() / 60;
return time;
}
function initTime() {
var time = getTime();
var percent = 100 * time / 24;
dynamicStyle.innerHTML = `.timetable {
background: linear-gradient(to right, gray ${percent}%, white ${percent}%);
background: linear-gradient(to right, var(--color-2) ${percent}%, var(--color-3) ${percent}%);
}`;
setTimeout(initTime, 60000);
}
function initTimeline() {
function sortKeys(obj) {
var keys = Object.keys(obj);
keys.sort(byAlpha);
return keys;
}
function byAlpha(a, b) {
return a < b ? -1 : a > b ? 1 : 0;
}
function formatTime(t) {
var meridiem = "am";
var hour = Math.floor(t);
var minute = Math.floor(60 * (t - hour));
if (hour >= 12) {
meridiem = "pm";
if (hour > 12) {
hour -= 12;
}
}
if (hour <= 0 || hour >= 24) { // will probably never happen
meridiem = "am";
hour = "12";
}
return hour + ":" + (minute < 10 ? "0" : "") + minute + meridiem;
}
function newTimeheader(building) {
var timeheader = document.createElement("div");
timeheader.className = "timeheader";
timeheader.id = building;
return timeheader;
}
function newTimetable(building, day) {
var timetable = document.createElement("div");
timetable.className = "timetable";
timetable.appendChild(newTimeline(building));
for (var room of sortKeys(database[building])) {
timetable.appendChild(newTimeline(building, room, day));
}
return timetable;
}
function newTimeline(building, room, day) {
var timeline = document.createElement("div");
timeline.className = "timeline";
for (var i = 0; i < 24; i++) {
timeline.appendChild(newTimeunit(i));
}
if (typeof room != "undefined") {
timeline.dataset.room = room;
for (var hours of database[building][room][day]) {
timeline.appendChild(newTimespan(building, room, hours[0], hours[1], day));
}
}
return timeline;
}
function newTimeunit(i) {
var timeunit = document.createElement("div");
timeunit.className = "timeunit";
return timeunit;
}
function newTimespan(building, room, a, b, day) {
var timespan = document.createElement("div");
timespan.className = "timespan";
timespan.style.left = (100 * a / 24) + "%";
timespan.style.width = (100 * (b - a) / 24) + "%";
return timespan;
}
timelist.innerHTML = "";
for (var building of sortKeys(database)) {
timelist.appendChild(newTimeheader(building));
timelist.appendChild(newTimetable(building, today));
}
var time = getTime();
timelist.scrollTo((time - 1) * 1500/24, 0);
}