From 1788f198e34163dabec877cd3e9ca1a95ab563af Mon Sep 17 00:00:00 2001 From: Sumedha Pramod Date: Mon, 14 Aug 2017 16:38:41 -0700 Subject: [PATCH 1/2] Chore: Toggling annotation mode handlers using emitted messages --- src/lib/annotations/Annotator.js | 4 ++++ src/lib/annotations/__tests__/Annotator-test.js | 11 +++++++++-- src/lib/viewers/BaseViewer.js | 5 +++++ src/lib/viewers/__tests__/BaseViewer-test.js | 2 +- 4 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/lib/annotations/Annotator.js b/src/lib/annotations/Annotator.js index eec154e21..f50ae6ad6 100644 --- a/src/lib/annotations/Annotator.js +++ b/src/lib/annotations/Annotator.js @@ -327,6 +327,10 @@ class Annotator extends EventEmitter { * @return {void} */ toggleAnnotationHandler(mode, event = {}) { + if (!this.isModeAnnotatable(mode)) { + return; + } + this.destroyPendingThreads(); // No specific mode available for annotation type diff --git a/src/lib/annotations/__tests__/Annotator-test.js b/src/lib/annotations/__tests__/Annotator-test.js index 56e1bf783..084bcd524 100644 --- a/src/lib/annotations/__tests__/Annotator-test.js +++ b/src/lib/annotations/__tests__/Annotator-test.js @@ -293,6 +293,7 @@ describe('lib/annotations/Annotator', () => { stubs.disable = sandbox.stub(annotator, 'disableAnnotationMode'); stubs.enable = sandbox.stub(annotator, 'enableAnnotationMode'); sandbox.stub(annotator.previewUI, 'getAnnotateButton'); + stubs.isAnnotatable = sandbox.stub(annotator, 'isModeAnnotatable').returns(true); annotator.modeButtons = { point: { selector: 'point_btn' }, @@ -304,11 +305,17 @@ describe('lib/annotations/Annotator', () => { annotator.modeButtons = {}; }); + it('should do nothing if specified annotation type is not annotatable', () => { + stubs.isAnnotatable.returns(false); + annotator.toggleAnnotationHandler('bleh'); + expect(stubs.destroyStub).to.not.be.called; + }); + it('should do nothing if specified annotation type does not have a mode button', () => { annotator.toggleAnnotationHandler(TYPES.highlight); expect(stubs.destroyStub).to.be.called; - expect(stubs.exitAnnotationModes) - }) + expect(stubs.exitModes).to.not.be.called; + }); it('should turn annotation mode on if it is off', () => { stubs.annotationMode.returns(false); diff --git a/src/lib/viewers/BaseViewer.js b/src/lib/viewers/BaseViewer.js index a2cd08190..826bcb243 100644 --- a/src/lib/viewers/BaseViewer.js +++ b/src/lib/viewers/BaseViewer.js @@ -677,6 +677,11 @@ class BaseViewer extends EventEmitter { }); this.annotator.init(this.scale); + // Add a custom listener for entering/exit annotations mode using the app's custom annotations buttons + this.addListener('toggleannotationmode', (data) => { + this.annotator.toggleAnnotationHandler(data); + }); + // Add a custom listener for events related to scaling/orientation changes this.addListener('scale', (data) => { this.annotator.emit('scaleAnnotations', data); diff --git a/src/lib/viewers/__tests__/BaseViewer-test.js b/src/lib/viewers/__tests__/BaseViewer-test.js index 7cce03335..905a9bfaf 100644 --- a/src/lib/viewers/__tests__/BaseViewer-test.js +++ b/src/lib/viewers/__tests__/BaseViewer-test.js @@ -769,7 +769,6 @@ describe('lib/viewers/BaseViewer', () => { base.annotator = { init: sandbox.stub(), addListener: sandbox.stub(), - setScale: sandbox.stub() }; base.annotatorConf = { CONSTRUCTOR: sandbox.stub().returns(base.annotator) @@ -779,6 +778,7 @@ describe('lib/viewers/BaseViewer', () => { it('should initialize the annotator', () => { expect(base.annotator.init).to.be.calledWith(1.5); + expect(base.addListener).to.be.calledWith('toggleannotationmode', sinon.match.func); expect(base.addListener).to.be.calledWith('scale', sinon.match.func); expect(base.annotator.addListener).to.be.calledWith('annotatorevent', sinon.match.func); }); From c3ca9552067311314f0e8a7def3f754c040b1ed1 Mon Sep 17 00:00:00 2001 From: Sumedha Pramod Date: Mon, 14 Aug 2017 17:02:14 -0700 Subject: [PATCH 2/2] Chore: cleaning up commit --- src/lib/annotations/Annotator.js | 5 +++-- src/lib/annotations/__tests__/Annotator-test.js | 5 +++-- src/lib/viewers/BaseViewer.js | 1 - 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/lib/annotations/Annotator.js b/src/lib/annotations/Annotator.js index f50ae6ad6..4232d93c6 100644 --- a/src/lib/annotations/Annotator.js +++ b/src/lib/annotations/Annotator.js @@ -89,6 +89,7 @@ class Annotator extends EventEmitter { this.unbindDOMListeners(); this.unbindCustomListenersOnService(); this.removeListener('scaleAnnotations', this.scaleAnnotations); + this.removeListener('toggleannotationmode', this.toggleAnnotationHandler); } /** @@ -697,8 +698,8 @@ class Annotator extends EventEmitter { handlers.push({ type: 'click', func: () => { - drawingThread.saveAnnotation(TYPES.draw); - this.toggleAnnotationHandler(TYPES.draw); + drawingThread.saveAnnotation(mode); + this.toggleAnnotationHandler(mode); }, eventObj: postButtonEl }); diff --git a/src/lib/annotations/__tests__/Annotator-test.js b/src/lib/annotations/__tests__/Annotator-test.js index 084bcd524..86246bf09 100644 --- a/src/lib/annotations/__tests__/Annotator-test.js +++ b/src/lib/annotations/__tests__/Annotator-test.js @@ -93,14 +93,15 @@ describe('lib/annotations/Annotator', () => { const unbindCustomStub = sandbox.stub(annotator, 'unbindCustomListenersOnThread'); const unbindDOMStub = sandbox.stub(annotator, 'unbindDOMListeners'); const unbindCustomListenersOnService = sandbox.stub(annotator, 'unbindCustomListenersOnService'); - const unbindScaleListener = sandbox.stub(annotator, 'removeListener'); + const unbindListener = sandbox.stub(annotator, 'removeListener'); annotator.destroy(); expect(unbindCustomStub).to.be.calledWith(stubs.thread); expect(unbindDOMStub).to.be.called; expect(unbindCustomListenersOnService).to.be.called; - expect(unbindScaleListener).to.be.calledWith('scaleAnnotations', sinon.match.func); + expect(unbindListener).to.be.calledWith('scaleAnnotations', sinon.match.func); + expect(unbindListener).to.be.calledWith('toggleannotationmode', sinon.match.func); }); }); diff --git a/src/lib/viewers/BaseViewer.js b/src/lib/viewers/BaseViewer.js index 826bcb243..00838dc9c 100644 --- a/src/lib/viewers/BaseViewer.js +++ b/src/lib/viewers/BaseViewer.js @@ -332,7 +332,6 @@ class BaseViewer extends EventEmitter { addCommonListeners() { // Attach common full screen event listeners fullscreen.addListener('enter', this.onFullscreenToggled); - fullscreen.addListener('exit', this.onFullscreenToggled); // Add a resize handler for the window