Skip to content

Commit

Permalink
New: Disable right click when user does not have download permissions (
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy Press authored Oct 17, 2017
1 parent ed0efb8 commit b3a6407
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 14 deletions.
12 changes: 12 additions & 0 deletions src/lib/viewers/BaseViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,12 @@ class BaseViewer extends EventEmitter {

fullscreen.removeAllListeners();
document.defaultView.removeEventListener('resize', this.debouncedResizeHandler);

if (this.preventDefault) {
this.containerEl.removeEventListener('contextmenu', this.preventDefault);
this.preventDefault = null;
}

this.removeAllListeners();

if (this.containerEl) {
Expand Down Expand Up @@ -347,6 +353,12 @@ class BaseViewer extends EventEmitter {
// Add a resize handler for the window
document.defaultView.addEventListener('resize', this.debouncedResizeHandler);

const { permissions } = this.options.file;
if (permissions && !permissions.can_download) {
this.preventDefault = (event) => event.preventDefault();
this.containerEl.addEventListener('contextmenu', this.preventDefault);
}

this.addListener('load', (event) => {
if (event && event.scale) {
this.scale = event.scale;
Expand Down
54 changes: 40 additions & 14 deletions src/lib/viewers/__tests__/BaseViewer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,23 +280,39 @@ describe('lib/viewers/BaseViewer', () => {
});

describe('addCommonListeners()', () => {
beforeEach(() => {
stubs.fullscreenAddListener = sandbox.stub(fullscreen, 'addListener');
stubs.baseAddListener = sandbox.spy(base, 'addListener');
stubs.documentAddEventListener = sandbox.stub(document.defaultView, 'addEventListener');
stubs.loadAnnotator = sandbox.stub(base, 'loadAnnotator');


});
it('should append common event listeners', () => {
sandbox.stub(fullscreen, 'addListener');
sandbox.stub(base, 'addListener');
sandbox.stub(document.defaultView, 'addEventListener');
base.addCommonListeners();

expect(stubs.fullscreenAddListener).to.be.calledWith('enter', sinon.match.func);
expect(stubs.fullscreenAddListener).to.be.calledWith('exit', sinon.match.func);
expect(stubs.documentAddEventListener).to.be.calledWith('resize', base.debouncedResizeHandler);
expect(stubs.baseAddListener).to.be.calledWith('load', sinon.match.func);
});

it('should prevent the context menu if preview only permissions', () => {
base.options.file.permissions = {
can_download: false
};

base.containerEl = {
addEventListener: sandbox.stub(),
removeEventListener: sandbox.stub()
}

base.addCommonListeners();

expect(fullscreen.addListener).to.be.calledWith('enter', sinon.match.func);
expect(fullscreen.addListener).to.be.calledWith('exit', sinon.match.func);
expect(document.defaultView.addEventListener).to.be.calledWith('resize', base.debouncedResizeHandler);
expect(base.addListener).to.be.calledWith('load', sinon.match.func);
expect(base.containerEl.addEventListener).to.be.calledWith('contextmenu', sinon.match.func);
});

it('should load the annotator when load is emitted with scale', (done) => {
sandbox.stub(fullscreen, 'addListener');
sandbox.stub(document.defaultView, 'addEventListener');
sandbox.stub(base, 'loadAnnotator');
base.containerEl = containerEl;
base.annotationsPromise = {
then: (arg) => {
Expand All @@ -314,9 +330,6 @@ describe('lib/viewers/BaseViewer', () => {
});

it('should load the annotator when load is emitted without an event', (done) => {
sandbox.stub(fullscreen, 'addListener');
sandbox.stub(document.defaultView, 'addEventListener');
sandbox.stub(base, 'loadAnnotator');
base.containerEl = containerEl;
base.annotationsPromise = {
then: (arg) => {
Expand Down Expand Up @@ -427,6 +440,19 @@ describe('lib/viewers/BaseViewer', () => {
expect(base.annotator.removeAllListeners).to.be.called;
expect(base.annotator.destroy).to.be.called;
});

it('should remove the context listener if its callback exists', () => {
base.preventDefault = sandbox.stub();
base.containerEl = {
addEventListener: sandbox.stub(),
removeEventListener: sandbox.stub()
}

base.destroy();

expect(base.containerEl.removeEventListener).to.be.calledWith('contextmenu', sinon.match.func)
expect(base.preventDefault).to.be.null;
});
});

describe('emit()', () => {
Expand Down Expand Up @@ -472,7 +498,7 @@ describe('lib/viewers/BaseViewer', () => {
base = new BaseViewer({
container: containerEl,
file: {
id: '123'
id: '123',
}
});
sandbox.stub(base, 'loadAssets').returns(Promise.resolve());
Expand Down

0 comments on commit b3a6407

Please sign in to comment.