-
Notifications
You must be signed in to change notification settings - Fork 105
/
CreateHighlightDialog.js
338 lines (294 loc) · 9.97 KB
/
CreateHighlightDialog.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import EventEmitter from 'events';
import { ICON_HIGHLIGHT, ICON_HIGHLIGHT_COMMENT } from '../../icons/icons';
import CommentBox from '../CommentBox';
import { hideElement, showElement } from '../annotatorUtil';
const CLASS_CREATE_DIALOG = 'bp-create-annotation-dialog';
const TITLE_HIGHLIGHT_TOGGLE = __('annotation_highlight_toggle');
const TITLE_HIGHLIGHT_COMMENT = __('annotation_highlight_comment');
const CREATE_HIGHLIGHT_DIALOG_TEMPLATE = `
<div class="bp-annotation-caret" style="left: 50%;"></div>
<div>
<div class="bp-annotation-highlight-dialog">
<span class="bp-annotations-highlight-btns">
<button class="bp-btn-plain bp-add-highlight-btn"
data-type="highlight-btn"
title="${TITLE_HIGHLIGHT_TOGGLE}">
${ICON_HIGHLIGHT}
</button>
<button class="bp-btn-plain bp-highlight-comment-btn"
data-type="add-highlight-comment-btn"
title="${TITLE_HIGHLIGHT_COMMENT}">
${ICON_HIGHLIGHT_COMMENT}
</button>
</span>
</div>
</div>`.trim();
/**
* Events emitted by this component.
*/
export const CreateEvents = {
plain: 'plain_highlight_create',
comment: 'comment_highlight_edit',
commentPost: 'comment_highlight_post'
};
class CreateHighlightDialog extends EventEmitter {
/**
* Container element for the dialog.
*
* @property {HTMLElement}
*/
containerEl;
/**
* The clickable element for creating plain highlights.
*
* @property {HTMLElement}
*/
highlightCreateEl;
/**
* The clickable element got creating comment highlights.
*
* @property {HTMLElement}
*/
commentCreateEl;
/**
* The parent container to nest the dialog element in.
*
* @property {HTMLElement}
*/
parentEl;
/**
* The element containing the buttons that can creaet highlights.
*
* @property {HTMLElement}
*/
buttonsEl;
/**
* The comment box instance. Contains area for text input and post/cancel buttons.
*
* @property {CommentBox}
*/
commentBox;
/**
* Position, on the DOM, to align the dialog to the end of a highlight.
*
* @property {Object}
*/
position = {
x: 0,
y: 0
};
/**
* A dialog used to create plain and comment highlights.
*
* [constructor]
*
* @param {HTMLElement} parentEl - Parent element
* @return {CreateHighlightDialog} CreateHighlightDialog instance
*/
constructor(parentEl) {
super();
this.parentEl = parentEl;
// Explicit scope binding for event listeners
this.onHighlightClick = this.onHighlightClick.bind(this);
this.onCommentClick = this.onCommentClick.bind(this);
this.onCommentPost = this.onCommentPost.bind(this);
this.onCommentCancel = this.onCommentCancel.bind(this);
}
/**
* Set the parent container to next this dialog in.
*
* @public
* @param {HTMLElement} newParentEl - The element that will contain this.
* @return {void}
*/
setParentEl(newParentEl) {
this.parentEl = newParentEl;
}
/**
* Set the coordinates to position the dialog at, and force an update.
*
* @public
* @param {number} x - The x coordinate to position the dialog at
* @param {number} y - The y coordinate to position the dialog at
* @return {void}
*/
setPosition(x, y) {
this.position.x = x;
this.position.y = y;
this.updatePosition();
}
/**
* Show the dialog. Adds to the parent container if it isn't already there.
*
* @public
* @param {HTMLElement} [newParentEl] - The new parent container to nest this in.
* @return {void}
*/
show(newParentEl) {
if (!this.containerEl) {
this.containerEl = this.createElement();
}
// Move to the correct parent element
if (newParentEl) {
this.setParentEl(newParentEl);
}
// Add to parent if it hasn't been added already
if (!this.parentEl.querySelector(`.${CLASS_CREATE_DIALOG}`)) {
this.parentEl.appendChild(this.containerEl);
}
this.setButtonVisibility(true);
showElement(this.containerEl);
}
/**
* Hide the dialog, and clear out the comment box text entry.
*
* @return {void}
*/
hide() {
if (!this.containerEl) {
return;
}
hideElement(this.containerEl);
this.commentBox.hide();
this.commentBox.clear();
}
/**
* [destructor]
*
* @return {void}
*/
destroy() {
if (!this.containerEl) {
return;
}
this.hide();
// Stop interacting with this element from triggering outside actions
this.containerEl.removeEventListener('click', this.stopPropagation);
this.containerEl.removeEventListener('mouseup', this.stopPropagation);
this.containerEl.removeEventListener('touchend', this.stopPropagation);
this.containerEl.removeEventListener('dblclick', this.stopPropagation);
// Event listeners
this.highlightCreateEl.removeEventListener('click', this.onHighlightClick);
this.commentCreateEl.removeEventListener('click', this.onCommentClick);
this.commentBox.removeListener(CommentBox.CommentEvents.post, this.onCommentPost);
this.commentBox.removeListener(CommentBox.CommentEvents.cancel, this.onCommentCancel);
this.containerEl.remove();
this.containerEl = null;
this.parentEl = null;
this.commentBox.destroy();
this.commentBox = null;
}
//--------------------------------------------------------------------------
// Private
//--------------------------------------------------------------------------
/**
* Update the position styling for the dialog so that the chevron points to
* the desired location.
*
* @return {void}
*/
updatePosition() {
// Plus 1 pixel for caret
this.containerEl.style.left = `${this.position.x - 1 - this.containerEl.clientWidth / 2}px`;
// Plus 5 pixels for caret
this.containerEl.style.top = `${this.position.y + 5}px`;
}
/**
* Fire an event notifying that the plain highlight button has been clicked.
*
* @return {void}
*/
onHighlightClick() {
this.emit(CreateEvents.plain);
}
/**
* Fire an event notifying that the comment button has been clicked. Also
* show the comment box, and give focus to the text area conatined by it.
*
* @return {void}
*/
onCommentClick() {
this.emit(CreateEvents.comment);
this.commentBox.show();
this.commentBox.focus();
this.setButtonVisibility(false);
this.updatePosition();
}
/**
* Fire an event notifying that the post button has been pressed. Clears
* out the comment box.
*
* @param {string} text - Text entered into the comment box
* @return {void}
*/
onCommentPost(text) {
this.emit(CreateEvents.commentPost, text);
this.commentBox.clear();
}
/**
* The cancel button has been pressed. Close the comment box, and return to
* default state.
*
* @return {void}
*/
onCommentCancel() {
this.commentBox.hide();
this.setButtonVisibility(true);
this.updatePosition();
}
/**
* Hide or show the plain and comment buttons, in the dialog.
*
* @param {boolean} visible - If true, shows the plain and comment buttons
* @return {void}
*/
setButtonVisibility(visible) {
if (visible) {
showElement(this.buttonsEl);
} else {
hideElement(this.buttonsEl);
}
}
/**
* Stop the dialog from propagating events to parent container. Pairs with
* giving focus to the text area in the comment box and clicking "Post".
*
* @param {Event} event - The DOM event coming from interacting with the element.
* @return {void}
*/
stopPropagation(event) {
event.stopPropagation();
}
/**
* Create the element containing highlight create and comment buttons, and comment box.
*
* @private
* @return {HTMLElement} The element containing Highlight creation UI
*/
createElement() {
const highlightDialogEl = document.createElement('div');
highlightDialogEl.classList.add(CLASS_CREATE_DIALOG);
highlightDialogEl.innerHTML = CREATE_HIGHLIGHT_DIALOG_TEMPLATE;
const containerEl = highlightDialogEl.querySelector('.bp-annotation-highlight-dialog');
// Reference HTML
this.highlightCreateEl = containerEl.querySelector('.bp-add-highlight-btn');
this.commentCreateEl = containerEl.querySelector('.bp-highlight-comment-btn');
this.buttonsEl = containerEl.querySelector('.bp-annotations-highlight-btns');
// Create comment box
this.commentBox = new CommentBox(containerEl);
// Stop interacting with this element from triggering outside actions
highlightDialogEl.addEventListener('click', this.stopPropagation);
highlightDialogEl.addEventListener('mouseup', this.stopPropagation);
highlightDialogEl.addEventListener('touchend', this.stopPropagation);
highlightDialogEl.addEventListener('dblclick', this.stopPropagation);
// Event listeners
this.highlightCreateEl.addEventListener('click', this.onHighlightClick);
this.commentCreateEl.addEventListener('click', this.onCommentClick);
this.commentBox.addListener(CommentBox.CommentEvents.post, this.onCommentPost);
this.commentBox.addListener(CommentBox.CommentEvents.cancel, this.onCommentCancel);
// Hide comment box, by default
this.commentBox.hide();
return highlightDialogEl;
}
}
export default CreateHighlightDialog;