|
|
|
@ -24,7 +24,7 @@ import { cloneDeep, difference } from "lodash/fp" |
|
|
|
* @returns {Array.<BindableProperty>} |
|
|
|
*/ |
|
|
|
export default function({ componentInstanceId, screen, components, tables }) { |
|
|
|
const walkResult = walk({ |
|
|
|
const result = walk({ |
|
|
|
// cloning so we are free to mutate props (e.g. by adding _contexts)
|
|
|
|
instance: cloneDeep(screen.props), |
|
|
|
targetId: componentInstanceId, |
|
|
|
@ -33,13 +33,10 @@ export default function({ componentInstanceId, screen, components, tables }) { |
|
|
|
}) |
|
|
|
|
|
|
|
return [ |
|
|
|
...walkResult.bindableInstances |
|
|
|
.filter(isInstanceInSharedContext(walkResult)) |
|
|
|
.map(componentInstanceToBindable(walkResult)), |
|
|
|
|
|
|
|
...(walkResult.target?._contexts |
|
|
|
.map(contextToBindables(tables, walkResult)) |
|
|
|
.flat() ?? []), |
|
|
|
...result.bindableInstances |
|
|
|
.filter(isInstanceInSharedContext(result)) |
|
|
|
.map(componentInstanceToBindable), |
|
|
|
...(result.target?._contexts.map(contextToBindables(tables)).flat() ?? []), |
|
|
|
] |
|
|
|
} |
|
|
|
|
|
|
|
@ -53,26 +50,18 @@ const isInstanceInSharedContext = walkResult => i => |
|
|
|
|
|
|
|
// turns a component instance prop into binding expressions
|
|
|
|
// used by the UI
|
|
|
|
const componentInstanceToBindable = walkResult => i => { |
|
|
|
const lastContext = |
|
|
|
i.instance._contexts.length && |
|
|
|
i.instance._contexts[i.instance._contexts.length - 1] |
|
|
|
const contextParentPath = lastContext |
|
|
|
? getParentPath(walkResult, lastContext) |
|
|
|
: "" |
|
|
|
|
|
|
|
const componentInstanceToBindable = i => { |
|
|
|
return { |
|
|
|
type: "instance", |
|
|
|
instance: i.instance, |
|
|
|
// how the binding expression persists, and is used in the app at runtime
|
|
|
|
runtimeBinding: `${contextParentPath}${i.instance._id}.${i.prop}`, |
|
|
|
runtimeBinding: `${i.instance._id}`, |
|
|
|
// how the binding exressions looks to the user of the builder
|
|
|
|
readableBinding: `${i.instance._instanceName}`, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
const contextToBindables = (tables, walkResult) => context => { |
|
|
|
const contextParentPath = getParentPath(walkResult, context) |
|
|
|
const contextToBindables = tables => context => { |
|
|
|
const tableId = context.table?.tableId ?? context.table |
|
|
|
const table = tables.find(table => table._id === tableId) |
|
|
|
let schema = |
|
|
|
@ -98,7 +87,7 @@ const contextToBindables = (tables, walkResult) => context => { |
|
|
|
fieldSchema, |
|
|
|
instance: context.instance, |
|
|
|
// how the binding expression persists, and is used in the app at runtime
|
|
|
|
runtimeBinding: `${contextParentPath}data.${runtimeBoundKey}`, |
|
|
|
runtimeBinding: `${context.instance._id}.${runtimeBoundKey}`, |
|
|
|
// how the binding expressions looks to the user of the builder
|
|
|
|
readableBinding: `${context.instance._instanceName}.${table.name}.${key}`, |
|
|
|
// table / view info
|
|
|
|
@ -118,20 +107,6 @@ const contextToBindables = (tables, walkResult) => context => { |
|
|
|
) |
|
|
|
} |
|
|
|
|
|
|
|
const getParentPath = (walkResult, context) => { |
|
|
|
// describes the number of "parent" in the path
|
|
|
|
// clone array first so original array is not mtated
|
|
|
|
const contextParentNumber = [...walkResult.target._contexts] |
|
|
|
.reverse() |
|
|
|
.indexOf(context) |
|
|
|
|
|
|
|
return ( |
|
|
|
new Array(contextParentNumber).fill("parent").join(".") + |
|
|
|
// trailing . if has parents
|
|
|
|
(contextParentNumber ? "." : "") |
|
|
|
) |
|
|
|
} |
|
|
|
|
|
|
|
const walk = ({ instance, targetId, components, tables, result }) => { |
|
|
|
if (!result) { |
|
|
|
result = { |
|
|
|
|