-
Notifications
You must be signed in to change notification settings - Fork 147
Expand file tree
/
Copy pathStandaloneButton.ts
More file actions
80 lines (66 loc) · 2.27 KB
/
StandaloneButton.ts
File metadata and controls
80 lines (66 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
* Represents a button that provides a context menu with interactions.
*
* @author Marcel Werk
* @copyright 2001-2025 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
* @since 6.2
*/
import { getContextMenuOptions } from "WoltLabSuite/Core/Api/Interactions/GetContextMenuOptions";
import UiDropdownSimple from "WoltLabSuite/Core/Ui/Dropdown/Simple";
export class StandaloneButton {
#container: HTMLElement;
#providerClassName: string;
#objectId: string | number;
#redirectUrl: string;
constructor(container: HTMLElement, providerClassName: string, objectId: string | number, redirectUrl: string) {
this.#container = container;
this.#providerClassName = providerClassName;
this.#objectId = objectId;
this.#redirectUrl = redirectUrl;
this.#initInteractions();
this.#initEventListeners();
}
async #refreshContextMenu(): Promise<void> {
const response = (await getContextMenuOptions(this.#providerClassName, this.#objectId)).unwrap();
const dropdown = this.#getDropdownMenu();
if (!dropdown) {
return;
}
dropdown.innerHTML = response.template;
this.#initInteractions();
}
#getDropdownMenu(): HTMLElement | undefined {
const button = this.#container.querySelector<HTMLButtonElement>(".dropdownToggle");
if (!button) {
return undefined;
}
let dropdown = UiDropdownSimple.getDropdownMenu(button.dataset.target!);
if (!dropdown) {
dropdown = button.closest(".dropdown")!.querySelector<HTMLElement>(".dropdownMenu")!;
}
return dropdown;
}
#initInteractions(): void {
this.#getDropdownMenu()
?.querySelectorAll<HTMLButtonElement>("[data-interaction]")
.forEach((element) => {
element.addEventListener("click", () => {
this.#container.dispatchEvent(
new CustomEvent("interaction:execute", {
detail: element.dataset,
bubbles: true,
}),
);
});
});
}
#initEventListeners(): void {
this.#container.addEventListener("interaction:invalidate", () => {
void this.#refreshContextMenu();
});
this.#container.addEventListener("interaction:remove", () => {
window.location.href = this.#redirectUrl;
});
}
}