cornfield 0.38.0 → 0.40.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/cli/cornfield.js +4 -1
- package/core/config.js +5 -0
- package/core/contrast.js +47 -6
- package/core/layers.js +412 -0
- package/core/map-tokens.js +45 -1
- package/core/proposal.js +22 -0
- package/core/strings.js +17 -0
- package/core/theme.js +5 -4
- package/core/validate.js +4 -1
- package/explorer/check.js +14 -10
- package/explorer/explorer.css +185 -31
- package/explorer/header-fit.js +8 -0
- package/explorer/index.html +22 -11
- package/explorer/kind-letters.js +70 -0
- package/explorer/layer-model.js +59 -0
- package/explorer/layers.js +239 -53
- package/explorer/main.js +11 -10
- package/explorer/map-style.js +28 -6
- package/explorer/panels.js +5 -3
- package/explorer/proposal.js +20 -86
- package/explorer/renderer-canvas2d.js +9 -0
- package/explorer/renderer-webgl.js +21 -11
- package/explorer/review-column.js +430 -0
- package/explorer/review.js +7 -7
- package/explorer/scenario-list-model.js +301 -0
- package/explorer/scenario-ui.js +142 -136
- package/explorer/scene.js +15 -1
- package/explorer/shaders.js +1 -1
- package/explorer/sheets.js +5 -2
- package/explorer/state.js +9 -0
- package/package.json +1 -1
- package/server/server.js +4 -1
package/explorer/index.html
CHANGED
|
@@ -53,11 +53,12 @@
|
|
|
53
53
|
<div class="hx-menu-title hx-menu-head" data-corn="menu-title">
|
|
54
54
|
<span>Section Toggles</span>
|
|
55
55
|
<span class="hx-menu-links">
|
|
56
|
-
<button type="button" class="hx-textbtn hx-textbtn--accent" data-action="layers-all" data-corn-kind="quiet">All</button>
|
|
56
|
+
<button type="button" class="hx-textbtn hx-textbtn--accent" data-action="layers-all" data-corn="layers-all" data-corn-kind="quiet" data-corn-string="layersAll">All</button>
|
|
57
57
|
<span class="hx-faint">•</span>
|
|
58
|
-
<button type="button" class="hx-textbtn" data-action="layers-none" data-corn-kind="quiet">None</button>
|
|
58
|
+
<button type="button" class="hx-textbtn" data-action="layers-none" data-corn="layers-none" data-corn-kind="quiet" data-corn-string="layersNone">None</button>
|
|
59
59
|
</span>
|
|
60
60
|
</div>
|
|
61
|
+
<div class="hx-menu-body">
|
|
61
62
|
<p id="layers-preset-note" class="hx-note" data-corn="notice" hidden>The data-flow view shows its own tiers and ignores these toggles. Switch it off to use them.</p>
|
|
62
63
|
<div id="layer-toggles-container" class="hx-toggles"></div>
|
|
63
64
|
<div id="flow-legend" hidden></div>
|
|
@@ -93,6 +94,7 @@
|
|
|
93
94
|
<span class="hx-faint hx-mono">Usage & Dependency</span>
|
|
94
95
|
</div>
|
|
95
96
|
</div>
|
|
97
|
+
</div><!-- .hx-menu-body: the rows scroll, the title with All and None stays (spec 0009 D8) -->
|
|
96
98
|
</div>
|
|
97
99
|
</div>
|
|
98
100
|
|
|
@@ -200,10 +202,11 @@
|
|
|
200
202
|
<div id="proposal-ways" class="hx-proposal-ways" role="group" aria-label="Ways on" data-corn="proposal-ways" hidden></div>
|
|
201
203
|
<div id="proposal-body">
|
|
202
204
|
<div class="hx-proposal-head" data-corn="proposal-head" role="group" aria-label="Proposal actions">
|
|
203
|
-
<
|
|
205
|
+
<h2 id="proposal-title" class="hx-proposal-name" data-corn="proposal-title"></h2>
|
|
204
206
|
<a id="proposal-source" class="hx-proposal-source" data-corn="proposal-source" hidden></a>
|
|
205
207
|
<div class="hx-proposal-counts">
|
|
206
|
-
<
|
|
208
|
+
<div id="proposal-chip" class="hx-proposal-chip" data-corn="proposal-counts"></div>
|
|
209
|
+
<p id="proposal-peek" class="hx-proposal-peek"></p>
|
|
207
210
|
<button type="button" id="proposal-mode" class="hx-proposal-mode" data-corn="proposal-mode" data-action="toggle-proposal-mode" data-peek-ok aria-expanded="false" aria-controls="proposal-seg" title="Before, Diff or After"></button>
|
|
208
211
|
</div>
|
|
209
212
|
<div id="proposal-seg" class="hx-seg" data-corn="compare" role="group" aria-label="Compare">
|
|
@@ -212,13 +215,8 @@
|
|
|
212
215
|
<button data-corn="compare-option" type="button" class="hx-seg-btn" data-action="proposal-view" data-view="after" aria-pressed="false" data-corn-string="after">After</button>
|
|
213
216
|
</div>
|
|
214
217
|
<div class="hx-proposal-tools">
|
|
215
|
-
<button data-corn-kind="secondary" type="button" id="proposal-play" class="hx-btn" aria-expanded="false" aria-controls="proposal-scenarios" title="Play one of this proposal's scenarios on the map" hidden>Play</button>
|
|
216
218
|
<button data-corn-kind="secondary" type="button" id="proposal-edit" class="hx-btn" aria-expanded="false" aria-controls="whatif" title="Try a change: add a node or an edge, reword a scenario">Edit</button>
|
|
217
219
|
</div>
|
|
218
|
-
<div class="hx-proposal-actions">
|
|
219
|
-
<button data-corn-kind="secondary" type="button" class="hx-btn" data-review="notes" data-peek-ok title="Send it back to the drafting agent with a note and your edits" data-corn-string="sendBack">Send back</button>
|
|
220
|
-
<button type="button" class="hx-btn hx-btn--approve" data-review="approved" data-peek-ok title="Sign this proposal off" data-corn-kind="approve" data-corn-string="approve">Approve</button>
|
|
221
|
-
</div>
|
|
222
220
|
<div class="hx-proposal-more">
|
|
223
221
|
<button type="button" id="proposal-more" class="hx-btn hx-btn--icon" data-action="toggle-proposal-more" aria-haspopup="menu" aria-expanded="false" aria-controls="proposal-more-menu" aria-label="More" data-corn-kind="icon"><svg width="16" height="16" viewBox="0 0 16 16" fill="currentColor" aria-hidden="true"><circle cx="3" cy="8" r="1.5"/><circle cx="8" cy="8" r="1.5"/><circle cx="13" cy="8" r="1.5"/></svg></button>
|
|
224
222
|
<div id="proposal-more-menu" class="hx-proposal-more-menu glass" data-corn="proposal-menu" role="menu" aria-label="More" hidden>
|
|
@@ -226,16 +224,29 @@
|
|
|
226
224
|
</div>
|
|
227
225
|
</div>
|
|
228
226
|
</div>
|
|
229
|
-
<p id="proposal-summary" class="hx-proposal-summary" hidden></p>
|
|
230
227
|
<details id="proposal-errors" class="hx-proposal-errors" data-corn="proposal-errors" hidden>
|
|
231
228
|
<summary data-corn="summary" id="proposal-errors-count"></summary>
|
|
232
229
|
<ul id="proposal-errors-list"></ul>
|
|
233
230
|
<p class="hx-faint">The map shows what it can.</p>
|
|
234
231
|
</details>
|
|
235
|
-
<div id="proposal-changes" class="hx-proposal-changes" data-corn="proposal-changes" hidden></div>
|
|
236
232
|
</div>
|
|
237
233
|
</section>
|
|
238
234
|
|
|
235
|
+
<!-- The review column (review-column.js, spec 0006): what the proposal changes, and the personas and the scenarios that apply to them. Docked left of the map from 768px, the expanded proposal sheet's body on a phone -->
|
|
236
|
+
<aside id="review-column" class="hx-review-column glass" aria-label="Review" data-corn="review-column" hidden>
|
|
237
|
+
<div class="hx-rc-scroll">
|
|
238
|
+
<p id="proposal-summary" class="hx-proposal-summary" hidden></p>
|
|
239
|
+
<div id="review-lists" class="hx-rc-lists"></div>
|
|
240
|
+
</div>
|
|
241
|
+
<!-- The decision, the column's foot: Send back and Approve (review.js). Spec 0007 puts its line about what Approve does above them here. -->
|
|
242
|
+
<div id="review-decision" class="hx-rc-foot" data-corn="decision" role="group" aria-label="Decision">
|
|
243
|
+
<div class="hx-proposal-actions">
|
|
244
|
+
<button data-corn-kind="secondary" type="button" class="hx-btn" data-review="notes" data-peek-ok title="Send it back to the drafting agent with a note and your edits" data-corn-string="sendBack">Send back</button>
|
|
245
|
+
<button type="button" class="hx-btn hx-btn--approve" data-review="approved" data-peek-ok title="Sign this proposal off" data-corn-kind="approve" data-corn-string="approve">Approve</button>
|
|
246
|
+
</div>
|
|
247
|
+
</div>
|
|
248
|
+
</aside>
|
|
249
|
+
|
|
239
250
|
<!-- The Work panel (forge-panel.js): Specs, PRs, Issues, Threads, Runs and Deploys, in the order of the loop. A slide-over from the left on desktop, a bottom sheet on a phone; hidden and inert until the config names a forge. `forge-panel` on the inner box is the old name of the hook, kept for one release (docs/theming.md) -->
|
|
240
251
|
<p id="offline-banner" class="hx-offline" data-corn="notice" hidden></p>
|
|
241
252
|
<aside id="forge-panel" class="hx-forge glass" aria-label="Work" inert hidden data-corn="work-panel">
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module explorer/kind-letters
|
|
3
|
+
*
|
|
4
|
+
* The letters a finer `kind` shows in the middle of its node (spec 0009 D5): `j` for a job, `h` for a hook (core/layers.js `kindLetters`
|
|
5
|
+
* chooses them; scene.js puts them on the node as `letters`). Both renderers draw the same picture, so it is made once, here: a small
|
|
6
|
+
* transparent image of the letters (dark ink under a light outline, so it reads over the node's own colour and the white core in either
|
|
7
|
+
* theme), and the rule for when it is drawn.
|
|
8
|
+
*
|
|
9
|
+
* - Canvas 2D draws it with `drawImage` where the theme's icon would go.
|
|
10
|
+
* - WebGL uploads it as a texture and draws it in the icon pass, grouped by letters like an icon is grouped by kind (`letters:<l>`).
|
|
11
|
+
*
|
|
12
|
+
* A theme icon for the node's kind replaces it (the node then carries `icon`); spec 0008's type glyphs sit in this same place and can draw
|
|
13
|
+
* beside or instead of it. The letters are text: an image of them is made with fillText, never markup.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { FONT_NODE } from './scene.js';
|
|
17
|
+
|
|
18
|
+
/** A node is at least this many px across (on screen) before its letters are drawn: smaller, they are a smudge */
|
|
19
|
+
export const LETTER_MIN_PX = 14;
|
|
20
|
+
/** The letters' box, as a fraction of the node's radius across (a square centred on the node, like an icon) */
|
|
21
|
+
export const LETTER_RATIO = 1;
|
|
22
|
+
/** The image's side in px: sharp up to a node several times its size on screen */
|
|
23
|
+
const SIDE = 96;
|
|
24
|
+
const KEY_PREFIX = 'letters:';
|
|
25
|
+
|
|
26
|
+
/** The key a node's letters go under in the renderers' icon tables */
|
|
27
|
+
export const letterKey = (letters) => `${KEY_PREFIX}${letters}`;
|
|
28
|
+
export const isLetterKey = (key) => typeof key === 'string' && key.startsWith(KEY_PREFIX);
|
|
29
|
+
export const lettersOfKey = (key) => key.slice(KEY_PREFIX.length);
|
|
30
|
+
|
|
31
|
+
/** Whether a node's letters are drawn at this zoom: it has some, no theme icon replaces them, and it is wide enough on screen */
|
|
32
|
+
export const showsLetters = (node, zoom) => !!node.letters && !node.icon && node.r * 2 * zoom >= LETTER_MIN_PX;
|
|
33
|
+
|
|
34
|
+
const images = new Map();
|
|
35
|
+
/** The most letter images made (and so WebGL textures, one each): a map's kinds are its own data, so the count is held, not trusted. A kind
|
|
36
|
+
* past it draws as a plain node, as one with no letters does */
|
|
37
|
+
export const MAX_LETTER_IMAGES = 128;
|
|
38
|
+
/** Lets the images go (a test starts afresh with it) */
|
|
39
|
+
export const forgetLetterImages = () => images.clear();
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* The image of some letters, made once (null where there is no canvas to draw one on, as in a test without a browser).
|
|
43
|
+
* @param {string} letters one or two characters
|
|
44
|
+
* @returns {CanvasImageSource | null}
|
|
45
|
+
*/
|
|
46
|
+
export function letterImage(letters) {
|
|
47
|
+
if (typeof letters !== 'string' || !letters) return null;
|
|
48
|
+
if (images.has(letters)) return images.get(letters);
|
|
49
|
+
if (images.size >= MAX_LETTER_IMAGES) return null;
|
|
50
|
+
let image = null;
|
|
51
|
+
try {
|
|
52
|
+
const canvas = typeof OffscreenCanvas !== 'undefined' ? new OffscreenCanvas(SIDE, SIDE) : typeof document !== 'undefined' ? Object.assign(document.createElement('canvas'), { width: SIDE, height: SIDE }) : null;
|
|
53
|
+
const ctx = canvas && canvas.getContext && canvas.getContext('2d');
|
|
54
|
+
if (ctx && ctx.fillText) {
|
|
55
|
+
const size = letters.length > 1 ? 56 : 72;
|
|
56
|
+
ctx.font = `700 ${size}px ${FONT_NODE}`;
|
|
57
|
+
ctx.textAlign = 'center';
|
|
58
|
+
ctx.textBaseline = 'middle';
|
|
59
|
+
ctx.lineJoin = 'round';
|
|
60
|
+
ctx.lineWidth = 10;
|
|
61
|
+
ctx.strokeStyle = 'rgba(255, 255, 255, 0.92)';
|
|
62
|
+
ctx.strokeText(letters, SIDE / 2, SIDE / 2 + 3);
|
|
63
|
+
ctx.fillStyle = '#0b1220';
|
|
64
|
+
ctx.fillText(letters, SIDE / 2, SIDE / 2 + 3);
|
|
65
|
+
image = canvas;
|
|
66
|
+
}
|
|
67
|
+
} catch { image = null; }
|
|
68
|
+
images.set(letters, image);
|
|
69
|
+
return image;
|
|
70
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module explorer/layer-model
|
|
3
|
+
*
|
|
4
|
+
* Which layer a node is in, as the Layers menu and the map read it (spec 0009 D3), from the state alone: no DOM, no drawing.
|
|
5
|
+
*
|
|
6
|
+
* - `projectLayerKey(state, node)`: the first project layer (`state.projectLayers`, from the config's `layers.add`) whose match the node
|
|
7
|
+
* meets, or null. A project layer takes its nodes from their starting layer, so a node is in exactly one row.
|
|
8
|
+
* - `rowKeyOf(state, node)`: the row a node is under: that layer, else its starting layer. The root has none (null).
|
|
9
|
+
* - `shownByProposal(state, node)`: a node the open proposal touches is always drawn, whatever its layer says, so a review never hides its
|
|
10
|
+
* own change.
|
|
11
|
+
*
|
|
12
|
+
* The matching itself is core/layers.js (data only); this file only hands it the map's ownership line.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { kindLetters, layerKeyOf, parentIndex, projectLayerOf } from '../core/layers.js';
|
|
16
|
+
import { touchedIds } from './proposal-model.js';
|
|
17
|
+
|
|
18
|
+
/** Asks the ownership line of the map the state holds, only when a layer matches by `parent` */
|
|
19
|
+
const parentFn = (state) => ((state.projectLayers || []).some((l) => l.match && l.match.parent !== undefined) ? (n) => parentIndex(state.edges || []).get(n.id) : undefined);
|
|
20
|
+
|
|
21
|
+
/** The key of the project layer that takes `node`, or null */
|
|
22
|
+
export function projectLayerKey(state, node) {
|
|
23
|
+
const add = state.projectLayers;
|
|
24
|
+
if (!add || !add.length) return null;
|
|
25
|
+
return projectLayerOf(add, node, parentFn(state));
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** The key of the row `node` is under (null for the root) */
|
|
29
|
+
export const rowKeyOf = (state, node) => layerKeyOf(state.projectLayers || [], node, parentFn(state));
|
|
30
|
+
|
|
31
|
+
const touchedMemo = new WeakMap();
|
|
32
|
+
/** The ids of the nodes the open proposal touches: a node it marks, and both ends of an edge it marks (what Diff keeps bright) */
|
|
33
|
+
function touchedBy(state) {
|
|
34
|
+
const edges = state.edges || [];
|
|
35
|
+
const hit = touchedMemo.get(state.nodes);
|
|
36
|
+
if (hit && hit.edges === edges && hit.n === state.nodes.length && hit.e === edges.length) return hit.touched;
|
|
37
|
+
const touched = touchedIds(state.nodes, edges).touched;
|
|
38
|
+
touchedMemo.set(state.nodes, { edges, n: state.nodes.length, e: edges.length, touched });
|
|
39
|
+
return touched;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Whether the open proposal touches `node` (false with no proposal open) */
|
|
43
|
+
export const shownByProposal = (state, node) => !!state.proposal && touchedBy(state).has(node.id);
|
|
44
|
+
|
|
45
|
+
const lettersMemo = new WeakMap();
|
|
46
|
+
/**
|
|
47
|
+
* The letters of the node's finer kind (core/layers.js `kindLetters`, over the whole map so two kinds of a type never share one), or ''.
|
|
48
|
+
* An outside node's kind is its layer (cloud or web), not a finer one.
|
|
49
|
+
*/
|
|
50
|
+
export function lettersOf(state, node) {
|
|
51
|
+
if (typeof node.kind !== 'string' || !node.kind || node.type === 'external') return '';
|
|
52
|
+
let hit = lettersMemo.get(state.nodes);
|
|
53
|
+
if (!hit || hit.n !== state.nodes.length) {
|
|
54
|
+
hit = { n: state.nodes.length, map: kindLetters(state.nodes) };
|
|
55
|
+
lettersMemo.set(state.nodes, hit);
|
|
56
|
+
}
|
|
57
|
+
const t = hit.map.get(node.type);
|
|
58
|
+
return (t && t.get(node.kind)) || '';
|
|
59
|
+
}
|
package/explorer/layers.js
CHANGED
|
@@ -7,38 +7,28 @@
|
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
import { state, touchScene, escHtml } from './state.js';
|
|
10
|
-
import { render, ensureFlowLoop } from './render.js';
|
|
10
|
+
import { render, ensureFlowLoop, isNodeVisible } from './render.js';
|
|
11
11
|
import { renderFlowLegend } from './flows.js';
|
|
12
|
-
import {
|
|
13
|
-
import { WHEN_STYLES, externalLayer } from './cloud-model.js';
|
|
12
|
+
import { WHEN_STYLES } from './cloud-model.js';
|
|
14
13
|
import { themed } from './theme-model.js';
|
|
15
14
|
import { onThemeChange } from './theme.js';
|
|
15
|
+
import { STARTING_LAYERS, clientLayers, kindLetters, orderRows, startingLayerOf } from '../core/layers.js';
|
|
16
|
+
import { rowKeyOf, shownByProposal } from './layer-model.js';
|
|
17
|
+
import { layerShows } from './scene.js';
|
|
18
|
+
import { getMapStyle, groupToken, setMapSubjects } from './map-style.js';
|
|
19
|
+
import { t } from './strings.js';
|
|
16
20
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
{ key: 'table', label: 'Tables', color: '#fde047', desc: 'SQLite tables, reads & writes' },
|
|
29
|
-
{ key: 'rom', label: 'ROM', color: '#f0abfc', desc: 'Render on Mutation' },
|
|
30
|
-
{ key: 'service', label: 'Services', color: '#84cc16', desc: 'API jobs, webhooks and hooks' },
|
|
31
|
-
{ key: 'cloud', label: 'Cloud', color: '#94a3b8', desc: 'Services outside the house that we do not control: what runs, where software comes from, accounts (topology.cloud.json)' },
|
|
32
|
-
{ key: 'web', label: 'Public web', color: '#94a3b8', desc: 'Any public page or picture a person points us at: not cloud' },
|
|
33
|
-
{ key: 'stack', label: 'Infra', color: '#a8a29e', desc: 'Compose stacks and the domains they serve' },
|
|
34
|
-
// The physical world (spec 0029). `live`: drawn or placed from the live layer, which loads when one of them goes on
|
|
35
|
-
{ key: 'machine', label: 'Machines', color: '#78716c', desc: 'The computers the code runs on, placed in their room from the inventory (live)', live: true },
|
|
36
|
-
{ key: 'container', label: 'Containers', color: '#d6d3d1', desc: 'One service of a stack, from its compose file' },
|
|
37
|
-
{ key: 'device', label: 'Devices', color: '#2dd4bf', desc: 'Cameras, and live: phones, NFC tags, Home Assistant devices', live: true },
|
|
38
|
-
{ key: 'house', label: 'House', color: '#a3e635', desc: 'Floors and rooms (live)', live: true },
|
|
39
|
-
{ key: 'runs', label: 'Runs in flight', color: '#5eead4', desc: 'A pulsing ring on the nodes an open agent run is changing; a dashed ring and a warning where two runs change the same node', overlay: true },
|
|
40
|
-
{ key: 'persona', label: 'People', color: '#fb7185', desc: 'Personas, with a live count of their accounts (never a name)', live: true },
|
|
41
|
-
];
|
|
21
|
+
/** The dot of each starting row: the colour its nodes are drawn in (a project layer's dot is its nodes' own, or the theme's group token) */
|
|
22
|
+
const COLORS = {
|
|
23
|
+
app: '#6366f1', pkg: '#06b6d4', page: '#38bdf8', atom: '#10b981', molecule: '#a855f7', token: '#ec4899', tool: '#f43f5e', flow: '#fb923c',
|
|
24
|
+
core: '#14b8a6', store: '#fb923c', table: '#fde047', rom: '#f0abfc', service: '#84cc16', cloud: '#94a3b8', web: '#94a3b8', stack: '#a8a29e',
|
|
25
|
+
machine: '#78716c', container: '#d6d3d1', device: '#2dd4bf', house: '#a3e635', runs: '#5eead4', persona: '#fb7185',
|
|
26
|
+
};
|
|
27
|
+
/** The starting layers that start off: the infra layer is its own view, and the physical world's are loaded when first turned on (spec 0029) */
|
|
28
|
+
const STARTS_OFF = new Set(['stack', 'machine', 'container', 'device', 'house', 'persona']);
|
|
29
|
+
|
|
30
|
+
/** The starting rows (core/layers.js STARTING_LAYERS) with the colour of their dot */
|
|
31
|
+
export const LAYER_DEFS = STARTING_LAYERS.map((l) => ({ ...l, color: COLORS[l.key] }));
|
|
42
32
|
|
|
43
33
|
const layerListeners = new Set();
|
|
44
34
|
/** Calls `fn` after a layer goes on or off (live.js starts or stops the live layer). Returns how to stop. */
|
|
@@ -64,61 +54,223 @@ export function closeLegendMenu() {
|
|
|
64
54
|
setLegendMenu(false);
|
|
65
55
|
}
|
|
66
56
|
|
|
67
|
-
/**
|
|
68
|
-
|
|
57
|
+
/**
|
|
58
|
+
* A click anywhere on the page: runs its action (`act`), then closes the Layers menu if the click was outside it. Whether it was inside is
|
|
59
|
+
* read BEFORE the action: a row's action draws the rows again, the clicked node is then out of the page, and asking afterwards would take a
|
|
60
|
+
* click on a row for a click outside the menu (main.js's document click handler).
|
|
61
|
+
* @param {EventTarget | null} target the click's target
|
|
62
|
+
* @param {() => void} act
|
|
63
|
+
*/
|
|
64
|
+
export function clickWithLegendMenu(target, act) {
|
|
65
|
+
const menu = document.getElementById('legend-dropdown');
|
|
66
|
+
const btn = document.getElementById('legend-toggle-btn');
|
|
67
|
+
const inside = !!menu && target instanceof Node && (menu.contains(target) || (!!btn && btn.contains(target)));
|
|
68
|
+
act();
|
|
69
|
+
if (menu && !menu.hidden && !inside) closeLegendMenu();
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// ---------------------------------------------------------------------------
|
|
73
|
+
// The rows (spec 0009 D1, D3, D8)
|
|
74
|
+
// ---------------------------------------------------------------------------
|
|
75
|
+
|
|
76
|
+
/** What a layer is when the viewer has not touched it: the config's `off`, else a starting layer's own default, else on */
|
|
77
|
+
const defaultOf = (key) => (Object.hasOwn(state.layerDefaults, key) ? state.layerDefaults[key] : !STARTS_OFF.has(key));
|
|
78
|
+
const isOn = (key) => state.activeLayers[key] !== false;
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* A project layer's dot: the theme's `--corn-map-group-<key>`, else the colour of the first node the layer holds (a project layer has no
|
|
82
|
+
* colour of its own in the config: it is the theme's to give, so the contrast check sees it).
|
|
83
|
+
*/
|
|
84
|
+
function projectColor(layer) {
|
|
85
|
+
const set = getMapStyle().values[groupToken(layer.key)];
|
|
86
|
+
if (set) return set;
|
|
87
|
+
const first = state.nodes.find((n) => rowKeyOf(state, n) === layer.key);
|
|
88
|
+
return first ? first.color : NO_COLOR;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const NO_COLOR = '#94a3b8';
|
|
92
|
+
/** A colour as the dot's style may hold it: a hex or an rgb()/rgba() of numbers, else the plain grey. A node's colour is the map's data
|
|
93
|
+
* (a proposal can set it), so it is checked before it reaches a style attribute, and escaped there all the same */
|
|
94
|
+
const SWATCH_RE = /^(#[0-9a-f]{3,8}|rgba?\([0-9.,%\s/]+\))$/i;
|
|
95
|
+
const swatch = (c) => (typeof c === 'string' && SWATCH_RE.test(c.trim()) ? c.trim() : NO_COLOR);
|
|
69
96
|
|
|
70
97
|
/**
|
|
71
|
-
* The rows: the
|
|
72
|
-
* a newer sync adds), so every type present can be turned off.
|
|
98
|
+
* The rows: the project layers that hold a node, then the starting ones in their usual order (`layers.order` first of all), and one more
|
|
99
|
+
* for any node type on this map that none of them covers (a type a plugin or a newer sync adds), so every type present can be turned off.
|
|
100
|
+
* `root` is always shown and has no row. The overlays are the rows with `overlay`: the menu draws them under their own heading.
|
|
73
101
|
*/
|
|
74
102
|
function layerRows() {
|
|
75
|
-
const
|
|
76
|
-
const
|
|
103
|
+
const held = new Set();
|
|
104
|
+
const left = new Set(); // starting layers a project layer took nodes from
|
|
77
105
|
state.nodes.forEach((n) => {
|
|
78
|
-
const key =
|
|
79
|
-
if (key
|
|
106
|
+
const key = rowKeyOf(state, n);
|
|
107
|
+
if (!key) return;
|
|
108
|
+
held.add(key);
|
|
109
|
+
const own = startingLayerOf(n);
|
|
110
|
+
if (own !== key) left.add(own);
|
|
111
|
+
});
|
|
112
|
+
const project = state.projectLayers.filter((l) => held.has(l.key)).map((l) => ({ key: l.key, label: l.label, desc: l.desc || '', project: true, color: projectColor(l) }));
|
|
113
|
+
// A starting row a project layer has taken every node from goes (spec 0009 D7, example 3); with no project layer, every starting row stays, as it always was
|
|
114
|
+
const starting = LAYER_DEFS.filter((l) => !l.overlay && (held.has(l.key) || !left.has(l.key)));
|
|
115
|
+
const known = new Set([...LAYER_DEFS.map((l) => l.key), ...state.projectLayers.map((l) => l.key)]);
|
|
116
|
+
const extra = [];
|
|
117
|
+
held.forEach((key) => {
|
|
118
|
+
if (known.has(key)) return;
|
|
80
119
|
known.add(key);
|
|
81
120
|
extra.push({ key, label: key.charAt(0).toUpperCase() + key.slice(1), color: '#94a3b8', desc: `Nodes of the type ${key}` });
|
|
82
121
|
});
|
|
83
|
-
return [...
|
|
122
|
+
return { layers: [...orderRows(project, starting, state.layerOrder), ...extra], overlays: LAYER_DEFS.filter((l) => l.overlay) };
|
|
84
123
|
}
|
|
85
124
|
|
|
86
125
|
/** The chip says how many layers are on: " · 13 of 20". An overlay (Runs in flight) is not a layer: it is not counted */
|
|
87
126
|
function paintCount() {
|
|
88
127
|
const el = document.getElementById('layers-count');
|
|
89
128
|
if (!el) return;
|
|
90
|
-
const layers = layerRows()
|
|
91
|
-
const on = layers.filter((l) =>
|
|
129
|
+
const { layers } = layerRows();
|
|
130
|
+
const on = layers.filter((l) => isOn(l.key)).length;
|
|
92
131
|
el.textContent = ` · ${on} of ${layers.length}`;
|
|
93
132
|
}
|
|
94
133
|
|
|
134
|
+
// ---------------------------------------------------------------------------
|
|
135
|
+
// What the viewer changed is kept in their browser (only the changes, so a layer the config adds later arrives with its default)
|
|
136
|
+
// ---------------------------------------------------------------------------
|
|
137
|
+
const STORE_KEY = 'cornfield:layers';
|
|
138
|
+
let changes = {};
|
|
139
|
+
const KEY_SHAPE = /^[a-z][a-z0-9-]{0,40}$/;
|
|
140
|
+
|
|
141
|
+
function readChanges() {
|
|
142
|
+
try {
|
|
143
|
+
const parsed = JSON.parse(window.localStorage.getItem(STORE_KEY) || '{}');
|
|
144
|
+
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) return {};
|
|
145
|
+
return Object.fromEntries(Object.entries(parsed).filter(([k, v]) => KEY_SHAPE.test(k) && typeof v === 'boolean'));
|
|
146
|
+
} catch { return {}; }
|
|
147
|
+
}
|
|
148
|
+
function writeChanges() {
|
|
149
|
+
try {
|
|
150
|
+
if (Object.keys(changes).length) window.localStorage.setItem(STORE_KEY, JSON.stringify(changes)); else window.localStorage.removeItem(STORE_KEY);
|
|
151
|
+
} catch { /* storage blocked: the choice holds for this visit */ }
|
|
152
|
+
}
|
|
153
|
+
/** Records what the viewer chose for a layer: a value equal to its default is no change */
|
|
154
|
+
function keep(key, on) {
|
|
155
|
+
if (on === defaultOf(key)) delete changes[key]; else changes[key] = on;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Takes the project's layers from the config (`layers`, as /cornfield/config.json carries it; anything that does not pass core/layers.js is
|
|
160
|
+
* ignored), sets what each layer is by default (the config's `off`), then lays the viewer's own changes over that.
|
|
161
|
+
* @param {unknown} raw
|
|
162
|
+
*/
|
|
163
|
+
export function setProjectLayers(raw) {
|
|
164
|
+
const cfg = clientLayers(raw);
|
|
165
|
+
state.projectLayers = cfg.add;
|
|
166
|
+
state.layerOrder = cfg.order;
|
|
167
|
+
const defaults = {};
|
|
168
|
+
for (const l of LAYER_DEFS) defaults[l.key] = !STARTS_OFF.has(l.key);
|
|
169
|
+
for (const l of cfg.add) defaults[l.key] = true;
|
|
170
|
+
for (const k of cfg.off) defaults[k] = false;
|
|
171
|
+
state.layerDefaults = defaults;
|
|
172
|
+
changes = readChanges();
|
|
173
|
+
for (const [k, v] of Object.entries(defaults)) state.activeLayers[k] = v;
|
|
174
|
+
for (const [k, v] of Object.entries(changes)) state.activeLayers[k] = v;
|
|
175
|
+
changed();
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// ---------------------------------------------------------------------------
|
|
179
|
+
// The menu
|
|
180
|
+
// ---------------------------------------------------------------------------
|
|
181
|
+
/** The rows whose kinds are open ("5 kinds"), by key: kept across redraws */
|
|
182
|
+
const openKinds = new Set();
|
|
183
|
+
|
|
184
|
+
/** "1 link", "3 in flight", else the number */
|
|
185
|
+
const UNIT = Object.assign(Object.create(null), { flow: (n) => `${n} ${n === 1 ? 'link' : 'links'}`, runs: (n) => `${n} in flight` });
|
|
186
|
+
|
|
187
|
+
/** The finer kinds of each row: `{ key: [{ kind, letters, n }] }`, by name, from the nodes the row holds (an outside node's kind is its layer, not a finer one) */
|
|
188
|
+
function kindsByRow(rowOf) {
|
|
189
|
+
const letters = kindLetters(state.nodes);
|
|
190
|
+
const rows = new Map();
|
|
191
|
+
state.nodes.forEach((n) => {
|
|
192
|
+
if (typeof n.kind !== 'string' || !n.kind || n.type === 'external') return;
|
|
193
|
+
const key = rowOf(n);
|
|
194
|
+
if (!key) return;
|
|
195
|
+
if (!rows.has(key)) rows.set(key, new Map());
|
|
196
|
+
const id = `${n.type}\u0000${n.kind}`;
|
|
197
|
+
const entry = rows.get(key).get(id) || { kind: n.kind, letters: letters.get(n.type).get(n.kind), n: 0 };
|
|
198
|
+
entry.n++;
|
|
199
|
+
rows.get(key).set(id, entry);
|
|
200
|
+
});
|
|
201
|
+
return new Map([...rows].map(([k, m]) => [k, [...m.values()].sort((a, b) => a.kind.localeCompare(b.kind))]));
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** What a viewer sees as "kinds" for a row: the disclosure button and, when open, the key rows. A key is a key, not a toggle (spec 0009 D5) */
|
|
205
|
+
function kindsHtml(layer, kinds) {
|
|
206
|
+
if (!kinds || !kinds.length) return '';
|
|
207
|
+
const open = openKinds.has(layer.key);
|
|
208
|
+
const id = `layer-kinds-${layer.key}`;
|
|
209
|
+
const words = `${kinds.length} ${kinds.length === 1 ? 'kind' : 'kinds'}`;
|
|
210
|
+
const list = open ? `<ul class="hx-kind-keys" id="${escHtml(id)}">${kinds.map((k) => `<li class="hx-kind-key" data-corn="kind-key"><span class="hx-kind-letter" aria-hidden="true">${escHtml(k.letters)}</span><span class="hx-kind-name">${escHtml(k.kind)}</span><span class="hx-count" data-corn="count">${k.n}</span></li>`).join('')}</ul>` : '';
|
|
211
|
+
return `<div class="hx-kinds"><button type="button" class="hx-textbtn hx-kinds-btn" data-action="toggle-layer-kinds" data-layer="${escHtml(layer.key)}" aria-expanded="${open}" aria-controls="${escHtml(id)}" data-corn="layer-kinds" data-corn-kind="quiet">${open ? '▾' : '▸'} ${escHtml(words)}</button>${list}</div>`;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** The finer kinds and project layers on the map, so the theme's tokens for them are read (map-style.js `setMapSubjects`) */
|
|
215
|
+
function syncMapSubjects() {
|
|
216
|
+
const seen = new Set();
|
|
217
|
+
const kinds = [];
|
|
218
|
+
state.nodes.forEach((n) => {
|
|
219
|
+
if (typeof n.kind !== 'string' || !n.kind) return;
|
|
220
|
+
const id = `${n.type}\u0000${n.kind}`;
|
|
221
|
+
if (!seen.has(id)) { seen.add(id); kinds.push([n.type, n.kind]); }
|
|
222
|
+
});
|
|
223
|
+
setMapSubjects({ kinds, layers: state.projectLayers.map((l) => l.key) });
|
|
224
|
+
}
|
|
225
|
+
|
|
95
226
|
export function renderLayerToggles() {
|
|
227
|
+
syncMapSubjects();
|
|
96
228
|
paintCount();
|
|
97
229
|
const container = document.getElementById('layer-toggles-container');
|
|
98
230
|
if (!container) return;
|
|
99
231
|
|
|
100
232
|
// Nodes by row. Data Flow has no nodes of its own: it counts its links, and says so; Runs in flight counts runs
|
|
101
|
-
const
|
|
102
|
-
|
|
233
|
+
const rowOf = (n) => rowKeyOf(state, n);
|
|
234
|
+
// Keyed by layer key: a project's key may be any word, "constructor" too, so these have no prototype to read through
|
|
235
|
+
const counts = Object.create(null);
|
|
236
|
+
const byProposal = Object.create(null);
|
|
237
|
+
state.nodes.forEach((n) => {
|
|
238
|
+
const key = rowOf(n);
|
|
239
|
+
if (!key) return;
|
|
240
|
+
counts[key] = (counts[key] || 0) + 1;
|
|
241
|
+
if (!layerShows(state, n) && shownByProposal(state, n)) byProposal[key] = (byProposal[key] || 0) + 1;
|
|
242
|
+
});
|
|
103
243
|
counts.flow = state.edges.filter((e) => e.kind === 'flow').length;
|
|
104
244
|
counts.runs = state.runsMap ? state.runsMap.flight.size : 0;
|
|
105
|
-
const
|
|
245
|
+
const kinds = kindsByRow(rowOf);
|
|
246
|
+
const { layers, overlays } = layerRows();
|
|
106
247
|
|
|
107
|
-
|
|
108
|
-
const on =
|
|
109
|
-
const color = themed(layer.color, state.theme); // the dot is the colour the map draws on this ground
|
|
248
|
+
const row = (layer) => {
|
|
249
|
+
const on = isOn(layer.key);
|
|
250
|
+
const color = escHtml(swatch(themed(swatch(layer.color), state.theme))); // the dot is the colour the map draws on this ground
|
|
251
|
+
const shown = byProposal[layer.key] ? `<span class="hx-faint hx-layer-note" data-corn="layer-note">+${byProposal[layer.key]} shown by the proposal</span>` : '';
|
|
110
252
|
return `
|
|
111
253
|
<button type="button" class="hx-toggle-row${on ? '' : ' is-off'}" data-action="toggle-layer" data-layer="${escHtml(layer.key)}" aria-pressed="${on}" title="${escHtml(layer.desc)}" data-corn="row">
|
|
112
254
|
<span class="hx-toggle-label">
|
|
113
255
|
<span class="hx-dot" data-corn="swatch" style="background-color: ${color}; box-shadow: ${on && state.theme !== 'light' ? `0 0 8px ${color}90` : 'none'}"></span>
|
|
114
256
|
<span class="hx-toggle-name">${escHtml(layer.label)}</span>${layer.live ? '<span class="hx-live-tag" data-corn="live-tag">live</span>' : ''}
|
|
115
257
|
</span>
|
|
116
|
-
<span class="hx-toggle-meta"
|
|
117
|
-
<span class="hx-count" data-corn="count">${(
|
|
258
|
+
<span class="hx-toggle-meta">${shown}
|
|
259
|
+
<span class="hx-count" data-corn="count">${(UNIT[layer.key] || String)(counts[layer.key] || 0)}</span>
|
|
118
260
|
<span class="hx-tick">${on ? '✓' : '—'}</span>
|
|
119
261
|
</span>
|
|
120
|
-
</button
|
|
121
|
-
}
|
|
262
|
+
</button>${kindsHtml(layer, kinds.get(layer.key))}`;
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
// The focus stays where it was across the redraw: the same row or disclosure, found again by what it names
|
|
266
|
+
const active = document.activeElement && container.contains(document.activeElement) ? document.activeElement : null;
|
|
267
|
+
const was = active ? { action: active.getAttribute('data-action'), layer: active.getAttribute('data-layer') } : null;
|
|
268
|
+
container.innerHTML = layers.map(row).join('')
|
|
269
|
+
+ (overlays.length ? `<div class="hx-menu-title hx-overlays" data-corn="overlays"><span>${escHtml(t('overlays'))}</span></div>${overlays.map(row).join('')}` : '');
|
|
270
|
+
if (was) {
|
|
271
|
+
const again = [...container.querySelectorAll('[data-action]')].find((el) => el.getAttribute('data-action') === was.action && el.getAttribute('data-layer') === was.layer);
|
|
272
|
+
if (again && typeof again.focus === 'function') again.focus({ preventScroll: true });
|
|
273
|
+
}
|
|
122
274
|
renderFlowLegend();
|
|
123
275
|
renderCloudLegend();
|
|
124
276
|
}
|
|
@@ -143,14 +295,48 @@ function changed() {
|
|
|
143
295
|
ensureFlowLoop();
|
|
144
296
|
}
|
|
145
297
|
|
|
146
|
-
|
|
147
|
-
|
|
298
|
+
/** The words a screen reader gets when a layer goes on or off, with how many nodes it now draws */
|
|
299
|
+
function say(text) {
|
|
300
|
+
const el = document.getElementById('map-announcer');
|
|
301
|
+
if (!el) return;
|
|
302
|
+
el.textContent = '';
|
|
303
|
+
setTimeout(() => { el.textContent = text; }, 30);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* Turns a layer on or off. `viewer`: the person chose it in the menu, so it is kept in their browser; a layer another part of the app
|
|
308
|
+
* turns on (a deploy that lights a stack) is not a choice of theirs and is not kept.
|
|
309
|
+
*/
|
|
310
|
+
export function toggleLayer(key, { viewer = false } = {}) {
|
|
311
|
+
const on = state.activeLayers[key] === false; // a layer with no entry yet is on, as the toggles show it
|
|
312
|
+
state.activeLayers[key] = on;
|
|
313
|
+
if (viewer) { keep(key, on); writeChanges(); }
|
|
148
314
|
changed();
|
|
315
|
+
if (viewer) {
|
|
316
|
+
const { layers, overlays } = layerRows();
|
|
317
|
+
const layer = [...layers, ...overlays].find((l) => l.key === key);
|
|
318
|
+
const n = state.nodes.filter((x) => isNodeVisible(x)).length;
|
|
319
|
+
say(`${layer ? layer.label : key} ${on ? 'on' : 'off'}. ${n} ${n === 1 ? 'node' : 'nodes'} shown.`);
|
|
320
|
+
}
|
|
149
321
|
}
|
|
150
322
|
|
|
151
|
-
|
|
152
|
-
|
|
323
|
+
/** All / None: every layer on or off, never an overlay; the root stays drawn */
|
|
324
|
+
export function toggleAllLayers(on, { viewer = false } = {}) {
|
|
325
|
+
const { layers } = layerRows();
|
|
326
|
+
const keys = new Set([...LAYER_DEFS.filter((l) => !l.overlay).map((l) => l.key), ...state.projectLayers.map((l) => l.key), ...layers.map((l) => l.key)]);
|
|
327
|
+
keys.forEach((k) => { state.activeLayers[k] = on; if (viewer) keep(k, on); });
|
|
328
|
+
if (viewer) writeChanges();
|
|
153
329
|
changed();
|
|
330
|
+
if (viewer) {
|
|
331
|
+
const n = state.nodes.filter((x) => isNodeVisible(x)).length;
|
|
332
|
+
say(`${on ? 'All' : 'No'} layers on. ${n} ${n === 1 ? 'node' : 'nodes'} shown.`);
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/** Opens or closes the kinds of one row ("5 kinds"); the focus stays on its button */
|
|
337
|
+
export function toggleLayerKinds(key) {
|
|
338
|
+
if (openKinds.has(key)) openKinds.delete(key); else openKinds.add(key);
|
|
339
|
+
renderLayerToggles();
|
|
154
340
|
}
|
|
155
341
|
|
|
156
342
|
// The dots and the legends are drawn in the theme's colours: drawn again when it changes (theme.js)
|