Skip to content

Commit

Permalink
New: Allow BoxAnnotations to be instantiated independently and passed…
Browse files Browse the repository at this point in the history
… into Preview as an option (#68)

* New: Allow annoatations to be passed into Preview/Viewers
* Fix: setting of supported annotation types from options
  • Loading branch information
pramodsum authored Dec 11, 2017
1 parent 36606cc commit 8c1637a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
29 changes: 19 additions & 10 deletions src/BoxAnnotations.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,12 @@ class BoxAnnotations {
/**
* [constructor]
*
* @param {Object} options - Viewer-specific annotator options
* @return {BoxAnnotations} BoxAnnotations instance
*/
constructor() {
constructor(options = {}) {
this.annotators = ANNOTATORS;
this.options = options;
}

/**
Expand All @@ -67,8 +69,8 @@ class BoxAnnotations {
/**
* Get all annotators for a given viewer.
*
* @param {string} viewerName - Name of the viewer to get annotators for
* @param {Array} [disabledAnnotators] - List of disabled annotators
* @param {string} viewerName Name of the viewer to get annotators for
* @param {Array} [disabledAnnotators] List of disabled annotators
* @return {Object} Annotator for the viewer
*/
getAnnotatorsForViewer(viewerName, disabledAnnotators = []) {
Expand All @@ -86,7 +88,7 @@ class BoxAnnotations {
* has already been instantiated or the config is invalid.
*
* @private
* @param {Object} annotatorConfig - The config where annotation type controller instances should be attached
* @param {Object} annotatorConfig The config where annotation type controller instances should be attached
* @return {void}
*/
instantiateControllers(annotatorConfig) {
Expand All @@ -106,15 +108,22 @@ class BoxAnnotations {
}

/**
* Determines the supported annotation types based on the viewer configurations
* Determines the supported annotation types based on the viewer configurations or passed in options
* if provided, otherwise using the viewer defaults
*
* @private
* @param {Object} annotatorConfig - The config where annotation type controller instances should be attached
* @param {Object} annotatorConfig The config where annotation type controller instances should be attached
* @return {void}
*/
getAnnotatorTypes(annotatorConfig) {
if (!this.viewerConfig) {
if (this.options && this.options[annotatorConfig.NAME]) {
// Sets supported annotation types based on passed in options
const annotatorOptions = this.options[annotatorConfig.NAME];
if (annotatorOptions.enabledTypes) {
return annotatorOptions.enabledTypes;
}
} else if (!this.viewerConfig) {
// Sets supported annotation types to viewer-specific defaults
return [...annotatorConfig.DEFAULT_TYPES];
}

Expand All @@ -134,9 +143,9 @@ class BoxAnnotations {
/**
* Chooses an annotator based on viewer.
*
* @param {Object} options - Viewer options
* @param {Object} [viewerConfig] - Viewer-specific annotations configs
* @param {Array} [disabledAnnotators] - List of disabled annotators
* @param {Object} options Viewer options
* @param {Object} [viewerConfig] Viewer-specific annotations configs
* @param {Array} [disabledAnnotators] List of disabled annotators
* @return {Object|null} A copy of the annotator to use, if available
*/
determineAnnotator(options, viewerConfig = {}, disabledAnnotators = []) {
Expand Down
7 changes: 7 additions & 0 deletions src/__tests__/BoxAnnotations-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@ describe('BoxAnnotations', () => {
};
});

it('should use the specified types from options', () => {
loader.options = {
'Document': { enabledTypes: ['draw'] }
}
expect(loader.getAnnotatorTypes(stubs.config)).to.deep.equal(['draw']);
});

it('should filter disabled annotation types from the annotator.TYPE', () => {
loader.viewerConfig = { disabledTypes: ['point'] };
expect(loader.getAnnotatorTypes(stubs.config)).to.deep.equal(['highlight']);
Expand Down

0 comments on commit 8c1637a

Please sign in to comment.