2021-01-19 18:38:24 +01:00
|
|
|
import { cloneDeep } from "lodash/fp"
|
2021-01-14 16:39:50 +01:00
|
|
|
import { get } from "svelte/store"
|
|
|
|
import { backendUiStore, store } from "builderStore"
|
|
|
|
import { findAllMatchingComponents, findComponentPath } from "./storeUtils"
|
|
|
|
|
2021-01-19 16:39:04 +01:00
|
|
|
// Regex to match mustache variables, for replacing bindings
|
|
|
|
const CAPTURE_VAR_INSIDE_MUSTACHE = /{{([^}]+)}}/g
|
|
|
|
|
2021-01-14 16:39:50 +01:00
|
|
|
/**
|
|
|
|
* Gets all bindable data context fields and instance fields.
|
|
|
|
*/
|
|
|
|
export const getBindableProperties = (rootComponent, componentId) => {
|
2021-01-20 12:14:36 +01:00
|
|
|
const contextBindings = getContextBindings(rootComponent, componentId)
|
|
|
|
const queryBindings = getQueryBindings(rootComponent, componentId)
|
|
|
|
const componentBindings = getComponentBindings(rootComponent)
|
|
|
|
return [...contextBindings, ...queryBindings, componentBindings]
|
2021-01-14 16:39:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-01-19 18:38:24 +01:00
|
|
|
* Gets all data provider components above a component.
|
2021-01-14 16:39:50 +01:00
|
|
|
*/
|
2021-01-19 18:38:24 +01:00
|
|
|
export const getDataProviderComponents = (rootComponent, componentId) => {
|
2021-01-14 16:39:50 +01:00
|
|
|
if (!rootComponent || !componentId) {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
2021-01-19 16:39:04 +01:00
|
|
|
// Get the component tree leading up to this component, ignoring the component
|
|
|
|
// itself
|
2021-01-14 16:39:50 +01:00
|
|
|
const path = findComponentPath(rootComponent, componentId)
|
|
|
|
path.pop()
|
|
|
|
|
2021-01-19 18:38:24 +01:00
|
|
|
// Filter by only data provider components
|
|
|
|
return path.filter(component => {
|
|
|
|
const def = store.actions.components.getDefinition(component._component)
|
|
|
|
return def?.dataProvider
|
|
|
|
})
|
|
|
|
}
|
2021-01-14 16:39:50 +01:00
|
|
|
|
2021-01-19 18:38:24 +01:00
|
|
|
/**
|
|
|
|
* Gets a datasource object for a certain data provider component
|
|
|
|
*/
|
|
|
|
export const getDatasourceForProvider = component => {
|
|
|
|
const def = store.actions.components.getDefinition(component?._component)
|
|
|
|
if (!def) {
|
|
|
|
return null
|
|
|
|
}
|
2021-01-19 16:39:04 +01:00
|
|
|
|
2021-01-19 18:38:24 +01:00
|
|
|
// Extract datasource from component instance
|
|
|
|
const datasourceSetting = def.settings.find(setting => {
|
|
|
|
return setting.key === def.datasourceSetting
|
|
|
|
})
|
|
|
|
if (!datasourceSetting) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
// There are different types of setting which can be a datasource, for
|
|
|
|
// example an actual datasource object, or a table ID string.
|
|
|
|
// Convert the datasource setting into a proper datasource object so that
|
|
|
|
// we can use it properly
|
|
|
|
if (datasourceSetting.type === "datasource") {
|
|
|
|
return component[datasourceSetting?.key]
|
|
|
|
} else if (datasourceSetting.type === "table") {
|
|
|
|
return {
|
|
|
|
tableId: component[datasourceSetting?.key],
|
|
|
|
type: "table",
|
2021-01-19 16:39:04 +01:00
|
|
|
}
|
2021-01-19 18:38:24 +01:00
|
|
|
}
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets all bindable data contexts. These are fields of schemas of data contexts
|
|
|
|
* provided by data provider components, such as lists or row detail components.
|
|
|
|
*/
|
2021-01-20 12:14:36 +01:00
|
|
|
export const getContextBindings = (rootComponent, componentId) => {
|
2021-01-19 18:38:24 +01:00
|
|
|
// Extract any components which provide data contexts
|
2021-01-20 12:14:36 +01:00
|
|
|
const dataProviders = getDataProviderComponents(rootComponent, componentId)
|
|
|
|
let contextBindings = []
|
2021-01-19 18:38:24 +01:00
|
|
|
dataProviders.forEach(component => {
|
|
|
|
const datasource = getDatasourceForProvider(component)
|
2021-01-19 16:39:04 +01:00
|
|
|
if (!datasource) {
|
2021-01-14 16:39:50 +01:00
|
|
|
return
|
|
|
|
}
|
2021-01-19 16:39:04 +01:00
|
|
|
|
2021-01-19 18:38:24 +01:00
|
|
|
// Get schema and add _id and _rev fields
|
|
|
|
let { schema, table } = getSchemaForDatasource(datasource)
|
|
|
|
schema["_id"] = { type: "string" }
|
|
|
|
schema["_rev"] = { type: "string " }
|
2021-01-15 15:47:36 +01:00
|
|
|
const keys = Object.keys(schema).sort()
|
2021-01-19 18:38:24 +01:00
|
|
|
|
|
|
|
// Create bindable properties for each schema field
|
2021-01-15 15:47:36 +01:00
|
|
|
keys.forEach(key => {
|
|
|
|
const fieldSchema = schema[key]
|
2021-01-14 16:39:50 +01:00
|
|
|
// Replace certain bindings with a new property to help display components
|
|
|
|
let runtimeBoundKey = key
|
2021-01-15 15:47:36 +01:00
|
|
|
if (fieldSchema.type === "link") {
|
2021-01-14 16:39:50 +01:00
|
|
|
runtimeBoundKey = `${key}_count`
|
2021-01-15 15:47:36 +01:00
|
|
|
} else if (fieldSchema.type === "attachment") {
|
2021-01-14 16:39:50 +01:00
|
|
|
runtimeBoundKey = `${key}_first`
|
|
|
|
}
|
|
|
|
|
2021-01-20 12:14:36 +01:00
|
|
|
contextBindings.push({
|
2021-01-14 16:39:50 +01:00
|
|
|
type: "context",
|
2021-01-19 18:38:24 +01:00
|
|
|
runtimeBinding: `${component._id}.${runtimeBoundKey}`,
|
|
|
|
readableBinding: `${component._instanceName}.${table.name}.${key}`,
|
2021-01-15 15:47:36 +01:00
|
|
|
fieldSchema,
|
2021-01-19 18:38:24 +01:00
|
|
|
providerId: component._id,
|
2021-01-19 16:39:04 +01:00
|
|
|
tableId: datasource.tableId,
|
2021-01-19 18:38:24 +01:00
|
|
|
field: key,
|
2021-01-14 16:39:50 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2021-01-20 12:14:36 +01:00
|
|
|
return contextBindings
|
2021-01-14 16:39:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets all bindable components. These are form components which allow their
|
|
|
|
* values to be bound to.
|
|
|
|
*/
|
2021-01-20 12:14:36 +01:00
|
|
|
export const getComponentBindings = rootComponent => {
|
2021-01-14 16:39:50 +01:00
|
|
|
if (!rootComponent) {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
const componentSelector = component => {
|
|
|
|
const type = component._component
|
|
|
|
const definition = store.actions.components.getDefinition(type)
|
2021-01-15 12:02:00 +01:00
|
|
|
return definition?.bindable
|
2021-01-14 16:39:50 +01:00
|
|
|
}
|
|
|
|
const components = findAllMatchingComponents(rootComponent, componentSelector)
|
2021-01-15 11:59:22 +01:00
|
|
|
return components.map(component => {
|
|
|
|
return {
|
|
|
|
type: "instance",
|
|
|
|
providerId: component._id,
|
|
|
|
runtimeBinding: `${component._id}`,
|
|
|
|
readableBinding: `${component._instanceName}`,
|
|
|
|
}
|
|
|
|
})
|
2021-01-14 16:39:50 +01:00
|
|
|
}
|
|
|
|
|
2021-01-20 12:14:36 +01:00
|
|
|
/**
|
|
|
|
* Gets all bindable query fields. These are fields of schemas of data contexts
|
|
|
|
* provided by data provider components, such as lists or row detail components.
|
|
|
|
*/
|
|
|
|
export const getQueryBindings = (rootComponent, componentId) => {
|
|
|
|
// Extract any components which provide data contexts
|
|
|
|
const dataProviders = getDataProviderComponents(rootComponent, componentId)
|
|
|
|
const queries = get(backendUiStore).queries
|
|
|
|
let queryBindings = []
|
|
|
|
dataProviders.forEach(component => {
|
|
|
|
const datasource = getDatasourceForProvider(component)
|
|
|
|
if (!datasource) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find a query for this table ID
|
|
|
|
const queryId = datasource.tableId
|
|
|
|
const query = queries.find(query => query._id === queryId)
|
|
|
|
const schema = query?.schema
|
|
|
|
if (!schema) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add all schema fields as bindable values
|
|
|
|
const keys = Object.keys(schema).sort()
|
|
|
|
keys.forEach(key => {
|
|
|
|
const fieldSchema = schema[key]
|
|
|
|
queryBindings.push({
|
|
|
|
type: "context",
|
|
|
|
fieldSchema,
|
|
|
|
runtimeBinding: `${component._id}.${key}`,
|
|
|
|
readableBinding: `${component._instanceName}.${query.name}.${key}`,
|
|
|
|
providerId: component._id,
|
|
|
|
tableId: datasource.tableId,
|
|
|
|
field: key,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
return queryBindings
|
|
|
|
}
|
|
|
|
|
2021-01-14 16:39:50 +01:00
|
|
|
/**
|
|
|
|
* Gets a schema for a datasource object.
|
|
|
|
*/
|
2021-01-19 18:38:24 +01:00
|
|
|
export const getSchemaForDatasource = datasource => {
|
|
|
|
let schema, table
|
|
|
|
if (datasource) {
|
|
|
|
const tables = get(backendUiStore).tables
|
|
|
|
const { type } = datasource
|
|
|
|
table = tables.find(table => table._id === datasource.tableId)
|
|
|
|
if (table) {
|
|
|
|
if (type === "table") {
|
|
|
|
schema = cloneDeep(table.schema)
|
|
|
|
} else if (type === "view") {
|
|
|
|
schema = cloneDeep(table.views?.[datasource.name]?.schema)
|
|
|
|
} else if (type === "link") {
|
|
|
|
schema = cloneDeep(table.schema)
|
|
|
|
}
|
2021-01-14 16:39:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return { schema, table }
|
|
|
|
}
|
2021-01-19 16:39:04 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts a readable data binding into a runtime data binding
|
|
|
|
*/
|
|
|
|
export function readableToRuntimeBinding(bindableProperties, textWithBindings) {
|
2021-01-19 18:38:24 +01:00
|
|
|
if (typeof textWithBindings !== "string") {
|
|
|
|
return textWithBindings
|
|
|
|
}
|
2021-01-19 16:39:04 +01:00
|
|
|
const boundValues = textWithBindings.match(CAPTURE_VAR_INSIDE_MUSTACHE) || []
|
|
|
|
let result = textWithBindings
|
|
|
|
boundValues.forEach(boundValue => {
|
|
|
|
const binding = bindableProperties.find(({ readableBinding }) => {
|
|
|
|
return boundValue === `{{ ${readableBinding} }}`
|
|
|
|
})
|
|
|
|
if (binding) {
|
|
|
|
result = result.replace(boundValue, `{{ ${binding.runtimeBinding} }}`)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts a runtime data binding into a readable data binding
|
|
|
|
*/
|
|
|
|
export function runtimeToReadableBinding(bindableProperties, textWithBindings) {
|
2021-01-19 18:38:24 +01:00
|
|
|
if (typeof textWithBindings !== "string") {
|
|
|
|
return textWithBindings
|
|
|
|
}
|
2021-01-19 16:39:04 +01:00
|
|
|
const boundValues = textWithBindings.match(CAPTURE_VAR_INSIDE_MUSTACHE) || []
|
|
|
|
let result = textWithBindings
|
|
|
|
boundValues.forEach(boundValue => {
|
|
|
|
const binding = bindableProperties.find(({ runtimeBinding }) => {
|
|
|
|
return boundValue === `{{ ${runtimeBinding} }}`
|
|
|
|
})
|
|
|
|
// Show invalid bindings as invalid rather than a long ID
|
|
|
|
result = result.replace(
|
|
|
|
boundValue,
|
|
|
|
`{{ ${binding?.readableBinding ?? "Invalid binding"} }}`
|
|
|
|
)
|
|
|
|
})
|
|
|
|
return result
|
|
|
|
}
|