// Anubis: the few behaviours the server-rendered pages need. No libraries, no requests to other servers. (function () { "use strict"; // Collapsible menu, remembered on this device. var collapse = document.querySelector("[data-collapse]"); if (collapse) { var sync = function () { var closed = document.documentElement.dataset.nav === "collapsed"; collapse.setAttribute("aria-expanded", closed ? "false" : "true"); collapse.setAttribute("aria-label", closed ? "Espandi il menu" : "Comprimi il menu"); }; sync(); collapse.addEventListener("click", function () { var closed = document.documentElement.dataset.nav !== "collapsed"; if (closed) { document.documentElement.dataset.nav = "collapsed"; } else { delete document.documentElement.dataset.nav; } try { localStorage.setItem("anubis.nav", closed ? "collapsed" : "open"); } catch (e) { /* storage blocked */ } sync(); }); } // Selects that apply at once (currency, theme). document.querySelectorAll("select[data-autosubmit]").forEach(function (select) { select.addEventListener("change", function () { select.form.submit(); }); }); // Buttons that ask before acting ("Ferma tutto"). document.querySelectorAll("[data-confirm]").forEach(function (button) { button.addEventListener("click", function (event) { if (!window.confirm(button.getAttribute("data-confirm"))) { event.preventDefault(); } }); }); // First run: PostgreSQL fields only when chosen; "Prova la connessione" without leaving the page. var wizard = document.querySelector("[data-wizard]"); if (wizard) { var fields = wizard.querySelector("[data-postgres]"); var engines = wizard.querySelectorAll("[data-engine]"); var showFields = function () { if (!fields) { return; } var pg = wizard.querySelector("[data-engine]:checked"); fields.hidden = !(pg && pg.value === "postgres"); }; engines.forEach(function (radio) { radio.addEventListener("change", showFields); }); showFields(); var test = wizard.querySelector("[data-test-connection]"); var result = wizard.querySelector("[data-test-result]"); if (test && result) { test.addEventListener("click", function (event) { event.preventDefault(); result.textContent = "Provo…"; fetch("/azioni/primo-avvio/prova", { method: "POST", body: new FormData(wizard), credentials: "same-origin" }) .then(function (response) { return response.json(); }) .then(function (answer) { result.textContent = answer.message; result.className = "test-result " + (answer.ok ? "gain" : "loss"); }) .catch(function () { result.textContent = "Non riesco a fare la prova adesso. Riprova tra poco."; result.className = "test-result loss"; }); }); } } // Live status over the WebSocket: bot state, freshness of the data, and a discreet notice when the link drops. var age = document.querySelector("[data-live='age']"); if (!age || !("WebSocket" in window)) { return; } var lastAge = Number(age.getAttribute("data-age")) || 0; var lastAt = Date.now(); var lastMessage = Date.now(); var connected = false; var retry = 1000; var stale = document.querySelector("[data-stale]"); var staleText = document.querySelector("[data-stale-text]"); var ago = function (seconds) { seconds = Math.max(0, Math.floor(seconds)); if (seconds < 60) { return seconds + (seconds === 1 ? " secondo fa" : " secondi fa"); } var minutes = Math.floor(seconds / 60); if (minutes < 60) { return minutes + (minutes === 1 ? " minuto fa" : " minuti fa"); } var hours = Math.floor(minutes / 60); return hours + (hours === 1 ? " ora fa" : " ore fa"); }; var tick = function () { var seconds = lastAge + (Date.now() - lastAt) / 1000; age.textContent = "Dati aggiornati " + ago(seconds); var silent = (Date.now() - lastMessage) / 1000; if (stale && (!connected || silent > 15)) { stale.hidden = false; staleText.textContent = "Collegamento interrotto: i dati sono fermi da " + ago(seconds).replace(" fa", "") + ". Riprovo da solo."; } else if (stale) { stale.hidden = true; } }; var apply = function (status) { lastAge = status.ageSeconds; lastAt = Date.now(); document.querySelectorAll("[data-live='bot']").forEach(function (el) { el.setAttribute("data-state", status.bot); }); document.querySelectorAll("[data-live='bot-label']").forEach(function (el) { el.textContent = status.botLabel; }); document.querySelectorAll("[data-live='bot-short']").forEach(function (el) { el.textContent = status.botShort; }); var now = document.querySelector("[data-live='now']"); if (now) { now.textContent = status.now; } }; var connect = function () { var socket = new WebSocket((location.protocol === "https:" ? "wss://" : "ws://") + location.host + "/ws"); socket.onopen = function () { connected = true; retry = 1000; }; socket.onmessage = function (event) { lastMessage = Date.now(); try { var message = JSON.parse(event.data); if (message.type === "status") { apply(message.status); } } catch (e) { /* ignore a malformed message */ } }; socket.onclose = function () { connected = false; setTimeout(connect, retry); retry = Math.min(retry * 2, 30000); }; }; connect(); setInterval(tick, 1000); })();