remember opened cws

This commit is contained in:
Rhiannon Morris 2020-08-04 23:07:27 +02:00
parent f9bf7ccb46
commit 3639a1126a

View file

@ -5,36 +5,39 @@ let mainfig;
let mainimg;
let mainlink;
function openCW(caption, focusLink) {
let opened = new Set;
function openCW(id, caption, focusLink) {
opened.add(id);
mainfig.removeChild(caption);
mainlink.tabIndex = 0;
if (focusLink) mainlink.focus();
}
function addCWListeners(caption) {
function addCWListeners(id, caption) {
if (caption) {
caption.addEventListener('click', e => openCW(caption));
caption.addEventListener('click', e => openCW(id, caption));
caption.addEventListener('keyup',
e => { if (e.key == 'Enter') openCW(caption, true) });
e => { if (e.key == 'Enter') openCW(id, caption, true) });
}
}
function setImage(src, href, cw) {
function setImage(id, src, href, cw) {
let caption = document.getElementById('cw');
let cwText = `<span id=cw-text>cw: <b>${cw}</b></span>`;
if (cw && caption) {
if (!opened.has(id) && cw && caption) {
caption.innerHTML = cwText;
} else if (caption) {
openCW(caption);
} else if (cw) {
openCW(id, caption);
} else if (!opened.has(id) && cw) {
let template = document.createElement('template');
template.innerHTML = `
<figcaption id=cw aria-role=button tabindex=0>
${cwText}
</figcaption>`;
let content = template.content;
addCWListeners(content.getElementById('cw'));
addCWListeners(id, content.getElementById('cw'));
mainfig.insertBefore(content, mainlink);
mainlink.tabIndex = -1;
}
@ -44,7 +47,8 @@ function setImage(src, href, cw) {
}
function activateButton(button) {
setImage(button.value, button.dataset.link, button.dataset.warning);
setImage(button.id, button.value,
button.dataset.link, button.dataset.warning);
}
function setup() {