diff --git a/.claude/skills/leetcode-solution-page/references/lesson-kit.md b/.claude/skills/leetcode-solution-page/references/lesson-kit.md index f43fad9..31dfac6 100644 --- a/.claude/skills/leetcode-solution-page/references/lesson-kit.md +++ b/.claude/skills/leetcode-solution-page/references/lesson-kit.md @@ -128,7 +128,11 @@ or cell with the same `data-*` attributes, so give each clickable one a stable `data-*` key (`data-set`, `data-i`). The part 1 widget uses `presetChips(sets, active)` for its chips (the active one is `aria-pressed`, which lesson.css draws as selected; a widget's own toggle chips should set it too) and `widgetLabel(text)` for the -note beside its heading. Anything a lesson draws uses a class from `lesson.css`, `kit.css` +note beside its heading. It ends with `wireWidget(host, { render, choose })`, +which draws it now and on every language change, calls `choose(i)` then +`render()` when chip `i` is clicked, and turns Enter or space on a +`role="button"` cell into a click, so the widget has one click listener and no +keydown of its own. Anything a lesson draws uses a class from `lesson.css`, `kit.css` or its own `style.css`; never a `style="…"` string (a width that varies can be a class per step, as x-sum's tally bars are). In `page.js`'s cost table, the gloss under an approach's name is ``. A class a second lesson would diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index cf947fb..561a402 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -123,7 +123,7 @@ boxes of your own inside the player. Shapes for the stage are in `src/lib/stage.js` (`cells`, `kv`, `stack`, `chain`, `tree`, `bars`, `readout`). Small helpers are in `src/lib/kit.js` — including `intList` / `intValue` for a control's `parse`, and `presetChips` / -`widgetLabel` for the widget — and LeetCode binary trees in `src/lib/tree.js`. Use their classes, never a +`widgetLabel` / `wireWidget` for the widget — and LeetCode binary trees in `src/lib/tree.js`. Use their classes, never a `style="…"`; a class only your page needs goes in its own `style.css`. Words and calls get `cells(items, { wide: true })` and a board or matrix `labelledRows(rows, { grid: true })`, so neither needs CSS of its own. diff --git a/src/lessons/3sum/lesson.js b/src/lessons/3sum/lesson.js index c763d58..2d66a64 100644 --- a/src/lessons/3sum/lesson.js +++ b/src/lessons/3sum/lesson.js @@ -8,9 +8,9 @@ * a two-pointer squeeze, where equal neighbours can simply be stepped over. */ import { mountLesson } from '../../lib/stepper.js'; -import { pick, onLangChange } from '../../lib/i18n.js'; +import { pick } from '../../lib/i18n.js'; import { cells, kv, readout, slots, stagePanel } from '../../lib/stage.js'; -import { t, exampleTitle, LANGUAGES, k, c, stageGap, intList, presetChips, widgetLabel } from '../../lib/kit.js'; +import { t, exampleTitle, LANGUAGES, k, c, stageGap, intList, presetChips, widgetLabel, wireWidget } from '../../lib/kit.js'; const MAX_LEN = 10; @@ -535,17 +535,10 @@ function mountTripletWidget(host) { render(); } host.addEventListener('click', (ev) => { - const chip = ev.target.closest('[data-set]'); - if (chip) { state.set = Number(chip.dataset.set); state.sel = [0, 1, 2]; return render(); } const cell = ev.target.closest('[data-i]'); if (cell) toggle(Number(cell.dataset.i)); }); - host.addEventListener('keydown', (ev) => { - const cell = ev.target.closest('[data-i]'); - if (cell && (ev.key === 'Enter' || ev.key === ' ')) { ev.preventDefault(); toggle(Number(cell.dataset.i)); } - }); - onLangChange(render); - render(); + wireWidget(host, { render, choose: (i) => { state.set = i; state.sel = [0, 1, 2]; } }); } /* ---------------- the approach, in brief ---------------- */ diff --git a/src/lessons/basic-calculator/lesson.js b/src/lessons/basic-calculator/lesson.js index f20509d..1176b9e 100644 --- a/src/lessons/basic-calculator/lesson.js +++ b/src/lessons/basic-calculator/lesson.js @@ -9,9 +9,9 @@ * recursive parser), or an explicit stack of (total, sign) pairs can. */ import { mountLesson } from '../../lib/stepper.js'; -import { pick, onLangChange } from '../../lib/i18n.js'; +import { pick } from '../../lib/i18n.js'; import { cells, slots, stagePanel, stack, readout } from '../../lib/stage.js'; -import { t, exampleTitle, LANGUAGES, k, c, stageGap, presetChips, widgetLabel, stageEmpty } from '../../lib/kit.js'; +import { t, exampleTitle, LANGUAGES, k, c, stageGap, presetChips, widgetLabel, stageEmpty, wireWidget } from '../../lib/kit.js'; const MAX_LEN = 24; @@ -528,8 +528,6 @@ function mountSignWidget(host) { q('[data-total]').innerHTML = `${total}${pick(t('value', 'တန်ဖိုး'))}`; } host.addEventListener('click', (ev) => { - const chip = ev.target.closest('[data-set]'); - if (chip) { state.set = Number(chip.dataset.set); state.s = QW_SETS[state.set].s; return render(); } const op = ev.target.closest('[data-flip]'); if (op) { const i = Number(op.dataset.flip); @@ -541,8 +539,7 @@ function mountSignWidget(host) { host.querySelector(`[data-flip="${i}"]`)?.focus(); } }); - onLangChange(render); - render(); + wireWidget(host, { render, choose: (i) => { state.set = i; state.s = QW_SETS[state.set].s; } }); } /* ---------------- the approach, in brief ---------------- */ diff --git a/src/lessons/best-time-to-buy-and-sell-stock/lesson.js b/src/lessons/best-time-to-buy-and-sell-stock/lesson.js index 962cc98..433aba5 100644 --- a/src/lessons/best-time-to-buy-and-sell-stock/lesson.js +++ b/src/lessons/best-time-to-buy-and-sell-stock/lesson.js @@ -11,10 +11,10 @@ * after the buy, which is the whole reason the answer is not * max(prices) - min(prices). */ -import { t, plural, exampleTitle, LANGUAGES, k, c, intList, presetChips, widgetLabel } from '../../lib/kit.js'; +import { t, plural, exampleTitle, LANGUAGES, k, c, intList, presetChips, widgetLabel, wireWidget } from '../../lib/kit.js'; import { mountLesson } from '../../lib/stepper.js'; import { bars, cells, slots, stagePanel } from '../../lib/stage.js'; -import { pick, onLangChange } from '../../lib/i18n.js'; +import { pick } from '../../lib/i18n.js'; /* A bilingual string: the { en, my } pair pick() reads at render time. */ @@ -528,16 +528,14 @@ function mountTradeWidget(host) { else return; render(); }); - host.addEventListener('click', (ev) => { - const chip = ev.target.closest('[data-set]'); - if (!chip) return; - state.set = Number(chip.dataset.set); - const { pair } = bestTrade(QW_SETS[state.set].prices); - [state.buy, state.sell] = pair ?? [0, 1]; - render(); + wireWidget(host, { + render, + choose: (i) => { + state.set = i; + const { pair } = bestTrade(QW_SETS[state.set].prices); + [state.buy, state.sell] = pair ?? [0, 1]; + }, }); - onLangChange(render); - render(); } /* ---------------- the approach, in brief ---------------- diff --git a/src/lessons/binary-tree-maximum-path-sum/lesson.js b/src/lessons/binary-tree-maximum-path-sum/lesson.js index b744b6b..dbd502b 100644 --- a/src/lessons/binary-tree-maximum-path-sum/lesson.js +++ b/src/lessons/binary-tree-maximum-path-sum/lesson.js @@ -9,9 +9,9 @@ * parent and records the bend on the way back: O(n). */ import { mountLesson } from '../../lib/stepper.js'; -import { pick, onLangChange } from '../../lib/i18n.js'; +import { pick } from '../../lib/i18n.js'; import { cells, tree, stack, readout, panels, slots, stagePanel } from '../../lib/stage.js'; -import { t, exampleTitle, LANGUAGES, k, c, stageGap, presetChips, widgetLabel } from '../../lib/kit.js'; +import { t, exampleTitle, LANGUAGES, k, c, stageGap, presetChips, widgetLabel, wireWidget } from '../../lib/kit.js'; import { buildTree, levelOrder, asNested, preorderKeys, nameOf, treeInput, formatLevelOrder } from '../../lib/tree.js'; const MAX_NODES = 15; @@ -448,12 +448,7 @@ function mountTopWidget(host) { q('[data-total]').innerHTML = `${best}${pick(t('best path', 'အကောင်းဆုံး'))}`; } host.addEventListener('input', (ev) => { if (ev.target.id === 'mps-b') { state.b = Number(ev.target.value) - 1; render(); } }); - host.addEventListener('click', (ev) => { - const chip = ev.target.closest('[data-set]'); - if (chip) { state.set = Number(chip.dataset.set); state.b = 0; render(); } - }); - onLangChange(render); - render(); + wireWidget(host, { render, choose: (i) => { state.set = i; state.b = 0; } }); } function atLeastOne(text) { diff --git a/src/lessons/climbing-stairs/lesson.js b/src/lessons/climbing-stairs/lesson.js index 539b241..c783cec 100644 --- a/src/lessons/climbing-stairs/lesson.js +++ b/src/lessons/climbing-stairs/lesson.js @@ -9,9 +9,9 @@ * variables walking up the stairs are all the memory the problem needs. */ import { mountLesson } from '../../lib/stepper.js'; -import { pick, onLangChange } from '../../lib/i18n.js'; +import { pick } from '../../lib/i18n.js'; import { cells, stack, bars, readout, panels, slots, stagePanel } from '../../lib/stage.js'; -import { t, exampleTitle, LANGUAGES, k, c, stageGap, intValue, widgetLabel } from '../../lib/kit.js'; +import { t, exampleTitle, LANGUAGES, k, c, stageGap, intValue, widgetLabel, wireWidget } from '../../lib/kit.js'; /* ten steps is 177 calls in the plain recursion — 441 frames, which is as far * as stepping by hand stays worth it */ @@ -464,8 +464,7 @@ function mountLastMoveWidget(host) { state.n = Number(chip.dataset.n); render(); }); - onLangChange(render); - render(); + wireWidget(host, { render }); } /* ---------------- the approach, in brief ---------------- diff --git a/src/lessons/coin-change/lesson.js b/src/lessons/coin-change/lesson.js index a02c66d..3ed625c 100644 --- a/src/lessons/coin-change/lesson.js +++ b/src/lessons/coin-change/lesson.js @@ -7,9 +7,9 @@ * so every answer it needs is already written down. */ import { mountLesson } from '../../lib/stepper.js'; -import { pick, onLangChange } from '../../lib/i18n.js'; +import { pick } from '../../lib/i18n.js'; import { cells, kv, stack, readout, slots, stagePanel } from '../../lib/stage.js'; -import { t, exampleTitle, LANGUAGES, k, c, stageRow, stageGap, intList, intValue, presetChips, widgetLabel } from '../../lib/kit.js'; +import { t, exampleTitle, LANGUAGES, k, c, stageRow, stageGap, intList, intValue, presetChips, widgetLabel, wireWidget } from '../../lib/kit.js'; const MAX_AMOUNT = 11; @@ -434,15 +434,7 @@ function mountGreedyWidget(host) { if (ev.target.id !== 'qw-amt') return; state.amount = Number(ev.target.value); render(); }); - host.addEventListener('click', (ev) => { - const chip = ev.target.closest('[data-set]'); - if (!chip) return; - const set = QW_SETS[Number(chip.dataset.set)]; - Object.assign(state, { set: Number(chip.dataset.set), amount: set.amount }); - render(); - }); - onLangChange(render); - render(); + wireWidget(host, { render, choose: (i) => { const set = QW_SETS[i]; Object.assign(state, { set: i, amount: set.amount }); } }); } /* ---------------- the approach, in brief ---------------- */ diff --git a/src/lessons/course-schedule/lesson.js b/src/lessons/course-schedule/lesson.js index 7355e15..e1fd40a 100644 --- a/src/lessons/course-schedule/lesson.js +++ b/src/lessons/course-schedule/lesson.js @@ -8,9 +8,9 @@ * nothing left to wait for and checks whether it ran out early. */ import { mountLesson } from '../../lib/stepper.js'; -import { pick, onLangChange } from '../../lib/i18n.js'; +import { pick } from '../../lib/i18n.js'; import { cells, stagePanel, stack, readout } from '../../lib/stage.js'; -import { t, exampleTitle, LANGUAGES, k, c, labelledRows, stageRow, stageGap, verdictAnswer, intValue, widgetLabel, presetChips } from '../../lib/kit.js'; +import { t, exampleTitle, LANGUAGES, k, c, labelledRows, stageRow, stageGap, verdictAnswer, intValue, widgetLabel, presetChips, wireWidget } from '../../lib/kit.js'; const MAX_N = 8, MAX_PAIRS = 12; @@ -601,22 +601,10 @@ function mountArrowWidget(host) { render(); } host.addEventListener('click', (ev) => { - const chip = ev.target.closest('[data-set]'); - if (chip) { load(Number(chip.dataset.set)); return render(); } const node = ev.target.closest('[data-u]'); if (node) tap(Number(node.dataset.u)); }); - host.addEventListener('keydown', (ev) => { - const node = ev.target.closest('[data-u]'); - if (node && (ev.key === 'Enter' || ev.key === ' ')) { - ev.preventDefault(); - const u = Number(node.dataset.u); - tap(u); - host.querySelector(`[data-u="${u}"]`)?.focus(); - } - }); - onLangChange(render); - render(); + wireWidget(host, { render, choose: load }); } /* ---------------- the approach, in brief ---------------- */ diff --git a/src/lessons/diameter-of-binary-tree/lesson.js b/src/lessons/diameter-of-binary-tree/lesson.js index 877cbab..a56faa9 100644 --- a/src/lessons/diameter-of-binary-tree/lesson.js +++ b/src/lessons/diameter-of-binary-tree/lesson.js @@ -9,9 +9,9 @@ * bottom-up, and records the bend on the way back. */ import { mountLesson } from '../../lib/stepper.js'; -import { pick, onLangChange } from '../../lib/i18n.js'; +import { pick } from '../../lib/i18n.js'; import { cells, tree, stack, readout, panels, slots, stagePanel } from '../../lib/stage.js'; -import { t, plural, exampleTitle, LANGUAGES, k, c, stageGap, presetChips, widgetLabel, stageEmpty } from '../../lib/kit.js'; +import { t, plural, exampleTitle, LANGUAGES, k, c, stageGap, presetChips, widgetLabel, stageEmpty, wireWidget } from '../../lib/kit.js'; import { buildTree, levelOrder, asNested, preorderKeys, treeDepth, nameOf, treeInput, formatLevelOrder } from '../../lib/tree.js'; const MAX_NODES = 15; @@ -551,15 +551,7 @@ function mountBendWidget(host) { if (ev.target.id !== 'qw-b') return; state.b = Number(ev.target.value) - 1; render(); }); - host.addEventListener('click', (ev) => { - const chip = ev.target.closest('[data-set]'); - if (!chip) return; - state.set = Number(chip.dataset.set); - state.b = 0; - render(); - }); - onLangChange(render); - render(); + wireWidget(host, { render, choose: (i) => { state.set = i; state.b = 0; } }); } /* The constraints start at one node, and a diameter needs somewhere to bend. */ diff --git a/src/lessons/find-median-from-data-stream/lesson.js b/src/lessons/find-median-from-data-stream/lesson.js index b151d59..67ae03a 100644 --- a/src/lessons/find-median-from-data-stream/lesson.js +++ b/src/lessons/find-median-from-data-stream/lesson.js @@ -10,9 +10,9 @@ */ import { mountLesson } from '../../lib/stepper.js'; import { heapNested } from '../../lib/tree.js'; -import { pick, onLangChange } from '../../lib/i18n.js'; +import { pick } from '../../lib/i18n.js'; import { cells, slots, stagePanel, tree, readout, panels } from '../../lib/stage.js'; -import { t, exampleTitle, LANGUAGES, k, c, stageRow, stageGap, presetChips, widgetLabel, stageEmpty } from '../../lib/kit.js'; +import { t, exampleTitle, LANGUAGES, k, c, stageRow, stageGap, presetChips, widgetLabel, stageEmpty, wireWidget } from '../../lib/kit.js'; const MAX_OPS = 12; @@ -550,12 +550,7 @@ function mountHalvesWidget(host) { q('[data-total]').innerHTML = `${fmt(med)}${pick(t('median', 'median'))}`; } host.addEventListener('input', (ev) => { if (ev.target.id === 'mf-n') { state.n = Number(ev.target.value); render(); } }); - host.addEventListener('click', (ev) => { - const chip = ev.target.closest('[data-set]'); - if (chip) { state.set = Number(chip.dataset.set); state.n = QW_SETS[state.set].nums.length; render(); } - }); - onLangChange(render); - render(); + wireWidget(host, { render, choose: (i) => { state.set = i; state.n = QW_SETS[state.set].nums.length; } }); } /* ---------------- the approach, in brief ---------------- */ diff --git a/src/lessons/implement-trie-prefix-tree/lesson.js b/src/lessons/implement-trie-prefix-tree/lesson.js index 59d5996..9977a63 100644 --- a/src/lessons/implement-trie-prefix-tree/lesson.js +++ b/src/lessons/implement-trie-prefix-tree/lesson.js @@ -8,9 +8,9 @@ * but has to look at every word for startsWith. */ import { mountLesson } from '../../lib/stepper.js'; -import { pick, onLangChange } from '../../lib/i18n.js'; +import { pick } from '../../lib/i18n.js'; import { cells, slots, stagePanel, readout, trieOutline } from '../../lib/stage.js'; -import { t, exampleTitle, LANGUAGES, k, c, stageRow, stageGap, presetChips, widgetLabel } from '../../lib/kit.js'; +import { t, exampleTitle, LANGUAGES, k, c, stageRow, stageGap, presetChips, widgetLabel, wireWidget } from '../../lib/kit.js'; const MAX_OPS = 8, MAX_LEN = 6; const NAMES = { insert: 'insert', search: 'search', startsWith: 'startsWith' }; @@ -574,12 +574,7 @@ function mountPrefixWidget(host) { q('[data-total]').innerHTML = `${hits.length}${pick(t('words match', 'word ကိုက်'))}`; } input.addEventListener('input', () => { state.prefix = input.value.toLowerCase().replace(/[^a-z]/g, '').slice(0, 6); render(); }); - host.addEventListener('click', (ev) => { - const chip = ev.target.closest('[data-set]'); - if (chip) { state.set = Number(chip.dataset.set); state.prefix = QW_SETS[state.set].words[0].slice(0, 2); render(); } - }); - onLangChange(render); - render(); + wireWidget(host, { render, choose: (i) => { state.set = i; state.prefix = QW_SETS[state.set].words[0].slice(0, 2); } }); } /* ---------------- the approach, in brief ---------------- */ diff --git a/src/lessons/invert-binary-tree/lesson.js b/src/lessons/invert-binary-tree/lesson.js index 04a30d0..a63bd42 100644 --- a/src/lessons/invert-binary-tree/lesson.js +++ b/src/lessons/invert-binary-tree/lesson.js @@ -7,9 +7,9 @@ * the stack and the queue beside it are the lesson. */ import { mountLesson } from '../../lib/stepper.js'; -import { pick, onLangChange } from '../../lib/i18n.js'; +import { pick } from '../../lib/i18n.js'; import { cells, tree, stack, panels, slots, stagePanel } from '../../lib/stage.js'; -import { t, plural, exampleTitle, LANGUAGES, k, c, stageRow, stageGap, presetChips, widgetLabel, stageEmpty } from '../../lib/kit.js'; +import { t, plural, exampleTitle, LANGUAGES, k, c, stageRow, stageGap, presetChips, widgetLabel, stageEmpty, wireWidget } from '../../lib/kit.js'; import { buildTree, levelOrder, asNested, treeDepth, copyKids as copy, nameOf, treeInput, formatLevelOrder } from '../../lib/tree.js'; /* The tree helpers — parsing LeetCode's level order, drawing, serializing — @@ -460,15 +460,7 @@ function mountMirrorWidget(host) { if (ev.target.id !== 'qw-k') return; state.k = Number(ev.target.value); render(); }); - host.addEventListener('click', (ev) => { - const chip = ev.target.closest('[data-set]'); - if (!chip) return; - state.set = Number(chip.dataset.set); - state.k = 0; - render(); - }); - onLangChange(render); - render(); + wireWidget(host, { render, choose: (i) => { state.set = i; state.k = 0; } }); } /* ---------------- the approach, in brief ---------------- diff --git a/src/lessons/kth-largest-element-in-an-array/lesson.js b/src/lessons/kth-largest-element-in-an-array/lesson.js index b4b7b37..8f7916b 100644 --- a/src/lessons/kth-largest-element-in-an-array/lesson.js +++ b/src/lessons/kth-largest-element-in-an-array/lesson.js @@ -7,9 +7,9 @@ */ import { mountLesson } from '../../lib/stepper.js'; import { heapNested } from '../../lib/tree.js'; -import { pick, onLangChange } from '../../lib/i18n.js'; +import { pick } from '../../lib/i18n.js'; import { cells, tree, readout, slots, stagePanel } from '../../lib/stage.js'; -import { t, exampleTitle, LANGUAGES, k, c, stageRow, stageGap, intList, intValue, presetChips, widgetLabel, stageEmpty } from '../../lib/kit.js'; +import { t, exampleTitle, LANGUAGES, k, c, stageRow, stageGap, intList, intValue, presetChips, widgetLabel, stageEmpty, wireWidget } from '../../lib/kit.js'; const MAX_LEN = 10; @@ -382,15 +382,7 @@ function mountRankWidget(host) { if (ev.target.id !== 'qw-k') return; state.k = Number(ev.target.value); render(); }); - host.addEventListener('click', (ev) => { - const chip = ev.target.closest('[data-set]'); - if (!chip) return; - const set = QW_SETS[Number(chip.dataset.set)]; - Object.assign(state, { set: Number(chip.dataset.set), k: set.k }); - render(); - }); - onLangChange(render); - render(); + wireWidget(host, { render, choose: (i) => { const set = QW_SETS[i]; Object.assign(state, { set: i, k: set.k }); } }); } /* ---------------- the approach, in brief ---------------- */ diff --git a/src/lessons/largest-rectangle-in-histogram/lesson.js b/src/lessons/largest-rectangle-in-histogram/lesson.js index da031dd..b0c6bd3 100644 --- a/src/lessons/largest-rectangle-in-histogram/lesson.js +++ b/src/lessons/largest-rectangle-in-histogram/lesson.js @@ -9,9 +9,9 @@ * left — so its widest rectangle is known the moment it is popped. */ import { mountLesson } from '../../lib/stepper.js'; -import { pick, onLangChange } from '../../lib/i18n.js'; +import { pick } from '../../lib/i18n.js'; import { bars, cells, slots, stagePanel, readout } from '../../lib/stage.js'; -import { t, exampleTitle, LANGUAGES, k, c, stageRow, stageGap, intList, listText, presetChips, widgetLabel } from '../../lib/kit.js'; +import { t, exampleTitle, LANGUAGES, k, c, stageRow, stageGap, intList, listText, presetChips, widgetLabel, wireWidget } from '../../lib/kit.js'; const MAX_N = 12; @@ -340,17 +340,10 @@ function mountShortestWidget(host) { } const pickCol = (el) => { state.at = Number(el.dataset.col); render(); host.querySelector(`[data-col="${state.at}"]`)?.focus(); }; host.addEventListener('click', (ev) => { - const chip = ev.target.closest('[data-set]'); - if (chip) { state.set = Number(chip.dataset.set); state.at = 0; return render(); } const col = ev.target.closest('[data-col]'); if (col) pickCol(col); }); - host.addEventListener('keydown', (ev) => { - const col = ev.target.closest('[data-col]'); - if (col && (ev.key === 'Enter' || ev.key === ' ')) { ev.preventDefault(); pickCol(col); } - }); - onLangChange(render); - render(); + wireWidget(host, { render, choose: (i) => { state.set = i; state.at = 0; } }); } /* ---------------- the approach, in brief ---------------- */ diff --git a/src/lessons/linked-list-cycle/lesson.js b/src/lessons/linked-list-cycle/lesson.js index 160f919..8e5b25b 100644 --- a/src/lessons/linked-list-cycle/lesson.js +++ b/src/lessons/linked-list-cycle/lesson.js @@ -5,9 +5,9 @@ * passes and the other writes down nothing at all. Watching the visited set * grow next to two pointers that never grow is the whole lesson. */ -import { t, plural, exampleTitle, LANGUAGES, k, c, verdictAnswer, stageGap, intList, intValue, presetChips, widgetLabel, stageEmpty } from '../../lib/kit.js'; +import { t, plural, exampleTitle, LANGUAGES, k, c, verdictAnswer, stageGap, intList, intValue, presetChips, widgetLabel, stageEmpty, wireWidget } from '../../lib/kit.js'; import { mountLesson } from '../../lib/stepper.js'; -import { pick, onLangChange } from '../../lib/i18n.js'; +import { pick } from '../../lib/i18n.js'; import { cells, chain, kv, readout, stagePanel } from '../../lib/stage.js'; @@ -496,15 +496,7 @@ function mountHiddenPosWidget(host) { if (ev.target.id !== 'qw-pos') return; state.pos = Number(ev.target.value); render(); }); - host.addEventListener('click', (ev) => { - const chip = ev.target.closest('[data-set]'); - if (!chip) return; - state.set = Number(chip.dataset.set); - state.pos = QW_SETS[state.set].pos; - render(); - }); - onLangChange(render); - render(); + wireWidget(host, { render, choose: (i) => { state.set = i; state.pos = QW_SETS[state.set].pos; } }); } /* ---------------- the approach, in brief ---------------- diff --git a/src/lessons/longest-increasing-path-in-a-matrix/lesson.js b/src/lessons/longest-increasing-path-in-a-matrix/lesson.js index 103a293..26421a1 100644 --- a/src/lessons/longest-increasing-path-in-a-matrix/lesson.js +++ b/src/lessons/longest-increasing-path-in-a-matrix/lesson.js @@ -9,9 +9,9 @@ * with no recursion at all. */ import { mountLesson } from '../../lib/stepper.js'; -import { pick, onLangChange } from '../../lib/i18n.js'; +import { pick } from '../../lib/i18n.js'; import { cells, slots, stagePanel, stack, readout, panels } from '../../lib/stage.js'; -import { t, exampleTitle, LANGUAGES, k, c, labelledRows, stageRow, stageGap, presetChips, widgetLabel } from '../../lib/kit.js'; +import { t, exampleTitle, LANGUAGES, k, c, labelledRows, stageRow, stageGap, presetChips, widgetLabel, wireWidget } from '../../lib/kit.js'; const MAX_R = 5, MAX_C = 5, MAX_V = 999; @@ -655,19 +655,12 @@ function mountClimbWidget(host) { host.querySelector(`[data-r="${state.r}"][data-c="${state.c}"]`)?.focus(); } host.addEventListener('click', (ev) => { - const chip = ev.target.closest('[data-set]'); - if (chip) { state.set = Number(chip.dataset.set); state.r = 0; state.c = 0; return render(); } const conn = ev.target.closest('[data-eight]'); if (conn) { state.eight = conn.dataset.eight === 'true'; return render(); } const el = ev.target.closest('[data-r]'); if (el) choose(el); }); - host.addEventListener('keydown', (ev) => { - const el = ev.target.closest('[data-r]'); - if (el && (ev.key === 'Enter' || ev.key === ' ')) { ev.preventDefault(); choose(el); } - }); - onLangChange(render); - render(); + wireWidget(host, { render, choose: (i) => { state.set = i; state.r = 0; state.c = 0; } }); } /* ---------------- the approach, in brief ---------------- */ diff --git a/src/lessons/longest-substring-without-repeating-characters/lesson.js b/src/lessons/longest-substring-without-repeating-characters/lesson.js index c9f8183..6a86007 100644 --- a/src/lessons/longest-substring-without-repeating-characters/lesson.js +++ b/src/lessons/longest-substring-without-repeating-characters/lesson.js @@ -8,9 +8,9 @@ * character enters the window once and the left edge only moves forward. */ import { mountLesson } from '../../lib/stepper.js'; -import { pick, onLangChange } from '../../lib/i18n.js'; +import { pick } from '../../lib/i18n.js'; import { cells, kv, readout, slots, stagePanel } from '../../lib/stage.js'; -import { t, esc, exampleTitle, LANGUAGES, k, c, stageRow, stageGap, presetChips, widgetLabel } from '../../lib/kit.js'; +import { t, esc, exampleTitle, LANGUAGES, k, c, stageRow, stageGap, presetChips, widgetLabel, wireWidget } from '../../lib/kit.js'; const MAX_LEN = 12; @@ -444,21 +444,16 @@ function mountRunWidget(host) { render(); } host.addEventListener('click', (ev) => { - const chip = ev.target.closest('[data-set]'); - if (chip) { - const set = QW_SETS[Number(chip.dataset.set)]; - Object.assign(state, { set: Number(chip.dataset.set), from: set.from, to: set.to, next: 'from' }); - return render(); - } const cell = ev.target.closest('[data-i]'); if (cell) pickCell(Number(cell.dataset.i)); }); - host.addEventListener('keydown', (ev) => { - const cell = ev.target.closest('[data-i]'); - if (cell && (ev.key === 'Enter' || ev.key === ' ')) { ev.preventDefault(); pickCell(Number(cell.dataset.i)); } + wireWidget(host, { + render, + choose: (i) => { + const set = QW_SETS[i]; + Object.assign(state, { set: i, from: set.from, to: set.to, next: 'from' }); + }, }); - onLangChange(render); - render(); } /* ---------------- the approach, in brief ---------------- */ diff --git a/src/lessons/lowest-common-ancestor-of-a-binary-tree/lesson.js b/src/lessons/lowest-common-ancestor-of-a-binary-tree/lesson.js index 9803cd7..0de1e7a 100644 --- a/src/lessons/lowest-common-ancestor-of-a-binary-tree/lesson.js +++ b/src/lessons/lowest-common-ancestor-of-a-binary-tree/lesson.js @@ -8,9 +8,9 @@ * from both sides is the answer. */ import { mountLesson } from '../../lib/stepper.js'; -import { pick, onLangChange } from '../../lib/i18n.js'; +import { pick } from '../../lib/i18n.js'; import { cells, tree, stack, readout, slots, stagePanel, panels } from '../../lib/stage.js'; -import { t, exampleTitle, LANGUAGES, k, c, stageGap, intValue, widgetLabel } from '../../lib/kit.js'; +import { t, exampleTitle, LANGUAGES, k, c, stageGap, intValue, widgetLabel, wireWidget } from '../../lib/kit.js'; import { buildTree, levelOrder, asNested, preorderKeys, nameOf, treeInput, formatLevelOrder } from '../../lib/tree.js'; const EX = [3, 5, 1, 6, 2, 0, 8, null, null, 7, 4]; @@ -507,12 +507,7 @@ function mountPathsWidget(host) { } function choose(el) { state[el.dataset.which] = Number(el.dataset.v); render(); } host.addEventListener('click', (ev) => { const el = ev.target.closest('[data-which]'); if (el) choose(el); }); - host.addEventListener('keydown', (ev) => { - const el = ev.target.closest('[data-which]'); - if (el && (ev.key === 'Enter' || ev.key === ' ')) { ev.preventDefault(); choose(el); } - }); - onLangChange(render); - render(); + wireWidget(host, { render }); } /* ---------------- the approach, in brief ---------------- */ diff --git a/src/lessons/lru-cache/lesson.js b/src/lessons/lru-cache/lesson.js index 73e8750..cf7ae36 100644 --- a/src/lessons/lru-cache/lesson.js +++ b/src/lessons/lru-cache/lesson.js @@ -9,9 +9,9 @@ * cut out and moved to the front in a few pointer writes. */ import { mountLesson } from '../../lib/stepper.js'; -import { pick, onLangChange } from '../../lib/i18n.js'; +import { pick } from '../../lib/i18n.js'; import { cells, slots, stagePanel, kv, chain, readout } from '../../lib/stage.js'; -import { t, exampleTitle, LANGUAGES, k, c, stageRow, stageGap, intValue, presetChips, widgetLabel } from '../../lib/kit.js'; +import { t, exampleTitle, LANGUAGES, k, c, stageRow, stageGap, intValue, presetChips, widgetLabel, wireWidget } from '../../lib/kit.js'; const MAX_OPS = 12; @@ -779,17 +779,10 @@ function mountEvictWidget(host) { host.querySelector(`[data-key="${el.dataset.key}"]`)?.focus(); } host.addEventListener('click', (ev) => { - const chip = ev.target.closest('[data-set]'); - if (chip) { state.set = Number(chip.dataset.set); state.seq = [...QW_SETS[state.set].seq]; return render(); } const key = ev.target.closest('[data-key]'); if (key) read(key); }); - host.addEventListener('keydown', (ev) => { - const key = ev.target.closest('[data-key]'); - if (key && (ev.key === 'Enter' || ev.key === ' ')) { ev.preventDefault(); read(key); } - }); - onLangChange(render); - render(); + wireWidget(host, { render, choose: (i) => { state.set = i; state.seq = [...QW_SETS[state.set].seq]; } }); } /* ---------------- the approach, in brief ---------------- */ diff --git a/src/lessons/majority-element/lesson.js b/src/lessons/majority-element/lesson.js index 9e4c340..903e651 100644 --- a/src/lessons/majority-element/lesson.js +++ b/src/lessons/majority-element/lesson.js @@ -9,9 +9,9 @@ * difference is the whole contrast: up to n/2 rows against two integers. */ import { mountLesson } from '../../lib/stepper.js'; -import { pick, onLangChange } from '../../lib/i18n.js'; +import { pick } from '../../lib/i18n.js'; import { cells, kv, stack, readout, slots, stagePanel } from '../../lib/stage.js'; -import { t, exampleTitle, LANGUAGES, k, c, stageGap, intList, presetChips, widgetLabel } from '../../lib/kit.js'; +import { t, exampleTitle, LANGUAGES, k, c, stageGap, intList, presetChips, widgetLabel, wireWidget } from '../../lib/kit.js'; const MAX_LEN = 15; @@ -392,15 +392,7 @@ function mountPairWidget(host) { if (ev.target.id !== 'qw-k') return; state.k = Number(ev.target.value); render(); }); - host.addEventListener('click', (ev) => { - const chip = ev.target.closest('[data-set]'); - if (!chip) return; - state.set = Number(chip.dataset.set); - state.k = 0; - render(); - }); - onLangChange(render); - render(); + wireWidget(host, { render, choose: (i) => { state.set = i; state.k = 0; } }); } /* ---------------- the approach, in brief ---------------- diff --git a/src/lessons/maximum-depth-of-binary-tree/lesson.js b/src/lessons/maximum-depth-of-binary-tree/lesson.js index 6b57918..4a12d58 100644 --- a/src/lessons/maximum-depth-of-binary-tree/lesson.js +++ b/src/lessons/maximum-depth-of-binary-tree/lesson.js @@ -8,9 +8,9 @@ * the call stack, the loop carries the current level. */ import { mountLesson } from '../../lib/stepper.js'; -import { pick, onLangChange } from '../../lib/i18n.js'; +import { pick } from '../../lib/i18n.js'; import { cells, tree, stack, readout, panels, slots, stagePanel } from '../../lib/stage.js'; -import { t, plural, exampleTitle, LANGUAGES, k, c, stageRow, stageGap, presetChips, widgetLabel, stageEmpty } from '../../lib/kit.js'; +import { t, plural, exampleTitle, LANGUAGES, k, c, stageRow, stageGap, presetChips, widgetLabel, stageEmpty, wireWidget } from '../../lib/kit.js'; import { buildTree, levelOrder, asNested, treeDepth, nameOf, treeInput, formatLevelOrder } from '../../lib/tree.js'; const MAX_NODES = 15; @@ -431,15 +431,7 @@ function mountPathWidget(host) { if (ev.target.id !== 'qw-p') return; state.p = Number(ev.target.value) - 1; render(); }); - host.addEventListener('click', (ev) => { - const chip = ev.target.closest('[data-set]'); - if (!chip) return; - state.set = Number(chip.dataset.set); - state.p = 0; - render(); - }); - onLangChange(render); - render(); + wireWidget(host, { render, choose: (i) => { state.set = i; state.p = 0; } }); } /* ---------------- the approach, in brief ---------------- diff --git a/src/lessons/maximum-subarray/lesson.js b/src/lessons/maximum-subarray/lesson.js index f31632c..5d5b89b 100644 --- a/src/lessons/maximum-subarray/lesson.js +++ b/src/lessons/maximum-subarray/lesson.js @@ -9,9 +9,9 @@ * appears. */ import { mountLesson } from '../../lib/stepper.js'; -import { pick, onLangChange } from '../../lib/i18n.js'; +import { pick } from '../../lib/i18n.js'; import { cells, readout, slots, stagePanel } from '../../lib/stage.js'; -import { t, exampleTitle, LANGUAGES, k, c, stageRow, stageGap, intList, presetChips, widgetLabel } from '../../lib/kit.js'; +import { t, exampleTitle, LANGUAGES, k, c, stageRow, stageGap, intList, presetChips, widgetLabel, wireWidget } from '../../lib/kit.js'; const MAX_LEN = 12; @@ -382,15 +382,7 @@ function mountRunWidget(host) { else return; render(); }); - host.addEventListener('click', (ev) => { - const chip = ev.target.closest('[data-set]'); - if (!chip) return; - const set = QW_SETS[Number(chip.dataset.set)]; - Object.assign(state, { set: Number(chip.dataset.set), from: set.from, to: set.to }); - render(); - }); - onLangChange(render); - render(); + wireWidget(host, { render, choose: (i) => { const set = QW_SETS[i]; Object.assign(state, { set: i, from: set.from, to: set.to }); } }); } /* ---------------- the approach, in brief ---------------- */ diff --git a/src/lessons/median-of-two-sorted-arrays/lesson.js b/src/lessons/median-of-two-sorted-arrays/lesson.js index 9f1a90e..b75bf4f 100644 --- a/src/lessons/median-of-two-sorted-arrays/lesson.js +++ b/src/lessons/median-of-two-sorted-arrays/lesson.js @@ -10,9 +10,9 @@ * big, give more. That is a binary search on i. */ import { mountLesson } from '../../lib/stepper.js'; -import { pick, onLangChange } from '../../lib/i18n.js'; +import { pick } from '../../lib/i18n.js'; import { cells, slots, stagePanel, readout } from '../../lib/stage.js'; -import { t, exampleTitle, LANGUAGES, k, c, labelledRows, stageGap, listText, presetChips, widgetLabel } from '../../lib/kit.js'; +import { t, exampleTitle, LANGUAGES, k, c, labelledRows, stageGap, listText, presetChips, widgetLabel, wireWidget } from '../../lib/kit.js'; const MAX_EACH = 8; const fmt = (x) => x.toFixed(5); @@ -437,12 +437,7 @@ function mountCutWidget(host) { q('[data-total]').innerHTML = ok ? `${med}${pick(t('median', 'median'))}` : `—${pick(t('not the split yet', 'ခွဲခြမ်းမှု မဟုတ်သေး'))}`; } q('[data-i]').addEventListener('input', (ev) => { state.i = Number(ev.target.value); render(); }); - host.addEventListener('click', (ev) => { - const chip = ev.target.closest('[data-set]'); - if (chip) { state.set = Number(chip.dataset.set); state.i = 0; render(); } - }); - onLangChange(render); - render(); + wireWidget(host, { render, choose: (i) => { state.set = i; state.i = 0; } }); } /* ---------------- the approach, in brief ---------------- */ diff --git a/src/lessons/merge-intervals/lesson.js b/src/lessons/merge-intervals/lesson.js index b02b3a2..70a3eb1 100644 --- a/src/lessons/merge-intervals/lesson.js +++ b/src/lessons/merge-intervals/lesson.js @@ -8,9 +8,9 @@ * so the sweep compares with one interval, not all of them. */ import { mountLesson } from '../../lib/stepper.js'; -import { pick, onLangChange } from '../../lib/i18n.js'; +import { pick } from '../../lib/i18n.js'; import { cells, readout, slots, stagePanel } from '../../lib/stage.js'; -import { t, esc, exampleTitle, LANGUAGES, k, c, stageGap, presetChips, widgetLabel, stageEmpty } from '../../lib/kit.js'; +import { t, esc, exampleTitle, LANGUAGES, k, c, stageGap, presetChips, widgetLabel, stageEmpty, wireWidget } from '../../lib/kit.js'; const MAX_LEN = 8; const TOP = 20; @@ -470,15 +470,7 @@ function mountOverlapWidget(host) { else return; render(); }); - host.addEventListener('click', (ev) => { - const chip = ev.target.closest('[data-set]'); - if (!chip) return; - const set = QW_SETS[Number(chip.dataset.set)]; - Object.assign(state, { set: Number(chip.dataset.set), aEnd: set.aEnd, bStart: set.bStart }); - render(); - }); - onLangChange(render); - render(); + wireWidget(host, { render, choose: (i) => { const set = QW_SETS[i]; Object.assign(state, { set: i, aEnd: set.aEnd, bStart: set.bStart }); } }); } /* ---------------- the approach, in brief ---------------- */ diff --git a/src/lessons/merge-k-sorted-lists/lesson.js b/src/lessons/merge-k-sorted-lists/lesson.js index 79f9c75..30dc401 100644 --- a/src/lessons/merge-k-sorted-lists/lesson.js +++ b/src/lessons/merge-k-sorted-lists/lesson.js @@ -8,9 +8,9 @@ * and there are about log₂ k rounds. */ import { mountLesson } from '../../lib/stepper.js'; -import { pick, onLangChange } from '../../lib/i18n.js'; +import { pick } from '../../lib/i18n.js'; import { cells, slots, stagePanel, readout } from '../../lib/stage.js'; -import { t, exampleTitle, LANGUAGES, k, c, labelledRows, stageRow, stageGap, presetChips, widgetLabel } from '../../lib/kit.js'; +import { t, exampleTitle, LANGUAGES, k, c, labelledRows, stageRow, stageGap, presetChips, widgetLabel, wireWidget } from '../../lib/kit.js'; const MAX_K = 6, MAX_N = 14; @@ -507,12 +507,7 @@ function mountMovesWidget(host) { q('[data-total]').innerHTML = `${(total(fold) / total(pairs)).toFixed(1)}×${pick(t('more moves', 'ရွှေ့ခြင်း ပိုများ'))}`; } q('[data-k]').addEventListener('input', (ev) => { state.k = Number(ev.target.value); render(); }); - host.addEventListener('click', (ev) => { - const chip = ev.target.closest('[data-set]'); - if (chip) { state.k = QW_SETS[Number(chip.dataset.set)].k; render(); } - }); - onLangChange(render); - render(); + wireWidget(host, { render, choose: (i) => { state.k = QW_SETS[i].k; } }); } /* ---------------- the approach, in brief ---------------- */ diff --git a/src/lessons/merge-two-sorted-lists/lesson.js b/src/lessons/merge-two-sorted-lists/lesson.js index 81e5379..69dc3d6 100644 --- a/src/lessons/merge-two-sorted-lists/lesson.js +++ b/src/lessons/merge-two-sorted-lists/lesson.js @@ -6,9 +6,9 @@ * pointer hanging off a dummy node. The recursion holds it in the call stack, * and does not link anything until the calls start returning. */ -import { t, plural, exampleTitle, LANGUAGES, k, labelledRows, intList, presetChips, widgetLabel, stageEmpty } from '../../lib/kit.js'; +import { t, plural, exampleTitle, LANGUAGES, k, labelledRows, intList, presetChips, widgetLabel, stageEmpty, wireWidget } from '../../lib/kit.js'; import { mountLesson } from '../../lib/stepper.js'; -import { pick, onLangChange } from '../../lib/i18n.js'; +import { pick } from '../../lib/i18n.js'; import { cells, chain, stack, panels, slots, stagePanel } from '../../lib/stage.js'; @@ -546,15 +546,7 @@ function mountFrontsWidget(host) { if (ev.target.id !== 'qw-k') return; state.k = Number(ev.target.value); render(); }); - host.addEventListener('click', (ev) => { - const chip = ev.target.closest('[data-set]'); - if (!chip) return; - state.set = Number(chip.dataset.set); - state.k = 0; - render(); - }); - onLangChange(render); - render(); + wireWidget(host, { render, choose: (i) => { state.set = i; state.k = 0; } }); } /* ---------------- the approach, in brief ---------------- diff --git a/src/lessons/minimum-window-substring/lesson.js b/src/lessons/minimum-window-substring/lesson.js index 6b755c7..2bf2bff 100644 --- a/src/lessons/minimum-window-substring/lesson.js +++ b/src/lessons/minimum-window-substring/lesson.js @@ -8,9 +8,9 @@ * fresh window from every start instead. */ import { mountLesson } from '../../lib/stepper.js'; -import { pick, onLangChange } from '../../lib/i18n.js'; +import { pick } from '../../lib/i18n.js'; import { cells, slots, stagePanel, kv, readout } from '../../lib/stage.js'; -import { t, exampleTitle, LANGUAGES, k, c, stageGap, presetChips, widgetLabel } from '../../lib/kit.js'; +import { t, exampleTitle, LANGUAGES, k, c, stageGap, presetChips, widgetLabel, wireWidget } from '../../lib/kit.js'; const MAX_S = 16, MAX_T = 6; @@ -490,12 +490,7 @@ function mountCoverWidget(host) { } qq('[data-lo]').addEventListener('input', (ev) => { state.lo = Number(ev.target.value); if (state.hi < state.lo) state.hi = state.lo; render(); }); qq('[data-hi]').addEventListener('input', (ev) => { state.hi = Number(ev.target.value); if (state.lo > state.hi) state.lo = state.hi; render(); }); - host.addEventListener('click', (ev) => { - const chip = ev.target.closest('[data-set]'); - if (chip) { state.set = Number(chip.dataset.set); state.lo = QW_SETS[state.set].lo; state.hi = QW_SETS[state.set].hi; render(); } - }); - onLangChange(render); - render(); + wireWidget(host, { render, choose: (i) => { state.set = i; state.lo = QW_SETS[state.set].lo; state.hi = QW_SETS[state.set].hi; } }); } /* ---------------- the approach, in brief ---------------- */ diff --git a/src/lessons/move-zeroes/lesson.js b/src/lessons/move-zeroes/lesson.js index 9ecd9ad..69b59f2 100644 --- a/src/lessons/move-zeroes/lesson.js +++ b/src/lessons/move-zeroes/lesson.js @@ -6,10 +6,10 @@ * `slow` is already where it belongs — so the array is partially correct at * every single step, and no second array is ever allocated. */ -import { t, exampleTitle, LANGUAGES, k, c, intList, presetChips, widgetLabel } from '../../lib/kit.js'; +import { t, exampleTitle, LANGUAGES, k, c, intList, presetChips, widgetLabel, wireWidget } from '../../lib/kit.js'; import { mountLesson } from '../../lib/stepper.js'; import { cells, panels, stagePanel } from '../../lib/stage.js'; -import { pick, onLangChange } from '../../lib/i18n.js'; +import { pick } from '../../lib/i18n.js'; /* ---------------- step generators ---------------- */ @@ -478,12 +478,6 @@ function mountWidget(host) { render(); }); host.addEventListener('click', (e) => { - const chip = e.target.closest('[data-set]'); - if (chip) { - state.set = Number(chip.dataset.set); - state.pos = 0; - return render(); - } const cell = e.target.closest('[data-i]'); if (cell) { state.pos = Number(cell.dataset.i); @@ -492,18 +486,7 @@ function mountWidget(host) { render(); } }); - host.addEventListener('keydown', (e) => { - const cell = e.target.closest('[data-i]'); - if (cell && (e.key === 'Enter' || e.key === ' ')) { - e.preventDefault(); - state.pos = Number(cell.dataset.i); - qw('#mw-sweep').value = String(state.pos); - qw('[data-out]').textContent = String(state.pos); - render(); - } - }); - onLangChange(render); - render(); + wireWidget(host, { render, choose: (i) => { state.set = i; state.pos = 0; } }); } /* ---------------- the approach, in brief ---------------- diff --git a/src/lessons/n-queens/lesson.js b/src/lessons/n-queens/lesson.js index b4e0cd0..5b655c1 100644 --- a/src/lessons/n-queens/lesson.js +++ b/src/lessons/n-queens/lesson.js @@ -9,9 +9,9 @@ * diagonals and checks in O(1). */ import { mountLesson } from '../../lib/stepper.js'; -import { pick, onLangChange } from '../../lib/i18n.js'; +import { pick } from '../../lib/i18n.js'; import { cells, slots, stagePanel, readout } from '../../lib/stage.js'; -import { t, LANGUAGES, k, c, labelledRows, stageGap, intValue, presetChips, widgetLabel } from '../../lib/kit.js'; +import { t, LANGUAGES, k, c, labelledRows, stageGap, intValue, presetChips, widgetLabel, wireWidget } from '../../lib/kit.js'; const MAX_N = 5; const SOLUTIONS = [1, 0, 0, 2, 10, 4, 40, 92, 352]; @@ -517,17 +517,10 @@ function mountDiagWidget(host) { host.querySelector(`[data-r="${r}"][data-c="${cc}"]`)?.focus(); } host.addEventListener('click', (ev) => { - const chip = ev.target.closest('[data-set]'); - if (chip) { load(Number(chip.dataset.set)); return render(); } const cell = ev.target.closest('[data-r]'); if (cell) toggle(cell); }); - host.addEventListener('keydown', (ev) => { - const cell = ev.target.closest('[data-r]'); - if (cell && (ev.key === 'Enter' || ev.key === ' ')) { ev.preventDefault(); toggle(cell); } - }); - onLangChange(render); - render(); + wireWidget(host, { render, choose: load }); } /* ---------------- the approach, in brief ---------------- */ diff --git a/src/lessons/number-of-islands/lesson.js b/src/lessons/number-of-islands/lesson.js index 54bc9b7..78b6418 100644 --- a/src/lessons/number-of-islands/lesson.js +++ b/src/lessons/number-of-islands/lesson.js @@ -7,9 +7,9 @@ * visited set. */ import { mountLesson } from '../../lib/stepper.js'; -import { pick, onLangChange } from '../../lib/i18n.js'; +import { pick } from '../../lib/i18n.js'; import { cells, slots, stagePanel, stack, readout } from '../../lib/stage.js'; -import { t, exampleTitle, LANGUAGES, k, c, labelledRows, stageRow, stageGap, presetChips, widgetLabel } from '../../lib/kit.js'; +import { t, exampleTitle, LANGUAGES, k, c, labelledRows, stageRow, stageGap, presetChips, widgetLabel, wireWidget } from '../../lib/kit.js'; const MAX_R = 6, MAX_C = 7; @@ -542,19 +542,12 @@ function mountIslandWidget(host) { host.querySelector(`[data-r="${r}"][data-c="${c}"]`)?.focus(); } host.addEventListener('click', (ev) => { - const chip = ev.target.closest('[data-set]'); - if (chip) { state.set = Number(chip.dataset.set); state.g = QW_SETS[state.set].g.map((r) => r.split('')); return render(); } const conn = ev.target.closest('[data-eight]'); if (conn) { state.eight = conn.dataset.eight === 'true'; return render(); } const cell = ev.target.closest('[data-r]'); if (cell) toggle(cell); }); - host.addEventListener('keydown', (ev) => { - const cell = ev.target.closest('[data-r]'); - if (cell && (ev.key === 'Enter' || ev.key === ' ')) { ev.preventDefault(); toggle(cell); } - }); - onLangChange(render); - render(); + wireWidget(host, { render, choose: (i) => { state.set = i; state.g = QW_SETS[state.set].g.map((r) => r.split('')); } }); } /* ---------------- the approach, in brief ---------------- */ diff --git a/src/lessons/permutations/lesson.js b/src/lessons/permutations/lesson.js index d165749..b64d29f 100644 --- a/src/lessons/permutations/lesson.js +++ b/src/lessons/permutations/lesson.js @@ -7,9 +7,9 @@ * free numbers in the tail of `nums` itself and swapping each into place. */ import { mountLesson } from '../../lib/stepper.js'; -import { pick, onLangChange } from '../../lib/i18n.js'; +import { pick } from '../../lib/i18n.js'; import { cells, slots, stagePanel, stack, readout } from '../../lib/stage.js'; -import { t, exampleTitle, LANGUAGES, k, c, stageRow, stageGap, intList, listText, presetChips, widgetLabel } from '../../lib/kit.js'; +import { t, exampleTitle, LANGUAGES, k, c, stageRow, stageGap, intList, listText, presetChips, widgetLabel, wireWidget } from '../../lib/kit.js'; const MAX_N = 4; const fact = (n) => (n <= 1 ? 1 : n * fact(n - 1)); @@ -456,17 +456,10 @@ function mountBuildWidget(host) { (host.querySelector('[data-pick]') || host.querySelector('[data-back]'))?.focus(); } host.addEventListener('click', (ev) => { - const chip = ev.target.closest('[data-set]'); - if (chip) { state.set = Number(chip.dataset.set); state.picked = []; return render(); } const cell = ev.target.closest('[data-pick], [data-back]'); if (cell) act(cell); }); - host.addEventListener('keydown', (ev) => { - const cell = ev.target.closest('[data-pick], [data-back]'); - if (cell && (ev.key === 'Enter' || ev.key === ' ')) { ev.preventDefault(); act(cell); } - }); - onLangChange(render); - render(); + wireWidget(host, { render, choose: (i) => { state.set = i; state.picked = []; } }); } /* ---------------- the approach, in brief ---------------- */ diff --git a/src/lessons/product-of-array-except-self/lesson.js b/src/lessons/product-of-array-except-self/lesson.js index 7208735..c657779 100644 --- a/src/lessons/product-of-array-except-self/lesson.js +++ b/src/lessons/product-of-array-except-self/lesson.js @@ -8,9 +8,9 @@ * special case. */ import { mountLesson } from '../../lib/stepper.js'; -import { pick, onLangChange } from '../../lib/i18n.js'; +import { pick } from '../../lib/i18n.js'; import { blank, cells, readout, stagePanel } from '../../lib/stage.js'; -import { t, exampleTitle, LANGUAGES, k, c, stageRow, stageGap, intList, presetChips, widgetLabel } from '../../lib/kit.js'; +import { t, exampleTitle, LANGUAGES, k, c, stageRow, stageGap, intList, presetChips, widgetLabel, wireWidget } from '../../lib/kit.js'; const MAX_LEN = 10; @@ -447,14 +447,7 @@ function mountSplitWidget(host) { if (ev.target.id !== 'qw-i') return; state.i = Number(ev.target.value); render(); }); - host.addEventListener('click', (ev) => { - const chip = ev.target.closest('[data-set]'); - if (!chip) return; - state.set = Number(chip.dataset.set); - render(); - }); - onLangChange(render); - render(); + wireWidget(host, { render, choose: (i) => { state.set = i; } }); } /* ---------------- the approach, in brief ---------------- */ diff --git a/src/lessons/reverse-linked-list/lesson.js b/src/lessons/reverse-linked-list/lesson.js index e5e8239..fed7c36 100644 --- a/src/lessons/reverse-linked-list/lesson.js +++ b/src/lessons/reverse-linked-list/lesson.js @@ -12,9 +12,9 @@ * suffix, and the suffix is reachable only through the variable saved one line * earlier. Drawing it as one tidy list would hide the entire problem. */ -import { t, plural, exampleTitle, LANGUAGES, k, c, intList, presetChips, widgetLabel } from '../../lib/kit.js'; +import { t, plural, exampleTitle, LANGUAGES, k, c, intList, presetChips, widgetLabel, wireWidget } from '../../lib/kit.js'; import { mountLesson } from '../../lib/stepper.js'; -import { pick, onLangChange } from '../../lib/i18n.js'; +import { pick } from '../../lib/i18n.js'; import { cells, chain, stack, panels, slots, stagePanel } from '../../lib/stage.js'; @@ -598,15 +598,7 @@ function mountRewireWidget(host) { if (ev.target.id !== 'qw-k') return; state.k = Number(ev.target.value); render(); }); - host.addEventListener('click', (ev) => { - const chip = ev.target.closest('[data-set]'); - if (!chip) return; - state.set = Number(chip.dataset.set); - state.k = 0; - render(); - }); - onLangChange(render); - render(); + wireWidget(host, { render, choose: (i) => { state.set = i; state.k = 0; } }); } /* ---------------- the approach, in brief ---------------- diff --git a/src/lessons/reverse-nodes-in-k-group/lesson.js b/src/lessons/reverse-nodes-in-k-group/lesson.js index 44fecfe..6bee01b 100644 --- a/src/lessons/reverse-nodes-in-k-group/lesson.js +++ b/src/lessons/reverse-nodes-in-k-group/lesson.js @@ -9,9 +9,9 @@ * extra space, which is the follow-up. */ import { mountLesson } from '../../lib/stepper.js'; -import { pick, onLangChange } from '../../lib/i18n.js'; +import { pick } from '../../lib/i18n.js'; import { cells, chain, slots, stagePanel, stack, readout } from '../../lib/stage.js'; -import { t, exampleTitle, LANGUAGES, k, c, labelledRows, stageGap, intList, intValue, listText, presetChips, widgetLabel } from '../../lib/kit.js'; +import { t, exampleTitle, LANGUAGES, k, c, labelledRows, stageGap, intList, intValue, listText, presetChips, widgetLabel, wireWidget } from '../../lib/kit.js'; const MAX_N = 10; @@ -446,12 +446,7 @@ function mountGroupWidget(host) { q('[data-total]').innerHTML = `${full}${pick(t('groups reversed', 'ပြောင်းပြန် အုပ်စု'))}`; } q('[data-k]').addEventListener('input', (ev) => { state.k = Number(ev.target.value); render(); }); - host.addEventListener('click', (ev) => { - const chip = ev.target.closest('[data-set]'); - if (chip) { state.set = Number(chip.dataset.set); render(); } - }); - onLangChange(render); - render(); + wireWidget(host, { render, choose: (i) => { state.set = i; } }); } /* ---------------- the approach, in brief ---------------- */ diff --git a/src/lessons/rotate-image/lesson.js b/src/lessons/rotate-image/lesson.js index d6ec3f8..479785f 100644 --- a/src/lessons/rotate-image/lesson.js +++ b/src/lessons/rotate-image/lesson.js @@ -7,9 +7,9 @@ * ((i, j) → (j, i)), then reversing every row ((j, i) → (j, n − 1 − i)). */ import { mountLesson } from '../../lib/stepper.js'; -import { pick, onLangChange } from '../../lib/i18n.js'; +import { pick } from '../../lib/i18n.js'; import { blank, cells, slots, stagePanel, readout } from '../../lib/stage.js'; -import { t, exampleTitle, LANGUAGES, k, c, labelledRows, stageGap, presetChips, widgetLabel } from '../../lib/kit.js'; +import { t, exampleTitle, LANGUAGES, k, c, labelledRows, stageGap, presetChips, widgetLabel, wireWidget } from '../../lib/kit.js'; const MAX_N = 5; @@ -348,17 +348,10 @@ function mountTurnWidget(host) { } function pickCell(el) { state.i = Number(el.dataset.i); state.j = Number(el.dataset.j); render(); } host.addEventListener('click', (ev) => { - const chip = ev.target.closest('[data-set]'); - if (chip) { state.set = Number(chip.dataset.set); state.i = 0; state.j = 1; return render(); } const cell = ev.target.closest('[data-i]'); if (cell) pickCell(cell); }); - host.addEventListener('keydown', (ev) => { - const cell = ev.target.closest('[data-i]'); - if (cell && (ev.key === 'Enter' || ev.key === ' ')) { ev.preventDefault(); pickCell(cell); } - }); - onLangChange(render); - render(); + wireWidget(host, { render, choose: (i) => { state.set = i; state.i = 0; state.j = 1; } }); } /* ---------------- the approach, in brief ---------------- */ diff --git a/src/lessons/search-in-rotated-sorted-array/lesson.js b/src/lessons/search-in-rotated-sorted-array/lesson.js index 2099a39..ea89fb3 100644 --- a/src/lessons/search-in-rotated-sorted-array/lesson.js +++ b/src/lessons/search-in-rotated-sorted-array/lesson.js @@ -8,9 +8,9 @@ * way. The scan is here as the answer that ignores the order entirely. */ import { mountLesson } from '../../lib/stepper.js'; -import { pick, onLangChange } from '../../lib/i18n.js'; +import { pick } from '../../lib/i18n.js'; import { cells, readout, slots, stagePanel } from '../../lib/stage.js'; -import { t, exampleTitle, LANGUAGES, k, c, stageRow, stageGap, intList, intValue, widgetLabel } from '../../lib/kit.js'; +import { t, exampleTitle, LANGUAGES, k, c, stageRow, stageGap, intList, intValue, widgetLabel, wireWidget } from '../../lib/kit.js'; const MAX_LEN = 12; @@ -389,8 +389,7 @@ function mountRotationWidget(host) { if (ev.target.id !== 'qw-k') return; state.k = Number(ev.target.value); render(); }); - onLangChange(render); - render(); + wireWidget(host, { render }); } /* ---------------- the approach, in brief ---------------- */ diff --git a/src/lessons/serialize-and-deserialize-binary-tree/lesson.js b/src/lessons/serialize-and-deserialize-binary-tree/lesson.js index 6f91ab1..a969f89 100644 --- a/src/lessons/serialize-and-deserialize-binary-tree/lesson.js +++ b/src/lessons/serialize-and-deserialize-binary-tree/lesson.js @@ -9,9 +9,9 @@ * two tokens. The first recurses as deep as the tree; the second never does. */ import { mountLesson } from '../../lib/stepper.js'; -import { pick, onLangChange } from '../../lib/i18n.js'; +import { pick } from '../../lib/i18n.js'; import { cells, slots, stagePanel, tree, stack, panels } from '../../lib/stage.js'; -import { t, exampleTitle, LANGUAGES, k, c, stageRow, stageGap, presetChips, widgetLabel, stageEmpty } from '../../lib/kit.js'; +import { t, exampleTitle, LANGUAGES, k, c, stageRow, stageGap, presetChips, widgetLabel, stageEmpty, wireWidget } from '../../lib/kit.js'; import { buildTree, levelOrder, asNested, nameOf, treeInput, formatLevelOrder } from '../../lib/tree.js'; const MAX_NODES = 12; @@ -677,13 +677,10 @@ function mountMarksWidget(host) { q('[data-total]').innerHTML = `${same ? '✗' : '✓'}${pick(same ? t('ambiguous', 'မရှင်း') : t('distinct', 'ကွဲပြား'))}`; } host.addEventListener('click', (ev) => { - const chip = ev.target.closest('[data-set]'); - if (chip) { state.set = Number(chip.dataset.set); return render(); } const m = ev.target.closest('[data-marks]'); if (m) { state.marks = m.dataset.marks === 'true'; render(); } }); - onLangChange(render); - render(); + wireWidget(host, { render, choose: (i) => { state.set = i; } }); } /* ---------------- the approach, in brief ---------------- */ diff --git a/src/lessons/single-number/lesson.js b/src/lessons/single-number/lesson.js index c75d253..f49aa95 100644 --- a/src/lessons/single-number/lesson.js +++ b/src/lessons/single-number/lesson.js @@ -8,9 +8,9 @@ * 1-bits flip those columns of the accumulator, and the second copy of that * value flips them straight back. */ -import { t, exampleTitle, LANGUAGES, k, c, intList, presetChips, widgetLabel } from '../../lib/kit.js'; +import { t, exampleTitle, LANGUAGES, k, c, intList, presetChips, widgetLabel, wireWidget } from '../../lib/kit.js'; import { mountLesson } from '../../lib/stepper.js'; -import { pick, onLangChange } from '../../lib/i18n.js'; +import { pick } from '../../lib/i18n.js'; import { cells, kv, strip as bitRow, slots, stagePanel } from '../../lib/stage.js'; @@ -441,15 +441,7 @@ function mountFoldWidget(host) { if (ev.target.id !== 'qw-k') return; state.k = Number(ev.target.value); render(); }); - host.addEventListener('click', (ev) => { - const chip = ev.target.closest('[data-set]'); - if (!chip) return; - state.set = Number(chip.dataset.set); - state.k = 0; - render(); - }); - onLangChange(render); - render(); + wireWidget(host, { render, choose: (i) => { state.set = i; state.k = 0; } }); } /* ---------------- the approach, in brief ---------------- diff --git a/src/lessons/sliding-window-maximum/lesson.js b/src/lessons/sliding-window-maximum/lesson.js index 74aff04..420c3b6 100644 --- a/src/lessons/sliding-window-maximum/lesson.js +++ b/src/lessons/sliding-window-maximum/lesson.js @@ -8,9 +8,9 @@ * slides out of the window. */ import { mountLesson } from '../../lib/stepper.js'; -import { pick, onLangChange } from '../../lib/i18n.js'; +import { pick } from '../../lib/i18n.js'; import { cells, slots, stagePanel, readout } from '../../lib/stage.js'; -import { t, exampleTitle, LANGUAGES, k, c, stageRow, stageGap, intList, intValue, presetChips, widgetLabel } from '../../lib/kit.js'; +import { t, exampleTitle, LANGUAGES, k, c, stageRow, stageGap, intList, intValue, presetChips, widgetLabel, wireWidget } from '../../lib/kit.js'; const MAX_N = 12; const win = (arr) => `[${arr.join(', ')}]`; @@ -373,12 +373,7 @@ function mountDequeWidget(host) { q('[data-total]').innerHTML = `${nums[alive[0]]}${pick(t('window max', 'window max'))}`; } q('[data-lo]').addEventListener('input', (ev) => { state.lo = Number(ev.target.value); render(); }); - host.addEventListener('click', (ev) => { - const chip = ev.target.closest('[data-set]'); - if (chip) { state.set = Number(chip.dataset.set); state.lo = 0; render(); } - }); - onLangChange(render); - render(); + wireWidget(host, { render, choose: (i) => { state.set = i; state.lo = 0; } }); } /* ---------------- the approach, in brief ---------------- */ diff --git a/src/lessons/trapping-rain-water/lesson.js b/src/lessons/trapping-rain-water/lesson.js index a50db25..3739a21 100644 --- a/src/lessons/trapping-rain-water/lesson.js +++ b/src/lessons/trapping-rain-water/lesson.js @@ -8,9 +8,9 @@ * tall — so one pass with two pointers and two running maxima does it. */ import { mountLesson } from '../../lib/stepper.js'; -import { pick, onLangChange } from '../../lib/i18n.js'; +import { pick } from '../../lib/i18n.js'; import { cells, stagePanel, readout, slots } from '../../lib/stage.js'; -import { t, exampleTitle, LANGUAGES, k, c, labelledRows, stageGap, intList, listText, presetChips, widgetLabel } from '../../lib/kit.js'; +import { t, exampleTitle, LANGUAGES, k, c, labelledRows, stageGap, intList, listText, presetChips, widgetLabel, wireWidget } from '../../lib/kit.js'; const MAX_N = 14, MAX_H = 8; @@ -381,19 +381,12 @@ function mountWallWidget(host) { } const pickCol = (el) => { state.at = Number(el.dataset.col); render(); host.querySelector(`[data-col="${state.at}"]`)?.focus(); }; host.addEventListener('click', (ev) => { - const chip = ev.target.closest('[data-set]'); - if (chip) { state.set = Number(chip.dataset.set); state.h = [...QW_SETS[state.set].h]; state.at = Math.floor(state.h.length / 2); return render(); } const d = ev.target.closest('[data-d]'); if (d) { const i = Math.min(state.at, state.h.length - 1); state.h[i] = Math.max(0, Math.min(MAX_H, state.h[i] + Number(d.dataset.d))); return render(); } const col = ev.target.closest('[data-col]'); if (col) pickCol(col); }); - host.addEventListener('keydown', (ev) => { - const col = ev.target.closest('[data-col]'); - if (col && (ev.key === 'Enter' || ev.key === ' ')) { ev.preventDefault(); pickCol(col); } - }); - onLangChange(render); - render(); + wireWidget(host, { render, choose: (i) => { state.set = i; state.h = [...QW_SETS[state.set].h]; state.at = Math.floor(state.h.length / 2); } }); } /* ---------------- the approach, in brief ---------------- */ diff --git a/src/lessons/two-sum/lesson.js b/src/lessons/two-sum/lesson.js index c38478a..f2ee1a1 100644 --- a/src/lessons/two-sum/lesson.js +++ b/src/lessons/two-sum/lesson.js @@ -5,9 +5,9 @@ * complete this one?" once per element. Same answer, and the second question * can be answered without looking at anything else. */ -import { t, exampleTitle, LANGUAGES, k, c, intList, intValue, presetChips, widgetLabel } from '../../lib/kit.js'; +import { t, exampleTitle, LANGUAGES, k, c, intList, intValue, presetChips, widgetLabel, wireWidget } from '../../lib/kit.js'; import { mountLesson } from '../../lib/stepper.js'; -import { pick, onLangChange } from '../../lib/i18n.js'; +import { pick } from '../../lib/i18n.js'; import { cells, kv, readout, slots, stagePanel } from '../../lib/stage.js'; @@ -353,22 +353,10 @@ function mountComplementWidget(host) { state.target = Number(e.target.value); render(); }); host.addEventListener('click', (e) => { - const chip = e.target.closest('[data-set]'); - if (chip) { - state.set = Number(chip.dataset.set); - state.target = QW_SETS[state.set].target; - state.sel = 0; - return render(); - } const c = e.target.closest('[data-i]'); if (c) { state.sel = Number(c.dataset.i); render(); } }); - host.addEventListener('keydown', (e) => { - const c = e.target.closest('[data-i]'); - if (c && (e.key === 'Enter' || e.key === ' ')) { e.preventDefault(); state.sel = Number(c.dataset.i); render(); } - }); - onLangChange(render); - render(); + wireWidget(host, { render, choose: (i) => { state.set = i; state.target = QW_SETS[state.set].target; state.sel = 0; } }); } /* ---------------- mount ---------------- diff --git a/src/lessons/valid-anagram/lesson.js b/src/lessons/valid-anagram/lesson.js index 3d9c31d..a11d183 100644 --- a/src/lessons/valid-anagram/lesson.js +++ b/src/lessons/valid-anagram/lesson.js @@ -10,10 +10,10 @@ * resolves with pick(). Only the cost strings stay as they are: "O(n log n) * time" is notation, not prose. */ -import { t, plural, exampleTitle, LANGUAGES, k, c, verdictAnswer, labelledRows, presetChips, widgetLabel } from '../../lib/kit.js'; +import { t, plural, exampleTitle, LANGUAGES, k, c, verdictAnswer, labelledRows, presetChips, widgetLabel, wireWidget } from '../../lib/kit.js'; import { mountLesson } from '../../lib/stepper.js'; import { cells, strip, kv, panels } from '../../lib/stage.js'; -import { pick, onLangChange } from '../../lib/i18n.js'; +import { pick } from '../../lib/i18n.js'; /* ---------------- step generators ---------------- */ @@ -468,13 +468,9 @@ function mountWidget(host) { host.addEventListener('click', (ev) => { const cell = ev.target.closest('[data-i]'); if (cell) return swap(Number(cell.dataset.i), false); - const chip = ev.target.closest('[data-set], [data-act]'); + const chip = ev.target.closest('[data-act]'); if (!chip) return; - if (chip.dataset.set != null) { - state.set = Number(chip.dataset.set); - settle([...QW_SETS[state.set].t]); - state.moves = 0; - } else if (chip.dataset.act === 'shuffle') { + if (chip.dataset.act === 'shuffle') { const word = shown(); const before = word.join(''); for (let tries = 0; tries < 40 && word.join('') === before; tries++) { @@ -507,8 +503,10 @@ function mountWidget(host) { swap(Number(cell.dataset.i), true); }); - onLangChange(() => render()); - render(); + wireWidget(host, { + render, + choose: (i) => { state.set = i; settle([...QW_SETS[i].t]); state.moves = 0; }, + }); } /* ---------------- the approach, in brief ---------------- diff --git a/src/lessons/valid-palindrome/lesson.js b/src/lessons/valid-palindrome/lesson.js index e35c537..439c2a2 100644 --- a/src/lessons/valid-palindrome/lesson.js +++ b/src/lessons/valid-palindrome/lesson.js @@ -5,9 +5,9 @@ * same question by *skipping* the characters the filter would have removed — * same O(n) time, but the only state that survives a step is two integers. */ -import { t, esc, plural, exampleTitle, LANGUAGES, k, c, verdictAnswer, stageRow, presetChips, widgetLabel } from '../../lib/kit.js'; +import { t, esc, plural, exampleTitle, LANGUAGES, k, c, verdictAnswer, stageRow, presetChips, widgetLabel, wireWidget } from '../../lib/kit.js'; import { mountLesson } from '../../lib/stepper.js'; -import { pick, onLangChange } from '../../lib/i18n.js'; +import { pick } from '../../lib/i18n.js'; import { cells, readout, stagePanel } from '../../lib/stage.js'; @@ -557,15 +557,7 @@ function mountFilterWidget(host) { if (ev.target.id !== 'qw-pair') return; state.pair = Number(ev.target.value); render(); }); - host.addEventListener('click', (ev) => { - const chip = ev.target.closest('[data-set]'); - if (!chip) return; - state.set = Number(chip.dataset.set); - state.pair = 1; - render(); - }); - onLangChange(render); - render(); + wireWidget(host, { render, choose: (i) => { state.set = i; state.pair = 1; } }); } /* ---------------- the approach, in brief ---------------- diff --git a/src/lessons/valid-parentheses/lesson.js b/src/lessons/valid-parentheses/lesson.js index 64317dc..db8bed8 100644 --- a/src/lessons/valid-parentheses/lesson.js +++ b/src/lessons/valid-parentheses/lesson.js @@ -7,9 +7,9 @@ * opener nobody has closed yet?" — and the top of a stack is exactly that, so * the answer is free and one pass is enough. */ -import { t, exampleTitle, LANGUAGES, k, c, verdictAnswer, stageRow, presetChips, widgetLabel } from '../../lib/kit.js'; +import { t, exampleTitle, LANGUAGES, k, c, verdictAnswer, stageRow, presetChips, widgetLabel, wireWidget } from '../../lib/kit.js'; import { mountLesson } from '../../lib/stepper.js'; -import { pick, onLangChange } from '../../lib/i18n.js'; +import { pick } from '../../lib/i18n.js'; import { cells, stack, stagePanel } from '../../lib/stage.js'; @@ -516,15 +516,7 @@ function mountStackWidget(host) { if (e.target.id !== 'qw-k') return; state.k = Number(e.target.value); render(); }); - host.addEventListener('click', (e) => { - const chip = e.target.closest('[data-set]'); - if (!chip) return; - state.set = Number(chip.dataset.set); - state.k = 0; - render(); - }); - onLangChange(render); - render(); + wireWidget(host, { render, choose: (i) => { state.set = i; state.k = 0; } }); } /* ---------------- the approach, in brief ---------------- diff --git a/src/lessons/word-ladder/lesson.js b/src/lessons/word-ladder/lesson.js index c630373..cbffdd8 100644 --- a/src/lessons/word-ladder/lesson.js +++ b/src/lessons/word-ladder/lesson.js @@ -9,9 +9,9 @@ * however long the list is. */ import { mountLesson } from '../../lib/stepper.js'; -import { pick, onLangChange } from '../../lib/i18n.js'; +import { pick } from '../../lib/i18n.js'; import { cells, slots, stagePanel, readout } from '../../lib/stage.js'; -import { t, exampleTitle, LANGUAGES, k, c, labelledRows, stageRow, stageGap, presetChips, widgetLabel } from '../../lib/kit.js'; +import { t, exampleTitle, LANGUAGES, k, c, labelledRows, stageRow, stageGap, presetChips, widgetLabel, wireWidget } from '../../lib/kit.js'; const MAX_WORDS = 10, MAX_LEN = 5; @@ -588,13 +588,10 @@ function mountLadderWidget(host) { q('[data-total]').innerHTML = `${ans}${pick(t('words', 'စကားလုံး'))}`; } host.addEventListener('click', (ev) => { - const chip = ev.target.closest('[data-set]'); - if (chip) { state.set = Number(chip.dataset.set); state.off = new Set(); return render(); } const w = ev.target.closest('[data-word]'); if (w) { const x = w.dataset.word; state.off.has(x) ? state.off.delete(x) : state.off.add(x); render(); } }); - onLangChange(render); - render(); + wireWidget(host, { render, choose: (i) => { state.set = i; state.off = new Set(); } }); } /* ---------------- the approach, in brief ---------------- */ diff --git a/src/lessons/word-search-ii/lesson.js b/src/lessons/word-search-ii/lesson.js index f73f3d5..f9fb13f 100644 --- a/src/lessons/word-search-ii/lesson.js +++ b/src/lessons/word-search-ii/lesson.js @@ -8,9 +8,9 @@ * walked again. */ import { mountLesson } from '../../lib/stepper.js'; -import { pick, onLangChange } from '../../lib/i18n.js'; +import { pick } from '../../lib/i18n.js'; import { cells, slots, stagePanel, readout, trieOutline } from '../../lib/stage.js'; -import { t, LANGUAGES, k, c, labelledRows, stageRow, stageGap, presetChips, widgetLabel } from '../../lib/kit.js'; +import { t, LANGUAGES, k, c, labelledRows, stageRow, stageGap, presetChips, widgetLabel, wireWidget } from '../../lib/kit.js'; const MAX_SIDE = 4, MAX_WORDS = 5, MAX_LEN = 6; const DIRS = [[1, 0], [-1, 0], [0, 1], [0, -1]]; @@ -612,17 +612,10 @@ function mountPathWidget(host) { host.querySelector(`[data-r="${r}"][data-c="${cc}"]`)?.focus(); } host.addEventListener('click', (ev) => { - const chip = ev.target.closest('[data-set]'); - if (chip) { state.set = Number(chip.dataset.set); state.path = []; return render(); } const el = ev.target.closest('[data-r]'); if (el) tap(el); }); - host.addEventListener('keydown', (ev) => { - const el = ev.target.closest('[data-r]'); - if (el && (ev.key === 'Enter' || ev.key === ' ')) { ev.preventDefault(); tap(el); } - }); - onLangChange(render); - render(); + wireWidget(host, { render, choose: (i) => { state.set = i; state.path = []; } }); } /* ---------------- the approach, in brief ---------------- */ diff --git a/src/lessons/x-sum/lesson.js b/src/lessons/x-sum/lesson.js index 396777b..53d5a10 100644 --- a/src/lessons/x-sum/lesson.js +++ b/src/lessons/x-sum/lesson.js @@ -13,9 +13,9 @@ * kit itself. Its generators, stage and widget are ported from the original. */ import { mountLesson } from '../../lib/stepper.js'; -import { pick, onLangChange } from '../../lib/i18n.js'; +import { pick } from '../../lib/i18n.js'; import { cells, slots, stagePanel } from '../../lib/stage.js'; -import { t, exampleTitle, LANGUAGES, k, c, intList, intValue, presetChips, widgetLabel } from '../../lib/kit.js'; +import { t, exampleTitle, LANGUAGES, k, c, intList, intValue, presetChips, widgetLabel, wireWidget } from '../../lib/kit.js'; const MAX_N = 12; @@ -1957,15 +1957,7 @@ function mountRankWidget(host) { if (ev.target.id !== 'qw-x') return; state.x = Number(ev.target.value); render(); }); - host.addEventListener('click', (ev) => { - const chip = ev.target.closest('[data-set]'); - if (!chip) return; - state.set = Number(chip.dataset.set); - state.x = Math.min(2, new Set(QW_SETS[state.set].win).size); - render(); - }); - onLangChange(render); - render(); + wireWidget(host, { render, choose: (i) => { state.set = i; state.x = Math.min(2, new Set(QW_SETS[state.set].win).size); } }); } /* ---------------- the approach, in brief ---------------- */ diff --git a/src/lib/kit.js b/src/lib/kit.js index 004ebba..de350a7 100644 --- a/src/lib/kit.js +++ b/src/lib/kit.js @@ -5,7 +5,7 @@ * markup returns an HTML string styled by kit.css — no inline styles. */ -import { pick } from './i18n.js'; +import { pick, onLangChange } from './i18n.js'; /** A reader-facing sentence in both languages; `pick()` chooses the side. */ export const t = (en, my) => ({ en, my }); @@ -121,3 +121,28 @@ export function widgetLabel(text) { const el = document.getElementById('q-label'); if (el) el.textContent = text; } + +/** + * The wiring every part 1 widget shares, called once its markup is in `host`: + * - a click on a preset chip ([data-set]) calls `choose(index)`, then redraws + * - Enter or space on a [role="button"] cell clicks it, so the widget's one + * click listener serves the keyboard too; a widget that handles a key its + * own way (valid-anagram's focus follows the moved letter) calls + * preventDefault() in a listener added before this, and is left alone + * - `render()` runs now and again on every language change + * The stepper puts focus back on a redrawn chip or cell (see keepFocus). + */ +export function wireWidget(host, { render, choose }) { + host.addEventListener('click', (e) => { + const chip = e.target.closest('[data-set]'); + if (chip && choose) { choose(Number(chip.dataset.set)); render(); } + }); + host.addEventListener('keydown', (e) => { + if ((e.key !== 'Enter' && e.key !== ' ') || e.defaultPrevented) return; + const cell = e.target.closest('[role="button"]'); + // dispatched, not .click(): an SVG node (course-schedule's graph) has no click() + if (cell) { e.preventDefault(); cell.dispatchEvent(new MouseEvent('click', { bubbles: true })); } + }); + onLangChange(() => render()); + render(); +}