1
0
Fork 0
mirror of synced 2024-06-14 00:14:39 +12:00
budibase/packages/builder/src/components/design/PropertiesPanel/PropertyControls/EventsEditor/EventEditor.svelte

180 lines
4 KiB
Svelte
Raw Normal View History

2021-01-09 07:22:03 +13:00
<script>
2021-03-11 06:56:16 +13:00
import { flip } from "svelte/animate"
import { dndzone } from "svelte-dnd-action"
2021-05-01 03:53:33 +12:00
import {
Icon,
Button,
Layout,
DrawerContent,
ActionMenu,
MenuItem,
} from "@budibase/bbui"
2021-01-09 07:22:03 +13:00
import actionTypes from "./actions"
import { generate } from "shortid"
2021-01-09 07:22:03 +13:00
2021-03-11 06:56:16 +13:00
const flipDurationMs = 150
2021-02-27 02:21:05 +13:00
const EVENT_TYPE_KEY = "##eventHandlerType"
2021-01-09 07:22:03 +13:00
export let actions
// dndzone needs an id on the array items, so this adds some temporary ones.
$: {
if (actions) {
2021-04-24 00:03:34 +12:00
actions.forEach((action) => {
if (!action.id) {
action.id = generate()
}
})
}
}
2021-01-09 07:22:03 +13:00
let selectedAction = actions?.length ? actions[0] : null
2021-01-09 07:22:03 +13:00
$: selectedActionComponent =
selectedAction &&
2021-04-24 00:03:34 +12:00
actionTypes.find((t) => t.name === selectedAction[EVENT_TYPE_KEY]).component
2021-01-09 07:22:03 +13:00
// Select the first action if we delete an action
$: {
if (selectedAction && !actions?.includes(selectedAction)) {
selectedAction = actions?.[0]
}
}
2021-04-24 00:03:34 +12:00
const deleteAction = (index) => {
2021-01-09 07:22:03 +13:00
actions.splice(index, 1)
actions = actions
}
2021-04-24 00:03:34 +12:00
const addAction = (actionType) => () => {
2021-01-09 07:22:03 +13:00
const newAction = {
parameters: {},
[EVENT_TYPE_KEY]: actionType.name,
id: generate(),
2021-01-09 07:22:03 +13:00
}
2021-01-28 06:29:30 +13:00
if (!actions) {
actions = []
}
2021-02-27 02:21:05 +13:00
actions = [...actions, newAction]
2021-01-09 07:22:03 +13:00
selectedAction = newAction
}
2021-04-24 00:03:34 +12:00
const selectAction = (action) => () => {
2021-01-09 07:22:03 +13:00
selectedAction = action
}
2021-02-27 02:21:05 +13:00
function handleDndConsider(e) {
2021-03-11 06:56:16 +13:00
actions = e.detail.items
}
function handleDndFinalize(e) {
actions = e.detail.items
}
2021-01-09 07:22:03 +13:00
</script>
<DrawerContent>
<div class="actions-list" slot="sidebar">
2021-04-29 01:04:30 +12:00
<Layout>
2021-05-01 03:53:33 +12:00
<ActionMenu>
<Button slot="control" secondary>Add Action</Button>
{#each actionTypes as actionType}
<MenuItem on:click={addAction(actionType)}>
{actionType.name}
</MenuItem>
{/each}
</ActionMenu>
2021-04-29 01:04:30 +12:00
{#if actions && actions.length > 0}
<div
class="action-dnd-container"
use:dndzone={{
items: actions,
flipDurationMs,
dropTargetStyle: { outline: "none" },
}}
on:consider={handleDndConsider}
on:finalize={handleDndFinalize}
>
{#each actions as action, index (action.id)}
2021-04-24 00:03:34 +12:00
<div
2021-04-29 01:04:30 +12:00
class="action-container"
animate:flip={{ duration: flipDurationMs }}
2021-04-24 00:03:34 +12:00
>
2021-04-29 01:04:30 +12:00
<div
class="action-header"
class:selected={action === selectedAction}
on:click={selectAction(action)}
>
{index + 1}.
{action[EVENT_TYPE_KEY]}
</div>
<div
on:click={() => deleteAction(index)}
style="margin-left: auto;"
>
<Icon size="S" hoverable name="Close" />
</div>
2021-04-24 00:03:34 +12:00
</div>
2021-04-29 01:04:30 +12:00
{/each}
</div>
{/if}
2021-04-29 01:04:30 +12:00
</Layout>
2021-01-09 07:22:03 +13:00
</div>
2021-04-29 01:04:30 +12:00
<Layout>
{#if selectedAction}
<div class="selected-action-container">
<svelte:component
this={selectedActionComponent}
parameters={selectedAction.parameters}
/>
</div>
{/if}
</Layout>
</DrawerContent>
2021-01-09 07:22:03 +13:00
<style>
.action-header {
display: flex;
flex-direction: row;
align-items: center;
2021-04-29 01:04:30 +12:00
margin-top: var(--spacing-s);
2021-01-09 07:22:03 +13:00
}
.action-header {
2021-01-09 07:22:03 +13:00
margin-bottom: var(--spacing-m);
2021-05-01 03:53:33 +12:00
font-size: var(--font-size-s);
color: var(--grey-7);
font-weight: 500;
2021-01-09 07:22:03 +13:00
}
.action-header:hover,
.action-header.selected {
2021-01-09 07:22:03 +13:00
cursor: pointer;
color: var(--ink);
2021-01-09 07:22:03 +13:00
}
.available-action {
padding: var(--spacing-s);
2021-05-01 03:53:33 +12:00
font-size: var(--font-size-s);
2021-01-09 07:22:03 +13:00
cursor: pointer;
}
.available-action:hover {
background: var(--grey-2);
}
.action-container {
border-bottom: 1px solid var(--grey-1);
2021-01-13 05:49:11 +13:00
display: flex;
align-items: center;
2021-01-09 07:22:03 +13:00
}
.action-container:last-child {
border-bottom: none;
}
2021-01-09 07:22:03 +13:00
i:hover {
color: var(--red);
cursor: pointer;
}
2021-01-09 07:22:03 +13:00
</style>