-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
306 lines (277 loc) · 13.4 KB
/
index.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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="index.css">
<title>Laurier Room Availability</title>
</head>
<body>
<div id="filter-status">Viewing: All Rooms</div>
<button id="filter-currently-empty">Currently Empty Rooms</button>
<button id="filter-empty-rest-day">Empty for Rest of the Day</button>
<div id="all-rooms-container" class="room-container"></div>
<div id="currently-empty-rooms" class="room-container"></div>
<div id="empty-rooms" class="room-container"></div>
<p>Winter 2025</p>
<a href="https://github.com/aiden10/rooms" target="_blank">Source Code</a>
<script>
var roomData;
const dayDict = {0: "M", 1: "T", 2: "W", 3: "R", 4: "F"};
const roomContainer = document.getElementById('all-rooms-container');
const currentlyEmptyDiv = document.getElementById('currently-empty-rooms');
const emptyForRestDiv = document.getElementById('empty-rooms');
const filterStatus = document.getElementById('filter-status');
const allRoomsDivs = [];
// Add functions to buttons
document.getElementById('filter-currently-empty').addEventListener('click', function() {
filterCurrentlyEmptyRooms();
});
document.getElementById('filter-empty-rest-day').addEventListener('click', function() {
filterEmptyForRestOfDayRooms();
});
function compare(a, b) {
const nameA = a.querySelector('h2').innerText;
const nameB = b.querySelector('h2').innerText;
if (nameA < nameB ){
return -1;
}
if ( nameA > nameB ){
return 1;
}
return 0;
}
function loadAllRooms(){
allRoomsDivs.sort(compare);
allRoomsDivs.forEach(room => {
roomContainer.appendChild(room);
});
}
function filterCurrentlyEmptyRooms(){
const currentDate = new Date();
const currentTime = new Date(currentDate.getTime());
const currentlyEmptyRooms = [];
var currentDay = currentTime.getDay();
if (currentDay === 0 || currentDay === 6){
window.alert("Today is a weekend and all rooms should be available");
return;
}
if (currentlyEmptyDiv.style.display === "block") {
filterStatus.innerText = "Viewing: All Rooms";
currentlyEmptyDiv.style.display = "none";
roomContainer.style.display = "block";
return;
}
filterStatus.innerText = "Viewing: Currently Empty Rooms";
emptyForRestDiv.style.display = "none";
roomContainer.style.display = "none";
currentlyEmptyDiv.style.display = "block";
for (const roomDiv of roomContainer.children){
var name = roomDiv.querySelector('h2').innerText;
var roomSchedule = roomData[name];
var currentHour = currentTime.getHours();
var currentMinutes = currentTime.getMinutes();
const dayCode = dayDict[currentDay - 1]
var currentSuffix = currentHour >= 12 ? "PM" : "AM";
var valid = isValidTime(roomSchedule, currentHour, currentMinutes, dayCode, currentSuffix, true);
if (valid) {
currentlyEmptyRooms.push(roomDiv.cloneNode(true));
}
}
roomContainer.style.display = "none";
currentlyEmptyDiv.innerHTML = '';
currentlyEmptyRooms.sort(compare);
currentlyEmptyRooms.forEach(room => {
const scheduleDiv = room.querySelector('.scheduleDiv');
room.querySelector('h2').addEventListener("click", function(){
var content = scheduleDiv;
if (content.style.display === "none"){
content.style.display = "block";
}
else{
content.style.display = "none";
}
});
currentlyEmptyDiv.appendChild(room);
});
}
function filterEmptyForRestOfDayRooms(){
const currentDate = new Date();
const currentTime = new Date(currentDate.getTime());
const emptyForRestRooms = [];
var currentDay = currentTime.getDay();
if (currentDay === 0 || currentDay === 6){
window.alert("Today is a weekend and all rooms should be available");
return;
}
if (emptyForRestDiv.style.display === "block") {
filterStatus.innerText = "Viewing: All Rooms";
emptyForRestDiv.style.display = "none";
roomContainer.style.display = "block";
return;
}
filterStatus.innerText = "Viewing: Empty For Rest of Day Rooms";
currentlyEmptyDiv.style.display = "none";
emptyForRestDiv.style.display = "block";
roomContainer.style.display = "none";
for (const roomDiv of roomContainer.children) {
const name = roomDiv.querySelector('h2').innerText;
const roomSchedule = roomData[name];
let timeCopy = new Date(currentTime.getTime());
let roomValid = true;
while (timeCopy.getHours() < 23) {
const currentHour = timeCopy.getHours();
const currentMinutes = timeCopy.getMinutes();
const dayCode = dayDict[currentDay - 1]
const currentSuffix = currentHour >= 12 ? "PM" : "AM";
const valid = isValidTime(roomSchedule, currentHour, currentMinutes, dayCode, currentSuffix, true);
if (!valid) {
roomValid = false;
break;
}
timeCopy.setMinutes(timeCopy.getMinutes() + 10); // Increment by 10 minutes
}
if (roomValid) {
emptyForRestRooms.push(roomDiv.cloneNode(true)); // Clone to preserve original
}
}
emptyForRestRooms.sort(compare);
roomContainer.style.display = "none"; // Hide original list
emptyForRestDiv.innerHTML = ''; // Clear previous filtered rooms
emptyForRestRooms.forEach(room => {
const scheduleDiv = room.querySelector('.scheduleDiv');
room.querySelector('h2').addEventListener("click", function(){
var content = scheduleDiv;
if (content.style.display === "none"){
content.style.display = "block";
}
else{
content.style.display = "none";
}
});
emptyForRestDiv.appendChild(room);
});
}
function isValidTime(schedule, hour, minute, cellDay, suffix, fromButton) {
// Get the room's schedule for the current day
let classHours = schedule[cellDay];
if (!classHours) return true; // If no classes, the room is empty.
if (!fromButton){
// Adjust hour for AM/PM
if (suffix === "PM" && hour !== 12) {
hour += 12;
}
else if (suffix === "AM" && hour === 12) {
hour = 0;
}
}
// Create a current time Date object for comparison
let currentTime = new Date();
currentTime.setHours(hour, minute, 0, 0); // Set current hour, minute, and reset seconds/milliseconds
for (const classTimes of classHours) {
let [start, end] = classTimes;
let [startHour, startMinute] = start.split(":").map(Number);
let [endHour, endMinute] = end.split(":").map(Number);
// Create Date objects for class start and end times
let classStart = new Date();
classStart.setHours(startHour, startMinute, 0, 0);
let classEnd = new Date();
classEnd.setHours(endHour, endMinute, 0, 0);
// Check if current time falls within the class time
if (currentTime >= classStart && currentTime < classEnd) {
return false; // Room is occupied during this time
}
}
return true;
}
fetch('schedules/winter.json')
.then(response => response.json())
.then(data => {
roomData = data.Rooms;
for (const room of Object.entries(roomData)){
// initialize elements and properties
const roomDiv = document.createElement('div');
const scheduleDiv = document.createElement('div');
scheduleDiv.style.display = "none";
const roomName = document.createElement('h2');
const timeTable = document.createElement('table');
roomDiv.className = "roomDiv";
scheduleDiv.className = "scheduleDiv";
// Create time table headers
const mondayHeader = document.createElement('th')
const tuesdayHeader = document.createElement('th')
const wednesdayHeader = document.createElement('th')
const thursdayHeader = document.createElement('th')
const fridayHeader = document.createElement('th')
const timeHeader = document.createElement('th');
const headerRow = document.createElement('tr');
mondayHeader.innerText = "Monday";
tuesdayHeader.innerText = "Tuesday";
wednesdayHeader.innerText = "Wednesday";
thursdayHeader.innerText = "Thursday";
fridayHeader.innerText = "Friday";
timeHeader.innerText = "Time";
headerRow.appendChild(timeHeader);
headerRow.appendChild(mondayHeader)
headerRow.appendChild(tuesdayHeader)
headerRow.appendChild(wednesdayHeader)
headerRow.appendChild(thursdayHeader)
headerRow.appendChild(fridayHeader)
timeTable.appendChild(headerRow);
let suffix = "AM";
let hour = 12;
let minutes = 0;
// Create the rest of the table
for (var i = 0; i < 144; i++) {
let row = document.createElement('tr');
let timeCell = document.createElement('td');
let formattedHour = hour === 0 ? 12 : hour;
let formattedMinutes = minutes < 10 ? "0" + minutes : minutes; // Add leading zero for single digits
timeCell.innerText = `${formattedHour}:${formattedMinutes} ${suffix}`;
row.appendChild(timeCell);
// Populate rows
for (let j = 0; j < 5; j++) {
let emptyCell = document.createElement('td');
let cellDay = dayDict[j];
let valid = isValidTime(room[1], Number(hour), Number(minutes), cellDay, suffix, false);
valid ? emptyCell.style.backgroundColor = "green" : emptyCell.style.backgroundColor = "red";
row.appendChild(emptyCell);
}
timeTable.appendChild(row);
// Time logic
minutes += 10;
if (minutes === 60) {
minutes = 0;
hour++;
}
if (hour === 12 && suffix === "AM" && i > 40) {
suffix = "PM";
}
else if (hour === 13) {
hour = 1;
}
}
// Make room divs collapsible
roomName.addEventListener("click", function(){
var content = scheduleDiv;
if (content.style.display === "none"){
content.style.display = "block";
}
else{
content.style.display = "none";
}
});
roomName.innerText = room[0];
scheduleDiv.appendChild(timeTable);
roomDiv.appendChild(roomName);
roomDiv.appendChild(scheduleDiv);
allRoomsDivs.push(roomDiv);
}
loadAllRooms();
})
.catch(error => {
console.error('Error loading rooms:', error);
});
</script>
</body>
</html>