2019-09-26 11:58:02 +00:00
|
|
|
// vim: set ts=2 sw=2 et tw=80:
|
2019-09-27 07:39:16 +00:00
|
|
|
|
2019-09-27 06:56:06 +00:00
|
|
|
// Real code
|
2019-09-26 11:58:02 +00:00
|
|
|
|
2019-09-26 20:48:50 +00:00
|
|
|
const URL = 'https://usirooms.maggioni.xyz/schedule.html?name=';
|
2019-09-26 16:54:29 +00:00
|
|
|
const NOW = new Date();
|
|
|
|
|
|
|
|
const timeTable = {};
|
2019-09-26 13:53:27 +00:00
|
|
|
|
2019-09-26 11:58:02 +00:00
|
|
|
function roomStatus(room, callback) {
|
|
|
|
return new Promise((resolve, _) => {
|
|
|
|
const xhr = new XMLHttpRequest();
|
|
|
|
xhr.addEventListener('load', () => {
|
|
|
|
const parser = new DOMParser();
|
|
|
|
const doc = parser.parseFromString(xhr.responseText, 'text/html');
|
|
|
|
const lessons =
|
|
|
|
doc.querySelectorAll('table.rsContentTable div.rsAptSimple');
|
|
|
|
const parsed = [];
|
|
|
|
|
|
|
|
for (let lesson of lessons) {
|
|
|
|
const time = lesson.querySelector('span[id$=lblOrario]');
|
|
|
|
const start = new Date();
|
2019-09-26 13:53:27 +00:00
|
|
|
start.setHours(parseInt(time.innerText.substring(1,3)));
|
|
|
|
start.setMinutes(parseInt(time.innerText.substring(4,6)));
|
2019-09-26 11:58:02 +00:00
|
|
|
start.setSeconds(0);
|
|
|
|
|
|
|
|
const end = new Date();
|
2019-09-26 13:53:27 +00:00
|
|
|
end.setHours(parseInt(time.innerText.substring(7,9)));
|
|
|
|
end.setMinutes(parseInt(time.innerText.substring(10,12)));
|
2019-09-26 11:58:02 +00:00
|
|
|
end.setSeconds(0);
|
|
|
|
|
|
|
|
parsed.push({
|
|
|
|
title: lesson.getAttribute('title'),
|
|
|
|
start: start,
|
|
|
|
end: end
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve(parsed);
|
|
|
|
});
|
2019-09-26 13:53:27 +00:00
|
|
|
xhr.open('GET', URL + room);
|
2019-09-26 11:58:02 +00:00
|
|
|
xhr.send();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const ROOMS = [
|
2019-09-26 16:54:29 +00:00
|
|
|
'SI-003',
|
|
|
|
'SI-015',
|
2019-10-18 08:49:03 +00:00
|
|
|
'SI-004',
|
2019-09-26 11:58:02 +00:00
|
|
|
'SI-006',
|
|
|
|
'SI-013',
|
2019-10-18 08:49:03 +00:00
|
|
|
'SI-007',
|
2019-09-26 16:54:29 +00:00
|
|
|
'SI-008',
|
2019-09-26 11:58:02 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
const ROOM_LIST = document.querySelector(".times");
|
|
|
|
const ROOM_TEMPLATE = document.getElementById("room");
|
|
|
|
const SLOT_TEMPLATE = document.getElementById("time-slot");
|
|
|
|
const FREE_SLOT_TEMPLATE = document.getElementById("time-free");
|
|
|
|
|
2019-09-26 16:54:29 +00:00
|
|
|
function getRoomNode() {
|
|
|
|
return document.importNode(ROOM_TEMPLATE.content, true);
|
|
|
|
}
|
2019-09-26 11:58:02 +00:00
|
|
|
|
|
|
|
function formatTime(date) {
|
2019-09-26 16:54:29 +00:00
|
|
|
const twoDigits = (n) => {
|
|
|
|
return n < 10 ? "0" + n : n;
|
|
|
|
}
|
|
|
|
|
|
|
|
return twoDigits(date.getHours()) + ':' +
|
|
|
|
twoDigits(date.getMinutes());
|
|
|
|
}
|
|
|
|
|
|
|
|
function colorRoom(roomTitle, node, time = NOW /* QuantumLeap */) {
|
|
|
|
const data = timeTable[roomTitle];
|
|
|
|
if (data == undefined) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const currentLecture = data.filter(d => d.start < time && d.end > time)[0];
|
|
|
|
const isFree = currentLecture === void(0);
|
|
|
|
const block = document.getElementById(roomTitle);
|
|
|
|
|
2019-09-27 06:56:06 +00:00
|
|
|
block.className = block.className.replace(" room-in-use", "")
|
|
|
|
.replace(" room-free", "");
|
|
|
|
block.className += isFree ? " room-free" : " room-in-use";
|
2019-09-26 16:54:29 +00:00
|
|
|
|
|
|
|
block.querySelector('p').innerHTML = isFree ? 'Free' :
|
|
|
|
currentLecture.title + "<br> (" + formatTime(currentLecture.start) + " - " +
|
|
|
|
formatTime(currentLecture.end) + ")";
|
2019-09-26 11:58:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function buildRoomMarkup(roomTitle) {
|
2019-09-26 16:54:29 +00:00
|
|
|
const data = await roomStatus(roomTitle);
|
|
|
|
const room = getRoomNode();
|
|
|
|
const title = room.querySelector('.room-title');
|
|
|
|
title.innerHTML = roomTitle;
|
2019-09-26 21:23:44 +00:00
|
|
|
title.id = "schedule-" + roomTitle;
|
2019-09-26 16:54:29 +00:00
|
|
|
const list = room.querySelector('.list');
|
|
|
|
|
|
|
|
for (const d of data) {
|
|
|
|
const slot = document.importNode(SLOT_TEMPLATE.content, true);
|
|
|
|
const title = slot.querySelector('.title');
|
2019-09-26 21:23:44 +00:00
|
|
|
title.innerHTML = d.title;
|
2019-09-26 16:54:29 +00:00
|
|
|
const start = slot.querySelector('.start');
|
|
|
|
start.innerHTML = formatTime(d.start);
|
|
|
|
const end = slot.querySelector('.end');
|
|
|
|
end.innerHTML = formatTime(d.end);
|
|
|
|
list.appendChild(slot);
|
|
|
|
}
|
|
|
|
|
|
|
|
timeTable[roomTitle] = data;
|
|
|
|
colorRoom(roomTitle, room);
|
|
|
|
|
|
|
|
if (data.length == 0) {
|
|
|
|
list.appendChild(document.importNode(FREE_SLOT_TEMPLATE.content, true));
|
|
|
|
}
|
|
|
|
|
|
|
|
ROOM_LIST.appendChild(room);
|
|
|
|
}
|
|
|
|
|
2019-09-27 07:39:16 +00:00
|
|
|
function setTimePreview(date) {
|
2019-09-26 16:54:29 +00:00
|
|
|
const timePreview = document.getElementById('timepreviewer');
|
2019-09-26 21:23:44 +00:00
|
|
|
timePreview.innerText = "Time: " + formatTime(date);
|
2019-09-26 16:54:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function setupTimeMachine() {
|
|
|
|
const slider = document.getElementById('timemachine');
|
|
|
|
slider.min = 0;
|
2019-09-27 06:56:06 +00:00
|
|
|
// Remaining minutes until 23:59
|
2019-09-27 07:39:16 +00:00
|
|
|
// 24 * 60 - [current hours * 60] - [current minutes] - [1 min to midnight]
|
|
|
|
slider.max = 1440 - (NOW.getHours() * 60) - NOW.getMinutes() - 1;
|
2019-09-26 16:54:29 +00:00
|
|
|
|
|
|
|
slider.addEventListener("input", (e) => {
|
2019-09-27 07:39:16 +00:00
|
|
|
const hours = Math.floor(slider.value / 60);
|
2019-09-26 21:23:44 +00:00
|
|
|
const mins = slider.value % 60;
|
2019-09-27 07:39:16 +00:00
|
|
|
|
2019-09-26 16:54:29 +00:00
|
|
|
let newDate = new Date();
|
|
|
|
newDate.setHours(newDate.getHours() + hours);
|
|
|
|
newDate.setMinutes(newDate.getMinutes() + mins);
|
|
|
|
const node = getRoomNode();
|
|
|
|
|
|
|
|
setTimePreview(newDate)
|
|
|
|
|
|
|
|
ROOMS.forEach((roomTitle) => {
|
|
|
|
colorRoom(roomTitle, node, newDate);
|
|
|
|
});
|
|
|
|
});
|
2019-09-27 07:39:16 +00:00
|
|
|
|
|
|
|
// 10 mins into the future by default
|
|
|
|
slider.value = 10;
|
|
|
|
setTimePreview(new Date(NOW.getTime() + 600000));
|
2019-09-26 11:58:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
(async function() {
|
|
|
|
for (const room of ROOMS) {
|
2019-09-26 16:54:29 +00:00
|
|
|
await buildRoomMarkup(room);
|
2019-09-26 11:58:02 +00:00
|
|
|
}
|
|
|
|
})()
|
2019-09-26 16:54:29 +00:00
|
|
|
|
|
|
|
setupTimeMachine();
|