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