| 1 | // presenter-arrange.mjs — arranging the presenter window (spec §5.4, |
| 2 | // mockup #parrange). |
| 3 | // |
| 4 | // The three panels (now, next, notes) sit on a 12×12 grid. Arrange this |
| 5 | // window, in the ⋯ menu, outlines them: a panel dragged moves, its |
| 6 | // corner dragged resizes it, and a focused panel moves a cell with an |
| 7 | // arrow key and resizes with Shift and an arrow, so none of it needs a |
| 8 | // pointer (WCAG 2.5.7). Every change snaps to the grid, stays inside |
| 9 | // it, keeps each panel at least 2×2 and never lets two overlap |
| 10 | // (clampPanels). Done saves the arrangement in the window's cookie |
| 11 | // (presenter-notes.mjs writes it, notes and all); Reset puts the |
| 12 | // default back and saves that; Esc leaves without saving. A tap or a |
| 13 | // click selects a panel and gives it eight buttons, a cell a press, so |
| 14 | // arranging needs no drag (WCAG 2.5.7) and no keyboard; each panel is a |
| 15 | // named group while arranging, and each change is said aloud. |
| 16 | // |
| 17 | // The panels are placed by a stylesheet that says what the server's |
| 18 | // does (presenter_window.go's css), so the arrangement on screen while |
| 19 | // arranging is the one the next first paint draws. Below 720×560 the |
| 20 | // panels stack, the arrangement is ignored, and there is nothing to |
| 21 | // arrange. |
| 22 | // |
| 23 | // No words here: the bar's are the page's. The grid itself, as data, is |
| 24 | // presenter-grid.mjs's. |
| 25 | import { DEFAULT_PANELS, GRID, NAMES, WIDE, parsePanels, formatPanels, clampPanels, keyChange, toolChange, canChange, mirror, whereWords, arrangeCSS } from './presenter-grid.mjs' |
| 26 | |
| 27 | /** |
| 28 | * Arranging, in the window root. panels is the arrangement the page was |
| 29 | * drawn with (the cookie's, or the default); save(panels) keeps one. |
| 30 | * Returns start (the menu's Arrange), isOn, and key, the keydown |
| 31 | * handler while arranging: the window gives it every key but the |
| 32 | * clicker's. |
| 33 | * |
| 34 | * While arranging each panel is a focusable group named for what it |
| 35 | * holds (data-arrange-name) and described by the keys |
| 36 | * (#presenter-arrange-help). Focusing one, or a tap or click on it, |
| 37 | * selects it: it gets the eight buttons (presenter-arrange-tools), one |
| 38 | * cell per press, disabled where the change cannot be made, so neither |
| 39 | * a drag nor a keyboard is needed. Every change, and every selection, |
| 40 | * is said in the bar's polite live region (presenter-arrange-status). |
| 41 | */ |
| 42 | export function mountArrange ({ root, panels: initial, save }) { |
| 43 | const doc = root.ownerDocument |
| 44 | const win = doc.defaultView |
| 45 | const bar = root.querySelector('[presenter-arrange-bar]') |
| 46 | const box = root.querySelector('[presenter-panels]') |
| 47 | const tools = root.querySelector('[presenter-arrange-tools]') |
| 48 | const status = root.querySelector('[presenter-arrange-status]') |
| 49 | const help = root.querySelector('#presenter-arrange-help') |
| 50 | const els = NAMES.map(n => root.querySelector(`[presenter-panel="${n}"]`)) |
| 51 | const wide = win.matchMedia(WIDE) |
| 52 | let panels = parsePanels(initial) || parsePanels(DEFAULT_PANELS) |
| 53 | let saved = panels // what Esc goes back to |
| 54 | let on = false |
| 55 | let style = null |
| 56 | let drag = null |
| 57 | let selected = 0 |
| 58 | |
| 59 | // A right-to-left window's grid runs the other way across. |
| 60 | const rtl = () => win.getComputedStyle(root).direction === 'rtl' |
| 61 | const across = () => (rtl() ? -1 : 1) |
| 62 | const toolOf = b => mirror(toolChange(b.getAttribute('data-arrange-op')), rtl(), true) |
| 63 | const nameOf = i => els[i]?.getAttribute('data-arrange-name') || '' |
| 64 | |
| 65 | function set (next) { |
| 66 | panels = next |
| 67 | if (!style) { |
| 68 | style = doc.createElement('style') |
| 69 | style.setAttribute('presenter-arranged', '') |
| 70 | doc.head.append(style) |
| 71 | } |
| 72 | style.textContent = arrangeCSS(panels) |
| 73 | } |
| 74 | |
| 75 | function say (i) { |
| 76 | if (status) status.textContent = whereWords(root.dataset.textArrangeWhere, nameOf(i), panels[i]) |
| 77 | } |
| 78 | |
| 79 | // The selected panel's buttons sit over its corner nearer the middle |
| 80 | // of the window, across and down, so however small the panel and |
| 81 | // wherever it is they open into the grid and stay in the window. Next |
| 82 | // to it in the focus order, each disabled where its change cannot be |
| 83 | // made. |
| 84 | function tooled () { |
| 85 | if (!tools || !on) return |
| 86 | const p = panels[selected] |
| 87 | tools.style.gridArea = `${p.y + 1} / ${p.x + 1} / span ${p.h} / span ${p.w}` |
| 88 | tools.style.justifySelf = (p.x * 2 + p.w > GRID) ? 'end' : 'start' |
| 89 | tools.style.alignSelf = (p.y * 2 + p.h > GRID) ? 'end' : 'start' |
| 90 | for (const b of tools.querySelectorAll('[data-arrange-op]')) { |
| 91 | b.setAttribute('aria-disabled', String(!canChange(panels, selected, toolOf(b)))) |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | function select (i) { |
| 96 | selected = i |
| 97 | els.forEach((el, j) => el.toggleAttribute('data-selected', j === i)) |
| 98 | if (tools) { |
| 99 | tools.setAttribute('aria-label', nameOf(i)) |
| 100 | els[i].after(tools) |
| 101 | tools.hidden = false |
| 102 | } |
| 103 | tooled() |
| 104 | say(i) |
| 105 | } |
| 106 | |
| 107 | // change applied to panel i: placed, the buttons brought up to date, |
| 108 | // and said when it moved anything. |
| 109 | function change (i, next) { |
| 110 | const moved = formatPanels(next) !== formatPanels(panels) |
| 111 | set(next) |
| 112 | tooled() |
| 113 | if (moved) say(i) |
| 114 | } |
| 115 | |
| 116 | function leave () { |
| 117 | on = false |
| 118 | drag = null |
| 119 | root.removeAttribute('data-arranging') |
| 120 | bar.hidden = true |
| 121 | if (tools) tools.hidden = true |
| 122 | if (status) status.textContent = '' |
| 123 | for (const el of els) { |
| 124 | for (const a of ['tabindex', 'role', 'aria-label', 'aria-describedby', 'data-selected']) el.removeAttribute(a) |
| 125 | el.querySelector(':scope > [presenter-arrange-handle]')?.remove() |
| 126 | } |
| 127 | root.querySelector('[presenter-more]')?.focus() |
| 128 | } |
| 129 | |
| 130 | function start () { |
| 131 | if (on || !bar || !box || els.some(el => !el) || !wide.matches) return |
| 132 | on = true |
| 133 | saved = panels |
| 134 | root.setAttribute('data-arranging', '') |
| 135 | bar.hidden = false |
| 136 | els.forEach((el, i) => { |
| 137 | el.tabIndex = 0 |
| 138 | el.setAttribute('role', 'group') |
| 139 | el.setAttribute('aria-label', nameOf(i)) |
| 140 | if (help) el.setAttribute('aria-describedby', help.id) |
| 141 | const handle = doc.createElement('span') |
| 142 | handle.setAttribute('presenter-arrange-handle', '') |
| 143 | handle.setAttribute('aria-hidden', 'true') |
| 144 | el.append(handle) |
| 145 | }) |
| 146 | els[0].focus() |
| 147 | select(0) |
| 148 | } |
| 149 | |
| 150 | function cancel () { |
| 151 | set(saved) |
| 152 | leave() |
| 153 | } |
| 154 | |
| 155 | bar?.querySelector('[presenter-arrange-done]')?.addEventListener('click', () => { |
| 156 | save(formatPanels(panels)) |
| 157 | saved = panels |
| 158 | leave() |
| 159 | }) |
| 160 | bar?.querySelector('[presenter-arrange-reset]')?.addEventListener('click', () => { |
| 161 | change(selected, parsePanels(DEFAULT_PANELS)) |
| 162 | save(DEFAULT_PANELS) |
| 163 | saved = panels |
| 164 | }) |
| 165 | tools?.addEventListener('click', e => { |
| 166 | const b = e.target.closest?.('[data-arrange-op]') |
| 167 | if (!on || !b || b.getAttribute('aria-disabled') === 'true') return |
| 168 | change(selected, clampPanels(panels, selected, toolOf(b))) |
| 169 | }) |
| 170 | // Narrowed below the grid while arranging: the panels stack, and |
| 171 | // there is nothing left to arrange. |
| 172 | wide.addEventListener?.('change', () => { if (on && !wide.matches) cancel() }) |
| 173 | |
| 174 | els.forEach((el, i) => { |
| 175 | if (!el) return |
| 176 | el.addEventListener('focus', () => { if (on && selected !== i) select(i) }) |
| 177 | el.addEventListener('pointerdown', e => { |
| 178 | if (!on || e.button !== 0) return |
| 179 | e.preventDefault() |
| 180 | el.focus({ preventScroll: true }) |
| 181 | if (selected !== i) select(i) |
| 182 | el.setPointerCapture?.(e.pointerId) |
| 183 | const r = box.getBoundingClientRect() |
| 184 | drag = { |
| 185 | i, |
| 186 | id: e.pointerId, |
| 187 | resize: !!e.target.closest?.('[presenter-arrange-handle]'), |
| 188 | x: e.clientX, |
| 189 | y: e.clientY, |
| 190 | w: r.width / GRID, |
| 191 | h: r.height / GRID, |
| 192 | from: panels |
| 193 | } |
| 194 | }) |
| 195 | el.addEventListener('pointermove', e => { |
| 196 | if (!drag || drag.i !== i || drag.id !== e.pointerId) return |
| 197 | const dx = Math.round(across() * (e.clientX - drag.x) / drag.w) |
| 198 | const dy = Math.round((e.clientY - drag.y) / drag.h) |
| 199 | change(i, clampPanels(drag.from, i, drag.resize ? { dw: dx, dh: dy } : { dx, dy })) |
| 200 | }) |
| 201 | const end = e => { if (drag && drag.id === e.pointerId) drag = null } |
| 202 | el.addEventListener('pointerup', end) |
| 203 | el.addEventListener('pointercancel', end) |
| 204 | }) |
| 205 | |
| 206 | function key (e) { |
| 207 | if (e.metaKey || e.ctrlKey || e.altKey) return |
| 208 | if (e.key === 'Escape') { |
| 209 | e.preventDefault() |
| 210 | cancel() |
| 211 | return |
| 212 | } |
| 213 | const i = els.indexOf(doc.activeElement) |
| 214 | const c = i >= 0 && keyChange(e.key, e.shiftKey) |
| 215 | if (!c) return |
| 216 | e.preventDefault() |
| 217 | change(i, clampPanels(panels, i, mirror(c, rtl(), false))) |
| 218 | } |
| 219 | |
| 220 | return { start, key, isOn: () => on } |
| 221 | } |
| 222 | |