cornfield 0.62.0 → 0.64.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/core/release-notes.js +2 -2
- package/explorer/css/changes.css +3 -7
- package/explorer/css/header.css +15 -0
- package/explorer/css/layers.css +11 -2
- package/explorer/css/map.css +0 -1
- package/explorer/css/node-panel.css +45 -0
- package/explorer/css/page.css +2 -7
- package/explorer/css/proposal.css +2 -3
- package/explorer/css/work.css +7 -7
- package/explorer/engine/hint.js +7 -7
- package/explorer/engine/keyboard.js +5 -4
- package/explorer/engine/map-rect.js +2 -2
- package/explorer/engine/render.js +2 -1
- package/explorer/forge-panel-model.js +24 -0
- package/explorer/history-model.js +21 -11
- package/explorer/history-replay.js +13 -5
- package/explorer/history.js +16 -7
- package/explorer/inbox-model.js +180 -0
- package/explorer/index.html +6 -3
- package/explorer/page.js +1 -2
- package/explorer/panel-model.js +103 -0
- package/explorer/panel-sections.js +140 -0
- package/explorer/pieces/inbox.js +201 -0
- package/explorer/pieces/layers.js +27 -6
- package/explorer/pieces/node-panel.js +78 -1
- package/explorer/pieces/search.js +22 -9
- package/explorer/pieces/work-deploys.js +13 -13
- package/explorer/pieces/work-issues.js +10 -5
- package/explorer/pieces/work-list.js +72 -9
- package/explorer/pieces/work-paint.js +45 -10
- package/explorer/pieces/work-prs.js +46 -34
- package/explorer/pieces/work-runs.js +17 -5
- package/explorer/pieces/work-specs.js +24 -4
- package/explorer/pieces/work-threads.js +59 -15
- package/explorer/pieces/work.js +6 -3
- package/explorer/proposal-model.js +3 -6
- package/explorer/runs-view.js +42 -8
- package/explorer/scene.js +5 -1
- package/explorer/work-model.js +266 -55
- package/explorer/work-view.css +60 -16
- package/explorer/work-view.js +175 -35
- package/package.json +1 -1
- package/server/server.js +1 -1
- package/server/work-graph.js +65 -8
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module explorer/inbox-model
|
|
3
|
+
*
|
|
4
|
+
* The Work inbox's rules, the pure half (spec 0016 D2 to D4): no DOM, no `window`, no clock of its own. From the server's work graph
|
|
5
|
+
* (server/work-graph.js: `{ nodes, edges }`) it works out which group each piece of work is in, why (in words), its one action, the order, and
|
|
6
|
+
* a waiting run folded into the spec it asks about. The page draws it (explorer/pieces/inbox.js).
|
|
7
|
+
*
|
|
8
|
+
* inboxOf(graph, { now }) -> { groups: { waiting, move, unclaimed, motion, done }, rows, drafts, count, rowsWaiting }
|
|
9
|
+
*
|
|
10
|
+
* A row: `{ id, item, group, why, bad, action, counted, also }`. `action` is `{ key, label }`, the key one of ACTIONS (what the node panel already does);
|
|
11
|
+
* `counted` is spec 0010 D1's rule (`item.waiting`), so `count` is the number the Work button shows; `also` is the row of a waiting run folded into it (D4).
|
|
12
|
+
* Nothing here files, archives or hides a row by hand (D7): a row moves only when the graph says its state moved.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { tagOf } from './work-model.js';
|
|
16
|
+
|
|
17
|
+
/** The groups, in the queue's order (D2), with their words, and whether each starts open */
|
|
18
|
+
export const GROUPS = Object.freeze([
|
|
19
|
+
{ key: 'waiting', title: 'Waiting on you', hint: '', open: true },
|
|
20
|
+
{ key: 'move', title: 'Your move', hint: 'not counted', open: true },
|
|
21
|
+
{ key: 'unclaimed', title: 'Nobody on it', hint: 'not counted', open: true },
|
|
22
|
+
{ key: 'motion', title: 'In motion', hint: '', open: false },
|
|
23
|
+
{ key: 'done', title: 'Done lately', hint: '', open: false },
|
|
24
|
+
]);
|
|
25
|
+
export const GROUP_KEYS = Object.freeze(GROUPS.map((g) => g.key));
|
|
26
|
+
|
|
27
|
+
/** The actions a row offers: each is one the node panel already has (spec 0012 D5), with its confirm step and server checks */
|
|
28
|
+
export const ACTIONS = Object.freeze({
|
|
29
|
+
review: 'Review',
|
|
30
|
+
'review-change': 'Review the change',
|
|
31
|
+
answer: 'Answer',
|
|
32
|
+
land: 'Land it',
|
|
33
|
+
rerun: 'Re-run failed',
|
|
34
|
+
retry: 'Start again',
|
|
35
|
+
agent: 'Start an agent',
|
|
36
|
+
reply: 'Reply',
|
|
37
|
+
logs: 'Logs',
|
|
38
|
+
});
|
|
39
|
+
const act = (key, label = ACTIONS[key]) => ({ key, label });
|
|
40
|
+
|
|
41
|
+
const text = (v) => (typeof v === 'string' ? v : '');
|
|
42
|
+
const cap = (s) => text(s).replace(/-/g, ' ').replace(/^./, (c) => c.toUpperCase());
|
|
43
|
+
const plural = (n, one, many = `${one}s`) => `${n} ${n === 1 ? one : many}`;
|
|
44
|
+
|
|
45
|
+
/** "lint failed · test running · 1 passed", from a pull request's `checks` (server/work-graph.js `checkCounts`); '' with none */
|
|
46
|
+
export function checkWords(checks) {
|
|
47
|
+
if (!checks) return '';
|
|
48
|
+
const names = (list, n) => (list && list.length ? list.join(', ') + (n > list.length ? ` and ${n - list.length} more` : '') : plural(n, 'check'));
|
|
49
|
+
return [
|
|
50
|
+
checks.failed ? `${names(checks.failedNames, checks.failed)} failed` : '',
|
|
51
|
+
checks.running ? `${names(checks.runningNames, checks.running)} running` : '',
|
|
52
|
+
checks.passed ? `${checks.passed} passed` : '',
|
|
53
|
+
].filter(Boolean).join(' · ');
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* A thread folded into a spec (spec 0015 D2): every line of it is `discusses` or `became` to a proposal. Such a thread is not a row.
|
|
58
|
+
* @param {string} id
|
|
59
|
+
* @param {{ from: string, to: string, rel: string }[]} edges
|
|
60
|
+
* @param {Map<string, any>} byId
|
|
61
|
+
*/
|
|
62
|
+
export function isFoldedThread(id, edges, byId) {
|
|
63
|
+
const mine = edges.filter((e) => e.from === id || e.to === id);
|
|
64
|
+
if (!mine.length) return false;
|
|
65
|
+
return mine.every((e) => e.from === id && (e.rel === 'discusses' || e.rel === 'became') && byId.has(e.to) && byId.get(e.to).kind === 'proposal');
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Where a piece of work goes, and why (D2). Null: it is not a row (a folded thread, a local draft: drafts are a count).
|
|
70
|
+
* @param {any} item a node of the server's graph
|
|
71
|
+
* @param {{ edges: any[], byId: Map<string, any> }} ctx
|
|
72
|
+
* @returns {{ group: string, why: string, bad?: boolean, action: { key: string, label: string } | null } | null}
|
|
73
|
+
*/
|
|
74
|
+
export function placeOf(item, { edges, byId }) {
|
|
75
|
+
switch (item.kind) {
|
|
76
|
+
case 'proposal':
|
|
77
|
+
if (item.state === 'local') return null;
|
|
78
|
+
if (item.waiting && item.state === 'changed') return { group: 'waiting', why: 'Changed since you approved it', action: act('review-change') };
|
|
79
|
+
if (item.waiting) return { group: 'waiting', why: 'Ready for your decision', action: act('review') };
|
|
80
|
+
if (!item.open) return { group: 'done', why: cap(item.state), action: null };
|
|
81
|
+
if (item.state === 'approved') return { group: 'move', why: 'Approved: landing it merges the spec', action: act('land') };
|
|
82
|
+
if (item.state === 'sent-back') return { group: 'motion', why: 'Sent back: with the agent until it pushes', action: null };
|
|
83
|
+
if (item.state === 'draft-pr') return { group: 'motion', why: 'A draft pull request: the agent is still writing it', action: null };
|
|
84
|
+
return { group: 'motion', why: cap(item.state), action: null };
|
|
85
|
+
case 'run': {
|
|
86
|
+
if (item.waiting) return { group: 'waiting', why: text(item.note) || 'Waiting on your answer', action: act('answer') };
|
|
87
|
+
if (item.state === 'failed') return { group: 'move', why: `Failed${item.note ? `: ${item.note}` : ''}`, bad: true, action: act('retry') };
|
|
88
|
+
if (item.state === 'done' && item.pull && item.pull.state === 'open') return { group: 'move', why: `Done: PR #${item.pull.number} is open${item.note ? `. ${item.note}` : ''}`, action: act('land', `Land PR #${item.pull.number}`) };
|
|
89
|
+
if (item.open) return { group: 'motion', why: `${cap(item.state)}${item.note ? `: ${item.note}` : ''}`, action: null };
|
|
90
|
+
return { group: 'done', why: text(item.note) || cap(item.state), action: null };
|
|
91
|
+
}
|
|
92
|
+
case 'pull': {
|
|
93
|
+
if (!item.open) return { group: 'done', why: cap(item.state), action: null };
|
|
94
|
+
const words = checkWords(item.checks);
|
|
95
|
+
if (item.checks && item.checks.failed) return { group: 'move', why: words, bad: true, action: act('rerun') };
|
|
96
|
+
return { group: 'motion', why: words || (item.draft ? 'Draft' : 'Open'), action: null };
|
|
97
|
+
}
|
|
98
|
+
case 'issue': {
|
|
99
|
+
if (!item.open) return { group: 'done', why: 'Closed', action: null };
|
|
100
|
+
const live = edges.filter((e) => e.from === item.id && e.rel === 'agent-on').map((e) => byId.get(e.to)).filter((r) => r && r.open);
|
|
101
|
+
if (live.length) return { group: 'motion', why: `${plural(live.length, 'agent run')} on it`, action: null };
|
|
102
|
+
return { group: 'unclaimed', why: 'Nobody is on it', action: act('agent') };
|
|
103
|
+
}
|
|
104
|
+
case 'thread': {
|
|
105
|
+
if (isFoldedThread(item.id, edges, byId)) return null;
|
|
106
|
+
if (!item.open) return { group: 'done', why: 'Closed', action: null };
|
|
107
|
+
const spec = edges.some((e) => e.from === item.id && (e.rel === 'became' || e.rel === 'discusses'));
|
|
108
|
+
if (spec) return { group: 'motion', why: 'A spec is being written from it', action: null };
|
|
109
|
+
return { group: 'unclaimed', why: 'No spec yet', action: act('reply') };
|
|
110
|
+
}
|
|
111
|
+
case 'deploy':
|
|
112
|
+
if (item.state === 'running' || item.state === 'queued') return { group: 'motion', why: cap(item.state), action: act('logs') };
|
|
113
|
+
return { group: 'done', why: item.state === 'failure' ? 'Failed' : cap(item.state), bad: item.state === 'failure', action: null };
|
|
114
|
+
default: return null;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const time = (iso) => { const t = Date.parse(text(iso)); return Number.isFinite(t) ? t : 0; };
|
|
119
|
+
/** Oldest first, so what has waited longest is on top (D2); Done lately newest first; then by id, so the order is the same for the same graph */
|
|
120
|
+
const oldest = (a, b) => time(a.item.updated) - time(b.item.updated) || (a.id < b.id ? -1 : a.id > b.id ? 1 : 0);
|
|
121
|
+
const newest = (a, b) => time(b.item.updated) - time(a.item.updated) || (a.id < b.id ? -1 : a.id > b.id ? 1 : 0);
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* The inbox of a work graph (D2 to D4).
|
|
125
|
+
* @param {{ nodes?: any[], edges?: any[] } | null} graph the server's answer
|
|
126
|
+
*/
|
|
127
|
+
export function inboxOf(graph) {
|
|
128
|
+
const nodes = (graph && Array.isArray(graph.nodes) ? graph.nodes : []).filter((n) => n && n.kind !== 'code' && typeof n.id === 'string');
|
|
129
|
+
const edges = graph && Array.isArray(graph.edges) ? graph.edges : [];
|
|
130
|
+
const byId = new Map(nodes.map((n) => [n.id, n]));
|
|
131
|
+
const rows = [];
|
|
132
|
+
let drafts = 0;
|
|
133
|
+
for (const item of nodes) {
|
|
134
|
+
if (item.kind === 'proposal' && item.state === 'local') { drafts += 1; continue; }
|
|
135
|
+
const place = placeOf(item, { edges, byId });
|
|
136
|
+
if (!place) continue;
|
|
137
|
+
rows.push({ id: item.id, item, tag: tagOf(item), group: place.group, why: place.why, bad: place.bad === true, action: place.action, counted: item.waiting === true, also: null, folded: false });
|
|
138
|
+
}
|
|
139
|
+
// D4: a waiting run about a spec that also waits rides in the spec's row
|
|
140
|
+
for (const r of rows) {
|
|
141
|
+
const slug = r.item.kind === 'run' && r.group === 'waiting' ? edges.find((e) => e.to === r.id && e.rel === 'agent-on' && byId.has(e.from) && byId.get(e.from).kind === 'proposal') : null;
|
|
142
|
+
if (!slug) continue;
|
|
143
|
+
const host = rows.find((x) => x.id === slug.from && x.group === 'waiting' && !x.also);
|
|
144
|
+
if (host) { host.also = r; r.folded = true; }
|
|
145
|
+
}
|
|
146
|
+
const groups = Object.fromEntries(GROUP_KEYS.map((k) => [k, rows.filter((r) => r.group === k && !r.folded).sort(k === 'done' ? newest : oldest)]));
|
|
147
|
+
const count = rows.filter((r) => r.counted).length;
|
|
148
|
+
return { groups, rows, drafts, count, rowsWaiting: groups.waiting.length };
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** The queue's heading: "4 waiting on you in 3 rows", "1 waiting on you", "Nothing waits on you" */
|
|
152
|
+
export function headingOf({ count, rowsWaiting }) {
|
|
153
|
+
if (!count) return 'Nothing waits on you';
|
|
154
|
+
return `${count} waiting on you${rowsWaiting !== count ? ` in ${plural(rowsWaiting, 'row')}` : ''}`;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** What a screen reader hears when the Inbox opens (D6): "Inbox: 4 waiting on you in 3 rows, 4 your move, 1 nobody on it." */
|
|
158
|
+
export function openedWords(inbox) {
|
|
159
|
+
const parts = [headingOf(inbox).replace(/^Nothing waits on you$/, 'nothing waits on you')];
|
|
160
|
+
if (inbox.groups.move.length) parts.push(`${inbox.groups.move.length} your move`);
|
|
161
|
+
if (inbox.groups.unclaimed.length) parts.push(`${inbox.groups.unclaimed.length} nobody on it`);
|
|
162
|
+
return `Inbox: ${parts.join(', ')}.`;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** The ids in the order J and K move through them: the open groups' rows, top to bottom (D6) */
|
|
166
|
+
export function orderOf(inbox, open = new Set(GROUPS.filter((g) => g.open).map((g) => g.key))) {
|
|
167
|
+
return GROUP_KEYS.filter((k) => open.has(k)).flatMap((k) => inbox.groups[k].map((r) => r.id));
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** The row after an action: the next one in the order, else the one before, else none (D6) */
|
|
171
|
+
export function nextAfter(order, id) {
|
|
172
|
+
const i = order.indexOf(id);
|
|
173
|
+
if (i < 0) return order[0] ?? null;
|
|
174
|
+
return order[i + 1] ?? order[i - 1] ?? null;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** A row read out: its tag, title, why and how long ago (D6) */
|
|
178
|
+
export function rowWords(row, ago) {
|
|
179
|
+
return `${row.tag}, ${text(row.item.title)}. ${row.why}${row.also ? `, and ${row.also.tag} waits on your answer` : ''}. ${ago}.`;
|
|
180
|
+
}
|
package/explorer/index.html
CHANGED
|
@@ -63,8 +63,10 @@
|
|
|
63
63
|
</button>
|
|
64
64
|
</div>
|
|
65
65
|
|
|
66
|
-
<!--
|
|
67
|
-
|
|
66
|
+
<!-- The controls of the view that is on (spec 0015 D5), then the Layers chip. In the Work view: Waiting on you (a toggle) and List, which explorer/work-view.js puts in the group;
|
|
67
|
+
the Map's Replay is on the History row of the Layers menu (D6). On a phone the two sit on the map's top-left row, the Layers chip first -->
|
|
68
|
+
<div class="hx-view-tools">
|
|
69
|
+
<div class="hx-view-controls" role="group" aria-label="Work view" data-corn="view-controls" hidden></div>
|
|
68
70
|
|
|
69
71
|
<!-- The Layers chip: how many of the layers are on; it opens the layers menu. On a phone it sits on the map, top left. It is a piece (explorer/pieces/layers.js): page.js mounts it here. Until then a stand-in (its markup without ids) holds its place, so the first paint has the chip and nothing moves when the piece comes; test/explorer/page-pieces.test.js keeps the two the same -->
|
|
70
72
|
<!--corn-placeholder:layers/menu-->
|
|
@@ -72,7 +74,7 @@
|
|
|
72
74
|
<button type="button" class="hx-layers-chip" data-action="toggle-layers-menu" aria-expanded="false" data-corn="layers-button" part="layers-button"><span data-corn-string="layers">Layers</span><span class="hx-layers-count" data-corn="count" part="count"></span></button>
|
|
73
75
|
<div class="hx-menu glass" hidden data-corn="layers-menu" part="layers-menu">
|
|
74
76
|
<div class="hx-menu-title hx-menu-head" data-corn="menu-title" part="menu-title">
|
|
75
|
-
<span>
|
|
77
|
+
<span>Layers</span>
|
|
76
78
|
<span class="hx-menu-links">
|
|
77
79
|
<button type="button" class="hx-textbtn hx-textbtn--accent" data-action="layers-all" data-corn="layers-all" part="layers-all" data-corn-kind="quiet" data-corn-string="layersAll">All</button>
|
|
78
80
|
<span class="hx-faint">•</span>
|
|
@@ -108,6 +110,7 @@
|
|
|
108
110
|
</div>
|
|
109
111
|
</div>
|
|
110
112
|
<!--corn-piece:layers/menu-->
|
|
113
|
+
</div>
|
|
111
114
|
|
|
112
115
|
<!-- The live layer's chip (explorer/live.js, spec 0029): shown once a live layer is on; a dot on a phone, its line on tap -->
|
|
113
116
|
<div id="live-wrap" class="hx-menu-wrap hx-live" data-tone="idle" hidden>
|
package/explorer/page.js
CHANGED
|
@@ -116,9 +116,8 @@ const ACTIONS = {
|
|
|
116
116
|
setView(state.view === 'dataflow' ? 'orbit' : 'dataflow');
|
|
117
117
|
},
|
|
118
118
|
'play-outage': (el) => playOutage(el.dataset.id), // the cloud panel's Play button (spec 0031 section 8)
|
|
119
|
-
'history-replay': () => { // Replay (spec 0014 D1) runs on the Map view: from another view it goes there first
|
|
119
|
+
'history-replay': () => { // Replay (spec 0014 D1), on the Layers menu's History row (spec 0015 D6), runs on the Map view: from another view it goes there first (history.js, which hears the click after this, opens it)
|
|
120
120
|
if (state.view !== 'orbit') { closeDrawer(); setView('orbit'); }
|
|
121
|
-
inst.emit('history:replay');
|
|
122
121
|
},
|
|
123
122
|
'retry': () => loadTopology(),
|
|
124
123
|
'set-theme': (el) => setThemeChoice(el.dataset.choice),
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module explorer/panel-model
|
|
3
|
+
*
|
|
4
|
+
* The node panel's shape (spec 0015 D4), as strings and plain data: the action bar that leads it, the one "Next" line under the bar, the sections that
|
|
5
|
+
* fold behind their title and count, and what the browser remembers of which are open, per kind of node. Pure: no DOM, no storage of its own
|
|
6
|
+
* (explorer/panel-sections.js reads and writes the browser's and wires the clicks).
|
|
7
|
+
*
|
|
8
|
+
* actionBarHtml({ primary, others, next }) the bar: the one next action first, the next two beside it, the rest under ⋯
|
|
9
|
+
* nextHtml(words) "Then: approve, and Land it once docs finishes (1 check running)."
|
|
10
|
+
* sectionHtml({ key, title, count, note, body, open }) a section with its count, closed unless it holds the action
|
|
11
|
+
* groupLinks(links) a node's Connected rows grouped by relation, in the order first seen
|
|
12
|
+
* readPrefs(text), isOpen(prefs, kind, key, fallback), withOpen(prefs, kind, key, open) what is remembered
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { escapeHtml as esc } from '../core/text.js';
|
|
16
|
+
|
|
17
|
+
/** How many actions sit beside the primary one before the rest go under ⋯ */
|
|
18
|
+
export const BESIDE = 2;
|
|
19
|
+
|
|
20
|
+
/** The browser's key for which sections are open, per kind of node */
|
|
21
|
+
export const SECTIONS_KEY = 'cornfield.panel.sections';
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The action bar: the primary action, the next ones beside it, the rest under ⋯ (a disclosure: a button that shows a group of buttons, reached with Tab, closed by Escape),
|
|
25
|
+
* then the Next line. Each action is the markup of a button or a link, as its part draws it today (its data attributes are what its click does). '' with nothing to show.
|
|
26
|
+
* @param {{ primary?: string, others?: string[], next?: string }} parts
|
|
27
|
+
*/
|
|
28
|
+
export function actionBarHtml({ primary = '', others = [], next = '' } = {}) {
|
|
29
|
+
const rest = others.filter(Boolean);
|
|
30
|
+
const first = primary || rest.shift() || '';
|
|
31
|
+
if (!first && !next) return '';
|
|
32
|
+
const beside = rest.slice(0, BESIDE);
|
|
33
|
+
const more = rest.slice(BESIDE);
|
|
34
|
+
const menu = more.length
|
|
35
|
+
? `<div class="hx-actions-more"><button type="button" class="hx-btn hx-btn--quiet hx-actions-more-btn" data-corn="more" data-corn-kind="quiet" data-more-toggle aria-expanded="false" aria-label="More actions" title="More actions">⋯</button><div class="hx-actions-menu glass" data-more-menu role="group" aria-label="More actions" hidden>${more.join('')}</div></div>`
|
|
36
|
+
: '';
|
|
37
|
+
const row = first ? `<div class="hx-actions-row">${first}${beside.join('')}${menu}</div>` : '';
|
|
38
|
+
return `<div class="hx-actions" data-corn="actions" role="group" aria-label="Actions">${row}${nextHtml(next)}</div>`;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** The one line under the bar that says what comes before an action can be taken; '' when nothing waits */
|
|
42
|
+
export const nextHtml = (words) => (words ? `<p class="hx-next">${esc(words)}</p>` : '');
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* A section of the panel: its title and count on a button that opens and closes it (`aria-expanded`), its body under it. `open` is where it starts: closed unless it holds
|
|
46
|
+
* the action; the browser's memory of it, per kind of node, is put on it once it is drawn (panel-sections.js `applySections`).
|
|
47
|
+
* @param {{ key: string, title: string, count?: number | null, note?: string, body: string, open?: boolean, extra?: string }} s `note`: a few words after the count ("1 failed")
|
|
48
|
+
*/
|
|
49
|
+
export function sectionHtml({ key, title, count = null, note = '', body, open = false, extra = '' }) {
|
|
50
|
+
const n = Number.isInteger(count) ? ` <span class="hx-sec-count" data-corn="count">${count}</span>` : '';
|
|
51
|
+
const said = note ? ` <span class="hx-sec-note">· ${esc(note)}</span>` : '';
|
|
52
|
+
return `<div class="hx-sec-block hx-psec" data-corn="section" data-sec="${esc(key)}"${extra}>`
|
|
53
|
+
+ `<h3 class="hx-sec-title"><button type="button" class="hx-sec-toggle" data-corn-kind="quiet" data-sec-toggle="${esc(key)}" data-sec-default="${open}" aria-expanded="${open}"><span class="hx-sec-name">${esc(title)}</span>${n}${said}</button></h3>`
|
|
54
|
+
+ `<div class="hx-sec-body"${open ? '' : ' hidden'}>${body}</div></div>`;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* A node's connections grouped by relation, each group in the order its first row came: `[{ rel, rows }]`.
|
|
59
|
+
* @template {{ rel: string }} T
|
|
60
|
+
* @param {T[]} links
|
|
61
|
+
* @returns {{ rel: string, rows: T[] }[]}
|
|
62
|
+
*/
|
|
63
|
+
export function groupLinks(links) {
|
|
64
|
+
const groups = new Map();
|
|
65
|
+
for (const l of links || []) {
|
|
66
|
+
if (!groups.has(l.rel)) groups.set(l.rel, []);
|
|
67
|
+
groups.get(l.rel).push(l);
|
|
68
|
+
}
|
|
69
|
+
return [...groups].map(([rel, rows]) => ({ rel, rows }));
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** "Discussed in", "Adds": a relation's words at the start of a row */
|
|
73
|
+
export const relTitle = (rel) => String(rel).charAt(0).toUpperCase() + String(rel).slice(1);
|
|
74
|
+
|
|
75
|
+
// ---------------------------------------------------------------------------
|
|
76
|
+
// What the browser remembers: { [kind of node]: { [section]: open } }
|
|
77
|
+
// ---------------------------------------------------------------------------
|
|
78
|
+
|
|
79
|
+
/** What was stored, or nothing remembered (a broken or foreign value reads as nothing) */
|
|
80
|
+
export function readPrefs(text) {
|
|
81
|
+
if (typeof text !== 'string' || !text) return {};
|
|
82
|
+
try {
|
|
83
|
+
const v = JSON.parse(text);
|
|
84
|
+
return v && typeof v === 'object' && !Array.isArray(v) ? v : {};
|
|
85
|
+
} catch {
|
|
86
|
+
return {};
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** Whether a kind's section is open: what was remembered, else `fallback` */
|
|
91
|
+
export function isOpen(prefs, kind, key, fallback) {
|
|
92
|
+
const k = prefs && Object.hasOwn(prefs, kind) ? prefs[kind] : null;
|
|
93
|
+
return k && typeof k === 'object' && typeof k[key] === 'boolean' ? k[key] : !!fallback;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** The prefs with one section of one kind remembered open or closed (a new object; the old is not changed) */
|
|
97
|
+
export function withOpen(prefs, kind, key, open) {
|
|
98
|
+
const was = prefs && Object.hasOwn(prefs, kind) && prefs[kind] && typeof prefs[kind] === 'object' ? prefs[kind] : {};
|
|
99
|
+
return { ...(prefs || {}), [kind]: { ...was, [key]: !!open } };
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** The kind of node a panel's memory is kept for: a piece of work's kind ("pull", "proposal"), else the node's type ("service") */
|
|
103
|
+
export const panelKind = (node) => (node && typeof node.work === 'string' && node.work ? node.work : node && typeof node.type === 'string' ? node.type : 'node');
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module explorer/panel-sections
|
|
3
|
+
*
|
|
4
|
+
* The node panel's sections and its ⋯ menu on an instance (spec 0015 D4): a section's title opens and closes it, and which are open is remembered per kind
|
|
5
|
+
* of node in the browser (`localStorage`, every read and write in a try: a private window, or storage turned off, only means nothing is remembered);
|
|
6
|
+
* the ⋯ of the action bar shows the rest of the actions, Escape closes it with the focus back on ⋯. The markup is panel-model.js's.
|
|
7
|
+
*
|
|
8
|
+
* applySections(inst, root, kind) puts what was remembered for `kind` on every section under `root` (after it is drawn)
|
|
9
|
+
* onSectionsClick(inst, e, kind) a click in the panel: a section's title, a count that opens its rows, ⋯, an action in the menu; true when it was one of the first three
|
|
10
|
+
* onSectionsKey(e) Escape in an open menu closes it; true when it did
|
|
11
|
+
* menuOpen(root), reopenMenu(root) whether the ⋯ menu under `root` is open, and opening it again after the bar was drawn anew
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { SECTIONS_KEY, isOpen, readPrefs, withOpen } from './panel-model.js';
|
|
15
|
+
|
|
16
|
+
const storeOf = (inst) => {
|
|
17
|
+
try { return inst && inst.win && inst.win.localStorage ? inst.win.localStorage : null; } catch { return null; }
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
/** What the browser remembers of the sections (nothing, when it cannot say) */
|
|
21
|
+
export function loadPrefs(inst) {
|
|
22
|
+
try {
|
|
23
|
+
const store = storeOf(inst);
|
|
24
|
+
return readPrefs(store ? store.getItem(SECTIONS_KEY) : '');
|
|
25
|
+
} catch {
|
|
26
|
+
return {};
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function savePrefs(inst, prefs) {
|
|
31
|
+
try {
|
|
32
|
+
const store = storeOf(inst);
|
|
33
|
+
if (store) store.setItem(SECTIONS_KEY, JSON.stringify(prefs));
|
|
34
|
+
} catch { /* nothing is remembered: the section is open or closed for now */ }
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const bodyOf = (toggle) => {
|
|
38
|
+
const block = toggle.closest('[data-sec]');
|
|
39
|
+
return block ? [...block.children].find((c) => c.classList && c.classList.contains('hx-sec-body')) || null : null;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
function setSection(toggle, open) {
|
|
43
|
+
toggle.setAttribute('aria-expanded', String(open));
|
|
44
|
+
const body = bodyOf(toggle);
|
|
45
|
+
if (body) body.hidden = !open;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Each section under `root` as `kind`'s were left (else as it was drawn: closed unless it holds the action) */
|
|
49
|
+
export function applySections(inst, root, kind) {
|
|
50
|
+
if (!root || !kind || typeof root.querySelectorAll !== 'function') return;
|
|
51
|
+
const prefs = loadPrefs(inst);
|
|
52
|
+
for (const toggle of root.querySelectorAll('[data-sec-toggle]')) setSection(toggle, isOpen(prefs, kind, toggle.dataset.secToggle, toggle.dataset.secDefault === 'true'));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function setMenu(toggle, open, { focus = false } = {}) {
|
|
56
|
+
const box = toggle.closest('.hx-actions-more');
|
|
57
|
+
const menu = box && box.querySelector('[data-more-menu]');
|
|
58
|
+
if (!menu) return;
|
|
59
|
+
toggle.setAttribute('aria-expanded', String(open));
|
|
60
|
+
menu.hidden = !open;
|
|
61
|
+
if (open && focus) {
|
|
62
|
+
const first = [...menu.querySelectorAll('button, a[href]')].find((el) => !el.hidden && !el.disabled);
|
|
63
|
+
if (first) first.focus();
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Closes every open ⋯ menu under `root` */
|
|
68
|
+
function closeMenus(root, except = null) {
|
|
69
|
+
for (const t of root.querySelectorAll('[data-more-toggle][aria-expanded="true"]')) if (t !== except) setMenu(t, false);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Whether a ⋯ menu under `root` is open */
|
|
73
|
+
export const menuOpen = (root) => !!(root && root.querySelector('[data-more-toggle][aria-expanded="true"]'));
|
|
74
|
+
|
|
75
|
+
/** Opens the ⋯ menu under `root` again (its bar was drawn anew while it was open) */
|
|
76
|
+
export function reopenMenu(root) {
|
|
77
|
+
const toggle = root && root.querySelector('[data-more-toggle]');
|
|
78
|
+
if (toggle) setMenu(toggle, true);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* A click in the panel. A section's title opens or closes it and the browser remembers it for `kind`; ⋯ opens or closes its menu (the focus goes to the first action in it);
|
|
83
|
+
* an action in the menu closes the menu once its own click is heard (it is the same button it always was); a click anywhere else closes an open menu.
|
|
84
|
+
* @returns {boolean} whether it was a section's title or ⋯ (nothing else need hear it)
|
|
85
|
+
*/
|
|
86
|
+
export function onSectionsClick(inst, e, kind) {
|
|
87
|
+
const t = e.target && e.target.closest ? e.target : null;
|
|
88
|
+
if (!t) return false;
|
|
89
|
+
const root = e.currentTarget && e.currentTarget.querySelectorAll ? e.currentTarget : t.ownerDocument;
|
|
90
|
+
const toggle = t.closest('[data-sec-toggle]');
|
|
91
|
+
if (toggle) {
|
|
92
|
+
const open = toggle.getAttribute('aria-expanded') !== 'true';
|
|
93
|
+
setSection(toggle, open);
|
|
94
|
+
if (kind) savePrefs(inst, withOpen(loadPrefs(inst), kind, toggle.dataset.secToggle, open));
|
|
95
|
+
return true;
|
|
96
|
+
}
|
|
97
|
+
// A count that opens its rows (a group of Connected, History's specs and issues): the list after it
|
|
98
|
+
const sub = t.closest('[data-sub-toggle]');
|
|
99
|
+
if (sub) {
|
|
100
|
+
const open = sub.getAttribute('aria-expanded') !== 'true';
|
|
101
|
+
sub.setAttribute('aria-expanded', String(open));
|
|
102
|
+
const list = sub.nextElementSibling;
|
|
103
|
+
if (list) list.hidden = !open;
|
|
104
|
+
return true;
|
|
105
|
+
}
|
|
106
|
+
const more = t.closest('[data-more-toggle]');
|
|
107
|
+
if (more) {
|
|
108
|
+
const open = more.getAttribute('aria-expanded') !== 'true';
|
|
109
|
+
closeMenus(root, more);
|
|
110
|
+
setMenu(more, open, { focus: open });
|
|
111
|
+
return true;
|
|
112
|
+
}
|
|
113
|
+
const inMenu = t.closest('[data-more-menu]');
|
|
114
|
+
if (inMenu) {
|
|
115
|
+
const box = inMenu.closest('.hx-actions-more');
|
|
116
|
+
const back = box && box.querySelector('[data-more-toggle]');
|
|
117
|
+
// after the action's own listeners (they are on the panel's root, which hears it later): the menu goes, the focus is not left on a hidden button
|
|
118
|
+
setTimeout(() => {
|
|
119
|
+
if (!back || !back.isConnected) return;
|
|
120
|
+
const had = inMenu.contains(back.ownerDocument.activeElement);
|
|
121
|
+
setMenu(back, false);
|
|
122
|
+
if (had) back.focus();
|
|
123
|
+
}, 0);
|
|
124
|
+
return false;
|
|
125
|
+
}
|
|
126
|
+
closeMenus(root);
|
|
127
|
+
return false;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** Escape in an open ⋯ menu (or on ⋯ while it is open) closes it, with the focus back on ⋯; the panel's own Escape does not follow */
|
|
131
|
+
export function onSectionsKey(e) {
|
|
132
|
+
if (e.key !== 'Escape' || e.defaultPrevented || !e.target || !e.target.closest) return false;
|
|
133
|
+
const box = e.target.closest('.hx-actions-more');
|
|
134
|
+
const toggle = box && box.querySelector('[data-more-toggle]');
|
|
135
|
+
if (!toggle || toggle.getAttribute('aria-expanded') !== 'true') return false;
|
|
136
|
+
e.preventDefault();
|
|
137
|
+
setMenu(toggle, false);
|
|
138
|
+
toggle.focus();
|
|
139
|
+
return true;
|
|
140
|
+
}
|