oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1// The sidebar follows the person's event stream: when a task, a step, a
2// step's activity or a project changes, the server renders the sidebar
3// again (counts and what each specialist is doing) and it is swapped in
4// place, keeping each working specialist's living mark moving.
5// Requests run one at a time; one asked for during another runs once
6// after it. The same stream tells the work pages (work-stream.js) to
7// follow, and elapsed clocks tick here, on every page.
8(function () {
9 "use strict";
10 // Settings' "‹ Equipo" goes back to where the person was before
11 // Settings (the Conversation without scripts, or when there is none).
12 var BEFORE = "equipo.before-settings";
13 try {
14 var leave = document.querySelector("[data-settings-leave]");
15 if (!leave) sessionStorage.setItem(BEFORE, location.pathname + location.search + location.hash);
16 else if (sessionStorage.getItem(BEFORE)) leave.setAttribute("href", sessionStorage.getItem(BEFORE));
17 } catch (e) { /* the Conversation it is */ }
18 var rail = document.querySelector("[data-nav-stream]");
19 // Clocks: "4:12", then "1:04:12" past an hour. They are content, so
20 // they tick under reduced motion too.
21 function two(n) { return String(n).padStart(2, "0"); }
22 function clock(ms) {
23 var s = Math.max(0, Math.floor((Date.now() - ms) / 1000)), m = Math.floor(s / 60);
24 if (m >= 60) return Math.floor(m / 60) + ":" + two(m % 60) + ":" + two(s % 60);
25 return m + ":" + two(s % 60);
26 }
27 function tick() {
28 document.querySelectorAll("[equipo-clock][data-since]").forEach(function (el) {
29 var ms = Number(el.dataset.since);
30 if (ms) el.textContent = clock(ms);
31 });
32 }
33 tick();
34 setInterval(tick, 1000);
35 // Relative times ("12 min ago") keep current on their own, once a
36 // minute, in the server's words.
37 var words = {};
38 try { words = JSON.parse((rail && rail.dataset.agoFormats) || "{}"); } catch (e) {}
39 function ago(ms) {
40 var d = Date.now() - ms, M = 60000, H = 60 * M, D = 24 * H;
41 if (d < M) return words.now;
42 if (d < H) return words.minutes.replace("{n}", String(Math.round(d / M)));
43 if (d < D) return words.hours.replace("{n}", String(Math.round(d / H)));
44 if (d < 2 * D) return words.yesterday;
45 return words.days.replace("{n}", String(Math.floor(d / D)));
46 }
47 function ages() {
48 if (!words.now) return;
49 document.querySelectorAll("[equipo-ago][data-at]").forEach(function (el) {
50 var ms = Number(el.dataset.at);
51 if (ms) el.textContent = (el.dataset.format || "{t}").replace("{t}", ago(ms));
52 });
53 }
54 window.equipoAges = ages;
55 setInterval(ages, 60000);
56 function work(type) {
57 document.dispatchEvent(new CustomEvent("equipo:work", { detail: { type: type } }));
58 }
59 if (!rail) return;
60 if (!window.EventSource || !window.fetch) return;
61 var cursor = Number(rail.dataset.cursor) || 0;
62 var key = rail.dataset.key || "";
63 var source = null, failures = 0, inflight = null, again = false, timer = 0;
64
65 // What each specialist row says it is doing now, by row.
66 function doing() {
67 var out = {};
68 rail.querySelectorAll("[equipo-spec]").forEach(function (d) {
69 var now = d.querySelector("[equipo-spec-now]");
70 out[d.id] = now ? now.dataset.step + "|" + now.textContent : "";
71 });
72 return out;
73 }
74 function load() {
75 return fetch("/nav/rail?key=" + encodeURIComponent(key), { credentials: "same-origin", headers: { "Accept": "text/html" } })
76 .then(function (r) { if (!r.ok) throw new Error("rail"); return r.text(); })
77 .then(function (body) {
78 var doc = new DOMParser().parseFromString(body, "text/html");
79 var next = doc.querySelector("[data-nav-stream]");
80 if (!next) return;
81 var before = doing(), focus = document.activeElement && rail.contains(document.activeElement) ? document.activeElement.id || "" : "";
82 var sparks = {};
83 rail.querySelectorAll("[equipo-spec] [data-spec-star] [data-live]").forEach(function (s) { sparks[s.closest("[equipo-spec]").id] = s; });
84 // Rendered after the event that asked for it, so never older.
85 rail.dataset.cursor = next.dataset.cursor;
86 rail.innerHTML = next.innerHTML;
87 Object.keys(sparks).forEach(function (id) {
88 var slot = document.querySelector("#" + id + " [data-spec-star]");
89 if (slot) slot.replaceChildren(sparks[id]);
90 });
91 var after = doing();
92 Object.keys(after).forEach(function (id) {
93 if (before[id] !== undefined && after[id] && before[id] !== after[id]) {
94 var line = document.querySelector("#" + id + " [equipo-spec-doing]");
95 if (line) line.setAttribute("data-new", "");
96 }
97 });
98 if (focus) { var f = document.getElementById(focus); if (f) f.focus(); }
99 tick();
100 document.dispatchEvent(new CustomEvent("equipo:rail"));
101 });
102 }
103 function refresh() {
104 if (inflight) { again = true; return; }
105 inflight = load().catch(function () {}).finally(function () {
106 inflight = null;
107 if (again) { again = false; refresh(); }
108 });
109 }
110 function soon() {
111 clearTimeout(timer);
112 timer = setTimeout(refresh, 150);
113 }
114 function seen(event) {
115 failures = 0;
116 cursor = Math.max(cursor, Number(event.lastEventId) || 0);
117 }
118 function open() {
119 source = new EventSource("/api/v1/events?after=" + cursor);
120 ["task", "step", "project"].forEach(function (type) {
121 source.addEventListener(type, function (event) {
122 seen(event);
123 soon();
124 work(type);
125 });
126 });
127 source.addEventListener("activity", function (event) {
128 seen(event);
129 var data = {};
130 try { data = JSON.parse(event.data); } catch (e) {}
131 if (data.resource === "step") {
132 soon();
133 work("activity");
134 }
135 });
136 // A question's state (asked, answering, answered): the team panel's
137 // Ask box shows it.
138 source.addEventListener("question", function (event) {
139 seen(event);
140 work("question");
141 });
142 source.addEventListener("card", function (event) {
143 seen(event);
144 soon();
145 work("card");
146 });
147 // The Housekeeping view follows its own resource.
148 source.addEventListener("housekeeping", function (event) {
149 seen(event);
150 work("housekeeping");
151 });
152 ["message", "turn", "ping"].forEach(function (type) {
153 source.addEventListener(type, seen);
154 });
155 // A runner came online or went away: the Machines screens follow
156 // it from this stream (machines.js) rather than opening their own.
157 source.addEventListener("runner", function (event) {
158 seen(event);
159 document.dispatchEvent(new CustomEvent("equipo:runner"));
160 });
161 source.onerror = function () {
162 source.close();
163 // Past retention (410) or a lost session: render afresh, then
164 // reopen from the newest cursor.
165 failures++;
166 setTimeout(function () { load().catch(function () {}).finally(function () { cursor = Number(rail.dataset.cursor) || cursor; open(); work("reopen"); }); }, Math.min(30000, 1000 * failures));
167 };
168 }
169 open();
170})();
171