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