@popcorn245/cornfield 0.48.0 → 0.49.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/strings.js +14 -0
- package/explorer/check.js +7 -227
- package/explorer/connection.js +3 -3
- package/explorer/engine/checks.js +252 -0
- package/explorer/engine/connection.js +36 -0
- package/explorer/engine/layers.js +1 -0
- package/explorer/engine/player.js +489 -0
- package/explorer/engine/proposal.js +373 -0
- package/explorer/engine/render.js +1 -0
- package/explorer/engine/source.js +315 -0
- package/explorer/engine/theme-sheet.js +444 -0
- package/explorer/index.html +6 -59
- package/explorer/instance.js +82 -6
- package/explorer/page-address.js +59 -0
- package/explorer/page-pieces.js +33 -5
- package/explorer/page.js +20 -20
- package/explorer/panels.js +1 -3
- package/explorer/pieces/changes.js +502 -0
- package/explorer/pieces/decision.js +657 -0
- package/explorer/pieces/layers.js +4 -0
- package/explorer/pieces/map.js +113 -2
- package/explorer/pieces/node-panel.js +34 -1
- package/explorer/pieces/proposal.js +488 -0
- package/explorer/pieces/scope-link.js +50 -0
- package/explorer/pieces/scope.js +247 -0
- package/explorer/pieces/status.css +24 -0
- package/explorer/pieces/status.js +144 -0
- package/explorer/plugins.js +0 -10
- package/explorer/proposal.js +64 -697
- package/explorer/review-column.js +24 -439
- package/explorer/review.js +27 -673
- package/explorer/scenario-ui.js +32 -478
- package/explorer/sheets.js +8 -7
- package/explorer/view.js +6 -5
- package/explorer/waiting-line.js +7 -50
- package/package.json +1 -1
package/explorer/scenario-ui.js
CHANGED
|
@@ -1,491 +1,45 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @module explorer/scenario-ui
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
* play or pause, next, close), answers the keyboard (space, left and right arrows), and glides the
|
|
8
|
-
* camera to the current step when that is off screen. The ROM simulation plays through here too.
|
|
9
|
-
* The bar also holds the viewer's speed: a small menu of how long a step plays (2 s, 5 s, 10 s,
|
|
10
|
-
* or Manual, which moves only on Next), kept in localStorage and applied to the running player.
|
|
11
|
-
*
|
|
12
|
-
* Frames: the player is a frame client of render.js's loop (`addFrameClient`), not a timer of its
|
|
13
|
-
* own. It asks for frames only while playing or while the camera glides, so a paused or finished
|
|
14
|
-
* scenario costs nothing, a hidden tab stops the loop, and under prefers-reduced-motion the steps
|
|
15
|
-
* still change but the packet does not travel and the camera jumps.
|
|
16
|
-
*
|
|
17
|
-
* The scenario list is the review column's (review-column.js): this module plays what it hands over and tells it where the
|
|
18
|
-
* player is (`onPlayerChange`), so the list can follow.
|
|
19
|
-
*
|
|
20
|
-
* On a phone (sheets.js, TOPO-18) the bar is a bottom sheet in `#sheet-dock`: a peek (title, step
|
|
21
|
-
* counter and text) until expanded, and the camera follows a step into the part of the map the
|
|
22
|
-
* sheets leave free.
|
|
23
|
-
*/
|
|
24
|
-
|
|
25
|
-
import { state, touchScene } from './state.js';
|
|
26
|
-
import { render, ensureFlowLoop, addFrameClient, isNodeVisible, getMinZoom } from './render.js';
|
|
27
|
-
import { flowReduceMotion } from './flows.js';
|
|
28
|
-
import { registerSheet, unregisterSheet, collapseAllSheets, visibleMapRect, onCoveredChange } from './sheets.js';
|
|
29
|
-
import { createPlayer, cameraFor, parseSpeed, SPEED_CHOICES, SPEED_KEY } from './scenario.js';
|
|
30
|
-
import { edgeKey } from '../core/proposal.js';
|
|
31
|
-
import { checksVersion, closeChecks, decorateSnapshot, openChecks, paintChecks } from './check.js';
|
|
32
|
-
import { blastRadius, outageLayers, outageScenario } from './outage-model.js';
|
|
33
|
-
import { setLayer, clearStepLayers, LAYER_DEFS } from './layers.js';
|
|
34
|
-
import { externalLayer } from './cloud-model.js';
|
|
35
|
-
import { layerOn, layersOf, projectLayerKey, shownByProposal } from './layer-model.js';
|
|
36
|
-
import { announce } from './keyboard.js';
|
|
37
|
-
import { t } from './strings.js';
|
|
38
|
-
import { currentStepSentence } from './scenario-list-model.js';
|
|
39
|
-
import { personaFor, personaLabel } from './check-model.js';
|
|
40
|
-
|
|
41
|
-
/** A frame that arrives later than this (a tab that was hidden) counts as this long */
|
|
42
|
-
const MAX_FRAME_MS = 250;
|
|
43
|
-
/** How quickly the camera closes on its target: the time for it to cover about 63% of the way */
|
|
44
|
-
const GLIDE_MS = 140;
|
|
45
|
-
|
|
46
|
-
let player = null;
|
|
47
|
-
let bar = null;
|
|
48
|
-
let signature = '';
|
|
49
|
-
let glide = null; // { to: {x, y, zoom}, last: nowMs|null }
|
|
50
|
-
let held = false; // the viewer took hold of the map since the step began: the camera is theirs until the next step
|
|
51
|
-
let framedFor = -1; // the player bar's height when the camera last followed the step
|
|
52
|
-
let barWatch = null; // a ResizeObserver on the bar: when it grows (a check's line arrives), the step is followed again
|
|
53
|
-
|
|
54
|
-
const $stage = () => (state.canvas ? state.canvas.parentElement : null);
|
|
55
|
-
|
|
56
|
-
// ---------------------------------------------------------------------------
|
|
57
|
-
// Playing
|
|
58
|
-
// ---------------------------------------------------------------------------
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Plays a scenario on the map, replacing any that is playing.
|
|
62
|
-
* @param {import('./scenario.js').Scenario} scenario
|
|
63
|
-
* @param {{ autoplay?: boolean, stepMs?: number | 'manual', step?: number, focus?: boolean }} [opts] `autoplay` false opens it paused on the first step;
|
|
64
|
-
* `step` opens it on that step (paused); `stepMs` is a speed for this scenario in place of the viewer's saved one;
|
|
65
|
-
* `focus` false leaves the keyboard focus where it is (the review column's step list keeps it)
|
|
66
|
-
* @returns {ReturnType<typeof createPlayer>} the player, for a caller that wants to drive it
|
|
67
|
-
*/
|
|
68
|
-
export function playScenario(scenario, opts = {}) {
|
|
69
|
-
const opener = openerOf();
|
|
70
|
-
const takeFocus = isKeyboardish() && opts.focus !== false; // checked now: whatever was pressed may go with the markup
|
|
71
|
-
clearScenario({ keepChecks: true });
|
|
72
|
-
returnTo = opener;
|
|
73
|
-
const speed = opts.stepMs ?? readSpeed();
|
|
74
|
-
player = createPlayer(scenario, { stepMs: speed, maxFrameMs: MAX_FRAME_MS });
|
|
75
|
-
if (Number.isFinite(opts.step) && opts.step > 0) player.goTo(opts.step);
|
|
76
|
-
showBar(speed);
|
|
77
|
-
openChecks(scenario, () => { sync(); render(); }); // PHY-10: one batch of permission checks for the scenario (check.js); the same scenario already open in the list keeps its answers
|
|
78
|
-
showOutageLayers(scenario);
|
|
79
|
-
showStepLayers(scenario);
|
|
80
|
-
if (opts.autoplay !== false && !(opts.step > 0)) player.play();
|
|
81
|
-
sync(true);
|
|
82
|
-
render();
|
|
83
|
-
ensureFlowLoop();
|
|
84
|
-
// Whatever started it may be gone: focus goes to the player's play and pause button, the first control of what opened
|
|
85
|
-
if (takeFocus && bar) {
|
|
86
|
-
const toggle = bar.querySelector('[data-action=scenario-toggle]');
|
|
87
|
-
if (toggle && !toggle.disabled) toggle.focus(); else bar.querySelector('[data-action=scenario-next]')?.focus();
|
|
88
|
-
}
|
|
89
|
-
return player;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
/** What had the focus when a scenario started, to give it back when the scenario closes (a control of another panel, say) */
|
|
93
|
-
let returnTo = null;
|
|
94
|
-
function openerOf() {
|
|
95
|
-
const active = document.activeElement;
|
|
96
|
-
if (active && active !== document.body && !active.closest?.('.hx-player') && !active.closest?.('#review-column') && active !== state.canvas) return active;
|
|
97
|
-
return null;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
/** Whether the focus is on the page's controls (not on nothing, and not in a field): a scenario started by a script from outside takes none */
|
|
101
|
-
function isKeyboardish() {
|
|
102
|
-
const active = document.activeElement;
|
|
103
|
-
return !!active && active !== document.body && !typingIn(active);
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
/** Who knows where the focus goes when the player closes: the review column's step list (review-column.js) gives the step the player was on */
|
|
107
|
-
let closeFocus = null;
|
|
108
|
-
export function setCloseFocus(fn) { closeFocus = typeof fn === 'function' ? fn : null; }
|
|
109
|
-
|
|
110
|
-
/** Who is told when the player moves: `fn(snapshot)` on each change of step, play state or answer, and `fn(null)` when it closes */
|
|
111
|
-
const playerListeners = new Set();
|
|
112
|
-
export function onPlayerChange(fn) {
|
|
113
|
-
playerListeners.add(fn);
|
|
114
|
-
return () => playerListeners.delete(fn);
|
|
115
|
-
}
|
|
116
|
-
const notify = (snap) => { for (const fn of playerListeners) fn(snap); };
|
|
117
|
-
|
|
118
|
-
/** Closing the player by the viewer's own act: the focus goes back to the step it was on in the list, else to what started it, else to the map */
|
|
119
|
-
function giveFocusBack() {
|
|
120
|
-
const to = returnTo;
|
|
121
|
-
returnTo = null;
|
|
122
|
-
const took = (el) => {
|
|
123
|
-
if (!el || !el.isConnected || el.hidden || el.disabled || el.closest('[hidden]')) return false;
|
|
124
|
-
el.focus();
|
|
125
|
-
return document.activeElement === el; // a control the stylesheet hides takes no focus
|
|
126
|
-
};
|
|
127
|
-
if (took(closeFocus && closeFocus()) || took(to)) return;
|
|
128
|
-
took(state.canvas);
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* An outage scenario (spec 0031 section 8) turns on the layers its blast radius is drawn on (containers and
|
|
133
|
-
* machines start off), as a scenario that names a persona turns the People layer on (check.js).
|
|
4
|
+
* A bridge (spec 0011 D2 and D3, step 5): the scenario player is now the instance's (engine/player.js) and part of the changes piece
|
|
5
|
+
* (pieces/changes.js). This file keeps the old names for the modules that are not yet on an instance (whatif.js, view.js, tests), bound to the page's
|
|
6
|
+
* instance and to the piece the page mounts. It goes when the last of those callers is converted.
|
|
134
7
|
*/
|
|
135
|
-
function showOutageLayers(scenario) {
|
|
136
|
-
if (!scenario || !Array.isArray(scenario.down) || !scenario.down.length) return;
|
|
137
|
-
outageLayers(blastRadius(scenario.down, state.nodes, state.edges), state.nodes)
|
|
138
|
-
.forEach((key) => { if (!layerOn(state, key)) setLayer(key, true, { scenario: true }); });
|
|
139
|
-
}
|
|
140
8
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
*/
|
|
145
|
-
export function layersHiding(node) {
|
|
146
|
-
if (!node || state.view === 'dataflow' || state.view === 'house') return [];
|
|
147
|
-
const layers = layersOf(state); // what is drawn: the viewer's layers, or the review's (layer-model.js)
|
|
148
|
-
const on = (key) => layers[key] !== false;
|
|
149
|
-
const t = node.type;
|
|
150
|
-
if (t === 'root' || shownByProposal(state, node)) return []; // a proposal's own nodes are drawn whatever their layer says (spec 0009 D3)
|
|
151
|
-
const project = projectLayerKey(state, node); // a project layer that takes the node decides alone (spec 0009 D3), as scene.js layerShows has it
|
|
152
|
-
if (project) return on(project) ? [] : [project];
|
|
153
|
-
if (t === 'core') return [...(on('core') ? [] : ['core']), ...(layers.pkg || layers.atom ? [] : ['pkg'])];
|
|
154
|
-
if (t === 'store') return ['flow', 'store'].filter((k) => !on(k));
|
|
155
|
-
if (t === 'rom') return ['rom', 'flow'].filter((k) => !on(k));
|
|
156
|
-
if (t === 'floor' || t === 'room') return on('house') ? [] : ['house'];
|
|
157
|
-
const key = t === 'external' ? externalLayer(node) : t;
|
|
158
|
-
return on(key) ? [] : [key];
|
|
159
|
-
}
|
|
9
|
+
import { pageInstance } from './state.js';
|
|
10
|
+
import { pagePiece } from './page-pieces.js';
|
|
11
|
+
import { demoScenario as demo } from './engine/player.js';
|
|
160
12
|
|
|
161
|
-
|
|
162
|
-
const stepNodeIds = (scenario) => (Array.isArray(scenario?.steps) ? scenario.steps : []).flatMap((st) => (st?.edge ? [st.edge.from, st.edge.to] : st && st.node !== undefined ? [st.node] : []));
|
|
13
|
+
export const demoScenario = (nodes = pageInstance().state.nodes, edges = pageInstance().state.edges) => demo(nodes, edges);
|
|
163
14
|
|
|
164
|
-
|
|
165
|
-
* A step on a layer that is off cannot be followed: the camera has nothing to go to (#212). Playing a scenario turns on the layers
|
|
166
|
-
* its steps are drawn on, as an outage turns on what it hits, and says which.
|
|
167
|
-
*/
|
|
168
|
-
function showStepLayers(scenario) {
|
|
169
|
-
const byId = new Map(state.nodes.map((n) => [n.id, n]));
|
|
170
|
-
const keys = [];
|
|
171
|
-
for (const id of stepNodeIds(scenario)) for (const key of layersHiding(byId.get(id))) if (!keys.includes(key)) keys.push(key);
|
|
172
|
-
if (!keys.length) return;
|
|
173
|
-
for (const key of keys) setLayer(key, true, { scenario: true }); // on, for the review too (state.review.step): stopping the scenario takes it off again
|
|
174
|
-
const names = keys.map((key) => (LAYER_DEFS.find((l) => l.key === key) || (state.projectLayers || []).find((l) => l.key === key) || { label: key }).label);
|
|
175
|
-
announce(`Turned on ${names.length > 1 ? `${names.slice(0, -1).join(', ')} and ${names[names.length - 1]}` : names[0]} to show this scenario's steps.`);
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
/** The cloud panel's Play button (CLOUD-07): plays "X is down" for a cloud node, generated from the map (outage-model.js) */
|
|
179
|
-
export function playOutage(id) {
|
|
180
|
-
const node = state.nodes.find((n) => n.id === id);
|
|
181
|
-
if (!node || node.type !== 'external' || node.kind !== 'cloud') return null;
|
|
182
|
-
return playScenario(outageScenario(node, state.nodes, state.edges));
|
|
183
|
-
}
|
|
15
|
+
const piece = () => pagePiece('changes', { anywhere: true });
|
|
184
16
|
|
|
17
|
+
/** Plays a scenario on the map, replacing any that is playing; returns the player */
|
|
18
|
+
export function playScenario(scenario, opts) { const p = piece(); return p ? p.playScenario(scenario, opts) : null; }
|
|
19
|
+
/** The cloud panel's Play button: plays "X is down" for a cloud node */
|
|
20
|
+
export function playOutage(id) { const p = piece(); return p ? p.playOutage(id) : null; }
|
|
185
21
|
/** The player of the scenario on the map, or null */
|
|
186
|
-
export const getPlayer = () =>
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
export function clearScenario({ keepChecks = false, closing = false } = {}) {
|
|
190
|
-
const had = !!player || !!state.scenario;
|
|
191
|
-
if (had) clearStepLayers(); // the layers this scenario turned on for a review go off with it: the focused view comes back
|
|
192
|
-
player = null;
|
|
193
|
-
glide = null;
|
|
194
|
-
signature = '';
|
|
195
|
-
state.scenario = null;
|
|
196
|
-
if (!keepChecks) closeChecks();
|
|
197
|
-
if (barWatch) { barWatch.disconnect(); barWatch = null; }
|
|
198
|
-
if (bar) { unregisterSheet('player'); bar.remove(); bar = null; }
|
|
199
|
-
if (had && (!keepChecks || closing)) notify(null);
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
/** Who keeps a closed scenario's checks: the review column, while its steps are open in the list (their verdicts stay there) */
|
|
203
|
-
let keepChecksOnClose = null;
|
|
204
|
-
export function setKeepChecksOnClose(fn) { keepChecksOnClose = typeof fn === 'function' ? fn : null; }
|
|
205
|
-
|
|
22
|
+
export const getPlayer = () => { const p = pagePiece('changes'); return p ? p.getPlayer() : null; };
|
|
23
|
+
/** Ends the scenario without touching the picture */
|
|
24
|
+
export function clearScenario(opts) { const p = pagePiece('changes'); if (p) p.clearScenario(opts); else pageInstance().state.scenario = null; }
|
|
206
25
|
/** Ends the scenario and redraws the map without it */
|
|
207
|
-
export function stopScenario() {
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
/** Brings `state.scenario` up to date with the player; when a step, the play state or the step start moved, rebuilds the scene and follows the step with the camera */
|
|
219
|
-
function sync(force = false) {
|
|
220
|
-
if (!player) return false;
|
|
221
|
-
const snap = decorateSnapshot(player.snapshot(), player.scenario());
|
|
222
|
-
state.scenario = snap;
|
|
223
|
-
const next = `${snap.index}|${snap.playing}|${snap.done}|${snap.stepStartMs}|${snap.stepMs}|${snap.manual}|${checksVersion()}`;
|
|
224
|
-
const changed = force || next !== signature;
|
|
225
|
-
if (changed) {
|
|
226
|
-
const stepChanged = force || next.split('|')[0] !== signature.split('|')[0];
|
|
227
|
-
signature = next;
|
|
228
|
-
touchScene();
|
|
229
|
-
if (stepChanged) { held = false; followStep(snap); }
|
|
230
|
-
}
|
|
231
|
-
updateBar(snap);
|
|
232
|
-
if (changed) notify({ id: snap.id, index: snap.index, total: snap.total, done: snap.done, playing: snap.playing, checks: checksVersion() });
|
|
233
|
-
return changed;
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
// ---------------------------------------------------------------------------
|
|
237
|
-
// The camera follows the step
|
|
238
|
-
// ---------------------------------------------------------------------------
|
|
239
|
-
function followStep(snap) {
|
|
240
|
-
const visible = new Map();
|
|
241
|
-
state.nodes.forEach((n) => { if (isNodeVisible(n)) visible.set(n.id, n); });
|
|
242
|
-
// The step must be in the part of the map nothing covers: not under the proposal bar, the node panel, the player or (on a phone) the sheets
|
|
243
|
-
const rect = visibleMapRect();
|
|
244
|
-
framedFor = bar ? bar.offsetHeight : 0;
|
|
245
|
-
const target = cameraFor({ node: snap.current.node, edge: snap.current.edge, at: snap.current.at, text: snap.current.text }, state.camera, rect, visible, 60, getMinZoom());
|
|
246
|
-
if (!target) return;
|
|
247
|
-
if (flowReduceMotion) { Object.assign(state.camera, target); glide = null; render(); return; } // reduced motion: jump, do not fly
|
|
248
|
-
glide = { to: target, last: null };
|
|
249
|
-
ensureFlowLoop();
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
function stepGlide(now) {
|
|
253
|
-
if (!glide) return;
|
|
254
|
-
const dt = glide.last === null ? 16 : Math.min(now - glide.last, 100);
|
|
255
|
-
glide.last = now;
|
|
256
|
-
const k = 1 - Math.exp(-dt / GLIDE_MS);
|
|
257
|
-
const cam = state.camera;
|
|
258
|
-
cam.x += (glide.to.x - cam.x) * k;
|
|
259
|
-
cam.y += (glide.to.y - cam.y) * k;
|
|
260
|
-
cam.zoom += (glide.to.zoom - cam.zoom) * k;
|
|
261
|
-
if (Math.abs(glide.to.x - cam.x) < 0.5 && Math.abs(glide.to.y - cam.y) < 0.5 && Math.abs(glide.to.zoom - cam.zoom) < 0.002) {
|
|
262
|
-
Object.assign(cam, glide.to);
|
|
263
|
-
glide = null;
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
/** Whether the player needs frames: while it plays or the camera glides; never when paused, done or closed */
|
|
268
|
-
export const wantsFrames = () => !!player && (player.isPlaying() || !!glide);
|
|
269
|
-
|
|
270
|
-
/** What the frame loop calls every frame with its clock (exported so a test can be the loop) */
|
|
271
|
-
export function tickScenario(now) {
|
|
272
|
-
if (!player) return;
|
|
273
|
-
player.tick(now);
|
|
274
|
-
sync();
|
|
275
|
-
stepGlide(now);
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
addFrameClient({ wants: wantsFrames, tick: tickScenario });
|
|
279
|
-
|
|
280
|
-
// ---------------------------------------------------------------------------
|
|
281
|
-
// The bar
|
|
282
|
-
// ---------------------------------------------------------------------------
|
|
283
|
-
function showBar(speed) {
|
|
284
|
-
const stage = $stage();
|
|
285
|
-
if (!stage) return;
|
|
286
|
-
bar = document.createElement('div');
|
|
287
|
-
bar.className = 'hx-player glass';
|
|
288
|
-
bar.dataset.corn = 'scenario-player';
|
|
289
|
-
bar.setAttribute('role', 'region');
|
|
290
|
-
bar.setAttribute('aria-label', 'Scenario player');
|
|
291
|
-
// One bar (spec 0006 D6): Previous, Pause or Play, Next; "2 of 4 · As Adult · the step's text · Allowed"; the speed and Close.
|
|
292
|
-
// The scenario's name is the title (heard, and shown on a phone's open sheet); the list beside the map has it open and the step marked.
|
|
293
|
-
bar.innerHTML = `
|
|
294
|
-
<span class="hx-player-title hx-sr-only"></span>
|
|
295
|
-
<div class="hx-player-controls" role="group">
|
|
296
|
-
<button data-corn-kind="secondary" type="button" class="hx-btn hx-player-prev" data-action="scenario-prev" data-peek-ok><span class="hx-player-label"></span></button>
|
|
297
|
-
<button type="button" class="hx-btn hx-btn--active hx-player-toggle" data-action="scenario-toggle" data-peek-ok data-corn-kind="primary"><span class="hx-player-label"></span></button>
|
|
298
|
-
<button data-corn-kind="secondary" type="button" class="hx-btn hx-player-next" data-action="scenario-next" data-peek-ok><span class="hx-player-label"></span></button>
|
|
299
|
-
<button data-corn-kind="secondary" type="button" class="hx-btn hx-player-next-scenario" data-action="scenario-next-scenario" data-peek-ok hidden></button>
|
|
300
|
-
</div>
|
|
301
|
-
<p class="hx-player-text"><span class="hx-player-count" data-corn="count"></span><span class="hx-player-sep" aria-hidden="true"> · </span><span class="hx-player-who"></span><span class="hx-player-step"></span></p>
|
|
302
|
-
<span class="hx-player-says hx-sr-only" role="status" aria-live="polite"></span>
|
|
303
|
-
<div class="hx-player-side">
|
|
304
|
-
<select data-corn="control" class="hx-player-speed" data-action="scenario-speed" aria-label="Time for each step" title="Time for each step">${SPEED_CHOICES.map((c) => `<option value="${c.value}">${c.label}</option>`).join('')}</select>
|
|
305
|
-
<button type="button" class="hx-close" data-action="scenario-close" aria-label="Close the scenario" data-corn-kind="icon">✕</button>
|
|
306
|
-
</div>
|
|
307
|
-
<div class="hx-player-track" data-corn="scenario-track" aria-hidden="true"><span class="hx-player-fill" data-corn="scenario-fill"></span></div>`;
|
|
308
|
-
bar.querySelector('.hx-player-controls').setAttribute('aria-label', t('steps'));
|
|
309
|
-
bar.querySelector('.hx-player-prev .hx-player-label').textContent = t('previousStep');
|
|
310
|
-
bar.querySelector('.hx-player-next .hx-player-label').textContent = t('nextStep');
|
|
311
|
-
bar.querySelector('.hx-player-next-scenario').textContent = t('nextScenario');
|
|
312
|
-
bar.addEventListener('click', (e) => {
|
|
313
|
-
const button = e.target.closest('[data-action]');
|
|
314
|
-
if (button) act(button.dataset.action);
|
|
315
|
-
});
|
|
316
|
-
const menu = bar.querySelector('.hx-player-speed');
|
|
317
|
-
menu.value = String(speed);
|
|
318
|
-
menu.addEventListener('change', (e) => chooseSpeed(e.target.value));
|
|
319
|
-
// On a phone the bar is a sheet in the dock, above the node panel; it opens as a peek (one row) and the others fold, so the map shows
|
|
320
|
-
const dock = document.getElementById('sheet-dock');
|
|
321
|
-
const drawer = document.getElementById('spec-drawer');
|
|
322
|
-
if (dock && drawer && drawer.parentElement === dock) dock.insertBefore(bar, drawer);
|
|
323
|
-
else (dock || stage).appendChild(bar);
|
|
324
|
-
registerSheet('player', bar);
|
|
325
|
-
collapseAllSheets();
|
|
326
|
-
// The bar grows once it has more to say (a step's check, its As row): the step it framed would now be under it, so it is followed again
|
|
327
|
-
if (typeof ResizeObserver === 'function') {
|
|
328
|
-
barWatch = new ResizeObserver(() => { if (bar && player && state.scenario && !held && bar.offsetHeight !== framedFor) followStep(player.snapshot()); });
|
|
329
|
-
barWatch.observe(bar);
|
|
330
|
-
}
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
/** Who knows the scenario after this one, in the order the list reads (review-column.js): `fn(scenarioId)` gives the scenario or null */
|
|
334
|
-
let nextScenarioOf = null;
|
|
335
|
-
export function setNextScenario(fn) { nextScenarioOf = typeof fn === 'function' ? fn : null; }
|
|
336
|
-
|
|
337
|
-
// The viewer's speed: a setting of theirs, kept for the next scenario and the next visit
|
|
338
|
-
function readSpeed() {
|
|
339
|
-
try { return parseSpeed(window.localStorage.getItem(SPEED_KEY)); } catch { return parseSpeed(null); }
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
function chooseSpeed(value) {
|
|
343
|
-
const speed = parseSpeed(value);
|
|
344
|
-
try { window.localStorage.setItem(SPEED_KEY, String(speed)); } catch { /* storage blocked: it still applies to this player */ }
|
|
345
|
-
if (!player) return;
|
|
346
|
-
player.setSpeed(speed);
|
|
347
|
-
sync();
|
|
348
|
-
render();
|
|
349
|
-
ensureFlowLoop();
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
/** One place for what the bar's buttons and the keys do */
|
|
353
|
-
function act(action) {
|
|
354
|
-
if (!player) return;
|
|
355
|
-
if (action === 'scenario-close') return stopScenario();
|
|
356
|
-
if (action === 'scenario-next-scenario') {
|
|
357
|
-
// the end of a scenario: nothing advances by itself; this is the reviewer's choice, and it starts the next one in the list's order
|
|
358
|
-
const next = nextScenarioOf ? nextScenarioOf(player.scenario().id) : null;
|
|
359
|
-
if (next) playScenario(next);
|
|
360
|
-
return;
|
|
361
|
-
}
|
|
362
|
-
if (action === 'scenario-toggle') {
|
|
363
|
-
const s = player.snapshot();
|
|
364
|
-
if (s.playing) player.pause(); else player.play();
|
|
365
|
-
announce(s.playing ? 'Paused' : s.done ? 'Playing again' : s.index > 0 || s.progress > 0 ? 'Resumed' : 'Playing');
|
|
366
|
-
} else if (action === 'scenario-prev') player.prev();
|
|
367
|
-
else if (action === 'scenario-next') player.next();
|
|
368
|
-
else return;
|
|
369
|
-
sync();
|
|
370
|
-
render();
|
|
371
|
-
ensureFlowLoop();
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
let lastFill = -1;
|
|
375
|
-
function updateBar(snap) {
|
|
376
|
-
if (!bar) return;
|
|
377
|
-
const q = (cls) => bar.querySelector(cls);
|
|
378
|
-
// this runs every frame: a live region is written only when its words change, so a step is said once, not once a frame
|
|
379
|
-
const put = (cls, text) => { const el = q(cls); if (el.textContent !== text) el.textContent = text; };
|
|
380
|
-
put('.hx-player-title', snap.title);
|
|
381
|
-
bar.setAttribute('aria-label', `Scenario player: ${snap.title}`);
|
|
382
|
-
put('.hx-player-count', `${Math.min(snap.index + 1, Math.max(snap.total, 1))} of ${snap.total}`);
|
|
383
|
-
const actor = snap.current.actor ? personaLabel(personaFor(state.nodes, snap.current.actor)) || snap.current.actor : '';
|
|
384
|
-
put('.hx-player-who', actor && !snap.done ? `As ${actor} · ` : '');
|
|
385
|
-
put('.hx-player-step', snap.done ? 'Played to the end' : snap.current.text);
|
|
386
|
-
bar.dataset.done = String(!!snap.done);
|
|
387
|
-
const toggle = q('[data-action=scenario-toggle]');
|
|
388
|
-
const label = snap.playing ? t('pause') : snap.done ? t('playAgain') : t('play');
|
|
389
|
-
put('[data-action=scenario-toggle] .hx-player-label', label);
|
|
390
|
-
toggle.dataset.state = snap.playing ? 'playing' : snap.done ? 'done' : 'paused';
|
|
391
|
-
toggle.disabled = snap.total === 0 || snap.manual;
|
|
392
|
-
toggle.title = snap.manual ? 'The time for each step is Manual: use Next' : '';
|
|
393
|
-
q('[data-action=scenario-prev]').disabled = snap.index === 0 && !snap.done;
|
|
394
|
-
const next = q('[data-action=scenario-next]');
|
|
395
|
-
next.disabled = snap.done || snap.total === 0;
|
|
396
|
-
// at the end: Play again and Next scenario (the next one in the list's order); nothing carries on by itself
|
|
397
|
-
const following = snap.done && nextScenarioOf ? nextScenarioOf(snap.id) : null;
|
|
398
|
-
q('[data-action=scenario-next-scenario]').hidden = !following;
|
|
399
|
-
next.hidden = !!following;
|
|
400
|
-
// the one polite line: a step as the list says it ("Step 2 of 3, as Adult: checkout page, Added. You pay. Allowed.")
|
|
401
|
-
put('.hx-player-says', snap.done ? `${snap.title}: played to the end.` : currentStepSentence({ snap, nodes: state.nodes, edges: state.edges, words: { notChecked: t('notChecked') } }));
|
|
402
|
-
const fill = snap.total ? Math.round(((snap.done ? snap.total : snap.index + snap.progress) / snap.total) * 1000) / 10 : 0;
|
|
403
|
-
if (fill !== lastFill) { lastFill = fill; q('.hx-player-fill').style.width = `${fill}%`; }
|
|
404
|
-
paintChecks(bar, snap);
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
// ---------------------------------------------------------------------------
|
|
408
|
-
// Keyboard: space plays and pauses, left and right step; never while typing
|
|
409
|
-
// ---------------------------------------------------------------------------
|
|
410
|
-
const TYPING = 'input, textarea, select, [contenteditable=""], [contenteditable="true"]';
|
|
411
|
-
|
|
412
|
-
function typingIn(target) {
|
|
413
|
-
return !!(target && target.closest && target.closest(TYPING));
|
|
414
|
-
}
|
|
415
|
-
|
|
416
|
-
function onKeydown(e) {
|
|
417
|
-
if (e.key === 'Escape' && !e.defaultPrevented && !e.ctrlKey && !e.metaKey && !e.altKey) {
|
|
418
|
-
if (player && !typingIn(e.target) && !otherPopupOpen()) { e.preventDefault(); stopScenario(); return; } // Escape closes the scenario; focus goes back to Play
|
|
419
|
-
}
|
|
420
|
-
if (!player || e.defaultPrevented || e.ctrlKey || e.metaKey || e.altKey || typingIn(e.target)) return;
|
|
421
|
-
if ((e.key === ' ' || e.code === 'Space') && e.target.closest?.('#review-column')) return; // Space on a step or a row presses it
|
|
422
|
-
const action = e.key === ' ' || e.code === 'Space' ? 'scenario-toggle' : e.key === 'ArrowLeft' ? 'scenario-prev' : e.key === 'ArrowRight' ? 'scenario-next' : null;
|
|
423
|
-
if (!action) return;
|
|
424
|
-
e.preventDefault(); // space on a focused button would press it as well
|
|
425
|
-
act(action);
|
|
426
|
-
}
|
|
427
|
-
|
|
428
|
-
/** A menu or dialog that has its own Escape: the layers menu, the toolbar's ⋯ menu, a dialog */
|
|
429
|
-
function otherPopupOpen() {
|
|
430
|
-
return !!document.querySelector('#legend-dropdown:not([hidden]), #proposal-more-menu:not([hidden]), [data-corn="dialog-scrim"]:not([hidden]), .hx-mockup-dialog');
|
|
431
|
-
}
|
|
432
|
-
|
|
433
|
-
/** Some browsers press a focused button on the key's release, so a space the player took must not reach it */
|
|
434
|
-
function onKeyup(e) {
|
|
435
|
-
if (player && (e.key === ' ' || e.code === 'Space') && !typingIn(e.target)) e.preventDefault();
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
// ---------------------------------------------------------------------------
|
|
439
|
-
// /?scenario-demo=1: a small scenario over real nodes, for looking at the player before a proposal exists
|
|
440
|
-
// ---------------------------------------------------------------------------
|
|
441
|
-
/**
|
|
442
|
-
* The demo scenario, walked over what the loaded topology really has: from the node with the most links that
|
|
443
|
-
* are not ownership, along such links for up to three hops, never back to a node it has been at. Fewer than
|
|
444
|
-
* two steps (a map with no links) means no demo (null).
|
|
445
|
-
*/
|
|
446
|
-
export function demoScenario(nodes = state.nodes, edges = state.edges) {
|
|
447
|
-
const ids = new Set(nodes.map((n) => n.id));
|
|
448
|
-
const label = (id) => (nodes.find((n) => n.id === id) || { label: id }).label;
|
|
449
|
-
const links = edges.filter((e) => (e.kind || e.dashed) && ids.has(e.from) && ids.has(e.to) && e.from !== e.to);
|
|
450
|
-
if (!links.length) return null;
|
|
451
|
-
const out = new Map();
|
|
452
|
-
for (const e of links) { if (!out.has(e.from)) out.set(e.from, []); out.get(e.from).push(e); }
|
|
453
|
-
const start = [...out.keys()].sort((x, y) => out.get(y).length - out.get(x).length || x.localeCompare(y))[0];
|
|
454
|
-
const steps = [{ node: start, text: `It starts at ${label(start)}.` }];
|
|
455
|
-
const seen = new Set([start]);
|
|
456
|
-
let at = start;
|
|
457
|
-
for (let hop = 0; hop < 3; hop++) {
|
|
458
|
-
const next = (out.get(at) || []).find((e) => !seen.has(e.to));
|
|
459
|
-
if (!next) break;
|
|
460
|
-
const edge = { from: next.from, to: next.to, ...(next.kind ? { kind: next.kind } : {}), ...(next.kind === 'flow' ? { flow: next.flow } : {}) };
|
|
461
|
-
steps.push({ edge, text: `${label(next.from)} ${next.label || next.kind || 'uses'} ${label(next.to)}.` });
|
|
462
|
-
steps.push({ node: next.to, text: `Now at ${label(next.to)}.` });
|
|
463
|
-
seen.add(next.to);
|
|
464
|
-
at = next.to;
|
|
465
|
-
}
|
|
466
|
-
return steps.length < 2 ? null : { id: 'demo', title: 'A walk over the map', steps };
|
|
467
|
-
}
|
|
468
|
-
|
|
26
|
+
export function stopScenario() { const p = pagePiece('changes'); if (p) p.stopScenario(); }
|
|
27
|
+
/** What the frame loop calls every frame with its clock (a test can be the loop) */
|
|
28
|
+
export function tickScenario(now) { const p = pagePiece('changes'); if (p) p.tickScenario(now); }
|
|
29
|
+
export const wantsFrames = () => { const p = pagePiece('changes'); return !!p && p.wantsFrames(); };
|
|
30
|
+
/** The Layers keys that hide a node, or [] when no layer does */
|
|
31
|
+
export function layersHiding(node) { const p = piece(); return p ? p.layersHiding(node) : []; }
|
|
32
|
+
/** Calls `fn(snapshot)` on each change of the player (null when it closes). Returns how to stop. */
|
|
33
|
+
export const onPlayerChange = (fn) => pageInstance().on('scenario:player', fn);
|
|
34
|
+
/** The player is ready where the page has its place (page.js mounts the changes piece; a second call does nothing) */
|
|
35
|
+
export function initScenarioUi() { piece(); }
|
|
469
36
|
/** Starts the demo when the page was opened with `?scenario-demo=1` (call once the topology is loaded) */
|
|
470
|
-
export function maybeStartDemo(search =
|
|
37
|
+
export function maybeStartDemo(search = typeof location === 'undefined' ? '' : location.search) {
|
|
471
38
|
if (new URLSearchParams(search).get('scenario-demo') !== '1') return false;
|
|
472
|
-
const
|
|
473
|
-
|
|
474
|
-
playScenario(scenario);
|
|
475
|
-
return true;
|
|
476
|
-
}
|
|
477
|
-
|
|
478
|
-
// ---------------------------------------------------------------------------
|
|
479
|
-
// Wiring (main.js calls this once)
|
|
480
|
-
// ---------------------------------------------------------------------------
|
|
481
|
-
let wired = false;
|
|
482
|
-
export function initScenarioUi() {
|
|
483
|
-
if (wired) return;
|
|
484
|
-
wired = true;
|
|
485
|
-
document.addEventListener('keydown', onKeydown);
|
|
486
|
-
document.addEventListener('keyup', onKeyup);
|
|
487
|
-
// A sheet that opened or closed changes what the map shows: keep the step in view
|
|
488
|
-
onCoveredChange(() => { if (player && state.scenario) followStep(player.snapshot()); });
|
|
489
|
-
// Taking hold of the map (a drag, the wheel, a pinch) ends the camera's glide: the camera is theirs
|
|
490
|
-
for (const type of ['pointerdown', 'wheel', 'touchstart', 'mousedown']) document.addEventListener(type, (e) => { if (e.target === state.canvas) { glide = null; held = true; } }, { passive: true, capture: true });
|
|
39
|
+
const p = piece();
|
|
40
|
+
return p ? p.demo(true) : false;
|
|
491
41
|
}
|
|
42
|
+
/** Who knows where the focus goes when the player closes, who knows the scenario after this one, who keeps a closed scenario's checks (the changes piece sets these itself; a test sets its own) */
|
|
43
|
+
export function setCloseFocus(fn) { const p = piece(); if (p) p.setCloseFocus(fn); }
|
|
44
|
+
export function setNextScenario(fn) { const p = piece(); if (p) p.setNextScenario(fn); }
|
|
45
|
+
export function setKeepChecksOnClose(fn) { const p = piece(); if (p) p.setKeepChecksOnClose(fn); }
|
package/explorer/sheets.js
CHANGED
|
@@ -2,13 +2,12 @@
|
|
|
2
2
|
* @module explorer/sheets
|
|
3
3
|
*
|
|
4
4
|
* A bridge (spec 0011 D2, step 4): the phone's bottom sheets now live in engine/sheets.js, one set per instance (the node panel registers its
|
|
5
|
-
* own, pieces/node-panel.js). This file keeps the old names for the modules that are not yet on an
|
|
6
|
-
*
|
|
7
|
-
* which is not a piece yet. It goes when the last of them is converted.
|
|
5
|
+
* own, pieces/node-panel.js, and so does the proposal's bar, pieces/proposal.js). This file keeps the old names for the modules that are not yet on an
|
|
6
|
+
* instance (the Work panel, deep links, the House), bound to the page's instance. It goes when the last of them is converted.
|
|
8
7
|
*/
|
|
9
8
|
|
|
10
9
|
import { pageInstance } from './state.js';
|
|
11
|
-
import { ensurePagePieces } from './page-pieces.js';
|
|
10
|
+
import { ensurePagePieces, pagePiece } from './page-pieces.js';
|
|
12
11
|
import * as S from './engine/sheets.js';
|
|
13
12
|
|
|
14
13
|
export { PHONE_QUERY } from './engine/sheets.js';
|
|
@@ -29,10 +28,12 @@ export const collapseSheet = (id) => S.collapseSheet(page(), id);
|
|
|
29
28
|
export const collapseAllSheets = () => S.collapseAllSheets(page());
|
|
30
29
|
export const toggleSheet = (id) => S.toggleSheet(page(), id);
|
|
31
30
|
|
|
32
|
-
/** Watches the page's dock and
|
|
31
|
+
/** Watches the page's dock (the node panel and the proposal's bar register their sheets as they mount) */
|
|
33
32
|
export function initSheets() {
|
|
34
33
|
const inst = page();
|
|
35
34
|
S.initSheets(inst);
|
|
36
|
-
ensurePagePieces(); // the node panel's
|
|
37
|
-
|
|
35
|
+
ensurePagePieces(); // the node panel's and the proposal's sheets, if the page has their places
|
|
36
|
+
// A bar the proposal piece did not make (a test's own markup) is a sheet all the same
|
|
37
|
+
const bar = inst.el('proposal-bar');
|
|
38
|
+
if (bar && !pagePiece('proposal')) S.registerSheet(inst, 'proposal', bar, { stub: () => ({ title: (inst.el('proposal-title') && inst.el('proposal-title').textContent) || 'Proposal' }) });
|
|
38
39
|
}
|
package/explorer/view.js
CHANGED
|
@@ -17,6 +17,7 @@ import { mergeLive, placeLiveNodes } from './live-model.js';
|
|
|
17
17
|
import { applyHouseLayout } from './house-model.js';
|
|
18
18
|
import { syncHint } from './hint.js';
|
|
19
19
|
import { ensurePagePieces } from './page-pieces.js';
|
|
20
|
+
import { TOPOLOGY_PATH, fetchTopology } from './engine/source.js';
|
|
20
21
|
|
|
21
22
|
const $ = (id) => document.getElementById(id);
|
|
22
23
|
|
|
@@ -35,11 +36,8 @@ export async function loadTopology() {
|
|
|
35
36
|
try {
|
|
36
37
|
// /?fixture=5k measures on the generated topology (bench.js, core/fixture.js)
|
|
37
38
|
const { fixtureUrl } = measureParams(window.location.search);
|
|
38
|
-
const url = fixtureUrl ||
|
|
39
|
-
const
|
|
40
|
-
if (!res.ok) throw new Error(`GET ${url} answered ${res.status} ${res.statusText}`.trim());
|
|
41
|
-
const data = await res.json();
|
|
42
|
-
if (!Array.isArray(data.nodes) || data.nodes.length === 0) throw new Error(`${url} has no nodes`);
|
|
39
|
+
const url = fixtureUrl || TOPOLOGY_PATH;
|
|
40
|
+
const data = await fetchTopology(pageInstance(), url); // engine/source.js: the same read a scope makes, here of the page's own origin
|
|
43
41
|
// The base stays as the file holds it; the map works on copies (the layout writes positions on them)
|
|
44
42
|
state.baseNodes = data.nodes;
|
|
45
43
|
state.baseEdges = Array.isArray(data.edges) ? data.edges : [];
|
|
@@ -167,3 +165,6 @@ export function syncViewUi() {
|
|
|
167
165
|
pageInstance().emit('view', state.view); // the Layers chip dims while the data-flow view is on (pieces/layers.js)
|
|
168
166
|
syncHint(); // the hint's words for this view and pointer, and whether it shows (hint.js)
|
|
169
167
|
}
|
|
168
|
+
|
|
169
|
+
// The proposal asks the page to lay the map out again from the nodes and edges it hands over (engine/proposal.js): this module is not on an instance yet
|
|
170
|
+
pageInstance().on('map:set', ({ nodes, edges }) => { setMap(nodes, edges); return true; });
|