Skip to content
Snippets Groups Projects
Commit c3c1c1d2 authored by Jack Reed's avatar Jack Reed
Browse files

Create an AnnotationFactory and AnnotationPage models to support WebAnnotation in Mirador 3

parent ce7c5558
No related branches found
No related tags found
No related merge requests found
import AnnotationFactory from '../../../src/lib/AnnotationFactory';
import AnnotationList from '../../../src/lib/AnnotationList';
import AnnotationPage from '../../../src/lib/AnnotationPage';
describe('AnnotationFactory', () => {
describe('determineAnnotation', () => {
describe('when falsey', () => {
it('returns null', () => {
expect(AnnotationFactory.determineAnnotation(undefined)).toBeNull();
});
});
describe('when Presentation v3', () => {
it('returns an AnnotationPage', () => {
expect(AnnotationFactory.determineAnnotation({
type: 'AnnotationPage',
})).toBeInstanceOf(AnnotationPage);
});
});
describe('when Presentation v2', () => {
it('returns an AnnotationPage', () => {
expect(AnnotationFactory.determineAnnotation({
'@type': 'AnnotationList',
})).toBeInstanceOf(AnnotationList);
});
});
});
});
import AnnotationPage from '../../../src/lib/AnnotationPage';
describe('AnnotationPage', () => {
describe('id', () => {
it('returns the id', () => {
expect(new AnnotationPage({ id: 'foo' }).id).toEqual('foo');
});
});
describe('present', () => {
it('checks for json', () => {
expect(new AnnotationPage().present()).toEqual(false);
});
it('checks for items', () => {
expect(new AnnotationPage({ id: 'foo' }).present()).toEqual(false);
expect(new AnnotationPage({ items: [] }).present()).toEqual(false);
});
});
describe('items', () => {
it('returns items', () => {
expect(new AnnotationPage(
{ items: [{ foo: 'bar' }] },
).items).toEqual([{ foo: 'bar' }]);
});
});
describe('resources', () => {
it('returns items', () => {
expect(new AnnotationPage(
{ items: [{ foo: 'bar' }] },
).resources).toEqual([{ foo: 'bar' }]);
});
});
});
import AnnotationList from './AnnotationList';
import AnnotationPage from './AnnotationPage';
/**
* Used to determine the type of Annotation supported by a version of the IIIF
* Presentation API.
*/
export default class AnnotationFactory {
/** */
static determineAnnotation(json, target) {
if (!json) {
return null;
}
// IIIF Presentation API v3. AnnotationPage
if (json.type === 'AnnotationPage') {
return new AnnotationPage(json, target);
}
// IIIF Presentation API v2. OpenAnnotation and SharedCanvas models
return new AnnotationList(json, target);
}
}
/**
* Annotation representation for IIIF Presentation v3
* https://iiif.io/api/presentation/3.0/#55-annotation-page
*/
export default class AnnotationPage {
/** */
constructor(json, target) {
this.json = json;
this.target = target;
}
/** */
get id() {
return this.json.id;
}
/** */
present() {
return (this.items
&& this.items.length > 0);
}
/** */
get items() {
if (!this.json || !this.json.items) return [];
return this.json.items;
}
/**
* Alias to items for compatibility for right now.
*/
get resources() {
return this.items;
}
}
import { createSelector } from 'reselect';
import filter from 'lodash/filter';
import flatten from 'lodash/flatten';
import AnnotationList from '../../lib/AnnotationList';
import AnnotationFactory from '../../lib/AnnotationFactory';
import { getCanvas, getVisibleCanvases } from './canvases';
const getAnnotationsOnCanvas = createSelector(
......@@ -22,7 +22,8 @@ const getPresentAnnotationsCanvas = createSelector(
getAnnotationsOnCanvas,
],
annotations => filter(
Object.values(annotations).map(annotation => annotation && new AnnotationList(annotation.json)),
Object.values(annotations)
.map(annotation => annotation && AnnotationFactory.determineAnnotation(annotation.json)),
annotation => annotation && annotation.present(),
),
);
......@@ -48,7 +49,8 @@ const getPresentAnnotationsOnSelectedCanvases = createSelector(
getAnnotationsOnSelectedCanvases,
],
annotations => filter(
Object.values(annotations).map(annotation => annotation && new AnnotationList(annotation.json)),
Object.values(annotations)
.map(annotation => annotation && AnnotationFactory.determineAnnotation(annotation.json)),
annotation => annotation && annotation.present(),
),
);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment