Files
simpleblog/static/js/lightbox.js
T
2026-11-14 17:05:01 +00:00

135 lines
4.5 KiB
JavaScript

// lightbox.js — tiny dependency-free image viewer for rendered pages.
// Click any <img> (from an uploaded TinyMCE image, or inline markdown) to open
// it full-size in an overlay within the page. Esc, backdrop click, or arrow keys
// close / navigate. Images with class "no-lightbox" (or data-no-lightbox) and
// the site banner stay non-clickable. No external CDN → works offline.
(function () {
"use strict";
var EXCLUDE_CLASSES = ["no-lightbox", "site-banner-img"];
function isExcluded(img) {
if (img.hasAttribute("data-no-lightbox")) return true;
var cls = (img.getAttribute("class") || "").toLowerCase();
for (var i = 0; i < EXCLUDE_CLASSES.length; i++) {
if (cls.indexOf(EXCLUDE_CLASSES[i]) !== -1) return true;
}
return false;
}
// Collect clickable images in document order (once).
var images = [];
var doc = document;
if (doc.querySelectorAll) {
var nodes = doc.querySelectorAll("img");
for (var n = 0; n < nodes.length; n++) {
if (!isExcluded(nodes[n])) images.push(nodes[n]);
}
}
var lightbox = null;
var imgEl = null;
var idx = -1;
function buildLightbox() {
if (lightbox) return lightbox;
lightbox = document.createElement("div");
lightbox.className = "lightbox";
lightbox.setAttribute("role", "dialog");
lightbox.setAttribute("aria-modal", "true");
lightbox.setAttribute("aria-label", "Image viewer");
var backdrop = document.createElement("div");
backdrop.className = "lightbox-backdrop";
var wrapper = document.createElement("div");
wrapper.className = "lightbox-wrapper";
imgEl = document.createElement("img");
imgEl.className = "lightbox-img";
imgEl.alt = "";
wrapper.appendChild(imgEl);
lightbox.appendChild(backdrop);
lightbox.appendChild(wrapper);
lightbox.addEventListener("click", onLightboxClick);
return lightbox;
}
function ensureLightbox() {
buildLightbox();
// Re-attach if the viewer was removed from the DOM (e.g. after a close),
// so clicking another image still reopens it.
if (lightbox && lightbox.parentNode !== document.body) {
document.body.appendChild(lightbox);
}
return lightbox;
}
function openAt(i) {
ensureLightbox(); // ensure the viewer (imgEl + lightbox) exists before we use them
idx = (i + images.length) % images.length;
var src = images[idx].getAttribute("src");
if (!src) src = images[idx].getAttribute("data-src") || "";
if (imgEl.getAttribute("src")) imgEl.removeAttribute("src"); // avoid flash
imgEl.setAttribute("src", src);
lightbox.classList.add("is-open");
document.body.style.overflow = "hidden";
}
function close() {
lightbox.classList.remove("is-open");
document.body.style.overflow = "";
setTimeout(function () { if (!lightbox.classList.contains("is-open")) lightbox.remove(); }, 300);
}
function next() { openAt(idx + 1); }
function prev() { openAt(idx - 1); }
function isClickable(img) {
if (img.hasAttribute("data-no-lightbox")) return false;
var cls = (img.getAttribute("class") || "").toLowerCase();
for (var i = 0; i < EXCLUDE_CLASSES.length; i++) {
if (cls.indexOf(EXCLUDE_CLASSES[i]) !== -1) return false;
}
return true;
}
function onImgClick(e) {
var t = e.target;
if (t.tagName !== "IMG" || !isClickable(t)) return; // delegated: currentTarget is always the listener element, never the img
e.preventDefault();
e.stopPropagation();
openAt(idx + 1);
}
function onLightboxClick(e) {
// Clicking anywhere in the viewer except the image itself closes it.
if (imgEl && imgEl.contains(e.target)) return;
close();
}
function onKeyDown(e) {
if (lightbox && lightbox.classList.contains("is-open")) {
if (e.key === "Escape") { close(); }
else if (e.key === "ArrowRight" || e.key === "ArrowDown") { e.preventDefault(); next(); }
else if (e.key === "ArrowLeft" || e.key === "ArrowUp") { e.preventDefault(); prev(); }
}
}
if (images.length > 0) {
// Delegate off the document so it survives images added later.
doc.addEventListener("click", onImgClick);
doc.addEventListener("keydown", onKeyDown);
// Re-scan on DOMContentLoaded in case images appear after load.
if (doc.readyState === "loading") {
doc.addEventListener("DOMContentLoaded", function () {
var again = doc.querySelectorAll("img");
for (var j = 0; j < again.length; j++) {
if (!images.indexOf(again[j])) images.push(again[j]);
}
});
}
}
})();