Use more readable bindings and remove extraneous aliases to enums

This commit is contained in:
Andrew Kingston 2024-10-11 16:06:20 +01:00
parent 038bfd1360
commit bd3e83789e
No known key found for this signature in database
1 changed files with 30 additions and 31 deletions

View File

@ -53,18 +53,11 @@
export let field export let field
const AUTO_TYPE = FieldType.AUTO
const FORMULA_TYPE = FieldType.FORMULA
const LINK_TYPE = FieldType.LINK
const STRING_TYPE = FieldType.STRING
const NUMBER_TYPE = FieldType.NUMBER
const JSON_TYPE = FieldType.JSON
const DATE_TYPE = FieldType.DATETIME
const SINGLE_USER_DEFAULT = `{{ ${safe("user")}.${safe("_id")} }}`
const MULTI_USER_DEFAULT = `{{ js "cmV0dXJuIFskKCJbdXNlcl0uW19pZF0iKV0=" }}`
const dispatch = createEventDispatcher() const dispatch = createEventDispatcher()
const { dispatch: gridDispatch, rows } = getContext("grid") const { dispatch: gridDispatch, rows } = getContext("grid")
const SafeID = `${safe("user")}.${safe("_id")}`
const SingleUserDefault = `{{ ${SafeID} }}`
const MultiUserDefault = `{{ js "${btoa(`return [$("${SafeID}")]`)}" }}`
let mounted = false let mounted = false
let originalName let originalName
@ -113,7 +106,7 @@
$: { $: {
// this parses any changes the user has made when creating a new internal relationship // this parses any changes the user has made when creating a new internal relationship
// into what we expect the schema to look like // into what we expect the schema to look like
if (editableColumn.type === LINK_TYPE) { if (editableColumn.type === FieldType.LINK) {
relationshipTableIdPrimary = table._id relationshipTableIdPrimary = table._id
if (relationshipPart1 === PrettyRelationshipDefinitions.ONE) { if (relationshipPart1 === PrettyRelationshipDefinitions.ONE) {
relationshipOpts2 = relationshipOpts2.filter( relationshipOpts2 = relationshipOpts2.filter(
@ -150,7 +143,7 @@
UNEDITABLE_USER_FIELDS.includes(editableColumn.name) UNEDITABLE_USER_FIELDS.includes(editableColumn.name)
$: invalid = $: invalid =
!editableColumn?.name || !editableColumn?.name ||
(editableColumn?.type === LINK_TYPE && !editableColumn?.tableId) || (editableColumn?.type === FieldType.LINK && !editableColumn?.tableId) ||
Object.keys(errors).length !== 0 || Object.keys(errors).length !== 0 ||
!optionsValid !optionsValid
$: errors = checkErrors(editableColumn) $: errors = checkErrors(editableColumn)
@ -176,9 +169,9 @@
$: defaultValuesEnabled = isEnabled("DEFAULT_VALUES") $: defaultValuesEnabled = isEnabled("DEFAULT_VALUES")
$: canHaveDefault = !required && canHaveDefaultColumn(editableColumn.type) $: canHaveDefault = !required && canHaveDefaultColumn(editableColumn.type)
$: canBeRequired = $: canBeRequired =
editableColumn?.type !== LINK_TYPE && editableColumn?.type !== FieldType.LINK &&
!uneditable && !uneditable &&
editableColumn?.type !== AUTO_TYPE && editableColumn?.type !== FieldType.AUTO &&
!editableColumn.autocolumn !editableColumn.autocolumn
$: hasDefault = $: hasDefault =
editableColumn?.default != null && editableColumn?.default !== "" editableColumn?.default != null && editableColumn?.default !== ""
@ -227,7 +220,7 @@
function makeFieldId(type, subtype, autocolumn) { function makeFieldId(type, subtype, autocolumn) {
// don't make field IDs for auto types // don't make field IDs for auto types
if (type === AUTO_TYPE || autocolumn) { if (type === FieldType.AUTO || autocolumn) {
return type.toUpperCase() return type.toUpperCase()
} else if ( } else if (
type === FieldType.BB_REFERENCE || type === FieldType.BB_REFERENCE ||
@ -252,7 +245,7 @@
// Here we are setting the relationship values based on the editableColumn // Here we are setting the relationship values based on the editableColumn
// This part of the code is used when viewing an existing field hence the check // This part of the code is used when viewing an existing field hence the check
// for the tableId // for the tableId
if (editableColumn.type === LINK_TYPE && editableColumn.tableId) { if (editableColumn.type === FieldType.LINK && editableColumn.tableId) {
relationshipTableIdPrimary = table._id relationshipTableIdPrimary = table._id
relationshipTableIdSecondary = editableColumn.tableId relationshipTableIdSecondary = editableColumn.tableId
if (editableColumn.relationshipType in relationshipMap) { if (editableColumn.relationshipType in relationshipMap) {
@ -293,14 +286,14 @@
delete saveColumn.fieldId delete saveColumn.fieldId
if (saveColumn.type === AUTO_TYPE) { if (saveColumn.type === FieldType.AUTO) {
saveColumn = buildAutoColumn( saveColumn = buildAutoColumn(
$tables.selected.name, $tables.selected.name,
saveColumn.name, saveColumn.name,
saveColumn.subtype saveColumn.subtype
) )
} }
if (saveColumn.type !== LINK_TYPE) { if (saveColumn.type !== FieldType.LINK) {
delete saveColumn.fieldName delete saveColumn.fieldName
} }
@ -387,9 +380,9 @@
editableColumn.subtype = definition.subtype editableColumn.subtype = definition.subtype
// Default relationships many to many // Default relationships many to many
if (editableColumn.type === LINK_TYPE) { if (editableColumn.type === FieldType.LINK) {
editableColumn.relationshipType = RelationshipType.MANY_TO_MANY editableColumn.relationshipType = RelationshipType.MANY_TO_MANY
} else if (editableColumn.type === FORMULA_TYPE) { } else if (editableColumn.type === FieldType.FORMULA) {
editableColumn.formulaType = "dynamic" editableColumn.formulaType = "dynamic"
} }
} }
@ -508,17 +501,23 @@
fieldToCheck.constraints = {} fieldToCheck.constraints = {}
} }
// some string types may have been built by server, may not always have constraints // some string types may have been built by server, may not always have constraints
if (fieldToCheck.type === STRING_TYPE && !fieldToCheck.constraints.length) { if (
fieldToCheck.type === FieldType.STRING &&
!fieldToCheck.constraints.length
) {
fieldToCheck.constraints.length = {} fieldToCheck.constraints.length = {}
} }
// some number types made server-side will be missing constraints // some number types made server-side will be missing constraints
if ( if (
fieldToCheck.type === NUMBER_TYPE && fieldToCheck.type === FieldType.NUMBER &&
!fieldToCheck.constraints.numericality !fieldToCheck.constraints.numericality
) { ) {
fieldToCheck.constraints.numericality = {} fieldToCheck.constraints.numericality = {}
} }
if (fieldToCheck.type === DATE_TYPE && !fieldToCheck.constraints.datetime) { if (
fieldToCheck.type === FieldType.DATETIME &&
!fieldToCheck.constraints.datetime
) {
fieldToCheck.constraints.datetime = {} fieldToCheck.constraints.datetime = {}
} }
} }
@ -593,13 +592,13 @@
on:input={e => { on:input={e => {
if ( if (
!uneditable && !uneditable &&
!(linkEditDisabled && editableColumn.type === LINK_TYPE) !(linkEditDisabled && editableColumn.type === FieldType.LINK)
) { ) {
editableColumn.name = e.target.value editableColumn.name = e.target.value
} }
}} }}
disabled={uneditable || disabled={uneditable ||
(linkEditDisabled && editableColumn.type === LINK_TYPE)} (linkEditDisabled && editableColumn.type === FieldType.LINK)}
error={errors?.name} error={errors?.name}
/> />
{/if} {/if}
@ -613,7 +612,7 @@
getOptionValue={field => field.fieldId} getOptionValue={field => field.fieldId}
getOptionIcon={field => field.icon} getOptionIcon={field => field.icon}
isOptionEnabled={option => { isOptionEnabled={option => {
if (option.type === AUTO_TYPE) { if (option.type === FieldType.AUTO) {
return availableAutoColumnKeys?.length > 0 return availableAutoColumnKeys?.length > 0
} }
return true return true
@ -656,7 +655,7 @@
bind:optionColors={editableColumn.optionColors} bind:optionColors={editableColumn.optionColors}
bind:valid={optionsValid} bind:valid={optionsValid}
/> />
{:else if editableColumn.type === DATE_TYPE && !editableColumn.autocolumn} {:else if editableColumn.type === FieldType.DATETIME && !editableColumn.autocolumn}
<div class="split-label"> <div class="split-label">
<div class="label-length"> <div class="label-length">
<Label size="M">Earliest</Label> <Label size="M">Earliest</Label>
@ -743,7 +742,7 @@
{tableOptions} {tableOptions}
{errors} {errors}
/> />
{:else if editableColumn.type === FORMULA_TYPE} {:else if editableColumn.type === FieldType.FORMULA}
{#if !externalTable} {#if !externalTable}
<div class="split-label"> <div class="split-label">
<div class="label-length"> <div class="label-length">
@ -786,12 +785,12 @@
/> />
</div> </div>
</div> </div>
{:else if editableColumn.type === JSON_TYPE} {:else if editableColumn.type === FieldType.JSON}
<Button primary text on:click={openJsonSchemaEditor}> <Button primary text on:click={openJsonSchemaEditor}>
Open schema editor Open schema editor
</Button> </Button>
{/if} {/if}
{#if editableColumn.type === AUTO_TYPE || editableColumn.autocolumn} {#if editableColumn.type === FieldType.AUTO || editableColumn.autocolumn}
<Select <Select
label="Auto column type" label="Auto column type"
value={editableColumn.subtype} value={editableColumn.subtype}
@ -840,7 +839,7 @@
/> />
{:else if editableColumn.subtype === BBReferenceFieldSubType.USER} {:else if editableColumn.subtype === BBReferenceFieldSubType.USER}
{@const defaultValue = {@const defaultValue =
editableColumn.type === FieldType.BB_REFERENCE editableColumn.type === FieldType.BB_REFERENCE_SINGLE
? SINGLE_USER_DEFAULT ? SINGLE_USER_DEFAULT
: MULTI_USER_DEFAULT} : MULTI_USER_DEFAULT}
<Toggle <Toggle