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