app.js

97 Zeilen · 4,0 KB · JavaScript
Rohdatei
// Mobile Navigation
document.addEventListener("click", (e) => {
  if (e.target.closest(".menu-toggle")) document.body.classList.toggle("nav-open");
  else if (!e.target.closest(".sidebar")) document.body.classList.remove("nav-open");
});

// Globale Suche: Strg+K / Cmd+K oder "/" fokussiert das Suchfeld
document.addEventListener("keydown", (e) => {
  const box = document.getElementById("global-search");
  if (!box) return;
  const typing = /^(INPUT|TEXTAREA|SELECT)$/.test(document.activeElement.tagName);
  if (((e.ctrlKey || e.metaKey) && e.key.toLowerCase() === "k") || (e.key === "/" && !typing)) {
    e.preventDefault();
    document.body.classList.add("nav-open");
    box.focus();
    box.select();
  }
});

// Bestätigungsdialoge: <form data-confirm="Text">
document.addEventListener("submit", (e) => {
  const msg = e.target.dataset.confirm;
  const btn = e.submitter;
  const btnMsg = btn && btn.dataset.confirm;
  if ((btnMsg || msg) && !window.confirm(btnMsg || msg)) e.preventDefault();
});

// Kiosk: automatische Abmeldung bei Inaktivität
(function () {
  const body = document.body;
  if (!body.classList.contains("kiosk") || !body.dataset.timeout) return;
  const limit = parseInt(body.dataset.timeout, 10) * 60;
  let left = limit;
  const label = document.getElementById("kiosk-timer");
  const reset = () => { left = limit; };
  ["click", "keydown", "touchstart", "input"].forEach((ev) => document.addEventListener(ev, reset, { passive: true }));
  setInterval(() => {
    left -= 1;
    if (label) label.textContent = `Automatische Abmeldung in ${Math.floor(left / 60)}:${String(left % 60).padStart(2, "0")}`;
    if (left <= 0) {
      const f = document.getElementById("logout-form");
      if (f) f.submit(); else location.href = "/kiosk";
    }
  }, 1000);
})();

// PIN-Pad im Kiosk
(function () {
  const dlg = document.getElementById("pin-dialog");
  if (!dlg) return;
  const display = dlg.querySelector(".pin-display");
  const input = dlg.querySelector("input[name=pin]");
  const uid = dlg.querySelector("input[name=user_id]");
  const who = dlg.querySelector(".pin-who");
  const update = () => { display.textContent = "•".repeat(input.value.length); };
  document.querySelectorAll(".tile[data-user]").forEach((t) =>
    t.addEventListener("click", () => {
      uid.value = t.dataset.user; who.textContent = t.dataset.name; input.value = ""; update(); dlg.showModal();
    }));
  dlg.querySelectorAll("[data-key]").forEach((b) =>
    b.addEventListener("click", () => {
      const k = b.dataset.key;
      if (k === "del") input.value = input.value.slice(0, -1);
      else if (k === "ok") { if (input.value.length >= 4) dlg.querySelector("form").submit(); return; }
      else if (input.value.length < 8) input.value += k;
      update();
    }));
  document.addEventListener("keydown", (e) => {
    if (!dlg.open) return;
    if (/^[0-9]$/.test(e.key) && input.value.length < 8) input.value += e.key;
    else if (e.key === "Backspace") input.value = input.value.slice(0, -1);
    else if (e.key === "Enter" && input.value.length >= 4) dlg.querySelector("form").submit();
    update();
  });
  dlg.querySelector("[data-close]").addEventListener("click", () => dlg.close());
})();

// Lagerfrist nur bei verpackter Beladung
(function () {
  const sel = document.querySelector("select[name=packaged]");
  const box = document.getElementById("storage-field");
  if (!sel || !box) return;
  const sync = () => { box.style.display = sel.value === "1" ? "" : "none"; };
  sel.addEventListener("change", sync); sync();
})();

// Lade-Hinweis bei Uploads/Umwandlung (verhindert Doppelklicks)
document.addEventListener("submit", (e) => {
  if (e.defaultPrevented) return;
  const f = e.target;
  const btn = e.submitter;
  const hasFile = [...f.querySelectorAll("input[type=file]")].some((i) => i.files && i.files.length);
  const busy = (btn && btn.dataset.busy) || (hasFile ? "Wird hochgeladen …" : null);
  if (busy && btn) {
    setTimeout(() => { btn.disabled = true; btn.textContent = busy; }, 0);
  }
});