1
0
Fork 0
mirror of synced 2024-09-20 11:27:56 +12:00

Update component settings bar to respect dependsOn metadata

This commit is contained in:
Andrew Kingston 2024-08-05 16:53:10 +01:00
parent c6cb267b30
commit 22cda514b4
No known key found for this signature in database
4 changed files with 57 additions and 48 deletions

View file

@ -1,5 +1,4 @@
<script> <script>
import { helpers } from "@budibase/shared-core"
import { DetailSummary, notifications } from "@budibase/bbui" import { DetailSummary, notifications } from "@budibase/bbui"
import { componentStore, builderStore } from "stores/builder" import { componentStore, builderStore } from "stores/builder"
import PropertyControl from "components/design/settings/controls/PropertyControl.svelte" import PropertyControl from "components/design/settings/controls/PropertyControl.svelte"
@ -8,6 +7,7 @@
import { getComponentForSetting } from "components/design/settings/componentSettings" import { getComponentForSetting } from "components/design/settings/componentSettings"
import InfoDisplay from "./InfoDisplay.svelte" import InfoDisplay from "./InfoDisplay.svelte"
import analytics, { Events } from "analytics" import analytics, { Events } from "analytics"
import { shouldDisplaySetting } from "@budibase/frontend-core"
export let componentDefinition export let componentDefinition
export let componentInstance export let componentInstance
@ -48,7 +48,7 @@
// Filter out settings which shouldn't be rendered // Filter out settings which shouldn't be rendered
sections.forEach(section => { sections.forEach(section => {
section.visible = shouldDisplay(instance, section) section.visible = shouldDisplaySetting(instance, section)
if (!section.visible) { if (!section.visible) {
return return
} }
@ -88,46 +88,6 @@
} }
} }
const shouldDisplay = (instance, setting) => {
let dependsOn = setting.dependsOn
if (dependsOn && !Array.isArray(dependsOn)) {
dependsOn = [dependsOn]
}
if (!dependsOn?.length) {
return true
}
// Ensure all conditions are met
return dependsOn.every(condition => {
let dependantSetting = condition
let dependantValues = null
let invert = !!condition.invert
if (typeof condition === "object") {
dependantSetting = condition.setting
dependantValues = condition.value
}
if (!dependantSetting) {
return false
}
// Ensure values is an array
if (!Array.isArray(dependantValues)) {
dependantValues = [dependantValues]
}
// If inverting, we want to ensure that we don't have any matches.
// If not inverting, we want to ensure that we do have any matches.
const currentVal = helpers.deepGet(instance, dependantSetting)
const anyMatches = dependantValues.some(dependantVal => {
if (dependantVal == null) {
return currentVal != null && currentVal !== false && currentVal !== ""
}
return dependantVal === currentVal
})
return anyMatches !== invert
})
}
const canRenderControl = (instance, setting, isScreen, includeHidden) => { const canRenderControl = (instance, setting, isScreen, includeHidden) => {
// Prevent rendering on click setting for screens // Prevent rendering on click setting for screens
if (setting?.type === "event" && isScreen) { if (setting?.type === "event" && isScreen) {
@ -142,7 +102,7 @@
if (setting.hidden && !includeHidden) { if (setting.hidden && !includeHidden) {
return false return false
} }
return shouldDisplay(instance, setting) return shouldDisplaySetting(instance, setting)
} }
</script> </script>

View file

@ -5,7 +5,7 @@
import SettingsColorPicker from "./SettingsColorPicker.svelte" import SettingsColorPicker from "./SettingsColorPicker.svelte"
import SettingsPicker from "./SettingsPicker.svelte" import SettingsPicker from "./SettingsPicker.svelte"
import { builderStore, componentStore, dndIsDragging } from "stores" import { builderStore, componentStore, dndIsDragging } from "stores"
import { Utils } from "@budibase/frontend-core" import { Utils, shouldDisplaySetting } from "@budibase/frontend-core"
import { findComponentParent } from "utils/components" import { findComponentParent } from "utils/components"
import { getGridVar, GridParams } from "utils/grid" import { getGridVar, GridParams } from "utils/grid"
@ -39,7 +39,7 @@
measured = false measured = false
} }
} }
$: settings = getBarSettings(definition) $: settings = getBarSettings(component, definition)
$: isRoot = componentId === $builderStore.screen?.props?._id $: isRoot = componentId === $builderStore.screen?.props?._id
$: insideGrid = $: insideGrid =
parent?._component.endsWith("/container") && parent.layout === "grid" parent?._component.endsWith("/container") && parent.layout === "grid"
@ -51,16 +51,21 @@
$: gridHAlignVar = getGridVar(device, GridParams.HAlign) $: gridHAlignVar = getGridVar(device, GridParams.HAlign)
$: gridVAlignVar = getGridVar(device, GridParams.VAlign) $: gridVAlignVar = getGridVar(device, GridParams.VAlign)
const getBarSettings = definition => { const getBarSettings = (component, definition) => {
let allSettings = [] let allSettings = []
definition?.settings?.forEach(setting => { definition?.settings?.forEach(setting => {
if (setting.section) { if (setting.section && shouldDisplaySetting(component, setting)) {
allSettings = allSettings.concat(setting.settings || []) allSettings = allSettings.concat(setting.settings || [])
} else { } else {
allSettings.push(setting) allSettings.push(setting)
} }
}) })
return allSettings.filter(setting => setting.showInBar && !setting.hidden) return allSettings.filter(
setting =>
setting.showInBar &&
!setting.hidden &&
shouldDisplaySetting(component, setting)
)
} }
const updatePosition = () => { const updatePosition = () => {

View file

@ -9,3 +9,4 @@ export { memo, derivedMemo } from "./memo"
export { createWebsocket } from "./websocket" export { createWebsocket } from "./websocket"
export * from "./download" export * from "./download"
export * from "./theme" export * from "./theme"
export * from "./settings"

View file

@ -0,0 +1,43 @@
import { helpers } from "@budibase/shared-core"
// Util to check if a setting can be rendered for a certain instance, based on
// the "dependsOn" metadata in the manifest
export const shouldDisplaySetting = (instance, setting) => {
let dependsOn = setting.dependsOn
if (dependsOn && !Array.isArray(dependsOn)) {
dependsOn = [dependsOn]
}
if (!dependsOn?.length) {
return true
}
// Ensure all conditions are met
return dependsOn.every(condition => {
let dependantSetting = condition
let dependantValues = null
let invert = !!condition.invert
if (typeof condition === "object") {
dependantSetting = condition.setting
dependantValues = condition.value
}
if (!dependantSetting) {
return false
}
// Ensure values is an array
if (!Array.isArray(dependantValues)) {
dependantValues = [dependantValues]
}
// If inverting, we want to ensure that we don't have any matches.
// If not inverting, we want to ensure that we do have any matches.
const currentVal = helpers.deepGet(instance, dependantSetting)
const anyMatches = dependantValues.some(dependantVal => {
if (dependantVal == null) {
return currentVal != null && currentVal !== false && currentVal !== ""
}
return dependantVal === currentVal
})
return anyMatches !== invert
})
}