UNPKG

@panyam/tsutils

Version:

Some basic TS utils for personal use

209 lines 6.75 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.svgBBox = svgBBox; exports.getFontMetrics = getFontMetrics; exports.forEachChild = forEachChild; exports.forEachNode = forEachNode; exports.forEachAttribute = forEachAttribute; exports.parseCSSStyles = parseCSSStyles; exports.getAttr = getAttr; exports.ensureAttr = ensureAttr; exports.setAttr = setAttr; exports.getAttrOrStyle = getAttrOrStyle; exports.createSVGNode = createSVGNode; exports.createNode = createNode; exports.removeNode = removeNode; exports.insertAfter = insertAfter; exports.getCSS = getCSS; exports.setCSS = setCSS; exports.ensureElement = ensureElement; const Geom = __importStar(require("./geom")); const browser_1 = require("./browser"); const fontMetrics = {}; function svgBBox(element) { var _a; const bbox = Geom.Rect.from(element.getBBox()); if (browser_1.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; } 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]; } 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; } } 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; } } 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; } } 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; } function getAttr(elem, attrib, ...validators) { let value = elem.getAttribute(attrib); for (let i = 0; i < validators.length; i++) { value = validators[i](value); } return value; } function ensureAttr(elem, attrib) { const value = elem.getAttribute(attrib) || null; if (value == null) { throw new Error(`Element MUST have Attribute: ${attrib}`); } return value; } function setAttr(elem, name, value) { return elem.setAttribute(name, value); } function getAttrOrStyle(elem, attribName, cssStyles, styleName) { return elem.getAttribute(attribName) || cssStyles[styleName]; } function createSVGNode(nodename, config) { config = config || {}; config.ns = "http://www.w3.org/2000/svg"; return createNode(nodename, config); } 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; } function removeNode(node) { var _a; (_a = node.parentNode) === null || _a === void 0 ? void 0 : _a.removeChild(node); } 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; } function getCSS(elem, attr) { return elem.style[attr]; } function setCSS(elem, attr, value) { elem.style[attr] = value; } 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