Skip to content

Commit

Permalink
feat(annotations): Hide annotation controls when fullscreen is active
Browse files Browse the repository at this point in the history
  • Loading branch information
Mingze Xiao committed Mar 31, 2020
1 parent 372ba14 commit bdfd7c9
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 9 deletions.
26 changes: 25 additions & 1 deletion src/lib/AnnotationControls.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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';
Expand Down Expand Up @@ -34,15 +35,38 @@ export default class AnnotationControls {
}

this.controls = controls;

this.attachEventHandlers();
}

/**
* [destructor]
*
* @return {void}
*/
public destroy(): void {
if (fullscreen) {
fullscreen.removeAllListeners();
}
}

/**
* Attaches event handlers
*
* @return {void}
*/
private attachEventHandlers = (): void => {
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}
*/
public toggleGroup = (isFullscreen: boolean): void => {
private handleFullscreenChange = (isFullscreen: boolean): void => {
const groupElement = this.controls.controlsEl.querySelector(`.${CLASS_ANNOTATIONS_GROUP}`);

if (isFullscreen) {
Expand Down
28 changes: 23 additions & 5 deletions src/lib/__tests__/AnnotationControls-test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
/* eslint-disable no-unused-expressions */
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 { ICON_REGION_COMMENT } from '../icons/icons';
import fullscreen from '../Fullscreen';

let annotationControls;
let stubs = {};
Expand All @@ -21,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 @@ -38,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 @@ -100,7 +118,7 @@ describe('lib/AnnotationControls', () => {
});
});

describe('toggleGroup()', () => {
describe('handleFullscreenChange()', () => {
beforeEach(() => {
stubs.classListAdd = sandbox.stub();
stubs.classListRemove = sandbox.stub();
Expand All @@ -114,11 +132,11 @@ describe('lib/AnnotationControls', () => {
});

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

annotationControls.toggleGroup(false);
annotationControls.handleFullscreenChange(false);
expect(stubs.classListRemove).to.be.calledWith(CLASS_GROUP_HIDE);
});
});
Expand Down
3 changes: 0 additions & 3 deletions src/lib/viewers/BaseViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -536,9 +536,6 @@ class BaseViewer extends EventEmitter {
*/
toggleFullscreen() {
fullscreen.toggle(this.containerEl);
if (this.options.showAnnotationsControls) {
this.annotationControls.toggleGroup(!fullscreen.isFullscreen()); // pass in the next state
}
}

/**
Expand Down
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

0 comments on commit bdfd7c9

Please sign in to comment.