Files
LektorenPlan/web/web/static/app.js
T

201 lines
6.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* LektorenPlan – kleine progressive Verbesserungen für die Web-Oberfläche. */
(function () {
"use strict";
function onReady(fn) {
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", fn, { once: true });
} else {
fn();
}
}
/* Bestätigung vor destruktiven Formularen ------------------------------- */
function initConfirmations() {
document.querySelectorAll("form[data-confirm]").forEach(function (form) {
form.addEventListener("submit", function (event) {
var message = form.getAttribute("data-confirm");
if (message && !window.confirm(message)) {
event.preventDefault();
}
});
});
}
/* Gottesdienstplan filtern --------------------------------------------- */
function initPlanFilters() {
var table = document.querySelector("[data-plan-table]");
var chips = document.querySelectorAll("[data-plan-filter]");
if (!table || !chips.length) return;
var rows = Array.prototype.slice.call(table.querySelectorAll("[data-plan-row]"));
var tbody = table.querySelector("table.calendar-table tbody");
var empty = null;
if (tbody) {
empty = document.createElement("tr");
empty.className = "plan-filter-empty";
empty.innerHTML = '<td colspan="99">Keine Tage passen zu diesem Filter.</td>';
empty.hidden = true;
tbody.appendChild(empty);
}
var mobileEmpty = table.querySelector("[data-plan-mobile-empty]");
function apply(filter) {
var visible = 0;
rows.forEach(function (row) {
var show = true;
if (filter === "services") {
show = row.getAttribute("data-has-services") === "true";
} else if (filter === "open") {
show = parseInt(row.getAttribute("data-open") || "0", 10) > 0;
} else if (filter === "feasts") {
show = row.getAttribute("data-special") === "true";
}
row.hidden = !show;
if (show) visible++;
});
if (empty) empty.hidden = visible !== 0;
if (mobileEmpty) mobileEmpty.hidden = visible !== 0;
}
chips.forEach(function (chip) {
chip.addEventListener("click", function () {
chips.forEach(function (other) { other.classList.remove("active"); });
chip.classList.add("active");
apply(chip.getAttribute("data-plan-filter"));
});
});
}
/* Team-Suche ------------------------------------------------------------ */
function initPersonSearch() {
var input = document.querySelector("[data-person-search]");
var grid = document.querySelector("[data-person-grid]");
if (!input || !grid) return;
var cards = Array.prototype.slice.call(grid.querySelectorAll("[data-person-name]"));
var empty = document.querySelector("[data-search-empty]");
function apply() {
var needle = input.value.trim().toLowerCase();
var visible = 0;
cards.forEach(function (card) {
var haystack = card.getAttribute("data-person-name") || "";
var show = needle === "" || haystack.indexOf(needle) !== -1;
card.hidden = !show;
if (show) visible++;
});
if (empty) empty.hidden = visible !== 0 || needle === "";
}
input.addEventListener("input", apply);
}
/* Datei-Auswahl per Drag & Drop ---------------------------------------- */
function initFileDrop() {
document.querySelectorAll("[data-file-drop]").forEach(function (drop) {
var input = drop.querySelector("[data-file-input]");
if (!input) return;
function highlight(active) {
drop.classList.toggle("dragging", active);
}
["dragenter", "dragover"].forEach(function (name) {
drop.addEventListener(name, function (event) {
event.preventDefault();
highlight(true);
});
});
["dragleave", "drop"].forEach(function (name) {
drop.addEventListener(name, function (event) {
event.preventDefault();
highlight(false);
});
});
drop.addEventListener("drop", function (event) {
if (event.dataTransfer && event.dataTransfer.files.length) {
input.files = event.dataTransfer.files;
input.dispatchEvent(new Event("change", { bubbles: true }));
}
});
input.addEventListener("change", function () {
var label = drop.querySelector("small");
if (input.files && input.files.length && label) {
label.textContent = input.files[0].name;
}
});
});
}
/* Mobile Navigation ----------------------------------------------------- */
function initNavToggle() {
var toggle = document.querySelector("[data-nav-toggle]");
var sidebar = document.querySelector(".sidebar");
if (!toggle || !sidebar) return;
function setOpen(open) {
sidebar.classList.toggle("open", open);
document.body.classList.toggle("nav-open", open);
toggle.setAttribute("aria-expanded", open ? "true" : "false");
}
var close = document.querySelector("[data-nav-close]");
if (close) {
close.addEventListener("click", function () { setOpen(false); });
}
toggle.addEventListener("click", function (event) {
event.stopPropagation();
setOpen(!sidebar.classList.contains("open"));
});
sidebar.querySelectorAll("a").forEach(function (link) {
link.addEventListener("click", function () { setOpen(false); });
});
document.addEventListener("click", function (event) {
if (!sidebar.classList.contains("open")) return;
if (sidebar.contains(event.target) || toggle.contains(event.target)) return;
setOpen(false);
});
document.addEventListener("keydown", function (event) {
if (event.key === "Escape") setOpen(false);
});
}
/* Einheiten für Zahlenfelder ------------------------------------------- */
function initRangeOutputs() {
document.querySelectorAll('input[type="range"]').forEach(function (input) {
var output = document.getElementById(input.id + "-value");
if (!output) return;
function update() { output.textContent = input.value; }
input.addEventListener("input", update);
update();
});
}
/* Formular-Doppelversand verhindern ------------------------------------ */
function initSubmitGuards() {
document.querySelectorAll("form").forEach(function (form) {
if (form.hasAttribute("data-confirm")) return;
form.addEventListener("submit", function () {
var button = form.querySelector('button[type="submit"]');
if (!button) return;
window.setTimeout(function () {
button.disabled = true;
button.classList.add("is-busy");
}, 0);
});
});
}
onReady(function () {
initConfirmations();
initNavToggle();
initPlanFilters();
initPersonSearch();
initFileDrop();
initRangeOutputs();
initSubmitGuards();
});
})();