@openbooknetwork/hesap 0.3.0 → 0.3.1
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/package.json +1 -1
- package/src/client.css +165 -0
- package/src/families/counters/add-figures.js +85 -0
- package/src/families/counters/add-goals.js +82 -0
- package/src/families/counters/add-model.js +82 -0
- package/src/families/counters/add.js +442 -0
- package/src/families/counters/add.mjs +102 -0
- package/src/families/counters/bonds.js +5 -5
- package/src/families/counters/client.css +37 -0
- package/src/families/counters/client.js +2 -1
- package/src/families/counters/count.js +4 -5
- package/src/families/counters/counter.js +14 -0
- package/src/families/counters/goals.js +1 -9
- package/src/families/counters/i18n/en.yaml +89 -45
- package/src/families/counters/i18n/tr.yaml +56 -12
- package/src/families/counters/index.mjs +5 -3
- package/src/families/counters/match-model.js +10 -1
- package/src/families/counters/match.js +31 -11
- package/src/families/counters/match.mjs +7 -1
- package/src/families/line/client.css +118 -0
- package/src/families/line/client.js +12 -0
- package/src/families/line/figures.js +143 -0
- package/src/families/line/goals.js +168 -0
- package/src/families/line/i18n/en.yaml +61 -0
- package/src/families/line/i18n/tr.yaml +61 -0
- package/src/families/line/index.mjs +227 -0
- package/src/families/line/lens.js +49 -0
- package/src/families/line/line.js +624 -0
- package/src/families/line/model.js +219 -0
- package/src/families.mjs +2 -1
- package/src/i18n/en.yaml +158 -46
- package/src/i18n/tr.yaml +124 -12
- package/src/kit/i18n/en.yaml +8 -1
- package/src/kit/i18n/tr.yaml +7 -0
- package/src/kit/kit.css +9 -0
- package/src/kit/panel.js +2 -0
- package/src/kit/random.js +9 -0
- package/src/kit/written.js +70 -0
- package/src/node.mjs +3 -1
|
@@ -0,0 +1,624 @@
|
|
|
1
|
+
// The line scene: a number line from left to right with a mark at each step, a point the reader drags along it
|
|
2
|
+
// that snaps to the nearest mark, and buttons that jump the point one gap. A page may add a bracket over the tens
|
|
3
|
+
// round the point, gaps that open into lines of their own, and label cards for blank marks. The looks stones and
|
|
4
|
+
// stones-on-line draw the frog's stepping stones of the opening pages. A challenge page asks the reader to put the
|
|
5
|
+
// point on a number.
|
|
6
|
+
import { opening, jump, place, pick, open, lay, show as reveal, next, toggleMiddle, estimate as setEstimate, moveStart, writtenOf, isFree, numbered, standing, posOf, slide, labelKeep, settle as settleMarks, gapsKey, sides, SETTLE, arcsOf, jumpsOf, counts, readoutsOf, measuresOf, readingOf, tensOf, gapsOf, numberAt, markOf, labelled, findsOf, shuffled, isStones, gapOn, labelRows, secondTo, drawLine } from './model.js';
|
|
7
|
+
import { GOALS } from './goals.js';
|
|
8
|
+
import { pointShape, flag, arc, frog, stone, pond, bracket as makeBracket, blankBox, card, house, plaque, van, book, bookmark, BOX, CARD } from './figures.js';
|
|
9
|
+
import { lens as makeLens, LENS_H } from './lens.js';
|
|
10
|
+
import { svg as el, h, svgPoint } from '../../kit/dom.js';
|
|
11
|
+
import { drag, nudge } from '../../kit/drag.js';
|
|
12
|
+
import { chip, button, segment, slider, stepper, row, check } from '../../kit/panel.js';
|
|
13
|
+
import { goalRow } from '../../kit/goal.js';
|
|
14
|
+
import { live } from '../../kit/text.js';
|
|
15
|
+
import { numbers } from '../../kit/format.js';
|
|
16
|
+
import { dice, fresh, another } from '../../kit/random.js';
|
|
17
|
+
import { writtenLine, words as writeWords, reading as writeReading, DASH } from '../../kit/written.js';
|
|
18
|
+
|
|
19
|
+
const FONT = 10, APART = 4; // The least size of a label, and the least room between two labels, in pixels.
|
|
20
|
+
// A line whose gaps are narrower than this shows the lens while the point moves. A house of the street needs more
|
|
21
|
+
// room than a mark to be read, so the street shows the lens below a wider gap.
|
|
22
|
+
const NARROW = 8, NARROW_STREET = 24;
|
|
23
|
+
const LENS_STAYS = 1600; // The lens stays this long after the last jump, in milliseconds.
|
|
24
|
+
const STILL = 4; // A press that moves less than this many pixels leaves the point or the card where it is.
|
|
25
|
+
const POLE = 16, FLAG = 24, STACK = 30; // A flag's pole, its box, and how far a second flag over the same place rises.
|
|
26
|
+
const LONG = 11, SHORT = 6; // Half the height of a long mark and of a short mark.
|
|
27
|
+
const BOOK_TOP = 34; // The bookmark stands this far above the edge of the book.
|
|
28
|
+
const WHO = ['point', 'second'];
|
|
29
|
+
const clamp = (v, lo, hi) => Math.max(lo, Math.min(hi, v));
|
|
30
|
+
|
|
31
|
+
export default function line({ host, stage, panel, T, lang, moving, motion }) {
|
|
32
|
+
const fx = motion.group();
|
|
33
|
+
const svg = el('svg', { class: 'line-svg' });
|
|
34
|
+
stage.appendChild(svg);
|
|
35
|
+
// base is the page as written, and spec the page with the line a challenge drew. s is its state. geo is the
|
|
36
|
+
// figure's layout in pixels, and parts the elements paint moves. held is the point or card under the reader's
|
|
37
|
+
// finger, glide a jump or a snap on its way, and looking whether the lens shows. On a challenge page target is the
|
|
38
|
+
// draw and goal the challenge's line.
|
|
39
|
+
let base = null, spec = null, s = null, geo = null, parts = null, chips = [], jumps = null, held = null, glide = null, area = { w: 0, h: 0 };
|
|
40
|
+
let looking = false, timer = 0, target = null, goal = null, solved = false, acts = {}, kb = null, drawn = null;
|
|
41
|
+
const int = n => numbers(lang()).int(n);
|
|
42
|
+
const num = (k, st = s) => numberAt(spec, k, st.step);
|
|
43
|
+
new ResizeObserver(() => { const r = svg.getBoundingClientRect(); if (spec && (Math.round(r.width) !== area.w || Math.round(r.height) !== area.h)) build(); }).observe(svg);
|
|
44
|
+
|
|
45
|
+
// wide gives the width of the widest of the words at size 1, measured in the label's own type.
|
|
46
|
+
function wide(words, cls = 'line-label') {
|
|
47
|
+
const probe = el('text', { class: cls, 'font-size': 100 }), widths = [];
|
|
48
|
+
svg.append(probe);
|
|
49
|
+
for (const w of words) { probe.textContent = w; widths.push(probe.getComputedTextLength()); }
|
|
50
|
+
probe.remove();
|
|
51
|
+
return Math.max(1, ...widths) / 100;
|
|
52
|
+
}
|
|
53
|
+
// The marks that carry labels. A challenge that draws its line shows every label once its goal holds, so its
|
|
54
|
+
// labels are set to fit all of them.
|
|
55
|
+
// The middle's label shows while its mark does.
|
|
56
|
+
const mid = k => spec.middle && k === gapsOf(spec) / 2;
|
|
57
|
+
const shown = (k, st = s) => labelled(spec, num(k, st)) || (spec.reveal && solved) || (mid(k) && st.middle);
|
|
58
|
+
const sized = k => labelled(spec, num(k)) || spec.reveal || mid(k);
|
|
59
|
+
const street = () => spec.look === 'street';
|
|
60
|
+
|
|
61
|
+
// layout fits the line to the stage: the gap between marks, the sizes that follow it, and the height of the line
|
|
62
|
+
// with the bands above and below it, so the figure stands in the middle of the stage.
|
|
63
|
+
function layout(W, H) {
|
|
64
|
+
const n = gapsOf(spec), stones = isStones(spec), onLine = spec.look !== 'stones';
|
|
65
|
+
// A blank box at an end of the line keeps its whole width on the stage.
|
|
66
|
+
const gap = gapOn(W - (spec.blank.length ? BOX.w : 0) - (street() ? 8 : 0), n, stones), x0 = (W - n * gap) / 2;
|
|
67
|
+
const big = gap >= 34, r = big ? 11 : 9, arcH = clamp(gap * 0.42, Math.min(10, gap * 1.5), 26);
|
|
68
|
+
const sw = Math.min(gap * 0.86, 58), fs = Math.min(Math.max(gap * 0.9, 26), 46);
|
|
69
|
+
let top, bottom, g = { gap, x0, r, arcH, sw, fs, n, x: k => x0 + k * gap, narrow: gap < (spec.look === 'street' ? NARROW_STREET : NARROW) && onLine && !isFree(spec), font: big ? 16 : 14, rows: 1 };
|
|
70
|
+
// A house of the street is about as wide as its gap, and a flag stands over its door number.
|
|
71
|
+
g.houseH = clamp(gap * 1.1, 12, 36); g.flagFrom = street() ? -(g.houseH + 30) : spec.look === 'book' ? -BOOK_TOP : -r;
|
|
72
|
+
if (!onLine) {
|
|
73
|
+
g.stoneY = 0; g.arcY = -fs * 0.95; g.downY = sw * 0.3 + 6;
|
|
74
|
+
g.water = [-sw * 0.3 - 14, g.downY + (spec.jump ? 2 * arcH * 0.5 + 12 : 8)];
|
|
75
|
+
top = g.arcY - arcH - 8; bottom = g.water[1] + 8;
|
|
76
|
+
} else {
|
|
77
|
+
// Every labelled mark keeps its label. The labels take one row where they fit it, and two rows where not.
|
|
78
|
+
const ks = Array.from({ length: n + 1 }, (_, k) => k).filter(sized);
|
|
79
|
+
const spacing = gap * Math.min(n, ...ks.slice(1).map((k, i) => k - ks[i]));
|
|
80
|
+
Object.assign(g, labelRows(wide(ks.map(k => int(num(k)))), spacing, { base: g.font, least: FONT, apart: APART }));
|
|
81
|
+
g.row = new Map(ks.map((k, i) => [k, g.rows === 2 ? i % 2 : 0]));
|
|
82
|
+
g.labelY = 26 + g.font / 2; g.arcY = -6;
|
|
83
|
+
if (street()) g.labelY = -(g.houseH + 16);
|
|
84
|
+
let y = street() ? 34 : Math.max(g.labelY + g.font / 2 + (g.rows === 2 ? g.font + 3 : 0), spec.blank.length ? g.labelY + BOX.h / 2 : 0) + 6;
|
|
85
|
+
if (spec.jump) { g.downY = y + 6; y = g.downY + g.arcH + 6; }
|
|
86
|
+
if (spec.reading || spec.moved) { g.readingY = y + 22; y += 46; }
|
|
87
|
+
if (spec.written) { g.writtenY = y + 32; y += 64; }
|
|
88
|
+
if (spec.open) { g.openTop = y; g.openY = y + 56; y = g.openY + 40; }
|
|
89
|
+
if (spec.cards) {
|
|
90
|
+
const per = Math.max(1, Math.floor((W - 20) / (CARD.w + 8))), all = s.cards.length, row0 = y + 30 + CARD.h / 2;
|
|
91
|
+
g.home = i => { const r0 = Math.floor(i / per), inRow = Math.min(per, all - r0 * per), c = i % per; return { x: W / 2 + (c - (inRow - 1) / 2) * (CARD.w + 8), y: row0 + r0 * (CARD.h + 10) }; };
|
|
92
|
+
y += 30 + Math.ceil(all / per) * (CARD.h + 10);
|
|
93
|
+
}
|
|
94
|
+
bottom = y;
|
|
95
|
+
// A challenge's point shows its flag once the goal holds, and a find page's once the reader presses Show, with
|
|
96
|
+
// the flag of the number to find over it.
|
|
97
|
+
const flags = spec.flag || spec.challenge || spec.find != null || spec.estimate != null ? -g.flagFrom + POLE + FLAG + (spec.second != null || spec.find != null || spec.challenge ? STACK : 0) : r;
|
|
98
|
+
g.lensY = street() ? g.labelY - 14 : -r - 6;
|
|
99
|
+
top = -Math.max(flags, spec.jump ? 6 + arcH + 4 : 0, g.narrow ? -g.lensY + LENS_H : 0, spec.bracket ? 30 : 0) - 8;
|
|
100
|
+
if (stones) {
|
|
101
|
+
// The stones stand over the line, each over the mark of its number, with the frog on the stone over the point.
|
|
102
|
+
g.water = [0, -(spec.jump ? arcH + 18 : 20)];
|
|
103
|
+
g.stoneY = g.water[1] - sw * 0.3 - 10; g.water[0] = g.stoneY - sw * 0.3 - 10;
|
|
104
|
+
top = g.stoneY - fs - 8;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
const tall = bottom - top;
|
|
108
|
+
g.y0 = tall < H ? (H - tall) / 2 - top : 8 - top;
|
|
109
|
+
return g;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function build() {
|
|
113
|
+
const r = svg.getBoundingClientRect(); area = { w: Math.round(r.width), h: Math.round(r.height) };
|
|
114
|
+
fx.stop(); held = null; glide = null; kb = null;
|
|
115
|
+
if (!area.w || !area.h) { geo = null; parts = null; svg.replaceChildren(); refresh(); return; } // A phone is showing the text.
|
|
116
|
+
svg.setAttribute('viewBox', `0 0 ${area.w} ${area.h}`);
|
|
117
|
+
svg.replaceChildren();
|
|
118
|
+
const g = geo = layout(area.w, area.h), y0 = g.y0, nodes = [], onLine = spec.look !== 'stones';
|
|
119
|
+
parts = { points: {}, flags: {}, labels: new Map(), marks: new Map(), cards: [] };
|
|
120
|
+
if (isStones(spec)) {
|
|
121
|
+
const [wy1, wy2] = g.water;
|
|
122
|
+
nodes.push(...pond(g.x(0) + g.gap * 0.45, Math.min(g.x(g.n) + g.gap * 0.6, area.w - 6), y0 + wy1, y0 + wy2, g.x(0) + g.gap * 0.42));
|
|
123
|
+
for (let k = 1; k <= g.n; k++) nodes.push(stone(g.x(k), y0 + g.stoneY, g.sw, int(k), Math.min(g.font, g.sw * 0.5)));
|
|
124
|
+
if (onLine) for (let k = 0; k <= g.n; k++) nodes.push(el('line', { class: 'line-guide', x1: g.x(k), x2: g.x(k), y1: y0 + wy2 + 2, y2: y0 - 14 }));
|
|
125
|
+
}
|
|
126
|
+
if (spec.open) nodes.push(...gaps());
|
|
127
|
+
if (street()) {
|
|
128
|
+
// The street: a road, a house at each mark, and the door numbers on plaques over the doors.
|
|
129
|
+
nodes.push(el('rect', { class: 'line-road', x: g.x(0) - Math.min(g.gap / 2, 12), y: y0 + 2, width: g.n * g.gap + 2 * Math.min(g.gap / 2, 12), height: 22 }));
|
|
130
|
+
for (let k = 0; k <= g.n; k++) {
|
|
131
|
+
nodes.push(house(g.x(k), y0, Math.min(g.gap * 0.84, 30), g.houseH));
|
|
132
|
+
if (labelled(spec, num(k))) { const p = plaque(g.x(k), y0 + g.labelY, int(num(k)), Math.max(FONT, Math.min(g.font, 13))); parts.labels.set(k, p); nodes.push(p); }
|
|
133
|
+
}
|
|
134
|
+
} else if (onLine) {
|
|
135
|
+
// The book is the edge of its pages between its two covers. Every other line is a line with an arrow's room.
|
|
136
|
+
if (spec.look === 'book') nodes.push(...book(g.x(0), g.x(g.n), y0));
|
|
137
|
+
else nodes.push(el('line', { class: 'line-axis', x1: g.x(0) - Math.min(14, g.gap / 2), x2: g.x(g.n) + Math.min(14, g.gap / 2), y1: y0, y2: y0 }));
|
|
138
|
+
// A labelled mark is long and carries its number, a mark without a label is short, and a blank mark has a box.
|
|
139
|
+
for (let k = 0; k <= g.n; k++) {
|
|
140
|
+
const n = num(k), long = labelled(spec, n), half = long ? LONG : SHORT;
|
|
141
|
+
// An unmarked line has its two end marks alone, and the middle's label when it shows.
|
|
142
|
+
if (isFree(spec) && !long) { if (sized(k)) { const t = el('text', { class: 'line-label', x: g.x(k), y: y0 + g.labelY, 'font-size': g.font }, [int(n)]); parts.labels.set(k, t); nodes.push(t); } continue; }
|
|
143
|
+
if (spec.look !== 'book') { const m = el('line', { class: `line-mark${long ? '' : ' line-short'}`, x1: g.x(k), x2: g.x(k), y1: y0 - half, y2: y0 + half }); parts.marks.set(k, m); nodes.push(m); }
|
|
144
|
+
if (sized(k)) { const t = el('text', { class: 'line-label', x: g.x(k), y: y0 + g.labelY + (g.row.get(k) ? g.font + 3 : 0), 'font-size': g.font }, [int(n)]); parts.labels.set(k, t); nodes.push(t); }
|
|
145
|
+
if (spec.blank.includes(n)) { const box = blankBox(g.x(k), y0 + g.labelY, Math.min(BOX.w, g.gap - 6)); parts.labels.set(k, box); nodes.push(box); }
|
|
146
|
+
}
|
|
147
|
+
// The estimate mark rises well above a long mark, with a diamond at its top, so it shows on an end mark too.
|
|
148
|
+
if (spec.estimate != null) {
|
|
149
|
+
const top = y0 - LONG - 20;
|
|
150
|
+
parts.estimate = el('g', { class: 'line-estimate', 'data-ref': 'estimate' }, [
|
|
151
|
+
el('line', { y1: top, y2: y0 + LONG + 4 }), el('path', { d: `M 0 ${top - 9} L 5 ${top - 4} L 0 ${top + 1} L -5 ${top - 4} Z` }),
|
|
152
|
+
]);
|
|
153
|
+
nodes.push(parts.estimate);
|
|
154
|
+
}
|
|
155
|
+
if (spec.middle) { const k = g.n / 2; parts.middle = el('line', { class: 'line-mark line-middle', 'data-ref': 'middle', x1: g.x(k), x2: g.x(k), y1: y0 - LONG - 4, y2: y0 + LONG }); nodes.push(parts.middle); }
|
|
156
|
+
}
|
|
157
|
+
if (spec.bracket) { parts.bracket = makeBracket(); nodes.push(parts.bracket.el); }
|
|
158
|
+
parts.arcs = el('g', { class: 'line-arcs', 'data-ref': 'jumps' });
|
|
159
|
+
nodes.push(parts.arcs);
|
|
160
|
+
if (spec.reading || spec.moved) { parts.reading = el('text', { class: 'line-reading', 'data-ref': spec.moved ? 'gaps' : 'reading', x: area.w / 2, y: y0 + g.readingY }); nodes.push(parts.reading); }
|
|
161
|
+
if (spec.moved) nodes.push(...handles());
|
|
162
|
+
const layer = el('g', { class: 'line-points' });
|
|
163
|
+
if (isStones(spec)) { parts.frog = el('g', { 'data-ref': spec.look === 'stones' ? 'point' : null }, [frog(g.fs)]); layer.append(parts.frog); }
|
|
164
|
+
if (onLine && s.at !== null) for (const who of WHO.filter(w => w === 'point' || spec.second != null)) {
|
|
165
|
+
// The point of the street is a van, which shows the number of its house on its side.
|
|
166
|
+
// The point of the book is a bookmark. The point of an estimate page stays where it is.
|
|
167
|
+
// On a page with start the point moves by the jump buttons alone.
|
|
168
|
+
const shape = street() ? van(14) : null, still = spec.estimate != null || (who === 'point' && spec.start != null);
|
|
169
|
+
const kids = shape ? shape.nodes : spec.look === 'book' ? bookmark(BOOK_TOP) : pointShape(who, g.r);
|
|
170
|
+
const p = el('g', { class: `${still ? 'line-fixed' : 'line-point'} line-c-${who}`, 'data-ref': who, tabindex: still ? null : 0, role: still ? null : 'slider', 'aria-label': T(street() ? 'van' : spec.look === 'book' ? 'bookmark' : `${who}-name`), 'aria-valuemin': still ? null : num(0), 'aria-valuemax': still ? null : num(g.n) }, kids);
|
|
171
|
+
if (shape) parts.van = shape;
|
|
172
|
+
parts.points[who] = p; layer.append(p);
|
|
173
|
+
if (!still) grab(p, who, layer);
|
|
174
|
+
if ((spec.flag || spec.challenge || spec.find != null || spec.estimate != null) && !shape) parts.flags[who] = flag(who);
|
|
175
|
+
}
|
|
176
|
+
if (spec.find != null || goalFlags()) parts.flags.find = flag('find');
|
|
177
|
+
// The second flag and the flag of the number to find lie under the point's, so their poles pass behind its flag
|
|
178
|
+
// when they stack.
|
|
179
|
+
nodes.push(...['find', 'second', 'point'].filter(w => parts.flags[w]).map(w => parts.flags[w].el));
|
|
180
|
+
if (spec.start != null) nodes.push(ring());
|
|
181
|
+
nodes.push(layer);
|
|
182
|
+
if (spec.written) { parts.written = null; parts.writtenAt = el('g', { class: 'line-written' }); nodes.push(parts.writtenAt); }
|
|
183
|
+
if (parts.keys) nodes.push(...parts.keys);
|
|
184
|
+
if (spec.cards) nodes.push(cards());
|
|
185
|
+
if (g.narrow) { parts.lens = makeLens(); nodes.push(parts.lens.el); }
|
|
186
|
+
svg.replaceChildren(...nodes);
|
|
187
|
+
paint();
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// gaps makes a target over each gap of the line. A tap opens the gap under the line as a line of its own, from
|
|
191
|
+
// its left mark to its right mark with a mark for every number between, and a tap on the open gap closes it.
|
|
192
|
+
function gaps() {
|
|
193
|
+
const g = geo, y0 = g.y0, hits = [];
|
|
194
|
+
parts.opened = el('g', { class: 'line-opened' });
|
|
195
|
+
for (let k = 0; k < g.n; k++) {
|
|
196
|
+
const t = el('rect', { class: 'line-gap', x: g.x(k), y: y0 - 22, width: g.gap, height: 22 + g.labelY + g.font / 2, tabindex: 0, role: 'button', 'data-gap': k });
|
|
197
|
+
const act = () => { s = open(s, k); paint(); };
|
|
198
|
+
t.addEventListener('click', act);
|
|
199
|
+
t.addEventListener('keydown', e => { if (e.key !== 'Enter' && e.key !== ' ') return; e.preventDefault(); e.stopPropagation(); act(); });
|
|
200
|
+
hits.push(t);
|
|
201
|
+
}
|
|
202
|
+
parts.gaps = hits;
|
|
203
|
+
return [parts.opened, ...hits];
|
|
204
|
+
}
|
|
205
|
+
// drawOpen draws the open gap again under the line, at the width of the stage, joined to its marks above.
|
|
206
|
+
function drawOpen(st) {
|
|
207
|
+
const g = geo, y0 = g.y0;
|
|
208
|
+
for (const t of parts.gaps) { const k = Number(t.dataset.gap), a = num(k, st), b = num(k + 1, st); t.setAttribute('aria-label', T('open-gap', { a: int(a), b: int(b) })); t.setAttribute('aria-pressed', st.open === k); }
|
|
209
|
+
if (st.open === null) return parts.opened.replaceChildren();
|
|
210
|
+
const k = st.open, a = num(k, st), b = num(k + 1, st), m = b - a, x1 = g.x(0), x2 = g.x(g.n), y = y0 + g.openY, x = j => x1 + (x2 - x1) * j / m;
|
|
211
|
+
const below = y0 + g.labelY + g.font / 2 + 4, ws = Array.from({ length: m + 1 }, (_, j) => int(a + j)), fit = labelRows(wide(ws), (x2 - x1) / m, { base: 14, least: FONT, apart: APART });
|
|
212
|
+
const kids = [
|
|
213
|
+
el('line', { class: 'line-funnel', x1: g.x(k), y1: below, x2: x1, y2: y - LONG }), el('line', { class: 'line-funnel', x1: g.x(k + 1), y1: below, x2: x2, y2: y - LONG }),
|
|
214
|
+
el('line', { class: 'line-open-gap', x1: g.x(k), x2: g.x(k + 1), y1: y0, y2: y0 }),
|
|
215
|
+
el('line', { class: 'line-axis', x1, x2, y1: y, y2: y }),
|
|
216
|
+
];
|
|
217
|
+
for (let j = 0; j <= m; j++) {
|
|
218
|
+
kids.push(el('line', { class: 'line-mark', x1: x(j), x2: x(j), y1: y - 9, y2: y + 9 }));
|
|
219
|
+
kids.push(el('text', { class: 'line-label', x: x(j), y: y + 22 + (fit.rows === 2 && j % 2 ? fit.font + 3 : 0), 'font-size': fit.font }, [ws[j]]));
|
|
220
|
+
}
|
|
221
|
+
parts.opened.replaceChildren(...kids);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// cards lays the label cards out in their row under the line. A card the reader drops on its own blank mark stays
|
|
225
|
+
// there as its label, and a card dropped anywhere else slides back to the row. By keys, Enter picks a card up, Left
|
|
226
|
+
// and Right carry it from one blank box to the next, Enter lays it and Escape puts it back.
|
|
227
|
+
function cards() {
|
|
228
|
+
const layer = el('g', { class: 'line-cards' }), boxes = [...spec.blank].sort((a, b) => a - b);
|
|
229
|
+
parts.cards = s.cards.map((n, i) => {
|
|
230
|
+
const face = el('g', {}, card(int(n))), g = el('g', { class: 'line-card', tabindex: s.placed.includes(n) ? -1 : 0, role: 'button', 'aria-label': T('card', int(n)) }, [face]), c = { n, i, g, face, at: s.placed.includes(n) ? sitting(n) : homeOf(i) };
|
|
231
|
+
let dx = 0, dy = 0;
|
|
232
|
+
drag(g, {
|
|
233
|
+
point: e => svgPoint(svg, e), raise: () => layer.append(g),
|
|
234
|
+
start(p) { if (s.placed.includes(n)) return false; c.tween?.pause(); held = { card: c, from: p, moved: false }; dx = c.at.x - p.x; dy = c.at.y - p.y; },
|
|
235
|
+
move(p) {
|
|
236
|
+
if (!held) return;
|
|
237
|
+
if (!held.moved && Math.hypot(p.x - held.from.x, p.y - held.from.y) < STILL) return;
|
|
238
|
+
held.moved = true; c.at = { x: p.x + dx, y: p.y + dy }; put(c);
|
|
239
|
+
},
|
|
240
|
+
drop() { if (!held) return; held = null; layDown(c, blankAt(c.at)); },
|
|
241
|
+
});
|
|
242
|
+
// Raising the card moves it in the page, which takes its focus, so the focus comes back and blur lets it be.
|
|
243
|
+
let raising = false;
|
|
244
|
+
const carry = () => { const m = boxes[kb.i]; c.at = { x: geo.x((m - spec.from) / spec.step), y: geo.y0 + geo.labelY - CARD.h * 0.7 }; put(c); };
|
|
245
|
+
g.addEventListener('keydown', e => {
|
|
246
|
+
const side = { ArrowLeft: -1, ArrowRight: 1 }[e.key];
|
|
247
|
+
if (e.key === 'Enter' || e.key === ' ') {
|
|
248
|
+
e.preventDefault(); e.stopPropagation();
|
|
249
|
+
if (s.placed.includes(n)) return;
|
|
250
|
+
if (kb?.card === c) { const m = boxes[kb.i]; kb = null; return layDown(c, m); }
|
|
251
|
+
if (kb) home(kb.card);
|
|
252
|
+
kb = { card: c, i: 0 }; c.tween?.pause();
|
|
253
|
+
raising = true; layer.append(g); g.focus(); raising = false;
|
|
254
|
+
carry();
|
|
255
|
+
} else if (side) {
|
|
256
|
+
// Left and Right stop at every card, carried or not, so the book does not turn the page.
|
|
257
|
+
e.preventDefault(); e.stopPropagation();
|
|
258
|
+
if (kb?.card === c) { kb.i = clamp(kb.i + side, 0, boxes.length - 1); carry(); }
|
|
259
|
+
} else if (e.key === 'Escape' && kb?.card === c) {
|
|
260
|
+
e.preventDefault(); e.stopPropagation(); kb = null; home(c);
|
|
261
|
+
}
|
|
262
|
+
});
|
|
263
|
+
g.addEventListener('blur', () => { if (!raising && kb?.card === c) { kb = null; home(c); } });
|
|
264
|
+
put(c); layer.append(g);
|
|
265
|
+
return c;
|
|
266
|
+
});
|
|
267
|
+
return layer;
|
|
268
|
+
}
|
|
269
|
+
// layDown lays a card on the blank mark m. It stays on its own mark, and goes back to the row from any other.
|
|
270
|
+
function layDown(c, m) {
|
|
271
|
+
const was = s.placed.length;
|
|
272
|
+
if (m !== null) s = lay(spec, s, c.n, m);
|
|
273
|
+
if (s.placed.length > was) { c.at = sitting(c.n); put(c); c.g.setAttribute('tabindex', -1); if (moving()) motion.pop(c.face); paint(); return; }
|
|
274
|
+
home(c);
|
|
275
|
+
}
|
|
276
|
+
// A card that sits on its mark stands in the mark's label box.
|
|
277
|
+
const sitting = n => ({ x: geo.x((n - spec.from) / spec.step), y: geo.y0 + geo.labelY });
|
|
278
|
+
// The row's places are measured from the line, as the rest of the layout is.
|
|
279
|
+
const homeOf = i => { const p = geo.home(i); return { x: p.x, y: p.y + geo.y0 }; };
|
|
280
|
+
const put = c => c.g.setAttribute('transform', `translate(${c.at.x} ${c.at.y})`);
|
|
281
|
+
// home slides a card back to its place in the row.
|
|
282
|
+
function home(c) {
|
|
283
|
+
const to = homeOf(c.i);
|
|
284
|
+
if (!moving()) { c.at = { ...to }; put(c); return; }
|
|
285
|
+
c.tween = fx.run(c.at, { ...to, duration: 260, ease: 'outCubic', onUpdate: () => put(c) });
|
|
286
|
+
}
|
|
287
|
+
// blankAt gives the number of the blank mark nearest a card's place, or null when the card is far from them all.
|
|
288
|
+
function blankAt(p) {
|
|
289
|
+
const g = geo, reach = Math.max(g.gap / 2, BOX.w / 2 + 6);
|
|
290
|
+
let best = null, far = Infinity;
|
|
291
|
+
for (const n of spec.blank) { const d = Math.abs(g.x((n - spec.from) / spec.step) - p.x); if (d < far) { far = d; best = n; } }
|
|
292
|
+
return far <= reach && Math.abs(p.y - (g.y0 + g.labelY)) < CARD.h + 10 ? best : null;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
// grab lets the reader drag a point along the line. It follows the finger and snaps to the nearest mark when the
|
|
296
|
+
// reader lets go. The arrow keys move a focused point one mark.
|
|
297
|
+
function grab(node, who, layer) {
|
|
298
|
+
drag(node, {
|
|
299
|
+
point: e => svgPoint(svg, e), raise: () => layer.append(node),
|
|
300
|
+
start(p, e) {
|
|
301
|
+
// Where the second point's touch area lies over the start's ring, a press nearer the start's mark goes to the
|
|
302
|
+
// ring, which takes the same pointer for its drag.
|
|
303
|
+
if (who === 'second' && parts.ring && Math.abs(p.x - xm(s.base)) < Math.abs(p.x - xm(s.second))) {
|
|
304
|
+
parts.ring.dispatchEvent(new PointerEvent('pointerdown', { pointerId: e.pointerId, pointerType: e.pointerType, isPrimary: e.isPrimary, clientX: e.clientX, clientY: e.clientY, button: 0, buttons: 1, bubbles: true }));
|
|
305
|
+
return false;
|
|
306
|
+
}
|
|
307
|
+
settle(); held = { who, x: xm(onMark(who)), from: p.x, moved: false };
|
|
308
|
+
},
|
|
309
|
+
move(p) {
|
|
310
|
+
if (!held) return;
|
|
311
|
+
if (!held.moved && Math.abs(p.x - held.from) < STILL) return;
|
|
312
|
+
held.moved = true; held.x = clamp(p.x, who === 'second' ? xm(secondTo(spec, s, 0)) : geo.x(0), geo.x(geo.n)); look(true); paint();
|
|
313
|
+
},
|
|
314
|
+
drop() {
|
|
315
|
+
if (!held) return;
|
|
316
|
+
const h0 = held; held = null;
|
|
317
|
+
if (!h0.moved) return paint();
|
|
318
|
+
// On an unmarked line the point stays where it is let go, and on a marked line it snaps to the nearest mark.
|
|
319
|
+
const k = nearest(h0.x);
|
|
320
|
+
s = place(s, who, who === 'second' ? secondTo(spec, s, k) : k); look(false);
|
|
321
|
+
if (isFree(spec)) paint(); else go({ who, from: h0.x, to: xm(k), hop: false }, 140);
|
|
322
|
+
},
|
|
323
|
+
});
|
|
324
|
+
nudge(node, { step(dx, dy) { settle(); const k = clamp(onMark(who) + (dx || -dy), 0, geo.n); s = place(s, who, who === 'second' ? secondTo(spec, s, k) : k); look(false); paint(); } });
|
|
325
|
+
}
|
|
326
|
+
// handles makes a touch band for each mark between the ends, as wide as the stretch halfway to each neighbour and
|
|
327
|
+
// at least 44px tall, under the point so the point keeps its own dot. A dragged mark slides between its neighbours
|
|
328
|
+
// and stays where the reader lets it go. Each mark also has a key stand-in after the point, so Tab reaches the point
|
|
329
|
+
// first and then the marks from left to right, and the arrow keys move a focused mark a tenth of a gap.
|
|
330
|
+
function handles() {
|
|
331
|
+
const g = geo, top = g.y0 - LONG, h = Math.max(44, g.labelY + g.font / 2 + 6 + LONG);
|
|
332
|
+
parts.handles = new Map(); parts.keys = [];
|
|
333
|
+
// A mark stops where its label's drawn box would come closer than 10px to its neighbour's. Half a pixel on each
|
|
334
|
+
// side covers the text landing on fractions of a pixel.
|
|
335
|
+
const keep = labelKeep(spec, area.w, j => (parts.labels.get(j)?.getBBox().width ?? 0) + 1);
|
|
336
|
+
const out = [];
|
|
337
|
+
for (let k = 1; k < g.n; k++) {
|
|
338
|
+
const band = el('rect', { class: 'line-band', y: top, height: h }), node = el('g', { class: 'line-handle', 'data-mark': k }, [band]);
|
|
339
|
+
drag(node, {
|
|
340
|
+
point: e => svgPoint(svg, e),
|
|
341
|
+
start(p) { settle(); held = { mark: k, dx: xm(k) - p.x, from: p.x, moved: false }; },
|
|
342
|
+
move(p) {
|
|
343
|
+
if (!held) return;
|
|
344
|
+
if (!held.moved && Math.abs(p.x - held.from) < STILL) return;
|
|
345
|
+
held.moved = true; s = slide(spec, s, k, (p.x + held.dx - g.x0) / g.gap, keep); paint();
|
|
346
|
+
},
|
|
347
|
+
drop() { if (!held) return; held = null; s = settleMarks(s); paint(); },
|
|
348
|
+
});
|
|
349
|
+
const key = el('g', { class: 'line-mark-key', tabindex: 0, role: 'slider', 'aria-label': T('mark-name', int(num(k))), 'aria-valuenow': num(k), 'aria-valuemin': num(k - 1), 'aria-valuemax': num(k + 1) });
|
|
350
|
+
key.addEventListener('focus', () => node.classList.add('line-focus'));
|
|
351
|
+
key.addEventListener('blur', () => node.classList.remove('line-focus'));
|
|
352
|
+
nudge(key, { step(dx) { if (!dx) return; s = settleMarks(slide(spec, s, k, posOf(s, k) + dx * SETTLE, keep)); paint(); } });
|
|
353
|
+
parts.handles.set(k, { g: key, band });
|
|
354
|
+
parts.keys.push(key);
|
|
355
|
+
out.push(node);
|
|
356
|
+
}
|
|
357
|
+
return out;
|
|
358
|
+
}
|
|
359
|
+
// ring draws the start of the jumps: a ring in the point's colour round its mark, larger than the point, with a
|
|
360
|
+
// touch area about 44px across. A drag or the arrow keys move it one mark at a time and carry the point and its
|
|
361
|
+
// arcs along. The start of a challenge stays where it is.
|
|
362
|
+
function ring() {
|
|
363
|
+
const fixed = !!spec.challenge;
|
|
364
|
+
const node = el('g', { class: `line-start line-c-point${fixed ? '' : ' line-moves'}`, 'data-ref': 'start', tabindex: fixed ? null : 0, role: fixed ? null : 'slider', 'aria-label': T('start'), 'aria-valuemin': num(0), 'aria-valuemax': num(geo.n) }, [
|
|
365
|
+
el('circle', { class: 'line-hit', r: 22 }), el('circle', { class: 'line-ring', r: geo.r + 7 }),
|
|
366
|
+
]);
|
|
367
|
+
parts.ring = node;
|
|
368
|
+
if (fixed) return node;
|
|
369
|
+
const to = k => { settle(); s = moveStart(spec, s, k); paint(); };
|
|
370
|
+
drag(node, {
|
|
371
|
+
point: e => svgPoint(svg, e),
|
|
372
|
+
start() { settle(); held = { start: true }; },
|
|
373
|
+
move(p) { if (held?.start) to(Math.round(clamp((p.x - geo.x0) / geo.gap, 0, geo.n))); },
|
|
374
|
+
drop() { held = null; paint(); },
|
|
375
|
+
});
|
|
376
|
+
nudge(node, { step(dx) { if (dx) to(s.base + dx); } });
|
|
377
|
+
return node;
|
|
378
|
+
}
|
|
379
|
+
const onMark = who => who === 'second' ? s.second : s.at;
|
|
380
|
+
// nearest gives the mark a point snaps to: the nearest where the marks stand, or the place itself on an unmarked line.
|
|
381
|
+
function nearest(x) {
|
|
382
|
+
if (spec.moved) return Array.from({ length: geo.n + 1 }, (_, k) => k).reduce((a, b) => Math.abs(xm(b) - x) < Math.abs(xm(a) - x) ? b : a);
|
|
383
|
+
const k = clamp((x - geo.x0) / geo.gap, 0, geo.n);
|
|
384
|
+
return isFree(spec) ? k : Math.round(k);
|
|
385
|
+
}
|
|
386
|
+
// xm gives the place of mark k where it stands, off its place on a page with moved marks.
|
|
387
|
+
const xm = (k, st = s) => geo.x(posOf(st, k));
|
|
388
|
+
// goalFlags says whether the page's goal shows where its number is once it holds.
|
|
389
|
+
const goalFlags = () => !!(spec.challenge && GOALS[spec.challenge.goal].flags);
|
|
390
|
+
// look shows the lens while the reader drags, and for a moment after the point lands, so the mark can be read.
|
|
391
|
+
function look(dragging) {
|
|
392
|
+
if (!geo?.narrow) return;
|
|
393
|
+
looking = true; clearTimeout(timer);
|
|
394
|
+
if (!dragging) timer = setTimeout(() => { looking = false; paint(); }, LENS_STAYS);
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
// press jumps the point one gap, and the frog hops with it.
|
|
398
|
+
function press(dir) {
|
|
399
|
+
if (held) return;
|
|
400
|
+
settle();
|
|
401
|
+
const next = jump(spec, s, dir);
|
|
402
|
+
if (!next) return;
|
|
403
|
+
const from = geo ? xm(s.at) : null, adds = jumpsOf(next) > jumpsOf(s);
|
|
404
|
+
s = next; look(false);
|
|
405
|
+
if (geo) go({ who: 'point', from, to: xm(s.at), hop: isStones(spec), adds }, 320);
|
|
406
|
+
else refresh();
|
|
407
|
+
}
|
|
408
|
+
// go runs a glide of a point from one place to the next. Without motion the point is there at once.
|
|
409
|
+
function go(g, duration) {
|
|
410
|
+
if (!moving()) { paint(); return; }
|
|
411
|
+
glide = { ...g, o: { t: 0 } };
|
|
412
|
+
const end = () => { glide = null; paint(); };
|
|
413
|
+
fx.run(glide.o, { t: 1, duration, ease: 'inOutSine', onUpdate: paint }, end, end);
|
|
414
|
+
paint();
|
|
415
|
+
}
|
|
416
|
+
// settle ends a glide on its way, so a new press starts from where the point stands.
|
|
417
|
+
function settle() { if (glide) fx.stop(); }
|
|
418
|
+
|
|
419
|
+
// where gives the place a point is drawn: under the finger, on its way, or on its mark.
|
|
420
|
+
function where(who) {
|
|
421
|
+
if (held?.who === who && held.moved) return { x: held.x, lift: 0 };
|
|
422
|
+
if (glide?.who === who) { const t = glide.o.t; return { x: glide.from + (glide.to - glide.from) * t, lift: glide.hop ? Math.sin(Math.PI * t) * geo.arcH * 1.6 : 0 }; }
|
|
423
|
+
return { x: xm(onMark(who)), lift: 0 };
|
|
424
|
+
}
|
|
425
|
+
// now is the state the readouts show: a point under the finger reads the mark it would snap to.
|
|
426
|
+
const now = () => held?.who && held.moved ? place(s, held.who, held.who === 'second' ? secondTo(spec, s, nearest(held.x)) : nearest(held.x)) : s;
|
|
427
|
+
|
|
428
|
+
function paint() {
|
|
429
|
+
if (!spec) return;
|
|
430
|
+
solved = holds().done;
|
|
431
|
+
if (geo && parts) draw();
|
|
432
|
+
refresh();
|
|
433
|
+
}
|
|
434
|
+
function draw() {
|
|
435
|
+
const g = geo, y0 = g.y0, st = now();
|
|
436
|
+
// The arcs run over the gaps the point has jumped. The newest one draws itself while the point is on its way.
|
|
437
|
+
const arcs = arcsOf(st).flatMap((a, i, all) => {
|
|
438
|
+
const down = a.dir < 0, y = y0 + (down ? g.downY : g.arcY), part = arc(xm(a.from, st), xm(a.to, st), y, g.arcH, down);
|
|
439
|
+
if (glide?.adds && i === all.length - 1) { part.path.setAttribute('stroke-dasharray', '1 1'); part.path.setAttribute('stroke-dashoffset', 1 - glide.o.t); part.head.setAttribute('opacity', glide.o.t > 0.9 ? 1 : 0); }
|
|
440
|
+
return g.narrow ? [part.path] : [part.path, part.head];
|
|
441
|
+
});
|
|
442
|
+
parts.arcs.replaceChildren(...arcs);
|
|
443
|
+
const at = {};
|
|
444
|
+
for (const who of Object.keys(parts.points)) {
|
|
445
|
+
const p = where(who), n = num(who === 'second' ? st.second : st.at);
|
|
446
|
+
at[who] = { x: p.x, words: int(Math.round(n)) };
|
|
447
|
+
parts.points[who].setAttribute('transform', `translate(${p.x} ${y0})`);
|
|
448
|
+
// A point that carries no number tells a screen reader where it stands, on a number or between two.
|
|
449
|
+
// The fixed point of an estimate page keeps its number to itself until Show writes it.
|
|
450
|
+
if (spec.estimate != null) { if (st.shown) parts.points[who].setAttribute('aria-valuenow', Math.round(n)); else parts.points[who].removeAttribute('aria-valuenow'); }
|
|
451
|
+
else if (numbered(spec)) parts.points[who].setAttribute('aria-valuenow', Math.round(n));
|
|
452
|
+
else {
|
|
453
|
+
const w = standing(spec, who === 'second' ? { ...st, at: st.second } : st);
|
|
454
|
+
parts.points[who].setAttribute('aria-valuenow', Math.round(n * 100) / 100);
|
|
455
|
+
parts.points[who].setAttribute('aria-valuetext', w.between ? T('between', { a: int(w.between[0]), b: int(w.between[1]) }) : int(w.on));
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
if (parts.frog) {
|
|
459
|
+
const p = where('point');
|
|
460
|
+
parts.frog.setAttribute('transform', `translate(${p.x} ${y0 + g.stoneY - g.sw * 0.1 - p.lift})`);
|
|
461
|
+
}
|
|
462
|
+
// A second flag that would cover the first stands one place higher. A challenge's flag shows while the goal holds.
|
|
463
|
+
// Show reveals the number at the point on its flag, or on the van, and a flag over the place of the number to find.
|
|
464
|
+
const fl = parts.flags, from = y0 + g.flagFrom, near = (who, x, words) => fl.point && Math.abs(at.point.x - x) < (fl.point.width(at.point.words) + fl[who].width(words)) / 2 + 4;
|
|
465
|
+
if (fl.point) { fl.point.set(at.point.words, from, POLE); fl.point.el.setAttribute('display', spec.flag || ((solved || st.shown) && numbered(spec) && spec.challenge?.goal !== 'jump-to') ? 'inline' : 'none'); }
|
|
466
|
+
if (fl.second) fl.second.set(at.second.words, from, POLE + (near('second', at.second.x, at.second.words) ? STACK : 0));
|
|
467
|
+
if (fl.find) {
|
|
468
|
+
const n = spec.challenge ? target.target : findsOf(spec)[st.find], open = st.shown || (solved && goalFlags());
|
|
469
|
+
at.find = { x: g.x(markOf(spec, n, st.step)), words: int(n) };
|
|
470
|
+
fl.find.set(at.find.words, from, POLE + (open && near('find', at.find.x, at.find.words) ? STACK : 0));
|
|
471
|
+
fl.find.el.setAttribute('display', open ? 'inline' : 'none');
|
|
472
|
+
}
|
|
473
|
+
if (parts.van) parts.van.say(st.shown ? at.point.words : null);
|
|
474
|
+
if (parts.middle) parts.middle.setAttribute('display', st.middle ? 'inline' : 'none');
|
|
475
|
+
// The estimate mark stays hidden while the reader sets the estimate, and Show draws it at the estimate's place.
|
|
476
|
+
if (parts.estimate) { parts.estimate.setAttribute('transform', `translate(${g.x(markOf(spec, st.estimate, st.step))} 0)`); parts.estimate.setAttribute('display', st.shown ? 'inline' : 'none'); }
|
|
477
|
+
// A line whose labels show once its goal holds makes those marks long with them, as a labelled mark is long.
|
|
478
|
+
if (spec.reveal) for (const [k, m] of parts.marks) { const long = shown(k, st), half = long ? LONG : SHORT; m.setAttribute('class', `line-mark${long ? '' : ' line-short'}`); m.setAttribute('y1', y0 - half); m.setAttribute('y2', y0 + half); }
|
|
479
|
+
for (const who of Object.keys(fl)) fl[who].el.setAttribute('transform', `translate(${at[who].x} 0)`);
|
|
480
|
+
// The labels a challenge keeps back show once its goal holds. The bracket lights the labels of its two tens.
|
|
481
|
+
const tens = spec.bracket && st.at !== null ? tensOf(spec, st) : null, lit = tens ? [tens.before, tens.after] : [];
|
|
482
|
+
for (const [k, t] of parts.labels) if (t.tagName === 'text') { t.setAttribute('display', shown(k, st) ? 'inline' : 'none'); t.classList.toggle('line-lit', lit.includes(num(k, st))); }
|
|
483
|
+
if (parts.bracket) parts.bracket.set(tens ? g.x((tens.before - spec.from) / spec.step) : null, tens ? g.x((tens.after - spec.from) / spec.step) : null, y0 - g.r - 4);
|
|
484
|
+
if (parts.gaps) drawOpen(st);
|
|
485
|
+
if (parts.lens && parts.points.point) {
|
|
486
|
+
// The lens gives way to the flags of a goal reached or of Show.
|
|
487
|
+
const show = looking && !solved && !st.shown;
|
|
488
|
+
parts.lens.show(show);
|
|
489
|
+
if (show) {
|
|
490
|
+
const x = at.point.x;
|
|
491
|
+
parts.lens.at(x, y0 + g.lensY, 4, area.w - 4);
|
|
492
|
+
parts.lens.draw((x - g.x0) / g.gap, g.n, k => shown(k, st) ? int(num(k, st)) : null, arcsOf(st), street());
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
// The ring of the start stands round its mark. The written line follows the jumps, and a challenge shows it once
|
|
496
|
+
// its goal holds. Its numbers take the colour of the point or the second point they write.
|
|
497
|
+
if (parts.ring) { parts.ring.setAttribute('transform', `translate(${xm(st.base, st)} ${y0})`); parts.ring.setAttribute('aria-valuenow', num(st.base, st)); }
|
|
498
|
+
if (parts.writtenAt) {
|
|
499
|
+
const w = writtenOf(spec, st), order = `${w.ids.a}|${w.ids.b}`;
|
|
500
|
+
if (parts.writtenOrder !== order) {
|
|
501
|
+
const cls = Object.fromEntries(Object.entries(w.ids).filter(([, id]) => id === 'start' || id === 'second').map(([role, id]) => [role, id === 'start' ? 'line-c-point' : 'line-c-second']));
|
|
502
|
+
parts.written = writtenLine({ refs: w.ids, cls }); parts.writtenOrder = order;
|
|
503
|
+
parts.writtenAt.replaceChildren(parts.written.el); parts.written.at(area.w / 2, y0 + g.writtenY);
|
|
504
|
+
}
|
|
505
|
+
parts.written.set(spec.challenge && !solved ? null : w, int);
|
|
506
|
+
}
|
|
507
|
+
if (parts.reading) fit(parts.reading, spec.moved ? T(gapsKey(st)) : words(st), area.w - 24);
|
|
508
|
+
// The marks, their labels and their touch bands stand where the marks stand.
|
|
509
|
+
if (spec.moved) {
|
|
510
|
+
for (const [k, m] of parts.marks) { m.setAttribute('x1', xm(k, st)); m.setAttribute('x2', xm(k, st)); parts.labels.get(k)?.setAttribute('x', xm(k, st)); }
|
|
511
|
+
for (const [k, h] of parts.handles) {
|
|
512
|
+
const a = (xm(k - 1, st) + xm(k, st)) / 2, b = (xm(k, st) + xm(k + 1, st)) / 2;
|
|
513
|
+
h.band.setAttribute('x', a); h.band.setAttribute('width', b - a);
|
|
514
|
+
h.g.setAttribute('aria-valuetext', T(sides(st, k)));
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
if (jumps) { jumps.left.disabled = !jump(spec, s, -1); jumps.right.disabled = !jump(spec, s, 1); }
|
|
518
|
+
if (acts.show) acts.show.disabled = s.shown;
|
|
519
|
+
if (acts.next) acts.next.disabled = !s.shown;
|
|
520
|
+
acts.middle?.setAttribute('aria-pressed', s.middle);
|
|
521
|
+
}
|
|
522
|
+
// fit writes the line of words and makes its type smaller where it would run past the stage's sides.
|
|
523
|
+
function fit(text, said, room) {
|
|
524
|
+
if (text.textContent === said) return;
|
|
525
|
+
text.textContent = said; text.style.fontSize = '';
|
|
526
|
+
const long = text.getComputedTextLength();
|
|
527
|
+
if (long > room) text.style.fontSize = `${Math.max(12, 20 * room / long)}px`;
|
|
528
|
+
}
|
|
529
|
+
const words = st => { const r = readingOf(spec, st); return T(r.key, { a: int(r.a), b: int(r.b) }); };
|
|
530
|
+
|
|
531
|
+
// goalWords writes a goal's hint or note: its string with its numbers in the page's language, and the calculation of
|
|
532
|
+
// jump-to as the kit writes it.
|
|
533
|
+
const goalWords = ({ key, vars }) => T(key, { ...Object.fromEntries(Object.entries(vars).map(([k, v]) => [k, int(v)])), calc: target?.sign ? writeWords({ a: target.start, sign: target.sign, b: target.n, open: true }, int) : '' });
|
|
534
|
+
|
|
535
|
+
// holds asks the page's goal whether the point is on the target, once the reader has pressed Check.
|
|
536
|
+
const holds = () => goal ? GOALS[spec.challenge.goal].holds({ checked: s.checked, point: num(s.at) }, spec, spec.challenge, target) : { done: false };
|
|
537
|
+
|
|
538
|
+
// refresh writes every value that follows the figure, in the readouts, the challenge's line and the text. Every
|
|
539
|
+
// goal speaks with the line and the notes of place.
|
|
540
|
+
function refresh() {
|
|
541
|
+
const st = now(), c = counts(spec, st), ids = measuresOf(spec), w = writtenOf(spec, st), value = id => id === 'gaps' ? T(gapsKey(st)) : id === 'written' ? writeWords(w, int) : id === 'reading' ? (w ? writeReading(w, int, T) : words(st)) : c[id] === null ? DASH : int(c[id]);
|
|
542
|
+
const shows = id => id === 'placed' ? T('placed-of', { n: int(c.placed), all: int(spec.blank.length) }) : value(id);
|
|
543
|
+
for (const k of chips) if (k.set(shows(k.id)) && moving() && !held) motion.flash(k.el);
|
|
544
|
+
if (goal) {
|
|
545
|
+
const r = holds();
|
|
546
|
+
// A goal with a done line of its own says it, such as place-unmarked, which holds near the number.
|
|
547
|
+
// jump-to names its calculation without the answer, from the kit's written line, and has notes of its own.
|
|
548
|
+
const name = spec.challenge.goal, says = { target: int(target.target) }, jumpTo = name === 'jump-to';
|
|
549
|
+
const asks = jumpTo ? T('goal-jump', { start: int(target.start), calc: writeWords({ a: target.start, sign: target.sign, b: target.n, open: true }, int) }) : T(`goal-${name}`, says) ?? T('goal-place', says);
|
|
550
|
+
goal.say(r.done ? T(`goal-${name}-done`, says) ?? T('goal-place-done', says) : asks);
|
|
551
|
+
if (goal.show(r.done, r.note ? goalWords(GOALS[name].note(target, r.note, { point: num(s.at) })) : undefined) && moving() && parts?.points.point) motion.pop(parts.points.point.lastElementChild);
|
|
552
|
+
}
|
|
553
|
+
live(host, { value: id => ids.includes(id) ? value(id) : null });
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
// The panel holds the challenge's line, the step picker, the jump buttons, Check and the readouts. The two jump
|
|
557
|
+
// buttons keep to one row.
|
|
558
|
+
function drawPanel() {
|
|
559
|
+
const jumper = (dir, key, sign) => {
|
|
560
|
+
const b = h('button', { type: 'button', class: 'hesap-btn line-jump', 'data-act': key }, dir < 0 ? [h('b', { 'aria-hidden': 'true' }, sign), h('span', {}, T(key))] : [h('span', {}, T(key)), h('b', { 'aria-hidden': 'true' }, sign)]);
|
|
561
|
+
b.addEventListener('click', () => press(dir));
|
|
562
|
+
return b;
|
|
563
|
+
};
|
|
564
|
+
jumps = spec.jump ? { left: jumper(-1, 'jump-left', '−'), right: jumper(1, 'jump-right', '+') } : null;
|
|
565
|
+
const ctls = [];
|
|
566
|
+
// A pick of the step keeps the gaps, the point's mark and its jumps, and writes every label again.
|
|
567
|
+
if (spec.stepCtl) ctls.push(segment({ id: 'step', label: T('step'), items: spec.stepCtl.items.map(v => [v, int(v)]), value: s.step, pick: v => { s = pick(s, Number(v)); build(); } }));
|
|
568
|
+
if (jumps) ctls.push(h('div', { class: 'line-jumps' }, [jumps.left, jumps.right]));
|
|
569
|
+
// Middle draws the middle's mark and takes it away. Show reveals where the number to find is, and Next asks for
|
|
570
|
+
// the next number of the list once the reader has seen this one.
|
|
571
|
+
acts = {};
|
|
572
|
+
const act = (key, change, primary = false) => button({ label: T(key), act: key, primary, press: () => { settle(); s = change(s); paint(); } });
|
|
573
|
+
if (spec.middle === 'button') ctls.push(acts.middle = act('middle', toggleMiddle));
|
|
574
|
+
// The estimate steps or slides, and moves its mark. The book names its Show button for the book.
|
|
575
|
+
if (spec.estimateCtl) {
|
|
576
|
+
const c = spec.estimateCtl, opts = { id: 'estimate', label: T('estimate'), min: c.lo, max: c.hi, value: s.estimate, write: int };
|
|
577
|
+
const change = v => { s = setEstimate(s, v); paint(); };
|
|
578
|
+
ctls.push(c.kind === 'stepper' ? stepper({ ...opts, change }) : slider({ ...opts, input: change }));
|
|
579
|
+
}
|
|
580
|
+
if (spec.find != null || spec.estimate != null) ctls.push(acts.show = act(spec.look === 'book' ? 'open-book' : 'show', reveal, true));
|
|
581
|
+
if (findsOf(spec).length > 1) ctls.push(acts.next = act('next', st => next(spec, st)));
|
|
582
|
+
// New line draws another line with its labels, boxes and cards, and takes every placed card back.
|
|
583
|
+
if (base.draw) ctls.push(button({ label: T('new-line'), act: 'new-line', press: () => { deal(); drawPanel(); build(); } }));
|
|
584
|
+
goal = null;
|
|
585
|
+
if (spec.challenge) {
|
|
586
|
+
const name = spec.challenge.goal;
|
|
587
|
+
goal = goalRow({ T, motion: moving() ? motion : null, hint: on => on ? goalWords(GOALS[name].hint(target, spec)) : null, another: () => { deal(); drawPanel(); build(); } });
|
|
588
|
+
ctls.push(check({ T, press: () => { if (!s.checked) { s = { ...s, checked: true }; paint(); } } }));
|
|
589
|
+
}
|
|
590
|
+
// A find page shows the number to place in a Find readout of its own, before the readouts it names.
|
|
591
|
+
const ids = [...new Set([...(spec.find != null ? ['find'] : []), ...spec.readout])].filter(id => readoutsOf(spec).includes(id));
|
|
592
|
+
// The written line's words take the kit's name, In words.
|
|
593
|
+
chips = ids.map(id => chip({ id, label: T(id === 'reading' && spec.written ? 'written-reading' : id), short: T(`${id}-short`), cls: ['written', 'reading'].includes(id) ? 'line-chip-wide' : '' }));
|
|
594
|
+
for (const k of chips) k.width(k.id === 'placed' ? 6 : 3);
|
|
595
|
+
panel.replaceChildren(...[goal?.el, ctls.length ? row('ctls', ctls) : null, chips.length ? row('out', chips) : null].filter(Boolean));
|
|
596
|
+
}
|
|
597
|
+
// deal sets the page's state as it opens. A challenge draws its target, another one each time, and a goal that
|
|
598
|
+
// draws its line gives the page that line.
|
|
599
|
+
function deal() {
|
|
600
|
+
looking = false; clearTimeout(timer); solved = false;
|
|
601
|
+
const c = base.challenge, g = c ? GOALS[c.goal] : null;
|
|
602
|
+
target = g ? another(g, c, base, dice(fresh()), target) : null;
|
|
603
|
+
spec = g?.page ? { ...base, ...g.page(target) } : base;
|
|
604
|
+
// A page with draw takes a line of label cards drawn at random, another one at each New line.
|
|
605
|
+
if (base.draw) { drawn = drawLine(dice(fresh()), drawn); spec = { ...base, from: drawn.from, to: drawn.to, step: drawn.step, labels: drawn.labels, blank: drawn.blank, cards: drawn.cards, point: false }; }
|
|
606
|
+
s = opening(spec); kb = null;
|
|
607
|
+
// The cards of cards: true come in a new order each time, never the order of their marks.
|
|
608
|
+
if (spec.cards === true) s = { ...s, cards: shuffled(s.cards, dice(fresh())) };
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
function point(id, on) {
|
|
612
|
+
if (!spec) return;
|
|
613
|
+
for (const part of svg.querySelectorAll(`[data-ref="${id}"]`)) part.classList.toggle('line-hot', on);
|
|
614
|
+
parts?.written?.light(id, on, ['written', 'reading']);
|
|
615
|
+
for (const part of panel.querySelectorAll('[data-ref]')) part.classList.toggle('hot', on && part.dataset.ref === id);
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
return {
|
|
619
|
+
show(page) { fx.stop(); base = page; target = null; drawn = null; held = null; glide = null; deal(); drawPanel(); build(); },
|
|
620
|
+
hide() { fx.stop(); clearTimeout(timer); base = null; spec = null; geo = null; parts = null; held = null; glide = null; goal = null; svg.replaceChildren(); },
|
|
621
|
+
point,
|
|
622
|
+
lang() { if (!spec) return; const was = goal; drawPanel(); goal?.keep(was); build(); },
|
|
623
|
+
};
|
|
624
|
+
}
|