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

feat(annotations): Hide annotation controls when fullscreen is active #1188

Merged
merged 6 commits into from
Mar 31, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 4 additions & 0 deletions src/lib/AnnotationControls.scss
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@
}
}
}

&.is-hidden {
display: none;
}
}
42 changes: 42 additions & 0 deletions src/lib/AnnotationControls.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import noop from 'lodash/noop';
import { ICON_REGION_COMMENT } from './icons/icons';
import Controls, { CLASS_BOX_CONTROLS_GROUP_BUTTON } from './Controls';
import fullscreen from './Fullscreen';

export const CLASS_ANNOTATIONS_GROUP = 'bp-AnnotationControls-group';
export const CLASS_REGION_BUTTON = 'bp-AnnotationControls-regionBtn';

export const CLASS_BUTTON_ACTIVE = 'is-active';
export const CLASS_GROUP_HIDE = 'is-hidden';

export type RegionHandler = ({ isRegionActive, event }: { isRegionActive: boolean; event: MouseEvent }) => void;
export type Options = {
Expand Down Expand Up @@ -32,8 +35,47 @@ export default class AnnotationControls {
}

this.controls = controls;

this.attachEventHandlers();
}

/**
* [destructor]
*
* @return {void}
*/
public destroy(): void {
if (fullscreen) {
fullscreen.removeAllListeners();
mxiao6 marked this conversation as resolved.
Show resolved Hide resolved
}
}

/**
* Attaches event handlers
*
* @return {void}
*/
private attachEventHandlers = (): void => {
mxiao6 marked this conversation as resolved.
Show resolved Hide resolved
fullscreen.addListener('enter', () => this.handleFullscreenChange(true));
fullscreen.addListener('exit', () => this.handleFullscreenChange(false));
};

/**
* Hide annotations control button group
*
* @param {boolean} isFullscreen - true if full screen will be active
* @return {void}
*/
private handleFullscreenChange = (isFullscreen: boolean): void => {
const groupElement = this.controls.controlsEl.querySelector(`.${CLASS_ANNOTATIONS_GROUP}`);

if (isFullscreen) {
groupElement.classList.add(CLASS_GROUP_HIDE);
} else {
groupElement.classList.remove(CLASS_GROUP_HIDE);
}
};

/**
* Region comment button click handler
*
Expand Down
52 changes: 49 additions & 3 deletions src/lib/__tests__/AnnotationControls-test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
/* eslint-disable no-unused-expressions */
import AnnotationControls, { CLASS_REGION_BUTTON, CLASS_BUTTON_ACTIVE } from '../AnnotationControls';
import Controls, { CLASS_BOX_CONTROLS_GROUP_BUTTON } from '../Controls';
import { ICON_REGION_COMMENT } from '../icons/icons';
import AnnotationControls, {
CLASS_ANNOTATIONS_GROUP,
CLASS_BUTTON_ACTIVE,
CLASS_GROUP_HIDE,
CLASS_REGION_BUTTON,
} from '../AnnotationControls';
import Controls, { CLASS_BOX_CONTROLS_GROUP_BUTTON } from '../Controls';
import fullscreen from '../Fullscreen';

let annotationControls;
let stubs = {};
Expand All @@ -16,8 +22,9 @@ describe('lib/AnnotationControls', () => {
beforeEach(() => {
fixture.load('__tests__/AnnotationControls-test.html');
const controls = new Controls(document.getElementById('test-annotation-controls-container'));
annotationControls = new AnnotationControls(controls);
stubs.fullscreenAddListener = sandbox.stub(fullscreen, 'addListener');
stubs.onRegionClick = sandbox.stub();
annotationControls = new AnnotationControls(controls);
});

afterEach(() => {
Expand All @@ -33,11 +40,27 @@ describe('lib/AnnotationControls', () => {
expect(annotationControls.controls).not.to.be.undefined;
});

it('should attach event listeners', () => {
expect(stubs.fullscreenAddListener).to.be.calledTwice;
});

it('should throw an exception if controls is not provided', () => {
expect(() => new AnnotationControls()).to.throw(Error, 'controls must be an instance of Controls');
});
});

describe('destroy()', () => {
beforeEach(() => {
stubs.fullscreenRemoveAllListeners = sandbox.stub(fullscreen, 'removeAllListeners');
});

it('should remove all listeners', () => {
annotationControls.destroy();

expect(stubs.fullscreenRemoveAllListeners).to.be.called;
});
});

describe('init()', () => {
beforeEach(() => {
stubs.add = sandbox.stub(annotationControls.controls, 'add');
Expand Down Expand Up @@ -94,4 +117,27 @@ describe('lib/AnnotationControls', () => {
});
});
});

describe('handleFullscreenChange()', () => {
beforeEach(() => {
stubs.classListAdd = sandbox.stub();
stubs.classListRemove = sandbox.stub();
stubs.querySelector = sandbox.stub().returns({
classList: {
add: stubs.classListAdd,
remove: stubs.classListRemove,
},
});
annotationControls.controls.controlsEl.querySelector = stubs.querySelector;
});

it('should hide entire group if fullscreen is active', () => {
annotationControls.handleFullscreenChange(true);
expect(stubs.querySelector).to.be.calledWith(`.${CLASS_ANNOTATIONS_GROUP}`);
expect(stubs.classListAdd).to.be.calledWith(CLASS_GROUP_HIDE);

annotationControls.handleFullscreenChange(false);
expect(stubs.classListRemove).to.be.calledWith(CLASS_GROUP_HIDE);
});
});
});
4 changes: 4 additions & 0 deletions src/lib/viewers/doc/DocBaseViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ class DocBaseViewer extends BaseViewer {
URL.revokeObjectURL(this.printURL);
}

if (this.annotationControls) {
this.annotationControls.destroy();
}

if (this.pageControls) {
this.pageControls.removeListener('pagechange', this.setPage);
}
Expand Down