From e3281cfc1c4509d789e97fc4c2e9c800e77ee555 Mon Sep 17 00:00:00 2001 From: Sumedha Pramod Date: Mon, 11 Dec 2017 14:41:15 -0800 Subject: [PATCH] New: Allow BoxAnnotations to be passed in as a Preview option (#539) --- src/lib/Preview.js | 3 ++ src/lib/viewers/BaseViewer.js | 27 ++++++++++++--- src/lib/viewers/__tests__/BaseViewer-test.js | 35 +++++++++++++++++++- 3 files changed, 59 insertions(+), 6 deletions(-) diff --git a/src/lib/Preview.js b/src/lib/Preview.js index 088e606cd..1116ede0d 100644 --- a/src/lib/Preview.js +++ b/src/lib/Preview.js @@ -690,6 +690,9 @@ class Preview extends EventEmitter { // Custom Box3D application definition this.options.box3dApplication = options.box3dApplication; + // Custom BoxAnnotations definition + this.options.boxAnnotations = options.boxAnnotations; + // Save the reference to any additional custom options for viewers this.options.viewers = options.viewers || {}; diff --git a/src/lib/viewers/BaseViewer.js b/src/lib/viewers/BaseViewer.js index f98a0e124..35a2bac83 100644 --- a/src/lib/viewers/BaseViewer.js +++ b/src/lib/viewers/BaseViewer.js @@ -694,18 +694,27 @@ class BaseViewer extends EventEmitter { return; } - this.annotationsLoadPromise = this.loadAssets([ANNOTATIONS_JS], [ANNOTATIONS_CSS]); + // Auto-resolves promise if BoxAnnotations is passed in as a Preview option + this.annotationsLoadPromise = + this.options.boxAnnotations instanceof BoxAnnotations + ? Promise.resolve() + : this.loadAssets([ANNOTATIONS_JS], [ANNOTATIONS_CSS]); } /** - * Fetches the Box Annotations library + * Fetches the Box Annotations library. Creates an instance of BoxAnnotations + * if one isn't passed in to the preview options * * @protected * @return {void} */ annotationsLoadHandler() { + // Set viewer-specific annotation options + const viewerOptions = {}; + viewerOptions[this.options.viewer.NAME] = this.viewerConfig; + /* global BoxAnnotations */ - const boxAnnotations = new BoxAnnotations(); + const boxAnnotations = this.options.boxAnnotations || new BoxAnnotations(viewerOptions); this.annotatorConf = boxAnnotations.determineAnnotator(this.options, this.viewerConfig); if (this.annotatorConf) { @@ -755,13 +764,21 @@ class BaseViewer extends EventEmitter { */ areAnnotationsEnabled() { // Respect viewer-specific annotation option if it is set - // #TODO(@spramod|@jholdstock): remove this after we have annotation instancing - this.viewerConfig = this.getViewerAnnotationsConfig(); + if (this.options.boxAnnotations instanceof BoxAnnotations) { + const { boxAnnotations, viewer } = this.options; + const annotatorConfig = boxAnnotations.options[viewer.NAME]; + this.viewerConfig = { + enabled: annotatorConfig.enabled || !!annotatorConfig.enabledTypes + }; + } else { + this.viewerConfig = this.getViewerAnnotationsConfig(); + } if (this.viewerConfig && this.viewerConfig.enabled !== undefined) { return this.viewerConfig.enabled; } + // Ignore viewer config if BoxAnnotations was pass into Preview as an option // Otherwise, use global preview annotation option return !!this.options.showAnnotations; } diff --git a/src/lib/viewers/__tests__/BaseViewer-test.js b/src/lib/viewers/__tests__/BaseViewer-test.js index 934c1042f..847ff7593 100644 --- a/src/lib/viewers/__tests__/BaseViewer-test.js +++ b/src/lib/viewers/__tests__/BaseViewer-test.js @@ -1,5 +1,6 @@ /* eslint-disable no-unused-expressions */ import EventEmitter from 'events'; +import BoxAnnotations from 'box-annotations'; import BaseViewer from '../BaseViewer'; import Browser from '../../Browser'; import RepStatus from '../../RepStatus'; @@ -775,10 +776,19 @@ describe('lib/viewers/BaseViewer', () => { }); + it('should resolve the promise if a BoxAnnotations instance was passed into Preview', (done) => { + base.areAnnotationsEnabled.returns(true); + base.options.boxAnnotations = new BoxAnnotations({}); + + base.loadAnnotator(); + expect(base.loadAssets).to.not.be.calledWith(['annotations.js']); + base.annotationsLoadPromise.then(() => done()); + }); + it('should load the annotations assets', () => { base.areAnnotationsEnabled.returns(true); base.loadAnnotator(); - expect(base.loadAssets).to.be.calledWith(['annotations.js']); + expect(base.loadAssets).to.be.calledWith(['annotations.js'], ['annotations.css']); }); }); @@ -792,6 +802,7 @@ describe('lib/viewers/BaseViewer', () => { }; beforeEach(() => { + base.options.viewer = { NAME: 'viewerName' }; window.BoxAnnotations = function BoxAnnotations() { this.determineAnnotator = sandbox.stub().returns(conf); } @@ -804,6 +815,14 @@ describe('lib/viewers/BaseViewer', () => { expect(base.annotatorConf).to.equal(conf); }); + it('should not instantiate an instance of BoxAnnotations if one is already passed in', () => { + base.options.boxAnnotations = { + determineAnnotator: sandbox.stub() + }; + base.annotationsLoadHandler(); + expect(base.options.boxAnnotations.determineAnnotator).to.be.called; + }); + it('should init annotations if a conf is present', () => { base.annotationsLoadHandler(); expect(base.initAnnotations).to.be.called; @@ -875,6 +894,20 @@ describe('lib/viewers/BaseViewer', () => { base.options.showAnnotations = false; expect(base.areAnnotationsEnabled()).to.equal(false); }); + + it('should user BoxAnnotations options if an instance of BoxAnnotations is passed into Preview', () => { + stubs.getViewerOption.withArgs('annotations').returns(null); + base.options.showAnnotations = false; + base.options.boxAnnotations = undefined; + expect(base.areAnnotationsEnabled()).to.equal(false); + + base.options.viewer = { NAME: 'viewerName' }; + base.options.boxAnnotations = sinon.createStubInstance(window.BoxAnnotations); + base.options.boxAnnotations.options = { + 'viewerName': { enabled: true } + } + expect(base.areAnnotationsEnabled()).to.equal(true); + }); }); describe('getViewerAnnotationsConfig()', () => {