open-webui/src/lib/components/admin/Settings/Interface/Banners.svelte

103 lines
3 KiB
Svelte
Raw Normal View History

2025-05-22 22:02:11 +00:00
<script lang="ts">
import Switch from '$lib/components/common/Switch.svelte';
2025-07-02 09:48:18 +00:00
import Textarea from '$lib/components/common/Textarea.svelte';
2025-05-22 22:02:11 +00:00
import Tooltip from '$lib/components/common/Tooltip.svelte';
import EllipsisVertical from '$lib/components/icons/EllipsisVertical.svelte';
2025-06-25 22:44:45 +00:00
import XMark from '$lib/components/icons/XMark.svelte';
2025-05-22 22:02:11 +00:00
import Sortable from 'sortablejs';
import { getContext } from 'svelte';
const i18n = getContext('i18n');
export let banners = [];
let sortable = null;
let bannerListElement = null;
const positionChangeHandler = () => {
const bannerIdOrder = Array.from(bannerListElement.children).map((child) =>
child.id.replace('banner-item-', '')
);
// Sort the banners array based on the new order
banners = bannerIdOrder.map((id) => {
const index = banners.findIndex((banner) => banner.id === id);
return banners[index];
});
};
2025-07-02 09:48:18 +00:00
const classNames: Record<string, string> = {
info: 'bg-blue-500/20 text-blue-700 dark:text-blue-200 ',
success: 'bg-green-500/20 text-green-700 dark:text-green-200',
warning: 'bg-yellow-500/20 text-yellow-700 dark:text-yellow-200',
error: 'bg-red-500/20 text-red-700 dark:text-red-200'
};
2025-05-22 22:02:11 +00:00
$: if (banners) {
init();
}
const init = () => {
if (sortable) {
sortable.destroy();
}
if (bannerListElement) {
sortable = Sortable.create(bannerListElement, {
animation: 150,
handle: '.item-handle',
onUpdate: async (event) => {
positionChangeHandler();
}
});
}
};
</script>
2025-07-02 09:48:18 +00:00
<div class=" flex flex-col gap-3" bind:this={bannerListElement}>
2025-05-22 22:02:11 +00:00
{#each banners as banner, bannerIdx (banner.id)}
2025-07-02 09:48:18 +00:00
<div class=" flex justify-between items-start -ml-1" id="banner-item-{banner.id}">
2025-05-22 22:02:11 +00:00
<EllipsisVertical className="size-4 cursor-move item-handle" />
2025-07-02 09:48:18 +00:00
<div class="flex flex-row flex-1 gap-2 items-start">
2025-05-22 22:02:11 +00:00
<select
2025-07-02 09:48:18 +00:00
class="w-fit capitalize rounded-xl text-xs bg-transparent outline-hidden pl-1 pr-5"
2025-05-22 22:02:11 +00:00
bind:value={banner.type}
required
>
{#if banner.type == ''}
<option value="" selected disabled class="text-gray-900">{$i18n.t('Type')}</option>
{/if}
<option value="info" class="text-gray-900">{$i18n.t('Info')}</option>
<option value="warning" class="text-gray-900">{$i18n.t('Warning')}</option>
<option value="error" class="text-gray-900">{$i18n.t('Error')}</option>
<option value="success" class="text-gray-900">{$i18n.t('Success')}</option>
</select>
2025-07-02 09:48:18 +00:00
<Textarea
className="mr-2 text-xs w-full bg-transparent outline-hidden resize-none"
2025-05-22 22:02:11 +00:00
placeholder={$i18n.t('Content')}
bind:value={banner.content}
2025-07-02 09:48:18 +00:00
maxSize={100}
2025-05-22 22:02:11 +00:00
/>
<div class="relative -left-2">
<Tooltip content={$i18n.t('Dismissible')} className="flex h-fit items-center">
<Switch bind:state={banner.dismissible} />
</Tooltip>
</div>
</div>
<button
class="pr-3"
type="button"
on:click={() => {
banners.splice(bannerIdx, 1);
banners = banners;
}}
>
2025-06-27 11:44:26 +00:00
<XMark className={'size-4'} />
2025-05-22 22:02:11 +00:00
</button>
</div>
{/each}
</div>