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 CHANGED
@@ -104,6 +104,20 @@ export const STRINGS = Object.freeze({
104
104
  loading: 'Loading topology.json …',
105
105
  errorTitle: 'The map could not be loaded',
106
106
  retry: 'Retry',
107
+ // A map that loads from a cornfield server (a scope, or a map with a src) while it loads and when it cannot
108
+ sourceLoading: 'Loading the map …',
109
+ sourceSignedOut: 'You are not signed in to cornfield',
110
+ sourceSignInText: "This map is behind cornfield's sign-in.",
111
+ sourceNotMaintainer: "This map is for the project's maintainers",
112
+ sourceNotMaintainerText: 'You are signed in to cornfield, but not as a maintainer.',
113
+ sourceSignIn: 'Sign in to cornfield',
114
+ sourceNotListed: 'This page is not listed by that cornfield',
115
+ sourceNotListedHint: 'Add this origin to embed.origins on the server:',
116
+ sourceOffline: 'The map cannot be reached',
117
+ sourceOfflineText: 'Check the connection, then try again.',
118
+ sourceBadAddress: 'That is not the address of a cornfield server',
119
+ sourceVersion: 'This map and its server are different versions',
120
+ sourceSecondMap: 'This scope already has a map',
107
121
  });
108
122
 
109
123
  /** The longest a value may be, in characters */
package/explorer/check.js CHANGED
@@ -1,233 +1,13 @@
1
1
  /**
2
2
  * @module explorer/check
3
3
  *
4
- * Checked scenarios in the page (spec 0029 sections 3 and 5, PHY-10; the pure half is
5
- * check-model.js). When a scenario opens, every check of every step goes to the checker (a plugin's
6
- * adapter, handed over by main.js) in ONE batch; each step then shows its state on the player bar,
7
- * and on the phone's sheet: a pill in the bar's head ("✕ Refused", seen on the peek too) and a line
8
- * under the step's text ("Refused: Only an adult can change the household's rooms (ADULT_ONLY_WRITES)").
9
- * The current step's node or edge is drawn red, green or amber (scene.js calls `paintVerdict` with the
10
- * verdict this module puts on the player's snapshot). The **As** menu (the personas on the map, what-if
11
- * ones too) acts every step as one persona and asks again; **Check again** asks again as it is.
12
- *
13
- * scenario-ui.js calls it at four points, and nothing else needs to:
14
- *
15
- * openChecks(scenario, onChange) a scenario opened: ask its batch
16
- * decorateSnapshot(snap, scenario) every sync: puts `verdict` (and the As actor) on the snapshot;
17
- * asks again when what is asked changed (a what-if edit, the As menu)
18
- * paintChecks(bar, snap) every bar update (the controls go on the bar at its first paint)
19
- * closeChecks() the scenario closed: cancel what is in flight
20
- *
21
- * A scenario that names an actor turns the People layer on, so its personas are on the map.
22
- * Nothing is stored: the answers live in memory for the scenario on the map, and go with it.
4
+ * A bridge (spec 0011 D2 and D3, step 5): the checked scenarios are now the instance's (engine/checks.js), part of the changes piece
5
+ * (pieces/changes.js), and the checker is the piece's option. This file keeps the old names for page.js and tests, bound to the page's instance.
23
6
  */
24
7
 
25
- import { state, escHtml } from './state.js';
26
- import { setLayer } from './layers.js';
27
- import { layersOf } from './layer-model.js';
28
- import { mapIndex, personaLabel, planChecks, runChecks, stepVerdict, verdictWords } from './check-model.js';
29
- import { cornState } from './corn-state.js';
30
-
31
- let checker = null;
32
- let current = null; // { scenario, as, nodes, plan, answer, controller, onChange }
33
- let version = 0;
34
-
35
- /** Hands the tool its checker (main.js, from the plugin); none means every checked step reads "Not checked" */
36
- export function initChecks(opts = {}) {
37
- const next = opts.checker || null;
38
- if (next !== checker) closeChecks(); // answers asked of another checker are not this one's
39
- checker = next;
40
- }
41
-
42
- /** Changes whenever a verdict may have: scenario-ui.js puts it in the player's signature, so an answer redraws the step */
43
- export const checksVersion = () => version;
44
-
45
- const namesActor = (scenario) => !!scenario && (!!scenario.actor || (Array.isArray(scenario.steps) && scenario.steps.some((s) => s && s.actor)));
46
-
47
- /**
48
- * A scenario opened: its batch is asked.
49
- * @param {object} scenario
50
- * @param {() => void} onChange called when an answer arrives or the As menu moved (scenario-ui.js syncs and redraws)
51
- */
52
- export function openChecks(scenario, onChange = () => {}) {
53
- // the scenario that is already being checked (opened in the list, now played): its answers stay, only who is told changes
54
- if (current && current.scenario && scenario && current.scenario.id === scenario.id) { current.onChange = onChange; replan(scenario); return; }
55
- closeChecks();
56
- current = { scenario: null, as: null, nodes: null, plan: null, answer: { status: 'idle' }, controller: null, onChange };
57
- if (namesActor(scenario) && layersOf(state).persona !== true) setLayer('persona', true, { scenario: true });
58
- replan(scenario);
59
- }
60
-
61
- /** The id of the scenario being checked (a player's or the list's), or null */
62
- export const checkedScenarioId = () => (current && current.scenario ? current.scenario.id : null);
63
-
64
- /** The scenario closed: nothing more is asked or shown */
65
- export function closeChecks() {
66
- if (current && current.controller) current.controller.abort();
67
- current = null;
68
- version++;
69
- }
70
-
71
- /** Plans the checks again when the scenario, the As persona or the map changed, and asks again when what is asked changed */
72
- function replan(scenario, force = false) {
73
- const c = current;
74
- if (!c) return;
75
- if (!force && c.scenario === scenario && c.nodes === state.nodes && c.plan) return;
76
- const before = c.plan ? c.plan.key : null;
77
- c.scenario = scenario;
78
- c.nodes = state.nodes;
79
- c.plan = planChecks(scenario, mapIndex(state.nodes, state.edges), checker, { as: c.as });
80
- version++;
81
- if (force || c.plan.key !== before) ask();
82
- }
83
-
84
- /** Asks the scenario's one batch; an answer that arrives for a batch no longer on the map is dropped */
85
- function ask() {
86
- const c = current;
87
- if (!c) return;
88
- if (c.controller) c.controller.abort();
89
- const items = c.plan.items;
90
- if (items.length === 0) { c.answer = { status: 'ok', results: new Map(), bad: new Map() }; version++; return; }
91
- const controller = new AbortController();
92
- c.controller = controller;
93
- c.answer = { status: 'pending' };
94
- version++;
95
- runChecks(checker, items, { signal: controller.signal }).then(
96
- ({ results, bad }) => settle(c, controller, { status: 'ok', results, bad }),
97
- (error) => { if (!controller.signal.aborted) settle(c, controller, { status: 'failed', error }); },
98
- );
99
- }
100
-
101
- function settle(c, controller, answer) {
102
- if (current !== c || c.controller !== controller) return;
103
- c.controller = null;
104
- c.answer = answer;
105
- version++;
106
- c.onChange();
107
- }
108
-
109
- /**
110
- * Puts the current step's verdict on the player's snapshot (`snap.verdict`, which scene.js draws) and,
111
- * while the As menu overrides it, the persona acting the step (`snap.current.actor`).
112
- * @param {object} snap the player's snapshot
113
- * @param {object} scenario the scenario the player plays (it changes with a what-if edit)
114
- */
115
- export function decorateSnapshot(snap, scenario) {
116
- if (!current) return snap;
117
- replan(scenario);
118
- const plan = current.plan.steps[snap.index];
119
- snap.verdict = stepVerdict(plan, current.answer);
120
- if (current.as && plan && plan.actor) snap.current.actor = plan.actor;
121
- return snap;
122
- }
123
-
124
- /** The verdict for each step of the scenario on the map (for a test or a caller that wants the whole run) */
125
- export function scenarioVerdicts() {
126
- if (!current || !current.plan) return [];
127
- return current.plan.steps.map((p) => stepVerdict(p, current.answer));
128
- }
129
-
130
- // ---------------------------------------------------------------------------
131
- // On the bar
132
- // ---------------------------------------------------------------------------
133
-
134
- /** The pill in the head, the line under the text, and the As row under the controls: only on the bar of a scenario that names an actor, so any other bar is as it was */
135
- function mountControls(bar) {
136
- // the pill ends the step's line; the reason is a line of its own under the bar's row, with the As row (the player is one bar: spec 0006 D6)
137
- const pill = document.createElement('span');
138
- pill.className = 'hx-player-verdict';
139
- pill.dataset.corn = 'state-pill';
140
- pill.hidden = true;
141
- const text = bar.querySelector('.hx-player-text');
142
- if (text) text.append(' ', pill); else bar.prepend(pill);
143
-
144
- const line = document.createElement('p');
145
- line.className = 'hx-player-check';
146
- line.dataset.corn = 'scenario-check';
147
- line.hidden = true;
148
-
149
- const row = document.createElement('div');
150
- row.className = 'hx-player-as';
151
- row.innerHTML = `
152
- <label class="hx-player-as-label">As <select data-corn="control" class="hx-player-speed hx-player-as-menu" data-check="as" aria-label="Act every step as"></select></label>
153
- <button data-corn-kind="secondary" type="button" class="hx-btn" data-check="recheck">Check again</button>
154
- <span class="hx-player-as-sum hx-faint"></span>`;
155
- const track = bar.querySelector('.hx-player-track');
156
- if (track) { track.before(line); track.before(row); } else { bar.appendChild(line); bar.appendChild(row); }
157
-
158
- row.querySelector('[data-check=as]').addEventListener('change', (e) => chooseActor(e.target.value));
159
- row.querySelector('[data-check=recheck]').addEventListener('click', recheck);
160
- }
161
-
162
- /** The As menu moved: every step acts as that persona ('' = as written), and the batch is asked again */
163
- export function chooseActor(id) {
164
- if (!current) return;
165
- current.as = id || null;
166
- replan(current.scenario, true);
167
- current.onChange();
168
- }
169
-
170
- /** Check again: the same batch, asked again */
171
- export function recheck() {
172
- if (!current) return;
173
- replan(current.scenario, true);
174
- current.onChange();
175
- }
176
-
177
- /** The personas on the map, what-if ones included, as the As menu's options */
178
- function fillMenu(menu) {
179
- const personas = state.nodes.filter((n) => n.type === 'persona');
180
- const options = [{ value: '', label: 'As written' }, ...personas.map((p) => ({ value: p.id.replace(/^persona-/, ''), label: personaLabel(p) }))];
181
- const key = options.map((o) => `${o.value}\u0000${o.label}`).join('\u0001');
182
- if (menu.dataset.key !== key) {
183
- menu.innerHTML = options.map((o) => `<option value="${escHtml(o.value)}">${escHtml(o.label)}</option>`).join('');
184
- menu.dataset.key = key;
185
- }
186
- menu.value = current && current.as ? current.as : '';
187
- }
188
-
189
- /** What the batch found, in a few words: "4 checks · 1 refused" */
190
- function summary() {
191
- const all = scenarioVerdicts();
192
- const asked = all.reduce((n, v) => n + v.checks.length, 0);
193
- if (!current || current.answer.status === 'pending') return asked ? `Checking ${asked}…` : '';
194
- if (current.answer.status === 'failed') return '';
195
- const refused = all.filter((v) => v.state === 'refused').length;
196
- const amber = all.filter((v) => v.mismatch).length;
197
- const parts = [`${asked} ${asked === 1 ? 'check' : 'checks'}`];
198
- if (refused) parts.push(`${refused} refused`);
199
- if (amber) parts.push(`${amber} not as expected`);
200
- return asked ? parts.join(' · ') : 'No permission questions';
201
- }
202
-
203
- /** Paints the step's state on the bar: called with every bar update */
204
- /** The words last written into each player's check line (paintChecks) */
205
- const lineHtml = new WeakMap();
8
+ import { pagePiece, mergePagePieceOptions } from './page-pieces.js';
206
9
 
207
- export function paintChecks(bar, snap) {
208
- if (!bar || !current) return;
209
- if (!bar.querySelector('.hx-player-as')) {
210
- if (!namesActor(current.scenario)) return;
211
- mountControls(bar);
212
- }
213
- const pill = bar.querySelector('.hx-player-verdict');
214
- const line = bar.querySelector('.hx-player-check');
215
- const row = bar.querySelector('.hx-player-as');
216
- if (!pill || !line || !row) return;
217
- const words = snap.done ? null : verdictWords(snap.verdict); // at the end there is no step to say a verdict of
218
- pill.hidden = !words;
219
- line.hidden = !words;
220
- if (words) {
221
- pill.dataset.tone = words.tone;
222
- if (cornState(words.tone)) pill.dataset.cornState = cornState(words.tone); else delete pill.dataset.cornState;
223
- pill.textContent = words.pill;
224
- pill.title = words.line;
225
- line.dataset.tone = words.tone;
226
- // painted every frame: the live line is rewritten only when its words change, so a verdict is said once
227
- const html = `${escHtml(words.line)}${words.detail ? ` <span class="hx-player-check-detail">${escHtml(words.detail)}</span>` : ''}`;
228
- if (lineHtml.get(line) !== html) { lineHtml.set(line, html); line.innerHTML = html; }
229
- }
230
- fillMenu(row.querySelector('[data-check=as]'));
231
- row.querySelector('[data-check=recheck]').disabled = current.answer.status === 'pending';
232
- row.querySelector('.hx-player-as-sum').textContent = summary();
233
- }
10
+ /** Hands the page's changes piece its checker (a plugin's adapter); none means every checked step reads "Not checked" */
11
+ export function initChecks(opts = {}) { mergePagePieceOptions('changes', { checker: opts.checker || null }); }
12
+ /** The verdict for each step of the scenario on the map */
13
+ export const scenarioVerdicts = () => { const p = pagePiece('changes'); return p ? p.checks.verdicts() : []; };
@@ -8,6 +8,8 @@
8
8
  */
9
9
 
10
10
  import { announce } from './keyboard.js';
11
+ import { pageInstance } from './state.js';
12
+ import { notSentWords as wordsFor } from './engine/connection.js';
11
13
 
12
14
  export const OFFLINE_WORDS = 'You are offline. Nothing is sent until you are back; what you have typed is kept.';
13
15
  export const ONLINE_WORDS = 'You are back online.';
@@ -21,9 +23,7 @@ let hideTimer = 0;
21
23
  export const isOffline = () => typeof navigator !== 'undefined' && navigator.onLine === false;
22
24
 
23
25
  /** The words for an action that got no answer: offline, it was not sent (and what was typed is still there); online, the server did not answer */
24
- export function notSentWords(what) {
25
- return isOffline() ? `You are offline. ${what} was not sent; what you typed is kept. Try again when you are back.` : `cornfield did not answer. ${what} was not sent; what you typed is kept.`;
26
- }
26
+ export const notSentWords = (what) => wordsFor(pageInstance(), what);
27
27
 
28
28
  function paint(said) {
29
29
  const el = document.getElementById('offline-banner');
@@ -0,0 +1,252 @@
1
+ /**
2
+ * @module explorer/engine/checks
3
+ *
4
+ * Checked scenarios on an instance (spec 0029 sections 3 and 5, PHY-10; the pure half is check-model.js). When a scenario opens, every check of every
5
+ * step goes to the checker (a plugin's adapter, handed to the piece by the host) in ONE batch; each step then shows its state on the player bar, and on
6
+ * the phone's sheet: a pill in the bar's head ("✕ Refused", seen on the peek too) and a line under the step's text ("Refused: Only an adult can change
7
+ * the household's rooms (ADULT_ONLY_WRITES)"). The current step's node or edge is drawn red, green or amber (scene.js calls `paintVerdict` with the
8
+ * verdict this module puts on the player's snapshot). The **As** menu (the personas on the map, what-if ones too) acts every step as one persona and
9
+ * asks again; **Check again** asks again as it is.
10
+ *
11
+ * createChecks(inst) -> {
12
+ * setChecker(checker) hands over the checker; none means every checked step reads "Not checked"
13
+ * open(scenario, onChange) a scenario opened: ask its batch
14
+ * decorate(snap, scenario) every sync: puts `verdict` (and the As actor) on the snapshot; asks again when what is asked changed
15
+ * paint(bar, snap) every bar update (the controls go on the bar at its first paint)
16
+ * close() the scenario closed: cancel what is in flight
17
+ * version() / checkedScenarioId() / verdicts() / chooseActor(id) / recheck()
18
+ * }
19
+ *
20
+ * A scenario that names an actor turns the People layer on, so its personas are on the map. Nothing is stored: the answers live in memory for the
21
+ * scenario on the map, and go with it.
22
+ */
23
+
24
+ import { escapeHtml as escHtml } from '../../core/text.js';
25
+ import { setLayer } from './layers.js';
26
+ import { layersOf } from '../layer-model.js';
27
+ import { mapIndex, personaLabel, planChecks, runChecks, stepVerdict, verdictWords } from '../check-model.js';
28
+ import { cornState } from '../corn-state.js';
29
+ import { addParts } from './piece.js';
30
+
31
+ const namesActor = (scenario) => !!scenario && (!!scenario.actor || (Array.isArray(scenario.steps) && scenario.steps.some((s) => s && s.actor)));
32
+
33
+ /**
34
+ * @param {ReturnType<import('../instance.js').createInstance>} inst
35
+ * @param {{ checker?: object | null }} [opts]
36
+ */
37
+ export function createChecks(inst, opts = {}) {
38
+ const { state } = inst;
39
+ let checker = opts.checker || null;
40
+ let current = null; // { scenario, as, nodes, plan, answer, controller, onChange }
41
+ let version = 0;
42
+ /** The words last written into each player's check line (paint) */
43
+ const lineHtml = new WeakMap();
44
+ /** What takes the listeners off the controls on the bar (the bar goes with them, but a listener is held until it is taken back) */
45
+ let controlOffs = [];
46
+
47
+ function close() {
48
+ if (current && current.controller) current.controller.abort();
49
+ current = null;
50
+ version++;
51
+ }
52
+
53
+ function setChecker(next) {
54
+ next = next || null;
55
+ if (next !== checker) close(); // answers asked of another checker are not this one's
56
+ checker = next;
57
+ }
58
+
59
+ /** Asks the scenario's one batch; an answer that arrives for a batch no longer on the map is dropped */
60
+ function ask() {
61
+ const c = current;
62
+ if (!c) return;
63
+ if (c.controller) c.controller.abort();
64
+ const items = c.plan.items;
65
+ if (items.length === 0) { c.answer = { status: 'ok', results: new Map(), bad: new Map() }; version++; return; }
66
+ const controller = new AbortController();
67
+ c.controller = controller;
68
+ c.answer = { status: 'pending' };
69
+ version++;
70
+ runChecks(checker, items, { signal: controller.signal }).then(
71
+ ({ results, bad }) => settle(c, controller, { status: 'ok', results, bad }),
72
+ (error) => { if (!controller.signal.aborted) settle(c, controller, { status: 'failed', error }); },
73
+ );
74
+ }
75
+
76
+ function settle(c, controller, answer) {
77
+ if (current !== c || c.controller !== controller) return;
78
+ c.controller = null;
79
+ c.answer = answer;
80
+ version++;
81
+ c.onChange();
82
+ }
83
+
84
+ /** Plans the checks again when the scenario, the As persona or the map changed, and asks again when what is asked changed */
85
+ function replan(scenario, force = false) {
86
+ const c = current;
87
+ if (!c) return;
88
+ if (!force && c.scenario === scenario && c.nodes === state.nodes && c.plan) return;
89
+ const before = c.plan ? c.plan.key : null;
90
+ c.scenario = scenario;
91
+ c.nodes = state.nodes;
92
+ c.plan = planChecks(scenario, mapIndex(state.nodes, state.edges), checker, { as: c.as });
93
+ version++;
94
+ if (force || c.plan.key !== before) ask();
95
+ }
96
+
97
+ /**
98
+ * A scenario opened: its batch is asked.
99
+ * @param {object} scenario
100
+ * @param {() => void} onChange called when an answer arrives or the As menu moved (the player syncs and redraws)
101
+ */
102
+ function open(scenario, onChange = () => {}) {
103
+ // the scenario that is already being checked (opened in the list, now played): its answers stay, only who is told changes
104
+ if (current && current.scenario && scenario && current.scenario.id === scenario.id) { current.onChange = onChange; replan(scenario); return; }
105
+ close();
106
+ current = { scenario: null, as: null, nodes: null, plan: null, answer: { status: 'idle' }, controller: null, onChange };
107
+ if (namesActor(scenario) && layersOf(state).persona !== true) setLayer(inst, 'persona', true, { scenario: true });
108
+ replan(scenario);
109
+ }
110
+
111
+ /** Puts the current step's verdict on the player's snapshot (`snap.verdict`, which scene.js draws) and, while the As menu overrides it, the persona acting the step */
112
+ function decorate(snap, scenario) {
113
+ if (!current) return snap;
114
+ replan(scenario);
115
+ const plan = current.plan.steps[snap.index];
116
+ snap.verdict = stepVerdict(plan, current.answer);
117
+ if (current.as && plan && plan.actor) snap.current.actor = plan.actor;
118
+ return snap;
119
+ }
120
+
121
+ /** The verdict for each step of the scenario on the map (for a test or a caller that wants the whole run) */
122
+ function verdicts() {
123
+ if (!current || !current.plan) return [];
124
+ return current.plan.steps.map((p) => stepVerdict(p, current.answer));
125
+ }
126
+
127
+ /** The As menu moved: every step acts as that persona ('' = as written), and the batch is asked again */
128
+ function chooseActor(id) {
129
+ if (!current) return;
130
+ current.as = id || null;
131
+ replan(current.scenario, true);
132
+ current.onChange();
133
+ }
134
+
135
+ /** Check again: the same batch, asked again */
136
+ function recheck() {
137
+ if (!current) return;
138
+ replan(current.scenario, true);
139
+ current.onChange();
140
+ }
141
+
142
+ /** The pill in the head, the line under the text, and the As row under the controls: only on the bar of a scenario that names an actor, so any other bar is as it was */
143
+ function mountControls(bar) {
144
+ const doc = inst.doc;
145
+ // the pill ends the step's line; the reason is a line of its own under the bar's row, with the As row (the player is one bar: spec 0006 D6)
146
+ const pill = doc.createElement('span');
147
+ pill.className = 'hx-player-verdict';
148
+ pill.dataset.corn = 'state-pill';
149
+ pill.hidden = true;
150
+ const text = bar.querySelector('.hx-player-text');
151
+ if (text) text.append(' ', pill); else bar.prepend(pill);
152
+
153
+ const line = doc.createElement('p');
154
+ line.className = 'hx-player-check';
155
+ line.dataset.corn = 'scenario-check';
156
+ line.hidden = true;
157
+
158
+ const row = doc.createElement('div');
159
+ row.className = 'hx-player-as';
160
+ row.innerHTML = `
161
+ <label class="hx-player-as-label">As <select data-corn="control" class="hx-player-speed hx-player-as-menu" data-check="as" aria-label="Act every step as"></select></label>
162
+ <button data-corn-kind="secondary" type="button" class="hx-btn" data-check="recheck">Check again</button>
163
+ <span class="hx-player-as-sum hx-faint"></span>`;
164
+ const track = bar.querySelector('.hx-player-track');
165
+ if (track) { track.before(line); track.before(row); } else { bar.appendChild(line); bar.appendChild(row); }
166
+ for (const el of [pill, line, row]) { if (el.hasAttribute('data-corn')) el.setAttribute('part', el.getAttribute('data-corn')); }
167
+ addParts(row);
168
+
169
+ const menu = row.querySelector('[data-check=as]');
170
+ const again = row.querySelector('[data-check=recheck]');
171
+ const onMenu = (e) => chooseActor(e.target.value);
172
+ menu.addEventListener('change', onMenu);
173
+ again.addEventListener('click', recheck);
174
+ controlOffs.push(() => { menu.removeEventListener('change', onMenu); again.removeEventListener('click', recheck); });
175
+ }
176
+
177
+ /** The bar goes: its controls let go of their listeners */
178
+ function unmount() {
179
+ const offs = controlOffs;
180
+ controlOffs = [];
181
+ offs.forEach((off) => off());
182
+ }
183
+
184
+ /** The personas on the map, what-if ones included, as the As menu's options */
185
+ function fillMenu(menu) {
186
+ const personas = state.nodes.filter((n) => n.type === 'persona');
187
+ const options = [{ value: '', label: 'As written' }, ...personas.map((p) => ({ value: p.id.replace(/^persona-/, ''), label: personaLabel(p) }))];
188
+ const key = options.map((o) => `${o.value}\u0000${o.label}`).join('\u0001');
189
+ if (menu.dataset.key !== key) {
190
+ menu.innerHTML = options.map((o) => `<option value="${escHtml(o.value)}">${escHtml(o.label)}</option>`).join('');
191
+ menu.dataset.key = key;
192
+ }
193
+ menu.value = current && current.as ? current.as : '';
194
+ }
195
+
196
+ /** What the batch found, in a few words: "4 checks · 1 refused" */
197
+ function summary() {
198
+ const all = verdicts();
199
+ const asked = all.reduce((n, v) => n + v.checks.length, 0);
200
+ if (!current || current.answer.status === 'pending') return asked ? `Checking ${asked}…` : '';
201
+ if (current.answer.status === 'failed') return '';
202
+ const refused = all.filter((v) => v.state === 'refused').length;
203
+ const amber = all.filter((v) => v.mismatch).length;
204
+ const parts = [`${asked} ${asked === 1 ? 'check' : 'checks'}`];
205
+ if (refused) parts.push(`${refused} refused`);
206
+ if (amber) parts.push(`${amber} not as expected`);
207
+ return asked ? parts.join(' · ') : 'No permission questions';
208
+ }
209
+
210
+ /** Paints the step's state on the bar: called with every bar update */
211
+ function paint(bar, snap) {
212
+ if (!bar || !current) return;
213
+ if (!bar.querySelector('.hx-player-as')) {
214
+ if (!namesActor(current.scenario)) return;
215
+ mountControls(bar);
216
+ }
217
+ const pill = bar.querySelector('.hx-player-verdict');
218
+ const line = bar.querySelector('.hx-player-check');
219
+ const row = bar.querySelector('.hx-player-as');
220
+ if (!pill || !line || !row) return;
221
+ const words = snap.done ? null : verdictWords(snap.verdict); // at the end there is no step to say a verdict of
222
+ pill.hidden = !words;
223
+ line.hidden = !words;
224
+ if (words) {
225
+ pill.dataset.tone = words.tone;
226
+ if (cornState(words.tone)) pill.dataset.cornState = cornState(words.tone); else delete pill.dataset.cornState;
227
+ pill.textContent = words.pill;
228
+ pill.title = words.line;
229
+ line.dataset.tone = words.tone;
230
+ // painted every frame: the live line is rewritten only when its words change, so a verdict is said once
231
+ const html = `${escHtml(words.line)}${words.detail ? ` <span class="hx-player-check-detail">${escHtml(words.detail)}</span>` : ''}`;
232
+ if (lineHtml.get(line) !== html) { lineHtml.set(line, html); line.innerHTML = html; }
233
+ }
234
+ fillMenu(row.querySelector('[data-check=as]'));
235
+ row.querySelector('[data-check=recheck]').disabled = current.answer.status === 'pending';
236
+ row.querySelector('.hx-player-as-sum').textContent = summary();
237
+ }
238
+
239
+ return {
240
+ setChecker,
241
+ open,
242
+ close,
243
+ decorate,
244
+ paint,
245
+ unmount,
246
+ verdicts,
247
+ chooseActor,
248
+ recheck,
249
+ version: () => version,
250
+ checkedScenarioId: () => (current && current.scenario ? current.scenario.id : null),
251
+ };
252
+ }
@@ -0,0 +1,36 @@
1
+ /**
2
+ * @module explorer/engine/connection
3
+ *
4
+ * The words for a failure that has no answer from the server, for an instance (spec 0011 D2): offline, it is "You are offline", not "cornfield
5
+ * could not be reached", and an action says it was not sent and keeps what the person typed. The banner that says it when the browser goes
6
+ * offline or comes back is the page's (explorer/connection.js); a piece only needs the words.
7
+ */
8
+
9
+ /** The words for an action that got no answer: offline, it was not sent (and what was typed is still there); online, the server did not answer */
10
+ export function notSentWords(inst, what) {
11
+ return inst.offline() ? `You are offline. ${what} was not sent; what you typed is kept. Try again when you are back.` : `cornfield did not answer. ${what} was not sent; what you typed is kept.`;
12
+ }
13
+
14
+ /**
15
+ * `fetch` for a read or a write that carries the maintainer's session: on the instance's own origin it is exactly `inst.fetch`; against a base
16
+ * on another origin (a host app, docs/embedding.md) the request is credentialed, which the server answers for a listed origin. A URL on another
17
+ * origin than the base's (an absolute URL, a `//host` path) is fetched without credentials.
18
+ */
19
+ export function credentialed(inst) {
20
+ return (path, init) => inst.fetch(path, sessionInit(inst, path, init));
21
+ }
22
+
23
+ /**
24
+ * The one rule for the maintainer's session (credentialed() here, and engine/source.js's reads of the config and the map): the fetch options for
25
+ * `path`, with `credentials: 'include'` added when the instance has a base and `path` is on the base's own origin, else `init` as it was
26
+ * (`undefined` stays `undefined`, so a request of the page's own is unchanged). What the caller set itself wins.
27
+ */
28
+ export function sessionInit(inst, path, init) {
29
+ return inst.base && underBase(inst, path) ? { credentials: 'include', ...init } : init;
30
+ }
31
+
32
+ /** Whether `path`, made absolute against the instance's base (as `inst.fetch` makes it), is on the base's origin: only those carry the maintainer's session */
33
+ export function underBase(inst, path) {
34
+ if (!inst.base) return false;
35
+ try { return new URL(inst.url(String(path)), inst.base).origin === new URL(inst.base).origin; } catch { return false; }
36
+ }
@@ -178,6 +178,7 @@ function changed(inst) {
178
178
  inst.touchScene();
179
179
  inst.emit('layers');
180
180
  redrawLayers(inst);
181
+ if (!inst.map) return; // a scope sets the project's layers before the map piece is there; it draws them when it comes
181
182
  inst.map.render();
182
183
  inst.map.ensureFlowLoop();
183
184
  }