Skip to content

Commit

Permalink
New: Added enabledTypes property. Backwards compat for disabledTypes (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinHoldstock authored Oct 23, 2017
1 parent 62a67f9 commit 15a7712
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 18 deletions.
32 changes: 24 additions & 8 deletions src/lib/annotations/BoxAnnotations.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,27 @@ import DrawingModeController from './drawing/DrawingModeController';
import { TYPES } from './annotationConstants';
import { canLoadAnnotations } from './annotatorUtil';

/**
* NAME: The name of the annotator.
* CONSTRUCTOR: Constructor for the annotator.
* VIEWER: The kinds of viewers that can be annotated by this.
* TYPE: The types of annotations that can be used by this annotator.
* DEFAULT_TYPES: The default annotation types enabled if none provided.
*/
const ANNOTATORS = [
{
NAME: 'Document',
CONSTRUCTOR: DocAnnotator,
VIEWER: ['Document', 'Presentation'],
TYPE: [TYPES.point, TYPES.highlight, TYPES.highlight_comment]
TYPE: [TYPES.point, TYPES.highlight, TYPES.highlight_comment, TYPES.draw],
DEFAULT_TYPES: [TYPES.point, TYPES.highlight, TYPES.highlight_comment]
},
{
NAME: 'Image',
CONSTRUCTOR: ImageAnnotator,
VIEWER: ['Image', 'MultiImage'],
TYPE: [TYPES.point]
TYPE: [TYPES.point],
DEFAULT_TYPES: [TYPES.point]
}
];

Expand Down Expand Up @@ -103,12 +112,19 @@ class BoxAnnotations {

modifiedAnnotator = Object.assign({}, annotator);

// Filter out disabled annotation types
if (Array.isArray(viewerConfig.disabledTypes)) {
modifiedAnnotator.TYPE = modifiedAnnotator.TYPE.filter((type) => {
return !viewerConfig.disabledTypes.includes(type);
});
}
const enabledTypes = viewerConfig.enabledTypes || [...modifiedAnnotator.DEFAULT_TYPES];

// Keeping disabledTypes for backwards compatibility
const disabledTypes = viewerConfig.disabledTypes || [];

const annotatorTypes = enabledTypes.filter((type) => {
return (
!disabledTypes.some((disabled) => disabled === type) &&
modifiedAnnotator.TYPE.some((allowed) => allowed === type)
);
});

modifiedAnnotator.TYPE = annotatorTypes;

return modifiedAnnotator;
}
Expand Down
6 changes: 3 additions & 3 deletions src/lib/annotations/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,15 @@ preview.show(..., {
VIEWER_NAME: {
annotations: {
enabled: Boolean, // Enables/disables if set. Respects "showAnnotations" if empty
disabledTypes: String[] // List of annotation types to disable
enabledTypes: String[] | null // List of annotation types to enable for this viewer. If empty, will respect default types for that annotator.
}
}
}
});
```

### Example
Enable all annotations, turn off for Image Viewers, and disable point annotations on Document viewer:
Enable all annotations, turn off for Image Viewers, and enable only point annotations on Document viewer:
```
preview.show(fileId, token, {
showAnnotations: true,
Expand All @@ -160,7 +160,7 @@ preview.show(fileId, token, {
Document: {
annotations: {
enabled: true,
disabledTypes: ['point']
enabledTypes: ['point']
}
}
}
Expand Down
71 changes: 64 additions & 7 deletions src/lib/annotations/__tests__/BoxAnnotations-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ describe('lib/annotators/BoxAnnotations', () => {
stubs.annotator = {
NAME: 'Document',
VIEWER: ['Document'],
TYPE: ['point']
TYPE: ['point'],
DEFAULT_TYPES: ['point']
};

stubs.options = {
Expand Down Expand Up @@ -103,11 +104,18 @@ describe('lib/annotators/BoxAnnotations', () => {

it('should return a copy of the annotator that matches', () => {
const viewer = 'Document';
sandbox.stub(loader, 'getAnnotatorsForViewer').returns(stubs.annotator);

const docAnnotator = {
NAME: viewer,
VIEWER: ['Document'],
TYPE: ['point', 'highlight'],
DEFAULT_TYPES: ['point']
};

sandbox.stub(loader, 'getAnnotatorsForViewer').returns(docAnnotator);
const annotator = loader.determineAnnotator(stubs.options);

stubs.annotator.NAME = 'another_name';
expect(annotator.NAME).to.equal(viewer);
expect(annotator.NAME).to.not.equal(stubs.annotator.NAME);
});

Expand All @@ -125,19 +133,68 @@ describe('lib/annotators/BoxAnnotations', () => {
enabled: true,
disabledTypes: ['point']
};

stubs.annotator.TYPE = ['point', 'highlight'];
sandbox.stub(loader, 'getAnnotatorsForViewer').returns(stubs.annotator);

const docAnnotator = {
NAME: 'Document',
VIEWER: ['Document'],
TYPE: ['point', 'highlight'],
DEFAULT_TYPES: ['point', 'highlight']
};
sandbox.stub(loader, 'getAnnotatorsForViewer').returns(docAnnotator);
const annotator = loader.determineAnnotator(stubs.options, config);

expect(annotator.TYPE.includes('point')).to.be.false;
expect(annotator.TYPE.includes('highlight')).to.be.true;
expect(annotator).to.deep.equal({
NAME: 'Document',
VIEWER: ['Document'],
TYPE: ['highlight']
TYPE: ['highlight'],
DEFAULT_TYPES: ['point', 'highlight']
});
expect(loader.getAnnotatorsForViewer).to.be.called;
});

it('should filter and only keep allowed types of annotations', () => {
const config = {
enabled: true,
enabledTypes: ['point', 'timestamp']
};

const docAnnotator = {
NAME: 'Document',
VIEWER: ['Document'],
TYPE: ['point', 'highlight', 'highlight-comment', 'draw'],
DEFAULT_TYPES: ['point', 'highlight']
};

sandbox.stub(loader, 'getAnnotatorsForViewer').returns(docAnnotator);
const annotator = loader.determineAnnotator(stubs.options, config);
expect(annotator.TYPE.includes('point')).to.be.true;
expect(annotator.TYPE.includes('highlight')).to.be.false;
expect(annotator).to.deep.equal({
NAME: 'Document',
VIEWER: ['Document'],
TYPE: ['point'],
DEFAULT_TYPES: ['point', 'highlight']
});
});

it('should respect default annotators if none provided', () => {
const config = {
enabled: true
};

const docAnnotator = {
NAME: 'Document',
VIEWER: ['Document'],
TYPE: ['point', 'highlight', 'highlight-comment', 'draw'],
DEFAULT_TYPES: ['point', 'draw']
};
sandbox.stub(loader, 'getAnnotatorsForViewer').returns(docAnnotator);
const annotator = loader.determineAnnotator(stubs.options, config);

expect(annotator.TYPE).to.deep.equal(['point', 'draw']);
});
});

describe('instantiateControllers()', () => {
Expand Down

0 comments on commit 15a7712

Please sign in to comment.