oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1// Settings save in the background (Paul: choosing an option made the
2// sliding nav play again, because each change posted and came back to
3// the page). Two kinds of control, never mixed in one group (Paul,
4// 2026-11-11):
5//
6// - At once (Pause, Prefer, a word set, Reset): the form posts with
7// fetch, the page is read again, only what changed is put back in
8// place, and a quiet "Saved" tick shows by the group's heading.
9// - Editable fields ([data-savebar-form]): Save in the bar at the foot
10// of the screen is off until something changes; then "Unsaved changes"
11// and Discard show beside it, and leaving the page asks first. Save
12// posts in the background and says "Saved" in the bar.
13//
14// No navigation, no reload. Without script every form posts as before.
15(function () {
16 "use strict";
17 var root = document.querySelector("[data-settings-live]");
18 if (!root || !window.fetch || !window.URLSearchParams) return;
19 var swaps = ["[equipo-conn-prefer]", "[data-hk-status]", "[equipo-notebook]"];
20 var template = document.querySelector("template[equipo-saved-template]");
21 var leaving = false;
22
23 // body is a form's fields, the ones a switch has turned off included:
24 // the server reads the hours whether or not quiet hours are on.
25 function body(form, submitter) {
26 var data = new FormData(form, submitter || undefined);
27 for (var i = 0; i < form.elements.length; i++) {
28 var el = form.elements[i];
29 if (!el.name || !el.matches(":disabled") || el.type === "submit" || el.type === "button" || el.type === "reset") continue;
30 if ((el.type === "checkbox" || el.type === "radio") && !el.checked) continue;
31 data.append(el.name, el.value);
32 }
33 return new URLSearchParams(data);
34 }
35
36 function post(form, submitter) {
37 return fetch(form.action, { method: "POST", credentials: "same-origin", headers: { "Accept": "application/json" }, body: body(form, submitter) })
38 .then(function (r) {
39 if (!r.ok) throw new Error("save");
40 return fetch(location.pathname + location.search, { credentials: "same-origin", headers: { "Accept": "text/html" } });
41 })
42 .then(function (r) {
43 if (!r.ok) throw new Error("read");
44 return r.text();
45 })
46 .then(function (html) { return new DOMParser().parseFromString(html, "text/html"); });
47 }
48
49 // fallback is the server's own answer, as without script.
50 function fallback(form) {
51 leaving = true;
52 HTMLFormElement.prototype.submit.call(form);
53 }
54
55 root.addEventListener("submit", function (event) {
56 var form = event.target;
57 if (!form.matches("form[method=post]")) return;
58 event.preventDefault();
59 if (form.matches("[data-savebar-form]")) save(form);
60 else apply(form, event.submitter);
61 });
62
63 // apply is a control that takes effect at once.
64 function apply(form, submitter) {
65 var group = form.closest("[equipo-sp-group]") || form.closest("[equipo-settings-section]") || root;
66 var id = group.id;
67 group.setAttribute("aria-busy", "true");
68 post(form, submitter)
69 .then(function (doc) {
70 refresh(form, doc);
71 group.removeAttribute("aria-busy");
72 // The group itself may have been put back (Status, the notebook).
73 var now = (id && document.getElementById(id)) || group;
74 now.removeAttribute("aria-busy");
75 tick(now);
76 })
77 .catch(function () {
78 group.removeAttribute("aria-busy");
79 fallback(form);
80 });
81 }
82
83 // refresh puts back the part of the page the change touched, and moves
84 // every form's revision on, so a form with unsaved changes keeps them
85 // and still saves over the new record.
86 function refresh(form, doc) {
87 for (var i = 0; i < swaps.length; i++) {
88 var mine = form.closest(swaps[i]), fresh = doc.querySelector(swaps[i]);
89 if (mine && fresh) {
90 mine.replaceWith(document.importNode(fresh, true));
91 break;
92 }
93 }
94 revisions(doc);
95 }
96
97 function revisions(doc) {
98 var seen = {};
99 root.querySelectorAll("form[method=post]").forEach(function (f) {
100 var action = f.getAttribute("action"), n = seen[action] || 0;
101 seen[action] = n + 1;
102 var next = doc.querySelectorAll('form[action="' + action + '"]')[n];
103 var a = f.querySelector("input[name=revision]"), b = next && next.querySelector("input[name=revision]");
104 if (a && b) a.value = a.defaultValue = b.value;
105 });
106 }
107
108 function tick(group) {
109 if (!template) return;
110 var head = group.querySelector("h2, h1") || group;
111 var t = head.querySelector("[equipo-saved-tick]");
112 if (!t) {
113 t = template.content.firstElementChild.cloneNode(true);
114 head.append(t);
115 }
116 t.removeAttribute("data-gone");
117 t.setAttribute("data-shown", "");
118 clearTimeout(t.timer);
119 t.timer = setTimeout(function () { t.setAttribute("data-gone", ""); }, 2400);
120 }
121
122 // A switch that turns other fields on and off: data-enables names the
123 // fieldset it governs.
124 function enables(form) {
125 form.querySelectorAll("[data-enables]").forEach(function (sw) {
126 var target = document.getElementById(sw.dataset.enables);
127 if (target) target.disabled = !sw.checked;
128 });
129 }
130
131 // The savebar forms.
132 var bars = [];
133 root.querySelectorAll("[data-savebar-form]").forEach(function (form) {
134 var bar = {
135 form: form,
136 save: form.querySelector("[data-savebar-save]"),
137 discard: form.querySelector("[data-savebar-discard]"),
138 unsaved: form.querySelector("[data-savebar-unsaved]"),
139 saved: form.querySelector("[data-savebar-saved]"),
140 busy: false,
141 timer: 0
142 };
143 form._bar = bar;
144 bar.base = state(form);
145 bars.push(bar);
146 enables(form);
147 show(bar);
148 form.addEventListener("input", function () { changed(bar); });
149 form.addEventListener("change", function (e) {
150 if (e.target.matches("[data-enables]")) enables(form);
151 changed(bar);
152 });
153 // Discard is the form's reset: back to what was last saved.
154 form.addEventListener("reset", function () {
155 setTimeout(function () {
156 enables(form);
157 changed(bar);
158 });
159 });
160 });
161
162 // state is what a person can change in the form, for "has it changed".
163 function state(form) {
164 var out = [];
165 for (var i = 0; i < form.elements.length; i++) {
166 var el = form.elements[i];
167 if (!el.name || el.type === "hidden" || el.type === "submit" || el.type === "button" || el.type === "reset") continue;
168 out.push(el.name + "=" + ((el.type === "checkbox" || el.type === "radio") ? el.checked : el.value));
169 }
170 return out.join("&");
171 }
172
173 function dirty(bar) { return state(bar.form) !== bar.base; }
174
175 function changed(bar) {
176 if (dirty(bar)) {
177 clearTimeout(bar.timer);
178 bar.saved.hidden = true;
179 }
180 show(bar);
181 }
182
183 function show(bar) {
184 var d = dirty(bar);
185 bar.save.disabled = !d || bar.busy;
186 bar.discard.hidden = !d || bar.busy;
187 bar.unsaved.hidden = !d;
188 bar.form.toggleAttribute("data-dirty", d);
189 }
190
191 function save(form) {
192 var bar = form._bar;
193 if (!bar || bar.busy || !dirty(bar)) return;
194 bar.busy = true;
195 bar.save.setAttribute("aria-busy", "true");
196 show(bar);
197 post(form)
198 .then(function (doc) {
199 revisions(doc);
200 // What was saved is now what Discard goes back to.
201 for (var i = 0; i < form.elements.length; i++) {
202 var el = form.elements[i];
203 if (el.type === "checkbox" || el.type === "radio") el.defaultChecked = el.checked;
204 else if (el.tagName === "SELECT") for (var j = 0; j < el.options.length; j++) el.options[j].defaultSelected = el.options[j].selected;
205 else if (el.type !== "hidden") el.defaultValue = el.value;
206 }
207 bar.base = state(form);
208 bar.busy = false;
209 bar.save.removeAttribute("aria-busy");
210 show(bar);
211 bar.saved.hidden = false;
212 clearTimeout(bar.timer);
213 bar.timer = setTimeout(function () { bar.saved.hidden = true; }, 2400);
214 tick(form);
215 })
216 .catch(function () {
217 bar.busy = false;
218 bar.save.removeAttribute("aria-busy");
219 fallback(form);
220 });
221 }
222
223 // Leaving with unsaved changes asks first.
224 window.addEventListener("beforeunload", function (e) {
225 if (leaving) return;
226 for (var i = 0; i < bars.length; i++) {
227 if (dirty(bars[i])) {
228 e.preventDefault();
229 e.returnValue = "";
230 return;
231 }
232 }
233 });
234})();
235
236// Teams (Paul, 2026-11-11): Forget is a quiet action with Undo. The row
237// goes at once with "Studio forgotten. Undo" in its place; the link is
238// forgotten a few seconds later, or as the page is left. Undo before
239// then keeps it, last sign-in and all. Without script Forget posts.
240(function () {
241 "use strict";
242 var list = document.querySelector("[data-team-list]");
243 var undo = document.querySelector("template[data-team-undo-template]");
244 if (!list || !undo || !window.fetch || !window.URLSearchParams) return;
245 var WAIT = 6000;
246 var pending = [];
247
248 list.addEventListener("submit", function (e) {
249 var form = e.target;
250 if (!form.matches("[data-team-forget-form]")) return;
251 e.preventDefault();
252 var row = form.closest("[data-team-row]");
253 row.insertAdjacentHTML("afterend", undo.innerHTML);
254 var note = row.nextElementSibling;
255 note.querySelector("[data-team-gone-text]").textContent = (list.dataset.forgotten || "%s").replace("%s", row.dataset.teamName || "");
256 row.hidden = true;
257 var p = { row: row, form: form, note: note };
258 p.timer = setTimeout(function () { commit(p); }, WAIT);
259 pending.push(p);
260 var button = note.querySelector("[data-team-undo]");
261 button.addEventListener("click", function () {
262 clearTimeout(p.timer);
263 pending.splice(pending.indexOf(p), 1);
264 note.remove();
265 row.hidden = false;
266 var link = row.querySelector("a");
267 if (link) link.focus();
268 });
269 button.focus();
270 });
271
272 function commit(p) {
273 var at = pending.indexOf(p);
274 if (at < 0) return;
275 pending.splice(at, 1);
276 fetch(p.form.action, { method: "POST", credentials: "same-origin", keepalive: true, headers: { "Accept": "application/json" }, body: new URLSearchParams(new FormData(p.form)) })
277 .then(function (r) {
278 if (!r.ok) throw new Error("forget");
279 p.note.remove();
280 p.row.remove();
281 // The last one gone: say there are none, as the page would.
282 if (!list.querySelector("[data-team-row], [data-team-forgotten]")) {
283 var none = document.createElement("p");
284 none.setAttribute("equipo-sp-empty", "");
285 none.setAttribute("data-empty-state", "teams.none");
286 none.textContent = list.dataset.none || "";
287 list.replaceWith(none);
288 }
289 })
290 .catch(function () { HTMLFormElement.prototype.submit.call(p.form); });
291 }
292
293 // Leaving the page forgets what is waiting: Undo is no longer there.
294 window.addEventListener("pagehide", function () {
295 pending.slice().forEach(function (p) {
296 clearTimeout(p.timer);
297 commit(p);
298 });
299 });
300})();
301
302// Add a team: the name from the address where it says one ("studio" in
303// studio.example.com), until the person types their own.
304(function () {
305 "use strict";
306 var origin = document.querySelector("[data-team-origin]");
307 var name = document.querySelector("[data-team-add] [data-team-name]");
308 if (!origin || !name) return;
309 var guessed = "";
310 origin.addEventListener("change", function () {
311 if (name.value && name.value !== guessed) return;
312 var host = "";
313 try { host = new URL(origin.value).hostname; } catch (e) { return; }
314 var parts = host.split(".").filter(function (p) { return p && !/^(www|app|equipo)$/i.test(p); });
315 if (parts.length < 2 || /^\d+$/.test(parts[0])) return;
316 guessed = parts[0].charAt(0).toUpperCase() + parts[0].slice(1);
317 name.value = guessed;
318 });
319})();
320