gallery/script/single.js

69 lines
1.7 KiB
JavaScript
Raw Normal View History

2020-08-03 13:38:14 -04:00
(function() {
2020-07-17 09:51:39 -04:00
'use strict';
2020-07-17 06:29:13 -04:00
2020-08-04 16:55:33 -04:00
let mainfig;
let mainimg;
let mainlink;
2020-07-21 04:56:55 -04:00
2020-08-04 17:07:27 -04:00
let opened = new Set;
function openCW(id, caption, focusLink) {
opened.add(id);
2020-08-03 13:38:14 -04:00
mainfig.removeChild(caption);
mainlink.tabIndex = 0;
if (focusLink) mainlink.focus();
2020-07-17 06:29:13 -04:00
}
2020-08-04 17:07:27 -04:00
function addCWListeners(id, caption) {
2020-08-03 13:38:14 -04:00
if (caption) {
2020-08-04 17:07:27 -04:00
caption.addEventListener('click', e => openCW(id, caption));
2020-08-03 13:38:14 -04:00
caption.addEventListener('keyup',
2020-08-04 17:07:27 -04:00
e => { if (e.key == 'Enter') openCW(id, caption, true) });
2020-08-03 13:38:14 -04:00
}
}
2020-07-17 06:29:13 -04:00
2020-08-04 17:07:27 -04:00
function setImage(id, src, href, cw) {
2020-08-04 13:14:12 -04:00
let caption = document.getElementById('cw');
2020-08-03 13:38:14 -04:00
let cwText = `<span id=cw-text>cw: <b>${cw}</b></span>`;
2020-08-04 17:07:27 -04:00
if (!opened.has(id) && cw && caption) {
2020-08-03 13:38:14 -04:00
caption.innerHTML = cwText;
} else if (caption) {
2020-08-04 17:07:27 -04:00
openCW(id, caption);
} else if (!opened.has(id) && cw) {
2020-08-03 13:38:14 -04:00
let template = document.createElement('template');
template.innerHTML = `
<figcaption id=cw aria-role=button tabindex=0>
${cwText}
</figcaption>`;
let content = template.content;
2020-08-04 17:07:27 -04:00
addCWListeners(id, content.getElementById('cw'));
2020-08-03 13:38:14 -04:00
mainfig.insertBefore(content, mainlink);
mainlink.tabIndex = -1;
2020-07-17 06:29:13 -04:00
}
2020-08-03 13:38:14 -04:00
mainimg.src = src;
mainlink.href = href;
2020-07-17 06:29:13 -04:00
}
function activateButton(button) {
2020-08-04 17:07:27 -04:00
setImage(button.id, button.value,
button.dataset.link, button.dataset.warning);
2020-07-17 06:29:13 -04:00
}
function setup() {
2020-08-04 16:55:33 -04:00
mainfig = document.getElementById('mainfig');
mainimg = document.getElementById('mainimg');
mainlink = document.getElementById('mainlink');
2020-08-03 13:38:14 -04:00
for (let button of document.querySelectorAll('#alts input')) {
button.onchange = e => { if (button.checked) activateButton(button); };
2020-07-17 06:29:13 -04:00
}
2020-08-04 13:14:12 -04:00
addCWListeners(document.getElementById('cw'));
2020-07-17 06:29:13 -04:00
}
2020-08-04 13:14:12 -04:00
window.addEventListener('DOMContentLoaded', setup);
2020-07-21 04:56:55 -04:00
})();