budibase/packages/client/src/state/stateManager.js

66 lines
1.4 KiB
JavaScript
Raw Normal View History

2020-09-10 22:11:05 +02:00
import { eventHandlers } from "./eventHandlers"
import { bbFactory } from "./bbComponentApi"
import renderTemplateString from "./renderTemplateString"
2020-08-06 22:12:35 +02:00
import appStore from "./store"
import hasBinding from "./hasBinding"
const doNothing = () => {}
doNothing.isPlaceholder = true
const isMetaProp = propName =>
propName === "_component" ||
propName === "_children" ||
propName === "_id" ||
propName === "_style" ||
propName === "_code" ||
2020-05-29 15:06:10 +02:00
propName === "_codeMeta" ||
propName === "_styles"
export const createStateManager = ({
componentLibraries,
onScreenSlotRendered,
routeTo,
}) => {
2020-09-10 22:11:05 +02:00
let runEventActions = eventHandlers(routeTo)
2020-05-30 01:14:41 +02:00
const bb = bbFactory({
componentLibraries,
onScreenSlotRendered,
2020-09-10 22:11:05 +02:00
runEventActions,
})
2020-09-10 22:11:05 +02:00
const setup = _setup(bb)
2020-05-30 01:14:41 +02:00
return {
setup,
2020-06-01 22:26:32 +02:00
destroy: () => {},
}
}
2020-09-10 22:11:05 +02:00
const _setup = bb => node => {
const props = node.props
const initialProps = { ...props }
for (let propName in props) {
if (isMetaProp(propName)) continue
2020-02-21 19:02:02 +01:00
const propValue = props[propName]
2020-08-06 22:12:35 +02:00
const isBound = hasBinding(propValue)
2020-02-21 19:02:02 +01:00
2020-05-30 01:14:41 +02:00
if (isBound) {
2020-08-06 22:12:35 +02:00
const state = appStore.getState(node.contextStoreKey)
initialProps[propName] = renderTemplateString(propValue, state)
2020-05-30 01:14:41 +02:00
if (!node.stateBound) {
node.stateBound = true
}
2020-05-29 15:06:10 +02:00
}
}
2020-09-10 22:11:05 +02:00
const setup = _setup(bb)
initialProps._bb = bb(node, setup)
return initialProps
}