parental-control/static/script.js

123 lines
4.5 KiB
JavaScript
Raw Permalink Normal View History

2023-09-09 10:41:07 +02:00
function humanReadableDate(str) {
let date = new Date();
let today = date.toISOString().split("T")[0];
date.setDate(date.getDate() - 1);
let yesterday = date.toISOString().split("T")[0];
date.setDate(date.getDate() + 2);
let tomorrow = date.toISOString().split("T")[0];
return str.replace("T", " ").replaceAll(yesterday, "včera").replaceAll(today, "dnes").replaceAll(tomorrow, "zítra");
}
function refreshStatus(data) {
let status = document.getElementById("status");
let action, then_action;
if (data["action"]) {
action = "Povoleno"
then_action = "zakázáno"
} else {
action = "Zakázáno"
then_action = "povoleno"
}
let text = action;
if (data["until"]) {
text += " do " + humanReadableDate(data["until"]) + ", pak " + then_action;
if (data["then_until"]) {
text += " do " + humanReadableDate(data["then_until"]);
} else {
text += " napořád"
}
} else {
text += " napořád"
}
status.textContent = text;
}
function refreshOverrides(data) {
let overrides = document.getElementById("overrides");
overrides.innerHTML = "";
for (const record of data) {
let overrideText = document.createElement("span");
overrideText.textContent = (record["action"] ? "Povoleno" : "Zakázáno") + " od " + humanReadableDate(record["start"]) + " do " + humanReadableDate(record["end"]);
let overrideButton = document.createElement("input");
overrideButton.type = "button";
overrideButton.value = "❌";
overrideButton.addEventListener("click", async () => {
let response = await fetch("api/delete-override", {
method: "POST",
headers: {"Content-Type": "application/json"},
body: JSON.stringify({
action: record["action"],
start: record["start"],
end: record["end"]
}),
});
await refresh(await response.json());
})
overrides.appendChild(overrideText);
overrides.appendChild(overrideButton);
}
}
function refreshSchedules(data) {
for (const [day, entries] of Object.entries(data)) {
let schedule = "Zakázáno"
if (entries.length > 0) {
schedule = entries.map((entry) => entry["start"] + " - " + entry["end"]).join(", ");
}
document.getElementById("schedule-" + day).textContent = schedule;
}
}
async function refresh(data) {
data = data || await (await fetch("api/data")).json();
refreshStatus(data["status"]);
refreshOverrides(data["overrides"]);
refreshSchedules(data["schedules"]);
}
async function addOverride(e) {
e.preventDefault();
let response = await fetch("api/override", {
method: "POST",
headers: {"Content-Type": "application/json"},
body: JSON.stringify({
action: document.getElementById("override-action").value == "allow",
start: document.getElementById("override-start").value,
end: document.getElementById("override-end").value,
}),
});
await refresh(await response.json());
document.getElementById("add-override").reset();
}
async function updateSchedule(e) {
e.preventDefault();
let response = await fetch("api/schedule", {
method: "POST",
headers: {"Content-Type": "application/json"},
body: JSON.stringify({
action: document.getElementById("schedule-action").value == "allow",
days: Array.from(document.getElementById("schedule-days").selectedOptions).map(v => parseInt(v.value)),
start: document.getElementById("schedule-start").value,
end: document.getElementById("schedule-end").value
})
});
await refresh(await response.json());
document.getElementById("update-schedule").reset();
}
async function fastAction(e) {
let response = await fetch("api/fast-action/" + e.target.dataset.mins);
await refresh(await response.json());
}
window.addEventListener("DOMContentLoaded", () => {
refresh();
document.getElementById("add-override").addEventListener("submit", addOverride);
document.getElementById("update-schedule").addEventListener("submit", updateSchedule);
Array.from(document.getElementsByClassName("fast-action")).forEach(function(element) {
element.addEventListener("click", fastAction);
});
window.setInterval(refresh, 5000);
});