Skip to content

Commit

Permalink
Fix: Only unbind listeners from existing DOM elements (#321)
Browse files Browse the repository at this point in the history
* Fix: Don't unbind listeners from non-existent elements

* Chore: Cleanup

* Chore: Standardize guard clauses

* Chore: Guarding the removal of additional event listener
  • Loading branch information
pramodsum authored and JustinHoldstock committed Jan 15, 2019
1 parent a474251 commit 6a71a9e
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 22 deletions.
6 changes: 2 additions & 4 deletions src/AnnotationThread.js
Original file line number Diff line number Diff line change
Expand Up @@ -454,11 +454,9 @@ class AnnotationThread extends EventEmitter {
* @return {void}
*/
unbindDOMListeners() {
if (!this.element) {
return;
if (this.element) {
this.element.removeEventListener('click', this.renderAnnotationPopover);
}

this.element.removeEventListener('click', this.renderAnnotationPopover);
}

/**
Expand Down
5 changes: 4 additions & 1 deletion src/Annotator.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,10 @@ class Annotator extends EventEmitter {
*/
unbindCustomListeners() {
this.removeListener(ANNOTATOR_EVENT.scale, this.scaleAnnotations);
this.api.removeListener(ANNOTATOR_EVENT.error, this.handleServicesErrors);

if (this.api) {
this.api.removeListener(ANNOTATOR_EVENT.error, this.handleServicesErrors);
}
}

/**
Expand Down
5 changes: 4 additions & 1 deletion src/controllers/AnnotationModeController.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,10 @@ class AnnotationModeController extends EventEmitter {
if (this.buttonEl) {
this.buttonEl.removeEventListener('click', this.toggleMode);
}
this.api.removeListener(CONTROLLER_EVENT.error, this.handleAPIErrors);

if (this.api) {
this.api.removeListener(CONTROLLER_EVENT.error, this.handleAPIErrors);
}
}

/**
Expand Down
22 changes: 13 additions & 9 deletions src/doc/DocAnnotator.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,10 +364,19 @@ class DocAnnotator extends Annotator {
unbindDOMListeners() {
super.unbindDOMListeners();

this.container.removeEventListener('resize', this.resetAnnotationUI);
this.annotatedElement.removeEventListener('wheel', this.hideCreateDialog);
this.annotatedElement.removeEventListener('touchend', this.hideCreateDialog);
this.annotatedElement.removeEventListener('click', this.clickHandler);
if (this.container) {
this.container.removeEventListener('resize', this.resetAnnotationUI);
}

if (this.annotatedElement) {
this.annotatedElement.removeEventListener('wheel', this.hideCreateDialog);
this.annotatedElement.removeEventListener('touchend', this.hideCreateDialog);
this.annotatedElement.removeEventListener('click', this.clickHandler);
this.annotatedElement.removeEventListener('mouseup', this.highlightMouseupHandler);
this.annotatedElement.removeEventListener('dblclick', this.highlightMouseupHandler);
this.annotatedElement.removeEventListener('mousedown', this.highlightMousedownHandler);
this.annotatedElement.removeEventListener('contextmenu', this.highlightMousedownHandler);
}

if (this.highlightThrottleHandle) {
cancelAnimationFrame(this.highlightThrottleHandle);
Expand All @@ -381,11 +390,6 @@ class DocAnnotator extends Annotator {

if (this.hasTouch) {
document.removeEventListener('selectionchange', this.onSelectionChange);
} else {
this.annotatedElement.removeEventListener('mouseup', this.highlightMouseupHandler);
this.annotatedElement.removeEventListener('dblclick', this.highlightMouseupHandler);
this.annotatedElement.removeEventListener('mousedown', this.highlightMousedownHandler);
this.annotatedElement.removeEventListener('contextmenu', this.highlightMousedownHandler);
}
}

Expand Down
12 changes: 7 additions & 5 deletions src/drawing/DrawingThread.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,13 @@ class DrawingThread extends AnnotationThread {
* @return {void}
*/
unbindDrawingListeners() {
this.annotatedElement.removeEventListener('mousemove', this.userMoveHandler);
this.annotatedElement.removeEventListener('mouseup', this.userStopHandler);
this.annotatedElement.removeEventListener('touchmove', this.userMoveHandler);
this.annotatedElement.removeEventListener('touchcancel', this.userStopHandler);
this.annotatedElement.removeEventListener('touchend', this.userStopHandler);
if (this.annotatedElement) {
this.annotatedElement.removeEventListener('mousemove', this.userMoveHandler);
this.annotatedElement.removeEventListener('mouseup', this.userStopHandler);
this.annotatedElement.removeEventListener('touchmove', this.userMoveHandler);
this.annotatedElement.removeEventListener('touchcancel', this.userStopHandler);
this.annotatedElement.removeEventListener('touchend', this.userStopHandler);
}
}

/**
Expand Down
8 changes: 6 additions & 2 deletions src/image/ImageAnnotator.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,17 @@ class ImageAnnotator extends Annotator {

/** @inheritdoc */
bindDOMListeners() {
this.annotatedElement.addEventListener('mouseup', this.hideAnnotations);
if (this.annotatedElement) {
this.annotatedElement.addEventListener('mouseup', this.hideAnnotations);
}
super.bindDOMListeners();
}

/** @inheritdoc */
unbindDOMListeners() {
this.annotatedElement.removeEventListener('mouseup', this.hideAnnotations);
if (this.annotatedElement) {
this.annotatedElement.removeEventListener('mouseup', this.hideAnnotations);
}
super.bindDOMListeners();
}
}
Expand Down

0 comments on commit 6a71a9e

Please sign in to comment.