Commit 3dc2f206 authored by Robert Knight's avatar Robert Knight

Add some basic API docs to `DocumentMeta` class

parent f6779e35
'use strict';
const baseURI = require('document-base-uri');
const Plugin = require('../plugin');
const { normalizeURI } = require('../util/url');
/*
** Adapted from:
** https://github.com/openannotation/annotator/blob/v1.2.x/src/plugin/document.coffee
......@@ -17,6 +12,15 @@ const { normalizeURI } = require('../util/url');
** https://github.com/openannotation/annotator/blob/master/LICENSE
*/
const baseURI = require('document-base-uri');
const Plugin = require('../plugin');
const { normalizeURI } = require('../util/url');
/**
* DocumentMeta reads metadata/links from the current HTML document and
* populates the `document` property of new annotations.
*/
class DocumentMeta extends Plugin {
constructor(element, options) {
super(element, options);
......@@ -34,7 +38,11 @@ class DocumentMeta extends Plugin {
this.getDocumentMetadata();
}
// Returns the primary URI for the document being annotated
/**
* Returns the primary URI for the document being annotated
*
* @return {string}
*/
uri() {
let uri = decodeURIComponent(this._getDocumentHref());
for (let link of this.metadata.link) {
......@@ -45,7 +53,11 @@ class DocumentMeta extends Plugin {
return uri;
}
// Returns all uris for the document being annotated
/**
* Returns all uris for the document being annotated
*
* @return {string[]}
*/
uris() {
const uniqueUrls = {};
for (let link of this.metadata.link) {
......@@ -54,10 +66,17 @@ 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.
*/
getDocumentMetadata() {
this.metadata = {};
......
'use strict';
const $ = require('jquery');
const DocumentMeta = require('../document');
/*
** Adapted from:
** https://github.com/openannotation/annotator/blob/v1.2.x/test/spec/plugin/document_spec.coffee
......@@ -15,6 +12,10 @@ const DocumentMeta = require('../document');
** https://github.com/openannotation/annotator/blob/master/LICENSE
*/
const $ = require('jquery');
const DocumentMeta = require('../document');
describe('DocumentMeta', function() {
let testDocument = 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