Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Add clickHandlers back for all enabled annotation modes #248

Merged
merged 3 commits into from
Jul 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/lib/viewers/BaseViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -767,9 +767,16 @@ class BaseViewer extends EventEmitter {
* @param {string} mode - Target annotation mode
* @return {Function|null} Click handler
*/
/* eslint-disable no-unused-vars */
getAnnotationModeClickHandler(mode) {}
/* eslint-enable no-unused-vars */
getAnnotationModeClickHandler(mode) {
if (!mode || !this.isAnnotatable(mode)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can isAnnotatable() just return false if null is passed in?

Copy link
Contributor Author

@pramodsum pramodsum Jul 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It defaults to the value of previewOptions.showAnnotations if no viewer specific options are specified. So ultimately it'll end up doing that

return null;
}

const eventName = `toggle${mode}annotationmode`;
return () => {
this.emit(eventName);
};
}

/**
* Disables viewer controls
Expand Down
27 changes: 27 additions & 0 deletions src/lib/viewers/__tests__/BaseViewer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -843,4 +843,31 @@ describe('lib/viewers/BaseViewer', () => {
expect(buttonEl.classList.contains(constants.CLASS_HIDDEN)).to.be.false;
});
});

describe('getAnnotationModeClickHandler()', () => {
beforeEach(() => {
stubs.isAnnotatable = sandbox.stub(base, 'isAnnotatable').returns(false);
});

it('should return null if you cannot annotate', () => {
const handler = base.getAnnotationModeClickHandler('point');
expect(stubs.isAnnotatable).to.be.called;
expect(handler).to.equal(null);
});

it('should return the toggle point mode handler', () => {
stubs.isAnnotatable.returns(true);
sandbox.stub(base, 'emit');
base.annotator = {
togglePointAnnotationHandler: () => {}
};

const handler = base.getAnnotationModeClickHandler('point');
expect(stubs.isAnnotatable).to.be.called;
expect(handler).to.be.a('function');

handler(event);
expect(base.emit).to.have.been.calledWith('togglepointannotationmode');
});
});
});
14 changes: 0 additions & 14 deletions src/lib/viewers/doc/DocBaseViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -442,20 +442,6 @@ class DocBaseViewer extends BaseViewer {
this.pdfViewer.currentScaleValue = newScale;
}

/**
* @inheritdoc
*/
getAnnotationModeClickHandler(mode) {
if (!mode || !this.isAnnotatable(mode)) {
return null;
}

const eventName = `toggle${mode}annotationmode`;
return () => {
this.emit(eventName);
};
}

/**
* Handles keyboard events for document viewer.
*
Expand Down
27 changes: 0 additions & 27 deletions src/lib/viewers/doc/__tests__/DocBaseViewer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -699,33 +699,6 @@ describe('src/lib/viewers/doc/DocBaseViewer', () => {
});
});

describe('getPointModeClickHandler()', () => {
beforeEach(() => {
stubs.isAnnotatable = sandbox.stub(docBase, 'isAnnotatable').returns(false);
});

it('should return null if you cannot annotate', () => {
const handler = docBase.getAnnotationModeClickHandler('point');
expect(stubs.isAnnotatable).to.be.called;
expect(handler).to.equal(null);
});

it('should return the toggle point mode handler', () => {
stubs.isAnnotatable.returns(true);
sandbox.stub(docBase, 'emit');
docBase.annotator = {
togglePointAnnotationHandler: () => {}
};

const handler = docBase.getAnnotationModeClickHandler('point');
expect(stubs.isAnnotatable).to.be.called;
expect(handler).to.be.a('function');

handler(event);
expect(docBase.emit).to.have.been.calledWith('togglepointannotationmode');
});
});

describe('onKeyDown()', () => {
beforeEach(() => {
stubs.previousPage = sandbox.stub(docBase, 'previousPage');
Expand Down
7 changes: 4 additions & 3 deletions src/lib/viewers/image/ImageBaseViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,15 +347,16 @@ class ImageBaseViewer extends BaseViewer {
/**
* @inheritdoc
*/
getPointModeClickHandler() {
if (!this.isAnnotatable('point')) {
getAnnotationModeClickHandler(mode) {
if (!mode || !this.isAnnotatable(mode)) {
return null;
}

const eventName = `toggle${mode}annotationmode`;
return () => {
this.imageEl.classList.remove(CSS_CLASS_ZOOMABLE);
this.imageEl.classList.remove(CSS_CLASS_PANNABLE);
this.emit('togglepointannotationmode');
this.emit(eventName);
};
}

Expand Down
39 changes: 39 additions & 0 deletions src/lib/viewers/image/__tests__/ImageBaseViewer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ describe('lib/viewers/image/ImageBaseViewer', () => {
imageBase = new ImageBaseViewer(containerEl);
imageBase.containerEl = containerEl;
imageBase.imageEl = document.createElement('div');

event = {
preventDefault: sandbox.stub(),
stopPropagation: sandbox.stub(),
touches: [0, 0]
};
});

afterEach(() => {
Expand All @@ -38,6 +44,7 @@ describe('lib/viewers/image/ImageBaseViewer', () => {

imageBase = null;
stubs = {};
event = {};
});

describe('destroy()', () => {
Expand Down Expand Up @@ -468,6 +475,38 @@ describe('lib/viewers/image/ImageBaseViewer', () => {
});
});

describe('getAnnotationModeClickHandler()', () => {
beforeEach(() => {
stubs.isAnnotatable = sandbox.stub(imageBase, 'isAnnotatable').returns(false);
});

it('should return null if you cannot annotate', () => {
const handler = imageBase.getAnnotationModeClickHandler('point');
expect(stubs.isAnnotatable).to.be.called;
expect(handler).to.equal(null);
});

it('should return the toggle point mode handler', () => {
stubs.isAnnotatable.returns(true);
stubs.emitter = sandbox.stub(imageBase, 'emit');
imageBase.annotator = {
togglePointAnnotationHandler: () => {}
};
imageBase.imageEl.classList.add(CSS_CLASS_PANNABLE);
imageBase.imageEl.classList.add(CSS_CLASS_ZOOMABLE);

const handler = imageBase.getAnnotationModeClickHandler('point');
expect(stubs.isAnnotatable).to.be.called;
expect(handler).to.be.a('function');

handler(event);

expect(imageBase.imageEl).to.not.have.class(CSS_CLASS_PANNABLE);
expect(imageBase.imageEl).to.not.have.class(CSS_CLASS_ZOOMABLE);
expect(imageBase.emit).to.have.been.calledWith('togglepointannotationmode');
});
});

describe('finishLoading()', () => {
beforeEach(() => {
imageBase.loaded = false;
Expand Down
4 changes: 2 additions & 2 deletions src/lib/viewers/image/__tests__/ImageViewer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ describe('lib/viewers/image/ImageViewer', () => {
describe('getPointModeClickHandler()', () => {
it('should do nothing if not annotatable', () => {
sandbox.stub(image, 'isAnnotatable').returns(false);
const handler = image.getPointModeClickHandler();
const handler = image.getAnnotationModeClickHandler('point');
expect(handler).to.be.null;
});

Expand All @@ -601,7 +601,7 @@ describe('lib/viewers/image/ImageViewer', () => {
image.imageEl.classList.add(CSS_CLASS_PANNABLE);
sandbox.stub(image, 'isAnnotatable').returns(true);

const handler = image.getPointModeClickHandler();
const handler = image.getAnnotationModeClickHandler('point');
expect(handler).to.be.a('function');

handler(event);
Expand Down