export class PopupImage extends HTMLElement { constructor() { super(); this.overlay = null; this._others = null; this._thisindex = -1; this._preview = null; this._description = null; this._imageURL = ""; this._hideDLButton = false; } connectedCallback() { this.classList.add("cursor-pointer"); this.classList.add("select-none"); this._imageURL = this.getAttribute("data-image-url") || ""; this._hideDLButton = this.getAttribute("data-hide-dl-button") || false; this._preview = this.querySelector("img"); this._description = this.querySelector(".image-description"); if (this._preview) { this._preview.addEventListener("click", () => { this.showOverlay(); }); } let enclosing = this.closest("image-reel, .image-reel"); if (!enclosing) { enclosing = document; } this._others = Array.from(enclosing.querySelectorAll("popup-image:not(.hidden)")); this._thisindex = this._others.indexOf(this); } disconnectedCallback() { // Optionally remove the overlay if the element is removed from the DOM if (this.overlay && this.overlay.parentNode) { this.overlay.parentNode.removeChild(this.overlay); } } Keys(evt) { if (evt.repeat) { return; } evt.preventDefault(); if (evt.key === "ArrowRight") { this.next(); } else if (evt.key === "ArrowLeft") { this.prev(); } else if (evt.key === "Escape") { this.hideOverlay(); } } next() { if (this._others[this._thisindex + 1]) { this.hideOverlay(); this._others[this._thisindex + 1].showOverlay(); } else { document.addEventListener("keydown", this.Keys.bind(this), { once: true }); } } prev() { if (this._others[this._thisindex - 1]) { this.hideOverlay(); this._others[this._thisindex - 1].showOverlay(); } else { document.addEventListener("keydown", this.Keys.bind(this), { once: true }); } } showOverlay() { this.overlay = document.createElement("div"); this.overlay.classList.add( "fixed", "inset-0", "z-50", "bg-black/70", "flex", "items-center", "justify-center", "p-4", ); this.overlay.innerHTML = `