Tidy up card binding logic

This commit is contained in:
Andrew Kingston 2024-04-19 11:08:59 +01:00
parent e98a9f7f80
commit 75bf928242
1 changed files with 21 additions and 18 deletions

View File

@ -321,39 +321,42 @@ export class ComponentStore extends BudiStore {
.map(([name]) => name) .map(([name]) => name)
} }
// Extract good field candidates to prefil our cards with // Inserts a card binding for a certain setting
const addBinding = (key, ...parts) => {
parts.unshift(`${_id}-repeater`)
component[key] = `{{ ${parts.map(safe).join(".")} }}`
}
// Extract good field candidates to prefil our cards with.
// Use the primary display as the best field, if it exists.
const fields = findFieldTypes([ const fields = findFieldTypes([
FieldType.STRING, FieldType.STRING,
FieldType.OPTIONS, FieldType.OPTIONS,
FieldType.DATETIME, FieldType.DATETIME,
FieldType.NUMBER, FieldType.NUMBER,
]) ])
// Use the primary display as the best field, if it exists
if (schema?.[table?.primaryDisplay]) { if (schema?.[table?.primaryDisplay]) {
fields.unshift(table.primaryDisplay) fields.unshift(table.primaryDisplay)
} }
// Fill our cards with as many bindings as we can // Fill our cards with as many bindings as we can
const prefix = safe(`${_id}-repeater`)
cardKeys.forEach(key => { cardKeys.forEach(key => {
if (!fields[0]) return if (fields[0]) {
component[key] = `{{ ${prefix}.${safe(fields[0])} }}` addBinding(key, fields[0])
fields.shift() fields.shift()
}
}) })
// Attempt to fill the image setting // Attempt to fill the image setting.
let imgFields = findFieldTypes([FieldType.ATTACHMENT_SINGLE]) // Check single attachment fields first.
if (imgFields[0]) { let imgField = findFieldTypes(FieldType.ATTACHMENT_SINGLE)[0]
component.cardImageURL = `{{ ${prefix}.${safe( if (imgField) {
imgFields[0] addBinding("cardImageURL", imgField, "url")
)}.[url] }}`
} else { } else {
imgFields = findFieldTypes([FieldType.ATTACHMENTS]) // Then try multi-attachment fields if no single ones exist
if (imgFields[0]) { imgField = findFieldTypes(FieldType.ATTACHMENTS)[0]
component.cardImageURL = `{{ ${prefix}.${safe( if (imgField) {
imgFields[0] addBinding("cardImageURL", imgField, 0, "url")
)}.[0].[url] }}`
} }
} }
} }