@popcorn245/cornfield 0.48.0 → 0.50.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.
Files changed (42) hide show
  1. package/core/strings.js +14 -0
  2. package/explorer/check.js +7 -227
  3. package/explorer/connection.js +3 -3
  4. package/explorer/css/review.css +49 -0
  5. package/explorer/decision-model.js +6 -0
  6. package/explorer/engine/checks.js +252 -0
  7. package/explorer/engine/connection.js +36 -0
  8. package/explorer/engine/layers.js +1 -0
  9. package/explorer/engine/layout.js +8 -3
  10. package/explorer/engine/player.js +489 -0
  11. package/explorer/engine/proposal.js +373 -0
  12. package/explorer/engine/render.js +1 -0
  13. package/explorer/engine/sheets.js +40 -7
  14. package/explorer/engine/source.js +315 -0
  15. package/explorer/engine/theme-sheet.js +444 -0
  16. package/explorer/engine/topology.js +126 -0
  17. package/explorer/index.html +6 -59
  18. package/explorer/instance.js +87 -7
  19. package/explorer/page-address.js +59 -0
  20. package/explorer/page-pieces.js +33 -5
  21. package/explorer/page.js +20 -20
  22. package/explorer/panels.js +1 -3
  23. package/explorer/pieces/changes.js +495 -0
  24. package/explorer/pieces/decision.js +664 -0
  25. package/explorer/pieces/layers.js +4 -0
  26. package/explorer/pieces/map.js +117 -25
  27. package/explorer/pieces/node-panel.js +34 -1
  28. package/explorer/pieces/proposal.js +488 -0
  29. package/explorer/pieces/review.js +280 -0
  30. package/explorer/pieces/scope-link.js +50 -0
  31. package/explorer/pieces/scope.js +247 -0
  32. package/explorer/pieces/status.css +24 -0
  33. package/explorer/pieces/status.js +144 -0
  34. package/explorer/plugins.js +0 -10
  35. package/explorer/proposal.js +64 -697
  36. package/explorer/review-column.js +24 -439
  37. package/explorer/review.js +27 -673
  38. package/explorer/scenario-ui.js +32 -478
  39. package/explorer/sheets.js +8 -7
  40. package/explorer/view.js +12 -64
  41. package/explorer/waiting-line.js +7 -50
  42. package/package.json +1 -1
@@ -0,0 +1,144 @@
1
+ /**
2
+ * @module explorer/pieces/status
3
+ *
4
+ * What a person sees while a map loads from a server, and when it cannot (spec 0011 D5, D6, D11): the quiet "Loading", "You are not signed in to
5
+ * cornfield" (or, for a 403, "This map is for the project's maintainers") with a real link "Sign in to cornfield" to the server's sign-in, "This page
6
+ * is not listed by that cornfield" with the origin it should list, offline with Retry, a server's own error, a `src` that is not an address, and the notice that the package and the server are different versions. It is engine markup, mounted by the
7
+ * map piece inside its stage (pieces/map.js); a shell holds none of it. Words are `strings` keys (core/strings.js), each element has a `data-corn`
8
+ * hook and so a `part`, and nothing here is built from data except through `escapeHtml`, `textContent` and a link checked to be http(s).
9
+ *
10
+ * const status = mount(container, inst, { onRetry }) -> { destroy, refuse }
11
+ *
12
+ * It follows the instance's `source` event (engine/source.js) and reads where the load is now from `sourceOf(inst)`, so a piece that joins late
13
+ * shows the right thing. A polite live region, apart from the card, says each state once (a second `server` error says itself again: it is a
14
+ * second answer). Focus is never taken; the one exception is the person's own Retry, after which focus goes to the card, or to the map when it loads.
15
+ */
16
+
17
+ import { escapeHtml } from '../../core/text.js';
18
+ import { sourceOf } from '../engine/source.js';
19
+ import { t } from '../engine/strings.js';
20
+ import { withParts } from '../engine/piece.js';
21
+
22
+ /** Each state's public `data-corn-state` (docs/theming.md "States"): loading is running, a sign-in is waiting on the person, a refusal is a failure, offline is stopped */
23
+ const STATE_OF = Object.freeze({ loading: 'running', 'sign-in': 'waiting', 'not-allowed': 'failure', offline: 'stopped', server: 'failure', address: 'failure', 'second-map': 'failure' });
24
+
25
+ /** The words of a state: `title` and `text` are `strings` keys, `extra` is data the state adds (escaped when shown) */
26
+ function wordsOf(inst, detail) {
27
+ switch (detail.state) {
28
+ case 'loading': return { title: t(inst, 'sourceLoading'), text: '' };
29
+ // What is the case is the title (signed out, or signed in as someone who is not a maintainer: a 403); what to do is the link
30
+ case 'sign-in': return detail.status === 403
31
+ ? { title: t(inst, 'sourceNotMaintainer'), text: t(inst, 'sourceNotMaintainerText'), link: detail.signIn }
32
+ : { title: t(inst, 'sourceSignedOut'), text: t(inst, 'sourceSignInText'), link: detail.signIn };
33
+ case 'not-allowed': return { title: t(inst, 'sourceNotListed'), text: t(inst, 'sourceNotListedHint'), origin: detail.origin, retry: true };
34
+ case 'offline': return { title: t(inst, 'sourceOffline'), text: t(inst, 'sourceOfflineText'), retry: true };
35
+ case 'server': return { title: t(inst, 'errorTitle'), text: detail.message || '', retry: true };
36
+ case 'address': return { title: t(inst, 'sourceBadAddress'), text: detail.problem || '' };
37
+ case 'second-map': return { title: t(inst, 'sourceSecondMap'), text: '' };
38
+ default: return null;
39
+ }
40
+ }
41
+
42
+ /** A link that is safe to put in an `href`: an absolute http(s) address, as the server's sign-in is */
43
+ const safeHref = (href) => (typeof href === 'string' && /^https?:\/\/[^\s\\]+$/i.test(href) ? href : '');
44
+
45
+ /** The markup of one state (the card), escaped */
46
+ export function cardHtml(inst, detail) {
47
+ const words = wordsOf(inst, detail);
48
+ if (!words) return '';
49
+ const href = words.link ? safeHref(words.link) : '';
50
+ const parts = [`<p data-corn="source-title">${escapeHtml(words.title)}</p>`];
51
+ if (words.text) parts.push(`<p data-corn="source-text">${escapeHtml(words.text)}</p>`);
52
+ if (words.origin) parts.push(`<p data-corn="source-text"><code data-corn="source-origin">${escapeHtml(words.origin)}</code></p>`);
53
+ if (href) parts.push(`<a data-corn="source-sign-in" data-corn-kind="primary" href="${escapeHtml(href)}" target="_top" rel="noopener">${escapeHtml(t(inst, 'sourceSignIn'))}</a>`);
54
+ if (words.retry) parts.push(`<button type="button" data-corn="source-retry" data-corn-kind="secondary">${escapeHtml(t(inst, 'retry'))}</button>`);
55
+ return parts.join('');
56
+ }
57
+
58
+ /** The words the polite region says for a state: its title and its sentence, once */
59
+ export function announcement(inst, detail) {
60
+ const words = wordsOf(inst, detail);
61
+ return words ? [words.title, words.text, words.origin].filter(Boolean).join('. ') : '';
62
+ }
63
+
64
+ /**
65
+ * @param {Element} container where the card goes (the map's stage)
66
+ * @param {ReturnType<typeof import('../instance.js').createInstance>} inst
67
+ * @param {{ onRetry?: () => void, focusMap?: () => void, follow?: boolean }} [options] `follow: false`: the piece shows only what `refuse` tells it (a second map)
68
+ */
69
+ export function mount(container, inst, { onRetry = () => {}, focusMap = () => {}, follow = true } = {}) {
70
+ const doc = inst.doc;
71
+ const active = () => { const r = container.getRootNode(); return (r && r.activeElement) || null; };
72
+ const make = (html) => {
73
+ const template = doc.createElement('template');
74
+ template.innerHTML = withParts(html);
75
+ return template.content.firstElementChild;
76
+ };
77
+ const card = make('<div data-corn="source-status" hidden></div>');
78
+ const notice = make('<p data-corn="source-notice" hidden></p>');
79
+ const live = make('<p data-corn="source-announcer" class="hx-sr-only" role="status" aria-live="polite" aria-atomic="true"></p>');
80
+ container.append(card, notice, live);
81
+ let shown = null;
82
+ let ended = false;
83
+
84
+ const render = (detail) => {
85
+ if (ended || !detail) return;
86
+ const words = wordsOf(inst, detail);
87
+ const hadFocus = card.contains(active());
88
+ if (!words) {
89
+ card.hidden = true;
90
+ card.textContent = '';
91
+ shown = detail.state;
92
+ if (hadFocus) focusMap(); // the card the focus was on is gone: it goes to the map
93
+ return;
94
+ }
95
+ card.innerHTML = withParts(cardHtml(inst, detail));
96
+ card.setAttribute('data-corn-state', STATE_OF[detail.state]);
97
+ card.hidden = false;
98
+ const again = shown !== detail.state || detail.state === 'server';
99
+ shown = detail.state;
100
+ if (again) live.textContent = announcement(inst, detail);
101
+ if (hadFocus) { card.setAttribute('tabindex', '-1'); card.focus(); } // the person's own Retry: the focus stays with what they were doing
102
+ };
103
+
104
+ const showNotice = (detail) => {
105
+ if (ended) return;
106
+ if (!detail) { notice.hidden = true; notice.textContent = ''; return; }
107
+ notice.textContent = `${t(inst, 'sourceVersion')} (${detail.package} / ${detail.server})`;
108
+ notice.hidden = false;
109
+ // The notice is a quiet line the load goes on under: it is said once, in the same region, after what the load said
110
+ live.textContent = `${live.textContent ? `${live.textContent}. ` : ''}${notice.textContent}`;
111
+ };
112
+
113
+ const onClick = (event) => {
114
+ const retry = event.target && event.target.closest ? event.target.closest('[data-corn="source-retry"]') : null;
115
+ if (retry && card.contains(retry)) { event.preventDefault(); onRetry(); }
116
+ };
117
+ const offClick = inst.listen(card, 'click', onClick);
118
+
119
+ const off = follow ? inst.on('source', (detail) => {
120
+ if (detail.state === 'version') showNotice(detail); else render(detail);
121
+ }) : () => {};
122
+
123
+ // A piece that joins after the load began shows where it is now
124
+ const source = follow ? sourceOf(inst) : null;
125
+ if (source) {
126
+ const current = source.state();
127
+ if (current && current.state !== 'ready') render(current);
128
+ if (source.notice) showNotice(source.notice);
129
+ }
130
+
131
+ return {
132
+ /** Says that this piece cannot have the scope it is in (a second map): the card says why, and the piece draws nothing */
133
+ refuse(why = 'second-map') { render({ state: why }); },
134
+ destroy() {
135
+ if (ended) return;
136
+ ended = true;
137
+ off();
138
+ offClick();
139
+ card.remove();
140
+ notice.remove();
141
+ live.remove();
142
+ },
143
+ };
144
+ }
@@ -38,16 +38,6 @@ export const failedSource = (slots) => Object.freeze({
38
38
  },
39
39
  });
40
40
 
41
- /** The config the server hands the browser; a page opened without the server reads as none */
42
- export async function loadClientConfig(fetcher = (...a) => globalThis.fetch(...a)) {
43
- try {
44
- const res = await fetcher('/cornfield/config.json', { cache: 'no-store' });
45
- return res.ok ? await res.json() : {};
46
- } catch {
47
- return {};
48
- }
49
- }
50
-
51
41
  /** An <img> for a brand image (the server's same-origin URL): decorative, the button it sits in carries the name; `alt` only shows if it fails to load */
52
42
  function brandImage(doc, src, kind, alt = '') {
53
43
  const img = doc.createElement('img');