From 26c099aa807d8e67944f72be04a325f37da305d5 Mon Sep 17 00:00:00 2001 From: Volpeon Date: Sat, 18 Jun 2022 00:04:21 +0200 Subject: Added page type for art pieces --- content/lightbox.js | 141 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 content/lightbox.js (limited to 'content/lightbox.js') diff --git a/content/lightbox.js b/content/lightbox.js new file mode 100644 index 0000000..a415930 --- /dev/null +++ b/content/lightbox.js @@ -0,0 +1,141 @@ +(() => { + const mainEl = document.querySelector("main"); + const lightboxContainerEl = document.querySelector(".js-lightbox"); + const lightboxEl = lightboxContainerEl.querySelector(".o-lightbox"); + const lightboxCloseEl = lightboxEl.querySelector(".o-lightbox__close-btn"); + const lightboxPrevEl = lightboxEl.querySelector(".o-lightbox__prev-btn"); + const lightboxNextEl = lightboxEl.querySelector(".o-lightbox__next-btn"); + const lightboxImgEl = lightboxEl.querySelector(".o-lightbox__img"); + const lightboxFooterEl = lightboxEl.querySelector(".o-lightbox__footer"); + const figureEls = Array.from(mainEl.querySelectorAll(".js-lightbox__image")); + + const closeable = lightboxContainerEl.classList.contains("js-lightbox--closeable"); + let currentIndex = 0; + let visible = !lightboxContainerEl.classList.contains("u-d-none"); + let mouseDown = false; + + const show = () => { + visible = true; + lightboxContainerEl.classList.remove("u-d-none"); + } + + const hide = () => { + if (closeable) { + visible = false; + lightboxContainerEl.classList.add("u-d-none"); + } + } + + const setIndex = (i) => { + if (i < 0 || i >= thumbnailEls.length) { + return; + } + thumbnailEls[currentIndex].classList.remove("is-selected"); + currentIndex = i; + lightboxImgEl.src = figureEls[i].firstElementChild.href; + thumbnailEls[i].classList.add("is-selected"); + }; + + const prev = () => { + setIndex(currentIndex > 0 ? currentIndex - 1 : figureEls.length - 1); + }; + + const next = () => { + setIndex(currentIndex < figureEls.length - 1 ? currentIndex + 1 : 0); + }; + + lightboxCloseEl?.addEventListener("click", hide); + lightboxPrevEl.addEventListener("click", prev); + lightboxNextEl.addEventListener("click", next); + + const thumbnailEls = figureEls.map((figureEl, i) => { + const go = e => { + e.preventDefault(); + show(); + setIndex(i); + }; + + let anchorEl; + let imgEl; + + if (figureEl.firstElementChild.tagName === "A") { + anchorEl = figureEl.firstElementChild; + imgEl = anchorEl.firstElementChild; + } else { + imgEl = figureEl.firstElementChild; + anchorEl = document.createElement("a"); + anchorEl.href = imgEl.src; + + figureEl.insertBefore(anchorEl, imgEl); + figureEl.removeChild(imgEl); + anchorEl.appendChild(imgEl); + } + + anchorEl.addEventListener("click", go); + + const thumbnailButtonEl = document.createElement("button"); + const thumbnailImgEl = document.createElement("img"); + + thumbnailImgEl.classList.add("o-lightbox__thumbnail-img"); + thumbnailImgEl.src = anchorEl.href; + + thumbnailButtonEl.classList.add("o-lightbox__thumbnail"); + thumbnailButtonEl.appendChild(thumbnailImgEl); + thumbnailButtonEl.addEventListener("click", go); + + if (i === currentIndex) { + thumbnailButtonEl.classList.add("is-selected"); + } + + lightboxFooterEl.appendChild(thumbnailButtonEl); + + return thumbnailButtonEl; + }); + + setIndex(0); + + if (closeable) { + document.addEventListener('mousedown', (e) => { + if (visible) { + mouseDown = !lightboxEl.contains(e.target) || lightboxEl === e.target; + } + }); + + document.addEventListener('click', (e) => { + if (visible && mouseDown) { + mouseDown = false; + e.preventDefault(); + e.stopPropagation(); + hide(); + } + }, { capture: true }); + } + + window.addEventListener("keydown", e => { + if (e.defaultPrevented || !visible) { + return; + } + + switch (e.key) { + case "Left": + case "ArrowLeft": + prev(); + break; + + case "Right": + case "ArrowRight": + next(); + break; + + case "Esc": + case "Escape": + hide(); + break; + + default: + return; + } + + e.preventDefault(); + }, true); +})(); -- cgit v1.2.3-70-g09d2