Commit 4e2fba09 authored by Robert Knight's avatar Robert Knight

Use `subscribe` to listen to `beforeAnnotationCreated` events

This removes the last remaining usage of the `events` property of
`Delegator`, enabling that code to be removed.
parent 55269a89
......@@ -69,15 +69,26 @@ export default class DocumentMeta extends Plugin {
constructor(element, options) {
super(element, options);
this.events = {
beforeAnnotationCreated: 'beforeAnnotationCreated',
};
this.metadata = createMetadata();
this.baseURI = options.baseURI || baseURI;
this.document = options.document || document;
this.normalizeURI = options.normalizeURI || normalizeURI;
/**
* Event handler that adds document metadata to new annotations.
*/
this.beforeAnnotationCreated = annotation => {
annotation.document = this.metadata;
};
// @ts-expect-error - Method comes from CoffeeScript base class.
this.subscribe('beforeAnnotationCreated', this.beforeAnnotationCreated);
}
destroy() {
// @ts-expect-error - Method comes from CoffeeScript base class.
this.unsubscribe('beforeAnnotationCreated', this.beforeAnnotationCreated);
}
pluginInit() {
......@@ -114,14 +125,6 @@ export default class DocumentMeta extends Plugin {
return Object.keys(uniqueUrls);
}
/**
* Hook that augments new annotations with metadata about the document they
* came from.
*/
beforeAnnotationCreated(annotation) {
annotation.document = this.metadata;
}
/**
* Return metadata for the current page.
*/
......
......@@ -31,14 +31,27 @@ describe('DocumentMeta', function () {
return normalizeURI(url, base);
});
testDocument = new DocumentMeta(tempDocument, {
// Root element to use for the `Delegator` event bus. This can be different
// than the document from which metadata is gathered.
const rootElement = document.body;
testDocument = new DocumentMeta(rootElement, {
document: tempDocument,
normalizeURI: fakeNormalizeURI,
});
testDocument.pluginInit();
});
afterEach(() => $(document).unbind());
afterEach(() => {
testDocument.destroy();
$(document).unbind();
});
it('attaches document metadata to new annotations', () => {
const annotation = {};
testDocument.publish('beforeAnnotationCreated', [annotation]);
assert.equal(annotation.document, testDocument.metadata);
});
describe('annotation should have some metadata', function () {
let metadata = null;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment