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 = `
${this.downloadButton()} ${this.nextButton()} ${this.prevButton()}
Popup Image ${this.description()}
`; const closeButton = this.overlay.querySelector("#closebutton"); if (closeButton) { closeButton.addEventListener("click", () => { this.hideOverlay(); }); } const nextButton = this.overlay.querySelector("#nextbtn"); if (nextButton) { nextButton.addEventListener("click", this.next.bind(this)); } const prevButton = this.overlay.querySelector("#prevbtn"); if (prevButton) { prevButton.addEventListener("click", this.prev.bind(this)); } this.overlay.addEventListener("click", (evt) => { if (evt.target === this.overlay) { this.hideOverlay(); } }); document.addEventListener("keydown", this.Keys.bind(this), { once: true }); document.body.appendChild(this.overlay); } descriptionImgClass() { if (!this.description) { return "0"; } return ""; } nextButton() { if (!this._others[this._thisindex + 1]) { return ""; } return `
Nächstes Bild
`; } prevButton() { if (!this._others[this._thisindex - 1]) { return ""; } return `
Vorheriges Bild
`; } description() { if (!this._description) { return ""; } return `
${this._description.innerHTML}
`; } downloadButton() { if (this._hideDLButton) { return ""; } return `
Bild herunterladen
`; } hideOverlay() { this.overlay.parentNode.removeChild(this.overlay); this.overlay = null; } }