greek-manifests/src/Canvas.js

74 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 = {};
/**
*
* @param {string} label
*/
constructor(IIIFApiVersion, baseURL) {
this.context = `http://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}/iiif/${resourceId}/canvas/${name}`;
this.resourceId = resourceId;
this.name = name;
}
/**
* @param {string} label A label for this canvas
*/
setLabel(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
*/
toObject() {
return {
"@context" : this.context,
"@id" : this.id,
"@type" : this.type,
"label" : this.label,
"images" : this.images,
"thumbnail" : this.thumbnail
}
}
}
export default Canvas;