orario-scolastico-itet/static/index.js

286 lines
8.8 KiB
JavaScript

function load_pdfs() {
fetch("/api/pdf")
.then((response) => response.json())
.then((data) => {
let i = 0;
if (data === undefined || data.length === 0) {
show_error(null, "Nessun orario trovato");
return;
}
data.forEach((el) => {
const select = document.getElementById("pdf");
const option = document.createElement("option");
option.value = i;
option.text = el;
select.appendChild(option);
i++;
});
document.getElementById("pdf-label").removeAttribute("aria-busy");
})
.catch((error) => {
show_error(error);
});
}
function load_teachers(id) {
fetch(`/api/professori?id=${id}`)
.then((response) => response.json())
.then((data) => {
sessionStorage.setItem("teachers", JSON.stringify(data));
let i = 0;
const list = document.getElementById("teachers");
list.innerHTML = "";
data.forEach((el) => {
const option = document.createElement("option");
option.value = el;
option.text = el;
list.appendChild(option);
i++;
});
document.getElementById("teacher-label").removeAttribute("aria-busy");
})
.catch((error) => {
show_error(error);
});
}
function load_classes(id) {
fetch(`/api/classi?id=${id}`)
.then((response) => response.json())
.then((data) => {
const select = document.getElementById("year");
for (let i = 0; i < select.children.length; i++) {
if (!select.children[i].hasAttribute("disabled")) {
select.removeChild(select.children[i]);
i--;
}
}
for (let i = 0; i < data.length; i++) {
const option = document.createElement("option");
option.value = i + 1;
option.text = i + 1;
select.appendChild(option);
}
sessionStorage.setItem("classes", JSON.stringify(data));
document.getElementById("class-label").removeAttribute("aria-busy");
})
.catch((error) => {
show_error(error);
});
}
function make_teacher_table(id, teacher) {
document.getElementById("table-title").innerText =
`Orario del docente ${teacher}`;
make_table(`/api/professore?id=${id}&professore=${teacher}`);
}
function make_student_table(id, student_class) {
document.getElementById("table-title").innerText =
`Orario della classe ${student_class}`;
make_table(`/api/classe?id=${id}&classe=${student_class}`);
}
function make_table(url) {
fetch(url)
.then((response) => response.json())
.then((data) => {
for (let key in data) {
let i = data[key].length - 1;
while (i >= 0 && data[key][i] === "") {
data[key].pop();
i--;
}
}
const columns = Math.max(
...Object.values(data).map((vettore) => vettore.length),
);
let table = document.getElementById("table");
let tbody = document.createElement("tbody");
let week = [
"Lunedì",
"Martedì",
"Mercoledì",
"Giovedì",
"Venerdì",
"Sabato",
];
let thead = document.createElement("thead");
let tr = document.createElement("tr");
let th = document.createElement("th");
th.scope = "col";
tr.appendChild(th);
for (let i = 0; i < columns; i++) {
let th = document.createElement("th");
th.scope = "col";
th.textContent = i + 1;
tr.appendChild(th);
}
thead.appendChild(tr);
table.appendChild(thead);
week.forEach((weekday) => {
let tr = document.createElement("tr");
let th = document.createElement("th");
th.scope = "row";
th.textContent = weekday;
tr.appendChild(th);
if (data[weekday] != null) {
let inserted = 0;
data[weekday].forEach((student_class) => {
let td = document.createElement("td");
td.textContent = student_class.replace(/ (?=[a-z])/g, "");
tr.appendChild(td);
inserted++;
});
while (inserted < columns) {
let td = document.createElement("td");
tr.appendChild(td);
inserted++;
}
tbody.appendChild(tr);
}
});
table.appendChild(tbody);
document.getElementById("table").removeAttribute("aria-busy");
})
.catch((error) => {
show_error(error);
});
}
function select_pdf() {
Cookies.set("id", document.getElementById("pdf").value);
reset_forms();
document.getElementById("type").value = "none";
document.getElementById("form-teacher").setAttribute("hidden", "");
document.getElementById("form-classes").setAttribute("hidden", "");
document.getElementById("form-type").removeAttribute("hidden");
}
function reset_forms() {
document.getElementById("year").value = "none";
document.getElementById("section").value = "none";
document.getElementById("major").value = "none";
document.getElementById("teacher").value = "";
}
function select_type() {
let id = document.getElementById("pdf").value;
let value = document.getElementById("type").value;
reset_forms();
if (value === "teacher") {
load_teachers(id);
document.getElementById("form-teacher").removeAttribute("hidden");
document.getElementById("form-classes").setAttribute("hidden", "");
} else if (value === "student") {
load_classes(id);
document.getElementById("form-classes").removeAttribute("hidden");
document.getElementById("form-teacher").setAttribute("hidden", "");
}
}
function load_section() {
let data = JSON.parse(sessionStorage.getItem("classes"));
let year = document.getElementById("year").value;
const select = document.getElementById("section");
for (let i = 0; i < select.children.length; i++) {
if (!select.children[i].hasAttribute("disabled")) {
select.removeChild(select.children[i]);
i--;
}
}
Object.keys(data[year - 1]).forEach((el) => {
const option = document.createElement("option");
option.value = el;
option.text = el;
select.appendChild(option);
});
}
function load_major() {
let data = JSON.parse(sessionStorage.getItem("classes"));
let year = document.getElementById("year").value;
let section = document.getElementById("section").value;
let select = document.getElementById("major");
for (let i = 0; i < select.children.length; i++) {
if (!select.children[i].hasAttribute("disabled")) {
select.removeChild(select.children[i]);
i--;
}
}
data[year - 1][section].forEach((el) => {
const option = document.createElement("option");
option.value = el;
option.text = el;
select.appendChild(option);
});
}
function select_teacher() {
let teacher = document.getElementById("teachers").value;
let teachers = JSON.parse(sessionStorage.getItem("teachers"));
if (!teachers.includes(teacher)) {
return false;
}
Cookies.set("type", "teacher");
Cookies.set("teacher", teacher);
}
function select_class() {
Cookies.set("type", "student");
let student_class = document.getElementById("year").value;
student_class += document.getElementById("section").value;
student_class += document.getElementById("major").value;
Cookies.set("class", student_class);
}
function delete_cookies() {
Cookies.remove("id");
Cookies.remove("type");
Cookies.remove("teacher");
Cookies.remove("class");
}
function show_error(e, message) {
let default_error = "Errore nel caricamento dei dati, riprova più tardi";
console.error(e ?? default_error);
document.getElementById("error").textContent = message ?? default_error;
document.getElementById("error-dialog").setAttribute("open", "");
}
function filter_teachers() {
let input = document.getElementById("teacher");
let filter = input.value.toUpperCase();
let select = document.getElementById("teachers");
let option = select.getElementsByTagName("option");
let found_first = false;
for (let i = 0; i < option.length; i++) {
let txtValue = option[i].textContent;
if (txtValue.toUpperCase().indexOf(filter) > -1) {
option[i].style.display = "";
if (!found_first) {
select.selectedIndex = i;
found_first = true;
}
} else {
option[i].style.display = "none";
if (option[i].hasAttribute("selected")) {
option[i].removeAttribute("selected");
}
}
}
}
if (Cookies.get("id") && Cookies.get("type")) {
if (Cookies.get("type") === "teacher" && Cookies.get("teacher")) {
make_teacher_table(Cookies.get("id"), Cookies.get("teacher"));
document.getElementById("dashboard").removeAttribute("hidden");
} else if (Cookies.get("type") === "student" && Cookies.get("class")) {
make_student_table(Cookies.get("id"), Cookies.get("class"));
document.getElementById("dashboard").removeAttribute("hidden");
}
} else {
document.getElementById("forms").removeAttribute("hidden");
load_pdfs();
}