@panyam/tsutils
Version:
Some basic TS utils for personal use
167 lines • 5.16 kB
JavaScript
import * as Geom from "./geom";
import { Browser } from "./browser";
const fontMetrics = {};
export function svgBBox(element) {
var _a;
const bbox = Geom.Rect.from(element.getBBox());
if (Browser.IS_SAFARI()) {
const clientRect = element.getClientRects()[0];
if (clientRect) {
const parentClientRect = (_a = element.parentElement) === null || _a === void 0 ? void 0 : _a.getBoundingClientRect();
bbox.x = bbox.x + clientRect.x - ((parentClientRect === null || parentClientRect === void 0 ? void 0 : parentClientRect.x) || 0);
bbox.width = clientRect.width;
bbox.height = clientRect.height;
}
}
return bbox;
}
export function getFontMetrics(fontFamily, fontSize) {
if (!(fontFamily in fontMetrics)) {
fontMetrics[fontFamily] = {};
}
const familyMetrics = fontMetrics[fontFamily];
if (!(fontSize in familyMetrics)) {
const a = $("<div style='position:absolute;top:0;left:0'>M</div>");
a.css("font-family", fontFamily);
a.css("font-size", fontSize);
$($(document)[0].body).append(a);
familyMetrics[fontSize] = {
height: a[0].offsetHeight,
size: fontSize,
};
a.remove();
}
return familyMetrics[fontSize];
}
export function forEachChild(elem, visitor) {
const children = elem.children;
const L = children.length;
for (let i = 0; i < L; i++) {
const child = children[i];
if (visitor(child, i) == false)
return false;
}
}
export function forEachNode(elem, visitor) {
const children = elem.childNodes;
const L = children.length;
for (let i = 0; i < L; i++) {
const child = children[i];
if (visitor(child, i) == false)
return;
}
}
export function forEachAttribute(elem, visitor) {
const nodeNameMap = elem.attributes;
for (let i = 0; i < nodeNameMap.length; i++) {
const attrib = nodeNameMap[i];
if (visitor(attrib.name, attrib.value) == false)
return;
}
}
export function parseCSSStyles(value) {
const out = {};
if (value && value != null) {
const values = value.split(";");
values.forEach(function (elem) {
if (elem.trim().length > 0) {
const kvpair = elem.split(":");
if (kvpair.length >= 2) {
const key = kvpair[0].trim();
const value = kvpair[1].trim();
if (key.length > 0) {
out[key] = value;
}
}
}
});
}
return out;
}
export function getAttr(elem, attrib, ...validators) {
let value = elem.getAttribute(attrib);
for (let i = 0; i < validators.length; i++) {
value = validators[i](value);
}
return value;
}
export function ensureAttr(elem, attrib) {
const value = elem.getAttribute(attrib) || null;
if (value == null) {
throw new Error(`Element MUST have Attribute: ${attrib}`);
}
return value;
}
export function setAttr(elem, name, value) {
return elem.setAttribute(name, value);
}
export function getAttrOrStyle(elem, attribName, cssStyles, styleName) {
return elem.getAttribute(attribName) || cssStyles[styleName];
}
export function createSVGNode(nodename, config) {
config = config || {};
config.ns = "http://www.w3.org/2000/svg";
return createNode(nodename, config);
}
export function createNode(nodename, config) {
let out;
config = config || {};
const ns = config.ns;
const doc = config.doc || document;
const attrs = config.attrs || {};
const text = config.text || "";
const parent = config.parent || null;
if (ns)
out = doc.createElementNS(ns, nodename);
else
out = doc.createElement(nodename);
if (parent != null) {
parent.appendChild(out);
}
for (const attr in attrs || {}) {
const value = attrs[attr];
out.setAttribute(attr, value);
}
if (text) {
out.textContent = text;
}
return out;
}
export function removeNode(node) {
var _a;
(_a = node.parentNode) === null || _a === void 0 ? void 0 : _a.removeChild(node);
}
export function insertAfter(node, ...newNodes) {
const parentNode = node.parentNode;
if (parentNode == null)
return false;
const nextNode = node.nextSibling;
if (nextNode == null) {
for (let i = 0; i < newNodes.length; i++) {
parentNode.appendChild(newNodes[i]);
}
}
else {
for (let i = 0; i < newNodes.length; i++) {
parentNode.insertBefore(newNodes[i], nextNode);
}
}
return true;
}
export function getCSS(elem, attr) {
return elem.style[attr];
}
export function setCSS(elem, attr, value) {
elem.style[attr] = value;
}
export function ensureElement(elemOrId, root = null) {
if (typeof elemOrId === "string") {
if (root == null)
root = document;
return root.querySelector(elemOrId);
}
else {
return elemOrId;
}
}
//# sourceMappingURL=dom.js.map