mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-19 17:52:21 +00:00
Vue: the credits line and its Data attribution list are a single-file component
mountCreditsView() keeps its signature and stays the owner: it computes the credits shown once each, keeps the list's open state and its focus, takes the expand and close clicks and Escape, and renders CreditsView synchronously (as the weather readout is rendered), so focus moves as soon as a change returns. The view teleports the line and the list to the end of the body, as the script appended them, so the page's rules that select them as children of the body still apply; CreditItem draws one credit (its logo or its text, only its linked words linked, links and logos kept only for a safe scheme). The ids, classes, roles, aria attributes and text are unchanged. The list's close button is a UiCloseButton with bare, keeping the browser's padding, so it only gains the kit's classes, which carry no rules. The expand button stays a plain button: its words are its content, which a UiButton slot would not word again when the owner renders a new locale. The list's items are keyed by credit, so a link that keeps its place keeps focus through a redraw; one that goes still hands focus to the close button. New tests: the line and list follow the locale, and they are the last of the body with the kit's close button. Co-authored-by: Manjunath Paparaju <[email protected]>
This commit is contained in:
1 parent
1f34210f71
commit
0b06bc6645
7 files changed
+267
-126
No files matched your search
@@ -0,0 +1,53 @@
|
||||
<script setup>
|
||||
import { computed } from 'vue';
|
||||
import {
|
||||
LINK_SCHEMES,
|
||||
LOGO_SCHEMES,
|
||||
safeUrl,
|
||||
splitLink,
|
||||
} from '../../render/creditMarkup.js';
|
||||
|
||||
// One credit: its logo or its text, linked when it has a link. 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.
|
||||
const props = defineProps({
|
||||
credit: { type: Object, required: true },
|
||||
});
|
||||
|
||||
const href = computed(() =>
|
||||
props.credit.href ? safeUrl(props.credit.href, LINK_SCHEMES) : null,
|
||||
);
|
||||
// A logo is drawn in place of the text, which names it (its alt text).
|
||||
const logo = computed(() =>
|
||||
props.credit.logoUrl ? safeUrl(props.credit.logoUrl, LOGO_SCHEMES) : null,
|
||||
);
|
||||
// Only the credit's linked words are the link, as the source wrote it.
|
||||
const parts = computed(() =>
|
||||
href.value && !logo.value && props.credit.linkText
|
||||
? splitLink(props.credit)
|
||||
: null,
|
||||
);
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<span v-if="parts" class="gev-credit"
|
||||
>{{ parts[0]
|
||||
}}<a :href="href" target="_blank" rel="noopener noreferrer">{{
|
||||
parts[1]
|
||||
}}</a
|
||||
>{{ parts[2] }}</span
|
||||
><component
|
||||
:is="href ? 'a' : 'span'"
|
||||
v-else
|
||||
class="gev-credit"
|
||||
:href="href || undefined"
|
||||
:target="href ? '_blank' : undefined"
|
||||
:rel="href ? 'noopener noreferrer' : undefined"
|
||||
><img
|
||||
v-if="logo"
|
||||
class="gev-credit-logo"
|
||||
:src="logo"
|
||||
:alt="credit.text"
|
||||
/><template v-else>{{ credit.text }}</template></component
|
||||
>
|
||||
</template>
|
||||
@@ -0,0 +1,82 @@
|
||||
<script setup>
|
||||
import { t } from '../../i18n/index.js';
|
||||
import UiCloseButton from '../kit/UiCloseButton.vue';
|
||||
import CreditItem from './CreditItem.vue';
|
||||
|
||||
// The attribution line at the bottom left and the "Data attribution" list it
|
||||
// opens, both at the end of `to` (the page's body), where the page's rules
|
||||
// place them. The owner (src/ui/creditsView.js) computes the credits shown,
|
||||
// keeps the list's open state and its focus, and takes the clicks and keys;
|
||||
// `elements` receives the line's, the button's and the list's elements for
|
||||
// it.
|
||||
const props = defineProps({
|
||||
to: { type: Object, required: true },
|
||||
panelId: { type: String, required: true },
|
||||
/** The credits on the line, once each. */
|
||||
inline: { type: Array, required: true },
|
||||
/** The credits in the list, once each. */
|
||||
listed: { type: Array, required: true },
|
||||
open: { type: Boolean, default: false },
|
||||
/**
|
||||
* The locale its words are in: a change words them again. The template
|
||||
* never reads it; `t()` is not reactive, so a new value is what makes the
|
||||
* view render again in the new language.
|
||||
*/
|
||||
locale: { type: String, default: '' },
|
||||
elements: { type: Object, required: true },
|
||||
onExpand: { type: Function, default: () => {} },
|
||||
onClose: { type: Function, default: () => {} },
|
||||
onKeydown: { type: Function, default: () => {} },
|
||||
});
|
||||
|
||||
/** Between the line's credits, and before the list's button. */
|
||||
const SEPARATOR = ' · ';
|
||||
/** A credit's key: the same text and link are one credit. */
|
||||
const key = (credit) => `${credit.text}\u0000${credit.href ?? ''}`;
|
||||
/** Keep `name`'s element for the owner. */
|
||||
const keep = (name) => (element) => {
|
||||
props.elements[name] = element?.$el ?? element;
|
||||
};
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<Teleport :to="props.to"
|
||||
><div id="gev-credits" :ref="keep('bar')" class="gev-credits" tabindex="-1"
|
||||
><span :ref="keep('line')" class="gev-credits-line"
|
||||
><template v-for="(credit, i) in props.inline" :key="key(credit)"
|
||||
>{{ i ? SEPARATOR : '' }}<CreditItem :credit="credit" /></template
|
||||
>{{ props.inline.length && props.listed.length ? SEPARATOR : '' }}</span
|
||||
><button
|
||||
:ref="keep('expand')"
|
||||
type="button"
|
||||
class="gev-credits-expand"
|
||||
aria-haspopup="dialog"
|
||||
:aria-controls="props.panelId"
|
||||
:aria-expanded="String(props.open)"
|
||||
:hidden="props.listed.length === 0"
|
||||
@click="props.onExpand()"
|
||||
>{{ t('Data attribution') }}</button
|
||||
></div
|
||||
><div
|
||||
:id="props.panelId"
|
||||
:ref="keep('panel')"
|
||||
class="gev-credits-panel"
|
||||
role="dialog"
|
||||
:aria-labelledby="`${props.panelId}-title`"
|
||||
:hidden="!props.open"
|
||||
@keydown="props.onKeydown($event)"
|
||||
><div class="gev-credits-header"
|
||||
><span class="gev-credits-title" :id="`${props.panelId}-title`">{{
|
||||
t('Data provided by:')
|
||||
}}</span
|
||||
><UiCloseButton
|
||||
:ref="keep('close')"
|
||||
class="gev-credits-close"
|
||||
bare
|
||||
:label="t('Close data attribution')"
|
||||
@click="props.onClose()" /></div
|
||||
><ul class="gev-credits-list"
|
||||
><li v-for="credit in props.listed" :key="key(credit)"
|
||||
><CreditItem :credit="credit" /></li></ul></div
|
||||
></Teleport>
|
||||
</template>
|
||||
+55
-122
@@ -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);
|
||||
};
|
||||
}
|
||||
@@ -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'],
|
||||
);
|
||||
});
|
||||
Reference in new issue
Block a user