budibase/packages/builder/src/components/userInterface/ModelViewSelect.svelte

106 lines
2.3 KiB
Svelte
Raw Normal View History

2020-08-21 17:10:56 +02:00
<script>
import { Button, Icon, DropdownMenu, Spacer, Heading } from "@budibase/bbui"
import { createEventDispatcher } from "svelte"
2020-08-21 17:10:56 +02:00
import { backendUiStore } from "builderStore"
const dispatch = createEventDispatcher()
let anchorRight, dropdownRight
2020-08-21 17:10:56 +02:00
export let value = {}
function handleSelected(selected) {
dispatch("change", selected)
dropdown.hide()
}
2020-08-21 17:10:56 +02:00
const models = $backendUiStore.models.map(m => ({
label: m.name,
name: `all_${m._id}`,
2020-08-21 17:10:56 +02:00
modelId: m._id,
isModel: true,
2020-08-21 17:10:56 +02:00
}))
const views = $backendUiStore.models.reduce((acc, cur) => {
let viewsArr = Object.entries(cur.views).map(([key, value]) => ({
label: key,
2020-08-21 17:10:56 +02:00
name: key,
...value,
2020-08-21 17:10:56 +02:00
}))
return [...acc, ...viewsArr]
}, [])
</script>
<div class="dropdownbutton" bind:this={anchorRight}>
<Button secondary wide on:click={dropdownRight.show}>
<span>{value.label ? value.label : 'Model / View'}</span>
2020-08-21 17:10:56 +02:00
<Icon name="arrowdown" />
</Button>
</div>
<DropdownMenu bind:this={dropdownRight} anchor={anchorRight}>
<div class="dropdown">
<div class="title">
<Heading extraSmall>Tables</Heading>
</div>
2020-08-21 17:10:56 +02:00
<ul>
{#each models as model}
<li
class:selected={value === model}
on:click={() => handleSelected(model)}>
{model.label}
2020-08-21 17:10:56 +02:00
</li>
{/each}
</ul>
<hr />
<div class="title">
<Heading extraSmall>Views</Heading>
</div>
2020-08-21 17:10:56 +02:00
<ul>
{#each views as view}
<li
class:selected={value === view}
on:click={() => handleSelected(view)}>
{view.label}
2020-08-21 17:10:56 +02:00
</li>
{/each}
</ul>
</div>
</DropdownMenu>
<style>
.dropdownbutton {
width: 100%;
2020-08-21 17:10:56 +02:00
}
.dropdown {
padding: var(--spacing-m) 0;
z-index: 99999999;
2020-08-21 17:10:56 +02:00
}
.title {
padding: 0 var(--spacing-m) var(--spacing-xs) var(--spacing-m);
2020-08-21 17:10:56 +02:00
}
hr {
margin: var(--spacing-m) 0 var(--spacing-xl) 0;
2020-08-21 17:10:56 +02:00
}
ul {
list-style: none;
padding-left: 0px;
margin: 0px;
}
li {
cursor: pointer;
margin: 0px;
padding: var(--spacing-s) var(--spacing-m);
font-size: var(--font-size-xs);
2020-08-21 17:10:56 +02:00
}
.selected {
background-color: var(--grey-4);
}
li:hover {
background-color: var(--grey-4);
}
</style>