2021-11-08 15:35:58 +01:00
|
|
|
<script context="module">
|
|
|
|
let SettingsDefinitionCache = {}
|
|
|
|
</script>
|
|
|
|
|
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 = {}
|
2021-09-16 08:52:49 +02:00
|
|
|
export let isLayout = false
|
|
|
|
export let isScreen = false
|
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-11-08 15:35:58 +01:00
|
|
|
// Settings are hashed when inside the builder preview and used as a key,
|
|
|
|
// so that components fully remount whenever any settings change
|
|
|
|
let hash = 0
|
2021-01-27 16:52:12 +01:00
|
|
|
|
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
|
2021-11-08 15:35:58 +01:00
|
|
|
// settings with old ones, depending on how long enrichment takes.
|
2021-01-29 14:22:38 +01:00
|
|
|
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")
|
2021-11-02 09:45:27 +01:00
|
|
|
const insideBlock = !!getContext("block")
|
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)
|
2021-11-08 15:35:58 +01:00
|
|
|
$: settingsDefinition = getSettingsDefinition(definition)
|
2021-06-11 09:05:49 +02:00
|
|
|
$: children = instance._children || []
|
|
|
|
$: id = instance._id
|
|
|
|
$: name = instance._instanceName
|
2021-11-08 15:35:58 +01:00
|
|
|
|
|
|
|
// Determine if the component is selected or is part of the critical path
|
|
|
|
// leading to the selected component
|
|
|
|
$: selected =
|
|
|
|
$builderStore.inBuilder && $builderStore.selectedComponentId === id
|
|
|
|
$: inSelectedPath = $builderStore.selectedComponentPath?.includes(id)
|
|
|
|
|
|
|
|
// Interactive components can be selected, dragged and highlighted inside
|
|
|
|
// the builder preview
|
2021-09-20 16:34:51 +02:00
|
|
|
$: interactive =
|
|
|
|
$builderStore.inBuilder &&
|
2021-11-02 09:45:27 +01:00
|
|
|
($builderStore.previewType === "layout" || insideScreenslot) &&
|
|
|
|
!insideBlock
|
2021-11-08 15:35:58 +01:00
|
|
|
$: draggable = interactive && !isLayout && !isScreen
|
|
|
|
$: droppable = interactive && !isLayout && !isScreen
|
|
|
|
|
|
|
|
// Empty components are those which accept children but do not have any.
|
|
|
|
// Empty states can be shown for these components, but can be disabled
|
|
|
|
// in the component manifest.
|
2021-09-20 16:34:51 +02:00
|
|
|
$: empty = interactive && !children.length && definition?.hasChildren
|
|
|
|
$: emptyState = empty && definition?.showEmptyState !== false
|
2021-11-08 15:35:58 +01:00
|
|
|
|
|
|
|
// Raw props are all props excluding internal props and children
|
|
|
|
$: rawSettings = getRawSettings(instance)
|
|
|
|
$: instanceKey = hashString(JSON.stringify(rawSettings))
|
|
|
|
|
|
|
|
// Component settings are those which are intended for this component and
|
|
|
|
// which need to be enriched
|
|
|
|
$: componentSettings = getComponentSettings(rawSettings, settingsDefinition)
|
|
|
|
$: enrichComponentSettings(rawSettings, instanceKey, $context)
|
|
|
|
|
|
|
|
// Nested settings are those which are intended for child components inside
|
|
|
|
// blocks and which should not be enriched at this level
|
|
|
|
$: nestedSettings = getNestedSettings(rawSettings, settingsDefinition)
|
|
|
|
|
|
|
|
// Evaluate conditional UI settings and store any component setting changes
|
|
|
|
// which need to be made
|
2021-07-21 15:03:49 +02:00
|
|
|
$: evaluateConditions(enrichedSettings?._conditions)
|
2021-11-08 15:35:58 +01:00
|
|
|
|
|
|
|
// Build up the final settings object to be passed to the component
|
|
|
|
$: settings = {
|
|
|
|
...enrichedSettings,
|
|
|
|
...nestedSettings,
|
|
|
|
...conditionalSettings,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Render key is used when in the builder preview to fully remount
|
|
|
|
// components when settings are changed
|
|
|
|
$: renderKey = `${hash}-${emptyState}`
|
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-09-20 16:34:51 +02:00
|
|
|
styles: { ...instance._styles, id, empty: emptyState, interactive },
|
|
|
|
empty: emptyState,
|
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-11-08 15:35:58 +01:00
|
|
|
const getRawSettings = instance => {
|
|
|
|
let validSettings = {}
|
2021-06-25 16:04:27 +02:00
|
|
|
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]) => {
|
2021-11-08 15:35:58 +01:00
|
|
|
validSettings[key] = value
|
2021-06-25 16:04:27 +02:00
|
|
|
})
|
2021-11-08 15:35:58 +01:00
|
|
|
return validSettings
|
2021-06-25 16:04:27 +02:00
|
|
|
}
|
|
|
|
|
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-11-08 15:35:58 +01:00
|
|
|
const getSettingsDefinition = definition => {
|
|
|
|
if (!definition) {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
if (SettingsDefinitionCache[definition.name]) {
|
|
|
|
return SettingsDefinitionCache[definition.name]
|
|
|
|
}
|
|
|
|
let settings = []
|
|
|
|
definition.settings?.forEach(setting => {
|
|
|
|
if (setting.section) {
|
|
|
|
settings = settings.concat(setting.settings || [])
|
|
|
|
} else {
|
|
|
|
settings.push(setting)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
SettingsDefinitionCache[definition] = settings
|
|
|
|
return settings
|
|
|
|
}
|
|
|
|
|
|
|
|
const getComponentSettings = (rawSettings, settingsDefinition) => {
|
|
|
|
let clone = { ...rawSettings }
|
|
|
|
settingsDefinition?.forEach(setting => {
|
|
|
|
if (setting.nested) {
|
|
|
|
delete clone[setting.key]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return clone
|
|
|
|
}
|
|
|
|
|
|
|
|
const getNestedSettings = (rawSettings, settingsDefinition) => {
|
|
|
|
let clone = { ...rawSettings }
|
|
|
|
settingsDefinition?.forEach(setting => {
|
|
|
|
if (!setting.nested) {
|
|
|
|
delete clone[setting.key]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return clone
|
|
|
|
}
|
|
|
|
|
2021-01-29 14:22:38 +01:00
|
|
|
// Enriches any string component props using handlebars
|
2021-11-08 15:35:58 +01:00
|
|
|
const enrichComponentSettings = (rawSettings, instanceKey, context) => {
|
2021-06-25 16:04:27 +02:00
|
|
|
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
|
|
|
|
|
2021-11-08 15:35:58 +01:00
|
|
|
// Enrich settings with context
|
|
|
|
const newEnrichedSettings = enrichProps(rawSettings, 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.
|
2021-11-08 15:35:58 +01:00
|
|
|
if (!newEnrichedSettings) {
|
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-11-08 15:35:58 +01:00
|
|
|
Object.keys(newEnrichedSettings).forEach(key => {
|
|
|
|
if (!propsAreSame(newEnrichedSettings[key], enrichedSettings[key])) {
|
2021-01-27 16:52:12 +01:00
|
|
|
propsChanged = true
|
2021-11-08 15:35:58 +01:00
|
|
|
enrichedSettings[key] = newEnrichedSettings[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-11-08 15:35:58 +01:00
|
|
|
hash = hashString(JSON.stringify(enrichedSettings))
|
2021-07-21 15:03:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-09-20 16:41:20 +02:00
|
|
|
{#key renderKey}
|
2021-11-08 15:35:58 +01:00
|
|
|
{#if constructor && settings && (visible || inSelectedPath)}
|
2021-09-20 16:41:20 +02:00
|
|
|
<!-- The ID is used as a class because getElementsByClassName is O(1) -->
|
|
|
|
<!-- and the performance matters for the selection indicators -->
|
|
|
|
<div
|
|
|
|
class={`component ${id}`}
|
|
|
|
class:draggable
|
|
|
|
class:droppable
|
|
|
|
class:empty
|
|
|
|
class:interactive
|
|
|
|
data-id={id}
|
|
|
|
data-name={name}
|
|
|
|
>
|
2021-11-08 15:35:58 +01:00
|
|
|
<svelte:component this={constructor} {...settings}>
|
2021-09-20 16:41:20 +02:00
|
|
|
{#if children.length}
|
|
|
|
{#each children as child (child._id)}
|
|
|
|
<svelte:self instance={child} />
|
|
|
|
{/each}
|
|
|
|
{:else if emptyState}
|
|
|
|
<Placeholder />
|
2021-11-02 09:45:27 +01:00
|
|
|
{:else if insideBlock}
|
|
|
|
<slot />
|
2021-09-20 16:41:20 +02:00
|
|
|
{/if}
|
|
|
|
</svelte:component>
|
|
|
|
</div>
|
|
|
|
{/if}
|
2021-07-21 15:03:49 +02:00
|
|
|
{/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;
|
|
|
|
}
|
2021-09-20 16:34:51 +02:00
|
|
|
.interactive :global(*:hover) {
|
|
|
|
cursor: pointer;
|
|
|
|
}
|
|
|
|
.draggable :global(*:hover) {
|
2021-09-20 09:26:44 +02:00
|
|
|
cursor: grab;
|
2021-09-16 08:28:59 +02:00
|
|
|
}
|
2021-06-08 09:00:54 +02:00
|
|
|
</style>
|