UNPKG

2.59 kBJavaScriptView Raw
1import { __decorate } from "tslib";
2import { singleton } from 'mana-syringe';
3import { UnitType, CSSUnitValue } from '../cssom';
4import { parseLengthOrPercentage, mergeDimensions } from '../parser';
5/**
6 * <length> & <percentage>
7 */
8
9var CSSPropertyLengthOrPercentage =
10/** @class */
11function () {
12 function CSSPropertyLengthOrPercentage() {
13 this.parser = parseLengthOrPercentage;
14 this.mixer = mergeDimensions;
15 }
16 /**
17 * according to parent's bounds
18 *
19 * @example
20 * CSS.percent(50) -> CSS.px(0.5 * parent.width)
21 */
22
23
24 CSSPropertyLengthOrPercentage.prototype.calculator = function (name, oldParsed, computed, object, registry) {
25 if (CSSUnitValue.isRelativeUnit(computed.unit)) {
26 if (computed.unit === UnitType.kPercentage) {
27 // try to resolve according to parent's geometry bounds
28 // if (object.parentElement) {
29 // // registry.registerParentGeometryBoundsChangedHandler(object, name);
30 // return this.calculateUsedValueWithParentBounds(object, name);
31 // } else {
32 // registry.addUnresolveProperty(object, name);
33 // // defer calculation after mounted
34 // // object.addEventListener(
35 // // ElementEvent.MOUNTED,
36 // // () => {
37 // // registry.registerParentGeometryBoundsChangedHandler(object, name);
38 // // },
39 // // { once: true },
40 // // );
41 // }
42 return new CSSUnitValue(0, 'px');
43 } else if (computed.unit === UnitType.kEms) {
44 // TODO: handle ems
45 return new CSSUnitValue(0, 'px');
46 }
47 } else {
48 // remove listener if exists
49 // registry.unregisterParentGeometryBoundsChangedHandler(object, name);
50 // return absolute value
51 return computed.clone();
52 }
53 };
54
55 CSSPropertyLengthOrPercentage.prototype.nameToBoundsIndex = function (name) {
56 if (name === 'x' || name === 'cx' || name === 'width') {
57 return 0;
58 } else if (name === 'y' || name === 'cy' || name === 'height') {
59 return 1;
60 }
61
62 return 2;
63 };
64
65 CSSPropertyLengthOrPercentage.prototype.calculateUsedValueWithParentBounds = function (object, name) {
66 var bounds = object.parentElement.getGeometryBounds();
67 var computedValue = object.computedStyle[name].value;
68 return new CSSUnitValue(bounds.halfExtents[this.nameToBoundsIndex(name)] * 2 * computedValue / 100, 'px');
69 };
70
71 CSSPropertyLengthOrPercentage = __decorate([singleton()], CSSPropertyLengthOrPercentage);
72 return CSSPropertyLengthOrPercentage;
73}();
74
75export { CSSPropertyLengthOrPercentage };
\No newline at end of file