open-webui/src/lib/components/common/Folder.svelte

103 lines
2.4 KiB
Svelte
Raw Normal View History

2024-10-15 07:35:14 +00:00
<script>
2024-10-15 09:12:39 +00:00
import { getContext, createEventDispatcher, onMount, onDestroy } from 'svelte';
2024-10-15 07:35:14 +00:00
const i18n = getContext('i18n');
const dispatch = createEventDispatcher();
import ChevronDown from '../icons/ChevronDown.svelte';
import ChevronRight from '../icons/ChevronRight.svelte';
import Collapsible from './Collapsible.svelte';
export let open = true;
2024-10-15 09:12:39 +00:00
export let id = '';
2024-10-15 07:35:14 +00:00
export let name = '';
2024-10-15 09:12:39 +00:00
export let collapsible = true;
let folderElement;
let dragged = false;
const onDragOver = (e) => {
e.preventDefault();
dragged = true;
};
const onDrop = (e) => {
e.preventDefault();
if (folderElement.contains(e.target)) {
console.log('Dropped on the Button');
// get data from the drag event
const dataTransfer = e.dataTransfer.getData('text/plain');
const data = JSON.parse(dataTransfer);
console.log(data);
dispatch('drop', data);
dragged = false;
}
};
const onDragLeave = (e) => {
e.preventDefault();
dragged = false;
};
onMount(() => {
folderElement.addEventListener('dragover', onDragOver);
folderElement.addEventListener('drop', onDrop);
folderElement.addEventListener('dragleave', onDragLeave);
});
onDestroy(() => {
folderElement.addEventListener('dragover', onDragOver);
folderElement.removeEventListener('drop', onDrop);
folderElement.removeEventListener('dragleave', onDragLeave);
});
2024-10-15 07:35:14 +00:00
</script>
2024-10-15 09:12:39 +00:00
<div bind:this={folderElement} class="relative">
{#if dragged}
2024-10-15 07:35:14 +00:00
<div
2024-10-15 09:12:39 +00:00
class="absolute top-0 left-0 w-full h-full rounded-sm bg-gray-200 bg-opacity-50 dark:bg-opacity-10 z-50 pointer-events-none touch-none"
></div>
{/if}
{#if collapsible}
<Collapsible
bind:open
className="w-full "
buttonClassName="w-full"
on:change={(e) => {
dispatch('change', e.detail);
2024-10-15 07:35:14 +00:00
}}
>
2024-10-15 09:12:39 +00:00
<!-- svelte-ignore a11y-no-static-element-interactions -->
<div class="mx-2 w-full">
<button
class="w-full py-1 px-1.5 rounded-md flex items-center gap-1 text-xs text-gray-500 dark:text-gray-500 font-medium hover:bg-gray-100 dark:hover:bg-gray-900 transition"
>
<div class="text-gray-300">
{#if open}
<ChevronDown className=" size-3" strokeWidth="2.5" />
{:else}
<ChevronRight className=" size-3" strokeWidth="2.5" />
{/if}
</div>
<div class="translate-y-[0.5px]">
{name}
</div>
</button>
</div>
<div slot="content">
<slot></slot>
</div>
</Collapsible>
{:else}
<slot></slot>
{/if}
2024-10-15 07:35:14 +00:00
</div>