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

130 lines
3.2 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"
import { ArrowDownIcon } from "../common/Icons/"
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
const lastPartOfName = c =>
2020-02-18 17:51:28 +01:00
c &&
2020-02-03 10:50:30 +01:00
last(c.name ? c.name.split("/") : c._component.split("/"))
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
class="hierarchy-item component"
class:selected={$store.currentPreviewItem.name === screen.title}
on:click|stopPropagation={() => store.setCurrentScreen(screen.title)}>
2020-02-03 10:50:30 +01:00
<span
class="icon"
style="transform: rotate({$store.currentPreviewItem.name === screen.title ? 0 : -90}deg);">
{#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>
<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-18 17:51:28 +01:00
onSelect={store.selectComponent}
2020-02-19 11:17:31 +01:00
onDeleteComponent={confirmDeleteComponent}
onMoveUpComponent={store.moveUpComponent}
onMoveDownComponent={store.moveDownComponent}
onCopyComponent={store.copyComponent}/>
2020-02-03 10:50:30 +01:00
{/if}
{/each}
</div>
2020-02-18 17:51:28 +01:00
<ConfirmDialog
bind:this={confirmDeleteDialog}
title="Confirm Delete"
body={`Are you sure you wish to delete this '${lastPartOfName(componentToDelete._component)}' component`}
okText="Delete Component"
onOk={() => store.deleteComponent(componentToDelete)}/>
<style>
2020-02-03 10:50:30 +01:00
.root {
font-weight: 500;
font-size: 0.9rem;
color: #828fa5;
2020-02-03 10:50:30 +01:00
}
2020-02-03 10:50:30 +01:00
.hierarchy-item {
cursor: pointer;
padding: 11px 7px;
margin: 5px 0;
border-radius: 5px;
display: flex;
align-items: center;
2020-02-03 10:50:30 +01:00
}
2020-02-03 10:50:30 +01:00
.hierarchy-item:hover {
/* color: var(--secondary); */
background: #fafafa;
2020-02-03 10:50:30 +01:00
}
2020-02-03 10:50:30 +01:00
.selected {
color: var(--button-text);
2020-02-03 10:50:30 +01:00
background: var(--background-button) !important;
}
2020-02-03 10:50:30 +01:00
.title {
margin-left: 10px;
2020-02-03 10:50:30 +01:00
}
2020-02-03 10:50:30 +01:00
.icon {
display: inline-block;
transition: 0.2s;
width: 24px;
height: 24px;
2020-02-03 10:50:30 +01:00
}
</style>