UNPKG

marko

Version:

UI Components + streaming, async, high performance, HTML templating for Node.js and the browser.

112 lines (86 loc) 2.29 kB
"use strict";var domInsert = require("./dom-insert"); // eslint-disable-next-line no-constant-binary-expression function getRootNode(el) { var cur = el; while (cur.parentNode) cur = cur.parentNode; return cur; } function getComponentDefs(result) { var componentDefs = result.b_; if (!componentDefs) { throw Error("No component"); } return componentDefs; } function RenderResult(out) { this.out = this.r_ = out; this.b_ = undefined; } module.exports = RenderResult; var proto = RenderResult.prototype = { getComponent: function () { return this.getComponents()[0]; }, getComponents: function (selector) { if (this.b_ === undefined) { throw Error("Not added to DOM"); } var componentDefs = getComponentDefs(this); var components = []; componentDefs.forEach(function (componentDef) { var component = componentDef.s_; if (!selector || selector(component)) { components.push(component); } }); return components; }, afterInsert: function (host) { var out = this.r_; var componentsContext = out.b_; if (componentsContext) { this.b_ = componentsContext.ah_(host); } else { this.b_ = null; } return this; }, getNode: function (host) { return this.r_.ai_(host); }, getOutput: function () { return this.r_.aj_(); }, toString: function () { return this.r_.toString(); }, document: typeof document === "object" && document }; Object.defineProperty(proto, "html", { get: function () { // eslint-disable-next-line no-constant-condition return this.toString(); } }); Object.defineProperty(proto, "context", { get: function () { // eslint-disable-next-line no-constant-condition return this.r_; } }); // Add all of the following DOM methods to Component.prototype: // - appendTo(referenceEl) // - replace(referenceEl) // - replaceChildrenOf(referenceEl) // - insertBefore(referenceEl) // - insertAfter(referenceEl) // - prependTo(referenceEl) domInsert( proto, function getEl(renderResult, referenceEl) { return renderResult.getNode(getRootNode(referenceEl)); }, function afterInsert(renderResult, referenceEl) { return renderResult.afterInsert(getRootNode(referenceEl)); } );