Select Git revision
WebAnnotation.js 2.19 KiB
/** */
export default class WebAnnotation {
/** */
constructor({
canvasId, id, fragsel, image, body, tags, svg, manifestId,
}) {
this.id = id;
this.canvasId = canvasId;
this.fragsel = fragsel;
this.body = body;
this.tags = tags;
this.svg = svg;
this.image = image;
this.manifestId = manifestId;
}
/** */
toJson() {
return {
body: this.createBody(),
id: this.id,
motivation: 'commenting',
target: this.target(),
type: 'Annotation',
};
}
/** */
createBody() {
let bodies = [];
if (this.body && this.body.value !== '') {
const textBody = {
type: 'TextualBody',
value: this.body.value,
};
bodies.push(textBody);
}
// TODO correct WebAnnotation format
if (this.image) {
const imgBody = { type: 'Image' };
Object.assign(imgBody, this.image);
imgBody.id = imgBody.url;
bodies.push(imgBody);
}
if (this.tags) {
bodies = bodies.concat(this.tags.map((tag) => ({
purpose: 'tagging',
type: 'TextualBody',
value: tag,
})));
}
if (bodies.length === 1) {
return bodies[0];
}
return bodies;
}
/** Fill target object with selectors (if any), else returns target url */
target() {
if (!this.svg
&& (!this.fragsel || !Object.values(this.fragsel).find((e) => e !== null))) {
return this.canvasId;
}
const target = { source: this.source() };
const selectors = [];
if (this.svg) {
selectors.push({
type: 'SvgSelector',
value: this.svg,
});
}
if (this.fragsel) {
selectors.push({
type: 'FragmentSelector',
value: Object.entries(this.fragsel)
.filter((kv) => kv[1])
.map((kv) => `${kv[0]}=${kv[1]}`)
.join('&'),
});
}
target.selector = selectors.length === 1 ? selectors[0] : selectors;
return target;
}
/** */
source() {
let source = this.canvasId;
if (this.manifest) {
source = {
id: this.canvasId,
partOf: {
id: this.manifest.id,
type: 'Manifest',
},
type: 'Canvas',
};
}
return source;
}
}