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

131 lines
3.1 KiB
Svelte
Raw Normal View History

<script>
2020-02-03 10:50:30 +01:00
import ComponentsHierarchyChildren from "./ComponentsHierarchyChildren.svelte"
2019-08-15 09:49:15 +02:00
2020-02-03 10:50:30 +01:00
import { last, sortBy, map, trimCharsStart, trimChars, join } from "lodash/fp"
2020-02-18 17:51:28 +01:00
import ConfirmDialog from "../common/ConfirmDialog.svelte"
2020-02-03 10:50:30 +01:00
import { pipe } from "../common/core"
import { store } from "../builderStore"
2020-02-20 18:11:41 +01:00
import { ArrowDownIcon, ShapeIcon } from "../common/Icons/"
2020-02-03 10:50:30 +01:00
export let screens = []
2020-02-03 10:50:30 +01:00
2020-02-18 17:51:28 +01:00
let confirmDeleteDialog
let componentToDelete = ""
2020-02-03 10:50:30 +01:00
const joinPath = join("/")
const normalizedName = name =>
pipe(
name,
[
trimCharsStart("./"),
trimCharsStart("~/"),
trimCharsStart("../"),
trimChars(" "),
]
)
2020-02-03 10:50:30 +01:00
2020-02-20 18:27:48 +01:00
const lastPartOfName = c => {
if (!c) return ""
const name = c.name ? c.name : c._component ? c._component : c
return last(name.split("/"))
}
2020-02-03 10:50:30 +01:00
const isComponentSelected = (current, comp) => current === comp
2020-02-03 10:50:30 +01:00
const isFolderSelected = (current, folder) => isInSubfolder(current, folder)
$: _screens = pipe(
screens,
[map(c => ({ component: c, title: lastPartOfName(c) })), sortBy("title")]
)
2020-02-03 10:50:30 +01:00
const isScreenSelected = component =>
component.component &&
$store.currentPreviewItem &&
component.component.name === $store.currentPreviewItem.name
2020-02-18 17:51:28 +01:00
const confirmDeleteComponent = component => {
componentToDelete = component
confirmDeleteDialog.show()
}
2020-02-03 10:50:30 +01:00
</script>
<div class="root">
{#each _screens as screen}
2020-02-03 10:50:30 +01:00
<div
2020-02-24 16:00:48 +01:00
class="budibase__nav-item component"
2020-02-20 18:11:41 +01:00
class:selected={$store.currentComponentInfo._id === screen.component.props._id}
on:click|stopPropagation={() => store.setCurrentScreen(screen.title)}>
2020-02-03 10:50:30 +01:00
<span
class="icon"
2020-02-20 18:11:41 +01:00
class:rotate={$store.currentPreviewItem.name !== screen.title}>
{#if screen.component.props._children.length}
2020-02-03 10:50:30 +01:00
<ArrowDownIcon />
{/if}
2020-02-03 10:50:30 +01:00
</span>
2020-02-20 18:11:41 +01:00
<span class="icon">
<ShapeIcon />
</span>
<span class="title">{screen.title}</span>
2020-02-03 10:50:30 +01:00
</div>
{#if $store.currentPreviewItem.name === screen.title && screen.component.props._children}
2020-02-03 10:50:30 +01:00
<ComponentsHierarchyChildren
components={screen.component.props._children}
2020-02-03 10:50:30 +01:00
currentComponent={$store.currentComponentInfo}
2020-02-20 18:11:41 +01:00
onSelect={store.selectComponent}
2020-02-19 11:17:31 +01:00
onDeleteComponent={confirmDeleteComponent}
onMoveUpComponent={store.moveUpComponent}
onMoveDownComponent={store.moveDownComponent}
2020-02-20 18:11:41 +01:00
onCopyComponent={store.copyComponent} />
2020-02-03 10:50:30 +01:00
{/if}
{/each}
</div>
2020-02-20 18:11:41 +01:00
<ConfirmDialog
bind:this={confirmDeleteDialog}
title="Confirm Delete"
body={`Are you sure you wish to delete this '${lastPartOfName(componentToDelete)}' component?`}
2020-02-18 17:51:28 +01:00
okText="Delete Component"
2020-02-20 18:11:41 +01:00
onOk={() => store.deleteComponent(componentToDelete)} />
2020-02-18 17:51:28 +01:00
<style>
2020-02-03 10:50:30 +01:00
.root {
2020-02-20 18:11:41 +01:00
font-weight: 400;
color: #333;
2020-02-03 10:50:30 +01:00
}
2020-02-03 10:50:30 +01:00
.title {
margin-left: 10px;
2020-02-20 18:11:41 +01:00
margin-top: 2px;
2020-02-03 10:50:30 +01:00
}
2020-02-03 10:50:30 +01:00
.icon {
display: inline-block;
transition: 0.2s;
2020-02-20 18:11:41 +01:00
width: 20px;
margin-top: 2px;
color: #333;
}
.icon:nth-of-type(2) {
width: 14px;
margin: 0 0 0 5px;
}
:global(svg) {
transition: 0.2s;
}
.rotate :global(svg) {
transform: rotate(-90deg);
2020-02-03 10:50:30 +01:00
}
</style>