Skip to content

Commit

Permalink
Chore: Toggling annotation mode handlers using emitted messages (#307)
Browse files Browse the repository at this point in the history
  • Loading branch information
pramodsum committed Aug 15, 2017
1 parent 5b66c82 commit 8e7d49a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
9 changes: 7 additions & 2 deletions src/lib/annotations/Annotator.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class Annotator extends EventEmitter {
this.unbindDOMListeners();
this.unbindCustomListenersOnService();
this.removeListener('scaleAnnotations', this.scaleAnnotations);
this.removeListener('toggleannotationmode', this.toggleAnnotationHandler);
}

/**
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
});
Expand Down
16 changes: 12 additions & 4 deletions src/lib/annotations/__tests__/Annotator-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

Expand Down Expand Up @@ -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' },
Expand All @@ -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);
Expand Down
6 changes: 5 additions & 1 deletion src/lib/viewers/BaseViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/viewers/__tests__/BaseViewer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
});
Expand Down

0 comments on commit 8e7d49a

Please sign in to comment.