UNPKG

marko

Version:

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

231 lines (194 loc) • 5.99 kB
"use strict";var beginComponent = require("@internal/components-beginComponent"); var endComponent = require("@internal/components-endComponent"); var registry = require("@internal/components-registry"); var componentsUtil = require("@internal/components-util"); var getComponentsContext = require("../ComponentsContext").S_; var componentLookup = componentsUtil._l_; var modernRenderer = require("../renderer"); var resolveComponentKey = modernRenderer.aX_; var trackAsyncComponents = modernRenderer.ba_; // eslint-disable-next-line no-constant-binary-expression function createRendererFunc(templateRenderFunc, componentProps) { var typeName = componentProps.t; //var assignedId = componentProps.id; var isSplit = componentProps.s === true; var isImplicit = componentProps.i === true; return function renderer(input, out, assignedId, renderingLogic) { trackAsyncComponents(out); var componentsContext = getComponentsContext(out); var parentLegacyComponentDef = componentsContext.bb_; if (isImplicit && parentLegacyComponentDef) { templateRenderFunc( input, out, parentLegacyComponentDef, parentLegacyComponentDef.s_, parentLegacyComponentDef.s_.aC_, out.global ); return; } var widgetBody = input.renderBody; var widgetState = input.widgetState; var widgetConfig = input.widgetConfig; var globalComponentsContext = componentsContext.p_; var component = globalComponentsContext.az_; var isRerender = component !== undefined; var id = assignedId; var isExisting; var parentComponentDef = componentsContext.o_; var ownerComponentDef = out.a__; var ownerComponentId = ownerComponentDef && ownerComponentDef.id; var key = out.aa_; var customEvents = out.bc_; out.a__ = null; if (component) { id = component.id; isExisting = true; globalComponentsContext.az_ = null; } else { if (key != null) { id = id || resolveComponentKey(key.toString(), parentComponentDef); } else if (parentComponentDef) { id = parentComponentDef.aO_(); } else { id = globalComponentsContext.aO_(); } } if (registry._J_ && typeName) { if (renderingLogic) delete renderingLogic.onRender; component = registry._G_( renderingLogic, id, input, out, typeName, customEvents, ownerComponentId ); if (isSplit || widgetState) { component.input = null; } else if (input.widgetProps) { // eslint-disable-next-line no-constant-condition component.input = input.widgetProps; } } else { if (!component) { if (isRerender) { // Look in in the DOM to see if a component with the same ID and type already exists. component = componentLookup[id]; if (component && component._q_ !== typeName) { component = undefined; } } if (component) { isExisting = true; } else { isExisting = false; // We need to create a new instance of the component if (typeName) { component = registry._G_(typeName, id); } } } } var isFakeComponent = false; if (!component) { isFakeComponent = true; component = { id: id, K_: {} }; } else { component._b_ = true; if (widgetState) { component.state = widgetState; } } component.widgetConfig = widgetConfig; component.Z_ = widgetBody || component.Z_; var componentDef = beginComponent( componentsContext, component, key, ownerComponentDef, isSplit, isFakeComponent ); componentsContext.bb_ = componentDef; // This is a hack, but we have to swap out the component instance stored with this node var vComponentNode = out.ab_; componentDef.s_ = isFakeComponent ? null : component; componentDef._u_ = isExisting; componentDef.y_ = true; componentDef.t = function (typeName) { if (typeName) { if (registry._J_) { var oldComponent = component; if (renderingLogic) delete renderingLogic.onRender; component = registry._G_( renderingLogic || {}, id, input, out, typeName, customEvents, ownerComponentId ); if (isSplit || widgetState) { component.input = null; } else if (input.widgetProps) { // eslint-disable-next-line no-constant-condition component.input = input.widgetProps; } Object.assign(component, oldComponent); beginComponent( componentsContext, component, key, ownerComponentDef, isSplit, false, this ); } else { vComponentNode.s_ = component = registry._G_( typeName, component.id ); } this.s_ = component; } return component; }; if (!registry._J_) { component.L_ && component.L_(); } // Render the template associated with the component using the final template // data that we constructed templateRenderFunc( input, out, componentDef, component, component.aC_, out.global ); if (customEvents && componentDef.s_) { if (registry._J_) { componentDef.W_ = customEvents; componentDef.X_ = ownerComponentId; } else { componentDef.s_.aD_( customEvents, ownerComponentId ); } } endComponent(out, componentDef); componentsContext.o_ = parentComponentDef; componentsContext.bb_ = parentLegacyComponentDef; }; } module.exports = createRendererFunc;