| 1 | // The Machines screens follow the person's event stream: nav-stream.js |
| 2 | // hears a runner come online or go away and says so (equipo:runner); |
| 3 | // this reads the screen again and swaps its live part in place, so a |
| 4 | // machine just approved on Add a machine shows connecting without a |
| 5 | // reload. It also reads it again each minute while it is seen, as a |
| 6 | // machine whose runner stops answering goes offline with no event. |
| 7 | // Rows open stay open; nothing is read again while a confirmation is |
| 8 | // open or a control in the live part has focus. The address read is |
| 9 | // always the screen's own without its code: a refresh never inspects a |
| 10 | // machine's code again. Copy puts the install line on the clipboard. |
| 11 | (function () { |
| 12 | "use strict"; |
| 13 | var root = document.querySelector("[data-machines]"); |
| 14 | if (!root || !window.fetch || !window.DOMParser) return; |
| 15 | var src = root.dataset.src; |
| 16 | var inflight = null, again = false; |
| 17 | |
| 18 | function busy(box) { |
| 19 | return box.querySelector("[equipo-mach-confirm][open]") || box.contains(document.activeElement) && document.activeElement !== document.body && !document.activeElement.matches("summary"); |
| 20 | } |
| 21 | function load() { |
| 22 | var box = root.querySelector("[data-machines-live]"); |
| 23 | if (!box || busy(box)) return Promise.resolve(); |
| 24 | return fetch(src, { credentials: "same-origin", headers: { "Accept": "text/html" } }) |
| 25 | .then(function (r) { if (!r.ok) throw new Error("refresh"); return r.text(); }) |
| 26 | .then(function (body) { |
| 27 | var next = new DOMParser().parseFromString(body, "text/html").querySelector("[data-machines-live]"); |
| 28 | if (!next || busy(box)) return; |
| 29 | var open = {}, seen = {}; |
| 30 | box.querySelectorAll("[data-machine]").forEach(function (el) { |
| 31 | seen[el.dataset.machine] = true; |
| 32 | if (el.open) open[el.dataset.machine] = true; |
| 33 | }); |
| 34 | var others = box.querySelector("[data-other-machines]"); |
| 35 | next.querySelectorAll("[data-machine]").forEach(function (el) { |
| 36 | if (open[el.dataset.machine]) el.open = true; |
| 37 | // Only what is new arrives with its motion. |
| 38 | if (!seen[el.dataset.machine]) el.setAttribute("data-new", ""); |
| 39 | }); |
| 40 | var folded = next.querySelector("[data-other-machines]"); |
| 41 | if (others && folded && others.open) folded.open = true; |
| 42 | box.replaceChildren.apply(box, Array.from(next.children)); |
| 43 | setTimeout(function () { |
| 44 | box.querySelectorAll("[data-machine][data-new]").forEach(function (el) { el.removeAttribute("data-new"); }); |
| 45 | }, 2800); |
| 46 | }); |
| 47 | } |
| 48 | function refresh() { |
| 49 | if (inflight) { again = true; return; } |
| 50 | inflight = load().catch(function () {}).finally(function () { |
| 51 | inflight = null; |
| 52 | if (again) { again = false; refresh(); } |
| 53 | }); |
| 54 | } |
| 55 | document.addEventListener("equipo:runner", function () { setTimeout(refresh, 150); }); |
| 56 | var ticked = Date.now(); |
| 57 | function tick() { |
| 58 | if (document.hidden) return; |
| 59 | ticked = Date.now(); |
| 60 | refresh(); |
| 61 | } |
| 62 | setInterval(tick, 60000); |
| 63 | document.addEventListener("visibilitychange", function () { |
| 64 | if (Date.now() - ticked >= 60000) tick(); |
| 65 | }); |
| 66 | |
| 67 | // Copy: the clipboard where it can, the line selected otherwise. |
| 68 | root.addEventListener("click", function (event) { |
| 69 | var button = event.target.closest("[data-copy]"); |
| 70 | if (!button) return; |
| 71 | var label = button.textContent; |
| 72 | function done() { |
| 73 | button.textContent = button.dataset.copied; |
| 74 | button.setAttribute("data-done", ""); |
| 75 | setTimeout(function () { button.textContent = label; button.removeAttribute("data-done"); }, 1800); |
| 76 | } |
| 77 | function select() { |
| 78 | var row = button.closest("[equipo-conn-code-row]"), code = row && row.querySelector("code"); |
| 79 | if (!code) return; |
| 80 | getSelection().selectAllChildren(code); |
| 81 | try { if (document.execCommand("copy")) done(); } catch (e) { /* selected for the person to copy */ } |
| 82 | } |
| 83 | if (navigator.clipboard && window.isSecureContext) navigator.clipboard.writeText(button.dataset.copy).then(done, select); |
| 84 | else select(); |
| 85 | }); |
| 86 | })(); |
| 87 | |