110 lines
2.6 KiB
JavaScript
110 lines
2.6 KiB
JavaScript
(function() {
|
|
'use strict';
|
|
|
|
let mainfig;
|
|
let mainimg;
|
|
let mainlink;
|
|
let altButtons;
|
|
let skipAll;
|
|
|
|
let opened = new Set;
|
|
|
|
function openCW(id, caption, focusLink = false) {
|
|
if (id) opened.add(id);
|
|
mainfig.removeChild(caption);
|
|
mainlink.tabIndex = 0;
|
|
if (focusLink) mainlink.focus();
|
|
}
|
|
|
|
function addCWListeners(id, caption) {
|
|
if (caption) {
|
|
caption.addEventListener('click', e => openCW(id, caption));
|
|
caption.addEventListener('keyup',
|
|
e => { if (e.key == 'Enter') openCW(id, caption, true) });
|
|
}
|
|
}
|
|
|
|
function setImage(id, src, href, cw) {
|
|
let caption = document.getElementById('cw');
|
|
let newCaption;
|
|
|
|
let checked = skipAll ? skipAll.checked : false;
|
|
|
|
if (!checked && !opened.has(id) && cw) {
|
|
newCaption = document.getElementById('cw-template')
|
|
.content.firstElementChild.cloneNode(true);
|
|
newCaption.querySelector('#cw-text').innerHTML = cw;
|
|
addCWListeners(id, newCaption);
|
|
}
|
|
|
|
if (caption) {
|
|
openCW(null, caption);
|
|
}
|
|
if (newCaption) {
|
|
mainfig.insertBefore(newCaption, mainlink);
|
|
mainlink.tabIndex = -1;
|
|
}
|
|
|
|
mainimg.src = src;
|
|
mainlink.href = href;
|
|
}
|
|
|
|
function activateButton(button, doPush = true) {
|
|
setImage(button.id, button.value,
|
|
button.dataset.link, button.dataset.warning);
|
|
|
|
if (doPush) history.pushState(null, '', '#' + button.id);
|
|
}
|
|
|
|
function useFragment(firstLoad = false) {
|
|
let button = altButtons[0];
|
|
|
|
let frag = decodeURIComponent(location.hash).replace(/^#/, '');
|
|
if (frag) {
|
|
let selected = document.getElementById(frag);
|
|
if (selected) button = selected;
|
|
}
|
|
|
|
let id;
|
|
|
|
if (button) {
|
|
id = button.id;
|
|
button.checked = true;
|
|
activateButton(button, false);
|
|
}
|
|
|
|
if (firstLoad) addCWListeners(id, document.getElementById('cw'));
|
|
}
|
|
|
|
function setup() {
|
|
mainfig = document.getElementById('mainfig');
|
|
mainimg = document.getElementById('mainimg');
|
|
mainlink = document.getElementById('mainlink');
|
|
skipAll = document.getElementById('skipAll');
|
|
|
|
let alts = document.getElementById('alts');
|
|
if (alts) {
|
|
let inputs = Array.from(alts.getElementsByTagName('input'));
|
|
altButtons = inputs.filter(e => e.name == 'variant');
|
|
} else {
|
|
altButtons = [];
|
|
}
|
|
|
|
for (let button of altButtons) {
|
|
button.onchange = e => { if (button.checked) activateButton(button); };
|
|
}
|
|
|
|
if (skipAll) {
|
|
skipAll.onchange = e => { if (skipAll.checked) {
|
|
let caption = document.getElementById('cw');
|
|
if (caption) { openCW(null, caption, false); }
|
|
} };
|
|
}
|
|
|
|
window.addEventListener('popstate', e => useFragment());
|
|
useFragment(true);
|
|
}
|
|
|
|
window.addEventListener('DOMContentLoaded', setup);
|
|
|
|
})();
|