From 45560ab1fa64837712a7fa8bc91de5544045ccd6 Mon Sep 17 00:00:00 2001 From: Chris Beer <cabeer@stanford.edu> Date: Thu, 28 Feb 2019 18:31:23 -0800 Subject: [PATCH] Refactor out thumbnailConstraints helper --- src/lib/ManifestoCanvas.js | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/src/lib/ManifestoCanvas.js b/src/lib/ManifestoCanvas.js index e092144d4..885e080df 100644 --- a/src/lib/ManifestoCanvas.js +++ b/src/lib/ManifestoCanvas.js @@ -51,31 +51,35 @@ export default class ManifestoCanvas { return undefined; } - if (maxWidth && maxHeight) { - const { aspectRatio } = this; - const desiredAspectRatio = maxWidth / maxHeight; - - // size to width - if (desiredAspectRatio < aspectRatio) { - height = null; - width = maxWidth; - } else { + switch (this.thumbnailConstraints(maxWidth, maxHeight)) { + case 'sizeByH': height = maxHeight; - width = null; - } - } else if (!maxWidth && !maxHeight) { - width = null; - height = '150'; - } else { - width = maxWidth; - height = maxHeight; + break; + case 'sizeByW': + width = maxWidth; + break; + default: + height = '150'; } + // note that, although the IIIF server may support sizeByConfinedWh (e.g. !w,h) // this is a IIIF level 2 feature, so we're instead providing w, or h,-style requests // which are only level 1. return this.canonicalImageUri.replace(/\/full\/.*\/0\//, `/full/${width || ''},${height || ''}/0/`); } + /** @private */ + thumbnailConstraints(maxWidth, maxHeight) { + if (!maxHeight && !maxWidth) return undefined; + if (maxHeight && !maxWidth) return 'sizeByH'; + if (!maxHeight && maxWidth) return 'sizeByW'; + + const { aspectRatio } = this; + const desiredAspectRatio = maxWidth / maxHeight; + + return desiredAspectRatio < aspectRatio ? 'sizeByW' : 'sizeByH'; + } + /** * checks whether the canvas has a valid height */ -- GitLab