Files
chatroom/resources/js/chat-room/toolbar.js
T
2026-04-30 09:40:50 +08:00

107 lines
3.0 KiB
JavaScript

// 聊天室竖向工具条事件绑定,替代顶部工具按钮内联 onclick。
let toolbarEventsBound = false;
/**
* 执行竖向工具条动作。
*
* @param {string} action 工具条动作
* @returns {void}
*/
export function runToolbarAction(action) {
// 工具条只做入口分发,具体业务仍由原有全局函数负责。
const actions = {
shop: () => window.openShopModal?.(),
ride: () => window.openRideModal?.(),
vip: () => window.openVipModal?.(),
"save-exp": () => window.saveExp?.(),
game: () => window.openGameHall?.(),
earn: () => window.dispatchEvent(new CustomEvent("open-earn-panel")),
bank: () => window.openBankModal?.(),
marriage: () => window.openMarriageStatusModal?.(),
friend: () => window.openFriendPanel?.(),
avatar: () => window.openAvatarPicker?.(),
settings: () => window.openSettingsModal?.(),
guestbook: () => window.openGuestbookModal?.(),
feedback: () => window.openFeedbackModal?.(),
};
actions[action]?.();
}
/**
* 执行功能菜单快捷入口,并在打开目标面板前关闭功能菜单。
*
* @param {string} action 快捷入口动作
* @returns {void}
*/
export function runFeatureShortcut(action) {
// 功能菜单和竖向工具条共享同一组业务入口,只额外关闭菜单浮层。
window.closeFeatureMenu?.();
runToolbarAction(action);
}
/**
* 确认并执行离开聊天室动作。
*
* @returns {void}
*/
function confirmToolbarLeaveRoom() {
// 离开房间保留二次确认,避免误点竖向工具条直接退出。
window.chatDialog
?.confirm("确定要离开聊天室吗?", "离开聊天室")
.then((ok) => {
if (ok) {
window.leaveRoom?.();
}
});
}
/**
* 绑定竖向工具条按钮事件。
*
* @returns {void}
*/
// ── 挂载到 window ──
window.runFeatureShortcut = runFeatureShortcut;
export function bindToolbarControls() {
if (toolbarEventsBound || typeof document === "undefined") {
return;
}
toolbarEventsBound = true;
document.addEventListener("click", (event) => {
if (!(event.target instanceof Element)) {
return;
}
const actionButton = event.target.closest("[data-toolbar-action]");
if (actionButton) {
event.preventDefault();
const action = actionButton.getAttribute("data-toolbar-action") || "";
if (action === "leave") {
confirmToolbarLeaveRoom();
return;
}
runToolbarAction(action);
return;
}
const urlButton = event.target.closest("[data-toolbar-url]");
if (!urlButton) {
return;
}
event.preventDefault();
// 后端路由保留在 Blade data 属性中,Vite 模块不硬编码 Laravel URL。
const url = urlButton.getAttribute("data-toolbar-url");
if (url) {
window.open(url, "_blank");
}
});
}