budibase/packages/builder/src/userInterface/ComponentSelector.svelte

139 lines
3.1 KiB
Svelte
Raw Normal View History

2019-10-07 07:03:41 +02:00
<script>
2020-02-03 10:50:30 +01:00
import { isRootComponent } from "./pagesParsing/searchComponents"
import { splitName } from "./pagesParsing/splitRootComponentName.js"
import { store } from "../builderStore"
import { find, sortBy } from "lodash/fp"
export let onComponentChosen
export let onGeneratorChosen
export let allowGenerators
let screens = []
let componentLibraries = []
const addRootComponent = (c, all, isGenerator) => {
const { libName } = splitName(c.name)
let group = find(r => r.libName === libName)(all)
if (!group) {
group = {
libName,
components: [],
generators: [],
}
all.push(group)
}
if (isGenerator) {
group.generators.push(c)
} else {
group.components.push(c)
}
}
$: {
const newComponentLibraries = []
const newscreens = []
for (let comp of sortBy(["name"])($store.components)) {
if (isRootComponent(comp)) {
addRootComponent(comp, newComponentLibraries, false)
} else {
newscreens.push(comp)
}
}
for (let generator of $store.generators) {
addRootComponent(generator, newComponentLibraries, true)
}
screens = sortBy(["name"])(newscreens)
componentLibraries = newComponentLibraries
}
2019-10-07 07:03:41 +02:00
</script>
{#each componentLibraries as lib}
2020-02-03 10:50:30 +01:00
<div class="library-header">{lib.libName}</div>
2019-10-07 07:03:41 +02:00
2020-02-03 10:50:30 +01:00
<div class="library-container">
2019-10-07 07:03:41 +02:00
2019-10-10 11:41:31 +02:00
{#if allowGenerators}
2020-02-03 10:50:30 +01:00
<div class="inner-header">Generators</div>
2019-10-07 07:03:41 +02:00
2020-02-03 10:50:30 +01:00
{#each lib.generators as generator}
<div class="component" on:click={() => onGeneratorChosen(generator)}>
<div class="name">{splitName(generator.name).componentName}</div>
<div class="description">{generator.description}</div>
2019-10-07 07:03:41 +02:00
</div>
2020-02-03 10:50:30 +01:00
{/each}
2019-10-10 11:41:31 +02:00
{/if}
2019-10-07 07:03:41 +02:00
2020-02-03 10:50:30 +01:00
<div class="inner-header">Components</div>
2019-10-07 07:03:41 +02:00
{#each lib.components as component}
2020-02-03 10:50:30 +01:00
<div class="component" on:click={() => onComponentChosen(component)}>
<div class="name">{splitName(component.name).componentName}</div>
<div class="description">{component.description}</div>
</div>
2019-10-07 07:03:41 +02:00
{/each}
2020-02-03 10:50:30 +01:00
</div>
2019-10-07 07:03:41 +02:00
{/each}
2020-02-03 10:50:30 +01:00
<div class="library-header">My Components</div>
2019-10-07 07:03:41 +02:00
<div class="library-container">
2020-02-03 10:50:30 +01:00
{#each screens as component}
<div class="component" on:click={() => onComponentChosen(component)}>
<div class="name">{component.name}</div>
<div class="description">{component.description}</div>
2019-10-07 07:03:41 +02:00
</div>
2020-02-03 10:50:30 +01:00
{/each}
2019-10-07 07:03:41 +02:00
</div>
<style>
2020-02-03 10:50:30 +01:00
.library-header {
2019-10-07 07:03:41 +02:00
font-size: 1.1em;
border-color: var(--primary25);
border-width: 1px 0px;
border-style: solid;
background-color: var(--primary10);
padding: 5px 0;
2020-02-03 10:50:30 +01:00
}
2019-10-07 07:03:41 +02:00
2020-02-03 10:50:30 +01:00
.library-container {
2019-10-07 07:03:41 +02:00
padding: 0 0 10px 10px;
2020-02-03 10:50:30 +01:00
}
2019-10-07 07:03:41 +02:00
2020-02-03 10:50:30 +01:00
.inner-header {
2019-10-07 07:03:41 +02:00
font-size: 0.9em;
font-weight: bold;
margin-top: 7px;
margin-bottom: 3px;
2020-02-03 10:50:30 +01:00
}
2019-10-07 07:03:41 +02:00
2020-02-03 10:50:30 +01:00
.component {
2019-10-07 07:03:41 +02:00
padding: 2px 0px;
cursor: pointer;
2020-02-03 10:50:30 +01:00
}
2019-10-07 07:03:41 +02:00
2020-02-03 10:50:30 +01:00
.component:hover {
2019-10-07 07:03:41 +02:00
background-color: var(--lightslate);
2020-02-03 10:50:30 +01:00
}
2019-10-07 07:03:41 +02:00
2020-02-03 10:50:30 +01:00
.component > .name {
2019-10-07 07:03:41 +02:00
color: var(--secondary100);
display: inline-block;
2020-02-03 10:50:30 +01:00
}
2019-10-07 07:03:41 +02:00
2020-02-03 10:50:30 +01:00
.component > .description {
2019-10-07 07:03:41 +02:00
font-size: 0.8em;
color: var(--secondary75);
display: inline-block;
margin-left: 10px;
2020-02-03 10:50:30 +01:00
}
2019-10-07 07:03:41 +02:00
</style>