// 手机端抽屉业务模块,承接工具菜单、在线名单和房间列表的 Blade 内联脚本。 import { escapeHtml } from "./html.js"; import { renderRoomsOnlineStatusToContainer } from "./rooms.js"; let mobileDrawerEventsBound = false; // 模块级状态用于维持抽屉互斥、搜索 RAF 节流和房间列表短缓存。 let mobileDrawerOpen = null; let mobileUserListRenderTimer = null; let mobileRoomsOnlineStatusCache = null; let mobileRoomsOnlineStatusCacheAt = 0; const MOBILE_ROOMS_ONLINE_STATUS_CACHE_TTL = 10000; /** * 判断点击是否来自手机抽屉区域。 * document 代理只处理浮动按钮、遮罩和抽屉内部,避免其它区域复用 data 属性时误触发。 * * @param {Element} target 事件目标 * @returns {boolean} */ function isMobileDrawerEventTarget(target) { return Boolean(target.closest("#mobile-fabs, #mobile-drawer-mask, .mobile-drawer")); } /** * 执行手机抽屉工具入口动作。 * * @param {string} action 工具动作 * @returns {void} */ function runMobileToolAction(action) { // 抽屉工具只负责分发到现有全局函数,业务实现仍留在原模块中。 const actions = { "daily-sign-in": () => window.quickDailySignIn?.(), shop: () => window.openShopModal?.(), vip: () => window.openVipModal?.(), "save-exp": () => window.saveExp?.(), game: () => window.openGameHall?.(), bank: () => window.openBankModal?.(), marriage: () => window.openMarriageStatusModal?.(), friend: () => window.openFriendPanel?.(), avatar: () => window.openAvatarPicker?.(), settings: () => { if (typeof window.openSettingsModal === "function") { window.openSettingsModal(); return; } // 兼容设置弹窗尚未迁移为全局函数的页面。 const settingsModal = document.getElementById("settings-modal"); if (settingsModal) { settingsModal.style.display = "flex"; } }, }; actions[action]?.(); } /** * 确认并执行手机端离开房间动作。 * * @returns {void} */ function confirmMobileLeaveRoom() { // 离开房间需要保留二次确认,避免手机误触直接退出。 window.chatDialog ?.confirm("确定要离开聊天室吗?", "离开聊天室") .then((ok) => { if (ok) { window.leaveRoom?.(); } }); } /** * 打开指定手机端抽屉。 * * @param {"toolbar"|"users"|string} which 抽屉名称 * @returns {void} */ export function openMobileDrawer(which) { if (!["toolbar", "users"].includes(which)) { return; } // 切换抽屉时只关闭旧面板,不恢复 body overflow,避免中间态闪烁。 if (mobileDrawerOpen && mobileDrawerOpen !== which) { document.getElementById(`mobile-drawer-${mobileDrawerOpen}`)?.classList.remove("open"); } const drawer = document.getElementById(`mobile-drawer-${which}`); const mask = document.getElementById("mobile-drawer-mask"); if (!drawer || !mask) { return; } drawer.classList.add("open"); mask.classList.add("open"); mobileDrawerOpen = which; document.body.style.overflow = "hidden"; if (which === "users") { renderMobileUserList(); } } /** * 关闭当前打开的手机端抽屉。 * * @param {boolean} resetOverflow 是否恢复 body overflow * @returns {void} */ export function closeMobileDrawer(resetOverflow = true) { if (mobileDrawerOpen) { document.getElementById(`mobile-drawer-${mobileDrawerOpen}`)?.classList.remove("open"); mobileDrawerOpen = null; } document.getElementById("mobile-drawer-mask")?.classList.remove("open"); if (resetOverflow) { document.body.style.overflow = ""; } } /** * 切换名单抽屉内的用户/房间 Tab。 * * @param {"users"|"rooms"|string} tab Tab 名称 * @returns {void} */ export function switchMobileTab(tab) { if (!["users", "rooms"].includes(tab)) { return; } const panelUsers = document.getElementById("mob-panel-users"); const panelRooms = document.getElementById("mob-panel-rooms"); const tabUsers = document.getElementById("mob-tab-users"); const tabRooms = document.getElementById("mob-tab-rooms"); if (panelUsers) { panelUsers.style.display = tab === "users" ? "flex" : "none"; } if (panelRooms) { panelRooms.style.display = tab === "rooms" ? "block" : "none"; } syncMobileTabButton(tabUsers, tab === "users"); syncMobileTabButton(tabRooms, tab === "rooms"); if (tab === "rooms") { void loadMobileRoomList(); } } /** * 同步手机抽屉 Tab 按钮选中样式。 * * @param {HTMLElement|null} button 按钮元素 * @param {boolean} active 是否选中 * @returns {void} */ function syncMobileTabButton(button, active) { if (!button) { return; } button.style.background = active ? "#336699" : "transparent"; button.style.color = active ? "#fff" : "#336699"; button.style.fontWeight = active ? "bold" : "normal"; } /** * 将在线用户渲染到手机端名单容器。 * * @returns {void} */ export function renderMobileUserList() { const onlineUsers = window.onlineUsers; if (!onlineUsers || typeof onlineUsers !== "object") { return; } const sortValue = document.getElementById("mob-user-sort-select")?.value || "default"; const keyword = (document.getElementById("mob-user-search-input")?.value || "").trim().toLowerCase(); const container = document.getElementById("mob-online-users-list"); if (!container) { return; } if (typeof window._renderUserListToContainer === "function") { window._renderUserListToContainer(container, sortValue, keyword); } else { // 降级渲染只展示用户名,保障主渲染函数异常缺失时手机名单仍可用。 const users = Object.keys(onlineUsers); container.innerHTML = users.length ? users .filter((username) => !keyword || username.toLowerCase().includes(keyword)) .map((username) => `