2020-08-07 17:13:57 +02:00
|
|
|
<script>
|
2021-02-09 19:49:12 +01:00
|
|
|
import {
|
|
|
|
Input,
|
|
|
|
Button,
|
|
|
|
Label,
|
|
|
|
Select,
|
|
|
|
Toggle,
|
2021-02-15 17:12:39 +01:00
|
|
|
Radio,
|
2021-02-09 19:49:12 +01:00
|
|
|
} from "@budibase/bbui"
|
2020-10-24 00:55:51 +02:00
|
|
|
import { cloneDeep } from "lodash/fp"
|
2021-04-01 11:29:47 +02:00
|
|
|
import { tables } from "stores/backend"
|
2021-03-23 11:54:03 +01:00
|
|
|
|
2020-11-24 15:04:14 +01:00
|
|
|
import { TableNames, UNEDITABLE_USER_FIELDS } from "constants"
|
2021-03-01 19:03:33 +01:00
|
|
|
import {
|
|
|
|
FIELDS,
|
|
|
|
AUTO_COLUMN_SUB_TYPES,
|
|
|
|
RelationshipTypes,
|
|
|
|
} from "constants/backend"
|
2021-02-16 17:38:36 +01:00
|
|
|
import { getAutoColumnInformation, buildAutoColumn } from "builderStore/utils"
|
2021-04-09 12:02:53 +02:00
|
|
|
import { notifications } from "@budibase/bbui"
|
2020-08-07 17:13:57 +02:00
|
|
|
import ValuesList from "components/common/ValuesList.svelte"
|
|
|
|
import DatePicker from "components/common/DatePicker.svelte"
|
2020-10-23 18:38:10 +02:00
|
|
|
import ConfirmDialog from "components/common/ConfirmDialog.svelte"
|
2021-03-01 18:06:08 +01:00
|
|
|
import { truncate } from "lodash"
|
2020-08-07 17:13:57 +02:00
|
|
|
|
2021-02-15 20:59:30 +01:00
|
|
|
const AUTO_COL = "auto"
|
2021-02-17 18:30:58 +01:00
|
|
|
const LINK_TYPE = FIELDS.LINK.type
|
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-10-14 20:40:01 +02:00
|
|
|
|
|
|
|
// Initial value for column name in other table for linked records
|
2021-03-23 11:54:03 +01:00
|
|
|
fieldName: $tables.selected.name,
|
2020-08-20 12:19:13 +02:00
|
|
|
}
|
2020-08-07 17:13:57 +02:00
|
|
|
|
2020-08-10 16:34:37 +02:00
|
|
|
let originalName = field.name
|
2020-10-14 20:40:01 +02:00
|
|
|
let primaryDisplay =
|
2021-03-23 11:54:03 +01:00
|
|
|
$tables.selected.primaryDisplay == null ||
|
|
|
|
$tables.selected.primaryDisplay === field.name
|
2021-02-15 17:12:39 +01:00
|
|
|
|
2021-03-23 11:54:03 +01:00
|
|
|
let table = $tables.selected
|
|
|
|
let indexes = [...($tables.selected.indexes || [])]
|
2020-10-23 18:38:10 +02:00
|
|
|
let confirmDeleteDialog
|
2020-10-27 14:04:32 +01:00
|
|
|
let deletion
|
2020-10-23 18:38:10 +02:00
|
|
|
|
2021-03-23 11:54:03 +01:00
|
|
|
$: tableOptions = $tables.list.filter(
|
|
|
|
table => table._id !== $tables.draft._id
|
2020-09-29 19:27:35 +02:00
|
|
|
)
|
2020-10-16 22:50:58 +02:00
|
|
|
$: required = !!field?.constraints?.presence || primaryDisplay
|
2020-11-25 16:30:10 +01:00
|
|
|
$: uneditable =
|
2021-03-23 11:54:03 +01:00
|
|
|
$tables.selected?._id === TableNames.USERS &&
|
2020-11-25 16:30:10 +01:00
|
|
|
UNEDITABLE_USER_FIELDS.includes(field.name)
|
2021-03-15 21:38:55 +01:00
|
|
|
$: invalid =
|
|
|
|
(field.type === LINK_TYPE && !field.tableId) ||
|
2021-04-01 11:29:47 +02:00
|
|
|
Object.keys($tables.draft.schema).some(key => key === field.name)
|
2020-08-07 18:41:20 +02:00
|
|
|
|
2021-02-15 20:59:30 +01:00
|
|
|
// used to select what different options can be displayed for column type
|
2021-02-15 20:59:49 +01:00
|
|
|
$: canBeSearched =
|
2021-02-17 18:30:58 +01:00
|
|
|
field.type !== LINK_TYPE &&
|
2021-02-15 20:59:49 +01:00
|
|
|
field.subtype !== AUTO_COLUMN_SUB_TYPES.CREATED_BY &&
|
|
|
|
field.subtype !== AUTO_COLUMN_SUB_TYPES.UPDATED_BY
|
2021-02-17 18:30:58 +01:00
|
|
|
$: canBeDisplay = field.type !== LINK_TYPE && field.type !== AUTO_COL
|
2021-02-15 20:59:49 +01:00
|
|
|
$: canBeRequired =
|
2021-02-17 18:30:58 +01:00
|
|
|
field.type !== LINK_TYPE && !uneditable && field.type !== AUTO_COL
|
2021-03-01 18:06:08 +01:00
|
|
|
$: relationshipOptions = getRelationshipOptions(field)
|
2021-02-15 20:59:30 +01:00
|
|
|
|
2020-08-07 17:13:57 +02:00
|
|
|
async function saveColumn() {
|
2021-02-15 20:59:30 +01:00
|
|
|
if (field.type === AUTO_COL) {
|
2021-04-01 11:29:47 +02:00
|
|
|
field = buildAutoColumn($tables.draft.name, field.name, field.subtype)
|
2021-02-15 20:59:30 +01:00
|
|
|
}
|
2021-03-23 11:54:03 +01:00
|
|
|
tables.saveField({
|
2021-04-01 11:29:47 +02:00
|
|
|
originalName,
|
|
|
|
field,
|
|
|
|
primaryDisplay,
|
|
|
|
indexes,
|
|
|
|
})
|
2020-08-07 17:13:57 +02:00
|
|
|
onClosed()
|
|
|
|
}
|
2020-08-20 12:19:13 +02:00
|
|
|
|
2020-10-23 18:38:10 +02:00
|
|
|
function deleteColumn() {
|
2021-03-23 11:54:03 +01:00
|
|
|
if (field.name === $tables.selected.primaryDisplay) {
|
2021-04-09 12:02:53 +02:00
|
|
|
notifications.error("You cannot delete the display column")
|
2020-10-23 18:38:10 +02:00
|
|
|
} else {
|
2021-03-23 11:54:03 +01:00
|
|
|
tables.deleteField(field)
|
2021-04-09 12:02:53 +02:00
|
|
|
notifications.success(`Column ${field.name} deleted.`)
|
2020-10-27 14:04:32 +01:00
|
|
|
onClosed()
|
2020-10-23 18:38:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-17 18:30:58 +01:00
|
|
|
function handleTypeChange(event) {
|
2021-02-15 20:59:49 +01:00
|
|
|
const definition = fieldDefinitions[event.target.value.toUpperCase()]
|
2021-02-15 20:59:30 +01:00
|
|
|
if (!definition) {
|
|
|
|
return
|
|
|
|
}
|
2021-02-17 18:30:58 +01:00
|
|
|
// remove any extra fields that may not be related to this type
|
|
|
|
delete field.autocolumn
|
|
|
|
delete field.subtype
|
|
|
|
delete field.tableId
|
2021-03-01 18:06:08 +01:00
|
|
|
delete field.relationshipType
|
|
|
|
// add in defaults and initial definition
|
|
|
|
field.type = definition.type
|
|
|
|
field.constraints = definition.constraints
|
|
|
|
// default relationships many to many
|
|
|
|
if (field.type === LINK_TYPE) {
|
|
|
|
field.relationshipType = RelationshipTypes.MANY_TO_MANY
|
|
|
|
}
|
2020-08-20 12:19:13 +02:00
|
|
|
}
|
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-10-16 22:50:58 +02:00
|
|
|
|
|
|
|
function onChangePrimaryDisplay(e) {
|
|
|
|
const isPrimary = e.target.checked
|
|
|
|
// primary display is always required
|
|
|
|
if (isPrimary) {
|
|
|
|
field.constraints.presence = { allowEmpty: false }
|
|
|
|
}
|
|
|
|
}
|
2020-10-23 18:38:10 +02:00
|
|
|
|
2021-02-09 19:49:12 +01:00
|
|
|
function onChangePrimaryIndex(e) {
|
2021-02-09 19:57:32 +01:00
|
|
|
indexes = e.target.checked ? [field.name] : []
|
2021-02-09 19:49:12 +01:00
|
|
|
}
|
2021-02-01 22:02:54 +01:00
|
|
|
|
2021-02-09 19:49:12 +01:00
|
|
|
function onChangeSecondaryIndex(e) {
|
2021-02-09 19:57:32 +01:00
|
|
|
if (e.target.checked) {
|
2021-02-09 19:49:12 +01:00
|
|
|
indexes[1] = field.name
|
|
|
|
} else {
|
2021-02-09 19:57:32 +01:00
|
|
|
indexes = indexes.slice(0, 1)
|
2021-02-09 19:49:12 +01:00
|
|
|
}
|
2021-02-01 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2020-10-23 18:38:10 +02:00
|
|
|
function confirmDelete() {
|
|
|
|
confirmDeleteDialog.show()
|
2020-10-27 14:04:32 +01:00
|
|
|
deletion = true
|
2020-10-24 00:55:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function hideDeleteDialog() {
|
|
|
|
confirmDeleteDialog.hide()
|
2020-10-27 14:04:32 +01:00
|
|
|
deletion = false
|
2020-10-23 18:38:10 +02:00
|
|
|
}
|
2021-03-01 18:06:08 +01:00
|
|
|
|
|
|
|
function getRelationshipOptions(field) {
|
|
|
|
if (!field || !field.tableId) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
const linkTable = tableOptions.find(table => table._id === field.tableId)
|
|
|
|
if (!linkTable) {
|
|
|
|
return null
|
|
|
|
}
|
2021-03-02 12:10:26 +01:00
|
|
|
const thisName = truncate(table.name, { length: 15 }),
|
|
|
|
linkName = truncate(linkTable.name, { length: 15 })
|
2021-03-01 18:06:08 +01:00
|
|
|
return [
|
2021-03-01 19:03:33 +01:00
|
|
|
{
|
2021-03-05 16:19:33 +01:00
|
|
|
name: `Many ${thisName} rows → many ${linkName} rows`,
|
2021-03-01 19:03:33 +01:00
|
|
|
value: RelationshipTypes.MANY_TO_MANY,
|
|
|
|
},
|
|
|
|
{
|
2021-03-05 16:19:33 +01:00
|
|
|
name: `One ${linkName} row → many ${thisName} rows`,
|
2021-03-01 19:03:33 +01:00
|
|
|
value: RelationshipTypes.ONE_TO_MANY,
|
|
|
|
},
|
|
|
|
{
|
2021-03-05 16:19:33 +01:00
|
|
|
name: `One ${thisName} row → many ${linkName} rows`,
|
2021-03-01 19:03:33 +01:00
|
|
|
value: RelationshipTypes.MANY_TO_ONE,
|
|
|
|
},
|
2021-03-01 18:06:08 +01:00
|
|
|
]
|
|
|
|
}
|
2020-08-07 17:13:57 +02:00
|
|
|
</script>
|
|
|
|
|
2020-10-27 14:04:32 +01:00
|
|
|
<div class="actions" class:hidden={deletion}>
|
2020-11-24 15:04:14 +01:00
|
|
|
<Input label="Name" thin bind:value={field.name} disabled={uneditable} />
|
2020-08-07 17:13:57 +02:00
|
|
|
|
2020-10-14 22:43:36 +02:00
|
|
|
<Select
|
|
|
|
disabled={originalName}
|
|
|
|
secondary
|
|
|
|
thin
|
|
|
|
label="Type"
|
2021-02-17 18:30:58 +01:00
|
|
|
on:change={handleTypeChange}
|
2020-10-14 22:43:36 +02:00
|
|
|
bind:value={field.type}>
|
|
|
|
{#each Object.values(fieldDefinitions) as field}
|
|
|
|
<option value={field.type}>{field.name}</option>
|
|
|
|
{/each}
|
2021-02-15 20:59:30 +01:00
|
|
|
<option value={AUTO_COL}>Auto Column</option>
|
2020-10-14 22:43:36 +02:00
|
|
|
</Select>
|
2020-08-07 17:13:57 +02:00
|
|
|
|
2021-02-15 20:59:30 +01:00
|
|
|
{#if canBeRequired}
|
2020-10-01 12:01:06 +02:00
|
|
|
<Toggle
|
2020-10-07 11:45:26 +02:00
|
|
|
checked={required}
|
|
|
|
on:change={onChangeRequired}
|
2020-10-16 22:50:58 +02:00
|
|
|
disabled={primaryDisplay}
|
2020-10-01 12:01:06 +02:00
|
|
|
thin
|
|
|
|
text="Required" />
|
|
|
|
{/if}
|
2020-08-07 17:13:57 +02:00
|
|
|
|
2021-02-15 20:59:30 +01:00
|
|
|
{#if canBeDisplay}
|
2020-10-14 20:40:01 +02:00
|
|
|
<Toggle
|
|
|
|
bind:checked={primaryDisplay}
|
2020-10-16 22:50:58 +02:00
|
|
|
on:change={onChangePrimaryDisplay}
|
2020-10-14 20:40:01 +02:00
|
|
|
thin
|
|
|
|
text="Use as table display column" />
|
2021-02-09 19:49:12 +01:00
|
|
|
|
2021-02-10 23:23:27 +01:00
|
|
|
<Label grey small>Search Indexes</Label>
|
2021-02-15 20:59:30 +01:00
|
|
|
{/if}
|
|
|
|
{#if canBeSearched}
|
2021-02-09 19:49:12 +01:00
|
|
|
<Toggle
|
|
|
|
checked={indexes[0] === field.name}
|
|
|
|
disabled={indexes[1] === field.name}
|
|
|
|
on:change={onChangePrimaryIndex}
|
|
|
|
thin
|
|
|
|
text="Primary" />
|
2021-02-01 22:02:54 +01:00
|
|
|
<Toggle
|
2021-02-09 19:49:12 +01:00
|
|
|
checked={indexes[1] === field.name}
|
|
|
|
disabled={!indexes[0] || indexes[0] === field.name}
|
|
|
|
on:change={onChangeSecondaryIndex}
|
2021-02-01 22:02:54 +01:00
|
|
|
thin
|
2021-02-09 19:49:12 +01:00
|
|
|
text="Secondary" />
|
2020-10-14 20:40:01 +02:00
|
|
|
{/if}
|
|
|
|
|
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>
|
2021-03-01 18:06:08 +01:00
|
|
|
{#if relationshipOptions && relationshipOptions.length > 0}
|
|
|
|
<div>
|
|
|
|
<Label grey extraSmall>Define the relationship</Label>
|
|
|
|
<div class="radio-buttons">
|
2021-03-01 19:03:33 +01:00
|
|
|
{#each relationshipOptions as { value, name }}
|
2021-03-01 18:06:08 +01:00
|
|
|
<Radio
|
2021-03-01 19:03:33 +01:00
|
|
|
disabled={originalName}
|
|
|
|
name="Relationship type"
|
|
|
|
{value}
|
|
|
|
bind:group={field.relationshipType}>
|
2021-03-01 18:06:08 +01:00
|
|
|
<div class="radio-button-labels">
|
2021-03-05 16:19:33 +01:00
|
|
|
<label for={value}>{name.split('→')[0]}</label>
|
|
|
|
<label class="rel-type-center" for={value}>→</label>
|
|
|
|
<label for={value}>{name.split('→')[1]}</label>
|
2021-03-01 18:06:08 +01:00
|
|
|
</div>
|
|
|
|
</Radio>
|
|
|
|
{/each}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
{/if}
|
2020-09-29 19:27:35 +02:00
|
|
|
<Input
|
|
|
|
label={`Column Name in Other Table`}
|
|
|
|
thin
|
|
|
|
bind:value={field.fieldName} />
|
2021-02-15 20:59:30 +01:00
|
|
|
{:else if field.type === AUTO_COL}
|
|
|
|
<Select label="Auto Column Type" thin secondary bind:value={field.subtype}>
|
|
|
|
<option value="">Choose a subtype</option>
|
|
|
|
{#each Object.entries(getAutoColumnInformation()) as [subtype, info]}
|
|
|
|
<option value={subtype}>{info.name}</option>
|
|
|
|
{/each}
|
|
|
|
</Select>
|
2020-09-25 12:35:32 +02:00
|
|
|
{/if}
|
2020-10-27 23:57:05 +01:00
|
|
|
<footer class="create-column-options">
|
2021-02-15 20:59:30 +01:00
|
|
|
{#if !uneditable && originalName != null}
|
2021-04-09 15:24:28 +02:00
|
|
|
<Button warning size="S" text on:click={confirmDelete}>Delete Column</Button>
|
2020-10-23 18:38:10 +02:00
|
|
|
{/if}
|
2021-04-08 18:10:18 +02:00
|
|
|
<Button on:click={onClosed}>Cancel</Button>
|
2021-04-09 15:24:28 +02:00
|
|
|
<Button cta on:click={saveColumn} bind:disabled={invalid}>
|
2021-02-17 18:34:15 +01:00
|
|
|
Save Column
|
|
|
|
</Button>
|
2020-10-23 18:38:10 +02:00
|
|
|
</footer>
|
2020-09-25 12:35:32 +02:00
|
|
|
</div>
|
2020-10-23 18:38:10 +02:00
|
|
|
<ConfirmDialog
|
|
|
|
bind:this={confirmDeleteDialog}
|
|
|
|
body={`Are you sure you wish to delete this column? Your data will be deleted and this action cannot be undone.`}
|
|
|
|
okText="Delete Column"
|
|
|
|
onOk={deleteColumn}
|
2020-10-24 00:55:51 +02:00
|
|
|
onCancel={hideDeleteDialog}
|
2020-10-28 17:05:19 +01:00
|
|
|
title="Confirm Deletion" />
|
2020-08-07 17:13:57 +02:00
|
|
|
|
|
|
|
<style>
|
2021-02-15 17:12:39 +01:00
|
|
|
.radio-buttons {
|
|
|
|
gap: var(--spacing-m);
|
2021-02-22 12:40:24 +01:00
|
|
|
font-size: var(--font-size-xs);
|
2021-02-15 17:12:39 +01:00
|
|
|
}
|
2021-03-01 18:06:08 +01:00
|
|
|
|
|
|
|
.radio-buttons :global(> *) {
|
2021-03-03 11:41:40 +01:00
|
|
|
margin-top: var(--spacing-s);
|
2021-03-01 18:06:08 +01:00
|
|
|
width: 100%;
|
|
|
|
}
|
|
|
|
|
2020-09-25 14:11:57 +02:00
|
|
|
.actions {
|
2020-08-07 17:13:57 +02:00
|
|
|
display: grid;
|
|
|
|
grid-gap: var(--spacing-xl);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2020-10-27 14:04:32 +01:00
|
|
|
|
2020-10-27 23:57:05 +01:00
|
|
|
:global(.create-column-options button:first-child) {
|
|
|
|
margin-right: auto;
|
|
|
|
}
|
|
|
|
|
2021-03-01 18:06:08 +01:00
|
|
|
.rel-type-center {
|
|
|
|
font-weight: 500;
|
|
|
|
color: var(--grey-6);
|
2021-03-03 11:41:40 +01:00
|
|
|
margin-right: 4px;
|
|
|
|
margin-left: 4px;
|
2021-03-01 18:48:52 +01:00
|
|
|
padding: 1px 3px 1px 3px;
|
2021-03-01 18:06:08 +01:00
|
|
|
background: var(--grey-3);
|
|
|
|
border-radius: 2px;
|
|
|
|
}
|
|
|
|
|
|
|
|
.radio-button-labels {
|
2021-03-02 12:10:26 +01:00
|
|
|
margin-top: 2px;
|
2020-10-27 14:04:32 +01:00
|
|
|
}
|
2020-08-07 17:13:57 +02:00
|
|
|
</style>
|