diff --git a/docs/VUE-MIGRATION.md b/docs/VUE-MIGRATION.md index 5d5f0c0b63..a085534fa2 100644 --- a/docs/VUE-MIGRATION.md +++ b/docs/VUE-MIGRATION.md @@ -108,8 +108,9 @@ 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, and so does the credits list's. The close +buttons built in script (the Quebec CCTV overlay, 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 @@ -177,6 +178,12 @@ page declares no dark `color-scheme`. Selects and number fields are and keeps native spinners dark; ranges are `.gev-quantitative-slider`, with a feature supplying only its accent. +`t()` is not reactive. Words a view passes to a kit component as a prop +(`UiCloseButton`'s `label`) follow a locale change on the view's next +render, but words in a kit component's slot do not: the compiled slot does +not render the child again. A view whose words must follow the locale at +once passes the locale itself as a prop or key, as the credits line does. + ## Phases 1. **Foundation.** Vue, Pinia and the Vite Vue plugin join the browser build @@ -368,7 +375,14 @@ a feature supplying only its accent. choices, DISMISS and "Don't show this again" call the launcher's controls; a refused save hands the tick back to the view to revert. The launcher still reads the rendered card for its hit test, keyboard trap, - focus and overflow measurement), then + focus and overflow measurement), the attribution line (for a renderer + without its own credit display, `mountCreditsView()` renders + `CreditsView` synchronously, as the weather readout is, and the view + teleports the line and its "Data attribution" list to the end of the + body, where the page's rules select them; the owner keeps the credits + shown, the list's open state and its focus, and words the view again + when the locale changes; the list's close button is a bare + `UiCloseButton`), then Cockpit (in progress: `CockpitBriefing` renders the briefing card's pages, cycle toggle, regional news and local conditions, and `CockpitContext` the Contact card's scope, navigation, counts, nearest diff --git a/scripts/format-scope.json b/scripts/format-scope.json index 76004da995..7dc54bc407 100644 --- a/scripts/format-scope.json +++ b/scripts/format-scope.json @@ -1585,6 +1585,8 @@ "src/ui/creditLineHeight.js", "src/ui/creditLineHeight.test.mjs", "src/ui/creditsView.test.mjs", + "src/ui/credits/CreditsView.vue", + "src/ui/credits/CreditItem.vue", "src/cockpitSubjectProviders.js", "src/cockpitSubjectProviders.test.mjs", "src/cockpitProgress.js", diff --git a/scripts/package-boundaries.json b/scripts/package-boundaries.json index caf06537ef..20e9c6de8f 100644 --- a/scripts/package-boundaries.json +++ b/scripts/package-boundaries.json @@ -5488,6 +5488,8 @@ "src/render/cesium/viewers.js", "src/ui/creditLineHeight.js", "src/ui/creditsView.js", + "src/ui/credits/CreditsView.vue", + "src/ui/credits/CreditItem.vue", "src/data/planespottersPhoto.js", "src/maps/keylessTerrain.js", "src/render/cesium/vectorBase.js", diff --git a/src/ui/credits/CreditItem.vue b/src/ui/credits/CreditItem.vue new file mode 100644 index 0000000000..fd3f95478c --- /dev/null +++ b/src/ui/credits/CreditItem.vue @@ -0,0 +1,53 @@ + + + diff --git a/src/ui/credits/CreditsView.vue b/src/ui/credits/CreditsView.vue new file mode 100644 index 0000000000..4181af2995 --- /dev/null +++ b/src/ui/credits/CreditsView.vue @@ -0,0 +1,82 @@ + + + diff --git a/src/ui/creditsView.js b/src/ui/creditsView.js index 03acd1358d..738d116161 100644 --- a/src/ui/creditsView.js +++ b/src/ui/creditsView.js @@ -6,16 +6,14 @@ * credits). Inline credits are shown on the line at the bottom left; the * rest are in the "Data attribution" list it opens. A credit is plain data * (`src/render/contract/credits.d.ts`): its text is set as text, and a link - * or logo is kept only for a safe scheme. + * or logo is kept only for a safe scheme. `CreditsView` renders the line and + * the list; this owner keeps the credits shown, the list's open state and + * its focus. */ +import { h, render as renderVue } from 'vue'; import { findCredits } from '../render/rendererOf.js'; -import { - LINK_SCHEMES, - LOGO_SCHEMES, - safeUrl, - splitLink, -} from '../render/creditMarkup.js'; -import { onLocaleChange, t } from '../i18n/index.js'; +import { getLocale, onLocaleChange } from '../i18n/index.js'; +import CreditsView from './credits/CreditsView.vue'; const PANEL_ID = 'gev-credits-panel'; @@ -30,42 +28,6 @@ function distinct(list) { }); } -/** One credit as DOM: its logo or its text, linked when it has a link. */ -function creditElement(doc, credit) { - const href = credit.href ? safeUrl(credit.href, LINK_SCHEMES) : null; - const logoUrl = credit.logoUrl ? safeUrl(credit.logoUrl, LOGO_SCHEMES) : null; - // Only the credit's linked words are the link, as the source wrote it. - if (href && !logoUrl && credit.linkText) { - const [before, words, after] = splitLink(credit); - const element = doc.createElement('span'); - element.className = 'gev-credit'; - const link = doc.createElement('a'); - link.href = href; - link.target = '_blank'; - link.rel = 'noopener noreferrer'; - link.textContent = words; - element.append(before, link, after); - return element; - } - const element = doc.createElement(href ? 'a' : 'span'); - element.className = 'gev-credit'; - if (href) { - element.href = href; - element.target = '_blank'; - element.rel = 'noopener noreferrer'; - } - // A logo is drawn in place of the text, which names it (its alt text). - const logo = credit.logoUrl ? safeUrl(credit.logoUrl, LOGO_SCHEMES) : null; - if (logo) { - const image = doc.createElement('img'); - image.className = 'gev-credit-logo'; - image.src = logo; - image.alt = credit.text; - element.append(image); - } else element.textContent = credit.text; - return element; -} - /** * Show `viewer`'s renderer credits at the bottom left of `root`'s page. * @param {object} viewer The scene object, bound to its renderer. @@ -76,106 +38,77 @@ export function mountCreditsView(viewer, { root = document.body } = {}) { const credits = findCredits(viewer); if (!credits || !root) return () => {}; const doc = root.ownerDocument; - const bar = doc.createElement('div'); - bar.id = 'gev-credits'; - bar.className = 'gev-credits'; - // Takes focus back from an emptied list when the line has no link. - bar.tabIndex = -1; - const line = doc.createElement('span'); - line.className = 'gev-credits-line'; - const expand = doc.createElement('button'); - expand.type = 'button'; - expand.className = 'gev-credits-expand'; - expand.setAttribute('aria-haspopup', 'dialog'); - expand.setAttribute('aria-controls', PANEL_ID); - expand.setAttribute('aria-expanded', 'false'); + // The view teleports the line and the list to the end of `root`; this + // host, never in the page, holds only its place. + const host = doc.createElement('div'); + const elements = {}; + let inline = []; + let listed = []; + let open = false; - const panel = doc.createElement('div'); - panel.id = PANEL_ID; - panel.className = 'gev-credits-panel'; - panel.setAttribute('role', 'dialog'); - panel.hidden = true; - const header = doc.createElement('div'); - header.className = 'gev-credits-header'; - const title = doc.createElement('span'); - title.className = 'gev-credits-title'; - title.id = `${PANEL_ID}-title`; - panel.setAttribute('aria-labelledby', title.id); - const close = doc.createElement('button'); - close.type = 'button'; - close.className = 'gev-credits-close'; - close.textContent = '×'; - header.append(title, close); - const list = doc.createElement('ul'); - list.className = 'gev-credits-list'; - panel.append(header, list); - bar.append(line, expand); - root.append(bar, panel); + // The view patches synchronously, so focus can move once it returns. + const paint = () => + renderVue( + h(CreditsView, { + to: root, + panelId: PANEL_ID, + inline, + listed, + open, + locale: getLocale(), + elements, + onExpand, + onClose, + onKeydown, + }), + host, + ); - const label = () => { - expand.textContent = t('Data attribution'); - title.textContent = t('Data provided by:'); - close.setAttribute('aria-label', t('Close data attribution')); - }; - - const setOpen = (open, { focus = false } = {}) => { - panel.hidden = !open; - expand.setAttribute('aria-expanded', String(open)); - if (open) close.focus(); - else if (focus) expand.focus(); + const setOpen = (next, { focus = false } = {}) => { + open = next; + paint(); + if (open) elements.close.focus(); + else if (focus) elements.expand.focus(); }; const render = (all) => { + const { bar, line, panel } = elements; const focused = panel.contains(doc.activeElement); const shown = distinct(Array.isArray(all) ? all : []); - const inline = shown.filter((credit) => credit.inline); - const listed = shown.filter((credit) => !credit.inline); - line.replaceChildren(); - inline.forEach((credit, i) => { - if (i) line.append(doc.createTextNode(' · ')); - line.append(creditElement(doc, credit)); - }); - if (inline.length && listed.length) line.append(doc.createTextNode(' · ')); - list.replaceChildren( - ...listed.map((credit) => { - const item = doc.createElement('li'); - item.append(creditElement(doc, credit)); - return item; - }), - ); - expand.hidden = listed.length === 0; - if (expand.hidden && !panel.hidden) { + inline = shown.filter((credit) => credit.inline); + listed = shown.filter((credit) => !credit.inline); + paint(); + if (listed.length === 0 && open) { // Focus in the list goes back to the line, not to the page. setOpen(false); if (focused) (line.querySelector('a') ?? bar).focus(); } else if (focused && !panel.contains(doc.activeElement)) { // A link that focus was on was redrawn: focus stays in the list. - close.focus(); + elements.close.focus(); } }; - const onExpand = () => setOpen(panel.hidden); - const onClose = () => setOpen(false, { focus: true }); + function onExpand() { + setOpen(!open); + } + function onClose() { + setOpen(false, { focus: true }); + } // Escape in the open list closes it (it takes focus as it opens). - const onKey = (event) => { - if (event.key !== 'Escape' || panel.hidden) return; + function onKeydown(event) { + if (event.key !== 'Escape' || !open) return; event.preventDefault(); setOpen(false, { focus: true }); - }; - expand.addEventListener('click', onExpand); - close.addEventListener('click', onClose); - panel.addEventListener('keydown', onKey); - label(); + } + + paint(); render(credits.list()); const offChange = credits.on('change', render); - const offLocale = onLocaleChange(label); + // The view words its text again in the new locale, as it renders. + const offLocale = onLocaleChange(paint); return () => { offChange(); offLocale(); - expand.removeEventListener('click', onExpand); - close.removeEventListener('click', onClose); - panel.removeEventListener('keydown', onKey); - bar.remove(); - panel.remove(); + renderVue(null, host); }; } diff --git a/src/ui/creditsView.test.mjs b/src/ui/creditsView.test.mjs index 17e443b52b..34a66b4255 100644 --- a/src/ui/creditsView.test.mjs +++ b/src/ui/creditsView.test.mjs @@ -4,6 +4,7 @@ import assert from 'node:assert/strict'; import { mountCreditsView } from './creditsView.js'; import { bindRenderer } from '../render/rendererOf.js'; import { createStubRenderer } from '../render/stub/index.js'; +import { setLocale } from '../i18n/index.js'; function scene() { document.body.replaceChildren(); @@ -118,10 +119,12 @@ test('focus in the list stays in it as it redraws, and goes to the line as it em const off = mountCreditsView(viewer); const panel = document.getElementById('gev-credits-panel'); document.querySelector('.gev-credits-expand').click(); - panel.querySelector('li a').focus(); + const link = panel.querySelector('li a'); + link.focus(); const removeOther = renderer.credits.addStatic({ text: 'NOAA' }); assert.equal(panel.hidden, false); assert.ok(panel.contains(document.activeElement), 'still in the list'); + assert.equal(document.activeElement, link, 'the focused link keeps it'); remove(); removeOther(); assert.equal(panel.hidden, true); @@ -152,3 +155,55 @@ test('a credit with linked words links only those words', () => { ); off(); }); + +test('the line and its list follow the locale', () => { + const { viewer, renderer } = scene(); + renderer.credits.addStatic({ text: 'NOAA' }); + const off = mountCreditsView(viewer); + const expand = document.querySelector('.gev-credits-expand'); + const close = document.querySelector('.gev-credits-close'); + try { + setLocale('fr'); + assert.equal(expand.textContent, 'Attribution des données'); + assert.equal( + document.getElementById('gev-credits-panel-title').textContent, + 'Données fournies par\u202f:', + ); + assert.equal( + close.getAttribute('aria-label'), + "Fermer l'attribution des données", + ); + } finally { + setLocale('en'); + } + assert.equal(expand.textContent, 'Data attribution'); + off(); +}); + +test('the line and its list are the last of the body, and its close button is the kit’s', () => { + const { viewer, renderer } = scene(); + document.body.append(document.createElement('main')); + renderer.credits.addStatic({ text: 'NOAA' }); + const off = mountCreditsView(viewer); + const children = [...document.body.children]; + assert.deepEqual( + children.slice(-2).map((element) => element.id), + ['gev-credits', 'gev-credits-panel'], + 'the page’s rules select them as children of the body', + ); + const close = document.querySelector('.gev-credits-close'); + assert.ok(close.classList.contains('ui-button'), 'a UiCloseButton'); + assert.equal(close.type, 'button'); + assert.equal(close.textContent, '×'); + assert.equal(close.hasAttribute('title'), false, 'no tip'); + assert.equal( + close.classList.contains('ui-close-button'), + false, + 'bare: it keeps the browser’s padding', + ); + off(); + assert.deepEqual( + [...document.body.children].map((element) => element.tagName), + ['MAIN'], + ); +});