2020-02-03 10:24:25 +01:00
|
|
|
import { writable } from "svelte/store"
|
|
|
|
import { createCoreApi } from "./core"
|
|
|
|
import { getStateOrValue } from "./state/getState"
|
|
|
|
import { setState, setStateFromBinding } from "./state/setState"
|
|
|
|
import { trimSlash } from "./common/trimSlash"
|
|
|
|
import { isBound } from "./state/isState"
|
|
|
|
import { _initialiseChildren } from "./render/initialiseChildren"
|
|
|
|
import { createTreeNode } from "./render/renderComponent"
|
2019-09-27 18:03:31 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
export const createApp = (
|
|
|
|
document,
|
|
|
|
componentLibraries,
|
|
|
|
appDefinition,
|
|
|
|
user,
|
|
|
|
uiFunctions
|
|
|
|
) => {
|
|
|
|
const coreApi = createCoreApi(appDefinition, user)
|
|
|
|
appDefinition.hierarchy = coreApi.templateApi.constructHierarchy(
|
|
|
|
appDefinition.hierarchy
|
|
|
|
)
|
|
|
|
const store = writable({
|
|
|
|
_bbuser: user,
|
|
|
|
})
|
2019-09-19 05:35:40 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
let globalState = null
|
|
|
|
store.subscribe(s => {
|
|
|
|
globalState = s
|
|
|
|
})
|
2019-09-27 18:03:31 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const relativeUrl = url =>
|
|
|
|
appDefinition.appRootPath
|
|
|
|
? appDefinition.appRootPath + "/" + trimSlash(url)
|
|
|
|
: url
|
2019-09-27 18:03:31 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const apiCall = method => (url, body) =>
|
|
|
|
fetch(relativeUrl(url), {
|
|
|
|
method: method,
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
body: body && JSON.stringify(body),
|
|
|
|
})
|
2019-09-29 07:40:06 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const api = {
|
|
|
|
post: apiCall("POST"),
|
|
|
|
get: apiCall("GET"),
|
|
|
|
patch: apiCall("PATCH"),
|
|
|
|
delete: apiCall("DELETE"),
|
|
|
|
}
|
2019-09-29 07:40:06 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const safeCallEvent = (event, context) => {
|
|
|
|
const isFunction = obj =>
|
|
|
|
!!(obj && obj.constructor && obj.call && obj.apply)
|
2019-09-29 07:40:06 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
if (isFunction(event)) event(context)
|
|
|
|
}
|
2019-09-29 07:40:06 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const initialiseChildrenParams = (hydrate, treeNode) => ({
|
|
|
|
bb,
|
|
|
|
coreApi,
|
|
|
|
store,
|
|
|
|
document,
|
|
|
|
componentLibraries,
|
|
|
|
appDefinition,
|
|
|
|
hydrate,
|
|
|
|
uiFunctions,
|
|
|
|
treeNode,
|
|
|
|
})
|
2019-10-07 07:03:41 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const bb = (treeNode, componentProps) => ({
|
|
|
|
hydrateChildren: _initialiseChildren(
|
|
|
|
initialiseChildrenParams(true, treeNode)
|
|
|
|
),
|
|
|
|
appendChildren: _initialiseChildren(
|
|
|
|
initialiseChildrenParams(false, treeNode)
|
|
|
|
),
|
|
|
|
insertChildren: (props, htmlElement, anchor) =>
|
|
|
|
_initialiseChildren(initialiseChildrenParams(false, treeNode))(
|
|
|
|
props,
|
|
|
|
htmlElement,
|
|
|
|
anchor
|
|
|
|
),
|
|
|
|
context: treeNode.context,
|
|
|
|
props: componentProps,
|
|
|
|
call: safeCallEvent,
|
|
|
|
setStateFromBinding: (binding, value) =>
|
|
|
|
setStateFromBinding(store, binding, value),
|
|
|
|
setState: (path, value) => setState(store, path, value),
|
|
|
|
getStateOrValue: (prop, currentContext) =>
|
|
|
|
getStateOrValue(globalState, prop, currentContext),
|
|
|
|
store,
|
|
|
|
relativeUrl,
|
|
|
|
api,
|
|
|
|
isBound,
|
|
|
|
parent,
|
|
|
|
})
|
2019-09-19 05:35:40 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
return bb(createTreeNode())
|
2019-09-19 05:35:40 +02:00
|
|
|
}
|