Merge pull request #196 from WoltLab/event-update-user-menu-item
[GitHub/WoltLab/com.woltlab.wcf.conversation.git] / files / js / WoltLabSuite / Core / Conversation / Ui / User / Menu / Data / Conversation.js
CommitLineData
b297c618
AE
1/**
2 * User menu for notifications.
3 *
4 * @author Alexander Ebert
5 * @copyright 2001-2021 WoltLab GmbH
6 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
b297c618
AE
7 * @woltlabExcludeBundle tiny
8 */
9define(["require", "exports", "tslib", "WoltLabSuite/Core/Ajax", "WoltLabSuite/Core/Ui/User/Menu/View", "WoltLabSuite/Core/Ui/User/Menu/Manager"], function (require, exports, tslib_1, Ajax_1, View_1, Manager_1) {
10 "use strict";
11 Object.defineProperty(exports, "__esModule", { value: true });
12 exports.setup = void 0;
dda3c78c 13 View_1 = tslib_1.__importDefault(View_1);
b297c618 14 class UserMenuDataConversation {
93fd9c5c
AE
15 button;
16 counter = 0;
17 options;
18 stale = true;
19 view = undefined;
b297c618 20 constructor(button, options) {
b297c618
AE
21 this.button = button;
22 this.options = options;
23 const badge = button.querySelector(".badge");
24 if (badge) {
25 const counter = parseInt(badge.textContent.trim());
26 if (counter) {
27 this.counter = counter;
28 }
29 }
3a2e468d
C
30 this.button.addEventListener("updateCounter", (event) => {
31 this.updateCounter(event.detail.counter);
32 this.stale = true;
33 });
b297c618
AE
34 }
35 getPanelButton() {
36 return this.button;
37 }
38 getMenuButtons() {
39 const buttons = [];
40 if (this.options.canStartConversation) {
41 buttons.push({
3f3d496b 42 icon: '<fa-icon size="24" name="plus"></fa-icon>',
b297c618
AE
43 link: this.options.newConversationLink,
44 name: "newConversation",
45 title: this.options.newConversationTitle,
46 });
47 }
48 return buttons;
49 }
50 getIdentifier() {
51 return "com.woltlab.wcf.conversation.conversations";
52 }
53 async getData() {
ed9666e1
AE
54 const data = (await (0, Ajax_1.dboAction)("getConversations", "wcf\\data\\conversation\\ConversationAction")
55 .disableLoadingIndicator()
56 .dispatch());
05e725cc 57 this.updateCounter(data.totalCount);
b297c618 58 this.stale = false;
05e725cc 59 return data.items;
b297c618
AE
60 }
61 getFooter() {
62 return {
63 link: this.options.showAllLink,
64 title: this.options.showAllTitle,
65 };
66 }
67 getTitle() {
68 return this.options.title;
69 }
70 getView() {
71 if (this.view === undefined) {
72 this.view = new View_1.default(this);
73 }
74 return this.view;
75 }
76 getEmptyViewMessage() {
77 return this.options.noItems;
78 }
9e759b54
AE
79 hasPlainTitle() {
80 return true;
81 }
b297c618
AE
82 hasUnreadContent() {
83 return this.counter > 0;
84 }
85 isStale() {
86 if (this.stale) {
87 return true;
88 }
89 const unreadItems = this.getView()
90 .getItems()
91 .filter((item) => item.dataset.isUnread === "true");
92 if (this.counter !== unreadItems.length) {
93 return true;
94 }
95 return false;
96 }
97 async markAsRead(objectId) {
98 const response = (await (0, Ajax_1.dboAction)("markAsRead", "wcf\\data\\conversation\\ConversationAction")
99 .objectIds([objectId])
100 .dispatch());
101 this.updateCounter(response.totalCount);
102 }
103 async markAllAsRead() {
104 await (0, Ajax_1.dboAction)("markAllAsRead", "wcf\\data\\conversation\\ConversationAction").dispatch();
969524a1 105 this.updateCounter(0);
b297c618
AE
106 }
107 updateCounter(counter) {
108 let badge = this.button.querySelector(".badge");
109 if (badge === null && counter > 0) {
110 badge = document.createElement("span");
ca519063 111 badge.classList.add("badge", "badgeUpdate");
b297c618
AE
112 this.button.querySelector("a").append(badge);
113 }
114 if (badge) {
115 if (counter === 0) {
116 badge.remove();
117 }
118 else {
119 badge.textContent = counter.toString();
120 }
121 }
122 this.counter = counter;
123 }
124 }
125 let isInitialized = false;
126 function setup(options) {
127 if (!isInitialized) {
128 const button = document.getElementById("unreadConversations");
129 if (button !== null) {
130 const provider = new UserMenuDataConversation(button, options);
131 (0, Manager_1.registerProvider)(provider);
132 }
133 isInitialized = true;
134 }
135 }
136 exports.setup = setup;
137});