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

Add some basic API docs to `DocumentMeta` class

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