UNPKG

ajsfw

Version:
95 lines (94 loc) 3.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var ajsfw_1 = require("ajsfw"); var utils_1 = require("ajsfw/utils"); var Tsx = (function () { function Tsx() { } Tsx.exception = function (message) { throw message; }; Tsx.renderStyle = function (style) { style = style.replace(/\^/g, "{").replace(/\$/g, "}"); var element = ajsfw_1.ajsConfig.debugging.console.styleRenderTarget.ownerDocument.createElement("style"); element.setAttribute("type", "text/css"); element.textContent = style; return element; }; Tsx.renderTag = function (tag, props) { var children = []; for (var _i = 2; _i < arguments.length; _i++) { children[_i - 2] = arguments[_i]; } var element = null; if (utils_1.htmlTags.indexOf(tag.toUpperCase()) !== -1) { element = ajsfw_1.ajsConfig.debugging.console.bodyRenderTarget.ownerDocument.createElement(tag); if (children instanceof Array) { Tsx.processChildren(element, children); } } Tsx.setElementAttribs(element, props); return element; }; Tsx.processChildren = function (element, children) { for (var i = 0; i < children.length; i++) { Tsx.processChild(element, children[i]); } }; Tsx.processChild = function (element, child) { if (child instanceof Array) { Tsx.processChildren(element, child); } else { if (child instanceof HTMLElement) { element.appendChild(child); } if (typeof child === "string" || typeof child === "number") { child = "" + child; var node = ajsfw_1.ajsConfig.debugging.console.styleRenderTarget.ownerDocument.createTextNode(child); element.appendChild(node); } } }; Tsx.setElementAttribs = function (element, props) { if (props) { for (var key in props) { if (props.hasOwnProperty(key)) { if (key !== "ajsdata") { if (props[key] instanceof Function) { element.addEventListener(key, props[key]); } else { element.setAttribute(key, props[key]); } } else { element[key] = props[key]; } } } } }; Tsx.createElement = function (tag, props) { var children = []; for (var _i = 2; _i < arguments.length; _i++) { children[_i - 2] = arguments[_i]; } var element = null; switch (tag) { case "style": element = (children.length === 1) ? Tsx.renderStyle(children[0]) : Tsx.exception("Unexpected style data!"); break; default: element = Tsx.renderTag(tag, props, children); } if (element === null) { throw "Invalid tag"; } return element; }; return Tsx; }()); exports.Tsx = Tsx; self["AjsDebugTsxFactory"] = Tsx; exports.AjsDebugTsxFactory = self["AjsDebugTsxFactory"];