-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathpaginated.svelte
168 lines (152 loc) · 4.42 KB
/
paginated.svelte
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<script lang="ts">
import { tick } from 'svelte';
import { page } from '$app/stores';
import Button from '$lib/holocene/button.svelte';
import IconButton from '$lib/holocene/icon-button.svelte';
import FilterSelect from '$lib/holocene/select/filter-select.svelte';
import {
currentPageKey,
defaultItemsPerPage,
MAX_PAGE_SIZE,
options,
pagination,
perPageKey,
} from '$lib/stores/pagination';
import { updateQueryParameters } from '$lib/utilities/update-query-parameters';
import PaginatedTable from './index.svelte';
type Item = $$Generic;
export let id: string = null;
export let items: Item[];
export let variant: 'primary' | 'split' = 'primary';
export let updating = false;
export let perPageLabel: string;
export let pageButtonLabel: (page: number) => string;
export let nextPageButtonLabel: string;
export let previousPageButtonLabel: string;
export let maxHeight = '';
export let pageSizeOptions: string[] = options;
export let fixed = false;
$: url = $page.url;
$: perPageParam = url.searchParams.get(perPageKey) ?? pageSizeOptions[0];
$: currentPageParam = url.searchParams.get(currentPageKey) || '1';
$: hash = $page.url.hash;
$: store = pagination(items, perPageParam, currentPageParam);
// keep the 'page-size' url search param within the supported options
$: {
if (parseInt(perPageParam, 10) > parseInt(MAX_PAGE_SIZE, 10)) {
updateQueryParameters({
parameter: perPageKey,
value: MAX_PAGE_SIZE,
url,
});
} else if (!pageSizeOptions.includes(perPageParam)) {
updateQueryParameters({
parameter: perPageKey,
value: defaultItemsPerPage,
url,
});
}
}
// Keep the 'page' url search param within 1 and the total number of pages
$: {
if (
$store.totalPages &&
parseInt(currentPageParam, 10) > $store.totalPages
) {
updateQueryParameters({
parameter: currentPageKey,
value: $store.totalPages,
url,
});
} else if (
currentPageParam === null ||
parseInt(currentPageParam, 10) < 0
) {
updateQueryParameters({
parameter: currentPageKey,
value: '1',
url,
});
}
}
const handlePageChange = (page: number) => {
updateQueryParameters({
parameter: currentPageKey,
value: page,
url,
});
};
const scrollToHashEvent = async () => {
store.jumpToHashPage(hash);
await tick();
let id = hash.slice(1);
const row = document?.querySelector(`[data-eventid="${id}"]`);
if (row) {
setTimeout(() => {
row?.scrollIntoView({ behavior: 'smooth' });
}, 500);
}
};
$: {
if (currentPageParam && !hash) store.jumpToPage(currentPageParam);
if (hash && !url.searchParams.get(currentPageKey)) scrollToHashEvent();
if (perPageParam) store.adjustPageSize(perPageParam);
}
</script>
<PaginatedTable
{updating}
{variant}
{maxHeight}
visibleItems={$store.items}
{fixed}
{id}
>
<slot name="caption" slot="caption" />
<slot name="headers" slot="headers" visibleItems={$store.items} />
<slot visibleItems={$store.items} />
<svelte:fragment slot="actions-start">
<FilterSelect
label={perPageLabel}
parameter={perPageKey}
value={perPageParam}
options={pageSizeOptions}
/>
</svelte:fragment>
<div class="hidden items-center gap-2 md:flex" slot="actions-center">
{#each $store.pageShortcuts as page}
{#if isNaN(page)}
<span class="text-primary">...</span>
{:else}
<Button
variant="ghost"
size="sm"
class={page === $store.currentPage
? 'bg-interactive-secondary-active'
: ''}
aria-label={pageButtonLabel(page)}
on:click={() => handlePageChange(page)}>{page}</Button
>
{/if}
{/each}
</div>
<nav
class="flex shrink-0 items-center gap-2"
aria-label={$$restProps['aria-label']}
slot="actions-end"
>
<slot name="actions-end-additional" />
<IconButton
label={previousPageButtonLabel}
disabled={!$store.hasPrevious}
icon="arrow-left"
on:click={() => handlePageChange($store.currentPage - 1)}
/>
<IconButton
label={nextPageButtonLabel}
disabled={!$store.hasNext}
on:click={() => handlePageChange($store.currentPage + 1)}
icon="arrow-right"
/>
</nav>
<slot name="empty" slot="empty" {updating} />
</PaginatedTable>