2020-08-07 17:13:57 +02:00
|
|
|
<script>
|
|
|
|
import { onMount } from "svelte"
|
2020-09-29 14:54:04 +02:00
|
|
|
import {
|
|
|
|
Input,
|
|
|
|
TextArea,
|
|
|
|
Button,
|
|
|
|
Select,
|
|
|
|
Toggle,
|
|
|
|
Label,
|
|
|
|
} from "@budibase/bbui"
|
2020-08-13 21:22:14 +02:00
|
|
|
import { cloneDeep, merge } from "lodash/fp"
|
2020-08-07 17:13:57 +02:00
|
|
|
import { store, backendUiStore } from "builderStore"
|
|
|
|
import { FIELDS } from "constants/backend"
|
|
|
|
import { notifier } from "builderStore/store/notifications"
|
|
|
|
import ButtonGroup from "components/common/ButtonGroup.svelte"
|
|
|
|
import NumberBox from "components/common/NumberBox.svelte"
|
|
|
|
import ValuesList from "components/common/ValuesList.svelte"
|
|
|
|
import ErrorsBox from "components/common/ErrorsBox.svelte"
|
|
|
|
import Checkbox from "components/common/Checkbox.svelte"
|
|
|
|
import ActionButton from "components/common/ActionButton.svelte"
|
|
|
|
import DatePicker from "components/common/DatePicker.svelte"
|
|
|
|
import LinkedRecordSelector from "components/common/LinkedRecordSelector.svelte"
|
|
|
|
import * as api from "../api"
|
|
|
|
|
2020-08-20 12:19:13 +02:00
|
|
|
let fieldDefinitions = cloneDeep(FIELDS)
|
|
|
|
|
2020-08-07 17:13:57 +02:00
|
|
|
export let onClosed
|
2020-08-20 12:19:13 +02:00
|
|
|
export let field = {
|
|
|
|
type: "string",
|
|
|
|
constraints: fieldDefinitions.STRING.constraints,
|
|
|
|
}
|
2020-08-07 17:13:57 +02:00
|
|
|
|
2020-08-10 16:34:37 +02:00
|
|
|
let originalName = field.name
|
2020-10-09 19:49:23 +02:00
|
|
|
$: tableOptions = $backendUiStore.tables.filter(
|
|
|
|
table => table._id !== $backendUiStore.draftTable._id
|
2020-09-29 19:27:35 +02:00
|
|
|
)
|
2020-10-07 11:45:26 +02:00
|
|
|
$: required = !!field?.constraints?.presence
|
2020-08-07 18:41:20 +02:00
|
|
|
|
2020-08-07 17:13:57 +02:00
|
|
|
async function saveColumn() {
|
2020-08-07 18:41:20 +02:00
|
|
|
backendUiStore.update(state => {
|
2020-10-09 19:49:23 +02:00
|
|
|
backendUiStore.actions.tables.saveField({
|
2020-08-07 18:41:20 +02:00
|
|
|
originalName,
|
2020-08-11 12:23:07 +02:00
|
|
|
field,
|
2020-08-07 18:41:20 +02:00
|
|
|
})
|
|
|
|
return state
|
2020-08-07 17:13:57 +02:00
|
|
|
})
|
|
|
|
onClosed()
|
|
|
|
}
|
2020-08-20 12:19:13 +02:00
|
|
|
|
|
|
|
function handleFieldConstraints(event) {
|
|
|
|
const { type, constraints } = fieldDefinitions[
|
|
|
|
event.target.value.toUpperCase()
|
|
|
|
]
|
|
|
|
field.type = type
|
|
|
|
field.constraints = constraints
|
|
|
|
}
|
2020-10-07 11:45:26 +02:00
|
|
|
|
|
|
|
function onChangeRequired(e) {
|
|
|
|
const req = e.target.checked
|
|
|
|
field.constraints.presence = req ? { allowEmpty: false } : false
|
|
|
|
required = req
|
|
|
|
}
|
2020-08-07 17:13:57 +02:00
|
|
|
</script>
|
|
|
|
|
2020-09-25 14:11:57 +02:00
|
|
|
<div class="actions">
|
2020-09-29 14:54:04 +02:00
|
|
|
<Input label="Name" thin bind:value={field.name} />
|
2020-08-07 17:13:57 +02:00
|
|
|
|
2020-08-20 12:19:13 +02:00
|
|
|
<Select
|
|
|
|
secondary
|
|
|
|
thin
|
2020-09-29 14:54:04 +02:00
|
|
|
label="Type"
|
2020-08-20 12:19:13 +02:00
|
|
|
on:change={handleFieldConstraints}
|
2020-08-20 16:49:18 +02:00
|
|
|
bind:value={field.type}>
|
2020-08-13 21:22:14 +02:00
|
|
|
{#each Object.values(fieldDefinitions) as field}
|
2020-08-20 16:49:18 +02:00
|
|
|
<option value={field.type}>{field.name}</option>
|
2020-08-07 17:13:57 +02:00
|
|
|
{/each}
|
|
|
|
</Select>
|
|
|
|
|
2020-10-01 12:01:06 +02:00
|
|
|
{#if field.type !== 'link'}
|
|
|
|
<Toggle
|
2020-10-07 11:45:26 +02:00
|
|
|
checked={required}
|
|
|
|
on:change={onChangeRequired}
|
2020-10-01 12:01:06 +02:00
|
|
|
thin
|
|
|
|
text="Required" />
|
|
|
|
{/if}
|
2020-08-07 17:13:57 +02:00
|
|
|
|
2020-09-29 19:27:35 +02:00
|
|
|
{#if field.type === 'string'}
|
2020-09-29 14:54:04 +02:00
|
|
|
<Input
|
|
|
|
thin
|
|
|
|
type="number"
|
2020-09-25 12:35:32 +02:00
|
|
|
label="Max Length"
|
|
|
|
bind:value={field.constraints.length.maximum} />
|
2020-09-29 19:27:35 +02:00
|
|
|
{:else if field.type === 'options'}
|
|
|
|
<ValuesList
|
|
|
|
label="Options (one per line)"
|
|
|
|
bind:values={field.constraints.inclusion} />
|
|
|
|
{:else if field.type === 'datetime'}
|
2020-09-25 12:35:32 +02:00
|
|
|
<DatePicker
|
|
|
|
label="Earliest"
|
|
|
|
bind:value={field.constraints.datetime.earliest} />
|
|
|
|
<DatePicker label="Latest" bind:value={field.constraints.datetime.latest} />
|
2020-09-29 19:27:35 +02:00
|
|
|
{:else if field.type === 'number'}
|
2020-09-29 14:54:04 +02:00
|
|
|
<Input
|
|
|
|
thin
|
|
|
|
type="number"
|
2020-09-25 12:35:32 +02:00
|
|
|
label="Min Value"
|
|
|
|
bind:value={field.constraints.numericality.greaterThanOrEqualTo} />
|
2020-09-29 14:54:04 +02:00
|
|
|
<Input
|
|
|
|
thin
|
|
|
|
type="number"
|
2020-09-25 12:35:32 +02:00
|
|
|
label="Max Value"
|
|
|
|
bind:value={field.constraints.numericality.lessThanOrEqualTo} />
|
|
|
|
{:else if field.type === 'link'}
|
2020-10-09 19:49:23 +02:00
|
|
|
<Select label="Table" thin secondary bind:value={field.tableId}>
|
2020-09-29 19:27:35 +02:00
|
|
|
<option value="">Choose an option</option>
|
2020-10-09 19:49:23 +02:00
|
|
|
{#each tableOptions as table}
|
|
|
|
<option value={table._id}>{table.name}</option>
|
2020-09-29 19:27:35 +02:00
|
|
|
{/each}
|
|
|
|
</Select>
|
|
|
|
<Input
|
|
|
|
label={`Column Name in Other Table`}
|
|
|
|
thin
|
|
|
|
bind:value={field.fieldName} />
|
2020-09-25 12:35:32 +02:00
|
|
|
{/if}
|
|
|
|
<footer>
|
2020-08-07 17:13:57 +02:00
|
|
|
<Button secondary on:click={onClosed}>Cancel</Button>
|
|
|
|
<Button primary on:click={saveColumn}>Save Column</Button>
|
2020-09-25 12:35:32 +02:00
|
|
|
</footer>
|
|
|
|
</div>
|
2020-08-07 17:13:57 +02:00
|
|
|
|
|
|
|
<style>
|
2020-09-25 14:11:57 +02:00
|
|
|
.actions {
|
2020-09-25 12:35:32 +02:00
|
|
|
padding: var(--spacing-xl);
|
2020-08-07 17:13:57 +02:00
|
|
|
display: grid;
|
|
|
|
grid-gap: var(--spacing-xl);
|
2020-09-25 12:35:32 +02:00
|
|
|
min-width: 400px;
|
2020-08-07 17:13:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
footer {
|
2020-09-25 12:35:32 +02:00
|
|
|
display: flex;
|
|
|
|
justify-content: flex-end;
|
|
|
|
gap: var(--spacing-m);
|
2020-08-07 17:13:57 +02:00
|
|
|
}
|
|
|
|
</style>
|