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-12-13 23:55:45 +13:00
|
|
|
import { getAvailableActions } from "./index"
|
2021-04-20 01:37:04 +12:00
|
|
|
import { generate } from "shortid"
|
2021-12-09 21:36:50 +13:00
|
|
|
import { getButtonContextBindings } from "builderStore/dataBinding"
|
2022-03-16 00:16:51 +13:00
|
|
|
import { currentAsset, store } from "builderStore"
|
2021-01-09 07:22:03 +13:00
|
|
|
|
2021-03-11 06:56:16 +13:00
|
|
|
const flipDurationMs = 150
|
2021-01-16 02:11:51 +13:00
|
|
|
const EVENT_TYPE_KEY = "##eventHandlerType"
|
2021-09-02 22:38:41 +12:00
|
|
|
const actionTypes = getAvailableActions()
|
2021-01-09 07:22:03 +13:00
|
|
|
|
2022-03-16 00:16:51 +13:00
|
|
|
export let key
|
2021-01-22 03:52:59 +13:00
|
|
|
export let actions
|
2021-08-02 00:39:33 +12:00
|
|
|
export let bindings = []
|
2021-02-27 05:11:14 +13:00
|
|
|
|
2021-12-09 21:36:50 +13:00
|
|
|
let selectedAction = actions?.length ? actions[0] : null
|
|
|
|
|
|
|
|
// These are ephemeral bindings which only exist while executing actions
|
|
|
|
$: buttonContextBindings = getButtonContextBindings(
|
2022-03-16 00:16:51 +13:00
|
|
|
$currentAsset,
|
|
|
|
$store.selectedComponentId,
|
|
|
|
key,
|
2021-12-09 21:36:50 +13:00
|
|
|
actions,
|
|
|
|
selectedAction?.id
|
|
|
|
)
|
|
|
|
$: allBindings = buttonContextBindings.concat(bindings)
|
|
|
|
|
|
|
|
// Assign a unique ID to each action
|
2021-04-20 01:37:04 +12:00
|
|
|
$: {
|
|
|
|
if (actions) {
|
2021-05-04 22:32:22 +12:00
|
|
|
actions.forEach(action => {
|
2021-04-20 01:37:04 +12:00
|
|
|
if (!action.id) {
|
|
|
|
action.id = generate()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2021-02-27 05:11:14 +13:00
|
|
|
}
|
2021-01-09 07:22:03 +13:00
|
|
|
|
|
|
|
$: selectedActionComponent =
|
|
|
|
selectedAction &&
|
2021-08-20 20:54:54 +12:00
|
|
|
actionTypes.find(t => t.name === selectedAction[EVENT_TYPE_KEY])?.component
|
2021-01-09 07:22:03 +13:00
|
|
|
|
2021-02-19 06:44:56 +13:00
|
|
|
// Select the first action if we delete an action
|
|
|
|
$: {
|
|
|
|
if (selectedAction && !actions?.includes(selectedAction)) {
|
|
|
|
selectedAction = actions?.[0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-04 22:32:22 +12:00
|
|
|
const deleteAction = index => {
|
2021-01-09 07:22:03 +13:00
|
|
|
actions.splice(index, 1)
|
|
|
|
actions = actions
|
|
|
|
}
|
|
|
|
|
2021-05-04 22:32:22 +12:00
|
|
|
const addAction = actionType => () => {
|
2021-01-09 07:22:03 +13:00
|
|
|
const newAction = {
|
|
|
|
parameters: {},
|
2021-01-16 02:11:51 +13:00
|
|
|
[EVENT_TYPE_KEY]: actionType.name,
|
2021-04-20 01:37:04 +12:00
|
|
|
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-05-04 22:32:22 +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>
|
|
|
|
|
2021-04-29 00:42:44 +12:00
|
|
|
<DrawerContent>
|
2021-07-21 01:34:27 +12:00
|
|
|
<Layout noPadding gap="S" slot="sidebar">
|
|
|
|
{#if actions && actions.length > 0}
|
|
|
|
<div
|
|
|
|
class="actions"
|
|
|
|
use:dndzone={{
|
|
|
|
items: actions,
|
|
|
|
flipDurationMs,
|
|
|
|
dropTargetStyle: { outline: "none" },
|
|
|
|
}}
|
|
|
|
on:consider={handleDndConsider}
|
|
|
|
on:finalize={handleDndFinalize}
|
|
|
|
>
|
|
|
|
{#each actions as action, index (action.id)}
|
|
|
|
<div
|
|
|
|
class="action-container"
|
|
|
|
animate:flip={{ duration: flipDurationMs }}
|
|
|
|
class:selected={action === selectedAction}
|
|
|
|
on:click={selectAction(action)}
|
|
|
|
>
|
|
|
|
<Icon name="DragHandle" size="XL" />
|
|
|
|
<div class="action-header">
|
|
|
|
{index + 1}. {action[EVENT_TYPE_KEY]}
|
2021-04-24 00:03:34 +12:00
|
|
|
</div>
|
2021-07-21 01:34:27 +12:00
|
|
|
<Icon
|
|
|
|
name="Close"
|
|
|
|
hoverable
|
|
|
|
size="S"
|
|
|
|
on:click={() => deleteAction(index)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
{/each}
|
|
|
|
</div>
|
|
|
|
{/if}
|
|
|
|
<ActionMenu>
|
|
|
|
<Button slot="control" secondary>Add Action</Button>
|
|
|
|
{#each actionTypes as actionType}
|
|
|
|
<MenuItem on:click={addAction(actionType)}>
|
|
|
|
{actionType.name}
|
|
|
|
</MenuItem>
|
|
|
|
{/each}
|
|
|
|
</ActionMenu>
|
|
|
|
</Layout>
|
|
|
|
<Layout noPadding>
|
2021-08-20 20:54:54 +12:00
|
|
|
{#if selectedActionComponent}
|
2022-02-14 04:18:54 +13:00
|
|
|
{#key selectedAction.id}
|
|
|
|
<div class="selected-action-container">
|
|
|
|
<svelte:component
|
|
|
|
this={selectedActionComponent}
|
|
|
|
parameters={selectedAction.parameters}
|
|
|
|
bindings={allBindings}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
{/key}
|
2021-04-29 01:04:30 +12:00
|
|
|
{/if}
|
|
|
|
</Layout>
|
2021-04-29 00:52:58 +12:00
|
|
|
</DrawerContent>
|
2021-01-09 07:22:03 +13:00
|
|
|
|
|
|
|
<style>
|
2021-07-21 01:34:27 +12:00
|
|
|
.actions {
|
2021-01-09 07:22:03 +13:00
|
|
|
display: flex;
|
2021-07-21 01:34:27 +12:00
|
|
|
flex-direction: column;
|
|
|
|
justify-content: flex-start;
|
|
|
|
align-items: stretch;
|
|
|
|
gap: var(--spacing-s);
|
2021-01-09 07:22:03 +13:00
|
|
|
}
|
|
|
|
|
2021-02-19 06:44:56 +13:00
|
|
|
.action-header {
|
2021-07-21 01:34:27 +12:00
|
|
|
color: var(--spectrum-global-color-gray-700);
|
2021-01-09 07:22:03 +13:00
|
|
|
|
2021-07-21 01:34:27 +12:00
|
|
|
flex: 1 1 auto;
|
2021-01-09 07:22:03 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
.action-container {
|
2021-07-21 01:34:27 +12:00
|
|
|
background-color: var(--background);
|
|
|
|
padding: var(--spacing-s) var(--spacing-m);
|
|
|
|
border-radius: 4px;
|
|
|
|
border: var(--border-light);
|
|
|
|
transition: background-color 130ms ease-in-out, color 130ms ease-in-out,
|
|
|
|
border-color 130ms ease-in-out;
|
|
|
|
gap: var(--spacing-m);
|
2021-01-13 05:49:11 +13:00
|
|
|
display: flex;
|
2021-07-21 01:34:27 +12:00
|
|
|
flex-direction: row;
|
|
|
|
justify-content: space-between;
|
2021-01-13 05:49:11 +13:00
|
|
|
align-items: center;
|
2021-01-09 07:22:03 +13:00
|
|
|
}
|
2021-07-21 01:34:27 +12:00
|
|
|
.action-container:hover,
|
|
|
|
.action-container.selected {
|
|
|
|
background-color: var(--spectrum-global-color-gray-50);
|
|
|
|
border-color: var(--spectrum-global-color-gray-500);
|
|
|
|
cursor: pointer;
|
|
|
|
}
|
|
|
|
.action-container:hover .action-header,
|
|
|
|
.action-container.selected .action-header {
|
|
|
|
color: var(--spectrum-global-color-gray-900);
|
2021-02-19 06:44:56 +13:00
|
|
|
}
|
2021-01-09 07:22:03 +13:00
|
|
|
</style>
|