2020-11-25 10:50:51 +01:00
|
|
|
import { enrichDataBindings } from "./enrichDataBinding"
|
|
|
|
import { enrichButtonActions } from "./buttonActions"
|
|
|
|
|
2021-01-21 11:41:30 +01:00
|
|
|
/**
|
|
|
|
* Deeply compares 2 props using JSON.stringify.
|
|
|
|
* Does not consider functions, as currently only button actions have a function
|
|
|
|
* prop and it's cheaper to just always re-render buttons than it is to deeply
|
|
|
|
* compare them.
|
|
|
|
*/
|
|
|
|
export const propsAreSame = (a, b) => {
|
|
|
|
if (a === b) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if (typeof a === "function" || typeof b === "function") {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return JSON.stringify(a) === JSON.stringify(b)
|
|
|
|
}
|
|
|
|
|
2020-11-25 10:50:51 +01:00
|
|
|
/**
|
|
|
|
* Enriches component props.
|
|
|
|
* Data bindings are enriched, and button actions are enriched.
|
|
|
|
*/
|
2021-06-01 15:59:42 +02:00
|
|
|
export const enrichProps = (props, context) => {
|
2020-11-25 10:50:51 +01:00
|
|
|
// Create context of all bindings and data contexts
|
|
|
|
// Duplicate the closest context as "data" which the builder requires
|
2021-02-01 19:51:22 +01:00
|
|
|
const totalContext = {
|
|
|
|
...context,
|
2021-02-02 15:32:58 +01:00
|
|
|
|
|
|
|
// This is only required for legacy bindings that used "data" rather than a
|
|
|
|
// component ID.
|
2021-02-01 19:51:22 +01:00
|
|
|
data: context[context.closestComponentId],
|
2020-11-25 10:50:51 +01:00
|
|
|
}
|
|
|
|
|
2021-12-09 12:25:32 +01:00
|
|
|
// We want to exclude any button actions from enrichment at this stage.
|
|
|
|
// Extract top level button action settings.
|
|
|
|
let normalProps = { ...props }
|
|
|
|
let actionProps = {}
|
|
|
|
Object.keys(normalProps).forEach(prop => {
|
2021-11-04 12:30:43 +01:00
|
|
|
if (prop?.toLowerCase().includes("onclick")) {
|
2021-12-09 12:25:32 +01:00
|
|
|
actionProps[prop] = normalProps[prop]
|
|
|
|
delete normalProps[prop]
|
2021-11-04 12:30:43 +01:00
|
|
|
}
|
|
|
|
})
|
2020-11-25 10:50:51 +01:00
|
|
|
|
2021-12-09 12:25:32 +01:00
|
|
|
// Handle conditional UI separately after normal settings
|
|
|
|
let conditions = normalProps._conditions
|
|
|
|
delete normalProps._conditions
|
|
|
|
|
|
|
|
// Enrich all props except button actions
|
|
|
|
let enrichedProps = enrichDataBindings(normalProps, totalContext)
|
|
|
|
|
|
|
|
// Enrich button actions.
|
|
|
|
// Actions are enriched into a function at this stage, but actual data
|
|
|
|
// binding enrichment is done dynamically at runtime.
|
|
|
|
Object.keys(actionProps).forEach(prop => {
|
|
|
|
enrichedProps[prop] = enrichButtonActions(actionProps[prop], totalContext)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Conditions
|
|
|
|
if (conditions?.length) {
|
|
|
|
let enrichedConditions = []
|
|
|
|
conditions.forEach(condition => {
|
2021-11-04 12:30:43 +01:00
|
|
|
if (condition.setting?.toLowerCase().includes("onclick")) {
|
2021-12-09 12:25:32 +01:00
|
|
|
// Copy and remove the setting value from the condition as it needs
|
|
|
|
// enriched separately
|
|
|
|
let toEnrich = { ...condition }
|
|
|
|
delete toEnrich.settingValue
|
2021-08-23 17:39:09 +02:00
|
|
|
|
2021-12-09 12:25:32 +01:00
|
|
|
// Join the condition back together
|
|
|
|
enrichedConditions.push({
|
|
|
|
...enrichDataBindings(toEnrich, totalContext),
|
|
|
|
settingValue: enrichButtonActions(
|
|
|
|
condition.settingValue,
|
|
|
|
totalContext
|
|
|
|
),
|
|
|
|
rand: Math.random(),
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
// Normal condition
|
|
|
|
enrichedConditions.push(enrichDataBindings(condition, totalContext))
|
2021-07-23 16:44:16 +02:00
|
|
|
}
|
|
|
|
})
|
2021-12-09 12:25:32 +01:00
|
|
|
enrichedProps._conditions = enrichedConditions
|
2021-07-23 16:44:16 +02:00
|
|
|
}
|
|
|
|
|
2020-11-25 10:50:51 +01:00
|
|
|
return enrichedProps
|
|
|
|
}
|