From f2ce65c40c00bf139a26a55ce9a3da2d299d43d0 Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Fri, 22 Sep 2023 15:01:22 +0200 Subject: [PATCH] Add support for click callbacks for menu buttons --- .../Core/Ui/User/Menu/Data/Provider.ts | 1 + ts/WoltLabSuite/Core/Ui/User/Menu/View.ts | 22 ++++++++++++------- .../js/WoltLabSuite/Core/Ui/User/Menu/View.js | 18 ++++++++++----- 3 files changed, 27 insertions(+), 14 deletions(-) diff --git a/ts/WoltLabSuite/Core/Ui/User/Menu/Data/Provider.ts b/ts/WoltLabSuite/Core/Ui/User/Menu/Data/Provider.ts index 25a9dee761..6e14bf926b 100644 --- a/ts/WoltLabSuite/Core/Ui/User/Menu/Data/Provider.ts +++ b/ts/WoltLabSuite/Core/Ui/User/Menu/Data/Provider.ts @@ -10,6 +10,7 @@ import UserMenuView from "../View"; export type UserMenuButton = { + clickCallback?: () => void; icon: string; link: string; name: string; diff --git a/ts/WoltLabSuite/Core/Ui/User/Menu/View.ts b/ts/WoltLabSuite/Core/Ui/User/Menu/View.ts index 3f2471d8cc..c7fcea36a8 100644 --- a/ts/WoltLabSuite/Core/Ui/User/Menu/View.ts +++ b/ts/WoltLabSuite/Core/Ui/User/Menu/View.ts @@ -263,6 +263,20 @@ export class UserMenuView { if (button.link === "#") { link = document.createElement("button"); link.type = "button"; + + if (button.name === "markAllAsRead") { + link.addEventListener("click", (event) => { + event.preventDefault(); + + void this.markAllAsRead(); + }); + } else if (typeof button.clickCallback === "function") { + link.addEventListener("click", (event) => { + event.preventDefault(); + + button.clickCallback!(); + }); + } } else { link = document.createElement("a"); link.href = button.link; @@ -272,14 +286,6 @@ export class UserMenuView { link.title = button.title; link.innerHTML = button.icon; - if (button.name === "markAllAsRead") { - link.addEventListener("click", (event) => { - event.preventDefault(); - - void this.markAllAsRead(); - }); - } - return link; } diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/Ui/User/Menu/View.js b/wcfsetup/install/files/js/WoltLabSuite/Core/Ui/User/Menu/View.js index f6777803e4..d316f78384 100644 --- a/wcfsetup/install/files/js/WoltLabSuite/Core/Ui/User/Menu/View.js +++ b/wcfsetup/install/files/js/WoltLabSuite/Core/Ui/User/Menu/View.js @@ -215,6 +215,18 @@ define(["require", "exports", "tslib", "../../../Date/Util", "../../../StringUti if (button.link === "#") { link = document.createElement("button"); link.type = "button"; + if (button.name === "markAllAsRead") { + link.addEventListener("click", (event) => { + event.preventDefault(); + void this.markAllAsRead(); + }); + } + else if (typeof button.clickCallback === "function") { + link.addEventListener("click", (event) => { + event.preventDefault(); + button.clickCallback(); + }); + } } else { link = document.createElement("a"); @@ -223,12 +235,6 @@ define(["require", "exports", "tslib", "../../../Date/Util", "../../../StringUti link.classList.add("userMenuButton", "jsTooltip"); link.title = button.title; link.innerHTML = button.icon; - if (button.name === "markAllAsRead") { - link.addEventListener("click", (event) => { - event.preventDefault(); - void this.markAllAsRead(); - }); - } return link; } async markAllAsRead() { -- 2.20.1