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"
|
2021-08-26 17:46:25 +02:00
|
|
|
import {
|
2021-09-30 18:24:43 +02:00
|
|
|
findAllMatchingComponents,
|
2021-08-26 17:46:25 +02:00
|
|
|
findComponent,
|
|
|
|
findComponentPath,
|
2021-11-04 12:30:43 +01:00
|
|
|
getComponentSettings,
|
2021-12-06 12:41:17 +01:00
|
|
|
} from "./componentUtils"
|
2021-04-29 20:10:02 +02:00
|
|
|
import { store } from "builderStore"
|
2022-04-05 17:05:29 +02:00
|
|
|
import {
|
|
|
|
queries as queriesStores,
|
|
|
|
tables as tablesStore,
|
|
|
|
roles as rolesStore,
|
|
|
|
} from "stores/backend"
|
2021-10-11 15:54:24 +02:00
|
|
|
import {
|
|
|
|
makePropSafe,
|
|
|
|
isJSBinding,
|
|
|
|
decodeJSBinding,
|
|
|
|
encodeJSBinding,
|
|
|
|
} from "@budibase/string-templates"
|
2021-01-28 15:29:35 +01:00
|
|
|
import { TableNames } from "../constants"
|
2022-01-20 12:19:37 +01:00
|
|
|
import { JSONUtils } from "@budibase/frontend-core"
|
2022-04-26 15:22:32 +02:00
|
|
|
import ActionDefinitions from "components/design/settings/controls/ButtonActionEditor/manifest.json"
|
2023-01-26 16:09:20 +01:00
|
|
|
import { environment, licensing } from "stores/portal"
|
2021-01-14 16:39:50 +01:00
|
|
|
|
2021-01-21 12:31:45 +01:00
|
|
|
// Regex to match all instances of template strings
|
|
|
|
const CAPTURE_VAR_INSIDE_TEMPLATE = /{{([^}]+)}}/g
|
2021-10-11 15:54:24 +02:00
|
|
|
const CAPTURE_VAR_INSIDE_JS = /\$\("([^")]+)"\)/g
|
2021-02-22 16:49:57 +01:00
|
|
|
const CAPTURE_HBS_TEMPLATE = /{{[\S\s]*?}}/g
|
2021-01-19 16:39:04 +01:00
|
|
|
|
2021-01-14 16:39:50 +01:00
|
|
|
/**
|
|
|
|
* Gets all bindable data context fields and instance fields.
|
|
|
|
*/
|
2021-02-15 21:03:29 +01:00
|
|
|
export const getBindableProperties = (asset, componentId) => {
|
|
|
|
const contextBindings = getContextBindings(asset, componentId)
|
|
|
|
const userBindings = getUserBindings()
|
2021-04-30 20:17:00 +02:00
|
|
|
const urlBindings = getUrlBindings(asset)
|
2021-08-13 12:24:47 +02:00
|
|
|
const deviceBindings = getDeviceBindings()
|
2021-08-26 12:28:44 +02:00
|
|
|
const stateBindings = getStateBindings()
|
2022-02-25 09:25:56 +01:00
|
|
|
const selectedRowsBindings = getSelectedRowsBindings(asset)
|
2022-04-05 17:05:29 +02:00
|
|
|
const roleBindings = getRoleBindings()
|
2021-08-13 12:24:47 +02:00
|
|
|
return [
|
|
|
|
...contextBindings,
|
2021-11-10 15:57:46 +01:00
|
|
|
...urlBindings,
|
|
|
|
...stateBindings,
|
2021-08-13 12:24:47 +02:00
|
|
|
...userBindings,
|
2021-11-10 15:57:46 +01:00
|
|
|
...deviceBindings,
|
2022-02-25 09:25:56 +01:00
|
|
|
...selectedRowsBindings,
|
2022-04-05 17:05:29 +02:00
|
|
|
...roleBindings,
|
2021-08-13 12:24:47 +02:00
|
|
|
]
|
2021-01-14 16:39:50 +01:00
|
|
|
}
|
|
|
|
|
2022-06-14 11:14:05 +02:00
|
|
|
/**
|
|
|
|
* Gets all rest bindable data fields
|
|
|
|
*/
|
|
|
|
export const getRestBindings = () => {
|
2023-01-26 17:16:56 +01:00
|
|
|
const environmentVariablesEnabled = get(licensing).environmentVariablesEnabled
|
2022-06-14 11:14:05 +02:00
|
|
|
const userBindings = getUserBindings()
|
2023-01-26 16:09:20 +01:00
|
|
|
return [
|
|
|
|
...userBindings,
|
|
|
|
...getAuthBindings(),
|
2023-01-26 17:16:56 +01:00
|
|
|
...(environmentVariablesEnabled ? getEnvironmentBindings() : []),
|
2023-01-26 16:09:20 +01:00
|
|
|
]
|
2022-07-01 18:27:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets all rest bindable auth fields
|
|
|
|
*/
|
|
|
|
export const getAuthBindings = () => {
|
|
|
|
let bindings = []
|
|
|
|
const safeUser = makePropSafe("user")
|
|
|
|
const safeOAuth2 = makePropSafe("oauth2")
|
|
|
|
const safeAccessToken = makePropSafe("accessToken")
|
|
|
|
|
|
|
|
const authBindings = [
|
|
|
|
{
|
|
|
|
runtime: `${safeUser}.${safeOAuth2}.${safeAccessToken}`,
|
|
|
|
readable: `Current User.OAuthToken`,
|
|
|
|
key: "accessToken",
|
2023-05-26 11:11:51 +02:00
|
|
|
display: { name: "OAuthToken", type: "text" },
|
2022-07-01 18:27:24 +02:00
|
|
|
},
|
2021-08-13 12:24:47 +02:00
|
|
|
]
|
2022-07-01 18:27:24 +02:00
|
|
|
|
2022-09-20 11:07:07 +02:00
|
|
|
bindings = authBindings.map(fieldBinding => {
|
2022-07-01 18:27:24 +02:00
|
|
|
return {
|
|
|
|
type: "context",
|
|
|
|
runtimeBinding: fieldBinding.runtime,
|
|
|
|
readableBinding: fieldBinding.readable,
|
|
|
|
fieldSchema: { type: "string", name: fieldBinding.key },
|
|
|
|
providerId: "user",
|
2022-09-16 18:32:50 +02:00
|
|
|
category: "Current User",
|
2022-09-20 11:07:07 +02:00
|
|
|
display: fieldBinding.display,
|
2022-07-01 18:27:24 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
return bindings
|
|
|
|
}
|
|
|
|
|
2023-01-25 12:39:13 +01:00
|
|
|
export const getEnvironmentBindings = () => {
|
|
|
|
let envVars = get(environment).variables
|
2023-01-26 15:59:29 +01:00
|
|
|
return envVars.map(variable => {
|
2023-01-25 12:39:13 +01:00
|
|
|
return {
|
|
|
|
type: "context",
|
|
|
|
runtimeBinding: `env.${makePropSafe(variable.name)}`,
|
|
|
|
readableBinding: `env.${variable.name}`,
|
|
|
|
category: "Environment",
|
|
|
|
icon: "Key",
|
|
|
|
display: { type: "string", name: variable.name },
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-07-01 18:27:24 +02:00
|
|
|
/**
|
|
|
|
* Utility - convert a key/value map to an array of custom 'context' bindings
|
|
|
|
* @param {object} valueMap Key/value pairings
|
|
|
|
* @param {string} prefix A contextual string prefix/path for a user readable binding
|
|
|
|
* @return {object[]} An array containing readable/runtime binding objects
|
|
|
|
*/
|
2022-09-16 18:32:50 +02:00
|
|
|
export const toBindingsArray = (valueMap, prefix, category) => {
|
2022-07-01 18:27:24 +02:00
|
|
|
if (!valueMap) {
|
|
|
|
return []
|
|
|
|
}
|
2022-07-03 18:33:25 +02:00
|
|
|
return Object.keys(valueMap).reduce((acc, binding) => {
|
2023-04-04 10:12:15 +02:00
|
|
|
if (!binding) {
|
2022-07-03 18:33:25 +02:00
|
|
|
return acc
|
|
|
|
}
|
2022-09-16 18:32:50 +02:00
|
|
|
|
|
|
|
let config = {
|
2022-07-01 18:27:24 +02:00
|
|
|
type: "context",
|
|
|
|
runtimeBinding: binding,
|
|
|
|
readableBinding: `${prefix}.${binding}`,
|
2022-09-16 18:32:50 +02:00
|
|
|
icon: "Brackets",
|
|
|
|
}
|
|
|
|
|
|
|
|
if (category) {
|
|
|
|
config.category = category
|
|
|
|
}
|
|
|
|
|
|
|
|
acc.push(config)
|
|
|
|
|
2022-07-03 18:33:25 +02:00
|
|
|
return acc
|
|
|
|
}, [])
|
2022-06-14 11:14:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Utility - coverting a map of readable bindings to runtime
|
|
|
|
*/
|
|
|
|
export const readableToRuntimeMap = (bindings, ctx) => {
|
|
|
|
if (!bindings || !ctx) {
|
|
|
|
return {}
|
|
|
|
}
|
|
|
|
return Object.keys(ctx).reduce((acc, key) => {
|
2022-09-15 20:35:55 +02:00
|
|
|
acc[key] = readableToRuntimeBinding(bindings, ctx[key])
|
2022-06-14 11:14:05 +02:00
|
|
|
return acc
|
|
|
|
}, {})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Utility - coverting a map of runtime bindings to readable
|
|
|
|
*/
|
|
|
|
export const runtimeToReadableMap = (bindings, ctx) => {
|
|
|
|
if (!bindings || !ctx) {
|
|
|
|
return {}
|
|
|
|
}
|
|
|
|
return Object.keys(ctx).reduce((acc, key) => {
|
2022-09-15 20:35:55 +02:00
|
|
|
acc[key] = runtimeToReadableBinding(bindings, ctx[key])
|
2022-06-14 11:14:05 +02:00
|
|
|
return acc
|
|
|
|
}, {})
|
2021-01-14 16:39:50 +01:00
|
|
|
}
|
|
|
|
|
2021-11-08 15:35:58 +01:00
|
|
|
/**
|
|
|
|
* Gets the bindable properties exposed by a certain component.
|
|
|
|
*/
|
|
|
|
export const getComponentBindableProperties = (asset, componentId) => {
|
|
|
|
if (!asset || !componentId) {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that the component exists and exposes context
|
|
|
|
const component = findComponent(asset.props, componentId)
|
|
|
|
const def = store.actions.components.getDefinition(component?._component)
|
|
|
|
if (!def?.context) {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the bindings for the component
|
|
|
|
return getProviderContextBindings(asset, component)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
2022-04-01 13:51:23 +02:00
|
|
|
export const getContextProviderComponents = (
|
|
|
|
asset,
|
|
|
|
componentId,
|
|
|
|
type,
|
|
|
|
options = { includeSelf: false }
|
|
|
|
) => {
|
2021-02-15 21:03:29 +01:00
|
|
|
if (!asset || !componentId) {
|
2021-01-14 16:39:50 +01:00
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
2021-01-19 16:39:04 +01:00
|
|
|
// Get the component tree leading up to this component, ignoring the component
|
|
|
|
// itself
|
2021-02-15 21:03:29 +01:00
|
|
|
const path = findComponentPath(asset.props, componentId)
|
2022-04-01 13:51:23 +02:00
|
|
|
if (!options?.includeSelf) {
|
|
|
|
path.pop()
|
|
|
|
}
|
2021-01-14 16:39:50 +01:00
|
|
|
|
2021-01-19 18:38:24 +01:00
|
|
|
// Filter by only data provider components
|
2021-05-04 12:32:22 +02:00
|
|
|
return path.filter(component => {
|
2021-01-19 18:38:24 +01:00
|
|
|
const def = store.actions.components.getDefinition(component._component)
|
2021-12-01 11:32:55 +01:00
|
|
|
if (!def?.context) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// If no type specified, return anything that exposes context
|
|
|
|
if (!type) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise only match components with the specific context type
|
|
|
|
const contexts = Array.isArray(def.context) ? def.context : [def.context]
|
|
|
|
return contexts.find(context => context.type === type) != null
|
2021-01-19 18:38:24 +01:00
|
|
|
})
|
|
|
|
}
|
2021-01-14 16:39:50 +01:00
|
|
|
|
2021-02-01 19:51:22 +01:00
|
|
|
/**
|
|
|
|
* Gets all data provider components above a component.
|
|
|
|
*/
|
2021-02-15 21:03:29 +01:00
|
|
|
export const getActionProviderComponents = (asset, componentId, actionType) => {
|
|
|
|
if (!asset || !componentId) {
|
2021-02-01 19:51:22 +01:00
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the component tree leading up to this component, ignoring the component
|
|
|
|
// itself
|
2021-02-15 21:03:29 +01:00
|
|
|
const path = findComponentPath(asset.props, componentId)
|
2021-02-01 19:51:22 +01:00
|
|
|
path.pop()
|
|
|
|
|
|
|
|
// Filter by only data provider components
|
2021-05-04 12:32:22 +02:00
|
|
|
return path.filter(component => {
|
2021-02-01 19:51:22 +01:00
|
|
|
const def = store.actions.components.getDefinition(component._component)
|
|
|
|
return def?.actions?.includes(actionType)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-01-19 18:38:24 +01:00
|
|
|
/**
|
|
|
|
* Gets a datasource object for a certain data provider component
|
|
|
|
*/
|
2021-03-18 18:34:51 +01:00
|
|
|
export const getDatasourceForProvider = (asset, component) => {
|
2021-11-05 13:37:42 +01:00
|
|
|
const settings = getComponentSettings(component?._component)
|
2021-01-19 16:39:04 +01:00
|
|
|
|
2021-03-18 18:34:51 +01:00
|
|
|
// If this component has a dataProvider setting, go up the stack and use it
|
2021-11-04 12:30:43 +01:00
|
|
|
const dataProviderSetting = settings.find(setting => {
|
2021-03-18 18:34:51 +01:00
|
|
|
return setting.type === "dataProvider"
|
|
|
|
})
|
|
|
|
if (dataProviderSetting) {
|
|
|
|
const settingValue = component[dataProviderSetting.key]
|
2021-03-22 18:57:19 +01:00
|
|
|
const providerId = extractLiteralHandlebarsID(settingValue)
|
2022-03-08 18:57:36 +01:00
|
|
|
const provider = findComponent(asset?.props, providerId)
|
2021-03-22 18:57:19 +01:00
|
|
|
return getDatasourceForProvider(asset, provider)
|
2021-03-18 18:34:51 +01:00
|
|
|
}
|
|
|
|
|
2021-01-19 18:38:24 +01:00
|
|
|
// Extract datasource from component instance
|
2021-03-16 20:11:00 +01:00
|
|
|
const validSettingTypes = ["dataSource", "table", "schema"]
|
2021-11-04 12:30:43 +01:00
|
|
|
const datasourceSetting = settings.find(setting => {
|
2021-02-04 19:11:56 +01:00
|
|
|
return validSettingTypes.includes(setting.type)
|
2021-01-19 18:38:24 +01:00
|
|
|
})
|
|
|
|
if (!datasourceSetting) {
|
|
|
|
return null
|
|
|
|
}
|
2022-08-25 10:47:04 +02:00
|
|
|
|
|
|
|
// For legacy compatibility, we need to be able to handle datasources that are
|
|
|
|
// just strings. These are not generated any more, so could be removed in
|
|
|
|
// future.
|
|
|
|
// TODO: remove at some point
|
|
|
|
const datasource = component[datasourceSetting?.key]
|
|
|
|
if (typeof datasource === "string") {
|
|
|
|
return {
|
|
|
|
tableId: datasource,
|
|
|
|
type: "table",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return datasource
|
2021-01-19 18:38:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-02-15 21:03:29 +01:00
|
|
|
* Gets all bindable data properties from component data contexts.
|
2021-01-19 18:38:24 +01:00
|
|
|
*/
|
2021-02-15 21:03:29 +01:00
|
|
|
const getContextBindings = (asset, componentId) => {
|
2021-01-19 18:38:24 +01:00
|
|
|
// Extract any components which provide data contexts
|
2021-11-30 15:32:18 +01:00
|
|
|
const dataProviders = getContextProviderComponents(asset, componentId)
|
2021-11-08 15:35:58 +01:00
|
|
|
|
|
|
|
// Generate bindings for all matching components
|
|
|
|
return getProviderContextBindings(asset, dataProviders)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the context bindings exposed by a set of data provider components.
|
|
|
|
*/
|
|
|
|
const getProviderContextBindings = (asset, dataProviders) => {
|
|
|
|
if (!asset || !dataProviders) {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure providers is an array
|
|
|
|
if (!Array.isArray(dataProviders)) {
|
|
|
|
dataProviders = [dataProviders]
|
|
|
|
}
|
2021-01-19 16:39:04 +01:00
|
|
|
|
2021-02-04 16:11:05 +01:00
|
|
|
// Create bindings for each data provider
|
2021-11-08 15:35:58 +01:00
|
|
|
let bindings = []
|
2021-05-04 12:32:22 +02:00
|
|
|
dataProviders.forEach(component => {
|
2021-03-16 14:54:34 +01:00
|
|
|
const def = store.actions.components.getDefinition(component._component)
|
2021-08-18 15:58:58 +02:00
|
|
|
const contexts = Array.isArray(def.context) ? def.context : [def.context]
|
|
|
|
|
|
|
|
// Create bindings for each context block provided by this data provider
|
|
|
|
contexts.forEach(context => {
|
|
|
|
if (!context?.type) {
|
2021-02-04 16:11:05 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-18 15:58:58 +02:00
|
|
|
let schema
|
2021-12-06 12:41:17 +01:00
|
|
|
let table
|
2021-08-18 15:58:58 +02:00
|
|
|
let readablePrefix
|
2021-11-08 15:35:58 +01:00
|
|
|
let runtimeSuffix = context.suffix
|
2021-08-18 15:58:58 +02:00
|
|
|
|
|
|
|
if (context.type === "form") {
|
|
|
|
// Forms do not need table schemas
|
|
|
|
// Their schemas are built from their component field names
|
|
|
|
schema = buildFormSchema(component)
|
|
|
|
readablePrefix = "Fields"
|
|
|
|
} else if (context.type === "static") {
|
|
|
|
// Static contexts are fully defined by the components
|
|
|
|
schema = {}
|
|
|
|
const values = context.values || []
|
|
|
|
values.forEach(value => {
|
2022-08-30 11:16:15 +02:00
|
|
|
schema[value.key] = {
|
|
|
|
name: value.label,
|
|
|
|
type: value.type || "string",
|
|
|
|
}
|
2021-08-18 15:58:58 +02:00
|
|
|
})
|
|
|
|
} else if (context.type === "schema") {
|
|
|
|
// Schema contexts are generated dynamically depending on their data
|
|
|
|
const datasource = getDatasourceForProvider(asset, component)
|
|
|
|
if (!datasource) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const info = getSchemaForDatasource(asset, datasource)
|
|
|
|
schema = info.schema
|
2021-12-06 12:41:17 +01:00
|
|
|
table = info.table
|
|
|
|
|
|
|
|
// For JSON arrays, use the array name as the readable prefix.
|
|
|
|
// Otherwise use the table name
|
|
|
|
if (datasource.type === "jsonarray") {
|
|
|
|
const split = datasource.label.split(".")
|
|
|
|
readablePrefix = split[split.length - 1]
|
|
|
|
} else {
|
|
|
|
readablePrefix = info.table?.name
|
|
|
|
}
|
2021-01-14 16:39:50 +01:00
|
|
|
}
|
2021-08-18 15:58:58 +02:00
|
|
|
if (!schema) {
|
|
|
|
return
|
2021-03-16 20:11:00 +01:00
|
|
|
}
|
2021-08-18 15:58:58 +02:00
|
|
|
|
|
|
|
const keys = Object.keys(schema).sort()
|
|
|
|
|
2021-11-08 15:35:58 +01:00
|
|
|
// Generate safe unique runtime prefix
|
2021-11-15 13:25:01 +01:00
|
|
|
let providerId = component._id
|
2021-11-08 15:35:58 +01:00
|
|
|
if (runtimeSuffix) {
|
2021-11-15 13:25:01 +01:00
|
|
|
providerId += `-${runtimeSuffix}`
|
2021-11-08 15:35:58 +01:00
|
|
|
}
|
2021-11-15 13:25:01 +01:00
|
|
|
const safeComponentId = makePropSafe(providerId)
|
2021-11-08 15:35:58 +01:00
|
|
|
|
2021-08-18 15:58:58 +02:00
|
|
|
// Create bindable properties for each schema field
|
|
|
|
keys.forEach(key => {
|
|
|
|
const fieldSchema = schema[key]
|
|
|
|
|
2021-11-09 16:45:32 +01:00
|
|
|
// Make safe runtime binding
|
2021-12-06 12:41:17 +01:00
|
|
|
const safeKey = key.split(".").map(makePropSafe).join(".")
|
|
|
|
const runtimeBinding = `${safeComponentId}.${safeKey}`
|
2021-08-18 15:58:58 +02:00
|
|
|
|
|
|
|
// Optionally use a prefix with readable bindings
|
|
|
|
let readableBinding = component._instanceName
|
|
|
|
if (readablePrefix) {
|
|
|
|
readableBinding += `.${readablePrefix}`
|
|
|
|
}
|
|
|
|
readableBinding += `.${fieldSchema.name || key}`
|
|
|
|
|
|
|
|
// Create the binding object
|
|
|
|
bindings.push({
|
|
|
|
type: "context",
|
|
|
|
runtimeBinding,
|
|
|
|
readableBinding,
|
|
|
|
// Field schema and provider are required to construct relationship
|
|
|
|
// datasource options, based on bindable properties
|
|
|
|
fieldSchema,
|
2021-11-15 13:25:01 +01:00
|
|
|
providerId,
|
2021-12-06 12:41:17 +01:00
|
|
|
// Table ID is used by JSON fields to know what table the field is in
|
|
|
|
tableId: table?._id,
|
2023-01-19 10:47:10 +01:00
|
|
|
component: component._component,
|
2022-08-30 11:16:15 +02:00
|
|
|
category: component._instanceName,
|
|
|
|
icon: def.icon,
|
|
|
|
display: {
|
|
|
|
name: fieldSchema.name || key,
|
|
|
|
type: fieldSchema.type,
|
|
|
|
},
|
2021-08-18 15:58:58 +02:00
|
|
|
})
|
2021-01-14 16:39:50 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2021-01-28 15:29:35 +01:00
|
|
|
|
2021-02-15 21:03:29 +01:00
|
|
|
return bindings
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets all bindable properties from the logged in user.
|
|
|
|
*/
|
2022-09-15 17:39:25 +02:00
|
|
|
export const getUserBindings = () => {
|
2021-02-15 21:03:29 +01:00
|
|
|
let bindings = []
|
2023-08-24 17:12:12 +02:00
|
|
|
const { schema } = getSchemaForDatasourcePlus(TableNames.USERS)
|
2021-01-28 15:29:35 +01:00
|
|
|
const keys = Object.keys(schema).sort()
|
2021-03-22 13:10:43 +01:00
|
|
|
const safeUser = makePropSafe("user")
|
2022-09-20 12:09:58 +02:00
|
|
|
|
|
|
|
bindings = keys.reduce((acc, key) => {
|
2021-01-28 15:29:35 +01:00
|
|
|
const fieldSchema = schema[key]
|
2022-10-05 13:40:53 +02:00
|
|
|
acc.push({
|
2021-01-28 15:29:35 +01:00
|
|
|
type: "context",
|
2021-11-09 16:45:32 +01:00
|
|
|
runtimeBinding: `${safeUser}.${makePropSafe(key)}`,
|
2021-01-28 15:29:35 +01:00
|
|
|
readableBinding: `Current User.${key}`,
|
2021-02-04 16:11:05 +01:00
|
|
|
// Field schema and provider are required to construct relationship
|
|
|
|
// datasource options, based on bindable properties
|
2021-01-28 15:29:35 +01:00
|
|
|
fieldSchema,
|
|
|
|
providerId: "user",
|
2022-08-30 11:16:15 +02:00
|
|
|
category: "Current User",
|
|
|
|
icon: "User",
|
2023-05-26 11:11:51 +02:00
|
|
|
display: {
|
|
|
|
name: key,
|
|
|
|
},
|
2021-01-28 15:29:35 +01:00
|
|
|
})
|
2022-09-20 12:09:58 +02:00
|
|
|
return acc
|
|
|
|
}, [])
|
|
|
|
|
2021-02-15 21:03:29 +01:00
|
|
|
return bindings
|
|
|
|
}
|
|
|
|
|
2021-08-13 12:24:47 +02:00
|
|
|
/**
|
|
|
|
* Gets all device bindings that are globally available.
|
|
|
|
*/
|
|
|
|
const getDeviceBindings = () => {
|
|
|
|
let bindings = []
|
|
|
|
if (get(store).clientFeatures?.deviceAwareness) {
|
|
|
|
const safeDevice = makePropSafe("device")
|
|
|
|
bindings.push({
|
|
|
|
type: "context",
|
|
|
|
runtimeBinding: `${safeDevice}.${makePropSafe("mobile")}`,
|
|
|
|
readableBinding: `Device.Mobile`,
|
2022-08-30 11:16:15 +02:00
|
|
|
category: "Device",
|
|
|
|
icon: "DevicePhone",
|
|
|
|
display: { type: "boolean", name: "mobile" },
|
2021-08-13 12:24:47 +02:00
|
|
|
})
|
|
|
|
bindings.push({
|
|
|
|
type: "context",
|
|
|
|
runtimeBinding: `${safeDevice}.${makePropSafe("tablet")}`,
|
|
|
|
readableBinding: `Device.Tablet`,
|
2022-08-30 11:16:15 +02:00
|
|
|
category: "Device",
|
|
|
|
icon: "DevicePhone",
|
|
|
|
display: { type: "boolean", name: "tablet" },
|
2021-08-13 12:24:47 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return bindings
|
|
|
|
}
|
|
|
|
|
2022-02-11 12:55:35 +01:00
|
|
|
/**
|
2022-02-25 09:25:56 +01:00
|
|
|
* Gets all selected rows bindings for tables in the current asset.
|
2022-02-11 12:55:35 +01:00
|
|
|
*/
|
2022-02-25 09:25:56 +01:00
|
|
|
const getSelectedRowsBindings = asset => {
|
2022-02-11 12:55:35 +01:00
|
|
|
let bindings = []
|
|
|
|
if (get(store).clientFeatures?.rowSelection) {
|
2022-02-24 10:17:27 +01:00
|
|
|
// Add bindings for table components
|
2022-02-25 09:25:56 +01:00
|
|
|
let tables = findAllMatchingComponents(asset?.props, component =>
|
2022-02-24 10:17:27 +01:00
|
|
|
component._component.endsWith("table")
|
|
|
|
)
|
2022-02-11 12:55:35 +01:00
|
|
|
const safeState = makePropSafe("rowSelection")
|
2022-02-24 10:17:27 +01:00
|
|
|
bindings = bindings.concat(
|
|
|
|
tables.map(table => ({
|
|
|
|
type: "context",
|
2022-03-07 13:06:11 +01:00
|
|
|
runtimeBinding: `${safeState}.${makePropSafe(table._id)}.${makePropSafe(
|
|
|
|
"selectedRows"
|
|
|
|
)}`,
|
2022-02-24 10:17:27 +01:00
|
|
|
readableBinding: `${table._instanceName}.Selected rows`,
|
2022-08-30 11:16:15 +02:00
|
|
|
category: "Selected rows",
|
|
|
|
icon: "ViewRow",
|
2023-07-14 10:11:34 +02:00
|
|
|
display: { name: table._instanceName },
|
2022-02-24 10:17:27 +01:00
|
|
|
}))
|
|
|
|
)
|
|
|
|
|
|
|
|
// Add bindings for table blocks
|
2022-02-25 09:25:56 +01:00
|
|
|
let tableBlocks = findAllMatchingComponents(asset?.props, component =>
|
2022-02-24 10:17:27 +01:00
|
|
|
component._component.endsWith("tableblock")
|
|
|
|
)
|
|
|
|
bindings = bindings.concat(
|
|
|
|
tableBlocks.map(block => ({
|
|
|
|
type: "context",
|
2022-03-07 13:06:11 +01:00
|
|
|
runtimeBinding: `${safeState}.${makePropSafe(
|
|
|
|
block._id + "-table"
|
|
|
|
)}.${makePropSafe("selectedRows")}`,
|
2022-02-24 10:17:27 +01:00
|
|
|
readableBinding: `${block._instanceName}.Selected rows`,
|
2022-10-15 14:06:34 +02:00
|
|
|
category: "Selected rows",
|
2023-07-14 10:11:34 +02:00
|
|
|
display: { name: block._instanceName },
|
2022-02-24 10:17:27 +01:00
|
|
|
}))
|
|
|
|
)
|
2022-02-11 12:55:35 +01:00
|
|
|
}
|
|
|
|
return bindings
|
|
|
|
}
|
|
|
|
|
2023-02-01 18:53:50 +01:00
|
|
|
export const makeStateBinding = key => {
|
|
|
|
return {
|
|
|
|
type: "context",
|
|
|
|
runtimeBinding: `${makePropSafe("state")}.${makePropSafe(key)}`,
|
|
|
|
readableBinding: `State.${key}`,
|
|
|
|
category: "State",
|
|
|
|
icon: "AutomatedSegment",
|
2023-05-29 22:19:44 +02:00
|
|
|
display: { name: key },
|
2023-02-01 18:53:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-26 12:28:44 +02:00
|
|
|
/**
|
|
|
|
* Gets all state bindings that are globally available.
|
|
|
|
*/
|
|
|
|
const getStateBindings = () => {
|
2021-09-02 12:39:41 +02:00
|
|
|
let bindings = []
|
2022-02-22 16:18:08 +01:00
|
|
|
if (get(store).clientFeatures?.state) {
|
2023-02-01 18:53:50 +01:00
|
|
|
bindings = getAllStateVariables().map(makeStateBinding)
|
2021-09-02 12:39:41 +02:00
|
|
|
}
|
|
|
|
return bindings
|
2021-08-26 12:28:44 +02:00
|
|
|
}
|
|
|
|
|
2021-02-15 21:03:29 +01:00
|
|
|
/**
|
|
|
|
* Gets all bindable properties from URL parameters.
|
|
|
|
*/
|
2021-05-04 12:32:22 +02:00
|
|
|
const getUrlBindings = asset => {
|
2021-02-15 21:03:29 +01:00
|
|
|
const url = asset?.routing?.route ?? ""
|
|
|
|
const split = url.split("/")
|
|
|
|
let params = []
|
2021-05-04 12:32:22 +02:00
|
|
|
split.forEach(part => {
|
2021-02-15 21:03:29 +01:00
|
|
|
if (part.startsWith(":") && part.length > 1) {
|
|
|
|
params.push(part.replace(/:/g, "").replace(/\?/g, ""))
|
|
|
|
}
|
|
|
|
})
|
2021-03-22 13:10:43 +01:00
|
|
|
const safeURL = makePropSafe("url")
|
2022-06-21 10:39:25 +02:00
|
|
|
const urlParamBindings = params.map(param => ({
|
2021-02-15 21:03:29 +01:00
|
|
|
type: "context",
|
2021-03-22 13:10:43 +01:00
|
|
|
runtimeBinding: `${safeURL}.${makePropSafe(param)}`,
|
2021-02-15 21:03:29 +01:00
|
|
|
readableBinding: `URL.${param}`,
|
2022-08-30 11:16:15 +02:00
|
|
|
category: "URL",
|
|
|
|
icon: "RailTop",
|
2023-05-26 11:11:51 +02:00
|
|
|
display: { type: "string", name: param },
|
2021-02-15 21:03:29 +01:00
|
|
|
}))
|
2022-06-21 10:39:25 +02:00
|
|
|
const queryParamsBinding = {
|
|
|
|
type: "context",
|
|
|
|
runtimeBinding: makePropSafe("query"),
|
|
|
|
readableBinding: "Query params",
|
2022-08-30 11:16:15 +02:00
|
|
|
category: "URL",
|
|
|
|
icon: "RailTop",
|
2023-05-26 11:11:51 +02:00
|
|
|
display: { type: "object", name: "Query params" },
|
2022-06-21 10:39:25 +02:00
|
|
|
}
|
|
|
|
return urlParamBindings.concat([queryParamsBinding])
|
2021-01-14 16:39:50 +01:00
|
|
|
}
|
|
|
|
|
2022-04-05 17:05:29 +02:00
|
|
|
const getRoleBindings = () => {
|
|
|
|
return (get(rolesStore) || []).map(role => {
|
|
|
|
return {
|
|
|
|
type: "context",
|
|
|
|
runtimeBinding: `trim "${role._id}"`,
|
2022-04-06 09:22:10 +02:00
|
|
|
readableBinding: `Role.${role.name}`,
|
2022-08-30 11:16:15 +02:00
|
|
|
category: "Role",
|
|
|
|
icon: "UserGroup",
|
|
|
|
display: { type: "string", name: role.name },
|
2022-04-05 17:05:29 +02:00
|
|
|
}
|
|
|
|
})
|
2021-01-14 16:39:50 +01:00
|
|
|
}
|
|
|
|
|
2021-12-08 16:31:06 +01:00
|
|
|
/**
|
2022-08-18 14:43:32 +02:00
|
|
|
* Gets all bindable properties exposed in an event action flow up until
|
2022-03-15 12:16:51 +01:00
|
|
|
* the specified action ID, as well as context provided for the action
|
|
|
|
* setting as a whole by the component.
|
2021-12-08 16:31:06 +01:00
|
|
|
*/
|
2022-08-18 14:43:32 +02:00
|
|
|
export const getEventContextBindings = (
|
2022-03-15 12:16:51 +01:00
|
|
|
asset,
|
|
|
|
componentId,
|
|
|
|
settingKey,
|
|
|
|
actions,
|
|
|
|
actionId
|
|
|
|
) => {
|
|
|
|
let bindings = []
|
|
|
|
// Check if any context bindings are provided by the component for this
|
|
|
|
// setting
|
|
|
|
const component = findComponent(asset.props, componentId)
|
2022-08-30 11:16:15 +02:00
|
|
|
const def = store.actions.components.getDefinition(component?._component)
|
2022-03-15 12:16:51 +01:00
|
|
|
const settings = getComponentSettings(component?._component)
|
|
|
|
const eventSetting = settings.find(setting => setting.key === settingKey)
|
2022-08-18 12:16:59 +02:00
|
|
|
if (eventSetting?.context?.length) {
|
2022-03-15 12:16:51 +01:00
|
|
|
eventSetting.context.forEach(contextEntry => {
|
|
|
|
bindings.push({
|
|
|
|
readableBinding: contextEntry.label,
|
|
|
|
runtimeBinding: `${makePropSafe("eventContext")}.${makePropSafe(
|
|
|
|
contextEntry.key
|
|
|
|
)}`,
|
2022-08-30 11:16:15 +02:00
|
|
|
category: component._instanceName,
|
|
|
|
icon: def.icon,
|
2023-06-01 12:47:27 +02:00
|
|
|
display: {
|
|
|
|
name: contextEntry.label,
|
|
|
|
},
|
2022-03-15 12:16:51 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-12-09 09:36:50 +01:00
|
|
|
// Get the steps leading up to this value
|
|
|
|
const index = actions?.findIndex(action => action.id === actionId)
|
|
|
|
if (index == null || index === -1) {
|
2022-03-15 12:16:51 +01:00
|
|
|
return bindings
|
2021-12-08 16:31:06 +01:00
|
|
|
}
|
2021-12-09 09:36:50 +01:00
|
|
|
const prevActions = actions.slice(0, index)
|
2021-12-08 16:31:06 +01:00
|
|
|
|
|
|
|
// Generate bindings for any steps which provide context
|
|
|
|
prevActions.forEach((action, idx) => {
|
2021-12-13 11:55:45 +01:00
|
|
|
const def = ActionDefinitions.actions.find(
|
2021-12-08 16:31:06 +01:00
|
|
|
x => x.name === action["##eventHandlerType"]
|
|
|
|
)
|
|
|
|
if (def.context) {
|
|
|
|
def.context.forEach(contextValue => {
|
|
|
|
bindings.push({
|
|
|
|
readableBinding: `Action ${idx + 1}.${contextValue.label}`,
|
|
|
|
runtimeBinding: `actions.${idx}.${contextValue.value}`,
|
2022-08-30 11:16:15 +02:00
|
|
|
category: "Actions",
|
|
|
|
icon: "JourneyAction",
|
2023-06-01 12:47:27 +02:00
|
|
|
display: {
|
|
|
|
name: contextValue.label,
|
|
|
|
},
|
2021-12-08 16:31:06 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2022-03-15 12:16:51 +01:00
|
|
|
|
2021-12-08 16:31:06 +01:00
|
|
|
return bindings
|
|
|
|
}
|
|
|
|
|
2022-02-15 15:59:11 +01:00
|
|
|
/**
|
2023-08-24 17:12:12 +02:00
|
|
|
* Gets the schema for a certain datasource plus.
|
2022-02-15 15:59:11 +01:00
|
|
|
* The options which can be passed in are:
|
|
|
|
* formSchema: whether the schema is for a form
|
|
|
|
* searchableSchema: whether to generate a searchable schema, which may have
|
|
|
|
* fewer fields than a readable schema
|
2023-08-24 17:12:12 +02:00
|
|
|
* @param resourceId the DS+ resource ID
|
2022-02-15 15:59:11 +01:00
|
|
|
* @param options options for generating the schema
|
|
|
|
* @return {{schema: Object, table: Object}}
|
|
|
|
*/
|
2023-08-24 17:12:12 +02:00
|
|
|
export const getSchemaForDatasourcePlus = (resourceId, options) => {
|
|
|
|
const isViewV2 = resourceId?.includes("view_ta_")
|
|
|
|
const datasource = isViewV2
|
|
|
|
? {
|
|
|
|
type: "viewV2",
|
|
|
|
id: resourceId,
|
|
|
|
tableId: resourceId.split("_").slice(1, 3).join("_"),
|
|
|
|
}
|
|
|
|
: { type: "table", tableId: resourceId }
|
|
|
|
return getSchemaForDatasource(null, datasource, options)
|
2022-02-15 15:59:11 +01:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:39:50 +01:00
|
|
|
/**
|
|
|
|
* Gets a schema for a datasource object.
|
2022-02-15 15:59:11 +01:00
|
|
|
* The options which can be passed in are:
|
|
|
|
* formSchema: whether the schema is for a form
|
|
|
|
* searchableSchema: whether to generate a searchable schema, which may have
|
|
|
|
* fewer fields than a readable schema
|
|
|
|
* @param asset the current root client app asset (layout or screen). This is
|
|
|
|
* optional and only needed for "provider" datasource types.
|
|
|
|
* @param datasource the datasource definition
|
|
|
|
* @param options options for generating the schema
|
|
|
|
* @return {{schema: Object, table: Object}}
|
2021-01-14 16:39:50 +01:00
|
|
|
*/
|
2022-02-15 15:59:11 +01:00
|
|
|
export const getSchemaForDatasource = (asset, datasource, options) => {
|
|
|
|
options = options || {}
|
2021-01-19 18:38:24 +01:00
|
|
|
let schema, table
|
2021-11-12 14:42:55 +01:00
|
|
|
|
2021-01-19 18:38:24 +01:00
|
|
|
if (datasource) {
|
|
|
|
const { type } = datasource
|
2021-12-06 12:41:17 +01:00
|
|
|
const tables = get(tablesStore).list
|
2021-11-12 14:42:55 +01:00
|
|
|
|
2021-12-06 12:41:17 +01:00
|
|
|
// Determine the entity which backs this datasource.
|
|
|
|
// "provider" datasources are those targeting another data provider
|
2021-05-27 16:11:08 +02:00
|
|
|
if (type === "provider") {
|
2022-03-08 18:57:36 +01:00
|
|
|
const component = findComponent(asset?.props, datasource.providerId)
|
2021-05-27 16:11:08 +02:00
|
|
|
const source = getDatasourceForProvider(asset, component)
|
2022-02-15 15:59:11 +01:00
|
|
|
return getSchemaForDatasource(asset, source, options)
|
2021-12-06 12:41:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// "query" datasources are those targeting non-plus datasources or
|
|
|
|
// custom queries
|
|
|
|
else if (type === "query") {
|
2021-04-13 10:20:30 +02:00
|
|
|
const queries = get(queriesStores).list
|
2021-05-04 12:32:22 +02:00
|
|
|
table = queries.find(query => query._id === datasource._id)
|
2021-12-06 12:41:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// "field" datasources are array-like fields of rows, such as attachments
|
|
|
|
// or multi-select fields
|
|
|
|
else if (type === "field") {
|
2021-11-12 16:19:25 +01:00
|
|
|
table = { name: datasource.fieldName }
|
2021-11-12 14:42:55 +01:00
|
|
|
const { fieldType } = datasource
|
|
|
|
if (fieldType === "attachment") {
|
|
|
|
schema = {
|
|
|
|
url: {
|
|
|
|
type: "string",
|
|
|
|
},
|
|
|
|
name: {
|
|
|
|
type: "string",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
} else if (fieldType === "array") {
|
|
|
|
schema = {
|
|
|
|
value: {
|
|
|
|
type: "string",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2021-12-06 12:41:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// "jsonarray" datasources are arrays inside JSON fields
|
|
|
|
else if (type === "jsonarray") {
|
|
|
|
table = tables.find(table => table._id === datasource.tableId)
|
2021-12-07 22:19:14 +01:00
|
|
|
let tableSchema = table?.schema
|
2022-01-20 12:19:37 +01:00
|
|
|
schema = JSONUtils.getJSONArrayDatasourceSchema(tableSchema, datasource)
|
2021-12-06 12:41:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise we assume we're targeting an internal table or a plus
|
|
|
|
// datasource, and we can treat it as a table with a schema
|
|
|
|
else {
|
2021-05-04 12:32:22 +02:00
|
|
|
table = tables.find(table => table._id === datasource.tableId)
|
2021-01-21 11:40:45 +01:00
|
|
|
}
|
2021-11-12 14:42:55 +01:00
|
|
|
|
2021-12-06 12:41:17 +01:00
|
|
|
// Determine the schema from the backing entity if not already determined
|
2021-11-12 14:42:55 +01:00
|
|
|
if (table && !schema) {
|
2021-01-21 11:40:45 +01:00
|
|
|
if (type === "view") {
|
2023-08-09 17:39:23 +02:00
|
|
|
// Old views
|
2021-01-19 18:38:24 +01:00
|
|
|
schema = cloneDeep(table.views?.[datasource.name]?.schema)
|
2023-08-09 17:39:23 +02:00
|
|
|
} else if (type === "viewV2") {
|
|
|
|
// New views which are DS+
|
2023-08-24 17:12:12 +02:00
|
|
|
const view = Object.values(table.views || {}).find(
|
|
|
|
view => view.id === datasource.id
|
|
|
|
)
|
2023-08-09 17:39:23 +02:00
|
|
|
schema = cloneDeep(view?.schema)
|
|
|
|
|
|
|
|
// Strip hidden fields
|
|
|
|
Object.keys(schema || {}).forEach(field => {
|
|
|
|
if (!schema[field].visible) {
|
|
|
|
delete schema[field]
|
|
|
|
}
|
|
|
|
})
|
2022-02-15 15:59:11 +01:00
|
|
|
} else if (
|
|
|
|
type === "query" &&
|
|
|
|
(options.formSchema || options.searchableSchema)
|
|
|
|
) {
|
|
|
|
// For queries, if we are generating a schema for a form or a searchable
|
|
|
|
// schema then we want to use the query parameters rather than the
|
|
|
|
// query schema
|
2021-02-04 14:01:49 +01:00
|
|
|
schema = {}
|
|
|
|
const params = table.parameters || []
|
2021-05-04 12:32:22 +02:00
|
|
|
params.forEach(param => {
|
2021-02-11 10:17:36 +01:00
|
|
|
if (param?.name) {
|
|
|
|
schema[param.name] = { ...param, type: "string" }
|
|
|
|
}
|
2021-02-04 14:01:49 +01:00
|
|
|
})
|
2021-01-21 11:40:45 +01:00
|
|
|
} else {
|
2022-02-15 15:59:11 +01:00
|
|
|
// Otherwise we just want the schema of the table
|
2021-01-19 18:38:24 +01:00
|
|
|
schema = cloneDeep(table.schema)
|
|
|
|
}
|
2021-01-14 16:39:50 +01:00
|
|
|
}
|
2021-03-19 15:04:00 +01:00
|
|
|
|
2021-12-10 15:18:01 +01:00
|
|
|
// Check for any JSON fields so we can add any top level properties
|
|
|
|
if (schema) {
|
|
|
|
let jsonAdditions = {}
|
|
|
|
Object.keys(schema).forEach(fieldKey => {
|
|
|
|
const fieldSchema = schema[fieldKey]
|
|
|
|
if (fieldSchema?.type === "json") {
|
2022-01-20 12:19:37 +01:00
|
|
|
const jsonSchema = JSONUtils.convertJSONSchemaToTableSchema(
|
|
|
|
fieldSchema,
|
|
|
|
{
|
|
|
|
squashObjects: true,
|
|
|
|
}
|
|
|
|
)
|
2021-12-10 15:18:01 +01:00
|
|
|
Object.keys(jsonSchema).forEach(jsonKey => {
|
|
|
|
jsonAdditions[`${fieldKey}.${jsonKey}`] = {
|
|
|
|
type: jsonSchema[jsonKey].type,
|
|
|
|
nestedJSON: true,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
schema = { ...schema, ...jsonAdditions }
|
|
|
|
}
|
|
|
|
|
2022-02-15 15:59:11 +01:00
|
|
|
// Determine if we should add ID and rev to the schema
|
|
|
|
const isInternal = table && !table.sql
|
|
|
|
const isTable = ["table", "link"].includes(datasource.type)
|
|
|
|
|
|
|
|
// ID is part of the readable schema for all tables
|
|
|
|
// Rev is part of the readable schema for internal tables only
|
|
|
|
let addId = isTable
|
|
|
|
let addRev = isTable && isInternal
|
|
|
|
|
|
|
|
// Don't add ID or rev for form schemas
|
|
|
|
if (options.formSchema) {
|
|
|
|
addId = false
|
|
|
|
addRev = false
|
|
|
|
}
|
|
|
|
|
|
|
|
// ID is only searchable for internal tables
|
|
|
|
else if (options.searchableSchema) {
|
|
|
|
addId = isTable && isInternal
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add schema properties if required
|
2022-02-17 10:25:02 +01:00
|
|
|
if (schema) {
|
|
|
|
if (addId) {
|
|
|
|
schema["_id"] = { type: "string" }
|
|
|
|
}
|
|
|
|
if (addRev) {
|
|
|
|
schema["_rev"] = { type: "string" }
|
|
|
|
}
|
2021-03-19 15:04:00 +01:00
|
|
|
}
|
|
|
|
|
2021-05-04 12:04:42 +02:00
|
|
|
// Ensure there are "name" properties for all fields and that field schema
|
|
|
|
// are objects
|
|
|
|
let fixedSchema = {}
|
|
|
|
Object.entries(schema || {}).forEach(([fieldName, fieldSchema]) => {
|
|
|
|
if (typeof fieldSchema === "string") {
|
|
|
|
fixedSchema[fieldName] = {
|
|
|
|
type: fieldSchema,
|
|
|
|
name: fieldName,
|
2021-03-19 15:04:00 +01:00
|
|
|
}
|
2021-05-04 12:04:42 +02:00
|
|
|
} else {
|
|
|
|
fixedSchema[fieldName] = {
|
|
|
|
...fieldSchema,
|
|
|
|
name: fieldName,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
schema = fixedSchema
|
2021-01-14 16:39:50 +01:00
|
|
|
}
|
|
|
|
return { schema, table }
|
|
|
|
}
|
2021-01-19 16:39:04 +01:00
|
|
|
|
2021-02-04 14:01:49 +01:00
|
|
|
/**
|
|
|
|
* Builds a form schema given a form component.
|
|
|
|
* A form schema is a schema of all the fields nested anywhere within a form.
|
|
|
|
*/
|
2022-04-14 10:46:07 +02:00
|
|
|
export const buildFormSchema = component => {
|
2021-02-04 14:01:49 +01:00
|
|
|
let schema = {}
|
|
|
|
if (!component) {
|
|
|
|
return schema
|
|
|
|
}
|
2022-04-01 13:51:23 +02:00
|
|
|
|
|
|
|
// If this is a form block, simply use the fields setting
|
|
|
|
if (component._component.endsWith("formblock")) {
|
|
|
|
let schema = {}
|
|
|
|
component.fields?.forEach(field => {
|
|
|
|
schema[field] = { type: "string" }
|
|
|
|
})
|
|
|
|
return schema
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise find all field component children
|
2021-11-04 12:30:43 +01:00
|
|
|
const settings = getComponentSettings(component._component)
|
|
|
|
const fieldSetting = settings.find(
|
2021-05-04 12:32:22 +02:00
|
|
|
setting => setting.key === "field" && setting.type.startsWith("field/")
|
2021-02-04 14:01:49 +01:00
|
|
|
)
|
|
|
|
if (fieldSetting && component.field) {
|
|
|
|
const type = fieldSetting.type.split("field/")[1]
|
|
|
|
if (type) {
|
2021-03-16 20:11:00 +01:00
|
|
|
schema[component.field] = { type }
|
2021-02-04 14:01:49 +01:00
|
|
|
}
|
|
|
|
}
|
2021-05-04 12:32:22 +02:00
|
|
|
component._children?.forEach(child => {
|
2021-02-04 14:01:49 +01:00
|
|
|
const childSchema = buildFormSchema(child)
|
|
|
|
schema = { ...schema, ...childSchema }
|
|
|
|
})
|
|
|
|
return schema
|
|
|
|
}
|
|
|
|
|
2021-12-08 16:31:06 +01:00
|
|
|
/**
|
|
|
|
* Returns an array of the keys of any state variables which are set anywhere
|
|
|
|
* in the app.
|
|
|
|
*/
|
|
|
|
export const getAllStateVariables = () => {
|
|
|
|
// Find all button action settings in all components
|
|
|
|
let eventSettings = []
|
2022-02-22 16:18:08 +01:00
|
|
|
getAllAssets().forEach(asset => {
|
2021-12-08 16:31:06 +01:00
|
|
|
findAllMatchingComponents(asset.props, component => {
|
|
|
|
const settings = getComponentSettings(component._component)
|
|
|
|
settings
|
|
|
|
.filter(setting => setting.type === "event")
|
|
|
|
.forEach(setting => {
|
|
|
|
eventSettings.push(component[setting.key])
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-06-20 17:12:46 +02:00
|
|
|
// Add on load settings from screens
|
|
|
|
get(store).screens.forEach(screen => {
|
|
|
|
if (screen.onLoad) {
|
|
|
|
eventSettings.push(screen.onLoad)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-12-08 16:31:06 +01:00
|
|
|
// Extract all state keys from any "update state" actions in each setting
|
|
|
|
let bindingSet = new Set()
|
|
|
|
eventSettings.forEach(setting => {
|
|
|
|
if (!Array.isArray(setting)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
setting.forEach(action => {
|
|
|
|
if (
|
|
|
|
action["##eventHandlerType"] === "Update State" &&
|
|
|
|
action.parameters?.type === "set" &&
|
|
|
|
action.parameters?.key &&
|
|
|
|
action.parameters?.value
|
|
|
|
) {
|
|
|
|
bindingSet.add(action.parameters.key)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
return Array.from(bindingSet)
|
|
|
|
}
|
|
|
|
|
2022-02-22 16:18:08 +01:00
|
|
|
export const getAllAssets = () => {
|
|
|
|
// Get all component containing assets
|
|
|
|
let allAssets = []
|
|
|
|
allAssets = allAssets.concat(get(store).layouts || [])
|
|
|
|
allAssets = allAssets.concat(get(store).screens || [])
|
|
|
|
|
|
|
|
return allAssets
|
|
|
|
}
|
|
|
|
|
2021-02-22 16:49:57 +01:00
|
|
|
/**
|
|
|
|
* Recurses the input object to remove any instances of bindings.
|
|
|
|
*/
|
2021-12-08 16:31:06 +01:00
|
|
|
export const removeBindings = (obj, replacement = "Invalid binding") => {
|
2021-02-22 16:49:57 +01:00
|
|
|
for (let [key, value] of Object.entries(obj)) {
|
2021-06-14 13:18:54 +02:00
|
|
|
if (value && typeof value === "object") {
|
2021-12-08 16:31:06 +01:00
|
|
|
obj[key] = removeBindings(value, replacement)
|
2021-02-22 16:49:57 +01:00
|
|
|
} else if (typeof value === "string") {
|
2021-12-08 16:31:06 +01:00
|
|
|
obj[key] = value.replace(CAPTURE_HBS_TEMPLATE, replacement)
|
2021-02-22 16:49:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return obj
|
|
|
|
}
|
|
|
|
|
2021-04-29 20:06:58 +02:00
|
|
|
/**
|
|
|
|
* When converting from readable to runtime it can sometimes add too many square brackets,
|
|
|
|
* this makes sure that doesn't happen.
|
|
|
|
*/
|
2021-12-08 16:31:06 +01:00
|
|
|
const shouldReplaceBinding = (currentValue, convertFrom, convertTo) => {
|
|
|
|
if (!currentValue?.includes(convertFrom)) {
|
2021-04-29 20:06:58 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
if (convertTo === "readableBinding") {
|
|
|
|
return true
|
|
|
|
}
|
2021-04-30 17:17:57 +02:00
|
|
|
// remove all the spaces, if the input is surrounded by spaces e.g. [ Auto ID ] then
|
|
|
|
// this makes sure it is detected
|
2021-04-29 20:06:58 +02:00
|
|
|
const noSpaces = currentValue.replace(/\s+/g, "")
|
2021-12-08 16:31:06 +01:00
|
|
|
const fromNoSpaces = convertFrom.replace(/\s+/g, "")
|
2021-04-30 17:17:57 +02:00
|
|
|
const invalids = [
|
|
|
|
`[${fromNoSpaces}]`,
|
|
|
|
`"${fromNoSpaces}"`,
|
|
|
|
`'${fromNoSpaces}'`,
|
|
|
|
]
|
2021-04-30 20:17:00 +02:00
|
|
|
return !invalids.find(invalid => noSpaces?.includes(invalid))
|
2021-04-29 20:06:58 +02:00
|
|
|
}
|
|
|
|
|
2021-12-08 16:31:06 +01:00
|
|
|
/**
|
|
|
|
* Utility function which replaces a string between given indices.
|
|
|
|
*/
|
|
|
|
const replaceBetween = (string, start, end, replacement) => {
|
2021-09-30 18:24:43 +02:00
|
|
|
return string.substring(0, start) + replacement + string.substring(end)
|
|
|
|
}
|
|
|
|
|
2021-01-19 16:39:04 +01:00
|
|
|
/**
|
2021-12-08 16:31:06 +01:00
|
|
|
* Utility function for the readableToRuntimeBinding and runtimeToReadableBinding.
|
2021-01-19 16:39:04 +01:00
|
|
|
*/
|
2021-12-08 16:31:06 +01:00
|
|
|
const bindingReplacement = (
|
|
|
|
bindableProperties,
|
|
|
|
textWithBindings,
|
|
|
|
convertTo
|
|
|
|
) => {
|
2021-10-11 15:54:24 +02:00
|
|
|
// Decide from base64 if using JS
|
|
|
|
const isJS = isJSBinding(textWithBindings)
|
|
|
|
if (isJS) {
|
|
|
|
textWithBindings = decodeJSBinding(textWithBindings)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine correct regex to find bindings to replace
|
|
|
|
const regex = isJS ? CAPTURE_VAR_INSIDE_JS : CAPTURE_VAR_INSIDE_TEMPLATE
|
|
|
|
|
2021-01-30 03:54:52 +01:00
|
|
|
const convertFrom =
|
|
|
|
convertTo === "runtimeBinding" ? "readableBinding" : "runtimeBinding"
|
2021-01-19 18:38:24 +01:00
|
|
|
if (typeof textWithBindings !== "string") {
|
|
|
|
return textWithBindings
|
|
|
|
}
|
2021-09-30 18:24:43 +02:00
|
|
|
// work from longest to shortest
|
2021-01-29 21:03:09 +01:00
|
|
|
const convertFromProps = bindableProperties
|
2021-05-04 12:32:22 +02:00
|
|
|
.map(el => el[convertFrom])
|
2021-01-29 21:03:09 +01:00
|
|
|
.sort((a, b) => {
|
|
|
|
return b.length - a.length
|
|
|
|
})
|
2021-10-11 15:54:24 +02:00
|
|
|
const boundValues = textWithBindings.match(regex) || []
|
2021-01-19 16:39:04 +01:00
|
|
|
let result = textWithBindings
|
2021-01-26 16:59:28 +01:00
|
|
|
for (let boundValue of boundValues) {
|
2021-01-29 21:03:09 +01:00
|
|
|
let newBoundValue = boundValue
|
2021-09-30 18:24:43 +02:00
|
|
|
// we use a search string, where any time we replace something we blank it out
|
|
|
|
// in the search, working from longest to shortest so always use best match first
|
|
|
|
let searchString = newBoundValue
|
2021-01-29 21:03:09 +01:00
|
|
|
for (let from of convertFromProps) {
|
2021-10-11 15:54:24 +02:00
|
|
|
if (isJS || shouldReplaceBinding(newBoundValue, from, convertTo)) {
|
2021-01-29 21:03:09 +01:00
|
|
|
const binding = bindableProperties.find(el => el[convertFrom] === from)
|
2021-09-30 18:24:43 +02:00
|
|
|
let idx
|
|
|
|
do {
|
|
|
|
// see if any instances of this binding exist in the search string
|
|
|
|
idx = searchString.indexOf(from)
|
|
|
|
if (idx !== -1) {
|
|
|
|
let end = idx + from.length,
|
2021-10-20 19:37:07 +02:00
|
|
|
searchReplace = Array(binding[convertTo].length + 1).join("*")
|
2021-09-30 18:24:43 +02:00
|
|
|
// blank out parts of the search string
|
|
|
|
searchString = replaceBetween(searchString, idx, end, searchReplace)
|
|
|
|
newBoundValue = replaceBetween(
|
|
|
|
newBoundValue,
|
|
|
|
idx,
|
|
|
|
end,
|
|
|
|
binding[convertTo]
|
|
|
|
)
|
|
|
|
}
|
|
|
|
} while (idx !== -1)
|
2021-01-29 21:03:09 +01:00
|
|
|
}
|
2021-01-19 16:39:04 +01:00
|
|
|
}
|
2021-01-26 16:59:58 +01:00
|
|
|
result = result.replace(boundValue, newBoundValue)
|
2021-01-26 16:59:28 +01:00
|
|
|
}
|
2021-10-11 15:54:24 +02:00
|
|
|
|
|
|
|
// Re-encode to base64 if using JS
|
|
|
|
if (isJS) {
|
|
|
|
result = encodeJSBinding(result)
|
|
|
|
}
|
|
|
|
|
2021-01-19 16:39:04 +01:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2021-03-22 18:57:19 +01:00
|
|
|
/**
|
|
|
|
* Extracts a component ID from a handlebars expression setting of
|
|
|
|
* {{ literal [componentId] }}
|
|
|
|
*/
|
2021-12-08 16:31:06 +01:00
|
|
|
const extractLiteralHandlebarsID = value => {
|
2022-10-15 13:58:32 +02:00
|
|
|
if (!value || typeof value !== "string") {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
return value.match(/{{\s*literal\s*\[+([^\]]+)].*}}/)?.[1]
|
2021-03-22 18:57:19 +01:00
|
|
|
}
|
|
|
|
|
2021-01-29 21:03:09 +01:00
|
|
|
/**
|
|
|
|
* Converts a readable data binding into a runtime data binding
|
|
|
|
*/
|
2021-12-08 16:31:06 +01:00
|
|
|
export const readableToRuntimeBinding = (
|
|
|
|
bindableProperties,
|
|
|
|
textWithBindings
|
|
|
|
) => {
|
2021-01-30 03:54:52 +01:00
|
|
|
return bindingReplacement(
|
|
|
|
bindableProperties,
|
|
|
|
textWithBindings,
|
|
|
|
"runtimeBinding"
|
|
|
|
)
|
2021-01-29 21:03:09 +01:00
|
|
|
}
|
|
|
|
|
2021-01-19 16:39:04 +01:00
|
|
|
/**
|
|
|
|
* Converts a runtime data binding into a readable data binding
|
|
|
|
*/
|
2021-12-08 16:31:06 +01:00
|
|
|
export const runtimeToReadableBinding = (
|
|
|
|
bindableProperties,
|
|
|
|
textWithBindings
|
|
|
|
) => {
|
2021-01-30 03:54:52 +01:00
|
|
|
return bindingReplacement(
|
|
|
|
bindableProperties,
|
|
|
|
textWithBindings,
|
|
|
|
"readableBinding"
|
|
|
|
)
|
2021-01-19 16:39:04 +01:00
|
|
|
}
|