From 121d11d3ff92671a14844d4cbc302bbc2f688528 Mon Sep 17 00:00:00 2001 From: Sameh Khamis Date: Thu, 19 Nov 2026 01:43:57 -0800 Subject: [PATCH] Vue: the Quebec CCTV overlay is a single-file component MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit QuebecCctvOverlay renders the Québec 511 player window and its map preview, teleported to the end of the body as before, with the kit's UiCloseButton and UiButtons. createQuebecCctvOverlay() keeps its API and every decision: it renders the view synchronously from the state it computes on each change, and keeps the parts the template never binds (each video's source, playback and refresh timer, the window's drag, and the marker's per-frame visibility and place). The markup is unchanged apart from the kit's button classes. The overlay test renders in the registered document, which Vue binds when it loads, and adds Escape closing the window until the camera is picked again. Co-authored-by: Manjunath Paparaju --- docs/VUE-MIGRATION.md | 13 +- scripts/package-boundaries.json | 7 + src/ui/quebecCctvOverlay.js | 296 ++++++++++------------ src/ui/quebecCctvOverlay.test.mjs | 60 +++-- src/ui/views/panels/QuebecCctvOverlay.vue | 110 ++++++++ 5 files changed, 302 insertions(+), 184 deletions(-) create mode 100644 src/ui/views/panels/QuebecCctvOverlay.vue diff --git a/docs/VUE-MIGRATION.md b/docs/VUE-MIGRATION.md index 5d5f0c0b63..8cdeb68781 100644 --- a/docs/VUE-MIGRATION.md +++ b/docs/VUE-MIGRATION.md @@ -108,8 +108,8 @@ no tip unless the site passes a `title`; attributes and classes pass through, so each site keeps its id, feature class and look. Every close, dismiss and clear button in the views is one (`src/ui/kit/closeButtons.test.mjs` pins it); Street Level's and the space mission's keep `bare`, since their skins -keep the browser's padding. The close buttons built in script (credits, the -Quebec CCTV overlay, the guide) are not converted yet. +keep the browser's padding. The close buttons built in script (credits and +the guide) are not converted yet. `UiSearchField` takes `v-model`, a required `label` (the input's accessible name), `placeholder` and `tag` (`div`, given `role="search"` by the site, or `label`); attributes and classes go to the frame, and @@ -244,8 +244,13 @@ a feature supplying only its accent. the whole panel from the `gev-cctv` store, including the camera picker, the inline pose editor, the frame preview, source badge and typed summary; `CctvControls` keeps its listeners, the frame requests and the - live video surface), Layers (done: `LayerPanel` presents each row and - renders `LayerRows` synchronously; keyed chips and list items keep focus + live video surface; `QuebecCctvOverlay` teleports the Québec 511 player + and its map preview to the end of the body, rendered synchronously by + `createQuebecCctvOverlay()` from the view it computes, as the rail cards + are; the owner keeps every decision and the parts the template never + binds: each video's source, playback and refresh timer, the window's drag + and the marker's per-frame visibility and place), Layers (done: + `LayerPanel` presents each row and renders `LayerRows` synchronously; keyed chips and list items keep focus through the refresh their own click triggers, and clicks re-read the live descriptor before acting), Display (in progress: `DisplayPanel` renders the HUD, Sonar, Scope, Celestial, detection, bloom, sharpen and the diff --git a/scripts/package-boundaries.json b/scripts/package-boundaries.json index caf06537ef..2367606ddb 100644 --- a/scripts/package-boundaries.json +++ b/scripts/package-boundaries.json @@ -1096,6 +1096,11 @@ "src/ui/cctvVideo.js", "src/ui/cctvPresentation.js", "src/ui/quebecCctvOverlay.js", + "src/ui/views/panels/QuebecCctvOverlay.vue", + "src/ui/kit/UiButton.vue", + "src/ui/kit/UiCloseButton.vue", + "src/ui/kit/UiIcon.vue", + "src/ui/rail/classToggle.js", "src/i18n/index.js", "src/i18n/locale.js", "src/i18n/locales/ar.json", @@ -1635,6 +1640,7 @@ "src/layers/wildlifeSightings/photoText.js", "src/data/iconOrientation.js", "src/ui/quebecCctvOverlay.js", + "src/ui/views/panels/QuebecCctvOverlay.vue", "src/time/worldClock.js", "src/cockpitSubjects.js", "src/cockpitRadar.js", @@ -5459,6 +5465,7 @@ "src/layers/wildlifeSightings/iconCollection.js", "src/layers/wildlifeSightings/view.js", "src/ui/quebecCctvOverlay.js", + "src/ui/views/panels/QuebecCctvOverlay.vue", "src/time/viewerClock.js", "src/time/worldClock.js", "src/cockpitSubjects.js", diff --git a/src/ui/quebecCctvOverlay.js b/src/ui/quebecCctvOverlay.js index 2416aca945..0cf3e0a61e 100644 --- a/src/ui/quebecCctvOverlay.js +++ b/src/ui/quebecCctvOverlay.js @@ -1,3 +1,4 @@ +import { h, render as renderVue } from 'vue'; import { fromDegrees } from '../geo/ellipsoid.js'; import { horizonFrom } from '../geo/horizon.js'; import { distance } from '../geo/vector.js'; @@ -8,6 +9,7 @@ import { findQuery, } from '../render/rendererOf.js'; import { t } from '../i18n/index.js'; +import QuebecCctvOverlay from './views/panels/QuebecCctvOverlay.vue'; function isQuebecCamera(camera) { return String(camera?.sourceKind || '').startsWith('quebec-'); @@ -77,72 +79,77 @@ function mediaRefreshUrl(value) { * Attach the active Québec source to a floating browser player and map * preview. The browser contacts Québec 511 only after the viewer presses Play * for that camera; selecting it (by click, auto-hop, a scene or voice) only - * shows its details and the play control. + * shows its details and the play control. The panel and marker are the + * `QuebecCctvOverlay` view, rendered synchronously on each change. */ export function createQuebecCctvOverlay(viewer) { if (!viewer || typeof document === 'undefined') return null; const controller = new AbortController(); const { signal } = controller; - const panel = document.createElement('section'); - panel.className = 'qci-panel'; - panel.hidden = true; - panel.setAttribute('role', 'region'); - panel.setAttribute('aria-label', t('CCTV camera')); - - const header = document.createElement('header'); - header.className = 'qci-header'; - const title = document.createElement('strong'); - const close = document.createElement('button'); - close.type = 'button'; - close.textContent = '×'; - close.setAttribute('aria-label', t('Close camera preview')); - header.append(title, close); - - const coordinates = document.createElement('p'); - coordinates.className = 'qci-coordinates'; - const credit = document.createElement('p'); - credit.className = 'qci-credit'; - const live = document.createElement('video'); - live.className = 'qci-live'; - live.controls = true; - live.muted = true; - live.autoplay = true; - live.loop = true; - live.playsInline = true; - live.setAttribute('aria-label', t('CCTV camera')); - - const actions = document.createElement('div'); - actions.className = 'qci-actions'; - const play = document.createElement('button'); - play.type = 'button'; - play.className = 'qci-play'; - play.textContent = t('Play'); - const refresh = document.createElement('button'); - refresh.type = 'button'; - refresh.textContent = t('Refresh map still'); - actions.append(play, refresh); - const note = document.createElement('p'); - note.className = 'qci-note'; - const sourceLink = document.createElement('a'); - sourceLink.target = '_blank'; - sourceLink.rel = 'noopener noreferrer'; - sourceLink.textContent = t("Open this image on the provider's site"); - panel.append(header, coordinates, credit, live, actions, note, sourceLink); - - const marker = document.createElement('div'); - marker.className = 'qci-marker'; - marker.hidden = true; - const still = document.createElement('video'); - still.className = 'qci-marker-still'; - still.muted = true; - still.playsInline = true; - still.preload = 'none'; - still.setAttribute('aria-hidden', 'true'); - const markerLabel = document.createElement('span'); - markerLabel.className = 'qci-marker-label'; - marker.append(still, markerLabel); - document.body.append(panel, marker); + // What the view renders. The owner writes it where it used to write the + // elements, then paints; the view never binds the media sources, the + // panel's dragged position or the marker's per-frame visibility and place. + const view = { + panelHidden: true, + title: '', + coordinates: '', + credit: '', + creditHidden: false, + liveHidden: false, + actionsHidden: false, + playHidden: false, + refreshHidden: false, + note: '', + noteHidden: false, + sourceHref: null, + sourceHidden: false, + markerLinkOnly: false, + stillHidden: false, + markerLabel: '', + }; + const host = document.createElement('div'); + let disposed = false; + const handlers = { + onClose: closeWindow, + onPlay: startPlayback, + onRefresh: refreshStill, + onKeydown(event) { + if (event.key === 'Escape') close.click(); + }, + onHeaderPointerdown: startDrag, + onLiveError: markLiveUnavailable, + onLiveLoadeddata() { + liveUnavailable = false; + view.note = t( + 'The map still uses a separate request and may show a different moment.', + ); + view.noteHidden = false; + paint(); + }, + onStillError: markStillUnavailable, + onStillLoadeddata() { + stillUnavailable = false; + if (selected) + view.markerLabel = markerLabelText(selected, Boolean(selectedMediaUrl)); + paint(); + still.pause(); + }, + }; + /** Render the view now: the panel and marker go to the end of the body. */ + function paint() { + if (disposed) return null; + const vnode = h(QuebecCctvOverlay, { view: { ...view }, ...handlers }); + renderVue(vnode, host); + return vnode.component.exposed; + } + const elements = paint(); + const panel = elements.panel.value; + const header = elements.header.value; + const close = elements.close.value.$el; + const live = elements.live.value; + const marker = elements.marker.value; + const still = elements.still.value; let selected = null; let selectedPosition = null; @@ -166,13 +173,15 @@ export function createQuebecCctvOverlay(viewer) { function markLiveUnavailable() { liveUnavailable = true; - note.textContent = t('Source unavailable'); - note.hidden = false; + view.note = t('Source unavailable'); + view.noteHidden = false; + paint(); } function markStillUnavailable() { stillUnavailable = true; - markerLabel.textContent = t('Source unavailable'); + view.markerLabel = t('Source unavailable'); + paint(); } function startRender() { @@ -218,16 +227,17 @@ export function createQuebecCctvOverlay(viewer) { playingCameraId = selected.id; liveUnavailable = false; stillUnavailable = false; - play.hidden = true; - refresh.hidden = false; - live.hidden = false; - still.hidden = false; - marker.classList.remove('qci-marker-link-only'); - note.textContent = t( + view.playHidden = true; + view.refreshHidden = false; + view.liveHidden = false; + view.stillHidden = false; + view.markerLinkOnly = false; + view.note = t( 'The map still uses a separate request and may show a different moment.', ); - note.hidden = false; - markerLabel.textContent = markerLabelText(selected, true); + view.noteHidden = false; + view.markerLabel = markerLabelText(selected, true); + paint(); setMedia(live, selectedMediaUrl); setMedia(still, selectedMediaUrl); void live.play().catch(onPlayFailure(markLiveUnavailable)); @@ -246,7 +256,8 @@ export function createQuebecCctvOverlay(viewer) { stillUnavailable = false; stopMediaRefresh(); stopRender(); - panel.hidden = true; + view.panelHidden = true; + paint(); marker.hidden = true; setMedia(live, ''); setMedia(still, ''); @@ -263,7 +274,8 @@ export function createQuebecCctvOverlay(viewer) { Number(camera.elevationM) || 10, ); if (closedWindowCameraId === camera.id && !reselected) { - panel.hidden = true; + view.panelHidden = true; + paint(); updateMarker(); return; } @@ -276,8 +288,8 @@ export function createQuebecCctvOverlay(viewer) { playingCameraId = null; } const playing = Boolean(mediaUrl) && playingCameraId === camera.id; - title.textContent = camera.name || t('CCTV camera'); - coordinates.textContent = [ + view.title = camera.name || t('CCTV camera'); + view.coordinates = [ `${Number(camera.lat).toFixed(5)}, ${Number(camera.lon).toFixed(5)}`, Number.isFinite(camera.accuracyM) && camera.accuracyM > 0 ? `±${Math.round(camera.accuracyM)} m` @@ -286,14 +298,13 @@ export function createQuebecCctvOverlay(viewer) { ] .filter(Boolean) .join(' · '); - credit.textContent = camera.credit || camera.provider || ''; - credit.hidden = !credit.textContent; - live.hidden = !playing; - play.hidden = !mediaUrl || playing; - refresh.hidden = !playing; - actions.hidden = !mediaUrl; - note.hidden = !mediaUrl; - note.textContent = mediaUrl + view.credit = camera.credit || camera.provider || ''; + view.creditHidden = !view.credit; + view.liveHidden = !playing; + view.playHidden = !mediaUrl || playing; + view.refreshHidden = !playing; + view.actionsHidden = !mediaUrl; + view.note = mediaUrl ? !playing ? t('Video plays from Québec 511, which sees your IP address.') : liveUnavailable @@ -304,15 +315,16 @@ export function createQuebecCctvOverlay(viewer) { : sourceUrl ? '' : t('Source unavailable'); - note.hidden = !note.textContent; - sourceLink.href = sourceUrl || '#'; - sourceLink.hidden = !sourceUrl; - markerLabel.textContent = stillUnavailable + view.noteHidden = !view.note; + view.sourceHref = sourceUrl || '#'; + view.sourceHidden = !sourceUrl; + view.markerLabel = stillUnavailable ? t('Source unavailable') : markerLabelText(camera, playing); - marker.classList.toggle('qci-marker-link-only', !playing); - still.hidden = !playing; - panel.hidden = false; + view.markerLinkOnly = !playing; + view.stillHidden = !playing; + view.panelHidden = false; + paint(); if (changed || mediaUrl !== selectedMediaUrl) { selectedMediaUrl = mediaUrl; @@ -366,34 +378,6 @@ export function createQuebecCctvOverlay(viewer) { ); } - still.addEventListener( - 'loadeddata', - () => { - stillUnavailable = false; - if (selected) - markerLabel.textContent = markerLabelText( - selected, - Boolean(selectedMediaUrl), - ); - still.pause(); - }, - { signal }, - ); - still.addEventListener('error', markStillUnavailable, { signal }); - live.addEventListener('error', markLiveUnavailable, { signal }); - live.addEventListener( - 'loadeddata', - () => { - liveUnavailable = false; - note.textContent = t( - 'The map still uses a separate request and may show a different moment.', - ); - note.hidden = false; - }, - { signal }, - ); - refresh.addEventListener('click', refreshStill, { signal }); - play.addEventListener('click', startPlayback, { signal }); const resetCanvasRect = () => { canvasRect = null; }; @@ -403,49 +387,35 @@ export function createQuebecCctvOverlay(viewer) { capture: true, passive: true, }); - panel.addEventListener( - 'keydown', - (event) => { - if (event.key === 'Escape') close.click(); - }, - { signal }, - ); - close.addEventListener( - 'click', - () => { - closedWindowCameraId = selected?.id || null; - panel.hidden = true; - live.pause(); - stopMediaRefresh(); - // The floating player closes independently; keep the paused map still. - updateMarker(); - }, - { signal }, - ); - header.addEventListener( - 'pointerdown', - (event) => { - if (event.target.closest('button')) return; - const rect = panel.getBoundingClientRect(); - const offsetX = event.clientX - rect.left; - const offsetY = event.clientY - rect.top; - header.setPointerCapture(event.pointerId); - const move = (moveEvent) => { - panel.style.left = `${Math.max(0, Math.min(window.innerWidth - panel.offsetWidth, moveEvent.clientX - offsetX))}px`; - panel.style.top = `${Math.max(0, Math.min(window.innerHeight - panel.offsetHeight, moveEvent.clientY - offsetY))}px`; - panel.style.right = 'auto'; - }; - const stop = () => { - header.removeEventListener('pointermove', move); - header.removeEventListener('pointerup', stop); - header.removeEventListener('pointercancel', stop); - }; - header.addEventListener('pointermove', move, { signal }); - header.addEventListener('pointerup', stop, { once: true, signal }); - header.addEventListener('pointercancel', stop, { once: true, signal }); - }, - { signal }, - ); + function closeWindow() { + closedWindowCameraId = selected?.id || null; + view.panelHidden = true; + paint(); + live.pause(); + stopMediaRefresh(); + // The floating player closes independently; keep the paused map still. + updateMarker(); + } + function startDrag(event) { + if (event.target.closest('button')) return; + const rect = panel.getBoundingClientRect(); + const offsetX = event.clientX - rect.left; + const offsetY = event.clientY - rect.top; + header.setPointerCapture(event.pointerId); + const move = (moveEvent) => { + panel.style.left = `${Math.max(0, Math.min(window.innerWidth - panel.offsetWidth, moveEvent.clientX - offsetX))}px`; + panel.style.top = `${Math.max(0, Math.min(window.innerHeight - panel.offsetHeight, moveEvent.clientY - offsetY))}px`; + panel.style.right = 'auto'; + }; + const stop = () => { + header.removeEventListener('pointermove', move); + header.removeEventListener('pointerup', stop); + header.removeEventListener('pointercancel', stop); + }; + header.addEventListener('pointermove', move, { signal }); + header.addEventListener('pointerup', stop, { once: true, signal }); + header.addEventListener('pointercancel', stop, { once: true, signal }); + } document.addEventListener( 'visibilitychange', () => { @@ -469,8 +439,8 @@ export function createQuebecCctvOverlay(viewer) { stopRender(); setMedia(live, ''); setMedia(still, ''); - panel.remove(); - marker.remove(); + disposed = true; + renderVue(null, host); selected = null; selectedPosition = null; }, diff --git a/src/ui/quebecCctvOverlay.test.mjs b/src/ui/quebecCctvOverlay.test.mjs index ca8a04934a..386dd7547a 100644 --- a/src/ui/quebecCctvOverlay.test.mjs +++ b/src/ui/quebecCctvOverlay.test.mjs @@ -1,6 +1,6 @@ +import '../testSupport/registerDom.mjs'; import { test } from 'node:test'; import assert from 'node:assert/strict'; -import { Window } from 'happy-dom'; import { fromDegrees } from '../geo/ellipsoid.js'; import { bindFakeDraw } from '../testSupport/fakeDrawPort.mjs'; import { fakeCameraPort } from '../testSupport/fakeCameraPort.mjs'; @@ -22,23 +22,36 @@ const CAMERA = Object.freeze({ }); /** - * Mount the overlay in a happy-dom window with recorded media loads, timers - * and postRender listeners. `play` decides what each play() returns. + * Mount the overlay in the registered happy-dom window (Vue binds the + * document it loads with) with recorded media loads, timers and postRender + * listeners. `play` decides what each play() returns. */ function mountOverlay(t, { play = () => Promise.resolve() } = {}) { - const window = new Window({ url: 'http://localhost:4173/' }); - const previousWindow = globalThis.window; - const previousDocument = globalThis.document; - globalThis.window = window; - globalThis.document = window.document; + const { window } = globalThis; + const media = window.HTMLMediaElement.prototype; + const previous = { + load: media.load, + play: media.play, + pause: media.pause, + setInterval: window.setInterval, + clearInterval: window.clearInterval, + }; + t.after(() => { + media.load = previous.load; + media.play = previous.play; + media.pause = previous.pause; + window.setInterval = previous.setInterval; + window.clearInterval = previous.clearInterval; + delete window.document.hidden; + }); const loads = []; - window.HTMLMediaElement.prototype.load = function () { + media.load = function () { if (this.getAttribute('src')) loads.push(this.getAttribute('src')); }; - window.HTMLMediaElement.prototype.play = function () { + media.play = function () { return play(this); }; - window.HTMLMediaElement.prototype.pause = function () {}; + media.pause = function () {}; const intervals = new Map(); let nextTimer = 1; window.setInterval = (fn) => { @@ -65,12 +78,7 @@ function mountOverlay(t, { play = () => Promise.resolve() } = {}) { }); t.after(unbind); const overlay = createQuebecCctvOverlay(viewer); - t.after(() => { - overlay?.dispose(); - globalThis.window = previousWindow; - globalThis.document = previousDocument; - window.close(); - }); + t.after(() => overlay?.dispose()); const [live, still] = window.document.querySelectorAll('video'); return { window, @@ -203,3 +211,21 @@ test('the refresh timer reloads with a cache-buster, skips hidden tabs, and stop assert.equal(ui.renderListeners.size, 0); assert.equal(ui.window.document.querySelector('.qci-panel') === null, true); }); + +test('Escape in the window closes it and stops the refresh until the camera is picked again', (t) => { + const ui = mountOverlay(t); + ui.overlay.setCamera(CAMERA); + ui.playButton().click(); + const panel = ui.window.document.querySelector('.qci-panel'); + panel.dispatchEvent( + new ui.window.KeyboardEvent('keydown', { key: 'Escape', bubbles: true }), + ); + assert.equal(panel.hidden, true); + assert.equal(ui.intervals.size, 0); + + ui.overlay.setCamera({ ...CAMERA }); + assert.equal(panel.hidden, true, 'a routine update keeps it closed'); + ui.overlay.setCamera({ ...CAMERA }, { reselected: true }); + assert.equal(panel.hidden, false, 'picking the camera again reopens it'); + assert.equal(ui.intervals.size, 1); +}); diff --git a/src/ui/views/panels/QuebecCctvOverlay.vue b/src/ui/views/panels/QuebecCctvOverlay.vue new file mode 100644 index 0000000000..e13a5e56fb --- /dev/null +++ b/src/ui/views/panels/QuebecCctvOverlay.vue @@ -0,0 +1,110 @@ + + + +