96 lines
2.4 KiB
JavaScript
96 lines
2.4 KiB
JavaScript
// 聊天室图片预览层控制,迁出 Blade 内联脚本后通过事件委托接管打开和关闭。
|
|
|
|
let lightboxEventsBound = false;
|
|
|
|
/**
|
|
* 打开聊天图片大图预览层。
|
|
*
|
|
* @param {string} imageUrl 图片地址
|
|
* @param {string} imageName 图片名称
|
|
* @returns {void}
|
|
*/
|
|
export function openChatImageLightbox(imageUrl, imageName = "聊天图片") {
|
|
const lightbox = document.getElementById("chat-image-lightbox");
|
|
const imageEl = document.getElementById("chat-image-lightbox-img");
|
|
const nameEl = document.getElementById("chat-image-lightbox-name");
|
|
|
|
if (!lightbox || !imageEl || !imageUrl) {
|
|
return;
|
|
}
|
|
|
|
imageEl.src = imageUrl;
|
|
imageEl.alt = imageName;
|
|
|
|
if (nameEl) {
|
|
nameEl.textContent = imageName;
|
|
}
|
|
|
|
lightbox.style.display = "block";
|
|
document.body.style.overflow = "hidden";
|
|
}
|
|
|
|
/**
|
|
* 关闭聊天图片大图预览层。
|
|
*
|
|
* @param {Event|null} event 点击事件
|
|
* @returns {void}
|
|
*/
|
|
export function closeChatImageLightbox(event = null) {
|
|
if (event && event.target !== event.currentTarget) {
|
|
return;
|
|
}
|
|
|
|
const lightbox = document.getElementById("chat-image-lightbox");
|
|
if (!lightbox) {
|
|
return;
|
|
}
|
|
|
|
lightbox.style.display = "none";
|
|
|
|
const imageEl = document.getElementById("chat-image-lightbox-img");
|
|
if (imageEl) {
|
|
imageEl.src = "";
|
|
}
|
|
|
|
document.body.style.overflow = "";
|
|
}
|
|
|
|
/**
|
|
* 绑定聊天图片预览层事件委托。
|
|
*
|
|
* @returns {void}
|
|
*/
|
|
export function initChatImageLightboxEvents() {
|
|
if (lightboxEventsBound) {
|
|
return;
|
|
}
|
|
|
|
lightboxEventsBound = true;
|
|
document.addEventListener("click", (event) => {
|
|
if (!(event.target instanceof Element)) {
|
|
return;
|
|
}
|
|
|
|
const imagePreview = event.target.closest("[data-chat-image-lightbox-image]");
|
|
if (imagePreview) {
|
|
return;
|
|
}
|
|
|
|
const opener = event.target.closest("[data-chat-image-lightbox-open]");
|
|
if (opener) {
|
|
event.preventDefault();
|
|
openChatImageLightbox(opener.dataset.full || opener.href, opener.dataset.alt || "聊天图片");
|
|
return;
|
|
}
|
|
|
|
if (event.target.closest("[data-chat-image-lightbox-close]")) {
|
|
event.preventDefault();
|
|
closeChatImageLightbox();
|
|
}
|
|
});
|
|
}
|
|
|
|
if (typeof document !== "undefined") {
|
|
initChatImageLightboxEvents();
|
|
}
|