2020-11-13 16:42:32 +01:00
|
|
|
<script>
|
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-18 20:18:18 +01:00
|
|
|
$: componentProps = extractValidProps(definition)
|
2020-11-17 13:08:24 +01:00
|
|
|
$: children = definition._children
|
|
|
|
$: componentName = extractComponentName(definition._component)
|
2020-11-18 20:18:18 +01:00
|
|
|
$: constructor = getComponentConstructor(componentName)
|
2020-11-17 13:08:24 +01:00
|
|
|
$: id = `${componentName}-${definition._id}`
|
|
|
|
$: styles = { ...definition._styles, id }
|
|
|
|
|
|
|
|
// 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
|
|
|
|
const extractValidProps = component => {
|
|
|
|
let props = {}
|
|
|
|
Object.entries(component)
|
|
|
|
.filter(([name]) => !name.startsWith("_"))
|
|
|
|
.forEach(([key, value]) => {
|
|
|
|
props[key] = value
|
|
|
|
})
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
$: console.log("Rendering: " + componentName)
|
2020-11-13 16:42:32 +01:00
|
|
|
</script>
|
|
|
|
|
2020-11-17 13:08:24 +01:00
|
|
|
{#if constructor}
|
|
|
|
<svelte:component this={constructor} {...componentProps} {styles}>
|
|
|
|
{#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}
|