| 1 | // presenter-window.mjs — the presenter window (templates/presenter.html, |
| 2 | // spec §5), moving at once. |
| 3 | // |
| 4 | // The server draws the window, and with scripts off its forms post and |
| 5 | // redirect. This takes over: the window holds the talk |
| 6 | // (talk-driver.mjs), a press redraws Now, Next, the notes and the header |
| 7 | // from it in one task, and the server is told afterwards. Every word |
| 8 | // comes from the page (data-text-* on the root, rendered from the |
| 9 | // catalog); nothing here is English. |
| 10 | // |
| 11 | // Every press is answered in the header: the step count flashes, and |
| 12 | // the position pulses, even when the press changes nothing (next at the |
| 13 | // end of the talk). |
| 14 | import { talkOrder } from './moves.mjs' |
| 15 | import { createDriver, bindForms } from './talk-driver.mjs' |
| 16 | import { createKeys, takeEarlyKeys } from './presenter-keys.mjs' |
| 17 | import { mountPicker } from './presenter-picker.mjs' |
| 18 | import { mountNotes, readPrefs, DEFAULT_SIZE, DEFAULT_PANELS } from './presenter-notes.mjs' |
| 19 | import { mountArrange } from './presenter-arrange.mjs' |
| 20 | import { install, reveal, fill, talkPlace, indexOf, restart, createPreview, mountClips } from './talk-draw.mjs' |
| 21 | import { mountClock } from './presenter-clock.mjs' |
| 22 | import { mountSceneLayer, scenePositions, talkMove } from './scene-layer.mjs' |
| 23 | |
| 24 | function el (tag, attr, text) { |
| 25 | const e = document.createElement(tag) |
| 26 | if (attr) e.setAttribute(attr, '') |
| 27 | if (text !== undefined) e.textContent = text |
| 28 | return e |
| 29 | } |
| 30 | |
| 31 | export function mountPresenterWindow (root) { |
| 32 | const T = root.dataset |
| 33 | const $ = sel => root.querySelector(sel) |
| 34 | const head = $('[presenter-head]') |
| 35 | const pos = $('[presenter-pos]') |
| 36 | const status = $('[presenter-status]') |
| 37 | const elapsed = $('[presenter-elapsed]') |
| 38 | window.name = 'slides-presenter' |
| 39 | |
| 40 | // The header's parts that come and go with the slide, made once and |
| 41 | // hidden when there is nothing to say. |
| 42 | function part (attr, after) { |
| 43 | let e = $('[' + attr + ']') |
| 44 | if (!e) { |
| 45 | e = el('span', attr) |
| 46 | e.hidden = true |
| 47 | after.after(e) |
| 48 | } |
| 49 | return e |
| 50 | } |
| 51 | const stepcount = part('presenter-stepcount', pos) |
| 52 | if (!stepcount.firstChild) { |
| 53 | stepcount.setAttribute('role', 'status') |
| 54 | const track = el('span', 'presenter-track') |
| 55 | track.setAttribute('aria-hidden', 'true') |
| 56 | stepcount.append(track, el('span', 'presenter-steptext')) |
| 57 | } |
| 58 | const mode = part('presenter-mode', stepcount) |
| 59 | const black = part('presenter-black', mode) |
| 60 | // Now runs the slide's scenes and clips; Next shows stills and posters (plan call R5). |
| 61 | let held = null |
| 62 | const scenes = mountSceneLayer({ root: $('[presenter-panel="now"] [presenter-frame]'), deck: () => held }) |
| 63 | mountClips({ root: $('[presenter-panel="now"] [presenter-scale]') }) |
| 64 | |
| 65 | function drawHead (s, to, store) { |
| 66 | const view = store.view |
| 67 | const count = pos.querySelector('[presenter-count]') |
| 68 | if (count) count.textContent = fill(T.textProgress, talkPlace(view, to.mode, indexOf(view, to.slide))) |
| 69 | const n = Number(s.presses) || 0 |
| 70 | stepcount.hidden = n === 0 |
| 71 | if (n) { |
| 72 | const track = stepcount.querySelector('[presenter-track]') |
| 73 | while (track.children.length < n) track.append(el('i')) |
| 74 | while (track.children.length > n) track.lastChild.remove() |
| 75 | ;[...track.children].forEach((i, k) => i.toggleAttribute('data-done', k < to.step)) |
| 76 | stepcount.querySelector('[presenter-steptext]').textContent = fill(T.textStep, { n: to.step, total: n }) |
| 77 | } |
| 78 | const both = store.slides.some(x => x.full) |
| 79 | mode.hidden = !both |
| 80 | if (both) mode.textContent = to.mode === 'full' ? T.textModeFull : T.textModeCore |
| 81 | black.hidden = !to.black |
| 82 | black.textContent = T.textBlack |
| 83 | const menuMode = root.querySelector('[presenter-menu] form input[name="op"][value="mode"]') |
| 84 | const label = menuMode && menuMode.form.querySelector('button') |
| 85 | if (label) label.textContent = to.mode === 'full' ? T.textMenuCore : T.textMenuFull |
| 86 | } |
| 87 | |
| 88 | // frame is a fresh presenter-frame holding html at step, or the end. |
| 89 | function frame (html, step) { |
| 90 | const f = el('div', 'presenter-frame') |
| 91 | if (html === null) { |
| 92 | f.setAttribute('data-end', '') |
| 93 | return f |
| 94 | } |
| 95 | const scale = el('div', 'presenter-scale') |
| 96 | install(scale, html, step) |
| 97 | f.append(scale) |
| 98 | return f |
| 99 | } |
| 100 | |
| 101 | // Next shows what the next press brings: the slide's next step, the |
| 102 | // next slide of the talk, or the end, once that slide is ready |
| 103 | // (talk-draw.mjs's createPreview). nextDrawn is what it shows, so a |
| 104 | // press that leaves it the same redraws nothing. |
| 105 | let nextDrawn = '' |
| 106 | const preview = createPreview(({ html, step, words, seq }) => { |
| 107 | $('[presenter-next-label] span').textContent = words |
| 108 | const drawn = seq + ':' + step + ':' + html |
| 109 | if (drawn === nextDrawn) return |
| 110 | nextDrawn = drawn |
| 111 | $('[presenter-panel="next"] [presenter-fit]').replaceChildren(frame(html, step)) |
| 112 | }) |
| 113 | |
| 114 | function drawNext (s, to, store) { |
| 115 | const view = store.view |
| 116 | const index = indexOf(view, to.slide) |
| 117 | const n = Number(s.presses) || 0 |
| 118 | const after = talkOrder(view, to.mode).find(i => i > index) |
| 119 | if (to.step < n) preview(store, s.id, to.step + 1, T.textNextStep) |
| 120 | else if (after !== undefined) preview(store, view.slides[after].id, 0, T.textNextSlide) |
| 121 | else preview(store, null, 0, T.textNextEnd) |
| 122 | } |
| 123 | |
| 124 | // The notes: the cookie's choice, or what the server drew when there |
| 125 | // is none (its data-notes-mode and the size its style gave). |
| 126 | const notesBox = $('[presenter-notes]') |
| 127 | const saved = readPrefs() || {} |
| 128 | const notes = mountNotes({ |
| 129 | root, |
| 130 | prefs: { |
| 131 | notes: saved.notes || notesBox.getAttribute('data-notes-mode'), |
| 132 | size: saved.size || Math.round(parseFloat(getComputedStyle(notesBox).fontSize) / 2) * 2 || DEFAULT_SIZE, |
| 133 | panels: saved.panels || DEFAULT_PANELS |
| 134 | } |
| 135 | }) |
| 136 | |
| 137 | // The empty talk (every slide parked or gone): the empty state the |
| 138 | // server draws for it, and nothing left of the slide that was up. |
| 139 | const panels = $('[presenter-panels]') |
| 140 | const emptyBox = $('[presenter-empty]') |
| 141 | function drawEmpty (to, store) { |
| 142 | root.setAttribute('data-empty', '') |
| 143 | if (emptyBox) emptyBox.hidden = false |
| 144 | if (panels) panels.hidden = true |
| 145 | $('[presenter-panel="now"] [presenter-scale]').replaceChildren() |
| 146 | scenes.show('', []) |
| 147 | $('[presenter-panel="next"] [presenter-fit]').replaceChildren() |
| 148 | nextDrawn = '' |
| 149 | notes.clear() |
| 150 | const count = pos.querySelector('[presenter-count]') |
| 151 | if (count) count.textContent = '' |
| 152 | pos.hidden = true |
| 153 | stepcount.hidden = true |
| 154 | mode.hidden = true |
| 155 | black.hidden = !to.black |
| 156 | root.setAttribute('data-slide', '') |
| 157 | root.setAttribute('data-step', '0') |
| 158 | root.setAttribute('data-mode', to.mode) |
| 159 | root.setAttribute('data-blackout', to.black ? 'true' : 'false') |
| 160 | root.setAttribute('data-deck-seq', String(store.deckSeq)) |
| 161 | } |
| 162 | |
| 163 | function draw (to, from, { fresh, store }) { |
| 164 | const s = to.slide ? store.slide(to.slide) : null |
| 165 | if (!s) { |
| 166 | if (!to.slide) drawEmpty(to, store) |
| 167 | return |
| 168 | } |
| 169 | const wasEmpty = root.hasAttribute('data-empty') |
| 170 | if (wasEmpty) { |
| 171 | root.removeAttribute('data-empty') |
| 172 | if (emptyBox) emptyBox.hidden = true |
| 173 | if (panels) panels.hidden = false |
| 174 | pos.hidden = false |
| 175 | } |
| 176 | const now = $('[presenter-panel="now"] [presenter-frame]') |
| 177 | const scale = now.querySelector('[presenter-scale]') |
| 178 | const same = !wasEmpty && !fresh && from && from.slide === to.slide |
| 179 | if (!same) install(scale, s.html, to.step) |
| 180 | else if (from.step !== to.step) reveal(scale, to.step) |
| 181 | held = store |
| 182 | scenes.show(to.slide, scenePositions(scale), { jump: !talkMove(store, from, to) }) |
| 183 | now.toggleAttribute('data-black', !!to.black) |
| 184 | drawHead(s, to, store) |
| 185 | drawNext(s, to, store) |
| 186 | if (!same || from.step !== to.step) notes.show(Array.isArray(s.notes) ? s.notes : [], to.step) |
| 187 | root.setAttribute('data-slide', String(s.number)) |
| 188 | root.setAttribute('data-step', String(to.step)) |
| 189 | root.setAttribute('data-mode', to.mode) |
| 190 | root.setAttribute('data-blackout', to.black ? 'true' : 'false') |
| 191 | root.setAttribute('data-deck-seq', String(store.deckSeq)) |
| 192 | // The address says where the window was drawn when it loaded; a |
| 193 | // reload should draw where the talk is. |
| 194 | const u = new URL(location.href) |
| 195 | if (u.searchParams.has('slide') || u.searchParams.has('step')) { |
| 196 | u.searchParams.delete('slide') |
| 197 | u.searchParams.delete('step') |
| 198 | history.replaceState(history.state, '', u.href) |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | // What the header says beside the talk: how far the loading has got, |
| 203 | // or that this window is on its own. |
| 204 | let progress = '' |
| 205 | let offline = false |
| 206 | function say () { |
| 207 | if (status) status.textContent = offline ? T.textOffline : progress |
| 208 | } |
| 209 | |
| 210 | // The timer starts at the talk's first press, here at once, and from |
| 211 | // the server's word whichever window pressed (presenter-clock.mjs). |
| 212 | const clock = mountClock(elapsed, $('[data-clock]')) |
| 213 | function pressed (m) { |
| 214 | restart(stepcount, 'data-flash') |
| 215 | restart(pos, 'data-pulse') |
| 216 | clock.press(m) |
| 217 | } |
| 218 | |
| 219 | const driver = createDriver({ |
| 220 | root, |
| 221 | talkHref: T.talk, |
| 222 | streamHref: T.stream, |
| 223 | action: T.command, |
| 224 | token: root.getAttribute('data-remote-token') || '', |
| 225 | draw, |
| 226 | onLoad: (store, at) => { held = store; scenes.show(at.slide, scenePositions($('[presenter-panel="now"] [presenter-scale]')), { jump: true }) }, |
| 227 | onPressed: pressed, |
| 228 | onTimer: ms => clock.heard(ms), |
| 229 | onStatus: s => { offline = s === 'offline'; say() }, |
| 230 | onProgress: p => { |
| 231 | root.setAttribute('data-loaded', p.finished && !p.failed ? 'all' : String(p.done)) |
| 232 | progress = !p.finished ? fill(T.textLoading, { n: p.done, total: p.total }) |
| 233 | : p.failed ? fill(T.textLoadingFailed, { n: p.failed }) : '' |
| 234 | say() |
| 235 | } |
| 236 | }) |
| 237 | |
| 238 | function resetTimer () { |
| 239 | driver.timerReset() |
| 240 | clock.reset() |
| 241 | } |
| 242 | |
| 243 | const menu = $('[presenter-more-menu]') |
| 244 | // Pairing opens in a window of its own (target slides-pair), so this |
| 245 | // one keeps its talk; the menu closes behind it. |
| 246 | root.addEventListener('submit', e => { |
| 247 | if (menu && e.target.getAttribute('target') === 'slides-pair') menu.open = false |
| 248 | }) |
| 249 | bindForms(root, T.command, { |
| 250 | move: m => driver.move(m), |
| 251 | timerReset: resetTimer, |
| 252 | get store () { return driver.store } |
| 253 | }, () => { if (menu) menu.open = false }) |
| 254 | |
| 255 | // Jumping to any slide: the position opens the picker, and a choice |
| 256 | // goes through the driver like any jump. |
| 257 | const picker = mountPicker({ |
| 258 | root, |
| 259 | talk: { |
| 260 | get slides () { return driver.store ? driver.store.slides : [] }, |
| 261 | get seq () { return driver.store ? driver.store.deckSeq : 0 } |
| 262 | }, |
| 263 | position: () => ({ number: Number(root.getAttribute('data-slide')) }), |
| 264 | go: s => driver.move({ op: 'goto', slide: s.id }) |
| 265 | }) |
| 266 | |
| 267 | const onKey = createKeys({ |
| 268 | act (a) { |
| 269 | if (a.move) driver.move(a.move) |
| 270 | else if (a.jump) driver.jump(a.jump) |
| 271 | else if (a.timer) resetTimer() |
| 272 | else if (a.escape) { |
| 273 | if (picker.isOpen()) { |
| 274 | // The focus goes back to the position, and stays. |
| 275 | picker.close() |
| 276 | return |
| 277 | } |
| 278 | if (menu && menu.open) menu.open = false |
| 279 | const f = document.activeElement |
| 280 | if (f && f !== document.body && f.blur) f.blur() |
| 281 | } |
| 282 | // P opens the presenter window, which this is. |
| 283 | } |
| 284 | }) |
| 285 | // Arranging the panels (presenter-arrange.mjs), from the menu. While |
| 286 | // it is on, the clicker's keys still press; every other key is its. |
| 287 | const arrange = mountArrange({ root, panels: saved.panels || DEFAULT_PANELS, save: notes.setPanels }) |
| 288 | $('[presenter-arrange]')?.addEventListener('click', () => { |
| 289 | if (menu) menu.open = false |
| 290 | arrange.start() |
| 291 | }) |
| 292 | function keydown (e) { |
| 293 | if (arrange.isOn() && e.key !== 'PageUp' && e.key !== 'PageDown') arrange.key(e) |
| 294 | else onKey(e) |
| 295 | } |
| 296 | document.addEventListener('keydown', keydown) |
| 297 | // And the keys pressed before this script was here (static/presskeys.js). |
| 298 | takeEarlyKeys(keydown) |
| 299 | } |
| 300 | |