2020-11-13 16:42:32 +01:00
|
|
|
<script>
|
2020-11-20 10:50:10 +01:00
|
|
|
import { getContext, setContext } from "svelte"
|
2020-11-18 20:18:18 +01:00
|
|
|
import * as ComponentLibrary from "@budibase/standard-components"
|
|
|
|
import Router from "./Router.svelte"
|
2020-11-13 16:42:32 +01:00
|
|
|
|
2020-11-17 13:08:24 +01:00
|
|
|
export let definition = {}
|
2020-11-13 16:42:32 +01:00
|
|
|
|
2020-11-17 13:08:24 +01:00
|
|
|
// Extracts the actual component name from the library name
|
2020-11-18 20:18:18 +01:00
|
|
|
const extractComponentName = name => {
|
|
|
|
const split = name?.split("/")
|
|
|
|
return split?.[split.length - 1]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extracts valid props to pass to the real svelte component
|
2020-11-20 10:50:10 +01:00
|
|
|
const extractValidProps = component => {
|
2020-11-18 20:18:18 +01:00
|
|
|
let props = {}
|
|
|
|
Object.entries(component)
|
|
|
|
.filter(([name]) => !name.startsWith("_"))
|
|
|
|
.forEach(([key, value]) => {
|
2020-11-20 10:50:10 +01:00
|
|
|
props[key] = value
|
2020-11-18 20:18:18 +01:00
|
|
|
})
|
|
|
|
return props
|
|
|
|
}
|
|
|
|
|
|
|
|
// Gets the component constructor for the specified component
|
|
|
|
const getComponentConstructor = name => {
|
|
|
|
return name === "screenslot" ? Router : ComponentLibrary[componentName]
|
2020-11-17 13:08:24 +01:00
|
|
|
}
|
|
|
|
|
2020-11-20 10:50:10 +01:00
|
|
|
// Extract component definition info
|
|
|
|
const componentName = extractComponentName(definition._component)
|
|
|
|
const constructor = getComponentConstructor(componentName)
|
|
|
|
const componentProps = extractValidProps(definition)
|
|
|
|
const dataContext = getContext("data")
|
|
|
|
const enrichedProps = dataContext.actions.enrichDataBindings(componentProps)
|
|
|
|
const children = definition._children
|
|
|
|
|
2020-11-20 11:49:39 +01:00
|
|
|
// Set contexts to be consumed by component
|
|
|
|
setContext("style", { ...definition._styles, id: definition._id })
|
2020-11-13 16:42:32 +01:00
|
|
|
</script>
|
|
|
|
|
2020-11-17 13:08:24 +01:00
|
|
|
{#if constructor}
|
2020-11-20 10:50:10 +01:00
|
|
|
<svelte:component this={constructor} {...enrichedProps}>
|
2020-11-17 13:08:24 +01:00
|
|
|
{#if children && children.length}
|
|
|
|
{#each children as child}
|
|
|
|
<svelte:self definition={child} />
|
|
|
|
{/each}
|
|
|
|
{/if}
|
|
|
|
</svelte:component>
|
2020-11-13 16:42:32 +01:00
|
|
|
{/if}
|