uicore-ts
Version:
UICore is a library to build native-like user interfaces using pure Typescript. No HTML is needed at all. Components are described as TS classes and all user interactions are handled explicitly. This library is strongly inspired by the UIKit framework tha
124 lines (123 loc) • 4.09 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var SizeConverter_exports = {};
__export(SizeConverter_exports, {
default: () => toPx
});
module.exports = __toCommonJS(SizeConverter_exports);
var import_UIObject = require("./UIObject");
let preCalculated = false;
let computedValueBug = false;
const defaultView = document.defaultView;
const getComputedStyle = defaultView && defaultView.getComputedStyle;
const runit = /^(-?[\d+\.\-]+)([a-z]+|%)$/i;
const convert = {
mm2px: 1 / 25.4,
cm2px: 1 / 2.54,
pt2px: 1 / 72,
pc2px: 1 / 6,
in2px: void 0,
mozmm2px: void 0
};
function toPx(element, value, prop = "width", force = import_UIObject.NO) {
if (!preCalculated) {
preCalculated = true;
preCalculate();
}
const rem = /r?em/i;
const unit = (value.match(runit) || [])[2];
let conversion = unit === "px" ? 1 : convert[`${unit}2px`];
let result;
if (conversion || rem.test(unit) && !force) {
element = conversion ? element : unit === "rem" ? document.documentElement : prop === "fontSize" ? element.parentNode || element : element;
conversion = conversion || parseFloat(curCSS(element, "fontSize"));
result = parseFloat(value) * conversion;
} else {
const style = element.style;
const inlineValue = style[prop];
try {
style[prop] = value;
} catch (e) {
return 0;
}
result = !style[prop] ? 0 : parseFloat(curCSS(element, prop));
style[prop] = inlineValue !== void 0 ? inlineValue : null;
}
return result;
}
function preCalculate() {
let testElem = document.createElement("test");
const docElement = document.documentElement;
docElement.appendChild(testElem);
if (getComputedStyle) {
testElem.style.marginTop = "1%";
computedValueBug = getComputedStyle(testElem).marginTop === "1%";
}
[
"mozmm2px",
"in2px",
"pc2px",
"pt2px",
"cm2px",
"mm2px"
].forEach(
(conversion) => convert[conversion] = convert[conversion] ? convert[conversion] * convert.in2px : toPx(testElem, `_${conversion}`)
);
docElement.removeChild(testElem);
testElem = void 0;
}
function curCSS(elem, prop) {
const pixel = elem.style[`pixel${prop.charAt(0).toUpperCase()}${prop.slice(1)}`];
let value;
if (getComputedStyle) {
value = getComputedStyle(elem)[prop];
} else if (pixel) {
value = pixel + "px";
} else if (prop === "fontSize") {
value = toPx(elem, "1em", "left", 1) + "px";
} else {
value = elem.currentStyle[prop];
}
const unit = (value.match(runit) || [])[2];
if (unit === "%" && computedValueBug) {
if (/^top|bottom/.test(prop)) {
const parent = elem.parentNode || elem;
const innerHeight = [
"borderBottom",
"borderTop",
"paddingBottom",
"paddingTop"
].reduce(
(height, prop2) => height - parseFloat(curCSS(parent, prop2)),
parent.offsetHeight
);
value = parseFloat(value) / 100 * innerHeight + "px";
} else {
value = toPx(elem, value);
}
} else if ((value === "auto" || unit && unit !== "px") && getComputedStyle) {
value = 0;
} else if (unit && unit !== "px" && !getComputedStyle) {
value = toPx(elem, value) + "px";
}
return value;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {});
//# sourceMappingURL=SizeConverter.js.map