2020-08-07 17:13:57 +02:00
|
|
|
<script>
|
|
|
|
import { onMount } from "svelte"
|
|
|
|
import { Input, TextArea, Button, Select } 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-08-07 18:41:20 +02:00
|
|
|
|
2020-08-07 17:13:57 +02:00
|
|
|
$: required =
|
|
|
|
field.constraints &&
|
|
|
|
field.constraints.presence &&
|
|
|
|
!field.constraints.presence.allowEmpty
|
|
|
|
|
|
|
|
async function saveColumn() {
|
2020-08-07 18:41:20 +02:00
|
|
|
backendUiStore.update(state => {
|
|
|
|
backendUiStore.actions.models.saveField({
|
|
|
|
originalName,
|
2020-08-11 12:23:07 +02:00
|
|
|
field,
|
2020-08-07 18:41:20 +02:00
|
|
|
})
|
2020-08-07 17:13:57 +02:00
|
|
|
|
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()
|
|
|
|
]
|
2020-08-20 16:49:18 +02:00
|
|
|
|
2020-08-20 12:19:13 +02:00
|
|
|
field.type = type
|
|
|
|
field.constraints = constraints
|
|
|
|
}
|
2020-08-07 17:13:57 +02:00
|
|
|
</script>
|
|
|
|
|
2020-09-25 12:35:32 +02:00
|
|
|
<div class="container">
|
2020-08-10 16:34:37 +02:00
|
|
|
<Input placeholder="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
|
|
|
|
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-09-25 12:35:32 +02:00
|
|
|
<div class="field">
|
|
|
|
<label>Required</label>
|
|
|
|
<input
|
|
|
|
type="checkbox"
|
|
|
|
bind:checked={required}
|
|
|
|
on:change={() => (field.constraints.presence.allowEmpty = required)} />
|
2020-08-07 17:13:57 +02:00
|
|
|
</div>
|
|
|
|
|
2020-09-25 12:35:32 +02:00
|
|
|
{#if field.type === 'string' && field.constraints}
|
|
|
|
<NumberBox
|
|
|
|
label="Max Length"
|
|
|
|
bind:value={field.constraints.length.maximum} />
|
|
|
|
<ValuesList label="Categories" bind:values={field.constraints.inclusion} />
|
|
|
|
{:else if field.type === 'datetime' && field.constraints}
|
|
|
|
<DatePicker
|
|
|
|
label="Earliest"
|
|
|
|
bind:value={field.constraints.datetime.earliest} />
|
|
|
|
<DatePicker label="Latest" bind:value={field.constraints.datetime.latest} />
|
|
|
|
{:else if field.type === 'number' && field.constraints}
|
|
|
|
<NumberBox
|
|
|
|
label="Min Value"
|
|
|
|
bind:value={field.constraints.numericality.greaterThanOrEqualTo} />
|
|
|
|
<NumberBox
|
|
|
|
label="Max Value"
|
|
|
|
bind:value={field.constraints.numericality.lessThanOrEqualTo} />
|
|
|
|
{:else if field.type === 'link'}
|
|
|
|
<div class="field">
|
|
|
|
<label>Link</label>
|
|
|
|
<select class="budibase__input" bind:value={field.modelId}>
|
|
|
|
<option value="">Choose an option</option>
|
|
|
|
{#each $backendUiStore.models as model}
|
|
|
|
{#if model._id !== $backendUiStore.draftModel._id}
|
|
|
|
<option value={model._id}>{model.name}</option>
|
|
|
|
{/if}
|
|
|
|
{/each}
|
|
|
|
</select>
|
|
|
|
</div>
|
|
|
|
{/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 12:35:32 +02:00
|
|
|
.container {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
.field {
|
2020-08-11 18:58:01 +02:00
|
|
|
display: grid;
|
|
|
|
grid-template-columns: auto 20px 1fr;
|
|
|
|
align-items: center;
|
|
|
|
grid-gap: 5px;
|
|
|
|
font-size: 14px;
|
|
|
|
font-weight: 500;
|
|
|
|
font-family: var(--font-normal);
|
2020-08-07 17:13:57 +02:00
|
|
|
}
|
|
|
|
</style>
|