@intentius/behold 0.29.41 → 0.29.43
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/AGENTS.md +32 -0
- package/dist/cli.js +111 -20
- package/package.json +1 -1
- package/web/app.js +12 -8
- package/web/carve-narration.js +32 -6
- package/web/carve-narration.test.js +31 -1
- package/web/dots-cards.js +362 -0
- package/web/dots-cards.test.js +303 -0
- package/web/dots.js +363 -74
- package/web/embed.js +16 -0
- package/web/embed.test.js +19 -1
- package/web/event-help-modal.js +1 -1
- package/web/index.html +192 -127
- package/web/kiosk-carve.js +26 -20
- package/web/kiosk-event-help-ui.js +1 -1
- package/web/kiosk-link.js +68 -0
- package/web/kiosk-link.test.js +69 -0
- package/web/kiosk-metrics.js +2 -2
- package/web/kiosk-trace.js +1 -1
- package/web/kiosk.html +186 -135
- package/web/kiosk.js +44 -1
- package/web/motion.js +42 -0
- package/web/motion.test.js +64 -0
- package/web/terralith.html +2 -2
- package/web/theme.js +99 -26
- package/web/theme.test.js +70 -6
- package/web/tooltip.js +212 -0
- package/web/tooltip.test.js +193 -0
- package/web/trunk-lane.js +160 -8
- package/web/trunk-lane.test.js +108 -1
|
@@ -0,0 +1,362 @@
|
|
|
1
|
+
// The dots layer's resource cards from the roster: the card level where the
|
|
2
|
+
// page's SVG has no card for a resource. A live carve framed at the kiosk's
|
|
3
|
+
// Components zoom draws one SVG card per root, never one per resource, so the
|
|
4
|
+
// cards a dot grows into are drawn on the canvas instead, from the roster row
|
|
5
|
+
// alone: its address, its type and the estate that owns it now.
|
|
6
|
+
//
|
|
7
|
+
// A dot's slot leaves about one pitch round it, far too little for a card that
|
|
8
|
+
// can hold a name. So at the card level each estate's resources move from
|
|
9
|
+
// their slots into a grid of cards that fills the estate's own box, in unit
|
|
10
|
+
// order: a dot grows into its card as it slides to its cell, and the boxes
|
|
11
|
+
// never move. One card size serves every estate and every moment of the show,
|
|
12
|
+
// so a card never changes size as the carve moves resources about. Pure: rows,
|
|
13
|
+
// layouts and sizes in, plain data out.
|
|
14
|
+
|
|
15
|
+
import { unitOf, parseAddress } from "./dots-layout.js";
|
|
16
|
+
import { BOX } from "./dots-scene.js";
|
|
17
|
+
import { LOD, lerp, smooth } from "./dots-lod.js";
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* A grid cell is `aspect` cell heights wide; the card leaves `gap` cell
|
|
21
|
+
* heights to its neighbours. `maxK` caps a cell's height in pitches, so a box
|
|
22
|
+
* with few resources does not get giant cards.
|
|
23
|
+
*/
|
|
24
|
+
export const GRID = { aspect: 3.2, gap: 0.12, maxK: 3 };
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The label's faces, in CSS px: the name over the type, growing with the card
|
|
28
|
+
* up to `max`; `pad` and `bar` (the estate's colour down the left) in px.
|
|
29
|
+
* `need` is clamped to [minW, maxW]: the card width at which the card level
|
|
30
|
+
* starts.
|
|
31
|
+
*/
|
|
32
|
+
export const CARD_TEXT = { name: 11.5, type: 10.5, max: 15, pad: 7, bar: 3, minW: 90, maxW: 230 };
|
|
33
|
+
|
|
34
|
+
/** The largest cell height (in the box's units) at which `n` cells `aspect` heights wide fit a W x H box in rows. */
|
|
35
|
+
export function bestFit(W, H, n, aspect = GRID.aspect) {
|
|
36
|
+
if (!(n > 0) || !(W > 0) || !(H > 0)) return 0;
|
|
37
|
+
let best = 0;
|
|
38
|
+
for (let c = 1; c <= n; c++) {
|
|
39
|
+
const k = Math.min(W / (c * aspect), H / Math.ceil(n / c));
|
|
40
|
+
if (k > best) best = k;
|
|
41
|
+
}
|
|
42
|
+
return best;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** A box's inner rect, where its resources go: inside its padding, under its title row. */
|
|
46
|
+
export function innerOf(b) {
|
|
47
|
+
return { x: b.x + BOX.pad, y: b.y + BOX.pad + BOX.title, w: Math.max(0, b.w - 2 * BOX.pad), h: Math.max(0, b.h - 2 * BOX.pad - BOX.title) };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Each estate's resources in a layout, in unit order (web/dots-layout.js), so a unit's cards sit together. */
|
|
51
|
+
export function membersOf(layout, keyOf = (a) => a) {
|
|
52
|
+
const by = new Map();
|
|
53
|
+
for (const a of layout.slots.keys()) {
|
|
54
|
+
const e = layout.owners.get(a);
|
|
55
|
+
if (!layout.boxes.has(e)) continue;
|
|
56
|
+
if (!by.has(e)) by.set(e, []);
|
|
57
|
+
by.get(e).push(a);
|
|
58
|
+
}
|
|
59
|
+
for (const list of by.values()) list.sort((x, y) => {
|
|
60
|
+
const kx = keyOf(x);
|
|
61
|
+
const ky = keyOf(y);
|
|
62
|
+
return kx < ky ? -1 : kx > ky ? 1 : x < y ? -1 : x > y ? 1 : 0;
|
|
63
|
+
});
|
|
64
|
+
return by;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* The one cell height (in pitches) every estate's cards fit at, across the
|
|
69
|
+
* given layouts: the tightest box's best fit, capped at GRID.maxK.
|
|
70
|
+
*/
|
|
71
|
+
export function gridScale(layouts, keyOf, { aspect = GRID.aspect, maxK = GRID.maxK } = {}) {
|
|
72
|
+
let k = maxK;
|
|
73
|
+
for (const L of layouts) {
|
|
74
|
+
for (const [e, list] of membersOf(L, keyOf)) {
|
|
75
|
+
const r = innerOf(L.boxes.get(e));
|
|
76
|
+
const f = bestFit(r.w, r.h, list.length, aspect);
|
|
77
|
+
if (f > 0) k = Math.min(k, f);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return k;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** A card's size in pitches for cell height `k`. */
|
|
84
|
+
export function cardSize(k, { aspect = GRID.aspect, gap = GRID.gap } = {}) {
|
|
85
|
+
return { w: k * (aspect - gap), h: k * (1 - gap) };
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Each resource's card centre in a layout, in pitches: its estate's
|
|
90
|
+
* resources in rows of cells `aspect * k` wide and `k` high, centred in the
|
|
91
|
+
* box's inner rect, in unit order.
|
|
92
|
+
*/
|
|
93
|
+
export function gridSlots(layout, k, keyOf, { aspect = GRID.aspect } = {}) {
|
|
94
|
+
const out = new Map();
|
|
95
|
+
if (!(k > 0)) return out;
|
|
96
|
+
const cw = aspect * k;
|
|
97
|
+
for (const [e, list] of membersOf(layout, keyOf)) {
|
|
98
|
+
const r = innerOf(layout.boxes.get(e));
|
|
99
|
+
const n = list.length;
|
|
100
|
+
const cols = Math.max(1, Math.min(n, Math.floor(r.w / cw + 1e-9)));
|
|
101
|
+
const rows = Math.ceil(n / cols);
|
|
102
|
+
const x0 = r.x + (r.w - cols * cw) / 2;
|
|
103
|
+
const y0 = r.y + Math.max(0, (r.h - rows * k) / 2);
|
|
104
|
+
list.forEach((a, i) => out.set(a, { x: x0 + ((i % cols) + 0.5) * cw, y: y0 + (Math.floor(i / cols) + 0.5) * k }));
|
|
105
|
+
}
|
|
106
|
+
return out;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** The unit-order key of a roster row, for {@link membersOf}. */
|
|
110
|
+
export function unitKeyOf(rosterBy) {
|
|
111
|
+
return (a) => {
|
|
112
|
+
const r = rosterBy.get(a);
|
|
113
|
+
return unitOf(r ? r.address : a, r ? r.type : "");
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* The type as a card shows it: without its provider's prefix
|
|
119
|
+
* (`aws_iam_role` is `iam_role`), since every card on the stage is the same
|
|
120
|
+
* provider's. `terraform_data` and the like keep theirs, and a type that is
|
|
121
|
+
* all prefix stays whole.
|
|
122
|
+
*/
|
|
123
|
+
export function shortType(type) {
|
|
124
|
+
const t = String(type || "");
|
|
125
|
+
const m = /^([a-z0-9]+)_(.+)$/.exec(t);
|
|
126
|
+
if (!m || m[1] === "terraform") return t;
|
|
127
|
+
return m[2];
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* What a roster row's card says: the short address (the resource's name and
|
|
132
|
+
* index, without its module path or type) and the type without its
|
|
133
|
+
* provider's prefix. A live dot's id may carry behold's own `#2` suffix (two
|
|
134
|
+
* rows with one address); that is not the roster's, so it is not shown.
|
|
135
|
+
*/
|
|
136
|
+
export function cardLabel(row) {
|
|
137
|
+
const address = String((row && row.address) || "").replace(/#\d+$/, "");
|
|
138
|
+
const parts = parseAddress(address);
|
|
139
|
+
const name = parts.name ? `${parts.name}${parts.index}` : address;
|
|
140
|
+
return { name, type: shortType((row && row.type) || (parts.name ? parts.type : "")), address };
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* The card width, in CSS px, at which the card level starts: wide enough for
|
|
145
|
+
* the longest name at the name's face and the longest type at the type's,
|
|
146
|
+
* clamped to [minW, maxW]. A name still wider than that is cut from the front.
|
|
147
|
+
* `measure(text, px, weight)` is the text's width.
|
|
148
|
+
*/
|
|
149
|
+
export function cardNeed(labels, measure) {
|
|
150
|
+
const t = CARD_TEXT;
|
|
151
|
+
let w = 0;
|
|
152
|
+
for (const l of labels) {
|
|
153
|
+
w = Math.max(w, measure(l.name, t.name, 600));
|
|
154
|
+
if (l.type) w = Math.max(w, measure(l.type, t.type, 400));
|
|
155
|
+
}
|
|
156
|
+
return Math.max(t.minW, Math.min(t.maxW, Math.ceil(w + t.pad * 2 + t.bar + 2)));
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** The level of detail for roster cards: a card at `need` px, a dot at the same share of it a card's dot is. */
|
|
160
|
+
export function rosterLod(need) {
|
|
161
|
+
return { ...LOD, cardPx: need, dotPx: (need * LOD.dotPx) / LOD.cardPx };
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* `text` cut from the front until `measure(text)` fits `room`, with an
|
|
166
|
+
* ellipsis where the cut is: the end of a name is the part that tells two
|
|
167
|
+
* apart. Returns "" when not even the ellipsis and one character fit.
|
|
168
|
+
*/
|
|
169
|
+
export function fitFront(text, room, measure) {
|
|
170
|
+
const s = String(text || "");
|
|
171
|
+
if (!s || !(room > 0)) return "";
|
|
172
|
+
if (measure(s) <= room) return s;
|
|
173
|
+
let lo = 1;
|
|
174
|
+
let hi = s.length - 1;
|
|
175
|
+
let best = "";
|
|
176
|
+
while (lo <= hi) {
|
|
177
|
+
const mid = (lo + hi) >> 1;
|
|
178
|
+
const cand = `…${s.slice(s.length - mid)}`;
|
|
179
|
+
if (measure(cand) <= room) {
|
|
180
|
+
best = cand;
|
|
181
|
+
lo = mid + 1;
|
|
182
|
+
} else hi = mid - 1;
|
|
183
|
+
}
|
|
184
|
+
return best;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* A type too wide for its line, on two lines broken after an underscore,
|
|
189
|
+
* each fitting `room`; null when no break fits.
|
|
190
|
+
*/
|
|
191
|
+
export function wrapType(text, room, measure) {
|
|
192
|
+
const s = String(text || "");
|
|
193
|
+
let best = null;
|
|
194
|
+
for (let i = s.indexOf("_"); i > 0; i = s.indexOf("_", i + 1)) {
|
|
195
|
+
const a = s.slice(0, i + 1);
|
|
196
|
+
const b = s.slice(i + 1);
|
|
197
|
+
if (!b || measure(a) > room || measure(b) > room) continue;
|
|
198
|
+
// The break nearest the middle.
|
|
199
|
+
if (!best || Math.abs(a.length - b.length) < Math.abs(best[0].length - best[1].length)) best = [a, b];
|
|
200
|
+
}
|
|
201
|
+
return best;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* The form a resource is drawn in, from `u`: 0 is its dot, 1 its card. The
|
|
206
|
+
* dot's disc grows into the card's rounded rect about the same centre. Sizes
|
|
207
|
+
* are in screen px.
|
|
208
|
+
*/
|
|
209
|
+
export function cardShape(cx, cy, u, { dotR, cardW, cardH }) {
|
|
210
|
+
const k = Math.max(0, Math.min(1, u));
|
|
211
|
+
const w = lerp(dotR * 2, Math.max(dotR * 2, cardW), k);
|
|
212
|
+
const h = lerp(dotR * 2, Math.max(dotR * 2, cardH), k);
|
|
213
|
+
const r = lerp(dotR, Math.min(6, h * 0.22), k);
|
|
214
|
+
return { x: cx - w / 2, y: cy - h / 2, w, h, r };
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/** How far a card's words have faded in at morph `u`: none until most of the way, so a growing dot never shows half-sized text. */
|
|
218
|
+
export function cardTextAlpha(u) {
|
|
219
|
+
return smooth((u - 0.6) / 0.35);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* The card's text, laid out for a card `w` x `h` px whose card level starts
|
|
224
|
+
* at `need` px: the name, then the type in muted ink. The faces are their
|
|
225
|
+
* sizes at `need` and grow with the card, so whatever fits at `need` fits at
|
|
226
|
+
* every size past it. A type too wide goes onto two lines when the card is
|
|
227
|
+
* tall enough, and is cut from the front when it is not; a name too wide is
|
|
228
|
+
* cut from the front. The words never shrink below their faces: a line the
|
|
229
|
+
* card has no room for is left out. `measure(text, px, weight)` is the
|
|
230
|
+
* text's width.
|
|
231
|
+
*/
|
|
232
|
+
export function cardText(label, { w, h, need = w }, measure) {
|
|
233
|
+
const t = CARD_TEXT;
|
|
234
|
+
const grow = Math.max(1, w / Math.max(1, need));
|
|
235
|
+
const fs = Math.min(t.max, t.name * grow);
|
|
236
|
+
const ft = Math.min(t.max - 1, t.type * grow);
|
|
237
|
+
const room = w - t.pad * 2 - t.bar;
|
|
238
|
+
const lines = [];
|
|
239
|
+
if (room < fs * 2 || h < fs * 1.3 + 2) return lines;
|
|
240
|
+
const name = fitFront(label.name, room, (s) => measure(s, fs, 600));
|
|
241
|
+
if (name) lines.push({ text: name, size: fs, weight: 600, ink: "fg" });
|
|
242
|
+
const lineH = (n) => (fs + ft * n) * 1.25 + 6;
|
|
243
|
+
if (label.type && h >= lineH(1)) {
|
|
244
|
+
const m = (s) => measure(s, ft, 400);
|
|
245
|
+
const two = m(label.type) > room && h >= lineH(2) ? wrapType(label.type, room, m) : null;
|
|
246
|
+
if (two) for (const part of two) lines.push({ text: part, size: ft, weight: 400, ink: "muted" });
|
|
247
|
+
else {
|
|
248
|
+
const type = fitFront(label.type, room, m);
|
|
249
|
+
if (type) lines.push({ text: type, size: ft, weight: 400, ink: "muted" });
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
return lines;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Which drawn mark a point is on: a card when the point is inside its rect
|
|
257
|
+
* (with `slack` px round it), else the nearest dot within `within` px. `pw`
|
|
258
|
+
* and `ph` are each mark's half width and height on screen, 0 for a dot.
|
|
259
|
+
* Returns the index, or -1.
|
|
260
|
+
*/
|
|
261
|
+
export function pickMark(x, y, { px, py, pvis, pw, ph }, { within = 10, slack = 2 } = {}) {
|
|
262
|
+
let best = -1;
|
|
263
|
+
let bd = within * within;
|
|
264
|
+
let inside = -1;
|
|
265
|
+
let insideD = Infinity;
|
|
266
|
+
for (let i = 0; i < px.length; i++) {
|
|
267
|
+
if (!pvis[i]) continue;
|
|
268
|
+
const dx = px[i] - x;
|
|
269
|
+
const dy = py[i] - y;
|
|
270
|
+
const d = dx * dx + dy * dy;
|
|
271
|
+
if (pw && pw[i] > 0 && Math.abs(dx) <= pw[i] + slack && Math.abs(dy) <= ph[i] + slack) {
|
|
272
|
+
// Two cards that meet: the one whose centre is nearer.
|
|
273
|
+
if (d < insideD) {
|
|
274
|
+
insideD = d;
|
|
275
|
+
inside = i;
|
|
276
|
+
}
|
|
277
|
+
continue;
|
|
278
|
+
}
|
|
279
|
+
if (d < bd) {
|
|
280
|
+
bd = d;
|
|
281
|
+
best = i;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
return inside >= 0 ? inside : best;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* The rail's Cards mark: how many cards can draw at the card level, and its
|
|
289
|
+
* words. `own`: the canvas draws the roster's cards (the page's SVG has none
|
|
290
|
+
* for these resources), so every resource has one. Else the cards are the
|
|
291
|
+
* SVG's: the resources it has a card for (`seeded`), or, when it has a card
|
|
292
|
+
* for none of them, the cards it draws of its own (`svgCards`, a live carve's
|
|
293
|
+
* roots).
|
|
294
|
+
*/
|
|
295
|
+
export function cardsMark({ resources = 0, seeded = 0, svgCards = 0, own = false } = {}) {
|
|
296
|
+
const count = own ? resources : seeded > 0 ? seeded : svgCards;
|
|
297
|
+
const n = count.toLocaleString("en-US");
|
|
298
|
+
return { count, words: count === 0 ? "no cards" : `${n} card${count === 1 ? "" : "s"}`, enabled: count > 0 };
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* Which estate the Cards mark lands on: the director's estate when it holds
|
|
303
|
+
* resources, else the source estate when it does, else the estate holding
|
|
304
|
+
* the most. null when no estate holds any.
|
|
305
|
+
*/
|
|
306
|
+
export function cardsEstate(focusEstate, source, counts) {
|
|
307
|
+
if (focusEstate && (counts.get(focusEstate) ?? 0) > 0) return focusEstate;
|
|
308
|
+
if ((counts.get(source) ?? 0) > 0) return source;
|
|
309
|
+
let best = null;
|
|
310
|
+
for (const [e, n] of counts) if (n > 0 && (best === null || n > counts.get(best))) best = e;
|
|
311
|
+
return best;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/** The card nearest the middle of a set of cards: where the Cards mark centres, so it always lands on one. */
|
|
315
|
+
export function middleCard(points) {
|
|
316
|
+
if (!points.length) return null;
|
|
317
|
+
let cx = 0;
|
|
318
|
+
let cy = 0;
|
|
319
|
+
for (const p of points) {
|
|
320
|
+
cx += p.x;
|
|
321
|
+
cy += p.y;
|
|
322
|
+
}
|
|
323
|
+
cx /= points.length;
|
|
324
|
+
cy /= points.length;
|
|
325
|
+
let best = points[0];
|
|
326
|
+
let bd = Infinity;
|
|
327
|
+
for (const p of points) {
|
|
328
|
+
const d = (p.x - cx) ** 2 + (p.y - cy) ** 2;
|
|
329
|
+
if (d < bd) {
|
|
330
|
+
bd = d;
|
|
331
|
+
best = p;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
return best;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/** A viewBox `w` wide centred on a point, at the pane's aspect (height / width). */
|
|
338
|
+
export function cardsFrame(at, w, aspect) {
|
|
339
|
+
const h = w * aspect;
|
|
340
|
+
return [at.x - w / 2, at.y - h / 2, w, h];
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/**
|
|
344
|
+
* How far in the zoom may go while the canvas draws the roster's cards: a
|
|
345
|
+
* viewBox no narrower than the one where a card is `past` times the width at
|
|
346
|
+
* which it is fully drawn. `paneW` px, `cardW` in viewBox units.
|
|
347
|
+
*/
|
|
348
|
+
export function minViewWidth(paneW, cardW, need, past = 1.8) {
|
|
349
|
+
return (paneW * cardW) / (need * past);
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* Where a tip goes beside a point: right of and below it, kept inside the
|
|
354
|
+
* pane less the rail on its right (`rightInset`), and over the mark when the
|
|
355
|
+
* pane's foot leaves no room under it.
|
|
356
|
+
*/
|
|
357
|
+
export function tipSpot(x, y, { tipW, tipH, paneW, paneH, rightInset = 0, gap = 12, below = 14, above = y }) {
|
|
358
|
+
const left = Math.max(4, Math.min(x + gap, paneW - rightInset - tipW - 4));
|
|
359
|
+
// No room under the mark: over it, from `above` (the mark's top edge).
|
|
360
|
+
const top = y + below + tipH > paneH - 4 ? Math.max(4, above - 6 - tipH) : y + below;
|
|
361
|
+
return { left, top };
|
|
362
|
+
}
|
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
GRID,
|
|
4
|
+
CARD_TEXT,
|
|
5
|
+
bestFit,
|
|
6
|
+
innerOf,
|
|
7
|
+
membersOf,
|
|
8
|
+
gridScale,
|
|
9
|
+
gridSlots,
|
|
10
|
+
cardSize,
|
|
11
|
+
shortType,
|
|
12
|
+
cardLabel,
|
|
13
|
+
cardNeed,
|
|
14
|
+
rosterLod,
|
|
15
|
+
fitFront,
|
|
16
|
+
wrapType,
|
|
17
|
+
cardShape,
|
|
18
|
+
cardText,
|
|
19
|
+
cardTextAlpha,
|
|
20
|
+
pickMark,
|
|
21
|
+
cardsMark,
|
|
22
|
+
cardsEstate,
|
|
23
|
+
middleCard,
|
|
24
|
+
cardsFrame,
|
|
25
|
+
minViewWidth,
|
|
26
|
+
tipSpot,
|
|
27
|
+
} from "./dots-cards.js";
|
|
28
|
+
import { LOD, cardMorph } from "./dots-lod.js";
|
|
29
|
+
import { BOX } from "./dots-scene.js";
|
|
30
|
+
|
|
31
|
+
// A face about as wide as the UI's: 0.55 em a character.
|
|
32
|
+
const measure = (s, px = CARD_TEXT.name) => s.length * px * 0.55;
|
|
33
|
+
|
|
34
|
+
// A layout as web/dots-scene.js's layoutAt makes one: slots, boxes and owners.
|
|
35
|
+
function layout(estates) {
|
|
36
|
+
const slots = new Map();
|
|
37
|
+
const boxes = new Map();
|
|
38
|
+
const owners = new Map();
|
|
39
|
+
for (const [e, { box, addrs }] of Object.entries(estates)) {
|
|
40
|
+
boxes.set(e, box);
|
|
41
|
+
for (const a of addrs) {
|
|
42
|
+
slots.set(a, { x: box.x + 2, y: box.y + 5 });
|
|
43
|
+
owners.set(a, e);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return { slots, boxes, owners };
|
|
47
|
+
}
|
|
48
|
+
const box = (x, y, w, h) => ({ x, y, w, h, members: 1 });
|
|
49
|
+
const ids = (pre, n) => Array.from({ length: n }, (_, i) => `${pre}.r${String(i).padStart(2, "0")}`);
|
|
50
|
+
|
|
51
|
+
describe("the card grid fills each estate's box", () => {
|
|
52
|
+
it("finds the largest cell that fits n cells in rows", () => {
|
|
53
|
+
// 4 cells 2 wide in a 4 x 2 box: two rows of two, each 1 high.
|
|
54
|
+
expect(bestFit(4, 2, 4, 2)).toBe(1);
|
|
55
|
+
expect(bestFit(10, 1, 1, 2)).toBe(1);
|
|
56
|
+
expect(bestFit(0, 5, 3)).toBe(0);
|
|
57
|
+
expect(bestFit(5, 5, 0)).toBe(0);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("puts every card inside its box's inner rect, none overlapping, in unit order", () => {
|
|
61
|
+
const L = layout({ a: { box: box(0, 0, 30, 20), addrs: ids("a", 9) }, b: { box: box(40, 0, 12, 14), addrs: ids("b", 3) } });
|
|
62
|
+
const k = gridScale([L], (x) => x);
|
|
63
|
+
const cells = gridSlots(L, k, (x) => x);
|
|
64
|
+
const c = cardSize(k);
|
|
65
|
+
expect(cells.size).toBe(12);
|
|
66
|
+
for (const [e] of L.boxes) {
|
|
67
|
+
const r = innerOf(L.boxes.get(e));
|
|
68
|
+
const mine = [...cells].filter(([a]) => L.owners.get(a) === e).map(([, p]) => p);
|
|
69
|
+
for (const p of mine) {
|
|
70
|
+
expect(p.x - c.w / 2).toBeGreaterThanOrEqual(r.x - 1e-9);
|
|
71
|
+
expect(p.x + c.w / 2).toBeLessThanOrEqual(r.x + r.w + 1e-9);
|
|
72
|
+
expect(p.y - c.h / 2).toBeGreaterThanOrEqual(r.y - 1e-9);
|
|
73
|
+
expect(p.y + c.h / 2).toBeLessThanOrEqual(r.y + r.h + 1e-9);
|
|
74
|
+
}
|
|
75
|
+
for (let i = 0; i < mine.length; i++)
|
|
76
|
+
for (let j = i + 1; j < mine.length; j++) expect(Math.abs(mine[i].x - mine[j].x) >= c.w || Math.abs(mine[i].y - mine[j].y) >= c.h).toBe(true);
|
|
77
|
+
}
|
|
78
|
+
// Reading order is unit order: the first card is top-left.
|
|
79
|
+
const first = cells.get("a.r00");
|
|
80
|
+
for (const a of ids("a", 9)) expect(cells.get(a).y >= first.y && (cells.get(a).y > first.y || cells.get(a).x >= first.x)).toBe(true);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("takes one cell size for every estate and moment: the tightest box's, capped", () => {
|
|
84
|
+
const roomy = layout({ a: { box: box(0, 0, 200, 200), addrs: ids("a", 1) } });
|
|
85
|
+
expect(gridScale([roomy], (x) => x)).toBe(GRID.maxK);
|
|
86
|
+
const tight = layout({ a: { box: box(0, 0, 200, 200), addrs: ids("a", 1) }, b: { box: box(0, 0, 3 + 2 * BOX.pad, 3 + 2 * BOX.pad + BOX.title), addrs: ids("b", 1) } });
|
|
87
|
+
expect(gridScale([roomy, tight], (x) => x)).toBeCloseTo(3 / GRID.aspect, 9);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it("gives a card several times the room of a dot's slot", () => {
|
|
91
|
+
// A one-unit box: an anchor and its ring, as web/dots-layout.js packs it (a 9-pitch cell holds about 6).
|
|
92
|
+
const L = layout({ a: { box: box(0, 0, 9 + 2 * BOX.pad, 9 + 2 * BOX.pad + BOX.title), addrs: ids("a", 6) } });
|
|
93
|
+
const c = cardSize(gridScale([L], (x) => x));
|
|
94
|
+
expect(c.w).toBeGreaterThan(3 * 0.88);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it("groups an estate's members by the key it is given", () => {
|
|
98
|
+
const L = layout({ a: { box: box(0, 0, 30, 20), addrs: ["z", "y", "x"] } });
|
|
99
|
+
expect(membersOf(L, (a) => ({ z: "1", y: "2", x: "3" })[a]).get("a")).toEqual(["z", "y", "x"]);
|
|
100
|
+
expect(membersOf(L).get("a")).toEqual(["x", "y", "z"]);
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
describe("roster cards: words from a roster row", () => {
|
|
105
|
+
it("labels a card with the resource's name and index, and its type without the provider's prefix", () => {
|
|
106
|
+
expect(cardLabel({ address: 'module.team_a.aws_s3_bucket.logs["eu"]', type: "aws_s3_bucket" })).toEqual({ name: 'logs["eu"]', type: "s3_bucket", address: 'module.team_a.aws_s3_bucket.logs["eu"]' });
|
|
107
|
+
expect(cardLabel({ address: "aws_iam_role.svc_role[2]", type: "" })).toMatchObject({ name: "svc_role[2]", type: "iam_role" });
|
|
108
|
+
expect(cardLabel({ address: "data.aws_vpc.main", type: "aws_vpc" })).toMatchObject({ name: "main", type: "vpc" });
|
|
109
|
+
});
|
|
110
|
+
it("keeps a type that is all prefix, and terraform's own", () => {
|
|
111
|
+
expect(shortType("aws_iam_role_policy_attachment")).toBe("iam_role_policy_attachment");
|
|
112
|
+
expect(shortType("terraform_data")).toBe("terraform_data");
|
|
113
|
+
expect(shortType("null")).toBe("null");
|
|
114
|
+
expect(shortType("")).toBe("");
|
|
115
|
+
});
|
|
116
|
+
it("never shows behold's own suffix for two rows with one address", () => {
|
|
117
|
+
expect(cardLabel({ address: "aws_sqs_queue.jobs#2", type: "aws_sqs_queue" })).toMatchObject({ name: "jobs", address: "aws_sqs_queue.jobs" });
|
|
118
|
+
});
|
|
119
|
+
it("falls back to the whole address when it names no resource", () => {
|
|
120
|
+
expect(cardLabel({ address: "odd", type: "" })).toMatchObject({ name: "odd", type: "" });
|
|
121
|
+
expect(cardLabel(null)).toMatchObject({ name: "", type: "" });
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
describe("the card level starts where the names fit", () => {
|
|
126
|
+
const labels = [
|
|
127
|
+
{ name: "team_0001_managed_attach", type: "iam_role_policy_attachment" },
|
|
128
|
+
{ name: "team_0001_role", type: "iam_role" },
|
|
129
|
+
];
|
|
130
|
+
it("is wide enough for the longest name at the name's face and the longest type at the type's", () => {
|
|
131
|
+
const need = cardNeed(labels, measure);
|
|
132
|
+
const room = need - CARD_TEXT.pad * 2 - CARD_TEXT.bar;
|
|
133
|
+
expect(measure("team_0001_managed_attach", CARD_TEXT.name)).toBeLessThanOrEqual(room);
|
|
134
|
+
expect(measure("iam_role_policy_attachment", CARD_TEXT.type)).toBeLessThanOrEqual(room);
|
|
135
|
+
});
|
|
136
|
+
it("is clamped", () => {
|
|
137
|
+
expect(cardNeed([{ name: "a", type: "" }], measure)).toBe(CARD_TEXT.minW);
|
|
138
|
+
expect(cardNeed([{ name: "x".repeat(200), type: "" }], measure)).toBe(CARD_TEXT.maxW);
|
|
139
|
+
});
|
|
140
|
+
it("scales the card's level of detail to it", () => {
|
|
141
|
+
const lod = rosterLod(180);
|
|
142
|
+
expect(cardMorph(180, { lod })).toBe(0);
|
|
143
|
+
expect(cardMorph((180 * LOD.dotPx) / LOD.cardPx, { lod })).toBe(1);
|
|
144
|
+
});
|
|
145
|
+
it("shows a whole name and a whole type on a card at that width", () => {
|
|
146
|
+
const need = cardNeed(labels, measure);
|
|
147
|
+
const h = (need * cardSize(1).h) / cardSize(1).w;
|
|
148
|
+
const lines = cardText({ name: "team_0001_managed_attach", type: "iam_role_policy_attachment" }, { w: need, h, need }, measure);
|
|
149
|
+
expect(lines.map((l) => l.text)).toEqual(["team_0001_managed_attach", "iam_role_policy_attachment"]);
|
|
150
|
+
expect(lines[0].size).toBe(CARD_TEXT.name);
|
|
151
|
+
expect(lines[1].size).toBe(CARD_TEXT.type);
|
|
152
|
+
});
|
|
153
|
+
it("keeps what fits at that width fitting as the card grows", () => {
|
|
154
|
+
const need = cardNeed(labels, measure);
|
|
155
|
+
for (const k of [1.2, 1.5, 2]) {
|
|
156
|
+
const w = need * k;
|
|
157
|
+
const lines = cardText({ name: "team_0001_managed_attach", type: "iam_role" }, { w, h: w * 0.29, need }, measure);
|
|
158
|
+
expect(lines[0].text).toBe("team_0001_managed_attach");
|
|
159
|
+
for (const l of lines) expect(measure(l.text, l.size)).toBeLessThanOrEqual(w - CARD_TEXT.pad * 2 - CARD_TEXT.bar);
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
describe("fitting the words", () => {
|
|
165
|
+
it("keeps a name that fits", () => {
|
|
166
|
+
expect(fitFront("logs", 100, measure)).toBe("logs");
|
|
167
|
+
});
|
|
168
|
+
it("cuts a name still too long from the front, keeping the longest tail that fits", () => {
|
|
169
|
+
const s = "team_platform_ingest_dead_letter_queue";
|
|
170
|
+
const room = 70;
|
|
171
|
+
const out = fitFront(s, room, measure);
|
|
172
|
+
expect(out.startsWith("…")).toBe(true);
|
|
173
|
+
expect(s.endsWith(out.slice(1))).toBe(true);
|
|
174
|
+
expect(measure(out)).toBeLessThanOrEqual(room);
|
|
175
|
+
expect(measure(`…${s.slice(s.length - out.length)}`)).toBeGreaterThan(room);
|
|
176
|
+
});
|
|
177
|
+
it("draws nothing with no room", () => {
|
|
178
|
+
expect(fitFront("logs", 0, measure)).toBe("");
|
|
179
|
+
expect(fitFront("logs", 3, measure)).toBe("");
|
|
180
|
+
expect(fitFront("", 100, measure)).toBe("");
|
|
181
|
+
});
|
|
182
|
+
it("wraps a type after an underscore, near the middle, when it does not fit one line", () => {
|
|
183
|
+
const m = (s) => measure(s, CARD_TEXT.type);
|
|
184
|
+
const two = wrapType("iam_role_policy_attachment", 100, m);
|
|
185
|
+
expect(two).toEqual(["iam_role_policy_", "attachment"]);
|
|
186
|
+
expect(wrapType("iam_role_policy_attachment", 10, m)).toBeNull();
|
|
187
|
+
expect(wrapType("nounderscore", 100, m)).toBeNull();
|
|
188
|
+
});
|
|
189
|
+
it("puts a too-wide type on two lines on a tall card, and cuts it on a short one", () => {
|
|
190
|
+
const label = { name: "role", type: "iam_role_policy_attachment" };
|
|
191
|
+
const tall = cardText(label, { w: 120, h: 60, need: 120 }, measure);
|
|
192
|
+
expect(tall.map((l) => l.text)).toEqual(["role", "iam_role_policy_", "attachment"]);
|
|
193
|
+
const short = cardText(label, { w: 120, h: 36, need: 120 }, measure);
|
|
194
|
+
expect(short.length).toBe(2);
|
|
195
|
+
expect(short[1].text.startsWith("…")).toBe(true);
|
|
196
|
+
});
|
|
197
|
+
it("leaves a line out rather than shrink it", () => {
|
|
198
|
+
const label = { name: 'logs["eu"]', type: "cloudwatch_log_group" };
|
|
199
|
+
expect(cardText(label, { w: 30, h: 60 }, measure)).toEqual([]);
|
|
200
|
+
expect(cardText(label, { w: 200, h: 10 }, measure)).toEqual([]);
|
|
201
|
+
expect(cardText(label, { w: 200, h: 20 }, measure).map((l) => l.ink)).toEqual(["fg"]);
|
|
202
|
+
expect(cardText({ name: "x", type: "" }, { w: 200, h: 80 }, measure).length).toBe(1);
|
|
203
|
+
});
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
describe("the dot grows into its card", () => {
|
|
207
|
+
const sizes = { dotR: 4, cardW: 120, cardH: 57 };
|
|
208
|
+
it("is the dot at 0 and the card at 1, about the same centre", () => {
|
|
209
|
+
expect(cardShape(50, 40, 0, sizes)).toEqual({ x: 46, y: 36, w: 8, h: 8, r: 4 });
|
|
210
|
+
const c = cardShape(50, 40, 1, sizes);
|
|
211
|
+
expect(c.w).toBe(120);
|
|
212
|
+
expect(c.h).toBe(57);
|
|
213
|
+
expect(c.x + c.w / 2).toBe(50);
|
|
214
|
+
expect(c.y + c.h / 2).toBe(40);
|
|
215
|
+
});
|
|
216
|
+
it("grows monotonically in between", () => {
|
|
217
|
+
let prev = 0;
|
|
218
|
+
for (let u = 0; u <= 1; u += 0.05) {
|
|
219
|
+
const c = cardShape(0, 0, u, sizes);
|
|
220
|
+
expect(c.w).toBeGreaterThanOrEqual(prev);
|
|
221
|
+
prev = c.w;
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
it("fades the words in only near the end of the morph", () => {
|
|
225
|
+
expect(cardTextAlpha(0.6)).toBe(0);
|
|
226
|
+
expect(cardTextAlpha(0.8)).toBeGreaterThan(0);
|
|
227
|
+
expect(cardTextAlpha(1)).toBe(1);
|
|
228
|
+
});
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
describe("pickMark: hover, tap and the arrow keys find the card under the pointer", () => {
|
|
232
|
+
const marks = { px: [10, 100, 300], py: [10, 100, 300], pvis: [1, 1, 1], pw: [0, 40, 0], ph: [0, 20, 0] };
|
|
233
|
+
it("picks a card anywhere on it, a dot only near its centre", () => {
|
|
234
|
+
expect(pickMark(135, 115, marks)).toBe(1);
|
|
235
|
+
expect(pickMark(12, 12, marks)).toBe(0);
|
|
236
|
+
expect(pickMark(30, 30, marks)).toBe(-1);
|
|
237
|
+
expect(pickMark(310, 300, marks, { within: 12 })).toBe(2);
|
|
238
|
+
});
|
|
239
|
+
it("skips a mark that is not drawn", () => {
|
|
240
|
+
expect(pickMark(100, 100, { ...marks, pvis: [1, 0, 1] })).toBe(-1);
|
|
241
|
+
});
|
|
242
|
+
it("prefers the card whose centre is nearer where two meet", () => {
|
|
243
|
+
const two = { px: [0, 60], py: [0, 0], pvis: [1, 1], pw: [40, 40], ph: [20, 20] };
|
|
244
|
+
expect(pickMark(35, 0, two)).toBe(1);
|
|
245
|
+
expect(pickMark(25, 0, two)).toBe(0);
|
|
246
|
+
});
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
describe("the rail's Cards mark", () => {
|
|
250
|
+
it("counts the roster's cards when the canvas draws them", () => {
|
|
251
|
+
expect(cardsMark({ resources: 82, seeded: 0, svgCards: 4, own: true })).toEqual({ count: 82, words: "82 cards", enabled: true });
|
|
252
|
+
});
|
|
253
|
+
it("counts the SVG's resource cards when the SVG draws them", () => {
|
|
254
|
+
expect(cardsMark({ resources: 1041, seeded: 1000, svgCards: 1000, own: false }).words).toBe("1,000 cards");
|
|
255
|
+
});
|
|
256
|
+
it("counts the SVG's own cards (a live carve's roots) when none is a resource's and the stage is the SVG's", () => {
|
|
257
|
+
expect(cardsMark({ resources: 82, seeded: 0, svgCards: 4, own: false })).toEqual({ count: 4, words: "4 cards", enabled: true });
|
|
258
|
+
expect(cardsMark({ resources: 82, seeded: 0, svgCards: 1, own: false }).words).toBe("1 card");
|
|
259
|
+
});
|
|
260
|
+
it("says so, and cannot be pressed, when no card can draw", () => {
|
|
261
|
+
expect(cardsMark({ resources: 82, seeded: 0, svgCards: 0, own: false })).toEqual({ count: 0, words: "no cards", enabled: false });
|
|
262
|
+
});
|
|
263
|
+
it("lands on the director's estate when it holds cards, else the source, else the fullest", () => {
|
|
264
|
+
const counts = new Map([["src", 0], ["team-a", 12], ["team-b", 30]]);
|
|
265
|
+
expect(cardsEstate("team-a", "src", counts)).toBe("team-a");
|
|
266
|
+
expect(cardsEstate("gone", "src", counts)).toBe("team-b");
|
|
267
|
+
expect(cardsEstate(null, "team-a", counts)).toBe("team-a");
|
|
268
|
+
expect(cardsEstate(null, "src", new Map())).toBeNull();
|
|
269
|
+
});
|
|
270
|
+
it("centres on a card, never on the gap between cards", () => {
|
|
271
|
+
const pts = [{ x: 0, y: 0 }, { x: 10, y: 0 }, { x: 0, y: 10 }, { x: 10, y: 10 }, { x: 4, y: 6 }];
|
|
272
|
+
expect(middleCard(pts)).toEqual({ x: 4, y: 6 });
|
|
273
|
+
expect(middleCard([])).toBeNull();
|
|
274
|
+
expect(cardsFrame({ x: 120, y: 210 }, 60, 0.5)).toEqual([90, 195, 60, 30]);
|
|
275
|
+
});
|
|
276
|
+
it("lands where the cards are fully drawn, and the zoom in stops a little past it", () => {
|
|
277
|
+
const paneW = 1300;
|
|
278
|
+
const cardW = 40;
|
|
279
|
+
const need = 180;
|
|
280
|
+
// A card at the Cards mark (web/dots-lod.js railMarks, cardAt = need * 1.1) is fully drawn.
|
|
281
|
+
const markW = (paneW * cardW) / (need * 1.1);
|
|
282
|
+
expect(cardMorph((cardW * paneW) / markW, { lod: rosterLod(need) })).toBe(0);
|
|
283
|
+
// The narrowest view keeps the mark within reach, and stops not far past it.
|
|
284
|
+
const minW = minViewWidth(paneW, cardW, need);
|
|
285
|
+
expect(minW).toBeLessThan(markW);
|
|
286
|
+
expect((cardW * paneW) / minW).toBeCloseTo(need * 1.8, 6);
|
|
287
|
+
});
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
describe("a card's tip", () => {
|
|
291
|
+
const pane = { tipW: 300, tipH: 40, paneW: 1440, paneH: 900 };
|
|
292
|
+
it("sits right of and under the mark", () => {
|
|
293
|
+
expect(tipSpot(100, 100, pane)).toEqual({ left: 112, top: 114 });
|
|
294
|
+
});
|
|
295
|
+
it("keeps clear of the rail on the right", () => {
|
|
296
|
+
const s = tipSpot(1300, 100, { ...pane, rightInset: 160 });
|
|
297
|
+
expect(s.left + pane.tipW).toBeLessThanOrEqual(1440 - 160);
|
|
298
|
+
});
|
|
299
|
+
it("goes over the mark when the pane's foot leaves no room under it", () => {
|
|
300
|
+
const s = tipSpot(100, 880, { ...pane, above: 840 });
|
|
301
|
+
expect(s.top + pane.tipH).toBeLessThanOrEqual(840);
|
|
302
|
+
});
|
|
303
|
+
});
|