budibase/packages/client/src/createApp.js

177 lines
4.8 KiB
JavaScript
Raw Normal View History

import {
split,
last
} from "lodash/fp";
import {writable} from "svelte/store";
import { $ } from "./core/common";
import {
setupBinding
} from "./state/stateBinding";
2019-09-23 07:08:06 +02:00
import { createCoreApi } from "./core";
import { getStateOrValue } from "./state/getState";
2019-10-10 07:18:02 +02:00
import { setState, setStateFromBinding } from "./state/setState";
import { trimSlash } from "./common/trimSlash";
2019-10-10 07:18:02 +02:00
import { isBound } from "./state/isState";
2019-09-23 07:08:06 +02:00
export const createApp = (componentLibraries, appDefinition, user) => {
const _initialiseChildren = (parentContext, hydrate) => (childrenProps, htmlElement, context, anchor=null) => {
const childComponents = [];
2020-01-24 14:18:31 +01:00
if(hydrate) {
while (htmlElement.firstChild) {
htmlElement.removeChild(htmlElement.firstChild);
}
}
for(let childProps of childrenProps) {
const {componentName, libName} = splitName(childProps._component);
2019-09-22 06:02:33 +02:00
if(!componentName || !libName) return;
2019-10-18 18:32:03 +02:00
const {initialProps, bind} = setupBinding(
store, childProps, coreApi,
context || parentContext, appDefinition.appRootPath);
2019-09-22 06:02:33 +02: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,
anchor
});
bind(component);
childComponents.push(component);
}
return childComponents;
}
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
});
let globalState = null;
store.subscribe(s => {
globalState = s;
});
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) => ({
hydrateChildren: _initialiseChildren(context, true),
appendChildren: _initialiseChildren(context, false),
insertChildren: (props, htmlElement, anchor, context) =>
_initialiseChildren(context, false)(props, htmlElement, context, anchor),
store,
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),
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-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;
}
const splitName = fullname => {
const componentName = $(fullname, [
split("/"),
last
]);
const libName =fullname.substring(
0, fullname.length - componentName.length - 1);
return {libName, componentName};
}