2020-02-03 10:24:25 +01:00
|
|
|
import { setupBinding } from "../state/stateBinding"
|
|
|
|
import { split, last } from "lodash/fp"
|
|
|
|
import { $ } from "../core/common"
|
|
|
|
import { renderComponent } from "./renderComponent"
|
2020-01-28 15:14:53 +01:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
export const _initialiseChildren = initialiseOpts => (
|
|
|
|
childrenProps,
|
|
|
|
htmlElement,
|
|
|
|
anchor = null
|
|
|
|
) => {
|
|
|
|
const {
|
|
|
|
uiFunctions,
|
|
|
|
bb,
|
|
|
|
coreApi,
|
|
|
|
store,
|
|
|
|
componentLibraries,
|
|
|
|
treeNode,
|
|
|
|
appDefinition,
|
|
|
|
document,
|
|
|
|
hydrate,
|
|
|
|
} = initialiseOpts
|
2020-01-28 15:14:53 +01:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
for (let childNode of treeNode.children) {
|
|
|
|
if (childNode.unsubscribe) childNode.unsubscribe()
|
|
|
|
if (childNode.component) childNode.component.$destroy()
|
|
|
|
}
|
2020-01-30 00:01:14 +01:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
if (hydrate) {
|
|
|
|
while (htmlElement.firstChild) {
|
|
|
|
htmlElement.removeChild(htmlElement.firstChild)
|
2020-01-30 00:01:14 +01:00
|
|
|
}
|
2020-02-03 10:24:25 +01:00
|
|
|
}
|
2020-01-28 15:14:53 +01:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
htmlElement.classList.add(`lay-${treeNode.props._id}`)
|
|
|
|
|
|
|
|
const renderedComponents = []
|
|
|
|
for (let childProps of childrenProps) {
|
|
|
|
const { componentName, libName } = splitName(childProps._component)
|
|
|
|
|
|
|
|
if (!componentName || !libName) return
|
|
|
|
|
|
|
|
const { initialProps, bind } = setupBinding(
|
|
|
|
store,
|
|
|
|
childProps,
|
|
|
|
coreApi,
|
|
|
|
appDefinition.appRootPath
|
|
|
|
)
|
2020-01-30 00:01:14 +01:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const componentConstructor = componentLibraries[libName][componentName]
|
2020-01-28 15:14:53 +01:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const renderedComponentsThisIteration = renderComponent({
|
|
|
|
props: childProps,
|
|
|
|
parentNode: treeNode,
|
|
|
|
componentConstructor,
|
|
|
|
uiFunctions,
|
|
|
|
htmlElement,
|
|
|
|
anchor,
|
|
|
|
initialProps,
|
|
|
|
bb,
|
|
|
|
})
|
2020-01-28 15:14:53 +01:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
for (let comp of renderedComponentsThisIteration) {
|
|
|
|
comp.unsubscribe = bind(comp.component)
|
|
|
|
renderedComponents.push(comp)
|
2020-01-28 15:14:53 +01:00
|
|
|
}
|
2020-02-03 10:24:25 +01:00
|
|
|
}
|
2020-01-28 15:14:53 +01:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
return renderedComponents
|
2020-01-28 15:14:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const splitName = fullname => {
|
2020-02-03 10:24:25 +01:00
|
|
|
const componentName = $(fullname, [split("/"), last])
|
2020-01-28 15:14:53 +01:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const libName = fullname.substring(
|
|
|
|
0,
|
|
|
|
fullname.length - componentName.length - 1
|
|
|
|
)
|
2020-01-28 15:14:53 +01:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
return { libName, componentName }
|
2020-01-30 00:01:14 +01:00
|
|
|
}
|