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