123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- 'use strict';
- /**
- * Licensed Materials - Property of IBM
- * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2014
- * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
- */
- define(['jquery'], function ($) {
- var ScalingUtil = {
- /**
- * @param params An object with the following members:
- * * width and height - the user inputed width and height to scale/
- * verify
- * * image - a Javascript Image object whose width and height are to
- * be scaled - either width and height, or image must be provided
- * * defaultDim - (optional) default dimensions to replace missing
- * or invalid width/height dimensions
- * * scaleWidth - (optional) indicates the width should be replaced
- * with a version scaled to match the height. This member should
- * be an object with width and height members indicating the
- * correct aspect ratio.
- * * scaleHeight - (optional) the equivalent of scaleWidth to set
- * the height. Either scaleWidth or scaleHeight should be
- * specified, not both.
- * * min - (optional) indicates minimum output dimensions. This
- * member should be an object with width and height members
- * indicating their respective minimum dimensions.
- * * max - (optional) the equivalent of min with the maximum
- * dimensions. If both min and max are specified, and it is not
- * possible to satisfy both, max has priority.
- * * format - (optional) set to "css" for the outputs to be strings
- * with "px"; otherwise they will be integers.
- * @return an object with width and height members
- */
- scale: function scale(params) {
- var dim = {
- width: params.width,
- height: params.height
- };
- var aspectRatio;
- if (!dim.width && !dim.height && params.image) {
- dim.width = params.image.naturalWidth;
- dim.height = params.image.naturalHeight;
- }
- dim.width = this._sanitizeNumberInput(dim.width);
- dim.height = this._sanitizeNumberInput(dim.height);
- if (params.defaultDim) {
- if (dim.width < 1 && !params.scaleWidth) {
- dim.width = params.defaultDim.width;
- }
- if (dim.height < 1 && !params.scaleHeight) {
- dim.height = params.defaultDim.height;
- }
- }
- this._applyScale(params.scaleWidth || params.scaleHeight, !!params.scaleWidth, dim);
- aspectRatio = dim.width / dim.height;
- this._enforceMin(params.min, aspectRatio, dim);
- this._enforceMax(params.max, aspectRatio, dim);
- this._formatDimens(params.format, dim);
- return dim;
- },
- // returns input as an integer if it is numeric, otherwise return input
- _sanitizeNumberInput: function _sanitizeNumberInput(input) {
- var number = 0;
- if ($.type(input) === 'number') {
- number = input;
- } else if ($.isNumeric(input)) {
- number = parseInt(input, 10);
- }
- return number;
- },
- // applies scale to dim
- // if dim doesn't have a value, overwrites it with scale
- _applyScale: function _applyScale(scale, scaleWidth, dim) {
- if (scale) {
- var wScale = scale.width;
- var hScale = scale.height;
- var bothSet = dim.width > 0 && dim.height > 0;
- if (dim.width < 1 && dim.height < 1) {
- dim.width = wScale;
- dim.height = hScale;
- } else {
- this._scaleMaintainRatio(scale, scaleWidth, bothSet, dim);
- }
- }
- },
- // scales dim to have the same aspect ratio as scale
- // if force=true, will overwrite previous value, otherwise it will keep previous value
- // if scaleWidth=true, modifies width, otherwise modifies height
- // precondition: dim has at least one of width/height defined
- _scaleMaintainRatio: function _scaleMaintainRatio(scale, scaleWidth, force, dim) {
- if (scaleWidth && (dim.width < 1 || force)) {
- dim.width = scale.width * dim.height / scale.height;
- } else if (!scaleWidth && (dim.height < 1 || force)) {
- dim.height = scale.height * dim.width / scale.width;
- }
- },
- // enforces min dimensions while preserving aspect ratio
- _enforceMin: function _enforceMin(min, aspectRatio, dim) {
- if (min) {
- var wMin = min.width;
- var hMin = min.height;
- var wRatio = wMin ? dim.width / wMin : 1;
- var hRatio = hMin ? dim.height / hMin : 1;
- if (wRatio < 1 && wRatio < hRatio) {
- dim.width = wMin;
- dim.height = wMin / aspectRatio;
- } else if (hRatio < 1) {
- dim.height = hMin;
- dim.width = hMin * aspectRatio;
- }
- }
- },
- // enforces max dimensions while preserving aspect ratio
- _enforceMax: function _enforceMax(max, aspectRatio, dim) {
- if (max) {
- var wMax = max.width;
- var hMax = max.height;
- var wRatio = wMax ? dim.width / wMax : 0;
- var hRatio = hMax ? dim.height / hMax : 0;
- if (wRatio > 1 && wRatio > hRatio) {
- dim.width = wMax;
- dim.height = wMax / aspectRatio;
- } else if (hRatio > 1) {
- dim.height = hMax;
- dim.width = hMax * aspectRatio;
- }
- }
- },
- // formats dimensions by rounding them and changing to css styles if format=css
- _formatDimens: function _formatDimens(format, dim) {
- dim.width = Math.round(dim.width);
- dim.height = Math.round(dim.height);
- if (format === 'css') {
- dim.width = dim.width + 'px';
- dim.height = dim.height + 'px';
- }
- }
- };
- return ScalingUtil;
- });
- //# sourceMappingURL=ScalingUtil.js.map
|