Merge branch 'control-flow'

This commit is contained in:
Michael Shanks 2020-01-28 21:28:36 +00:00
commit 54202e6344
4 changed files with 44 additions and 80 deletions

View File

@ -50,74 +50,32 @@ export const createApp = (componentLibraries, appDefinition, user, uiFunctions)
if(isFunction(event)) event(context); if(isFunction(event)) event(context);
} }
const initialiseChildrenParams = (parentContext, hydrate) => ({ const initialiseChildrenParams = (hydrate, parentContext, childIndex) => ({
bb, coreApi, store, bb, coreApi, store, parentContext,
componentLibraries, appDefinition, componentLibraries, appDefinition,
parentContext, hydrate, uiFunctions hydrate, uiFunctions, childIndex
}); });
const bb = (context, props) => ({ const bb = (componentProps, componentContext, childIndex) => ({
hydrateChildren: _initialiseChildren(initialiseChildrenParams(context, true)), hydrateChildren: _initialiseChildren(initialiseChildrenParams(true, componentContext, childIndex)),
appendChildren: _initialiseChildren(initialiseChildrenParams(context, false)), appendChildren: _initialiseChildren(initialiseChildrenParams(false, componentContext, childIndex)),
insertChildren: (props, htmlElement, anchor, context) => insertChildren: (props, htmlElement, anchor) =>
_initialiseChildren(initialiseChildrenParams(context, false)) _initialiseChildren(initialiseChildrenParams(false, componentContext, childIndex))
(props, htmlElement, context, anchor), (props, htmlElement, anchor),
store, context: componentContext,
relativeUrl, props: componentProps,
api,
call:safeCallEvent, call:safeCallEvent,
isBound,
setStateFromBinding: (binding, value) => setStateFromBinding(store, binding, value), setStateFromBinding: (binding, value) => setStateFromBinding(store, binding, value),
setState: (path, value) => setState(store, path, value), setState: (path, value) => setState(store, path, value),
getStateOrValue: (prop, currentContext) => getStateOrValue: (prop, currentContext) =>
getStateOrValue(globalState, prop, currentContext), getStateOrValue(globalState, prop, currentContext),
context, store,
props relativeUrl,
api,
isBound,
parent
}); });
return bb(); return bb();
} }
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;
}

View File

@ -35,16 +35,16 @@ export const loadBudibase = async ({
props = appDefinition.props; props = appDefinition.props;
} }
const _app = createApp( const app = createApp(
componentLibraries, componentLibraries,
appDefinition, appDefinition,
user, user,
uiFunctions || {}); uiFunctions || {});
_app.hydrateChildren( app.hydrateChildren(
[props], [props],
window.document.body); window.document.body);
return _app; return app;
}; };
if(window) { if(window) {

View File

@ -9,10 +9,10 @@ import { $ } from "../core/common";
import { renderComponent } from "./renderComponent"; import { renderComponent } from "./renderComponent";
export const _initialiseChildren = (initialiseOpts) => export const _initialiseChildren = (initialiseOpts) =>
(childrenProps, htmlElement, context, anchor=null) => { (childrenProps, htmlElement, anchor=null) => {
const { uiFunctions, bb, coreApi, const { uiFunctions, bb, coreApi,
store, componentLibraries, store, componentLibraries, childIndex,
appDefinition, parentContext, hydrate } = initialiseOpts; appDefinition, parentContext, hydrate } = initialiseOpts;
const childComponents = []; const childComponents = [];
@ -23,6 +23,7 @@ export const _initialiseChildren = (initialiseOpts) =>
} }
} }
let childIndex = 0;
for(let childProps of childrenProps) { for(let childProps of childrenProps) {
const {componentName, libName} = splitName(childProps._component); const {componentName, libName} = splitName(childProps._component);
@ -30,25 +31,25 @@ export const _initialiseChildren = (initialiseOpts) =>
if(!componentName || !libName) return; if(!componentName || !libName) return;
const {initialProps, bind} = setupBinding( const {initialProps, bind} = setupBinding(
store, childProps, coreApi, store, childProps, coreApi,
context || parentContext, appDefinition.appRootPath); appDefinition.appRootPath);
/// here needs to go inside renderComponent ???
const componentProps = {
...initialProps,
_bb:bb(context || parentContext, childProps)
};
const componentConstructor = componentLibraries[libName][componentName]; const componentConstructor = componentLibraries[libName][componentName];
const {component} = renderComponent({ const {component, context, lastChildIndex} = renderComponent({
componentConstructor,uiFunctions, componentConstructor,uiFunctions,
htmlElement, anchor, htmlElement, anchor, childIndex,
parentContext, componentProps}); parentContext, initialProps, bb});
childIndex = lastChildIndex;
bind(component);
childComponents.push(component); const unsubscribe = bind(component);
childComponents.push({
component,
context,
unsubscribe
});
} }
return childComponents; return childComponents;

View File

@ -2,9 +2,9 @@
export const renderComponent = ({ export const renderComponent = ({
componentConstructor, uiFunctions, componentConstructor, uiFunctions,
htmlElement, anchor, parentContext, htmlElement, anchor, parentContext,
componentProps}) => { initialProps, bb, childIndex}) => {
const func = componentProps._id const func = initialProps._id
? uiFunctions[componentProps._id] ? uiFunctions[componentProps._id]
: undefined; : undefined;
@ -19,12 +19,16 @@ export const renderComponent = ({
componentContext = parentContext; componentContext = parentContext;
} }
initialProps._bb = bb(initialProps, componentContext);
component = new componentConstructor({ component = new componentConstructor({
target: htmlElement, target: htmlElement,
props: componentProps, props: initialProps,
hydrate:false, hydrate:false,
anchor anchor
}); });
childIndex += 1;
} }
if(func) { if(func) {
@ -35,6 +39,7 @@ export const renderComponent = ({
return ({ return ({
context: componentContext, context: componentContext,
lastChildIndex: childIndex,
component component
}); });
} }