oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1// The team panel. Each specialist row in the sidebar is a link to that
2// specialist's page (/team/{specialist}); with script it opens the same
3// page as a drawer over the one the person is on. The drawer follows the
4// event stream (nav-stream.js says so with equipo:work) while it is
5// open, keeping a question being typed, the focus and any open command.
6// Its Ask box posts in the background. The still marks, in the sidebar
7// and the drawer, come alive as the shared Stardust (stardust.js).
8(function () {
9 "use strict";
10 var dust = window.EquipoStardust;
11 function enliven(scope) {
12 if (!dust || !scope) return;
13 scope.querySelectorAll("[data-spec-star]").forEach(function (slot) {
14 if (slot.querySelector("[data-live]")) return;
15 var live = dust.spark(dust.look);
16 live.setAttribute("data-live", "");
17 slot.replaceChildren(live);
18 });
19 }
20 enliven(document);
21 // nav-stream.js swaps the sidebar in and keeps the marks that were
22 // already alive; the new ones come alive here.
23 document.addEventListener("equipo:rail", function () { enliven(document.querySelector("#rail")); });
24
25 // The Ask box, in the drawer or on the page: posted in the background,
26 // the question then shows in the panel with its answer when it comes.
27 document.addEventListener("submit", function (e) {
28 var form = e.target.closest && e.target.closest("[data-team-ask]");
29 if (!form || !window.fetch) return;
30 e.preventDefault();
31 var button = form.querySelector("button[type=submit]"), error = form.querySelector("[data-ask-error]");
32 var id = form.querySelector("[name=request_id]");
33 if (button.disabled) return;
34 button.disabled = true;
35 if (error) error.hidden = true;
36 fetch(form.action, { method: "POST", credentials: "same-origin", headers: { "Accept": "application/json", "Content-Type": "application/x-www-form-urlencoded" }, body: new URLSearchParams(new FormData(form)) })
37 .then(function (r) { if (!r.ok) throw new Error("ask"); return r.json(); })
38 .then(function () {
39 form.querySelector("textarea").value = "";
40 // A fresh key: the next question is a new one.
41 if (id && window.crypto && crypto.randomUUID) id.value = crypto.randomUUID();
42 refresh();
43 })
44 .catch(function () { if (error) error.hidden = false; })
45 .finally(function () { button.disabled = false; });
46 });
47
48 if (!window.fetch || !window.DOMParser || typeof HTMLDialogElement !== "function") return;
49 var still = window.matchMedia ? matchMedia("(prefers-reduced-motion: reduce)") : null;
50 var dialog = null, href = "", from = "", inflight = null, again = false, timer = 0;
51
52 function ensure() {
53 if (dialog) return dialog;
54 dialog = document.createElement("dialog");
55 dialog.setAttribute("equipo-drawer", "");
56 dialog.setAttribute("aria-labelledby", "team-title");
57 document.body.append(dialog);
58 // A click on the scrim (the dialog itself, outside its panel) or on
59 // the close button closes it; so does Escape, natively.
60 dialog.addEventListener("click", function (e) {
61 if (e.target === dialog || (e.target.closest && e.target.closest("[data-team-close]"))) close();
62 });
63 dialog.addEventListener("cancel", function (e) { e.preventDefault(); close(); });
64 dialog.addEventListener("close", function () {
65 href = "";
66 dialog.removeAttribute("data-leaving");
67 dialog.replaceChildren();
68 var back = from && document.getElementById(from);
69 if (back) back.focus();
70 });
71 return dialog;
72 }
73 function close() {
74 if (!dialog || !dialog.open || dialog.hasAttribute("data-leaving")) return;
75 if (still && still.matches) { dialog.close(); return; }
76 dialog.setAttribute("data-leaving", "");
77 setTimeout(function () { if (dialog.open) dialog.close(); }, 180);
78 }
79 function load(url) {
80 return fetch(url, { credentials: "same-origin", headers: { "Accept": "text/html" } })
81 .then(function (r) {
82 if (r.status === 404) { var gone = new Error("team"); gone.gone = true; throw gone; }
83 if (!r.ok) throw new Error("team");
84 return r.text();
85 })
86 .then(function (body) {
87 var next = new DOMParser().parseFromString(body, "text/html").querySelector("[equipo-team]");
88 if (!next) throw new Error("team");
89 return next;
90 });
91 }
92 // place puts a freshly rendered panel in the drawer, keeping what the
93 // person was doing in the old one.
94 function place(next) {
95 var d = ensure(), old = d.querySelector("[equipo-team]");
96 var shut = next.querySelector("[data-team-close]");
97 if (shut) shut.hidden = false;
98 if (old) {
99 var draft = old.querySelector("[data-team-ask] textarea"), typed = draft ? draft.value : "", focused = draft && document.activeElement === draft;
100 var opened = [].filter.call(old.querySelectorAll("[equipo-team-raw]"), function (x) { return x.open; }).map(function (x) { return x.textContent; });
101 var top = old.scrollTop;
102 var oldKey = old.querySelector("[data-team-ask] [name=request_id]");
103 // A live mark that is still wanted keeps moving rather than restarting.
104 var spark = old.querySelector("[equipo-team-state] [data-live]"), slot = next.querySelector("[equipo-team-state] [data-spec-star]");
105 if (spark && slot) slot.replaceChildren(spark);
106 old.replaceWith(next);
107 next.scrollTop = top;
108 var box = next.querySelector("[data-team-ask] textarea");
109 if (box && draft && next.querySelector("[data-ask-task]") && old.querySelector("[data-ask-task]") && next.querySelector("[data-ask-task]").dataset.askTask === old.querySelector("[data-ask-task]").dataset.askTask) {
110 box.value = typed;
111 var key = next.querySelector("[data-team-ask] [name=request_id]");
112 if (key && oldKey) key.value = oldKey.value;
113 if (focused) box.focus();
114 }
115 next.querySelectorAll("[equipo-team-raw]").forEach(function (x) { if (opened.indexOf(x.textContent) >= 0) x.open = true; });
116 } else {
117 d.append(next);
118 }
119 enliven(next);
120 if (window.equipoAges) window.equipoAges();
121 }
122 function open(url, row) {
123 href = url;
124 from = row || "";
125 load(url).then(function (next) {
126 if (href !== url) return;
127 var d = ensure();
128 place(next);
129 if (!d.open) d.showModal();
130 var c = d.querySelector("[data-team-close]");
131 if (c) c.focus();
132 }).catch(function () { location.assign(url); });
133 }
134 function refresh() {
135 if (!href) return;
136 if (inflight) { again = true; return; }
137 var url = href;
138 // A specialist no longer on the team (Design revoked) closes its
139 // drawer: its Ask form would reach whoever has the kind's steps now.
140 inflight = load(url).then(function (next) { if (href === url) place(next); }).catch(function (e) { if (e && e.gone && href === url) close(); }).finally(function () {
141 inflight = null;
142 if (again) { again = false; refresh(); }
143 });
144 }
145 document.addEventListener("click", function (e) {
146 var a = e.target.closest && e.target.closest("a[data-team-open]");
147 if (!a || e.defaultPrevented || e.button !== 0 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) return;
148 e.preventDefault();
149 open(a.getAttribute("href"), a.id);
150 });
151 document.addEventListener("equipo:work", function () {
152 if (!href) return;
153 clearTimeout(timer);
154 timer = setTimeout(refresh, 200);
155 });
156})();
157