115 lines
4.5 KiB
JavaScript
115 lines
4.5 KiB
JavaScript
// gallery_lightbox.js — full-album gallery viewer for /photos/<slug>.
|
|
// A random image from the album is shown as a hero at the top of the page.
|
|
// Clicking the hero (or any thumbnail) opens a modal showing every photo of the
|
|
// album, with the album title, prev/next navigation and a thumbnail strip.
|
|
// Esc, backdrop click, or the close button close it. Dependency-free / offline.
|
|
(function () {
|
|
"use strict";
|
|
|
|
var imgs = [];
|
|
var lightbox = null;
|
|
var mainImg = null;
|
|
var idx = -1;
|
|
|
|
function collectGallery() {
|
|
var out = [];
|
|
if (document.querySelectorAll) {
|
|
var nodes = document.querySelectorAll(".gallery-img");
|
|
for (var i = 0; i < nodes.length; i++) out.push(nodes[i]);
|
|
}
|
|
return out;
|
|
}
|
|
|
|
function buildLightbox() {
|
|
// Reuse the existing viewer ONLY while it is still attached to the DOM.
|
|
// Once close() has removed it, `lightbox` must be rebuilt so the next
|
|
// thumbnail click doesn't set its src on a detached (invisible) node.
|
|
if (lightbox && lightbox.isConnected) return lightbox;
|
|
lightbox = document.getElementById("gallery-lightbox");
|
|
if (!lightbox) return null;
|
|
var closeBtn = document.querySelector(".lightbox-close");
|
|
var prevBtn = document.querySelector(".lightbox-prev");
|
|
var nextBtn = document.querySelector(".lightbox-next");
|
|
mainImg = lightbox.querySelector(".lightbox-img-main");
|
|
|
|
if (closeBtn) closeBtn.addEventListener("click", close);
|
|
if (prevBtn) prevBtn.addEventListener("click", function () { show(idx - 1); });
|
|
if (nextBtn) nextBtn.addEventListener("click", function () { show(idx + 1); });
|
|
|
|
lightbox.addEventListener("click", function (e) {
|
|
// Don't close when clicking a control button or the main image; only the
|
|
// dark backdrop should dismiss the viewer.
|
|
var t = e.target;
|
|
if (t.closest(".lightbox-close, .lightbox-prev, .lightbox-next")) return;
|
|
if (mainImg && mainImg.contains(t)) return;
|
|
close();
|
|
});
|
|
|
|
document.addEventListener("keydown", function (e) {
|
|
if (!lightbox || !lightbox.classList.contains("is-open")) return;
|
|
if (e.key === "Escape") { close(); }
|
|
else if (e.key === "ArrowRight" || e.key === "ArrowDown") { e.preventDefault(); show(idx + 1); }
|
|
else if (e.key === "ArrowLeft" || e.key === "ArrowUp") { e.preventDefault(); show(idx - 1); }
|
|
});
|
|
|
|
return lightbox;
|
|
}
|
|
|
|
// Pick a random thumbnail to feature as the hero.
|
|
function pickRandomHero() {
|
|
var g = collectGallery();
|
|
if (!g.length) return null;
|
|
return g[Math.floor(Math.random() * g.length)];
|
|
}
|
|
|
|
function show(i) {
|
|
var g = collectGallery();
|
|
if (!g.length) return;
|
|
idx = (i + g.length) % g.length;
|
|
var el = g[idx];
|
|
var src = el.getAttribute("data-src") || el.getAttribute("src") || "";
|
|
buildLightbox(); // ensure the viewer (mainImg) exists before we use it
|
|
if (mainImg && mainImg.getAttribute("src")) mainImg.removeAttribute("src"); // avoid flash
|
|
mainImg.setAttribute("src", src);
|
|
lightbox.classList.add("is-open");
|
|
document.body.style.overflow = "hidden";
|
|
}
|
|
|
|
function open() {
|
|
var heroImg = document.querySelector(".gallery-hero-img");
|
|
if (heroImg && !heroImg.getAttribute("src")) {
|
|
var heroEl = pickRandomHero();
|
|
if (heroEl) heroImg.setAttribute("src", heroEl.getAttribute("data-src") || heroEl.getAttribute("src"));
|
|
}
|
|
buildLightbox();
|
|
show(0);
|
|
}
|
|
|
|
function close() {
|
|
if (!lightbox) return;
|
|
lightbox.classList.remove("is-open");
|
|
document.body.style.overflow = "";
|
|
lightbox = null; // drop the cached node so buildLightbox() rebuilds next open
|
|
setTimeout(function () { if (!lightbox.classList.contains("is-open")) lightbox.remove(); }, 300);
|
|
}
|
|
|
|
// Wire up clicks: the hero opens the gallery (showing image 0), while any
|
|
// individual thumbnail opens the gallery showing THAT photo.
|
|
var heroFigure = document.getElementById("gallery-hero");
|
|
if (heroFigure) heroFigure.addEventListener("click", open);
|
|
var g = collectGallery();
|
|
for (let k = 0; k < g.length; k++) {
|
|
// NOTE: `let` gives each thumbnail its OWN copy of `k`, so clicking one
|
|
// shows THAT photo. With the original `var k`, every handler shared the
|
|
// final loop value (g.length) and always called show(0) instead — the bug.
|
|
g[k].addEventListener("click", function () { buildLightbox(); show(k); });
|
|
}
|
|
|
|
// Feature a random image on load.
|
|
if (document.readyState === "loading") {
|
|
document.addEventListener("DOMContentLoaded", pickRandomHero);
|
|
} else {
|
|
pickRandomHero();
|
|
}
|
|
})();
|