mirror of
https://github.com/dyiop/astute.git
synced 2025-04-09 06:40:20 -04:00
79 lines
2.9 KiB
JavaScript
79 lines
2.9 KiB
JavaScript
'use strict';
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
value: true
|
|
});
|
|
exports.calculateLengthFromValueWithUnit = exports.LENGTH_TYPE = undefined;
|
|
|
|
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
|
|
|
|
var _NodeContainer = require('./NodeContainer');
|
|
|
|
var _NodeContainer2 = _interopRequireDefault(_NodeContainer);
|
|
|
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
|
|
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
|
|
var LENGTH_WITH_UNIT = /([\d.]+)(px|r?em|%)/i;
|
|
|
|
var LENGTH_TYPE = exports.LENGTH_TYPE = {
|
|
PX: 0,
|
|
PERCENTAGE: 1
|
|
};
|
|
|
|
var Length = function () {
|
|
function Length(value) {
|
|
_classCallCheck(this, Length);
|
|
|
|
this.type = value.substr(value.length - 1) === '%' ? LENGTH_TYPE.PERCENTAGE : LENGTH_TYPE.PX;
|
|
var parsedValue = parseFloat(value);
|
|
if (process.env.NODE_ENV !== 'production' && isNaN(parsedValue)) {
|
|
console.error('Invalid value given for Length: "' + value + '"');
|
|
}
|
|
this.value = isNaN(parsedValue) ? 0 : parsedValue;
|
|
}
|
|
|
|
_createClass(Length, [{
|
|
key: 'isPercentage',
|
|
value: function isPercentage() {
|
|
return this.type === LENGTH_TYPE.PERCENTAGE;
|
|
}
|
|
}, {
|
|
key: 'getAbsoluteValue',
|
|
value: function getAbsoluteValue(parentLength) {
|
|
return this.isPercentage() ? parentLength * (this.value / 100) : this.value;
|
|
}
|
|
}], [{
|
|
key: 'create',
|
|
value: function create(v) {
|
|
return new Length(v);
|
|
}
|
|
}]);
|
|
|
|
return Length;
|
|
}();
|
|
|
|
exports.default = Length;
|
|
|
|
|
|
var getRootFontSize = function getRootFontSize(container) {
|
|
var parent = container.parent;
|
|
return parent ? getRootFontSize(parent) : parseFloat(container.style.font.fontSize);
|
|
};
|
|
|
|
var calculateLengthFromValueWithUnit = exports.calculateLengthFromValueWithUnit = function calculateLengthFromValueWithUnit(container, value, unit) {
|
|
switch (unit) {
|
|
case 'px':
|
|
case '%':
|
|
return new Length(value + unit);
|
|
case 'em':
|
|
case 'rem':
|
|
var length = new Length(value);
|
|
length.value *= unit === 'em' ? parseFloat(container.style.font.fontSize) : getRootFontSize(container);
|
|
return length;
|
|
default:
|
|
// TODO: handle correctly if unknown unit is used
|
|
return new Length('0');
|
|
}
|
|
}; |