-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscheduler.html
216 lines (195 loc) · 6.71 KB
/
scheduler.html
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
<html>
<head>
<title>Scheduler</title>
<style>
html,body {
font-family:'Lucida Sans', 'Lucida Sans Regular', 'Lucida Grande', 'Lucida Sans Unicode', Geneva, Verdana, sans-serif;
}
@media not print {
#schedule>div {
display: inline-block;
}
}
@media print {
#top {
display: none;
}
}
</style>
</head>
<body>
<div id="top">
<input id="file-select" name="file-select" type="file" accept=".json" />
<span id="nav">
<input id="prev" name="prev" type="button" value="<" />
<span id="curr"></span>
<input id="next" name="next" type="button" value=">" />
</div>
</div>
<div id="schedule">
</div>
<script src="https://d3js.org/d3.v7.min.js"></script>
<script type="text/javascript">
var root_json = null;
var root_courses = null;
var curr_index = -1;
const getHashCode = (str) => {
let h1 = 0x10293847;
let h2 = 0xfeedf00d;
for (let i = 0, ch; i < str.length; i++) {
ch = str.charCodeAt(i);
h1 = Math.imul(h1 ^ ch, 2654435761);
h2 = Math.imul(h2 ^ ch, 1597334677);
}
h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507);
h1 ^= Math.imul(h2 ^ (h2 >>> 13), 3266489909);
h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507);
h2 ^= Math.imul(h1 ^ (h1 >>> 13), 3266489909);
return 4294967296 * (2097151 & h2) + (h1 >>> 0);
};
const intToHSL = (val) => {
const shortened = Math.imul(val, Math.imul(val, val)) % 360;
return "hsla(" + shortened + ",100%,30%,20%)";
};
function map_course(course) {
var result = Array();
const name = course.name;
const room = course.room;
const faculty = course.faculty;
for (const t in course.time) {
var entry = {...course.time[t]};
entry['name'] = name;
entry['room'] = room;
entry['faculty'] = faculty;
result.push(entry);
}
if (course.lab) {
var entry = {...course.lab.time};
entry['name'] = name;
entry['room'] = course.lab.room;
entry['faculty'] = faculty;
result.push(entry);
}
return result;
}
function load_index(index) {
document.getElementById("curr").textContent = (index + 1) + " of " + root_json.length;
document.getElementById("next").disabled = index == root_json.length - 1;
document.getElementById("prev").disabled = index == 0;
curr_index = index;
d3.select('#schedule').html("");
const the_data = root_json[index].flatMap(map_course);
for (const room of ["Roddy 136", "Roddy 140", "Roddy 147", "Mac", "Linux", "elsewhere"]) {
const div = d3.select("#schedule")
.append("div");
div.append("h2")
.text(room);
const days = div.append("div");
days.append('div')
.style('width', '40px')
.style("display", "inline-block");
const DAYS = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'];
for (const i in DAYS) {
days.append('span')
.text(DAYS[i])
.style("font-size", "10px")
.style("width", "150px")
.style("display", "inline-block")
.style("text-align", "center");
}
const svg = div.append("svg")
.attr("width", 800)
.attr("height", 720);
div.append("p")
.style("page-break-after", "always");
const TIMES = [8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19];
svg.selectAll('line')
.data(TIMES)
.enter()
.append('line')
.style("stroke", "lightgrey")
.style("stroke-width", 1)
.attr('x1', 0).attr('y1', d => (d - 8) * 60)
.attr('x2', 800).attr('y2', d => (d - 8) * 60);
svg.selectAll('text')
.data(TIMES)
.enter()
.append('text')
.attr('x', 0).attr('y', d => (d - 8) * 60)
.attr('dy', 12)
.text(d => {
const h = (d > 12) ? d - 12 : d;
const ap = (d >= 12) ? "PM" : "AM";
return h + ":00 " + ap;
})
.style("font-size", "10px");
const filtered = the_data.filter(x => x.room == room);
svg.selectAll('rect')
.data(filtered)
.enter()
.append('rect')
.attr('x', d => (d.day - 1) * 150 + 45 + 5)
.attr('y', d => d.start - 8 * 60)
.attr('width', 140)
.attr('height', d => d.duration)
.style('fill', d => intToHSL(getHashCode(d.faculty)))
.style('stroke', 'black')
const group = svg.selectAll('g')
.data(filtered)
.enter()
.append('g');
group.append('text')
.attr('dy', 12)
.attr('x', d => (d.day - 1) * 150 + 50 + 5)
.attr('y', d => d.start - 8 * 60)
.text(d => d.name)
.style('font-weight', 700)
.style("font-size", "12px");
group.append('text')
.attr('dy', 12)
.attr('x', d => (d.day - 1) * 150 + 50 + 5)
.attr('y', d => d.start - 8 * 60 + 15)
.text(d => d.faculty)
.style("font-size", "10px");
group.append('text')
.attr('dy', 12)
.attr('x', d => (d.day - 1) * 150 + 50 + 5)
.attr('y', d => d.start - 8 * 60 + 30)
.text(d => {
const fn = (t) => {
const hh = Math.floor(t / 60);
const mm = t % 60;
const h = (hh > 12) ? hh - 12 : hh;
const ap = (hh >= 12) ? "PM" : "AM";
return h + ":" + String(mm).padStart(2, '0') + " " + ap;
};
return fn(d.start) + " - " + fn(d.start + d.duration);
})
.style("font-size", "10px");
}
}
function read_json(event) {
const result = event.target.result;
root_json = JSON.parse(result);
document.getElementById('nav').style.display = "initial";
load_index(0);
}
function file_reader() {
if (this.files && this.files[0]) {
const file = this.files[0];
const reader = new FileReader();
reader.addEventListener('load', read_json);
reader.readAsText(file);
}
}
document.getElementById('file-select').addEventListener("change", file_reader);
document.getElementById('nav').style.display = "none";
document.getElementById('prev').addEventListener("click", () => {
load_index(curr_index - 1);
});
document.getElementById('next').addEventListener("click", () => {
load_index(curr_index + 1);
});
</script>
</body>
</html>