PxPercentUtil.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. 'use strict';
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. /**
  4. * Licensed Materials - Property of IBM
  5. * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2018
  6. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  7. */
  8. define([], function () {
  9. var PxPercentUtil = function () {
  10. function PxPercentUtil() {
  11. _classCallCheck(this, PxPercentUtil);
  12. }
  13. /**
  14. * Convert the model positional values to Percent
  15. * @param {Object} positionalModel - An object that contains any of following positional properties
  16. * { top: xxx, left: xxx, bottom: xxx, right: xxx, width: xxx, height: xxx }
  17. * xxx should be in percent (ex. '25%')
  18. * @param {Object} pageSize - An object that describes the page size where the positional model resides
  19. * The structure is { width: xxxx, height: yyyy }.
  20. * yyy needs to be in pixels (ex. '100px')
  21. * @param {Number} decimalPlaces - The number of decimal places to truncate the percent float
  22. * @returns Undefined
  23. */
  24. PxPercentUtil.changePixelPropertiesToPercent = function changePixelPropertiesToPercent(positionalModel, pageSize, decimalPlaces) {
  25. var converter = new PixelToPercentConverter(decimalPlaces);
  26. this._convertUnits(positionalModel, pageSize, converter);
  27. };
  28. /**
  29. * Convert the model positional values to Pixel
  30. * @param {Object} positionalModel - An object that contains any of following positional properties
  31. * { top: xxx, left: xxx, bottom: xxx, right: xxx, width: xxx, height: xxx }
  32. * xxx should be in pixels (ex. '100px')
  33. * @param {Object} pageSize - An object that describes the page size where the positional model resides
  34. * The structure is { width: xxxx, height: yyyy }.
  35. * yyy needs to be in pixels (ex. '100px')
  36. * @returns Undefined
  37. */
  38. PxPercentUtil.changePercentPropertiesToPixel = function changePercentPropertiesToPixel(positionalModel, pageSize) {
  39. var converter = new PercentToPixelConverter();
  40. this._convertUnits(positionalModel, pageSize, converter);
  41. };
  42. PxPercentUtil._convertUnits = function _convertUnits(positionalModel, pageSize, converter) {
  43. if (!positionalModel || !pageSize) {
  44. throw new Error('Invalid options specified.');
  45. }
  46. // Position values
  47. this._convertProperty(positionalModel, 'top', pageSize.height, converter);
  48. this._convertProperty(positionalModel, 'left', pageSize.width, converter);
  49. // Width/height values
  50. // Some models have right & bottom and others have width & height
  51. this._convertProperty(positionalModel, 'height', pageSize.height, converter);
  52. this._convertProperty(positionalModel, 'width', pageSize.width, converter);
  53. // Bottom and right values are relative to bottom and right side instead of top/left
  54. this._convertProperty(positionalModel, 'bottom', pageSize.height, converter, false);
  55. this._convertProperty(positionalModel, 'right', pageSize.width, converter, false);
  56. };
  57. PxPercentUtil._convertProperty = function _convertProperty(positionalModel, prop, maxSize, converter) {
  58. var relativeToBottomRight = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false;
  59. if (positionalModel && positionalModel[prop]) {
  60. positionalModel[prop] = converter.convertValue(positionalModel[prop], maxSize, relativeToBottomRight);
  61. }
  62. };
  63. return PxPercentUtil;
  64. }();
  65. var PixelToPercentConverter = function () {
  66. function PixelToPercentConverter(decimalPlaces) {
  67. _classCallCheck(this, PixelToPercentConverter);
  68. this.decimalPlaces = decimalPlaces;
  69. }
  70. PixelToPercentConverter.prototype.convertValue = function convertValue(value, maxSize, relativeToBottomRight) {
  71. var result = value;
  72. if (value && value.indexOf && value.indexOf('px') !== -1) {
  73. var size = 100 * parseFloat(value) / maxSize;
  74. if (relativeToBottomRight) {
  75. size = 100 - size;
  76. }
  77. if (this.decimalPlaces) {
  78. result = String(Number(size.toFixed(this.decimalPlaces))) + '%';
  79. } else {
  80. result = size + '%';
  81. }
  82. }
  83. return result;
  84. };
  85. return PixelToPercentConverter;
  86. }();
  87. var PercentToPixelConverter = function () {
  88. function PercentToPixelConverter() {
  89. _classCallCheck(this, PercentToPixelConverter);
  90. }
  91. PercentToPixelConverter.prototype.convertValue = function convertValue(value, maxSize, relativeToBottomRight) {
  92. var result = value;
  93. if (value && value.indexOf && value.indexOf('%') !== -1) {
  94. var size = parseFloat(value);
  95. if (relativeToBottomRight && size === 0) {
  96. result = maxSize;
  97. } else {
  98. result = Math.round(maxSize * size / 100);
  99. }
  100. result = result + 'px';
  101. }
  102. return result;
  103. };
  104. return PercentToPixelConverter;
  105. }();
  106. return PxPercentUtil;
  107. });
  108. //# sourceMappingURL=PxPercentUtil.js.map