-
Notifications
You must be signed in to change notification settings - Fork 105
/
CommentBox.js
239 lines (205 loc) · 6.02 KB
/
CommentBox.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import EventEmitter from 'events';
import { CLASS_ACTIVE } from '../constants';
import { hideElement, showElement } from './annotatorUtil';
// Display Text
const TEXT_ANNOTATION_CANCEL = __('annotation_cancel');
const TEXT_ANNOTATION_POST = __('annotation_post');
const TEXT_ADD_COMMENT_PLACEHOLDER = __('annotation_add_comment_placeholder');
class CommentBox extends EventEmitter {
/**
* Text displayed in the Cancel button element.
*
* @property {string}
*/
cancelText = TEXT_ANNOTATION_CANCEL;
/**
* Text displayed in the Post button element.
*
* @property {string}
*/
postText = TEXT_ANNOTATION_POST;
/**
* Placeholder text displayed in the text area element.
*
* @property {string}
*/
placeholderText = TEXT_ADD_COMMENT_PLACEHOLDER;
/**
* Reference to the comment box element. Contains buttons and text area.
*
* @property {HTMLElement}
*/
containerEl;
/**
* Reference to the cancel button element in the comment box.
*
* @property {HTMLElement}
*/
cancelEl;
/**
* Reference to the post button element in the comment box.
*
* @property {HTMLElement}
*/
postEl;
/**
* Reference to the text area element in the comment box.
*
* @property {HTMLElement}
*/
textAreaEl;
/**
* Reference to parent element that the comment box should be nested inside.
*
* @property {HTMLElement}
*/
parentEl;
/* Events that the comment box can emit. */
static CommentEvents = {
cancel: 'comment_cancel',
post: 'comment_post'
};
/**
* Creates an element for text entry, submission and cancellation.
*
* @param {HTMLElement} parentEl - Parent element
* @param {Object} [config] - Object containing text values to be displayed to the user.
* @param {string} config.cancel - Text displayed in the "Cancel" button
* @param {string} config.post - Text displayed in the "Post" button
* @param {string} config.placeholder - Placeholder text displayed in the text area
*/
constructor(parentEl, config = {}) {
super();
this.parentEl = parentEl;
this.cancelText = config.cancel || this.cancelText;
this.postText = config.post || this.postText;
this.placeholderText = config.placeholder || this.placeholderText;
// Explicit scope binding for event listeners
this.onCancel = this.onCancel.bind(this);
this.onPost = this.onPost.bind(this);
}
/**
* Focus on the text box.
*
* @return {void}
*/
focus() {
if (!this.containerEl) {
return;
}
this.textAreaEl.focus();
}
/**
* Clear out the text box.
*
* @return {void}
*/
clear() {
if (!this.containerEl) {
return;
}
this.textAreaEl.value = '';
}
/**
* Hide the element.
*
* @return {void}
*/
hide() {
if (!this.containerEl) {
return;
}
hideElement(this.containerEl);
}
/**
* Show the element.
*
* @return {void}
*/
show() {
if (!this.containerEl) {
this.containerEl = this.createCommentBox();
this.parentEl.appendChild(this.containerEl);
}
showElement(this.containerEl);
}
/**
* [destructor]
*
* @return {void}
*/
destroy() {
if (!this.containerEl) {
return;
}
this.containerEl.remove();
this.parentEl = null;
this.containerEl = null;
this.cancelEl.removeEventListener('click', this.onCancel);
this.postEl.removeEventListener('click', this.onPost);
}
//--------------------------------------------------------------------------
// Private
//--------------------------------------------------------------------------
/**
* Create HTML containing the UI for the comment box.
*
* @private
* @return {HTMLElement} HTML containing UI for the comment box.
*/
createHTML() {
const containerEl = document.createElement('section');
containerEl.classList.add('bp-create-highlight-comment');
containerEl.innerHTML = `
<textarea class="bp-textarea annotation-textarea ${CLASS_ACTIVE}"
placeholder="${this.placeholderText}"></textarea>
<div class="button-container">
<button class="bp-btn cancel-annotation-btn">
${this.cancelText}
</button>
<button class="bp-btn bp-btn-primary post-annotation-btn">
${this.postText}
</button>
</div>`.trim();
return containerEl;
}
/**
* Clear the current text in the textarea element and notify listeners.
*
* @private
* @return {void}
*/
onCancel() {
this.clear();
this.emit(CommentBox.CommentEvents.cancel);
}
/**
* Notify listeners of submit event and then clear textarea element.
*
* @private
* @return {void}
*/
onPost() {
this.emit(CommentBox.CommentEvents.post, this.textAreaEl.value);
this.clear();
}
/**
* Create HTML for the comment box. Assigns references to elements, attach event listeners.
* ie) Post button, cancel button
*
* @private
* @return {HTMLElement} The HTML to append to this.parentElement
*/
createCommentBox() {
const containerEl = this.createHTML();
// Reference HTML
this.textAreaEl = containerEl.querySelector('.annotation-textarea');
this.cancelEl = containerEl.querySelector('.cancel-annotation-btn');
this.postEl = containerEl.querySelector('.post-annotation-btn');
// Add event listeners
this.cancelEl.addEventListener('click', this.onCancel);
this.postEl.addEventListener('click', this.onPost);
return containerEl;
}
}
export default CommentBox;