-
Notifications
You must be signed in to change notification settings - Fork 105
/
AnnotationDialog.js
670 lines (589 loc) · 22.4 KB
/
AnnotationDialog.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
import autobind from 'autobind-decorator';
import EventEmitter from 'events';
import * as annotatorUtil from './annotatorUtil';
import * as constants from './annotationConstants';
import { CLASS_ACTIVE, CLASS_HIDDEN } from '../constants';
import { decodeKeydown } from '../util';
import { ICON_CLOSE, ICON_DELETE } from '../icons/icons';
const CLASS_ANIMATE_DIALOG = 'bp-animate-show-dialog';
@autobind class AnnotationDialog extends EventEmitter {
//--------------------------------------------------------------------------
// Typedef
//--------------------------------------------------------------------------
/**
* The data object for constructing a dialog.
* @typedef {Object} AnnotationDialogData
* @property {HTMLElement} annotatedElement HTML element being annotated on
* @property {Annotation[]} annotations Annotations in dialog, can be an
* empty array for a new thread
* @property {Object} location Location object
* @property {boolean} canAnnotate Whether or not user can annotate
*/
//--------------------------------------------------------------------------
// Public
//--------------------------------------------------------------------------
/**
* [constructor]
*
* @param {AnnotationDialogData} data - Data for constructing thread
* @return {AnnotationDialog} Annotation dialog instance
*/
constructor(data) {
super();
this.annotatedElement = data.annotatedElement;
this.location = data.location;
this.hasAnnotations = data.annotations.length > 0;
this.canAnnotate = data.canAnnotate;
this.locale = data.locale;
}
/**
* [destructor]
*
* @return {void}
*/
destroy() {
if (this.element) {
this.unbindDOMListeners();
if (this.element.parentNode) {
this.element.parentNode.removeChild(this.element);
}
this.element = null;
}
}
/**
* Positions and shows the dialog.
*
* @return {void}
*/
show() {
// Populate mobile annotations dialog with annotations information
if (this.isMobile) {
this.element = document.querySelector(`.${constants.CLASS_MOBILE_ANNOTATION_DIALOG}`);
annotatorUtil.showElement(this.element);
this.element.appendChild(this.dialogEl);
if (this.highlightDialogEl && !this.hasComments) {
this.element.classList.add('bp-plain-highlight');
const headerEl = this.element.querySelector('.bp-annotation-mobile-header');
headerEl.classList.add(CLASS_HIDDEN);
}
const dialogCloseButtonEl = this.element.querySelector('.bp-annotation-dialog-close');
dialogCloseButtonEl.addEventListener('click', this.hideMobileDialog);
this.element.classList.add(CLASS_ANIMATE_DIALOG);
this.bindDOMListeners();
}
const textAreaEl = this.hasAnnotations
? this.element.querySelector(constants.SELECTOR_REPLY_TEXTAREA)
: this.element.querySelector(constants.SELECTOR_ANNOTATION_TEXTAREA);
// Don't re-position if reply textarea is already active
const textareaIsActive = textAreaEl.classList.contains(CLASS_ACTIVE);
if (textareaIsActive && this.element.parentNode) {
return;
}
// Position and show - we need to reposition every time since the DOM
// could have changed from zooming
if (!this.isMobile) {
this.position();
}
// Activate appropriate textarea
if (this.hasAnnotations) {
this.activateReply();
} else {
textAreaEl.classList.add(CLASS_ACTIVE);
}
// Move cursor to end of text area
if (textAreaEl.selectionStart) {
textAreaEl.selectionEnd = textAreaEl.value.length;
textAreaEl.selectionStart = textAreaEl.selectionEnd;
}
// If user cannot annotate, hide reply/edit/delete UI
if (!this.canAnnotate) {
this.element.classList.add(constants.CLASS_CANNOT_ANNOTATE);
}
// Focus the textarea if visible
if (annotatorUtil.isElementInViewport(textAreaEl)) {
textAreaEl.focus();
}
}
/**
* Hides and resets the shared mobile dialog.
*
* @return {void}
*/
hideMobileDialog() {
if (!this.element) {
return;
}
this.element.classList.remove(CLASS_ANIMATE_DIALOG);
// Clear annotations from dialog
this.element.innerHTML = `
<div class="bp-annotation-mobile-header">
<button class="bp-annotation-dialog-close">${ICON_CLOSE}</button>
</div>`.trim();
this.element.classList.remove('bp-plain-highlight');
const dialogCloseButtonEl = this.element.querySelector('.bp-annotation-dialog-close');
dialogCloseButtonEl.removeEventListener('click', this.hideMobileDialog);
annotatorUtil.hideElement(this.element);
this.unbindDOMListeners();
// Cancel any unsaved annotations
if (!this.hasAnnotations) {
this.cancelAnnotation();
}
}
/**
* Hides the dialog.
*
* @param {boolean} [noDelay] - Whether or not to have a timeout delay
* @return {void}
*/
hide() {
if (this.isMobile) {
this.hideMobileDialog();
}
annotatorUtil.hideElement(this.element);
this.deactivateReply();
}
/**
* Adds an annotation to the dialog.
*
* @param {Annotation} annotation - Annotation to add
* @return {void}
*/
addAnnotation(annotation) {
// Show new section if needed
if (!this.hasAnnotations) {
const createSectionEl = this.element.querySelector('[data-section="create"]');
const showSectionEl = this.element.querySelector('[data-section="show"]');
annotatorUtil.hideElement(createSectionEl);
annotatorUtil.showElement(showSectionEl);
this.hasAnnotations = true;
}
this.addAnnotationElement(annotation);
this.deactivateReply(true); // Deactivate reply area and focus
}
/**
* Removes an annotation from the dialog.
*
* @param {string} annotationID - ID of annotation to remove
* @return {void}
*/
removeAnnotation(annotationID) {
const annotationEl = this.element.querySelector(`[data-annotation-id="${annotationID}"]`);
if (annotationEl) {
annotationEl.parentNode.removeChild(annotationEl);
this.deactivateReply(); // Deactivate reply area and focus
}
}
/**
* Posts an annotation in the dialog.
*
* @param {string} [textInput] - Annotation text to post
* @return {void}
*/
postAnnotation(textInput) {
const annotationTextEl = this.element.querySelector(constants.SELECTOR_ANNOTATION_TEXTAREA);
const text = textInput || annotationTextEl.value;
if (text.trim() === '') {
return;
}
this.emit('annotationcreate', { text });
annotationTextEl.value = '';
}
//--------------------------------------------------------------------------
// Abstract
//--------------------------------------------------------------------------
/**
* Must be implemented to position the dialog on the preview.
*
* @return {void}
*/
position() {}
//--------------------------------------------------------------------------
// Protected
//--------------------------------------------------------------------------
/**
* Sets up the dialog element.
*
* @param {Annotation[]} annotations - to show in the dialog
* @return {void}
* @protected
*/
setup(annotations) {
// Generate HTML of dialog
this.dialogEl = this.generateDialogEl(annotations.length);
this.dialogEl.classList.add('annotation-container');
if (!this.isMobile) {
this.element = document.createElement('div');
this.element.setAttribute('data-type', 'annotation-dialog');
this.element.classList.add(constants.CLASS_ANNOTATION_DIALOG);
this.element.innerHTML = '<div class="bp-annotation-caret"></div>';
this.element.appendChild(this.dialogEl);
// Adding thread number to dialog
if (annotations.length > 0) {
this.element.dataset.threadNumber = annotations[0].thread;
}
this.bindDOMListeners();
}
// Add annotation elements
annotations.forEach((annotation) => {
this.addAnnotationElement(annotation);
});
}
/**
* Binds DOM event listeners.
*
* @protected
* @return {void}
*/
bindDOMListeners() {
this.element.addEventListener('keydown', this.keydownHandler);
this.element.addEventListener('click', this.clickHandler);
this.element.addEventListener('mouseup', this.stopPropagation);
this.element.addEventListener('wheel', this.stopPropagation);
if (!this.isMobile) {
this.element.addEventListener('mouseenter', this.mouseenterHandler);
this.element.addEventListener('mouseleave', this.mouseleaveHandler);
}
}
/**
* Unbinds DOM event listeners.
*
* @protected
* @return {void}
*/
unbindDOMListeners() {
this.element.removeEventListener('keydown', this.keydownHandler);
this.element.removeEventListener('click', this.clickHandler);
this.element.removeEventListener('mouseup', this.stopPropagation);
this.element.removeEventListener('wheel', this.stopPropagation);
if (!this.isMobile) {
this.element.removeEventListener('mouseenter', this.mouseenterHandler);
this.element.removeEventListener('mouseleave', this.mouseleaveHandler);
}
}
/**
* Keydown handler for dialog.
*
* @protected
* @param {Event} event - DOM event
* @return {void}
*/
keydownHandler(event) {
event.stopPropagation();
const key = decodeKeydown(event);
if (key === 'Escape') {
this.hide(); // hide without delay
} else {
const dataType = annotatorUtil.findClosestDataType(event.target);
if (dataType === 'reply-textarea') {
this.activateReply();
}
}
}
/**
* Stops propagation of DOM event.
*
* @protected
* @param {Event} event - DOM event
* @return {void}
*/
stopPropagation(event) {
event.stopPropagation();
}
/**
* Mouseenter handler. Clears hide timeout.
*
* @protected
* @return {void}
*/
mouseenterHandler() {
if (this.element.classList.contains(CLASS_HIDDEN)) {
annotatorUtil.showElement(this.element);
const replyTextArea = this.element.querySelector(constants.SELECTOR_REPLY_TEXTAREA);
const commentsTextArea = this.element.querySelector(constants.SELECTOR_ANNOTATION_TEXTAREA);
if (replyTextArea.textContent !== '' || commentsTextArea.textContent !== '') {
this.emit('annotationcommentpending');
}
// Ensure textarea stays open
this.activateReply();
}
}
/**
* Mouseleave handler. Hides dialog if we aren't creating the first one.
*
* @protected
* @return {void}
*/
mouseleaveHandler() {
if (this.hasAnnotations) {
this.hide();
}
}
/**
* Click handler on dialog.
*
* @protected
* @param {Event} event - DOM event
* @return {void}
*/
clickHandler(event) {
event.stopPropagation();
const eventTarget = event.target;
const dataType = annotatorUtil.findClosestDataType(eventTarget);
const annotationID = annotatorUtil.findClosestDataType(eventTarget, 'data-annotation-id');
switch (dataType) {
// Clicking 'Post' button to create an annotation
case 'post-annotation-btn':
this.postAnnotation();
break;
// Clicking 'Cancel' button to cancel the annotation
case 'cancel-annotation-btn':
if (this.isMobile) {
this.hideMobileDialog();
} else {
this.cancelAnnotation();
}
this.deactivateReply(true);
break;
// Clicking inside reply text area
case 'reply-textarea':
this.activateReply();
break;
// Canceling a reply
case 'cancel-reply-btn':
this.deactivateReply(true);
break;
// Clicking 'Post' button to create a reply annotation
case 'post-reply-btn':
this.postReply();
break;
// Clicking trash icon to initiate deletion
case 'delete-btn':
this.showDeleteConfirmation(annotationID);
break;
// Clicking 'Cancel' button to cancel deletion
case 'cancel-delete-btn':
this.hideDeleteConfirmation(annotationID);
break;
// Clicking 'Delete' button to confirm deletion
case 'confirm-delete-btn': {
this.deleteAnnotation(annotationID);
break;
}
default:
break;
}
}
//--------------------------------------------------------------------------
// Private
//--------------------------------------------------------------------------
/**
* Adds an annotation to the dialog.
*
* @private
* @param {Annotation} annotation - Annotation to add
* @return {void}
*/
addAnnotationElement(annotation) {
const userId = annotatorUtil.htmlEscape(annotation.user.id || '0');
// Temporary until annotation user API is available
let userName;
if (userId === '0') {
userName = __('annotation_posting_message');
} else {
userName = annotatorUtil.htmlEscape(annotation.user.name) || __('annotation_anonymous_user_name');
}
const avatarUrl = annotatorUtil.htmlEscape(annotation.user.avatarUrl || '');
const avatarHtml = annotatorUtil.getAvatarHtml(avatarUrl, userId, userName);
const created = new Date(annotation.created).toLocaleString(this.locale, {
month: '2-digit',
day: '2-digit',
year: 'numeric',
hour: '2-digit',
minute: '2-digit'
});
const text = annotatorUtil.htmlEscape(annotation.text);
const annotationEl = document.createElement('div');
annotationEl.classList.add('annotation-comment');
annotationEl.setAttribute('data-annotation-id', annotation.annotationID);
annotationEl.innerHTML = `
<div class="profile-image-container">${avatarHtml}</div>
<div class="profile-container">
<div class="user-name">${userName}</div>
<div class="comment-date">${created}</div>
</div>
<div class="comment-text">${text}</div>
<button class="bp-btn-plain delete-comment-btn ${annotation.permissions.can_delete ? '' : 'bp-is-hidden'}" data-type="delete-btn" title="${__('annotation_delete')}">
${ICON_DELETE}
</button>
<div class="delete-confirmation ${CLASS_HIDDEN}">
<div class="delete-confirmation-message">
${__('annotation_delete_confirmation_message')}
</div>
<div class="button-container">
<button class="bp-btn cancel-delete-btn" data-type="cancel-delete-btn">
${__('annotation_cancel')}
</button>
<button class="bp-btn bp-btn-primary confirm-delete-btn" data-type="confirm-delete-btn">
${__('annotation_delete')}
</button>
</div>
</div>`.trim();
const annotationContainerEl = this.dialogEl.querySelector(constants.SELECTOR_COMMENTS_CONTAINER);
annotationContainerEl.appendChild(annotationEl);
}
/**
* Cancels posting an annotation.
*
* @private
* @return {void}
*/
cancelAnnotation() {
this.emit('annotationcancel');
}
/**
* Activates reply textarea.
*
* @private
* @return {void}
*/
activateReply() {
if (!this.dialogEl) {
return;
}
const replyTextEl = this.dialogEl.querySelector(constants.SELECTOR_REPLY_TEXTAREA);
// Don't activate if reply textarea is already active
const isActive = replyTextEl.classList.contains(CLASS_ACTIVE);
if (isActive) {
return;
}
const replyButtonEls = replyTextEl.parentNode.querySelector(constants.SELECTOR_BUTTON_CONTAINER);
replyTextEl.classList.add(CLASS_ACTIVE);
annotatorUtil.showElement(replyButtonEls);
// Auto scroll annotations dialog to bottom where new comment was added
const annotationsEl = this.dialogEl.querySelector('.annotation-container');
if (annotationsEl) {
annotationsEl.scrollTop = annotationsEl.scrollHeight - annotationsEl.clientHeight;
}
}
/**
* Deactivate reply textarea.
*
* @private
* @param {boolean} clearText - Whether or not text in text area should be cleared
* @return {void}
*/
deactivateReply(clearText) {
if (!this.dialogEl) {
return;
}
const replyContainerEl = this.dialogEl.querySelector(constants.SELECTOR_REPLY_CONTAINER);
const replyTextEl = replyContainerEl.querySelector(constants.SELECTOR_REPLY_TEXTAREA);
const replyButtonEls = replyContainerEl.querySelector(constants.SELECTOR_BUTTON_CONTAINER);
annotatorUtil.resetTextarea(replyTextEl, clearText);
annotatorUtil.hideElement(replyButtonEls);
if (annotatorUtil.isElementInViewport(replyTextEl)) {
replyTextEl.focus();
}
// Auto scroll annotations dialog to bottom where new comment was added
const annotationsEl = this.dialogEl.querySelector('.annotation-container');
if (annotationsEl) {
annotationsEl.scrollTop = annotationsEl.scrollHeight - annotationsEl.clientHeight;
}
}
/**
* Posts a reply in the dialog.
*
* @private
* @return {void}
*/
postReply() {
const replyTextEl = this.element.querySelector(constants.SELECTOR_REPLY_TEXTAREA);
const text = replyTextEl.value;
if (text.trim() === '') {
return;
}
this.emit('annotationcreate', { text });
replyTextEl.value = '';
}
/**
* Shows delete confirmation.
*
* @private
* @param {string} annotationID - ID of annotation to delete
* @return {void}
*/
showDeleteConfirmation(annotationID) {
const annotationEl = this.element.querySelector(`[data-annotation-id="${annotationID}"]`);
const deleteConfirmationEl = annotationEl.querySelector('.delete-confirmation');
const cancelDeleteButtonEl = annotationEl.querySelector('.cancel-delete-btn');
const deleteButtonEl = annotationEl.querySelector('.delete-comment-btn');
annotatorUtil.hideElement(deleteButtonEl);
annotatorUtil.showElement(deleteConfirmationEl);
cancelDeleteButtonEl.focus();
}
/**
* Hides delete confirmation.
*
* @private
* @param {string} annotationID - ID of annotation to delete
* @return {void}
*/
hideDeleteConfirmation(annotationID) {
const annotationEl = this.element.querySelector(`[data-annotation-id="${annotationID}"]`);
const deleteConfirmationEl = annotationEl.querySelector('.delete-confirmation');
const deleteButtonEl = annotationEl.querySelector('.delete-comment-btn');
annotatorUtil.showElement(deleteButtonEl);
annotatorUtil.hideElement(deleteConfirmationEl);
deleteButtonEl.focus();
}
/**
* Broadcasts message to delete an annotation.
*
* @private
* @param {string} annotationID - ID of annotation to delete
* @return {void}
*/
deleteAnnotation(annotationID) {
this.emit('annotationdelete', { annotationID });
}
/**
* Generates the annotation dialog DOM element
*
* @private
* @param {number} numAnnotations - length of annotations array
* @return {HTMLElement} Annotation dialog DOM element
*/
generateDialogEl(numAnnotations) {
const dialogEl = document.createElement('div');
dialogEl.innerHTML = `
<section class="${numAnnotations ? CLASS_HIDDEN : ''}" data-section="create">
<textarea class="bp-textarea annotation-textarea"
placeholder="${__('annotation_add_comment_placeholder')}"></textarea>
<div class="button-container">
<button class="bp-btn cancel-annotation-btn" data-type="cancel-annotation-btn">
${__('annotation_cancel')}
</button>
<button class="bp-btn bp-btn-primary post-annotation-btn" data-type="post-annotation-btn">
${__('annotation_post')}
</button>
</div>
</section>
<section class="${numAnnotations ? '' : CLASS_HIDDEN}" data-section="show">
<div class="annotation-comments"></div>
<div class="reply-container">
<textarea class="bp-textarea reply-textarea"
placeholder="${__('annotation_reply_placeholder')}" data-type="reply-textarea"></textarea>
<div class="button-container ${CLASS_HIDDEN}">
<button class="bp-btn cancel-annotation-btn" data-type="cancel-reply-btn">
${__('annotation_cancel')}
</button>
<button class="bp-btn bp-btn-primary post-annotation-btn" data-type="post-reply-btn">
${__('annotation_post')}
</button>
</div>
</div>
</section>`.trim();
return dialogEl;
}
}
export default AnnotationDialog;