UNPKG

marko

Version:

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

230 lines (193 loc) • 5.93 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._k_; var modernRenderer = require("../renderer"); var resolveComponentKey = modernRenderer.aV_; var trackAsyncComponents = modernRenderer.aZ_; 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.b__; if (isImplicit && parentLegacyComponentDef) { templateRenderFunc( input, out, parentLegacyComponentDef, parentLegacyComponentDef.s_, parentLegacyComponentDef.s_.aB_, out.global ); return; } var widgetBody = input.renderBody; var widgetState = input.widgetState; var widgetConfig = input.widgetConfig; var globalComponentsContext = componentsContext.p_; var component = globalComponentsContext.ay_; var isRerender = component !== undefined; var id = assignedId; var isExisting; var parentComponentDef = componentsContext.o_; var ownerComponentDef = out._Z_; var ownerComponentId = ownerComponentDef && ownerComponentDef.id; var key = out.a__; var customEvents = out.ba_; out._Z_ = null; if (component) { id = component.id; isExisting = true; globalComponentsContext.ay_ = null; } else { if (key != null) { id = id || resolveComponentKey(key.toString(), parentComponentDef); } else if (parentComponentDef) { id = parentComponentDef.aM_(); } else { id = globalComponentsContext.aM_(); } } if (registry._I_ && typeName) { if (renderingLogic) delete renderingLogic.onRender; component = registry._F_( 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._p_ !== typeName) { component = undefined; } } if (component) { isExisting = true; } else { isExisting = false; // We need to create a new instance of the component if (typeName) { component = registry._F_(typeName, id); } } } } var isFakeComponent = false; if (!component) { isFakeComponent = true; component = { id: id, K_: {} }; } else { component._a_ = true; if (widgetState) { component.state = widgetState; } } component.widgetConfig = widgetConfig; component.Y_ = widgetBody || component.Y_; var componentDef = beginComponent( componentsContext, component, key, ownerComponentDef, isSplit, isFakeComponent ); componentsContext.b__ = componentDef; // This is a hack, but we have to swap out the component instance stored with this node var vComponentNode = out.aa_; componentDef.s_ = isFakeComponent ? null : component; componentDef._t_ = isExisting; componentDef.y_ = true; componentDef.t = function (typeName) { if (typeName) { if (registry._I_) { var oldComponent = component; if (renderingLogic) delete renderingLogic.onRender; component = registry._F_( 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._F_( typeName, component.id ); } this.s_ = component; } return component; }; if (!registry._I_) { 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.aB_, out.global ); if (customEvents && componentDef.s_) { if (registry._I_) { componentDef.V_ = customEvents; componentDef.W_ = ownerComponentId; } else { componentDef.s_.aC_( customEvents, ownerComponentId ); } } endComponent(out, componentDef); componentsContext.o_ = parentComponentDef; componentsContext.b__ = parentLegacyComponentDef; }; } module.exports = createRendererFunc;