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-22 06:02:33 +02:00
|
|
|
import { setupBinding } from "./state/stateBinding";
|
2019-09-23 07:08:06 +02:00
|
|
|
import { createCoreApi } from "./core";
|
2019-09-19 05:35:40 +02:00
|
|
|
|
2019-09-23 07:08:06 +02:00
|
|
|
export const createApp = (componentLibraries, appDefinition, user) => {
|
2019-09-19 05:35:40 +02:00
|
|
|
|
|
|
|
const initialiseComponent = (props, htmlElement) => {
|
|
|
|
|
|
|
|
const {componentName, libName} = splitName(props._component);
|
|
|
|
|
2019-09-22 06:02:33 +02:00
|
|
|
if(!componentName || !libName) return;
|
|
|
|
|
2019-09-23 07:08:06 +02:00
|
|
|
const {initialProps, bind} = setupBinding(store, props, coreApi);
|
2019-09-22 06:02:33 +02:00
|
|
|
|
2019-09-19 05:35:40 +02:00
|
|
|
const component = new (componentLibraries[libName][componentName])({
|
|
|
|
target: htmlElement,
|
2019-09-26 06:40:58 +02:00
|
|
|
props: {...initialProps, _app},
|
|
|
|
hydrate:true
|
2019-09-19 05:35:40 +02:00
|
|
|
});
|
|
|
|
|
2019-09-22 06:02:33 +02:00
|
|
|
bind(component);
|
2019-09-19 05:35:40 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-09-23 07:08:06 +02:00
|
|
|
const coreApi = createCoreApi(appDefinition, user);
|
2019-09-26 06:40:58 +02:00
|
|
|
const store = writable({
|
|
|
|
_bbuser: user
|
|
|
|
});
|
2019-09-19 05:35:40 +02:00
|
|
|
|
|
|
|
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};
|
|
|
|
}
|