1
0
Fork 0
mirror of synced 2024-07-11 01:06:04 +12:00

Merge with master, fix repeater block scope, fix grid block global actions

This commit is contained in:
Andrew Kingston 2023-11-28 16:14:21 +00:00
parent b1a218beb0
commit 3f40efea53
4 changed files with 85 additions and 92 deletions

View file

@ -254,7 +254,7 @@ export const getComponentContexts = (
delete map[componentId] delete map[componentId]
} }
return Object.values(map) return Object.values(map).filter(x => x.contexts.length > 0)
} }
/** /**

View file

@ -1,5 +1,4 @@
import { getComponentContexts } from "builderStore/dataBinding" import { getComponentContexts } from "builderStore/dataBinding"
import { store } from "builderStore"
import { capitalise } from "helpers" import { capitalise } from "helpers"
// Generates bindings for all components that provider "datasource like" // Generates bindings for all components that provider "datasource like"
@ -8,52 +7,51 @@ import { capitalise } from "helpers"
// Some examples are saving rows or duplicating rows. // Some examples are saving rows or duplicating rows.
export const getDatasourceLikeProviders = ({ asset, componentId, nested }) => { export const getDatasourceLikeProviders = ({ asset, componentId, nested }) => {
// Get all form context providers // Get all form context providers
const formComponents = getComponentContexts(asset, componentId, "form", { const formComponentContexts = getComponentContexts(
includeSelf: nested, asset,
}) componentId,
"form",
{
includeSelf: nested,
}
)
// Get all schema context providers // Get all schema context providers
const schemaComponents = getComponentContexts(asset, componentId, "schema", { const schemaComponentContexts = getComponentContexts(
includeSelf: nested, asset,
}) componentId,
"schema",
{
includeSelf: nested,
}
)
// Generate contexts for all form providers console.log(formComponentContexts)
const formContexts = formComponents.map(component => ({
component,
context: extractComponentContext(component, "form"),
}))
// Generate contexts for all schema providers
const schemaContexts = schemaComponents.map(component => ({
component,
context: extractComponentContext(component, "schema"),
}))
// Check for duplicate contexts by the same component. In this case, attempt // Check for duplicate contexts by the same component. In this case, attempt
// to label contexts with their suffixes // to label contexts with their suffixes
schemaContexts.forEach(schemaContext => { schemaComponentContexts.forEach(schemaContext => {
// Check if we have a form context for this component // Check if we have a form context for this component
const id = schemaContext.component._id const id = schemaContext.component._id
const existing = formContexts.find(x => x.component._id === id) const existing = formComponentContexts.find(x => x.component._id === id)
if (existing) { if (existing) {
if (existing.context.suffix) { if (existing.contexts[0].suffix) {
const suffix = capitalise(existing.context.suffix) const suffix = capitalise(existing.contexts[0].suffix)
existing.readableSuffix = ` - ${suffix}` existing.readableSuffix = ` - ${suffix}`
} }
if (schemaContext.context.suffix) { if (schemaContext.contexts[0].suffix) {
const suffix = capitalise(schemaContext.context.suffix) const suffix = capitalise(schemaContext.contexts[0].suffix)
schemaContext.readableSuffix = ` - ${suffix}` schemaContext.readableSuffix = ` - ${suffix}`
} }
} }
}) })
// Generate bindings for all contexts // Generate bindings for all contexts
const allContexts = formContexts.concat(schemaContexts) const allContexts = formComponentContexts.concat(schemaComponentContexts)
return allContexts.map(({ component, context, readableSuffix }) => { return allContexts.map(({ component, contexts, readableSuffix }) => {
let readableBinding = component._instanceName let readableBinding = component._instanceName
let runtimeBinding = component._id let runtimeBinding = component._id
if (context.suffix) { if (contexts[0].suffix) {
runtimeBinding += `-${context.suffix}` runtimeBinding += `-${contexts[0].suffix}`
} }
if (readableSuffix) { if (readableSuffix) {
readableBinding += readableSuffix readableBinding += readableSuffix
@ -64,13 +62,3 @@ export const getDatasourceLikeProviders = ({ asset, componentId, nested }) => {
} }
}) })
} }
// Gets a context definition of a certain type from a component definition
const extractComponentContext = (component, contextType) => {
const def = store.actions.components.getDefinition(component?._component)
if (!def) {
return null
}
const contexts = Array.isArray(def.context) ? def.context : [def.context]
return contexts.find(context => context?.type === contextType)
}

View file

@ -6003,7 +6003,8 @@
}, },
{ {
"type": "schema", "type": "schema",
"suffix": "repeater" "suffix": "repeater",
"scope": "local"
} }
] ]
}, },

View file

@ -19,7 +19,36 @@
export let onRowClick = null export let onRowClick = null
export let buttons = null export let buttons = null
// parses columns to fix older formats const context = getContext("context")
const component = getContext("component")
const {
styleable,
API,
builderStore,
notificationStore,
enrichButtonActions,
ActionTypes,
createContextStore,
Provider,
} = getContext("sdk")
let grid
$: columnWhitelist = parsedColumns
?.filter(col => col.active)
?.map(col => col.field)
$: schemaOverrides = getSchemaOverrides(parsedColumns)
$: enrichedButtons = enrichButtons(buttons)
$: parsedColumns = getParsedColumns(columns)
$: actions = [
{
type: ActionTypes.RefreshDatasource,
callback: () => grid?.getContext()?.rows.actions.refreshData(),
metadata: { dataSource: table },
},
]
// Parses columns to fix older formats
const getParsedColumns = columns => { const getParsedColumns = columns => {
// If the first element has an active key all elements should be in the new format // If the first element has an active key all elements should be in the new format
if (columns?.length && columns[0]?.active !== undefined) { if (columns?.length && columns[0]?.active !== undefined) {
@ -33,28 +62,6 @@
})) }))
} }
$: parsedColumns = getParsedColumns(columns)
const context = getContext("context")
const component = getContext("component")
const {
styleable,
API,
builderStore,
notificationStore,
enrichButtonActions,
ActionTypes,
createContextStore,
} = getContext("sdk")
let grid
$: columnWhitelist = parsedColumns
?.filter(col => col.active)
?.map(col => col.field)
$: schemaOverrides = getSchemaOverrides(parsedColumns)
$: enrichedButtons = enrichButtons(buttons)
const getSchemaOverrides = columns => { const getSchemaOverrides = columns => {
let overrides = {} let overrides = {}
columns?.forEach(column => { columns?.forEach(column => {
@ -78,11 +85,6 @@
const id = get(component).id const id = get(component).id
const gridContext = createContextStore(context) const gridContext = createContextStore(context)
gridContext.actions.provideData(id, row) gridContext.actions.provideData(id, row)
gridContext.actions.provideAction(
id,
ActionTypes.RefreshDatasource,
() => grid?.getContext()?.rows.actions.refreshData()
)
const fn = enrichButtonActions(settings.onClick, get(gridContext)) const fn = enrichButtonActions(settings.onClick, get(gridContext))
return await fn?.({ row }) return await fn?.({ row })
}, },
@ -94,29 +96,31 @@
use:styleable={$component.styles} use:styleable={$component.styles}
class:in-builder={$builderStore.inBuilder} class:in-builder={$builderStore.inBuilder}
> >
<Grid <Provider {actions}>
bind:this={grid} <Grid
datasource={table} bind:this={grid}
{API} datasource={table}
{stripeRows} {API}
{initialFilter} {stripeRows}
{initialSortColumn} {initialFilter}
{initialSortOrder} {initialSortColumn}
{fixedRowHeight} {initialSortOrder}
{columnWhitelist} {fixedRowHeight}
{schemaOverrides} {columnWhitelist}
canAddRows={allowAddRows} {schemaOverrides}
canEditRows={allowEditRows} canAddRows={allowAddRows}
canDeleteRows={allowDeleteRows} canEditRows={allowEditRows}
canEditColumns={false} canDeleteRows={allowDeleteRows}
canExpandRows={false} canEditColumns={false}
canSaveSchema={false} canExpandRows={false}
showControls={false} canSaveSchema={false}
notifySuccess={notificationStore.actions.success} showControls={false}
notifyError={notificationStore.actions.error} notifySuccess={notificationStore.actions.success}
buttons={enrichedButtons} notifyError={notificationStore.actions.error}
on:rowclick={e => onRowClick?.({ row: e.detail })} buttons={enrichedButtons}
/> on:rowclick={e => onRowClick?.({ row: e.detail })}
/>
</Provider>
</div> </div>
<style> <style>