Skip to content

Commit

Permalink
feat(annotations): add support for passed annotations options
Browse files Browse the repository at this point in the history
* Add support for annotation options that will populate intl object that gets passed to annotator if present.
  • Loading branch information
mickr committed May 8, 2020
1 parent 4a97878 commit cf3649b
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 6 deletions.
19 changes: 15 additions & 4 deletions src/lib/viewers/BaseViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -931,17 +931,29 @@ class BaseViewer extends EventEmitter {
return;
}

const boxAnnotations = this.options.boxAnnotations || new global.BoxAnnotations(viewerOptions);
const boxAnnotations =
this.options.boxAnnotations || new global.BoxAnnotations(viewerOptions, intl.createAnnotatorIntl());
this.annotatorConf = boxAnnotations.determineAnnotator(this.options, this.viewerConfig);

if (!this.annotatorConf) {
return;
}

const annotatorOptions = this.createAnnotatorOptions({
const options = {
annotator: this.annotatorConf,
modeButtons: ANNOTATION_BUTTONS,
});
};

if (this.areNewAnnotationsEnabled() && boxAnnotations.getAnnotationsOptions) {
const annotationOptions = boxAnnotations.getAnnotationsOptions();

if (annotationOptions) {
const { locale, messages } = annotationOptions;
options.intl = { locale, messages };
}
}

const annotatorOptions = this.createAnnotatorOptions(options);
this.annotator = new this.annotatorConf.CONSTRUCTOR(annotatorOptions);

if (this.annotatorPromiseResolver) {
Expand Down Expand Up @@ -1177,7 +1189,6 @@ class BaseViewer extends EventEmitter {
...this.options,
...moreOptions,
hasTouch: this.hasTouch,
intl: intl.createAnnotatorIntl(),
isMobile: this.isMobile,
locale: this.options.location.locale,
localizedStrings,
Expand Down
53 changes: 51 additions & 2 deletions src/lib/viewers/__tests__/BaseViewer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1058,6 +1058,10 @@ describe('lib/viewers/BaseViewer', () => {

describe('createAnnotator()', () => {
const annotatorMock = {};
const annotationsOptions = {
messages: { test: 'Test Message' },
locale: 'en-US',
};
const conf = {
annotationsEnabled: true,
types: {
Expand All @@ -1073,6 +1077,7 @@ describe('lib/viewers/BaseViewer', () => {
base.options.showAnnotations = true;
window.BoxAnnotations = function BoxAnnotations() {
this.determineAnnotator = sandbox.stub().returns(conf);
this.getAnnotationsOptions = sandbox.stub().returns(annotationsOptions);
};

sandbox.stub(base, 'initAnnotations');
Expand Down Expand Up @@ -1102,6 +1107,52 @@ describe('lib/viewers/BaseViewer', () => {
base.createAnnotator();
expect(base.options.boxAnnotations.determineAnnotator).to.be.called;
});

it('should call getAnnotationsOptions if new annotations enabled', () => {
sandbox.stub(base, 'areAnnotationsEnabled').returns(true);
sandbox.stub(base, 'areNewAnnotationsEnabled').returns(true);

base.options.boxAnnotations = {
determineAnnotator: sandbox.stub().returns(conf),
getAnnotationsOptions: sandbox.stub().returns(annotationsOptions),
};

base.createAnnotator();

expect(base.options.boxAnnotations.getAnnotationsOptions).to.be.called;
});

it('should call createAnnotatorOptions with language and messages from options', () => {
sandbox.stub(base, 'areAnnotationsEnabled').returns(true);
sandbox.stub(base, 'areNewAnnotationsEnabled').returns(true);
sandbox.stub(base, 'createAnnotatorOptions');

base.options.boxAnnotations = {
determineAnnotator: sandbox.stub().returns(conf),
getAnnotationsOptions: sandbox.stub().returns(annotationsOptions),
};

base.createAnnotator();

expect(base.options.boxAnnotations.getAnnotationsOptions).to.be.called;
expect(base.createAnnotatorOptions).to.be.calledWith(sinon.match({ intl: annotationsOptions }));
});

it('should not have intl in annotator options if language and messages are not present ', () => {
sandbox.stub(base, 'areAnnotationsEnabled').returns(true);
sandbox.stub(base, 'areNewAnnotationsEnabled').returns(true);
sandbox.stub(base, 'createAnnotatorOptions');

base.options.boxAnnotations = {
determineAnnotator: sandbox.stub().returns(conf),
getAnnotationsOptions: sandbox.stub().returns(undefined),
};

base.createAnnotator();

expect(base.options.boxAnnotations.getAnnotationsOptions).to.be.called;
expect(base.createAnnotatorOptions).not.to.be.calledWith(sinon.match.has('intl'));
});
});

describe('initAnnotations()', () => {
Expand Down Expand Up @@ -1448,8 +1499,6 @@ describe('lib/viewers/BaseViewer', () => {
expect(combinedOptions.location).to.deep.equal({ locale: 'en-US' });
expect(combinedOptions.randomOption).to.equal('derp');
expect(combinedOptions.localizedStrings).to.not.be.undefined;
expect(combinedOptions.intl.language).to.equal('en-US');
expect(combinedOptions.intl.messages).to.be.an('object');
});
});

Expand Down

0 comments on commit cf3649b

Please sign in to comment.