Skip to content

Commit

Permalink
Fix: Only execute viewer setup if not already setup (#955)
Browse files Browse the repository at this point in the history
  • Loading branch information
Conrad Chan authored Mar 12, 2019
1 parent 5616b74 commit 8c2b193
Show file tree
Hide file tree
Showing 37 changed files with 186 additions and 75 deletions.
2 changes: 1 addition & 1 deletion src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
}

/* global Box */
var preview = new Box.Preview();
preview = new Box.Preview();

var previewOptions = options || {
enableThumbnailsSidebar: true,
Expand Down
6 changes: 6 additions & 0 deletions src/lib/Preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -1161,6 +1161,9 @@ class Preview extends EventEmitter {
// Add listeners for viewer events
this.attachViewerListeners();

// Setup the viewer DOM
this.viewer.setup();

// Load the representation into the viewer
this.viewer.load();

Expand Down Expand Up @@ -1472,6 +1475,9 @@ class Preview extends EventEmitter {
// Instantiate the error viewer
this.viewer = this.getErrorViewer();

// Setup error viewer
this.viewer.setup();

// Add listeners for viewer events
this.attachViewerListeners();

Expand Down
6 changes: 4 additions & 2 deletions src/lib/__tests__/Preview-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1577,7 +1577,8 @@ describe('lib/Preview', () => {
stubs.viewer = {
load: sandbox.stub(),
addListener: sandbox.stub(),
getName: sandbox.stub()
getName: sandbox.stub(),
setup: sandbox.stub()
};

/* eslint-disable require-jsdoc */
Expand Down Expand Up @@ -2163,7 +2164,8 @@ describe('lib/Preview', () => {
describe('triggerError()', () => {
const ErrorViewer = {
load: sandbox.stub(),
addListener: sandbox.stub()
addListener: sandbox.stub(),
setup: sandbox.stub()
};

beforeEach(() => {
Expand Down
15 changes: 12 additions & 3 deletions src/lib/viewers/BaseViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
replacePlaceholders
} from '../util';
import {
ANNOTATOR_EVENT,
CLASS_BOX_PREVIEW_MOBILE,
CLASS_HIDDEN,
FILE_OPTION_START,
Expand All @@ -26,10 +27,9 @@ import {
SELECTOR_BOX_PREVIEW_CONTENT,
SELECTOR_BOX_PREVIEW_CRAWLER_WRAPPER,
SELECTOR_BOX_PREVIEW_ICON,
STATUS_SUCCESS,
STATUS_VIEWABLE,
SELECTOR_BOX_PREVIEW,
ANNOTATOR_EVENT
STATUS_SUCCESS,
STATUS_VIEWABLE
} from '../constants';
import { getIconFromExtension, getIconFromName } from '../icons/icons';
import { VIEWER_EVENT, ERROR_CODE, LOAD_METRIC, DOWNLOAD_REACHABILITY_METRICS } from '../events';
Expand Down Expand Up @@ -114,6 +114,9 @@ class BaseViewer extends EventEmitter {
/** @property {HTMLElement} - The .bp-content which is the container for the viewer's content */
containerEl;

/** @property {boolean} - Stores whether the Viewer has been setup yet. */
isSetup = false;

/**
* [constructor]
*
Expand Down Expand Up @@ -156,6 +159,10 @@ class BaseViewer extends EventEmitter {
* @return {void}
*/
setup() {
if (this.isSetup) {
return;
}

if (this.options.file) {
const fileExt = this.options.file.extension;
this.fileLoadingIcon = getIconFromExtension(fileExt);
Expand Down Expand Up @@ -193,6 +200,8 @@ class BaseViewer extends EventEmitter {
this.annotatorPromiseResolver = resolve;
});
}

this.isSetup = true;
}

/**
Expand Down
5 changes: 4 additions & 1 deletion src/lib/viewers/box3d/Box3DViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ class Box3DViewer extends BaseViewer {
* @inheritdoc
*/
setup() {
if (this.isSetup) {
return;
}

// Call super() to set up common layout
super.setup();

Expand Down Expand Up @@ -179,7 +183,6 @@ class Box3DViewer extends BaseViewer {
* @return {Promise} to load assets and representation
*/
load() {
this.setup();
super.load();
return Promise.all([this.loadAssets(JS), this.getRepStatus().getPromise()])
.then(this.postLoad)
Expand Down
1 change: 0 additions & 1 deletion src/lib/viewers/box3d/__tests__/Box3DViewer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,6 @@ describe('lib/viewers/box3d/Box3DViewer', () => {
});

it('should call renderer.load()', () => {
Object.defineProperty(BaseViewer.prototype, 'setup', { value: sandbox.mock() });
box3d.containerEl = document.querySelector(SELECTOR_BOX_PREVIEW_CONTENT);
Object.defineProperty(BaseViewer.prototype, 'load', { value: sandbox.mock() });
sandbox.stub(box3d, 'loadAssets').returns(Promise.resolve());
Expand Down
4 changes: 4 additions & 0 deletions src/lib/viewers/box3d/image360/Image360Viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ class Image360Viewer extends Box3DViewer {
* @inheritdoc
*/
setup() {
if (this.isSetup) {
return;
}

// Call super() to set up common layout
super.setup();

Expand Down
4 changes: 4 additions & 0 deletions src/lib/viewers/box3d/model3d/Model3DViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ class Model3DViewer extends Box3DViewer {
* @inheritdoc
*/
setup() {
if (this.isSetup) {
return;
}

// Call super() first to set up common layout
super.setup();

Expand Down
4 changes: 4 additions & 0 deletions src/lib/viewers/box3d/video360/Video360Viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ class Video360Viewer extends DashViewer {

/** @inheritdoc */
setup() {
if (this.isSetup) {
return;
}

// Call super() to set up common layout
super.setup();

Expand Down
7 changes: 6 additions & 1 deletion src/lib/viewers/doc/DocBaseViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ class DocBaseViewer extends BaseViewer {
* @inheritdoc
*/
setup() {
if (this.isSetup) {
return;
}

// Call super() to set up common layout
super.setup();

Expand Down Expand Up @@ -188,6 +192,8 @@ class DocBaseViewer extends BaseViewer {

if (this.thumbnailsSidebar) {
this.thumbnailsSidebar.destroy();
this.thumbnailsSidebarEl.remove();
this.thumbnailsSidebarEl = null;
}

super.destroy();
Expand Down Expand Up @@ -305,7 +311,6 @@ class DocBaseViewer extends BaseViewer {
* @return {Promise} Promise to resolve assets
*/
load() {
this.setup();
super.load();
this.showPreload();

Expand Down
4 changes: 4 additions & 0 deletions src/lib/viewers/doc/DocumentViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ class DocumentViewer extends DocBaseViewer {
* @inheritdoc
*/
setup() {
if (this.isSetup) {
return;
}

// Call super() to set up common layout
super.setup();
this.docEl.classList.add('bp-doc-document');
Expand Down
4 changes: 4 additions & 0 deletions src/lib/viewers/doc/PresentationViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ class PresentationViewer extends DocBaseViewer {
* @inheritdoc
*/
setup() {
if (this.isSetup) {
return;
}

// Call super() to set up common layout
super.setup();
this.docEl.classList.add('bp-doc-presentation');
Expand Down
2 changes: 1 addition & 1 deletion src/lib/viewers/doc/__tests__/DocBaseViewer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ describe('src/lib/viewers/doc/DocBaseViewer', () => {

return docBase.load().then(() => {
expect(docBase.loadAssets).to.be.called;
expect(docBase.setup).to.be.called;
expect(docBase.setup).not.to.be.called;
expect(docBase.createContentUrlWithAuthParams).to.be.calledWith('foo');
expect(docBase.handleAssetAndRepLoad).to.be.called;
});
Expand Down
6 changes: 4 additions & 2 deletions src/lib/viewers/error/PreviewErrorViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ class PreviewErrorViewer extends BaseViewer {
* @inheritdoc
*/
setup() {
if (this.isSetup) {
return;
}

// Call super() first to set up common layout
super.setup();

Expand Down Expand Up @@ -65,8 +69,6 @@ class PreviewErrorViewer extends BaseViewer {
* @return {void}
*/
load(err) {
this.setup();

const error =
err instanceof PreviewError
? err
Expand Down
43 changes: 16 additions & 27 deletions src/lib/viewers/error/__tests__/PreviewErrorViewer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ describe('lib/viewers/error/PreviewErrorViewer', () => {
});
Object.defineProperty(BaseViewer.prototype, 'setup', { value: sandbox.mock() });
error.containerEl = containerEl;
error.setup();
});

afterEach(() => {
Expand All @@ -45,7 +46,6 @@ describe('lib/viewers/error/PreviewErrorViewer', () => {

describe('setup()', () => {
it('should set appropriate properties', () => {
error.setup();
expect(error.infoEl.classList.contains('bp-error')).to.be.true;
expect(error.iconEl instanceof HTMLElement).to.be.true;
expect(error.iconEl.parentNode).to.equal(error.infoEl);
Expand All @@ -55,12 +55,7 @@ describe('lib/viewers/error/PreviewErrorViewer', () => {
});

describe('load()', () => {
[
['zip', true],
['tgz', true],
['flv', true],
['blah', false]
].forEach((testCase) => {
[['zip', true], ['tgz', true], ['flv', true], ['blah', false]].forEach((testCase) => {
it('should set appropriate icon', () => {
const getIconFromExtensionStub = sandbox.stub(icons, 'getIconFromExtension');
const getIconFromNameStub = sandbox.stub(icons, 'getIconFromName');
Expand Down Expand Up @@ -132,11 +127,9 @@ describe('lib/viewers/error/PreviewErrorViewer', () => {

error.load(err);

expect(error.emit).to.be.calledWith(
VIEWER_EVENT.load, {
error: 'this is bad'
}
);
expect(error.emit).to.be.calledWith(VIEWER_EVENT.load, {
error: 'this is bad'
});
});

it('should broadcast the display message if there is no error message', () => {
Expand All @@ -145,32 +138,31 @@ describe('lib/viewers/error/PreviewErrorViewer', () => {

error.load(err);

expect(error.emit).to.be.calledWith(
VIEWER_EVENT.load, {
error: 'display message!'
}
);
expect(error.emit).to.be.calledWith(VIEWER_EVENT.load, {
error: 'display message!'
});
});

it('should filter out access tokens before broadcasting', () => {
sandbox.stub(error, 'emit');
const err = new PreviewError('some_code', 'display', {},
const err = new PreviewError(
'some_code',
'display',
{},
'Unexpected server response (0) while retrieving PDF "www.box.com?access_token=blah&test=okay"'
);

error.load(err);

expect(error.emit).to.be.calledWith(
VIEWER_EVENT.load, {
error: 'Unexpected server response (0) while retrieving PDF "www.box.com?access_token=[FILTERED]&test=okay"'
}
);
expect(error.emit).to.be.calledWith(VIEWER_EVENT.load, {
error:
'Unexpected server response (0) while retrieving PDF "www.box.com?access_token=[FILTERED]&test=okay"'
});
});
});

describe('addLinkButton()', () => {
it('should add a link button with the appropriate message and URL', () => {
error.setup();
error.addLinkButton('test', 'someUrl');
const linkBtnEl = error.infoEl.querySelector('a');

Expand All @@ -183,7 +175,6 @@ describe('lib/viewers/error/PreviewErrorViewer', () => {

describe('addDownloadButton()', () => {
it('should add a download button and attach a download click handler', () => {
error.setup();
sandbox.stub(error, 'download');

error.addDownloadButton();
Expand All @@ -201,7 +192,6 @@ describe('lib/viewers/error/PreviewErrorViewer', () => {

describe('download()', () => {
it('should emit download', () => {
error.setup();
sandbox.stub(error, 'emit');
error.download();
expect(error.emit).to.be.calledWith(VIEWER_EVENT.download);
Expand All @@ -210,7 +200,6 @@ describe('lib/viewers/error/PreviewErrorViewer', () => {

describe('destroy()', () => {
it('should remove download button click handler', () => {
error.setup();
sandbox.stub(error, 'download');

error.addDownloadButton();
Expand Down
6 changes: 4 additions & 2 deletions src/lib/viewers/iframe/IFrameViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ class IFrameViewer extends BaseViewer {
* @inheritdoc
*/
setup() {
if (this.isSetup) {
return;
}

// Call super() to set up common layout
super.setup();

Expand All @@ -23,8 +27,6 @@ class IFrameViewer extends BaseViewer {
* @return {void}
*/
load() {
this.setup();

let src = '';
const { file, sharedLink = '', appHost } = this.options;
const { extension } = file;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/viewers/iframe/__tests__/IFrameViewer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ describe('lib/viewers/iframe/IFrameViewer', () => {

Object.defineProperty(BaseViewer.prototype, 'setup', { value: sandbox.mock() });
iframe.containerEl = containerEl;
iframe.setup();
});

afterEach(() => {
Expand All @@ -41,7 +42,6 @@ describe('lib/viewers/iframe/IFrameViewer', () => {

describe('setup()', () => {
it('should setup iframe element and load timeout', () => {
iframe.setup();
expect(iframe.iframeEl).to.be.instanceof(HTMLElement);
expect(iframe.iframeEl).to.have.attribute('width', '100%');
expect(iframe.iframeEl).to.have.attribute('height', '100%');
Expand Down
Loading

0 comments on commit 8c2b193

Please sign in to comment.