2020-02-14 12:51:45 +01:00
|
|
|
import { attachChildren } from "./render/attachChildren"
|
2020-02-18 13:29:38 +01:00
|
|
|
import { createTreeNode } from "./render/prepareRenderComponent"
|
2020-02-10 16:51:09 +01:00
|
|
|
import { screenRouter } from "./render/screenRouter"
|
2020-02-18 13:29:38 +01:00
|
|
|
import { createStateManager } from "./state/stateManager"
|
2020-07-03 00:22:20 +02:00
|
|
|
import { parseAppIdFromCookie } from "./render/getAppId"
|
2019-09-27 18:03:31 +02:00
|
|
|
|
2020-05-29 15:06:10 +02:00
|
|
|
export const createApp = ({
|
2020-02-03 10:24:25 +01:00
|
|
|
componentLibraries,
|
2020-02-10 22:35:51 +01:00
|
|
|
frontendDefinition,
|
2020-06-01 11:41:28 +02:00
|
|
|
window,
|
2020-05-29 15:06:10 +02:00
|
|
|
}) => {
|
2020-02-10 16:51:09 +01:00
|
|
|
let routeTo
|
|
|
|
let currentUrl
|
2020-02-18 13:29:38 +01:00
|
|
|
let screenStateManager
|
2019-10-07 07:03:41 +02:00
|
|
|
|
2020-02-10 16:51:09 +01:00
|
|
|
const onScreenSlotRendered = screenSlotNode => {
|
2020-05-30 01:14:41 +02:00
|
|
|
const onScreenSelected = (screen, url) => {
|
2020-02-18 13:29:38 +01:00
|
|
|
const stateManager = createStateManager({
|
|
|
|
componentLibraries,
|
|
|
|
onScreenSlotRendered: () => {},
|
2020-02-21 15:44:48 +01:00
|
|
|
routeTo,
|
2020-02-18 13:29:38 +01:00
|
|
|
})
|
2020-05-30 01:14:41 +02:00
|
|
|
const getAttachChildrenParams = attachChildrenParams(stateManager)
|
2020-02-14 12:51:45 +01:00
|
|
|
screenSlotNode.props._children = [screen.props]
|
2020-05-30 01:14:41 +02:00
|
|
|
const initialiseChildParams = getAttachChildrenParams(screenSlotNode)
|
2020-02-14 12:51:45 +01:00
|
|
|
attachChildren(initialiseChildParams)(screenSlotNode.rootElement, {
|
|
|
|
hydrate: true,
|
|
|
|
force: true,
|
|
|
|
})
|
2020-02-18 13:29:38 +01:00
|
|
|
if (screenStateManager) screenStateManager.destroy()
|
|
|
|
screenStateManager = stateManager
|
2020-02-10 16:51:09 +01:00
|
|
|
currentUrl = url
|
|
|
|
}
|
|
|
|
|
2020-05-30 01:14:41 +02:00
|
|
|
routeTo = screenRouter({
|
|
|
|
screens: frontendDefinition.screens,
|
2020-02-21 16:20:00 +01:00
|
|
|
onScreenSelected,
|
2020-06-14 21:30:23 +02:00
|
|
|
window,
|
2020-05-30 01:14:41 +02:00
|
|
|
})
|
2020-06-14 21:30:23 +02:00
|
|
|
const fallbackPath = window.location.pathname.replace(
|
2020-07-03 00:22:20 +02:00
|
|
|
parseAppIdFromCookie(window.document.cookie),
|
2020-06-14 21:30:23 +02:00
|
|
|
""
|
|
|
|
)
|
2020-02-21 17:52:29 +01:00
|
|
|
routeTo(currentUrl || fallbackPath)
|
2020-02-10 16:51:09 +01:00
|
|
|
}
|
|
|
|
|
2020-05-30 01:14:41 +02:00
|
|
|
const attachChildrenParams = stateManager => {
|
|
|
|
const getInitialiseParams = treeNode => ({
|
|
|
|
componentLibraries,
|
|
|
|
treeNode,
|
|
|
|
onScreenSlotRendered,
|
|
|
|
setupState: stateManager.setup,
|
|
|
|
})
|
|
|
|
|
|
|
|
return getInitialiseParams
|
|
|
|
}
|
2020-02-10 16:51:09 +01:00
|
|
|
|
|
|
|
let rootTreeNode
|
2020-02-18 13:29:38 +01:00
|
|
|
const pageStateManager = createStateManager({
|
|
|
|
componentLibraries,
|
|
|
|
onScreenSlotRendered,
|
2020-02-21 15:44:48 +01:00
|
|
|
// seems weird, but the routeTo variable may not be available at this point
|
|
|
|
routeTo: url => routeTo(url),
|
2020-02-18 13:29:38 +01:00
|
|
|
})
|
2020-02-10 16:51:09 +01:00
|
|
|
|
|
|
|
const initialisePage = (page, target, urlPath) => {
|
|
|
|
currentUrl = urlPath
|
2019-09-19 05:35:40 +02:00
|
|
|
|
2020-02-10 16:51:09 +01:00
|
|
|
rootTreeNode = createTreeNode()
|
2020-02-14 12:51:45 +01:00
|
|
|
rootTreeNode.props = {
|
|
|
|
_children: [page.props],
|
|
|
|
}
|
2020-05-30 01:14:41 +02:00
|
|
|
const getInitialiseParams = attachChildrenParams(pageStateManager)
|
|
|
|
const initChildParams = getInitialiseParams(rootTreeNode)
|
2020-02-10 16:51:09 +01:00
|
|
|
|
2020-02-14 12:51:45 +01:00
|
|
|
attachChildren(initChildParams)(target, {
|
|
|
|
hydrate: true,
|
|
|
|
force: true,
|
|
|
|
})
|
2020-02-10 16:51:09 +01:00
|
|
|
|
|
|
|
return rootTreeNode
|
|
|
|
}
|
2020-04-06 18:06:04 +02:00
|
|
|
|
2020-02-10 16:51:09 +01:00
|
|
|
return {
|
|
|
|
initialisePage,
|
2020-02-18 13:29:38 +01:00
|
|
|
screenStore: () => screenStateManager.store,
|
|
|
|
pageStore: () => pageStateManager.store,
|
2020-02-10 16:51:09 +01:00
|
|
|
routeTo: () => routeTo,
|
|
|
|
rootNode: () => rootTreeNode,
|
|
|
|
}
|
2019-09-19 05:35:40 +02:00
|
|
|
}
|