Add support for click callbacks for menu buttons
authorAlexander Ebert <ebert@woltlab.com>
Fri, 22 Sep 2023 13:01:22 +0000 (15:01 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Fri, 22 Sep 2023 13:01:22 +0000 (15:01 +0200)
ts/WoltLabSuite/Core/Ui/User/Menu/Data/Provider.ts
ts/WoltLabSuite/Core/Ui/User/Menu/View.ts
wcfsetup/install/files/js/WoltLabSuite/Core/Ui/User/Menu/View.js

index 25a9dee7611118856b2304d9277edfe56fae9022..6e14bf926bad36bcb381a4fc492da28a2fa3a5f7 100644 (file)
@@ -10,6 +10,7 @@
 import UserMenuView from "../View";
 
 export type UserMenuButton = {
+  clickCallback?: () => void;
   icon: string;
   link: string;
   name: string;
index 3f2471d8ccb5b3d1659092e73d8e7ef3aa785cb8..c7fcea36a82765b21be65f8c7eb746c3ba6ebe77 100644 (file)
@@ -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;
   }
 
index f6777803e4fbdbc31520a7cba615cb0101366564..d316f783849d7937e911c3e33c77a28821859a6d 100644 (file)
@@ -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() {