2020-02-03 10:24:25 +01:00
|
|
|
import { writable } from "svelte/store"
|
|
|
|
import { createCoreApi } from "./core"
|
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"
|
2019-09-27 18:03:31 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
export const createApp = (
|
|
|
|
componentLibraries,
|
2020-02-10 22:35:51 +01:00
|
|
|
frontendDefinition,
|
|
|
|
backendDefinition,
|
2020-02-03 10:24:25 +01:00
|
|
|
user,
|
2020-02-18 13:29:38 +01:00
|
|
|
uiFunctions,
|
|
|
|
window
|
2020-02-03 10:24:25 +01:00
|
|
|
) => {
|
2020-02-10 22:35:51 +01:00
|
|
|
const coreApi = createCoreApi(backendDefinition, user)
|
|
|
|
backendDefinition.hierarchy = coreApi.templateApi.constructHierarchy(
|
|
|
|
backendDefinition.hierarchy
|
2020-02-03 10:24:25 +01:00
|
|
|
)
|
2019-09-29 07:40:06 +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 => {
|
|
|
|
const onScreenSelected = (screen, store, url) => {
|
2020-02-18 13:29:38 +01:00
|
|
|
const stateManager = createStateManager({
|
|
|
|
store,
|
|
|
|
coreApi,
|
|
|
|
frontendDefinition,
|
|
|
|
componentLibraries,
|
|
|
|
uiFunctions,
|
|
|
|
onScreenSlotRendered: () => {},
|
2020-02-21 15:44:48 +01:00
|
|
|
routeTo,
|
2020-02-18 13:29:38 +01:00
|
|
|
})
|
|
|
|
const getAttchChildrenParams = attachChildrenParams(stateManager)
|
2020-02-14 12:51:45 +01:00
|
|
|
screenSlotNode.props._children = [screen.props]
|
2020-02-18 13:29:38 +01:00
|
|
|
const initialiseChildParams = getAttchChildrenParams(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-02-12 13:45:24 +01:00
|
|
|
routeTo = screenRouter(frontendDefinition.screens, onScreenSelected)
|
2020-02-10 16:51:09 +01:00
|
|
|
routeTo(currentUrl || window.location.pathname)
|
|
|
|
}
|
|
|
|
|
2020-02-18 13:29:38 +01:00
|
|
|
const attachChildrenParams = stateManager => {
|
2020-02-14 12:51:45 +01:00
|
|
|
const getInitialiseParams = treeNode => ({
|
2020-02-10 16:51:09 +01:00
|
|
|
componentLibraries,
|
|
|
|
uiFunctions,
|
|
|
|
treeNode,
|
|
|
|
onScreenSlotRendered,
|
2020-02-18 13:29:38 +01:00
|
|
|
setupState: stateManager.setup,
|
|
|
|
getCurrentState: stateManager.getCurrentState,
|
2020-02-10 16:51:09 +01:00
|
|
|
})
|
|
|
|
|
2020-02-18 13:29:38 +01:00
|
|
|
return getInitialiseParams
|
2020-02-10 16:51:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
let rootTreeNode
|
2020-02-18 13:29:38 +01:00
|
|
|
const pageStateManager = createStateManager({
|
|
|
|
store: writable({ _bbuser: user }),
|
|
|
|
coreApi,
|
|
|
|
frontendDefinition,
|
|
|
|
componentLibraries,
|
|
|
|
uiFunctions,
|
|
|
|
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],
|
|
|
|
}
|
|
|
|
rootTreeNode.rootElement = target
|
2020-02-18 13:29:38 +01:00
|
|
|
const getInitialiseParams = attachChildrenParams(pageStateManager)
|
2020-02-14 12:51:45 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|