diff --git a/src/lib/annotations/Annotator.js b/src/lib/annotations/Annotator.js index eec154e21..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); } /** @@ -327,6 +328,10 @@ class Annotator extends EventEmitter { * @return {void} */ toggleAnnotationHandler(mode, event = {}) { + if (!this.isModeAnnotatable(mode)) { + return; + } + this.destroyPendingThreads(); // No specific mode available for annotation type @@ -693,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 56e1bf783..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); }); }); @@ -293,6 +294,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 +306,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..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 @@ -677,6 +676,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); });