2020-11-13 16:42:32 +01:00
|
|
|
<script>
|
2020-11-20 10:50:10 +01:00
|
|
|
import { getContext, setContext } from "svelte"
|
2021-01-27 16:52:12 +01:00
|
|
|
import { writable, get } from "svelte/store"
|
2021-09-01 12:41:48 +02:00
|
|
|
import * as AppComponents from "components/app"
|
2020-11-18 20:18:18 +01:00
|
|
|
import Router from "./Router.svelte"
|
2021-09-01 12:41:48 +02:00
|
|
|
import { enrichProps, propsAreSame } from "utils/componentProps"
|
|
|
|
import { builderStore } from "stores"
|
|
|
|
import { hashString } from "utils/helpers"
|
|
|
|
import Manifest from "manifest.json"
|
|
|
|
import { getActiveConditions, reduceConditionActions } from "utils/conditions"
|
|
|
|
import Placeholder from "components/app/Placeholder.svelte"
|
2020-11-13 16:42:32 +01:00
|
|
|
|
2021-06-11 09:05:49 +02:00
|
|
|
export let instance = {}
|
2020-11-13 16:42:32 +01:00
|
|
|
|
2021-07-21 15:03:49 +02:00
|
|
|
// The enriched component settings
|
|
|
|
let enrichedSettings
|
|
|
|
|
|
|
|
// Any prop overrides that need to be applied due to conditional UI
|
|
|
|
let conditionalSettings
|
2020-11-13 16:42:32 +01:00
|
|
|
|
2021-01-27 16:52:12 +01:00
|
|
|
// Props are hashed when inside the builder preview and used as a key, so that
|
|
|
|
// components fully remount whenever any props change
|
|
|
|
let propsHash = 0
|
|
|
|
|
2021-01-29 14:22:38 +01:00
|
|
|
// Latest timestamp that we started a props update.
|
|
|
|
// Due to enrichment now being async, we need to avoid overwriting newer
|
|
|
|
// props with old ones, depending on how long enrichment takes.
|
|
|
|
let latestUpdateTime
|
|
|
|
|
2021-06-25 16:04:27 +02:00
|
|
|
// Keep track of stringified representations of context and instance
|
|
|
|
// to avoid enriching bindings as much as possible
|
|
|
|
let lastContextKey
|
|
|
|
let lastInstanceKey
|
|
|
|
|
2021-07-21 15:03:49 +02:00
|
|
|
// Visibility flag used by conditional UI
|
|
|
|
let visible = true
|
|
|
|
|
2021-01-06 11:13:30 +01:00
|
|
|
// Get contexts
|
2021-02-01 19:51:22 +01:00
|
|
|
const context = getContext("context")
|
2021-06-11 14:34:37 +02:00
|
|
|
const insideScreenslot = !!getContext("screenslot")
|
2020-11-18 20:18:18 +01:00
|
|
|
|
2020-11-25 10:50:51 +01:00
|
|
|
// Create component context
|
|
|
|
const componentStore = writable({})
|
|
|
|
setContext("component", componentStore)
|
2020-11-17 13:08:24 +01:00
|
|
|
|
2021-06-11 09:05:49 +02:00
|
|
|
// Extract component instance info
|
|
|
|
$: constructor = getComponentConstructor(instance._component)
|
|
|
|
$: definition = getComponentDefinition(instance._component)
|
|
|
|
$: children = instance._children || []
|
|
|
|
$: id = instance._id
|
|
|
|
$: name = instance._instanceName
|
|
|
|
$: empty =
|
2021-06-11 09:49:06 +02:00
|
|
|
!children.length &&
|
|
|
|
definition?.hasChildren &&
|
|
|
|
definition?.showEmptyState !== false &&
|
|
|
|
$builderStore.inBuilder
|
2021-06-25 16:04:27 +02:00
|
|
|
$: rawProps = getRawProps(instance)
|
|
|
|
$: instanceKey = JSON.stringify(rawProps)
|
|
|
|
$: updateComponentProps(rawProps, instanceKey, $context)
|
2021-06-08 09:00:54 +02:00
|
|
|
$: selected =
|
|
|
|
$builderStore.inBuilder &&
|
2021-06-11 09:05:49 +02:00
|
|
|
$builderStore.selectedComponentId === instance._id
|
2021-08-23 15:01:57 +02:00
|
|
|
$: inSelectedPath = $builderStore.selectedComponentPath?.includes(id)
|
2021-06-11 14:34:37 +02:00
|
|
|
$: interactive = $builderStore.previewType === "layout" || insideScreenslot
|
2021-07-21 15:03:49 +02:00
|
|
|
$: evaluateConditions(enrichedSettings?._conditions)
|
|
|
|
$: componentSettings = { ...enrichedSettings, ...conditionalSettings }
|
2021-01-06 11:13:30 +01:00
|
|
|
|
2020-11-24 12:02:10 +01:00
|
|
|
// Update component context
|
2021-01-22 12:08:42 +01:00
|
|
|
$: componentStore.set({
|
|
|
|
id,
|
|
|
|
children: children.length,
|
2021-06-11 14:34:37 +02:00
|
|
|
styles: { ...instance._styles, id, empty, interactive },
|
2021-06-11 09:05:49 +02:00
|
|
|
empty,
|
2021-06-08 09:00:54 +02:00
|
|
|
selected,
|
2021-06-11 09:05:49 +02:00
|
|
|
name,
|
2021-01-22 12:08:42 +01:00
|
|
|
})
|
2020-11-25 10:50:51 +01:00
|
|
|
|
2021-06-25 16:04:27 +02:00
|
|
|
const getRawProps = instance => {
|
|
|
|
let validProps = {}
|
|
|
|
Object.entries(instance)
|
2021-07-21 15:03:49 +02:00
|
|
|
.filter(([name]) => name === "_conditions" || !name.startsWith("_"))
|
2021-06-25 16:04:27 +02:00
|
|
|
.forEach(([key, value]) => {
|
|
|
|
validProps[key] = value
|
|
|
|
})
|
|
|
|
return validProps
|
|
|
|
}
|
|
|
|
|
2021-01-29 14:22:38 +01:00
|
|
|
// Gets the component constructor for the specified component
|
|
|
|
const getComponentConstructor = component => {
|
|
|
|
const split = component?.split("/")
|
|
|
|
const name = split?.[split.length - 1]
|
|
|
|
if (name === "screenslot" && $builderStore.previewType !== "layout") {
|
|
|
|
return Router
|
|
|
|
}
|
2021-09-01 12:41:48 +02:00
|
|
|
return AppComponents[name]
|
2021-01-29 14:22:38 +01:00
|
|
|
}
|
|
|
|
|
2021-06-11 09:05:49 +02:00
|
|
|
const getComponentDefinition = component => {
|
|
|
|
const prefix = "@budibase/standard-components/"
|
|
|
|
const type = component?.replace(prefix, "")
|
|
|
|
return type ? Manifest[type] : null
|
|
|
|
}
|
|
|
|
|
2021-01-29 14:22:38 +01:00
|
|
|
// Enriches any string component props using handlebars
|
2021-06-25 16:04:27 +02:00
|
|
|
const updateComponentProps = (rawProps, instanceKey, context) => {
|
|
|
|
const instanceSame = instanceKey === lastInstanceKey
|
|
|
|
const contextSame = context.key === lastContextKey
|
|
|
|
|
|
|
|
if (instanceSame && contextSame) {
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
lastInstanceKey = instanceKey
|
|
|
|
lastContextKey = context.key
|
|
|
|
}
|
|
|
|
|
2021-01-29 14:22:38 +01:00
|
|
|
// Record the timestamp so we can reference it after enrichment
|
|
|
|
latestUpdateTime = Date.now()
|
|
|
|
const enrichmentTime = latestUpdateTime
|
|
|
|
|
|
|
|
// Enrich props with context
|
2021-06-25 16:04:27 +02:00
|
|
|
const enrichedProps = enrichProps(rawProps, context)
|
2021-01-29 14:22:38 +01:00
|
|
|
|
|
|
|
// Abandon this update if a newer update has started
|
|
|
|
if (enrichmentTime !== latestUpdateTime) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the component props.
|
|
|
|
// Most props are deeply compared so that svelte will only trigger reactive
|
|
|
|
// statements on props that have actually changed.
|
|
|
|
if (!enrichedProps) {
|
2021-01-21 12:31:45 +01:00
|
|
|
return
|
|
|
|
}
|
2021-01-27 16:52:12 +01:00
|
|
|
let propsChanged = false
|
2021-07-21 15:03:49 +02:00
|
|
|
if (!enrichedSettings) {
|
|
|
|
enrichedSettings = {}
|
2021-01-27 16:52:12 +01:00
|
|
|
propsChanged = true
|
2021-01-21 12:31:45 +01:00
|
|
|
}
|
2021-01-29 14:22:38 +01:00
|
|
|
Object.keys(enrichedProps).forEach(key => {
|
2021-07-21 15:03:49 +02:00
|
|
|
if (!propsAreSame(enrichedProps[key], enrichedSettings[key])) {
|
2021-01-27 16:52:12 +01:00
|
|
|
propsChanged = true
|
2021-07-21 15:03:49 +02:00
|
|
|
enrichedSettings[key] = enrichedProps[key]
|
2021-01-21 11:41:30 +01:00
|
|
|
}
|
|
|
|
})
|
2021-01-29 14:22:38 +01:00
|
|
|
|
|
|
|
// Update the hash if we're in the builder so we can fully remount this
|
|
|
|
// component
|
2021-01-27 16:52:12 +01:00
|
|
|
if (get(builderStore).inBuilder && propsChanged) {
|
2021-07-21 15:03:49 +02:00
|
|
|
propsHash = hashString(JSON.stringify(enrichedSettings))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const evaluateConditions = conditions => {
|
|
|
|
if (!conditions?.length) {
|
|
|
|
return
|
2021-01-27 16:52:12 +01:00
|
|
|
}
|
2021-07-21 15:03:49 +02:00
|
|
|
|
|
|
|
// Default visible to false if there is a show condition
|
2021-07-26 14:16:45 +02:00
|
|
|
let nextVisible = !conditions.find(condition => condition.action === "show")
|
2021-07-21 15:03:49 +02:00
|
|
|
|
2021-07-26 14:16:45 +02:00
|
|
|
// Execute conditions and determine settings and visibility changes
|
2021-07-21 15:03:49 +02:00
|
|
|
const activeConditions = getActiveConditions(conditions)
|
|
|
|
const result = reduceConditionActions(activeConditions)
|
|
|
|
if (result.visible != null) {
|
|
|
|
nextVisible = result.visible
|
|
|
|
}
|
|
|
|
|
2021-07-26 14:16:45 +02:00
|
|
|
// Update state from condition results
|
|
|
|
conditionalSettings = result.settingUpdates
|
2021-07-21 15:03:49 +02:00
|
|
|
visible = nextVisible
|
2021-01-21 11:41:30 +01:00
|
|
|
}
|
2020-11-13 16:42:32 +01:00
|
|
|
</script>
|
|
|
|
|
2021-07-21 15:03:49 +02:00
|
|
|
{#key propsHash}
|
2021-08-23 15:01:57 +02:00
|
|
|
{#if constructor && componentSettings && (visible || inSelectedPath)}
|
2021-07-21 15:03:49 +02:00
|
|
|
<div
|
|
|
|
class={`component ${id}`}
|
|
|
|
data-type={interactive ? "component" : ""}
|
|
|
|
data-id={id}
|
|
|
|
data-name={name}
|
|
|
|
>
|
|
|
|
<svelte:component this={constructor} {...componentSettings}>
|
2021-06-08 09:00:54 +02:00
|
|
|
{#if children.length}
|
|
|
|
{#each children as child (child._id)}
|
2021-06-11 09:05:49 +02:00
|
|
|
<svelte:self instance={child} />
|
2021-06-08 09:00:54 +02:00
|
|
|
{/each}
|
2021-06-11 09:05:49 +02:00
|
|
|
{:else if empty}
|
2021-06-11 10:14:22 +02:00
|
|
|
<Placeholder />
|
2021-06-08 09:00:54 +02:00
|
|
|
{/if}
|
|
|
|
</svelte:component>
|
2021-07-21 15:03:49 +02:00
|
|
|
</div>
|
|
|
|
{/if}
|
|
|
|
{/key}
|
2021-06-08 09:00:54 +02:00
|
|
|
|
|
|
|
<style>
|
2021-06-11 09:05:49 +02:00
|
|
|
.component {
|
2021-06-08 09:00:54 +02:00
|
|
|
display: contents;
|
|
|
|
}
|
|
|
|
</style>
|