Add basic typescript support (#143)
[GitHub/WoltLab/com.woltlab.wcf.conversation.git] / ts / WoltLabSuite / Core / Conversation / Ui / Subject / Editor.js
1 /**
2 * Provides the editor for conversation subjects.
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/Subject/Editor
8 */
9 define(['Ajax', 'EventKey', 'Language', 'Ui/Dialog', 'Ui/Notification'], function (Ajax, EventKey, Language, UiDialog, UiNotification) {
10 "use strict";
11
12 var _objectId = 0;
13 var _subject = null;
14
15 /**
16 * @exports WoltLabSuite/Core/Conversation/Ui/Subject/Editor
17 */
18 return {
19 /**
20 * Shows the edit dialog for the selected conversation's subject.
21 *
22 * @param {int} objectId
23 */
24 beginEdit: function (objectId) {
25 _objectId = objectId;
26
27 UiDialog.open(this);
28 },
29
30 /**
31 * Validates and saves the new subject.
32 *
33 * @param {Event} event
34 * @protected
35 */
36 _saveEdit: function (event) {
37 event.preventDefault();
38
39 var innerError = _subject.nextElementSibling;
40 if (innerError && innerError.classList.contains('innerError')) {
41 elRemove(innerError);
42 }
43
44 var value = _subject.value.trim();
45 if (value === '') {
46 innerError = elCreate('small');
47 innerError.className = 'innerError';
48 innerError.textContent = Language.get('wcf.global.form.error.empty');
49 _subject.parentNode.insertBefore(innerError, _subject.nextElementSibling);
50 }
51 else {
52 Ajax.api(this, {
53 parameters: {
54 subject: value
55 },
56 objectIDs: [_objectId]
57 });
58 }
59 },
60
61 /**
62 * Retrieves the current conversation subject.
63 *
64 * @return {string}
65 * @protected
66 */
67 _getCurrentValue: function () {
68 var value = '';
69 elBySelAll('.jsConversationSubject[data-conversation-id="' + _objectId + '"], .conversationLink[data-object-id="' + _objectId + '"]', undefined, function (subject) {
70 value = subject.textContent;
71 });
72
73 return value;
74 },
75
76 _ajaxSuccess: function (data) {
77 UiDialog.close(this);
78
79 elBySelAll('.jsConversationSubject[data-conversation-id="' + _objectId + '"], .conversationLink[data-object-id="' + _objectId + '"]', undefined, function (subject) {
80 subject.textContent = data.returnValues.subject;
81 });
82
83 UiNotification.show();
84 },
85
86 _dialogSetup: function () {
87 return {
88 id: 'dialogConversationSubjectEditor',
89 options: {
90 onSetup: (function (content) {
91 _subject = elById('jsConversationSubject');
92 _subject.addEventListener('keyup', (function (event) {
93 if (EventKey.Enter(event)) {
94 this._saveEdit(event);
95 }
96 }).bind(this));
97
98 elBySel('.jsButtonSave', content).addEventListener('click', this._saveEdit.bind(this));
99 }).bind(this),
100 onShow: (function () {
101 _subject.value = this._getCurrentValue();
102 }).bind(this),
103 title: Language.get('wcf.conversation.edit.subject')
104 },
105 source: '<dl>'
106 + '<dt><label for="jsConversationSubject">' + Language.get('wcf.global.subject') + '</label></dt>'
107 + '<dd><input type="text" id="jsConversationSubject" class="long" maxlength="255"></dd>'
108 + '</dl>'
109 + '<div class="formSubmit"><button class="buttonPrimary jsButtonSave">' + Language.get('wcf.global.button.save') + '</button></div>'
110 };
111 },
112
113 _ajaxSetup: function () {
114 return {
115 data: {
116 actionName: 'editSubject',
117 className: 'wcf\\data\\conversation\\ConversationAction'
118 }
119 }
120 }
121 };
122 });