Skip to content

Commit

Permalink
Event names into separate file (#589)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinHoldstock authored Jan 18, 2018
1 parent f6c3041 commit 3805904
Show file tree
Hide file tree
Showing 26 changed files with 106 additions and 68 deletions.
25 changes: 13 additions & 12 deletions src/lib/Preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {
X_REP_HINT_VIDEO_DASH,
X_REP_HINT_VIDEO_MP4
} from './constants';
import { VIEWER_EVENT } from './events';
import './Preview.scss';

const DEFAULT_DISABLED_VIEWERS = ['Office']; // viewers disabled by default
Expand Down Expand Up @@ -946,7 +947,7 @@ class Preview extends EventEmitter {
attachViewerListeners() {
// Node requires listener attached to 'error'
this.viewer.addListener('error', this.triggerError);
this.viewer.addListener('viewerevent', this.handleViewerEvents);
this.viewer.addListener(VIEWER_EVENT.default, this.handleViewerEvents);
}

/**
Expand All @@ -959,34 +960,34 @@ class Preview extends EventEmitter {
handleViewerEvents(data) {
/* istanbul ignore next */
switch (data.event) {
case 'download':
case VIEWER_EVENT.download:
this.download();
break;
case 'reload':
case VIEWER_EVENT.reload:
this.reload(); // Reload preview and fetch updated file info depending on `skipServerUpdate` option
break;
case 'load':
case VIEWER_EVENT.load:
this.finishLoading(data.data);
break;
case 'progressstart':
case VIEWER_EVENT.progressStart:
this.ui.startProgressBar();
break;
case 'progressend':
case VIEWER_EVENT.progressEnd:
this.ui.finishProgressBar();
break;
case 'notificationshow':
case VIEWER_EVENT.notificationShow:
this.ui.showNotification(data.data);
break;
case 'notificationhide':
case VIEWER_EVENT.notificationHide:
this.ui.hideNotification();
break;
case 'mediaendautoplay':
case VIEWER_EVENT.mediaEndAutoplay:
this.navigateRight();
break;
default:
// This includes 'notification', 'preload' and others
this.emit(data.event, data.data);
this.emit('viewerevent', data);
this.emit(VIEWER_EVENT.default, data);
}
}

Expand Down Expand Up @@ -1033,7 +1034,7 @@ class Preview extends EventEmitter {
this.count.error += 1;

// 'load' with { error } signifies a preview error
this.emit('load', {
this.emit(VIEWER_EVENT.load, {
error,
metrics: this.logger.done(this.count),
file: this.file
Expand All @@ -1048,7 +1049,7 @@ class Preview extends EventEmitter {
this.count.success += 1;

// Finally emit the viewer instance back with a load event
this.emit('load', {
this.emit(VIEWER_EVENT.load, {
viewer: this.viewer,
metrics: this.logger.done(this.count),
file: this.file
Expand Down
21 changes: 11 additions & 10 deletions src/lib/__tests__/Preview-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Browser from '../Browser';
import * as file from '../file';
import * as util from '../util';
import { API_HOST, CLASS_NAVIGATION_VISIBILITY } from '../constants';
import { VIEWER_EVENT } from '../events';

const tokens = require('../tokens');

Expand Down Expand Up @@ -1435,60 +1436,60 @@ describe('lib/Preview', () => {

preview.attachViewerListeners();
expect(preview.viewer.addListener).to.be.calledWith('error', sinon.match.func);
expect(preview.viewer.addListener).to.be.calledWith('viewerevent', sinon.match.func);
expect(preview.viewer.addListener).to.be.calledWith(VIEWER_EVENT.default, sinon.match.func);
});
});

describe('handleViewerEvents()', () => {
it('should call download on download event', () => {
sandbox.stub(preview, 'download');
preview.handleViewerEvents({ event: 'download' });
preview.handleViewerEvents({ event: VIEWER_EVENT.download });
expect(preview.download).to.be.called;
});

it('should reload preview on reload event', () => {
sandbox.stub(preview, 'reload');
preview.handleViewerEvents({ event: 'reload' });
preview.handleViewerEvents({ event: VIEWER_EVENT.reload });
expect(preview.reload).to.be.called;
});

it('should finish loading preview on load event', () => {
sandbox.stub(preview, 'finishLoading');
preview.handleViewerEvents({ event: 'load' });
preview.handleViewerEvents({ event: VIEWER_EVENT.load });
expect(preview.finishLoading).to.be.called;
});

it('should start progress bar on progressstart event', () => {
sandbox.stub(preview.ui, 'startProgressBar');
preview.handleViewerEvents({ event: 'progressstart' });
preview.handleViewerEvents({ event: VIEWER_EVENT.progressStart });
expect(preview.ui.startProgressBar).to.be.called;
});

it('should finish progress bar on progressend event', () => {
sandbox.stub(preview.ui, 'finishProgressBar');
preview.handleViewerEvents({ event: 'progressend' });
preview.handleViewerEvents({ event: VIEWER_EVENT.progressEnd });
expect(preview.ui.finishProgressBar).to.be.called;
});

it('should show notification with message on notificationshow event', () => {
const message = 'notification_message';
sandbox.stub(preview.ui, 'showNotification');
preview.handleViewerEvents({
event: 'notificationshow',
event: VIEWER_EVENT.notificationShow,
data: message
});
expect(preview.ui.showNotification).to.be.calledWith(message);
});

it('should hide notification on notificationhide event', () => {
sandbox.stub(preview.ui, 'hideNotification');
preview.handleViewerEvents({ event: 'notificationhide' });
preview.handleViewerEvents({ event: VIEWER_EVENT.notificationHide });
expect(preview.ui.hideNotification).to.be.called;
});

it('should navigate right on mediaendautoplay event', () => {
sandbox.stub(preview, 'navigateRight');
const data = { event: 'mediaendautoplay' };
const data = { event: VIEWER_EVENT.mediaEndAutoplay };

preview.handleViewerEvents(data);
expect(preview.navigateRight).to.be.called;
Expand All @@ -1502,7 +1503,7 @@ describe('lib/Preview', () => {
};
preview.handleViewerEvents(data);
expect(preview.emit).to.be.calledWith(data.event, data.data);
expect(preview.emit).to.be.calledWith('viewerevent', data);
expect(preview.emit).to.be.calledWith(VIEWER_EVENT.default, data);
});
});

Expand Down
13 changes: 13 additions & 0 deletions src/lib/events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Events emitted by Viewers
// eslint-disable-next-line import/prefer-default-export
export const VIEWER_EVENT = {
download: 'download', // Begin downloading the file.
reload: 'reload', // Reload preview.
load: 'load', // Preview is finished loading.
progressStart: 'progressstart', // Begin using loading indicator.
progressEnd: 'progressend', // Stop using loading indicator.
notificationShow: 'notificationshow', // Show notification modal.
notificationHide: 'notificationhide', // Hide notification modal.
mediaEndAutoplay: 'mediaendautoplay', // Media playback has completed, with autoplay enabled.
default: 'viewerevent' // The default viewer event
};
13 changes: 7 additions & 6 deletions src/lib/viewers/BaseViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
STATUS_VIEWABLE
} from '../constants';
import { getIconFromExtension, getIconFromName } from '../icons/icons';
import { VIEWER_EVENT } from '../events';

const ANNOTATIONS_JS = 'annotations.js';
const ANNOTATIONS_CSS = 'annotations.css';
Expand Down Expand Up @@ -378,7 +379,7 @@ class BaseViewer extends EventEmitter {
this.containerEl.addEventListener('contextmenu', this.preventDefault);
}

this.addListener('load', this.viewerLoadHandler);
this.addListener(VIEWER_EVENT.load, this.viewerLoadHandler);
}

/**
Expand Down Expand Up @@ -474,7 +475,7 @@ class BaseViewer extends EventEmitter {
const { file, viewer } = this.options;

super.emit(event, data);
super.emit('viewerevent', {
super.emit(VIEWER_EVENT.default, {
event,
data,
viewerName: viewer ? viewer.NAME : '',
Expand Down Expand Up @@ -827,22 +828,22 @@ class BaseViewer extends EventEmitter {
this.disableViewerControls();

if (data.data.mode === ANNOTATION_TYPE_POINT) {
this.emit('notificationshow', __('notification_annotation_point_mode'));
this.emit(VIEWER_EVENT.notificationShow, __('notification_annotation_point_mode'));
} else if (data.data.mode === ANNOTATION_TYPE_DRAW) {
this.emit('notificationshow', __('notification_annotation_draw_mode'));
this.emit(VIEWER_EVENT.notificationShow, __('notification_annotation_draw_mode'));
this.previewUI.replaceHeader(data.data.headerSelector);
}
break;
case ANNOTATOR_EVENT.modeExit:
this.enableViewerControls();
this.emit('notificationhide');
this.emit(VIEWER_EVENT.notificationHide);

if (data.data.mode === ANNOTATION_TYPE_DRAW) {
this.previewUI.replaceHeader(data.data.headerSelector);
}
break;
case ANNOTATOR_EVENT.error:
this.emit('notificationshow', data.data);
this.emit(VIEWER_EVENT.notificationShow, data.data);
break;
case ANNOTATOR_EVENT.fetch:
this.emit('scale', {
Expand Down
19 changes: 10 additions & 9 deletions src/lib/viewers/__tests__/BaseViewer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as util from '../../util';
import * as file from '../../file';
import * as icons from '../../icons/icons';
import * as constants from '../../constants';
import { VIEWER_EVENT } from '../../events';

let base;
let containerEl;
Expand Down Expand Up @@ -299,7 +300,7 @@ describe('lib/viewers/BaseViewer', () => {
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', sinon.match.func);
expect(stubs.baseAddListener).to.be.calledWith('load', sinon.match.func);
expect(stubs.baseAddListener).to.be.calledWith(VIEWER_EVENT.load, sinon.match.func);
});

it('should prevent the context menu if preview only permissions', () => {
Expand All @@ -319,7 +320,7 @@ describe('lib/viewers/BaseViewer', () => {

it('should handle annotations load', () => {
base.addCommonListeners();
expect(stubs.baseAddListener).to.be.calledWith('load', sinon.match.func);
expect(stubs.baseAddListener).to.be.calledWith(VIEWER_EVENT.load, sinon.match.func);
});
});

Expand Down Expand Up @@ -496,7 +497,7 @@ describe('lib/viewers/BaseViewer', () => {
base.emit(event, data);

expect(emitStub).to.be.calledWith(event, data);
expect(emitStub).to.be.calledWithMatch('viewerevent', {
expect(emitStub).to.be.calledWithMatch(VIEWER_EVENT.default, {
event,
data,
viewerName,
Expand Down Expand Up @@ -1009,7 +1010,7 @@ describe('lib/viewers/BaseViewer', () => {
};
base.handleAnnotatorEvents(data);
expect(base.disableViewerControls).to.be.called;
expect(base.emit).to.be.calledWith('notificationshow', sinon.match.string);
expect(base.emit).to.be.calledWith(VIEWER_EVENT.notificationShow, sinon.match.string);
expect(base.emit).to.be.calledWith(data.event, data.data);
expect(base.emit).to.be.calledWith('annotatorevent', data);
});
Expand All @@ -1024,7 +1025,7 @@ describe('lib/viewers/BaseViewer', () => {
};
base.handleAnnotatorEvents(data);
expect(base.disableViewerControls).to.be.called;
expect(base.emit).to.be.calledWith('notificationshow', sinon.match.string);
expect(base.emit).to.be.calledWith(VIEWER_EVENT.notificationShow, sinon.match.string);
expect(base.emit).to.be.calledWith(data.event, data.data);
expect(base.emit).to.be.calledWith('annotatorevent', data);
});
Expand All @@ -1038,7 +1039,7 @@ describe('lib/viewers/BaseViewer', () => {
};
base.handleAnnotatorEvents(data);
expect(base.enableViewerControls).to.be.called;
expect(base.emit).to.be.calledWith('notificationhide');
expect(base.emit).to.be.calledWith(VIEWER_EVENT.notificationHide);
expect(base.emit).to.be.calledWith(data.event, data.data);
expect(base.emit).to.be.calledWith('annotatorevent', data);
});
Expand All @@ -1052,7 +1053,7 @@ describe('lib/viewers/BaseViewer', () => {
};
base.handleAnnotatorEvents(data);
expect(base.enableViewerControls).to.be.called;
expect(base.emit).to.be.calledWith('notificationhide');
expect(base.emit).to.be.calledWith(VIEWER_EVENT.notificationHide);
expect(base.emit).to.be.calledWith(data.event, data.data);
expect(base.emit).to.be.calledWith('annotatorevent', data);
});
Expand All @@ -1063,7 +1064,7 @@ describe('lib/viewers/BaseViewer', () => {
data: 'message'
};
base.handleAnnotatorEvents(data);
expect(base.emit).to.be.calledWith('notificationshow', data.data);
expect(base.emit).to.be.calledWith(VIEWER_EVENT.notificationShow, data.data);
expect(base.emit).to.be.calledWith(data.event, data.data);
expect(base.emit).to.be.calledWith('annotatorevent', data);
});
Expand Down Expand Up @@ -1091,7 +1092,7 @@ describe('lib/viewers/BaseViewer', () => {
base.handleAnnotatorEvents(data);
expect(base.disableViewerControls).to.not.be.called;
expect(base.enableViewerControls).to.not.be.called;
expect(base.emit).to.not.be.calledWith('notificationshow', data.data);
expect(base.emit).to.not.be.calledWith(VIEWER_EVENT.notificationShow, data.data);
expect(base.emit).to.not.be.calledWith('scale', {
scale: base.scale,
rotationAngle: base.rotationAngle
Expand Down
3 changes: 2 additions & 1 deletion src/lib/viewers/box3d/Box3DViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
} from './box3DConstants';
import JS from './box3DAssets';
import './Box3D.scss';
import { VIEWER_EVENT } from '../../events';

// Milliseconds to wait for model to load before cancelling Preview
const LOAD_TIMEOUT = 50000;
Expand Down Expand Up @@ -249,7 +250,7 @@ class Box3DViewer extends BaseViewer {
handleContextRestored() {
this.detachEventHandlers();
this.contextNotification.show('WebGL Context Restored');
this.emit('progressstart');
this.emit(VIEWER_EVENT.progressStart);
this.previewUI.showLoadingIndicator();
this.postLoad();
}
Expand Down
3 changes: 2 additions & 1 deletion src/lib/viewers/box3d/__tests__/Box3DViewer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
EVENT_TOGGLE_VR,
EVENT_WEBGL_CONTEXT_RESTORED
} from '../box3DConstants';
import { VIEWER_EVENT } from '../../../events';

const sandbox = sinon.sandbox.create();

Expand Down Expand Up @@ -547,7 +548,7 @@ describe('lib/viewers/box3d/Box3DViewer', () => {
describe('handleContextRestored()', () => {
it('should call emit() with params ["progressstart"]', () => {
const emitStub = sandbox.stub(box3d, 'emit').callsFake((eventName) => {
expect(eventName).to.equal('progressstart');
expect(eventName).to.equal(VIEWER_EVENT.progressStart);
});

box3d.handleContextRestored();
Expand Down
5 changes: 3 additions & 2 deletions src/lib/viewers/doc/DocBaseViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { checkPermission, getRepresentation } from '../../file';
import { get, createAssetUrlCreator, getMidpoint, getDistance, getClosestPageToPinch } from '../../util';
import { ICON_PRINT_CHECKMARK } from '../../icons/icons';
import { JS, CSS } from './docAssets';
import { VIEWER_EVENT } from '../../events';

const CURRENT_PAGE_MAP_KEY = 'doc-current-page-map';
const DEFAULT_SCALE_DELTA = 1.1;
Expand Down Expand Up @@ -857,7 +858,7 @@ class DocBaseViewer extends BaseViewer {
// Broadcast that preview has 'loaded' when page structure is available
if (!this.loaded) {
this.loaded = true;
this.emit('load', {
this.emit(VIEWER_EVENT.load, {
numPages: this.pdfViewer.pagesCount,
endProgress: false, // Indicate that viewer will end progress later
scale: this.pdfViewer.currentScale
Expand Down Expand Up @@ -891,7 +892,7 @@ class DocBaseViewer extends BaseViewer {
// Fire progressend event to hide progress bar and cleanup preload after a page is rendered
if (!this.somePageRendered) {
this.hidePreload();
this.emit('progressend');
this.emit(VIEWER_EVENT.progressEnd);
this.somePageRendered = true;
}
}
Expand Down
Loading

0 comments on commit 3805904

Please sign in to comment.