2021-01-26 21:55:44 +13:00
|
|
|
<script>
|
2021-11-17 06:52:00 +13:00
|
|
|
import { getContext } from "svelte"
|
2021-06-10 22:17:14 +12:00
|
|
|
import InnerForm from "./InnerForm.svelte"
|
2022-01-20 23:16:13 +13:00
|
|
|
import { Helpers } from "@budibase/bbui"
|
2021-01-26 21:55:44 +13:00
|
|
|
|
2021-03-20 02:09:22 +13:00
|
|
|
export let dataSource
|
2021-01-26 21:55:44 +13:00
|
|
|
export let theme
|
|
|
|
export let size
|
2021-02-18 04:16:44 +13:00
|
|
|
export let disabled = false
|
2021-06-09 23:55:17 +12:00
|
|
|
export let actionType = "Create"
|
2021-01-26 21:55:44 +13:00
|
|
|
|
2022-04-04 19:49:01 +12:00
|
|
|
// Not exposed as a builder setting. Used internally to disable validation
|
|
|
|
// for fields rendered in things like search blocks.
|
|
|
|
export let disableValidation = false
|
|
|
|
|
2022-07-22 22:10:20 +12:00
|
|
|
// Not exposed as a builder setting. Used internally to allow searching on
|
|
|
|
// auto columns.
|
|
|
|
export let editAutoColumns = false
|
|
|
|
|
2021-02-02 07:51:22 +13:00
|
|
|
const context = getContext("context")
|
2021-12-17 21:22:04 +13:00
|
|
|
const { API, fetchDatasourceSchema } = getContext("sdk")
|
2021-08-19 23:53:31 +12:00
|
|
|
|
|
|
|
let loaded = false
|
|
|
|
let schema
|
|
|
|
let table
|
2021-01-26 21:55:44 +13:00
|
|
|
|
2021-11-17 06:42:27 +13:00
|
|
|
$: fetchSchema(dataSource)
|
2022-03-19 02:35:06 +13:00
|
|
|
$: fetchTable(dataSource)
|
2021-11-17 06:42:27 +13:00
|
|
|
|
2021-02-17 04:30:20 +13:00
|
|
|
// Returns the closes data context which isn't a built in context
|
2021-06-09 23:53:12 +12:00
|
|
|
const getInitialValues = (type, dataSource, context) => {
|
|
|
|
// Only inherit values for update forms
|
|
|
|
if (type !== "Update") {
|
|
|
|
return {}
|
|
|
|
}
|
2021-06-25 23:15:45 +12:00
|
|
|
// Only inherit values for forms targeting internal tables
|
2021-06-09 23:53:12 +12:00
|
|
|
if (!dataSource?.tableId) {
|
|
|
|
return {}
|
|
|
|
}
|
|
|
|
// Don't inherit values representing built in contexts
|
2021-02-17 04:30:20 +13:00
|
|
|
if (["user", "url"].includes(context.closestComponentId)) {
|
|
|
|
return {}
|
|
|
|
}
|
2022-09-14 23:11:20 +12:00
|
|
|
// Always inherit the closest datasource
|
2021-06-09 23:53:12 +12:00
|
|
|
const closestContext = context[`${context.closestComponentId}`] || {}
|
2021-08-20 04:18:41 +12:00
|
|
|
return closestContext || {}
|
2021-02-17 04:30:20 +13:00
|
|
|
}
|
|
|
|
|
2021-11-04 00:57:47 +13:00
|
|
|
// Fetches the form schema from this form's dataSource
|
2021-11-17 06:42:27 +13:00
|
|
|
const fetchSchema = async dataSource => {
|
2021-11-04 00:57:47 +13:00
|
|
|
if (!dataSource) {
|
2021-08-19 23:53:31 +12:00
|
|
|
schema = {}
|
|
|
|
}
|
2021-11-04 00:57:47 +13:00
|
|
|
|
|
|
|
// If the datasource is a query, then we instead use a schema of the query
|
|
|
|
// parameters rather than the output schema
|
|
|
|
else if (
|
|
|
|
dataSource.type === "query" &&
|
|
|
|
dataSource._id &&
|
|
|
|
actionType === "Create"
|
|
|
|
) {
|
2022-01-20 22:57:57 +13:00
|
|
|
try {
|
|
|
|
const query = await API.fetchQueryDefinition(dataSource._id)
|
|
|
|
let paramSchema = {}
|
|
|
|
const params = query.parameters || []
|
|
|
|
params.forEach(param => {
|
|
|
|
paramSchema[param.name] = { ...param, type: "string" }
|
|
|
|
})
|
|
|
|
schema = paramSchema
|
|
|
|
} catch (error) {
|
|
|
|
schema = {}
|
|
|
|
}
|
2021-11-04 00:57:47 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
// For all other cases, just grab the normal schema
|
|
|
|
else {
|
2021-12-17 21:22:04 +13:00
|
|
|
const dataSourceSchema = await fetchDatasourceSchema(dataSource)
|
2021-11-04 00:57:47 +13:00
|
|
|
schema = dataSourceSchema || {}
|
|
|
|
}
|
|
|
|
|
2021-11-17 06:42:27 +13:00
|
|
|
if (!loaded) {
|
|
|
|
loaded = true
|
|
|
|
}
|
2021-08-19 23:53:31 +12:00
|
|
|
}
|
|
|
|
|
2022-03-19 02:35:06 +13:00
|
|
|
const fetchTable = async dataSource => {
|
2022-04-13 21:27:32 +12:00
|
|
|
if (dataSource?.tableId && dataSource?.type !== "query") {
|
2022-03-19 02:35:06 +13:00
|
|
|
try {
|
|
|
|
table = await API.fetchTableDefinition(dataSource.tableId)
|
|
|
|
} catch (error) {
|
|
|
|
table = null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-10 22:17:14 +12:00
|
|
|
$: initialValues = getInitialValues(actionType, dataSource, $context)
|
2022-01-20 22:57:57 +13:00
|
|
|
$: resetKey = Helpers.hashString(
|
2021-11-17 06:42:27 +13:00
|
|
|
JSON.stringify(initialValues) + JSON.stringify(schema)
|
|
|
|
)
|
2021-01-26 21:55:44 +13:00
|
|
|
</script>
|
|
|
|
|
2021-08-19 23:53:31 +12:00
|
|
|
{#if loaded}
|
|
|
|
{#key resetKey}
|
|
|
|
<InnerForm
|
|
|
|
{dataSource}
|
|
|
|
{theme}
|
|
|
|
{size}
|
|
|
|
{disabled}
|
|
|
|
{actionType}
|
|
|
|
{schema}
|
|
|
|
{table}
|
|
|
|
{initialValues}
|
2022-04-04 19:49:01 +12:00
|
|
|
{disableValidation}
|
2022-07-22 22:10:20 +12:00
|
|
|
{editAutoColumns}
|
2021-08-19 23:53:31 +12:00
|
|
|
>
|
|
|
|
<slot />
|
|
|
|
</InnerForm>
|
|
|
|
{/key}
|
|
|
|
{/if}
|