123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- 'use strict';
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
- define([], function () {
- var PxPercentUtil = function () {
- function PxPercentUtil() {
- _classCallCheck(this, PxPercentUtil);
- }
-
- PxPercentUtil.changePixelPropertiesToPercent = function changePixelPropertiesToPercent(positionalModel, pageSize, decimalPlaces) {
- var converter = new PixelToPercentConverter(decimalPlaces);
- this._convertUnits(positionalModel, pageSize, converter);
- };
-
- PxPercentUtil.changePercentPropertiesToPixel = function changePercentPropertiesToPixel(positionalModel, pageSize) {
- var converter = new PercentToPixelConverter();
- this._convertUnits(positionalModel, pageSize, converter);
- };
- PxPercentUtil._convertUnits = function _convertUnits(positionalModel, pageSize, converter) {
- if (!positionalModel || !pageSize) {
- throw new Error('Invalid options specified.');
- }
-
- this._convertProperty(positionalModel, 'top', pageSize.height, converter);
- this._convertProperty(positionalModel, 'left', pageSize.width, converter);
-
-
- this._convertProperty(positionalModel, 'height', pageSize.height, converter);
- this._convertProperty(positionalModel, 'width', pageSize.width, converter);
-
- this._convertProperty(positionalModel, 'bottom', pageSize.height, converter, false);
- this._convertProperty(positionalModel, 'right', pageSize.width, converter, false);
- };
- PxPercentUtil._convertProperty = function _convertProperty(positionalModel, prop, maxSize, converter) {
- var relativeToBottomRight = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false;
- if (positionalModel && positionalModel[prop]) {
- positionalModel[prop] = converter.convertValue(positionalModel[prop], maxSize, relativeToBottomRight);
- }
- };
- return PxPercentUtil;
- }();
- var PixelToPercentConverter = function () {
- function PixelToPercentConverter(decimalPlaces) {
- _classCallCheck(this, PixelToPercentConverter);
- this.decimalPlaces = decimalPlaces;
- }
- PixelToPercentConverter.prototype.convertValue = function convertValue(value, maxSize, relativeToBottomRight) {
- var result = value;
- if (value && value.indexOf && value.indexOf('px') !== -1) {
- var size = 100 * parseFloat(value) / maxSize;
- if (relativeToBottomRight) {
- size = 100 - size;
- }
- if (this.decimalPlaces) {
- result = String(Number(size.toFixed(this.decimalPlaces))) + '%';
- } else {
- result = size + '%';
- }
- }
- return result;
- };
- return PixelToPercentConverter;
- }();
- var PercentToPixelConverter = function () {
- function PercentToPixelConverter() {
- _classCallCheck(this, PercentToPixelConverter);
- }
- PercentToPixelConverter.prototype.convertValue = function convertValue(value, maxSize, relativeToBottomRight) {
- var result = value;
- if (value && value.indexOf && value.indexOf('%') !== -1) {
- var size = parseFloat(value);
- if (relativeToBottomRight && size === 0) {
- result = maxSize;
- } else {
- result = Math.round(maxSize * size / 100);
- }
- result = result + 'px';
- }
- return result;
- };
- return PercentToPixelConverter;
- }();
- return PxPercentUtil;
- });
|