Skip to content
Snippets Groups Projects
Commit 709dae6f authored by Mark A. Matney, Jr's avatar Mark A. Matney, Jr
Browse files

Fix failing tests

Also simplify and DRY up ThumbnailFactory helper methods.
parent 80e252b8
No related branches found
No related tags found
No related merge requests found
......@@ -66,6 +66,16 @@ class ThumbnailFactory {
return { height: resource.getProperty('height'), url: resource.id, width: resource.getProperty('width') };
}
/**
* Selects the image resource that is representative of the given canvas.
* @param {Object} canvas A Manifesto Canvas
* @return {Object} A Manifesto Image Resource
*/
static getPreferredImage(canvas) {
const miradorCanvas = new MiradorCanvas(canvas);
return miradorCanvas.iiifImageResources[0] || miradorCanvas.imageResource;
}
/**
* Chooses the best available image size based on a target area (w x h) value.
* @param {Object} service A IIIF Image API service that has a `sizes` array
......@@ -114,8 +124,9 @@ class ThumbnailFactory {
}
/**
* Creates a canonical image request for a thumb
* @param {Number} height
* Determines the appropriate thumbnail to use to represent an Image Resource.
* @param {Object} resource The Image Resource from which to derive a thumbnail
* @return {Object} The thumbnail URL and any spatial dimensions that can be determined
*/
iiifThumbnailUrl(resource) {
let size;
......@@ -131,27 +142,26 @@ class ThumbnailFactory {
const service = iiifImageService(resource);
if (!service) return undefined;
if (!service) return ThumbnailFactory.staticImageUrl(resource);
const aspectRatio = resource.getWidth()
&& resource.getHeight()
&& (resource.getWidth() / resource.getHeight());
// just bail to a static image, even though sizes might provide something better
if (isLevel0ImageProfile(service)) {
const target = (requestedMaxWidth && requestedMaxHeight)
? requestedMaxWidth * requestedMaxHeight
: maxHeight * maxWidth;
const closestSize = ThumbnailFactory.selectBestImageSize(service, target);
/** Bail if the best available size is the full size.. maybe we'll get lucky with the @id */
if (!closestSize && !service.getProperty('height') && !service.getProperty('width')) {
return ThumbnailFactory.staticImageUrl(resource);
}
if (closestSize) {
// Embedded service advertises an appropriate size
width = closestSize.width;
height = closestSize.height;
size = `${width},${height}`;
} else if (isLevel0ImageProfile(service)) {
/** Bail if the best available size is the full size.. maybe we'll get lucky with the @id */
if (!service.getProperty('height') && !service.getProperty('width')) {
return ThumbnailFactory.staticImageUrl(resource);
}
} else if (requestedMaxHeight && requestedMaxWidth) {
// IIIF level 2, no problem.
if (isLevel2ImageProfile(service)) {
......@@ -195,77 +205,76 @@ class ThumbnailFactory {
};
}
/** */
getThumbnail(resource, { requireIiif, quirksMode }) {
if (!resource) return undefined;
const thumb = resource.getThumbnail();
if (thumb && iiifImageService(thumb)) return this.iiifThumbnailUrl(thumb);
if (requireIiif) return undefined;
if (thumb && typeof thumb.__jsonld !== 'string') return ThumbnailFactory.staticImageUrl(thumb);
if (!quirksMode) return undefined;
return (thumb && typeof thumb.__jsonld === 'string') ? { url: thumb.__jsonld } : undefined;
/**
* Determines the content resource from which to derive a thumbnail to represent a given resource.
* This method is recursive.
* @param {Object} resource A IIIF resource to derive a thumbnail from
* @return {Object|undefined} The Image Resource to derive a thumbnail from, or undefined
* if no appropriate resource exists
*/
getSourceContentResource(resource) {
const thumbnail = resource.getThumbnail();
// Any resource type may have a thumbnail
if (thumbnail) {
if (typeof thumbnail.__jsonld === 'string') return thumbnail.__jsonld;
// Prefer an image's ImageService over its image's thumbnail
// Note that Collection, Manifest, and Canvas don't have `getType()`
if (!resource.isCollection() && !resource.isManifest() && !resource.isCanvas()) {
if (resource.getType() === 'image' && iiifImageService(resource) && !iiifImageService(thumbnail)) {
return resource;
}
/** */
getResourceThumbnail(resource) {
const thumb = this.getThumbnail(resource, { requireIiif: true });
if (thumb) return thumb;
if (iiifImageService(resource)) return this.iiifThumbnailUrl(resource);
if (['image', 'dctypes:Image'].includes(resource.getProperty('type'))) return ThumbnailFactory.staticImageUrl(resource);
return this.getThumbnail(resource, { quirksMode: true, requireIiif: false });
}
/** */
getIIIFThumbnail(canvas) {
const thumb = this.getThumbnail(canvas, { requireIiif: true });
if (thumb) return thumb;
const miradorCanvas = new MiradorCanvas(canvas);
return thumbnail;
}
const preferredCanvasResource = miradorCanvas.iiifImageResources[0]
|| canvas.imageResource;
if (resource.isCollection()) {
const firstManifest = resource.getManifests()[0];
if (firstManifest) return this.getSourceContentResource(firstManifest);
return (preferredCanvasResource && this.getResourceThumbnail(preferredCanvasResource))
|| this.getThumbnail(canvas, { quirksMode: true, requireIiif: false });
return undefined;
}
/** */
getManifestThumbnail(manifest) {
const thumb = this.getThumbnail(manifest, { requireIiif: true });
if (thumb) return thumb;
const miradorManifest = new MiradorManifest(manifest);
if (resource.isManifest()) {
const miradorManifest = new MiradorManifest(resource);
const canvas = miradorManifest.startCanvas || miradorManifest.canvasAt(0);
if (canvas) return this.getSourceContentResource(canvas);
return (canvas && this.getIIIFThumbnail(canvas))
|| this.getThumbnail(manifest, { quirksMode: true, requireIiif: false });
return undefined;
}
/** */
getCollectionThumbnail(collection) {
const thumb = this.getThumbnail(collection, { requireIiif: true });
if (thumb) return thumb;
if (resource.isCanvas()) {
const image = ThumbnailFactory.getPreferredImage(resource);
if (image) return this.getSourceContentResource(image);
const firstManifest = this.resource.getManifests()[0];
return undefined;
}
return (firstManifest && this.getManifestThumbnail(firstManifest))
|| this.getThumbnail(collection, { quirksMode: true, requireIiif: false });
if (resource.getType() === 'image') {
return resource;
}
/** */
return undefined;
}
/**
* Gets a thumbnail representing the resource.
* @return {Object|undefined} A thumbnail representing the resource, or undefined if none could
* be determined
*/
get() {
if (!this.resource) return undefined;
if (this.resource.isCanvas()) return this.getIIIFThumbnail(this.resource);
if (this.resource.isManifest()) return this.getManifestThumbnail(this.resource);
if (this.resource.isCollection()) return this.getCollectionThumbnail(this.resource);
return this.getResourceThumbnail(this.resource, { requireIiif: true });
// Determine which content resource we should use to derive a thumbnail
const sourceContentResource = this.getSourceContentResource(this.resource);
if (!sourceContentResource) return undefined;
// Special treatment for external resources
if (typeof sourceContentResource === 'string') return { url: sourceContentResource };
return this.iiifThumbnailUrl(sourceContentResource);
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment