2019-09-19 05:35:40 +02:00
|
|
|
import {
|
|
|
|
split,
|
|
|
|
last
|
|
|
|
} from "lodash/fp";
|
|
|
|
import {writable} from "svelte/store";
|
|
|
|
import { $ } from "./core/common";
|
2019-09-27 18:03:31 +02:00
|
|
|
import {
|
|
|
|
setupBinding
|
|
|
|
} from "./state/stateBinding";
|
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";
|
2019-09-27 18:03:31 +02:00
|
|
|
|
2019-09-23 07:08:06 +02:00
|
|
|
export const createApp = (componentLibraries, appDefinition, user) => {
|
2019-09-19 05:35:40 +02:00
|
|
|
|
2020-01-18 00:06:42 +01:00
|
|
|
const _initialiseChildren = (parentContext, hydrate) => (childrenProps, htmlElement, context, anchor=null) => {
|
2019-09-19 05:35:40 +02:00
|
|
|
|
2020-01-18 00:06:42 +01:00
|
|
|
const childComponents = [];
|
2019-09-19 05:35:40 +02:00
|
|
|
|
2020-01-24 14:18:31 +01:00
|
|
|
if(hydrate) {
|
|
|
|
while (htmlElement.firstChild) {
|
|
|
|
htmlElement.removeChild(htmlElement.firstChild);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-18 00:06:42 +01:00
|
|
|
for(let childProps of childrenProps) {
|
|
|
|
const {componentName, libName} = splitName(childProps._component);
|
2019-09-22 06:02:33 +02:00
|
|
|
|
2020-01-18 00:06:42 +01:00
|
|
|
if(!componentName || !libName) return;
|
2019-10-18 18:32:03 +02:00
|
|
|
|
2020-01-18 00:06:42 +01:00
|
|
|
const {initialProps, bind} = setupBinding(
|
|
|
|
store, childProps, coreApi,
|
|
|
|
context || parentContext, appDefinition.appRootPath);
|
2019-09-22 06:02:33 +02:00
|
|
|
|
2020-01-18 00:06:42 +01:00
|
|
|
|
|
|
|
const componentProps = {
|
|
|
|
...initialProps,
|
|
|
|
_bb:bb(context || parentContext, childProps)
|
|
|
|
};
|
|
|
|
|
|
|
|
const component = new (componentLibraries[libName][componentName])({
|
|
|
|
target: htmlElement,
|
|
|
|
props: componentProps,
|
2020-01-24 14:18:31 +01:00
|
|
|
hydrate:false,
|
2020-01-18 00:06:42 +01:00
|
|
|
anchor
|
|
|
|
});
|
2019-09-19 05:35:40 +02:00
|
|
|
|
2020-01-18 00:06:42 +01:00
|
|
|
bind(component);
|
|
|
|
childComponents.push(component);
|
|
|
|
}
|
2019-09-19 05:35:40 +02:00
|
|
|
|
2020-01-18 00:06:42 +01:00
|
|
|
return childComponents;
|
2019-09-19 05:35:40 +02: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 = {
|
|
|
|
post: apiCall("POST"),
|
|
|
|
get: apiCall("GET"),
|
|
|
|
patch: apiCall("PATCH"),
|
|
|
|
delete:apiCall("DELETE")
|
|
|
|
};
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-10-19 08:24:20 +02:00
|
|
|
const bb = (context, props) => ({
|
2020-01-18 00:06:42 +01:00
|
|
|
hydrateChildren: _initialiseChildren(context, true),
|
|
|
|
appendChildren: _initialiseChildren(context, false),
|
|
|
|
insertChildren: (props, htmlElement, anchor, context) =>
|
|
|
|
_initialiseChildren(context, false)(props, htmlElement, context, anchor),
|
2019-09-27 18:03:31 +02:00
|
|
|
store,
|
2019-09-29 07:40:06 +02:00
|
|
|
relativeUrl,
|
|
|
|
api,
|
2019-10-07 07:03:41 +02:00
|
|
|
call:safeCallEvent,
|
2019-10-10 07:18:02 +02:00
|
|
|
isBound,
|
|
|
|
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),
|
2019-10-18 18:32:03 +02:00
|
|
|
context,
|
|
|
|
props
|
2019-10-01 06:57:45 +02:00
|
|
|
});
|
|
|
|
|
2019-10-10 07:18:02 +02:00
|
|
|
return bb();
|
2019-09-19 05:35:40 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-10-18 18:32:03 +02:00
|
|
|
const buildBindings = (boundProps, boundArrays, contextBoundProps) => {
|
|
|
|
const bindings = {};
|
|
|
|
if(boundProps && boundProps.length > 0) {
|
|
|
|
for(let p of boundProps) {
|
|
|
|
bindings[p.propName] = {
|
|
|
|
path: p.path,
|
|
|
|
fallback: p.fallback,
|
|
|
|
source: p.source
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(contextBoundProps && contextBoundProps.length > 0) {
|
|
|
|
for(let p of contextBoundProps) {
|
|
|
|
bindings[p.propName] = {
|
|
|
|
path: p.path,
|
|
|
|
fallback: p.fallback,
|
|
|
|
source: p.source
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(boundArrays && boundArrays.length > 0) {
|
|
|
|
for(let a of boundArrays) {
|
|
|
|
const arrayOfBindings = [];
|
|
|
|
|
|
|
|
for(let b of a.arrayOfBindings) {
|
|
|
|
arrayOfBindings.push(
|
|
|
|
buildBindings(
|
|
|
|
b.boundProps,
|
|
|
|
b.boundArrays,
|
|
|
|
b.contextBoundProps)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
bindings[a.propName] = arrayOfBindings;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return bindings;
|
|
|
|
}
|
|
|
|
|
2019-09-19 05:35:40 +02:00
|
|
|
|
|
|
|
const splitName = fullname => {
|
|
|
|
const componentName = $(fullname, [
|
|
|
|
split("/"),
|
|
|
|
last
|
|
|
|
]);
|
|
|
|
|
|
|
|
const libName =fullname.substring(
|
|
|
|
0, fullname.length - componentName.length - 1);
|
|
|
|
|
|
|
|
return {libName, componentName};
|
2019-09-29 07:40:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|