budibase/packages/standard-components/src/StackPanel.svelte

115 lines
2.3 KiB
Svelte
Raw Normal View History

2019-09-03 11:42:19 +02:00
<script>
2019-10-03 07:12:13 +02:00
2019-10-01 06:57:45 +02:00
import { emptyProps } from "./emptyProps";
2019-09-03 11:42:19 +02:00
export let direction = "horizontal";
export let children = [];
export let width = "auto";
export let height = "auto";
export let containerClass="";
export let itemContainerClass="";
2019-10-01 06:57:45 +02:00
export let onLoad;
2019-09-03 11:42:19 +02:00
2019-10-01 06:57:45 +02:00
export let data=[];
export let dataItemComponent;
2019-09-03 11:42:19 +02:00
export let _bb;
2019-09-03 11:42:19 +02:00
2019-10-01 06:57:45 +02:00
let staticHtmlElements = {};
let staticComponents = {};
let dataBoundElements = {};
let dataBoundComponents = {};
2019-09-03 11:42:19 +02:00
2019-10-03 07:12:13 +02:00
let onLoadCalled = false;
2019-10-01 06:57:45 +02:00
const hasDataBoundComponents = () =>
Object.getOwnPropertyNames(dataBoundComponents).length === 0;
const hasData = () =>
Array.isArray(data) && data.length > 0;
const hasStaticComponents = () => {
return Object.getOwnPropertyNames(staticComponents).length === 0;
}
$: {
if(staticHtmlElements) {
if(hasStaticComponents()) {
for(let c in staticComponents) {
staticComponents[c].$destroy();
}
staticComponents = {};
}
for(let el in staticHtmlElements) {
staticComponents[el] = _bb.initialiseComponent(
2019-09-03 11:42:19 +02:00
children[el].control,
2019-10-01 06:57:45 +02:00
staticHtmlElements[el]
);
}
}
if(hasDataBoundComponents()) {
for(let c in dataBoundComponents) {
dataBoundComponents[c].$destroy();
}
dataBoundComponents = {};
}
if(hasData()) {
let index = 0;
for(let d in dataBoundElements) {
_bb.initialiseComponent(
dataItemComponent,
dataBoundElements[d],
data[parseInt(d)]
2019-09-03 11:42:19 +02:00
);
}
}
2019-10-03 07:12:13 +02:00
if(!onLoadCalled && onLoad && !onLoad.isPlaceholder) {
onLoad();
onLoadCalled = true;
}
2019-10-01 06:57:45 +02:00
}
2019-09-03 11:42:19 +02:00
</script>
<div class="root {containerClass}"
style="width: {width}; height: {height}">
2019-10-03 07:12:13 +02:00
{#if children}
2019-09-03 11:42:19 +02:00
{#each children as child, index}
<div class={direction}>
<div class="{itemContainerClass}"
2019-10-01 06:57:45 +02:00
bind:this={staticHtmlElements[index]}>
</div>
</div>
{/each}
2019-10-03 07:12:13 +02:00
{/if}
{#if data && data.length > 0}
2019-10-01 06:57:45 +02:00
{#each data as child, index}
<div class={direction}>
<div class="{itemContainerClass}"
bind:this={dataBoundElements[index]}>
2019-09-03 11:42:19 +02:00
</div>
</div>
{/each}
2019-10-03 07:12:13 +02:00
{/if}
2019-09-03 11:42:19 +02:00
</div>
<style>
.horizontal {
display:inline-block;
}
.vertical {
display: block;
}
</style>