More effeciently render component settings and filter out empty sections

This commit is contained in:
Andrew Kingston 2022-08-23 14:59:43 +01:00
parent 44e9395299
commit 3ea5e598e3
1 changed files with 69 additions and 52 deletions

View File

@ -12,13 +12,13 @@
export let componentBindings export let componentBindings
export let isScreen = false export let isScreen = false
$: sections = getSections(componentDefinition) $: sections = getSections(componentInstance, componentDefinition, isScreen)
const getSections = definition => { const getSections = (instance, definition, isScreen) => {
const settings = definition?.settings ?? [] const settings = definition?.settings ?? []
const generalSettings = settings.filter(setting => !setting.section) const generalSettings = settings.filter(setting => !setting.section)
const customSections = settings.filter(setting => setting.section) const customSections = settings.filter(setting => setting.section)
return [ let sections = [
{ {
name: "General", name: "General",
info: componentDefinition?.info, info: componentDefinition?.info,
@ -26,6 +26,16 @@
}, },
...(customSections || []), ...(customSections || []),
] ]
// Filter out settings which shouldn't be rendered
sections.forEach(section => {
section.settings.forEach(setting => {
setting.visible = canRenderControl(instance, setting, isScreen)
})
section.visible = section.settings.some(setting => setting.visible)
})
return sections
} }
const updateSetting = async (key, value) => { const updateSetting = async (key, value) => {
@ -36,7 +46,7 @@
} }
} }
const canRenderControl = (setting, isScreen) => { const canRenderControl = (instance, setting, isScreen) => {
// Prevent rendering on click setting for screens // Prevent rendering on click setting for screens
if (setting?.type === "event" && isScreen) { if (setting?.type === "event" && isScreen) {
return false return false
@ -51,6 +61,7 @@
if (setting.dependsOn) { if (setting.dependsOn) {
let dependantSetting = setting.dependsOn let dependantSetting = setting.dependsOn
let dependantValue = null let dependantValue = null
let invert = !!setting.dependsOn.invert
if (typeof setting.dependsOn === "object") { if (typeof setting.dependsOn === "object") {
dependantSetting = setting.dependsOn.setting dependantSetting = setting.dependsOn.setting
dependantValue = setting.dependsOn.value dependantValue = setting.dependsOn.value
@ -62,7 +73,7 @@
// If no specific value is depended upon, check if a value exists at all // If no specific value is depended upon, check if a value exists at all
// for the dependent setting // for the dependent setting
if (dependantValue == null) { if (dependantValue == null) {
const currentValue = componentInstance[dependantSetting] const currentValue = instance[dependantSetting]
if (currentValue === false) { if (currentValue === false) {
return false return false
} }
@ -73,7 +84,11 @@
} }
// Otherwise check the value matches // Otherwise check the value matches
return componentInstance[dependantSetting] === dependantValue if (invert) {
return instance[dependantSetting] !== dependantValue
} else {
return instance[dependantSetting] === dependantValue
}
} }
return true return true
@ -81,6 +96,7 @@
</script> </script>
{#each sections as section, idx (section.name)} {#each sections as section, idx (section.name)}
{#if section.visible}
<DetailSummary name={section.name} collapsible={false}> <DetailSummary name={section.name} collapsible={false}>
{#if idx === 0 && !componentInstance._component.endsWith("/layout") && !isScreen} {#if idx === 0 && !componentInstance._component.endsWith("/layout") && !isScreen}
<PropertyControl <PropertyControl
@ -92,7 +108,7 @@
/> />
{/if} {/if}
{#each section.settings as setting (setting.key)} {#each section.settings as setting (setting.key)}
{#if canRenderControl(setting, isScreen)} {#if setting.visible}
<PropertyControl <PropertyControl
type={setting.type} type={setting.type}
control={getComponentForSetting(setting)} control={getComponentForSetting(setting)}
@ -130,6 +146,7 @@
</div> </div>
{/if} {/if}
</DetailSummary> </DetailSummary>
{/if}
{/each} {/each}
<style> <style>