2022-03-23 06:40:36 +08:00
|
|
|
<script lang="ts">
|
2023-03-07 04:52:31 +08:00
|
|
|
import { hover } from "@testing-library/user-event/dist/hover";
|
2022-03-24 07:36:07 +08:00
|
|
|
import { createEventDispatcher } from "svelte";
|
2022-07-22 02:12:46 +08:00
|
|
|
import type { SvelteComponentDev, ComponentType } from "svelte/internal";
|
2022-03-24 07:36:07 +08:00
|
|
|
import { component_map } from "./directory";
|
2023-03-14 08:12:41 +08:00
|
|
|
import type { SelectData } from "@gradio/utils";
|
2022-03-24 07:36:07 +08:00
|
|
|
|
2022-07-22 02:12:46 +08:00
|
|
|
export let components: Array<keyof typeof component_map>;
|
2022-08-20 07:56:48 +08:00
|
|
|
export let label: string = "Examples";
|
2022-03-23 06:40:36 +08:00
|
|
|
export let headers: Array<string>;
|
2022-03-24 07:36:07 +08:00
|
|
|
export let samples: Array<Array<any>>;
|
2022-05-12 12:40:41 +08:00
|
|
|
export let elem_id: string = "";
|
2023-03-16 05:01:53 +08:00
|
|
|
export let elem_classes: Array<string> = [];
|
2022-06-17 07:49:54 +08:00
|
|
|
export let visible: boolean = true;
|
2022-07-22 02:12:46 +08:00
|
|
|
export let value: number | null = null;
|
2022-03-25 12:00:30 +08:00
|
|
|
export let root: string;
|
2022-11-04 03:35:56 +08:00
|
|
|
export let root_url: null | string;
|
2022-03-24 07:36:07 +08:00
|
|
|
export let samples_per_page: number = 10;
|
2022-03-23 06:40:36 +08:00
|
|
|
|
2023-03-14 08:12:41 +08:00
|
|
|
const dispatch = createEventDispatcher<{
|
|
|
|
click: number;
|
|
|
|
select: SelectData;
|
|
|
|
}>();
|
2022-03-24 07:36:07 +08:00
|
|
|
|
2023-02-07 23:55:51 +08:00
|
|
|
let samples_dir: string = root_url
|
2023-03-08 00:36:25 +08:00
|
|
|
? "proxy=" + root_url + "/file="
|
|
|
|
: root + "/file=";
|
2022-03-24 07:36:07 +08:00
|
|
|
let page = 0;
|
2023-02-02 01:09:39 +08:00
|
|
|
$: gallery = components.length < 2;
|
2022-03-24 07:36:07 +08:00
|
|
|
let paginate = samples.length > samples_per_page;
|
|
|
|
|
2022-07-22 02:12:46 +08:00
|
|
|
let selected_samples: Array<Array<any>>;
|
2022-03-24 07:36:07 +08:00
|
|
|
let page_count: number;
|
|
|
|
let visible_pages: Array<number> = [];
|
2022-08-31 04:56:59 +08:00
|
|
|
|
2023-01-18 04:47:40 +08:00
|
|
|
let current_hover = -1;
|
|
|
|
|
|
|
|
function handle_mouseenter(i: number) {
|
|
|
|
current_hover = i;
|
|
|
|
}
|
|
|
|
function handle_mouseleave() {
|
|
|
|
current_hover = -1;
|
|
|
|
}
|
|
|
|
|
2022-03-24 07:36:07 +08:00
|
|
|
$: {
|
|
|
|
if (paginate) {
|
|
|
|
visible_pages = [];
|
|
|
|
selected_samples = samples.slice(
|
|
|
|
page * samples_per_page,
|
|
|
|
(page + 1) * samples_per_page
|
|
|
|
);
|
|
|
|
page_count = Math.ceil(samples.length / samples_per_page);
|
|
|
|
[0, page, page_count - 1].forEach((anchor) => {
|
|
|
|
for (let i = anchor - 2; i <= anchor + 2; i++) {
|
|
|
|
if (i >= 0 && i < page_count && !visible_pages.includes(i)) {
|
|
|
|
if (
|
|
|
|
visible_pages.length > 0 &&
|
|
|
|
i - visible_pages[visible_pages.length - 1] > 1
|
|
|
|
) {
|
|
|
|
visible_pages.push(-1);
|
|
|
|
}
|
|
|
|
visible_pages.push(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
selected_samples = samples.slice();
|
|
|
|
}
|
|
|
|
}
|
2022-07-22 02:12:46 +08:00
|
|
|
|
|
|
|
$: component_meta = selected_samples.map((sample_row) =>
|
|
|
|
sample_row.map((sample_cell, j) => ({
|
|
|
|
value: sample_cell,
|
|
|
|
component: component_map[
|
|
|
|
components[j]
|
|
|
|
] as ComponentType<SvelteComponentDev>
|
|
|
|
}))
|
|
|
|
);
|
2022-03-23 06:40:36 +08:00
|
|
|
</script>
|
|
|
|
|
2023-03-16 05:01:53 +08:00
|
|
|
<div id={elem_id} class="wrap {elem_classes.join(' ')}" class:hide={!visible}>
|
2023-01-18 04:47:40 +08:00
|
|
|
<div class="label">
|
2022-05-02 16:38:18 +08:00
|
|
|
<svg
|
|
|
|
xmlns="http://www.w3.org/2000/svg"
|
|
|
|
xmlns:xlink="http://www.w3.org/1999/xlink"
|
|
|
|
aria-hidden="true"
|
|
|
|
role="img"
|
|
|
|
width="1em"
|
|
|
|
height="1em"
|
|
|
|
preserveAspectRatio="xMidYMid meet"
|
|
|
|
viewBox="0 0 32 32"
|
2023-01-18 04:47:40 +08:00
|
|
|
>
|
|
|
|
<path
|
2022-05-02 16:38:18 +08:00
|
|
|
fill="currentColor"
|
|
|
|
d="M10 6h18v2H10zm0 18h18v2H10zm0-9h18v2H10zm-6 0h2v2H4zm0-9h2v2H4zm0 18h2v2H4z"
|
2023-01-18 04:47:40 +08:00
|
|
|
/>
|
|
|
|
</svg>
|
2022-08-20 07:56:48 +08:00
|
|
|
{label}
|
2022-05-02 16:38:18 +08:00
|
|
|
</div>
|
2022-03-24 07:36:07 +08:00
|
|
|
{#if gallery}
|
2023-01-18 04:47:40 +08:00
|
|
|
<div class="gallery">
|
2022-03-24 07:36:07 +08:00
|
|
|
{#each selected_samples as sample_row, i}
|
|
|
|
<button
|
2023-03-07 04:52:31 +08:00
|
|
|
class="gallery-item"
|
2022-03-24 07:36:07 +08:00
|
|
|
on:click={() => {
|
2022-06-10 07:53:28 +08:00
|
|
|
value = i + page * samples_per_page;
|
|
|
|
dispatch("click", value);
|
2023-03-14 08:12:41 +08:00
|
|
|
dispatch("select", { index: value, value: sample_row });
|
2022-03-24 07:36:07 +08:00
|
|
|
}}
|
2023-01-18 04:47:40 +08:00
|
|
|
on:mouseenter={() => handle_mouseenter(i)}
|
|
|
|
on:mouseleave={() => handle_mouseleave()}
|
2022-03-24 07:36:07 +08:00
|
|
|
>
|
2022-07-22 02:12:46 +08:00
|
|
|
{#if Object.keys(component_map).includes(components[0]) && component_map[components[0]]}
|
|
|
|
<svelte:component
|
|
|
|
this={component_meta[0][0].component}
|
|
|
|
value={sample_row[0]}
|
|
|
|
{samples_dir}
|
2023-01-18 04:47:40 +08:00
|
|
|
type="gallery"
|
|
|
|
selected={current_hover === i}
|
|
|
|
index={i}
|
2022-07-22 02:12:46 +08:00
|
|
|
/>
|
|
|
|
{/if}
|
2022-03-24 07:36:07 +08:00
|
|
|
</button>
|
|
|
|
{/each}
|
|
|
|
</div>
|
|
|
|
{:else}
|
2023-01-18 04:47:40 +08:00
|
|
|
<div class="table-wrap">
|
|
|
|
<table>
|
2022-05-02 16:38:18 +08:00
|
|
|
<thead>
|
2023-01-18 04:47:40 +08:00
|
|
|
<tr class="tr-head">
|
2022-05-02 16:38:18 +08:00
|
|
|
{#each headers as header}
|
2023-01-18 04:47:40 +08:00
|
|
|
<th>
|
2022-05-02 16:38:18 +08:00
|
|
|
{header}
|
|
|
|
</th>
|
2022-03-24 07:36:07 +08:00
|
|
|
{/each}
|
|
|
|
</tr>
|
2022-05-02 16:38:18 +08:00
|
|
|
</thead>
|
|
|
|
<tbody>
|
2022-07-22 02:12:46 +08:00
|
|
|
{#each component_meta as sample_row, i}
|
2022-05-02 16:38:18 +08:00
|
|
|
<tr
|
2023-01-18 04:47:40 +08:00
|
|
|
class="tr-body"
|
2022-05-02 16:38:18 +08:00
|
|
|
on:click={() => {
|
2022-06-10 07:53:28 +08:00
|
|
|
value = i + page * samples_per_page;
|
|
|
|
dispatch("click", value);
|
2022-05-02 16:38:18 +08:00
|
|
|
}}
|
2023-01-18 04:47:40 +08:00
|
|
|
on:mouseenter={() => handle_mouseenter(i)}
|
|
|
|
on:mouseleave={() => handle_mouseleave()}
|
2022-05-02 16:38:18 +08:00
|
|
|
>
|
2022-07-22 02:12:46 +08:00
|
|
|
{#each sample_row as { value, component }, j}
|
|
|
|
{#if components[j] !== undefined && component_map[components[j]] !== undefined}
|
2023-01-18 04:47:40 +08:00
|
|
|
<td>
|
|
|
|
<svelte:component
|
|
|
|
this={component}
|
|
|
|
{value}
|
|
|
|
{samples_dir}
|
|
|
|
type="table"
|
|
|
|
selected={current_hover === i}
|
|
|
|
index={i}
|
|
|
|
/>
|
2022-07-22 02:12:46 +08:00
|
|
|
</td>
|
|
|
|
{/if}
|
2022-05-02 16:38:18 +08:00
|
|
|
{/each}
|
|
|
|
</tr>
|
|
|
|
{/each}
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
</div>
|
2022-03-24 07:36:07 +08:00
|
|
|
{/if}
|
2023-02-08 04:30:30 +08:00
|
|
|
{#if paginate}
|
|
|
|
<div class="paginate">
|
|
|
|
Pages:
|
|
|
|
{#each visible_pages as visible_page}
|
|
|
|
{#if visible_page === -1}
|
|
|
|
<div>...</div>
|
|
|
|
{:else}
|
|
|
|
<button
|
|
|
|
class:current-page={page === visible_page}
|
|
|
|
on:click={() => (page = visible_page)}
|
|
|
|
>
|
|
|
|
{visible_page + 1}
|
|
|
|
</button>
|
|
|
|
{/if}
|
|
|
|
{/each}
|
|
|
|
</div>
|
|
|
|
{/if}
|
2022-03-24 07:36:07 +08:00
|
|
|
</div>
|
2023-01-18 04:47:40 +08:00
|
|
|
|
|
|
|
<style>
|
|
|
|
.wrap {
|
|
|
|
display: inline-block;
|
|
|
|
width: var(--size-full);
|
|
|
|
max-width: var(--size-full);
|
2023-03-07 04:52:31 +08:00
|
|
|
color: var(--body-text-color);
|
2023-01-18 04:47:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
.hide {
|
|
|
|
display: none;
|
|
|
|
}
|
|
|
|
|
|
|
|
.label {
|
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
|
|
|
margin-bottom: var(--size-2);
|
2023-03-17 22:41:53 +08:00
|
|
|
color: var(--block-label-text-color);
|
2023-03-07 04:52:31 +08:00
|
|
|
font-weight: var(--block-label-text-weight);
|
|
|
|
font-size: var(--block-label-text-size);
|
2023-01-18 04:47:40 +08:00
|
|
|
line-height: var(--line-sm);
|
|
|
|
}
|
|
|
|
|
|
|
|
svg {
|
|
|
|
margin-right: var(--size-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
.gallery {
|
|
|
|
display: flex;
|
|
|
|
flex-wrap: wrap;
|
2023-03-07 04:52:31 +08:00
|
|
|
gap: var(--spacing-lg);
|
2023-01-18 04:47:40 +08:00
|
|
|
}
|
|
|
|
|
2023-03-07 04:52:31 +08:00
|
|
|
.gallery-item {
|
2023-03-17 22:41:53 +08:00
|
|
|
border: 1px solid var(--border-color-primary);
|
2023-03-07 04:52:31 +08:00
|
|
|
border-radius: var(--button-large-radius);
|
2023-01-18 04:47:40 +08:00
|
|
|
overflow: hidden;
|
|
|
|
}
|
|
|
|
|
2023-03-07 04:52:31 +08:00
|
|
|
.gallery-item:hover {
|
2023-03-17 22:41:53 +08:00
|
|
|
border-color: var(--border-color-accent);
|
2023-03-07 04:52:31 +08:00
|
|
|
background: var(--table-row-focus);
|
2023-01-18 04:47:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
.table-wrap {
|
2023-03-17 22:41:53 +08:00
|
|
|
border: 1px solid var(--border-color-primary);
|
2023-03-07 04:52:31 +08:00
|
|
|
border-radius: var(--table-radius);
|
2023-01-18 04:47:40 +08:00
|
|
|
width: var(--size-full);
|
|
|
|
table-layout: auto;
|
|
|
|
overflow-x: auto;
|
|
|
|
line-height: var(--line-sm);
|
|
|
|
}
|
|
|
|
table {
|
|
|
|
width: var(--size-full);
|
|
|
|
}
|
|
|
|
|
|
|
|
.tr-head {
|
|
|
|
box-shadow: var(--shadow-drop-lg);
|
2023-03-17 22:41:53 +08:00
|
|
|
border-bottom: 1px solid var(--border-color-primary);
|
2023-01-18 04:47:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
.tr-head > * + * {
|
|
|
|
border-right-width: 0px;
|
|
|
|
border-left-width: 1px;
|
2023-03-17 22:41:53 +08:00
|
|
|
border-color: var(--border-color-primary);
|
2023-01-18 04:47:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
th {
|
|
|
|
padding: var(--size-2);
|
|
|
|
white-space: nowrap;
|
|
|
|
}
|
|
|
|
|
|
|
|
.tr-body {
|
|
|
|
cursor: pointer;
|
2023-03-17 22:41:53 +08:00
|
|
|
border-bottom: 1px solid var(--border-color-primary);
|
2023-01-18 04:47:40 +08:00
|
|
|
background: var(--table-even-background);
|
|
|
|
}
|
|
|
|
|
|
|
|
.tr-body:last-child {
|
|
|
|
border: none;
|
|
|
|
}
|
|
|
|
|
|
|
|
.tr-body:nth-child(odd) {
|
|
|
|
background: var(--table-odd-background);
|
|
|
|
}
|
|
|
|
|
|
|
|
.tr-body:hover {
|
2023-03-07 04:52:31 +08:00
|
|
|
background: var(--table-row-focus);
|
2023-01-18 04:47:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
.tr-body > * + * {
|
|
|
|
border-right-width: 0px;
|
|
|
|
border-left-width: 1px;
|
2023-03-17 22:41:53 +08:00
|
|
|
border-color: var(--border-color-primary);
|
2023-01-18 04:47:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
.tr-body:hover > * + * {
|
2023-03-17 22:41:53 +08:00
|
|
|
border-color: var(--border-color-accent);
|
2023-01-18 04:47:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
td {
|
|
|
|
padding: var(--size-2);
|
2023-03-07 04:52:31 +08:00
|
|
|
text-align: center;
|
2023-01-18 04:47:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
.paginate {
|
|
|
|
display: flex;
|
|
|
|
justify-content: center;
|
|
|
|
align-items: center;
|
2023-03-07 04:52:31 +08:00
|
|
|
gap: var(--spacing-sm);
|
2023-02-08 04:30:30 +08:00
|
|
|
margin-top: var(--size-2);
|
2023-03-17 22:41:53 +08:00
|
|
|
color: var(--block-label-text-color);
|
2023-03-07 04:52:31 +08:00
|
|
|
font-size: var(--text-sm);
|
2023-01-18 04:47:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
button.current-page {
|
|
|
|
font-weight: var(--weight-bold);
|
|
|
|
}
|
|
|
|
</style>
|