1
0
Fork 0
mirror of synced 2024-09-20 03:08:18 +12:00
budibase/packages/client/src/createApp.js

132 lines
3.5 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 17:08:06 +12:00
import { createCoreApi } from "./core";
import { getStateOrValue } from "./state/getState";
2019-10-10 18:18:02 +13:00
import { setState, setStateFromBinding } from "./state/setState";
import { trimSlash } from "./common/trimSlash";
2019-10-10 18:18:02 +13:00
import { isBound } from "./state/isState";
2019-09-23 17:08:06 +12:00
export const createApp = (componentLibraries, appDefinition, user) => {
const _initialiseComponent = (parentContext, hydrate) => (props, htmlElement, context, anchor=null) => {
const {componentName, libName} = splitName(props._component);
2019-09-22 16:02:33 +12:00
if(!componentName || !libName) return;
2019-10-14 20:32:20 +13:00
const {initialProps, bind, boundProps} = setupBinding(
store, props, coreApi, context || parentContext, appDefinition.appRootPath);
2019-10-10 18:18:02 +13:00
const bindings = {};
if(boundProps && boundProps.length > 0) {
for(let p of boundProps) {
bindings[p.propName] = {
path: p.path,
fallback: p.fallback,
source: p.source
}
}
}
const componentProps = {
...initialProps,
_bb:bb(bindings, context || parentContext)
};
2019-09-22 16:02:33 +12:00
const component = new (componentLibraries[libName][componentName])({
target: htmlElement,
2019-10-10 18:18:02 +13:00
props: componentProps,
hydrate,
anchor
});
2019-09-22 16:02:33 +12:00
bind(component);
2019-10-01 17:57:45 +13:00
return component;
}
2019-09-23 17:08:06 +12:00
const coreApi = createCoreApi(appDefinition, user);
2019-09-26 16:40:58 +12: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 18:03:41 +13:00
const safeCallEvent = (event, context) => {
const isFunction = (obj) =>
!!(obj && obj.constructor && obj.call && obj.apply);
if(isFunction(event)) event(context);
}
2019-10-10 18:18:02 +13:00
const bb = (bindings, context) => ({
hydrateComponent: _initialiseComponent(context, true),
appendComponent: _initialiseComponent(context, false),
insertComponent: (props, htmlElement, anchor, context) =>
_initialiseComponent(context, false)(props, htmlElement, context, anchor),
store,
relativeUrl,
api,
2019-10-07 18:03:41 +13:00
call:safeCallEvent,
2019-10-10 18:18:02 +13:00
isBound,
setStateFromBinding: (binding, value) => setStateFromBinding(store, binding, value),
setState: (path, value) => setState(store, path, value),
getStateOrValue: (prop, currentContext) =>
2019-10-10 18:18:02 +13:00
getStateOrValue(globalState, prop, currentContext),
bindings,
context,
2019-10-01 17:57:45 +13:00
});
2019-10-10 18:18:02 +13:00
return bb();
}
const splitName = fullname => {
const componentName = $(fullname, [
split("/"),
last
]);
const libName =fullname.substring(
0, fullname.length - componentName.length - 1);
return {libName, componentName};
}