Merge branch '5.5'
[GitHub/WoltLab/com.woltlab.wcf.conversation.git] / files / js / WoltLabSuite / Core / Conversation / Ui / User / Menu / Data / Conversation.js
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>
7 * @woltlabExcludeBundle tiny
8 */
9 define(["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;
13 View_1 = tslib_1.__importDefault(View_1);
14 class UserMenuDataConversation {
15 button;
16 counter = 0;
17 options;
18 stale = true;
19 view = undefined;
20 constructor(button, options) {
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 }
30 }
31 getPanelButton() {
32 return this.button;
33 }
34 getMenuButtons() {
35 const buttons = [];
36 if (this.options.canStartConversation) {
37 buttons.push({
38 icon: '<fa-icon size="24" name="plus"></fa-icon>',
39 link: this.options.newConversationLink,
40 name: "newConversation",
41 title: this.options.newConversationTitle,
42 });
43 }
44 return buttons;
45 }
46 getIdentifier() {
47 return "com.woltlab.wcf.conversation.conversations";
48 }
49 async getData() {
50 const data = (await (0, Ajax_1.dboAction)("getConversations", "wcf\\data\\conversation\\ConversationAction")
51 .disableLoadingIndicator()
52 .dispatch());
53 this.updateCounter(data.totalCount);
54 this.stale = false;
55 return data.items;
56 }
57 getFooter() {
58 return {
59 link: this.options.showAllLink,
60 title: this.options.showAllTitle,
61 };
62 }
63 getTitle() {
64 return this.options.title;
65 }
66 getView() {
67 if (this.view === undefined) {
68 this.view = new View_1.default(this);
69 }
70 return this.view;
71 }
72 getEmptyViewMessage() {
73 return this.options.noItems;
74 }
75 hasPlainTitle() {
76 return true;
77 }
78 hasUnreadContent() {
79 return this.counter > 0;
80 }
81 isStale() {
82 if (this.stale) {
83 return true;
84 }
85 const unreadItems = this.getView()
86 .getItems()
87 .filter((item) => item.dataset.isUnread === "true");
88 if (this.counter !== unreadItems.length) {
89 return true;
90 }
91 return false;
92 }
93 async markAsRead(objectId) {
94 const response = (await (0, Ajax_1.dboAction)("markAsRead", "wcf\\data\\conversation\\ConversationAction")
95 .objectIds([objectId])
96 .dispatch());
97 this.updateCounter(response.totalCount);
98 }
99 async markAllAsRead() {
100 await (0, Ajax_1.dboAction)("markAllAsRead", "wcf\\data\\conversation\\ConversationAction").dispatch();
101 this.updateCounter(0);
102 }
103 updateCounter(counter) {
104 let badge = this.button.querySelector(".badge");
105 if (badge === null && counter > 0) {
106 badge = document.createElement("span");
107 badge.classList.add("badge", "badgeUpdate");
108 this.button.querySelector("a").append(badge);
109 }
110 if (badge) {
111 if (counter === 0) {
112 badge.remove();
113 }
114 else {
115 badge.textContent = counter.toString();
116 }
117 }
118 this.counter = counter;
119 }
120 }
121 let isInitialized = false;
122 function setup(options) {
123 if (!isInitialized) {
124 const button = document.getElementById("unreadConversations");
125 if (button !== null) {
126 const provider = new UserMenuDataConversation(button, options);
127 (0, Manager_1.registerProvider)(provider);
128 }
129 isInitialized = true;
130 }
131 }
132 exports.setup = setup;
133 });