Add basic typescript support (#143)
[GitHub/WoltLab/com.woltlab.wcf.conversation.git] / files / js / WoltLabSuite / Core / Conversation / Ui / Participant / Add.js
1 /**
2 * Adds participants to an existing conversation.
3 *
4 * @author Alexander Ebert
5 * @copyright 2001-2019 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 define(['Ajax', 'Language', 'Ui/Dialog', 'Ui/Notification', 'WoltLabSuite/Core/Ui/ItemList/User'], function (Ajax, Language, UiDialog, UiNotification, UiItemListUser) {
10 "use strict";
11 /**
12 * @constructor
13 * @param {int} conversationId conversation id
14 */
15 function UiParticipantAdd(conversationId) { this.init(conversationId); }
16 UiParticipantAdd.prototype = {
17 /**
18 * Manages the form to add one or more participants to an existing conversation.
19 *
20 * @param {int} conversationId conversation id
21 */
22 init: function (conversationId) {
23 this._conversationId = conversationId;
24 Ajax.api(this, {
25 actionName: 'getAddParticipantsForm'
26 });
27 },
28 _ajaxSetup: function () {
29 return {
30 data: {
31 className: 'wcf\\data\\conversation\\ConversationAction',
32 objectIDs: [this._conversationId]
33 }
34 };
35 },
36 /**
37 * Handles successful Ajax requests.
38 *
39 * @param {Object} data response data
40 */
41 _ajaxSuccess: function (data) {
42 switch (data.actionName) {
43 case 'addParticipants':
44 this._handleResponse(data);
45 break;
46 case 'getAddParticipantsForm':
47 this._render(data);
48 break;
49 }
50 },
51 /**
52 * Shows the success message and closes the dialog overlay.
53 *
54 * @param {Object} data response data
55 */
56 _handleResponse: function (data) {
57 //noinspection JSUnresolvedVariable
58 if (data.returnValues.errorMessage) {
59 var innerError = elCreate('small');
60 innerError.className = 'innerError';
61 //noinspection JSUnresolvedVariable
62 innerError.textContent = data.returnValues.errorMessage;
63 var itemList = elById('participantsInput').closest('.inputItemList');
64 itemList.parentNode.insertBefore(innerError, itemList.nextSibling);
65 var oldError = innerError.nextElementSibling;
66 if (oldError && oldError.classList.contains('innerError')) {
67 elRemove(oldError);
68 }
69 return;
70 }
71 //noinspection JSUnresolvedVariable
72 if (data.returnValues.count) {
73 //noinspection JSUnresolvedVariable
74 UiNotification.show(data.returnValues.successMessage, window.location.reload.bind(window.location));
75 }
76 UiDialog.close(this);
77 },
78 /**
79 * Renders the dialog to add participants.
80 *
81 * @param {object} data response data
82 */
83 _render: function (data) {
84 //noinspection JSUnresolvedVariable
85 UiDialog.open(this, data.returnValues.template);
86 var buttonSubmit = elById('addParticipants');
87 buttonSubmit.disabled = true;
88 //noinspection JSUnresolvedVariable
89 UiItemListUser.init('participantsInput', {
90 callbackChange: function (elementId, values) { buttonSubmit.disabled = (values.length === 0); },
91 excludedSearchValues: data.returnValues.excludedSearchValues,
92 maxItems: data.returnValues.maxItems,
93 includeUserGroups: data.returnValues.canAddGroupParticipants && data.returnValues.restrictUserGroupIDs.length > 0,
94 restrictUserGroupIDs: data.returnValues.restrictUserGroupIDs,
95 csvPerType: true
96 });
97 buttonSubmit.addEventListener('click', this._submit.bind(this));
98 },
99 /**
100 * Sends a request to add participants.
101 */
102 _submit: function () {
103 var values = UiItemListUser.getValues('participantsInput'), participants = [], participantsGroupIDs = [];
104 for (var i = 0, length = values.length; i < length; i++) {
105 if (values[i].type === 'group')
106 participantsGroupIDs.push(values[i].objectId);
107 else
108 participants.push(values[i].value);
109 }
110 var parameters = {
111 participants: participants,
112 participantsGroupIDs: participantsGroupIDs
113 };
114 var visibility = elBySel('input[name="messageVisibility"]:checked, input[name="messageVisibility"][type="hidden"]', UiDialog.getDialog(this).content);
115 if (visibility)
116 parameters.visibility = visibility.value;
117 Ajax.api(this, {
118 actionName: 'addParticipants',
119 parameters: parameters
120 });
121 },
122 _dialogSetup: function () {
123 return {
124 id: 'conversationAddParticipants',
125 options: {
126 title: Language.get('wcf.conversation.edit.addParticipants')
127 },
128 source: null
129 };
130 }
131 };
132 return UiParticipantAdd;
133 });