UNPKG

@nwebui/react-niagara-px

Version:
124 lines 4.01 kB
import * as baja from "@nwebui/bajascript"; const types = {}; const knownWidgets = { AdvancedBoundLabel: "tacToolKit", AnalogMeter: "kitPx", Bargraph: "kitPx", BorderPane: "bajaui", BoundLabel: "kitPx", Button: "bajaui", CanvasPane: "bajaui", CheckBox: "bajaui", Ellipse: "bajaui", ImageButton: "kitPx", Label: "bajaui", LayerTag: "bajaui", Line: "bajaui", Picture: "bajaui", Polygon: "bajaui", PopupBinding: "kitPx", Rect: "bajaui", ScrollPane: "bajaui", ToggleButton: "bajaui", }; export default class PxDecoder { pxDoc; modules = []; constructor(pxDoc) { this.pxDoc = pxDoc; } async decodeDocument() { const docElem = this.pxDoc.documentElement; if (docElem.tagName !== "px") { throw new Error("root element must be px"); } this.decodeImport(docElem); return this.decodeContent(docElem); } decodeImport(elem) { elem.querySelectorAll("module").forEach((module) => { this.modules.push(module.getAttribute("name")); }); } async decodeContent(elem) { const contentElem = elem.querySelector("content"); if (!contentElem) { throw new Error("Missing <content> element"); } return (await this.decodeFromElem(contentElem.firstElementChild)); } async decodeFromElem(elem) { const type = await this.toType(elem); const obj = type.newInstance(); // @ts-ignore obj.elem = elem; if (obj.isSimple()) { return obj.decodeFromString(elem.getAttribute("value")); } else { await this.decodeProps(elem, obj); } return obj; } async decodeProps(elem, comp) { comp.elem = elem; for (let i = 0; i < elem.attributes.length; i++) { const attr = elem.attributes.item(i); const v = comp.get(attr.name); if (v && v.isSimple()) { comp.set(attr.name, v.decodeFromString(attr.value)); } } for (let i = 0; i < elem.childNodes.length; i++) { const child = elem.childNodes.item(i); if (child.nodeType == Node.ELEMENT_NODE) { const name = child.getAttribute("name"); const childComp = await this.decodeFromElem(child); if (name && comp.get(name)) { comp.set(name, childComp); } else { const facets = baja.REGISTRY.newInstance("baja:Facets"); comp.add(name, childComp, 0, facets); } } } } async toType(elem) { let tagName = elem.tagName; switch (tagName) { case "BoundLabelEx": tagName = "BoundLabel"; break; } if (types[tagName]) { return types[tagName]; } if (knownWidgets[tagName]) { try { const typespec = knownWidgets[tagName] + ":" + tagName; await baja.REGISTRY.importTypes([typespec]); const type = baja.REGISTRY.getType(typespec); types[tagName] = type; return type; } catch (e) { // empty } } for (const module of this.modules) { try { const typespec = module + ":" + tagName; await baja.REGISTRY.importTypes([typespec]); const type = baja.REGISTRY.getType(typespec); types[tagName] = type; return type; } catch (e) { // empty } } throw new Error("type not found for " + tagName); } } //# sourceMappingURL=PxDecoder.js.map