gallery/script/single.js

115 lines
2.8 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-08-16 05:31:07 -04:00
let altButtons;
2022-12-26 16:04:57 -05:00
let skipAll;
2020-07-21 04:56:55 -04:00
2020-08-04 17:07:27 -04:00
let opened = new Set;
2022-12-26 16:04:57 -05:00
function openCW(id, caption, focusLink = false) {
2020-08-08 19:19:41 -04:00
if (id) 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-08 19:19:41 -04:00
function setImage(id, src, width, height, href, cw, firstLoad) {
2020-08-04 13:14:12 -04:00
let caption = document.getElementById('cw');
2020-09-13 19:34:45 -04:00
let newCaption;
2020-08-03 13:38:14 -04:00
2022-12-29 21:18:52 -05:00
let checked = skipAll ? skipAll.checked : false;
if (!checked && !opened.has(id) && cw) {
2020-09-13 20:34:21 -04:00
newCaption = document.getElementById('cw-template')
.content.firstElementChild.cloneNode(true);
newCaption.querySelector('#cw-text').innerHTML = cw;
2020-09-18 14:14:50 -04:00
addCWListeners(id, newCaption);
2020-09-13 19:34:45 -04:00
}
if (caption) {
openCW(null, caption);
}
if (newCaption) {
mainfig.insertBefore(newCaption, mainlink);
2020-08-03 13:38:14 -04:00
mainlink.tabIndex = -1;
2020-07-17 06:29:13 -04:00
}
2020-08-08 19:22:00 -04:00
mainimg.src = src;
mainfig.dataset.width = width;
mainfig.dataset.height = height;
mainlink.href = href;
2020-07-17 06:29:13 -04:00
}
2020-08-08 19:19:41 -04:00
function activateButton(button, doPush = true, firstLoad = false) {
2020-08-04 17:07:27 -04:00
setImage(button.id, button.value,
2020-08-08 19:19:41 -04:00
button.dataset.width, button.dataset.height,
button.dataset.link, button.dataset.warning,
firstLoad);
2020-08-04 19:43:27 -04:00
if (doPush) history.pushState(null, '', '#' + button.id);
}
2020-08-08 19:19:41 -04:00
function useFragment(firstLoad = false) {
2020-08-29 08:28:21 -04:00
let button = altButtons[0];
2020-08-04 19:43:27 -04:00
2020-09-22 13:12:18 -04:00
let frag = decodeURIComponent(location.hash).replace(/^#/, '');
2020-08-04 19:43:27 -04:00
if (frag) {
2020-08-16 05:31:07 -04:00
let selected = document.getElementById(frag);
2020-08-29 08:28:21 -04:00
if (selected) button = selected;
2020-08-04 19:43:27 -04:00
}
2020-08-08 19:19:41 -04:00
let id;
2020-08-04 19:43:27 -04:00
if (button) {
2020-08-08 19:19:41 -04:00
id = button.id;
2020-08-04 19:43:27 -04:00
button.checked = true;
2020-08-08 19:19:41 -04:00
activateButton(button, false, firstLoad);
2020-08-04 19:43:27 -04:00
}
2020-08-08 19:19:41 -04:00
if (firstLoad) addCWListeners(id, document.getElementById('cw'));
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');
2022-12-26 16:04:57 -05:00
skipAll = document.getElementById('skipAll');
let alts = document.getElementById('alts');
2022-12-26 16:04:57 -05:00
if (alts) {
let inputs = Array.from(alts.getElementsByTagName('input'));
altButtons = inputs.filter(e => e.name == 'variant');
} else {
altButtons = [];
}
2020-08-04 16:55:33 -04:00
2020-08-16 05:31:07 -04:00
for (let button of altButtons) {
2020-08-03 13:38:14 -04:00
button.onchange = e => { if (button.checked) activateButton(button); };
2020-07-17 06:29:13 -04:00
}
2022-12-29 21:18:52 -05:00
if (skipAll) {
skipAll.onchange = e => { if (skipAll.checked) {
let caption = document.getElementById('cw');
if (caption) { openCW(null, caption, false); }
} };
}
2022-12-26 16:04:57 -05:00
2020-08-04 19:43:27 -04:00
window.addEventListener('popstate', useFragment);
2020-08-08 19:19:41 -04:00
useFragment(true);
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
})();