Commit | Line | Data |
---|---|---|
6c576993 MS |
1 | /** |
2 | * Adds participants to an existing conversation. | |
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 | * @module WoltLabSuite/Core/Conversation/Ui/Participant/Add | |
8 | */ | |
9 | ||
10 | import * as Ajax from "WoltLabSuite/Core/Ajax"; | |
11 | import { AjaxCallbackObject } from "WoltLabSuite/Core/Ajax/Data"; | |
12 | import { AjaxCallbackSetup, ResponseData } from "WoltLabSuite/Core/Ajax/Data"; | |
13 | import DomUtil from "WoltLabSuite/Core/Dom/Util"; | |
14 | import UiDialog from "WoltLabSuite/Core/Ui/Dialog"; | |
15 | import * as UiNotification from "WoltLabSuite/Core/Ui/Notification"; | |
16 | import { DialogCallbackObject, DialogCallbackSetup } from "WoltLabSuite/Core/Ui/Dialog/Data"; | |
17 | import * as UiItemListUser from "WoltLabSuite/Core/Ui/ItemList/User"; | |
18 | import { ItemData } from "WoltLabSuite/Core/Ui/ItemList"; | |
19 | import * as Language from "WoltLabSuite/Core/Language"; | |
20 | ||
21 | interface AjaxResponseData extends ResponseData { | |
22 | actionName: "addParticipants" | "getAddParticipantsForm"; | |
23 | } | |
24 | ||
25 | interface AjaxAddParticipantsData extends AjaxResponseData { | |
26 | returnValues: { | |
27 | count?: number; | |
28 | errorMessage?: string; | |
29 | successMessage?: string; | |
30 | }; | |
31 | } | |
32 | ||
33 | interface AjaxGetAddParticipantsFormData extends AjaxResponseData { | |
34 | returnValues: { | |
35 | canAddGroupParticipants: boolean; | |
36 | csvPerType: boolean; | |
37 | excludedSearchValues: string[]; | |
38 | maxItems: number; | |
39 | restrictUserGroupIDs: number[]; | |
40 | template: string; | |
41 | }; | |
42 | } | |
43 | ||
44 | class UiParticipantAdd implements AjaxCallbackObject, DialogCallbackObject { | |
45 | protected readonly conversationId: number; | |
46 | ||
47 | constructor(conversationId: number) { | |
48 | this.conversationId = conversationId; | |
49 | ||
50 | Ajax.api(this, { | |
51 | actionName: "getAddParticipantsForm", | |
52 | }); | |
53 | } | |
54 | ||
55 | _ajaxSetup(): ReturnType<AjaxCallbackSetup> { | |
56 | return { | |
57 | data: { | |
58 | className: "wcf\\data\\conversation\\ConversationAction", | |
59 | objectIDs: [this.conversationId], | |
60 | }, | |
61 | }; | |
62 | } | |
63 | ||
64 | _ajaxSuccess(data: AjaxResponseData): void { | |
65 | switch (data.actionName) { | |
66 | case "addParticipants": | |
67 | this.handleResponse(data as AjaxAddParticipantsData); | |
68 | break; | |
69 | case "getAddParticipantsForm": | |
70 | this.render(data as AjaxGetAddParticipantsFormData); | |
71 | break; | |
72 | } | |
73 | } | |
74 | ||
75 | /** | |
76 | * Shows the success message and closes the dialog overlay. | |
77 | */ | |
78 | protected handleResponse(data: AjaxAddParticipantsData): void { | |
79 | if (data.returnValues.errorMessage) { | |
80 | DomUtil.innerError( | |
81 | document.getElementById("participantsInput")!.closest(".inputItemList") as HTMLElement, | |
13d4e7d7 | 82 | data.returnValues.errorMessage, |
6c576993 MS |
83 | ); |
84 | return; | |
85 | } | |
86 | ||
87 | if (data.returnValues.count) { | |
88 | UiNotification.show(data.returnValues.successMessage, () => window.location.reload()); | |
89 | } | |
90 | ||
91 | UiDialog.close(this); | |
92 | } | |
93 | ||
94 | /** | |
95 | * Renders the dialog to add participants. | |
96 | * @protected | |
97 | */ | |
98 | protected render(data: AjaxGetAddParticipantsFormData): void { | |
99 | UiDialog.open(this, data.returnValues.template); | |
100 | ||
101 | const buttonSubmit = document.getElementById("addParticipants") as HTMLButtonElement; | |
102 | buttonSubmit.disabled = true; | |
103 | ||
104 | UiItemListUser.init("participantsInput", { | |
105 | callbackChange: (elementId: string, values: ItemData[]): void => { | |
106 | buttonSubmit.disabled = values.length === 0; | |
107 | }, | |
108 | excludedSearchValues: data.returnValues.excludedSearchValues, | |
109 | maxItems: data.returnValues.maxItems, | |
110 | includeUserGroups: data.returnValues.canAddGroupParticipants && data.returnValues.restrictUserGroupIDs.length > 0, | |
111 | restrictUserGroupIDs: data.returnValues.restrictUserGroupIDs, | |
112 | csvPerType: true, | |
113 | }); | |
114 | buttonSubmit.addEventListener("click", () => this.submit()); | |
115 | } | |
116 | ||
117 | /** | |
118 | * Sends a request to add participants. | |
119 | */ | |
120 | protected submit(): void { | |
121 | const participants: string[] = []; | |
122 | const participantsGroupIDs: number[] = []; | |
123 | UiItemListUser.getValues("participantsInput").forEach((value) => { | |
124 | if (value.type === "group") { | |
125 | participantsGroupIDs.push(value.objectId); | |
126 | } else { | |
127 | participants.push(value.value); | |
128 | } | |
129 | }); | |
130 | ||
131 | const parameters = { | |
132 | participants: participants, | |
133 | participantsGroupIDs: participantsGroupIDs, | |
134 | visibility: null as null | string, | |
135 | }; | |
136 | const visibility = UiDialog.getDialog(this)!.content.querySelector( | |
13d4e7d7 | 137 | 'input[name="messageVisibility"]:checked, input[name="messageVisibility"][type="hidden"]', |
6c576993 MS |
138 | ) as HTMLInputElement; |
139 | ||
140 | if (visibility) { | |
141 | parameters.visibility = visibility.value; | |
142 | } | |
143 | ||
144 | Ajax.api(this, { | |
145 | actionName: "addParticipants", | |
146 | parameters: parameters, | |
147 | }); | |
148 | } | |
149 | ||
150 | _dialogSetup(): ReturnType<DialogCallbackSetup> { | |
151 | return { | |
152 | id: "conversationAddParticipants", | |
153 | options: { | |
154 | title: Language.get("wcf.conversation.edit.addParticipants"), | |
155 | }, | |
156 | source: null, | |
157 | }; | |
158 | } | |
159 | } | |
160 | ||
161 | export = UiParticipantAdd; |