1
0
Fork 0
mirror of synced 2024-09-19 18:59:06 +12:00
budibase/packages/client/src/createApp.js

55 lines
1.2 KiB
JavaScript
Raw Normal View History

import {
split,
last
} from "lodash/fp";
import {writable} from "svelte/store";
import { $ } from "./core/common";
2019-09-22 16:02:33 +12:00
import { setupBinding } from "./state/stateBinding";
2019-09-23 17:08:06 +12:00
import { createCoreApi } from "./core";
2019-09-23 17:08:06 +12:00
export const createApp = (componentLibraries, appDefinition, user) => {
const initialiseComponent = (props, htmlElement) => {
const {componentName, libName} = splitName(props._component);
2019-09-22 16:02:33 +12:00
if(!componentName || !libName) return;
2019-09-23 17:08:06 +12:00
const {initialProps, bind} = setupBinding(store, props, coreApi);
2019-09-22 16:02:33 +12:00
const component = new (componentLibraries[libName][componentName])({
target: htmlElement,
2019-09-26 16:40:58 +12:00
props: {...initialProps, _app},
hydrate:true
});
2019-09-22 16:02:33 +12:00
bind(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
});
const _app = {
initialiseComponent,
store
};
return _app;
}
const splitName = fullname => {
const componentName = $(fullname, [
split("/"),
last
]);
const libName =fullname.substring(
0, fullname.length - componentName.length - 1);
return {libName, componentName};
}