2022-03-23 06:40:36 +08:00
|
|
|
<script lang="ts">
|
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";
|
|
|
|
|
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 = "";
|
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
|
|
|
|
2022-03-24 07:36:07 +08:00
|
|
|
const dispatch = createEventDispatcher<{ click: number }>();
|
|
|
|
|
2022-11-04 03:35:56 +08:00
|
|
|
let samples_dir: string = (root_url ?? root) + "file=";
|
2022-03-24 07:36:07 +08:00
|
|
|
let page = 0;
|
|
|
|
let gallery = headers.length === 1;
|
|
|
|
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
|
|
|
|
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>
|
|
|
|
|
2022-06-17 07:49:54 +08:00
|
|
|
<div
|
|
|
|
id={elem_id}
|
|
|
|
class="mt-4 inline-block max-w-full text-gray-700 w-full"
|
2022-06-17 08:22:09 +08:00
|
|
|
class:!hidden={!visible}
|
2022-06-17 07:49:54 +08:00
|
|
|
>
|
2022-05-02 16:38:18 +08:00
|
|
|
<div class="text-xs mb-2 flex items-center text-gray-500">
|
|
|
|
<svg
|
|
|
|
xmlns="http://www.w3.org/2000/svg"
|
|
|
|
xmlns:xlink="http://www.w3.org/1999/xlink"
|
|
|
|
aria-hidden="true"
|
|
|
|
role="img"
|
|
|
|
class="mr-1"
|
|
|
|
width="1em"
|
|
|
|
height="1em"
|
|
|
|
preserveAspectRatio="xMidYMid meet"
|
|
|
|
viewBox="0 0 32 32"
|
|
|
|
><path
|
|
|
|
fill="currentColor"
|
|
|
|
d="M10 6h18v2H10zm0 18h18v2H10zm0-9h18v2H10zm-6 0h2v2H4zm0-9h2v2H4zm0 18h2v2H4z"
|
|
|
|
/></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}
|
2022-05-02 16:38:18 +08:00
|
|
|
<div class="gr-samples-gallery">
|
2022-03-24 07:36:07 +08:00
|
|
|
{#each selected_samples as sample_row, i}
|
2022-07-22 02:12:46 +08:00
|
|
|
<!-- {@const x = component_map[]} -->
|
|
|
|
|
2022-03-24 07:36:07 +08:00
|
|
|
<button
|
2022-05-02 16:38:18 +08:00
|
|
|
class="group rounded-lg"
|
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);
|
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}
|
|
|
|
/>
|
|
|
|
{/if}
|
2022-03-24 07:36:07 +08:00
|
|
|
</button>
|
|
|
|
{/each}
|
|
|
|
</div>
|
|
|
|
{:else}
|
2022-05-02 16:38:18 +08:00
|
|
|
<div class="overflow-x-auto border table-auto rounded-lg w-full text-sm">
|
|
|
|
<table class="gr-samples-table">
|
|
|
|
<thead>
|
2022-05-10 08:26:09 +08:00
|
|
|
<tr
|
|
|
|
class="border-b dark:border-gray-800 divide-x dark:divide-gray-800 shadow-sm"
|
|
|
|
>
|
2022-05-02 16:38:18 +08:00
|
|
|
{#each headers as header}
|
|
|
|
<th class="p-2 whitespace-nowrap min-w-lg text-left">
|
|
|
|
{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
|
2022-05-10 08:26:09 +08:00
|
|
|
class="group cursor-pointer odd:bg-gray-50 border-b dark:border-gray-800 divide-x dark:divide-gray-800 last:border-none hover:bg-orange-50 hover:divide-orange-100 dark:hover:bg-gray-700"
|
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
|
|
|
}}
|
|
|
|
>
|
2022-07-22 02:12:46 +08:00
|
|
|
{#each sample_row as { value, component }, j}
|
|
|
|
{#if components[j] !== undefined && component_map[components[j]] !== undefined}
|
|
|
|
<td class="p-2">
|
|
|
|
<svelte:component this={component} {value} {samples_dir} />
|
|
|
|
</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}
|
|
|
|
</div>
|
|
|
|
{#if paginate}
|
2022-05-02 16:38:18 +08:00
|
|
|
<div class="flex gap-2 items-center justify-center text-sm">
|
2022-03-24 07:36:07 +08:00
|
|
|
Pages:
|
|
|
|
{#each visible_pages as visible_page}
|
|
|
|
{#if visible_page === -1}
|
|
|
|
<div>...</div>
|
|
|
|
{:else}
|
|
|
|
<button
|
|
|
|
class:font-bold={page === visible_page}
|
|
|
|
on:click={() => (page = visible_page)}
|
|
|
|
>
|
|
|
|
{visible_page + 1}
|
|
|
|
</button>
|
|
|
|
{/if}
|
2022-03-23 06:40:36 +08:00
|
|
|
{/each}
|
2022-03-24 07:36:07 +08:00
|
|
|
</div>
|
|
|
|
{/if}
|