72 lines
1.8 KiB
JavaScript
72 lines
1.8 KiB
JavaScript
import IIIFResource from './IIIFResource.js';
|
|
import Image from './Image.js';
|
|
/**
|
|
* @implements IIIFResource
|
|
*/
|
|
class Canvas {
|
|
id = '';
|
|
#type = 'sc:Canvas';
|
|
#label = '';
|
|
resourceId = '';
|
|
name = '';
|
|
images = [];
|
|
thumbnail = {};
|
|
|
|
constructor(IIIFApiVersion, baseURL) {
|
|
this.context = `https://iiif.io/api/presentation/${IIIFApiVersion}/context.json`;
|
|
this.BASE_URL = baseURL;
|
|
}
|
|
/**
|
|
* @param {string} resourceId The resource ID for this canvas
|
|
* @param {int|string} name A unique name for this canvas
|
|
*/
|
|
generateID(resourceId, name) {
|
|
this.id = `${this.BASE_URL}/${resourceId}/canvas/${name}`;
|
|
this.resourceId = resourceId;
|
|
this.name = name;
|
|
}
|
|
/**
|
|
* @param {string} label A label for this canvas
|
|
*/
|
|
set label(label) {
|
|
this.#label = label;
|
|
}
|
|
/**
|
|
* @param {Image} image Add an image to the canvas
|
|
*/
|
|
addImage(image) {
|
|
this.images.push(image.toObject());
|
|
}
|
|
/**
|
|
* Add a thumbnail object
|
|
* @todo Support multiple thumbs?
|
|
* @param {number} height
|
|
* @param {number} width
|
|
* @param {string} imageId The full image ID as server URL
|
|
*/
|
|
setThumbnail(height, width, imageId) {
|
|
this.thumbnail = {
|
|
"@id" : `${imageId.replace(/\/full.*$/,'')}/full/${width},${height}/0/default.jpg`,
|
|
"@type" : 'dctypes:Image',
|
|
height,
|
|
width
|
|
}
|
|
}
|
|
/**
|
|
* Object representation
|
|
* @returns {object}
|
|
*/
|
|
toObject() {
|
|
return {
|
|
"@context" : this.context,
|
|
"@id" : this.id,
|
|
"@type" : this.#type,
|
|
"label" : this.#label,
|
|
"images" : this.images,
|
|
"thumbnail" : this.thumbnail
|
|
}
|
|
}
|
|
}
|
|
|
|
export default Canvas;
|