-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTable.svelte
484 lines (460 loc) · 16.3 KB
/
Table.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
<script>
// @ts-nocheck
import { onMount, afterUpdate, onDestroy } from 'svelte';
import { writable } from 'svelte/store';
import { hashCode, saveSettings, loadSettings, hideOnClickOutside } from '$lib/utils';
import { supabase } from '$lib/supabaseClient';
import CrudForm from './CrudForm.svelte';
import ReadModal from './ReadModal.svelte';
export let actions = [];
export let headers = ['Nom', 'Email', 'Rôle', 'Actions'];
export let filters = [];
export let dbInfo = {}; // { table: 'users', key: 'id, email, role'}
export let type = 'utilisateur';
export let type_accord = 'un';
export let parseItems = null;
export let size = 10;
export let addNew = null;
let hash = hashCode(dbInfo);
let can_update_settings = false;
const filtersStore = writable(filters);
$: {
filtersStore.set(filters);
if (can_update_settings) saveSettings(hash, filters);
}
let items = [];
let current_page = 0;
let total_items = 0;
let page = [];
$: {
page = [];
if (items.length > 0) {
for (let i = 0; i <= total_items / size; i++) {
page = [...page, i + 1];
}
}
}
/**
* Load the page of items
* @param {number} page - The page number
* @param {string} filter - The filter to apply to the query (optional, default '')
* @param {number} step - The number of items per page (optional, default 5 items)
* @returns {none} - Sets the items variable
*/
async function loadPage(page, filter = '', step = size) {
let items = [];
let query = supabase.from(dbInfo.table).select(dbInfo.key, { count: 'estimated', head: false });
if (filter) {
filter = filter.split('&');
for (let i = 0; i < filter.length; i++) {
query = query.filter(...filter[i].split(':'));
}
}
const { data, error, count } = await query.range(page * step, (page + 1) * step - 1);
if (error) {
console.error(error);
return;
}
total_items = count;
items = parseItems ? parseItems(data) : data;
return items;
}
function getFiltersString(filters) {
let filtersString = '';
// remove el from array if active is false
// copy array
let tmp = JSON.parse(JSON.stringify(filters));
tmp.forEach((el) => {
el.options = el.options.filter((option) => option.active);
});
// create string
tmp.forEach((el) => {
if (el.options.length > 0) {
filtersString += `${el.value}:in:("${el.options.map((option) => option.value).join('","')}")&`;
}
});
return filtersString.slice(0, -1);
}
let mounted = false;
let filter_state = false;
filtersStore.subscribe(async (value) => {
if (!mounted) return;
const filtersString = getFiltersString(value);
items = await loadPage(0, filtersString);
current_page = 0;
});
onMount(async () => {
let tmp = loadSettings(hash);
if (tmp.length > 0) {
filters = tmp;
}
items = await loadPage(0, getFiltersString(filters));
mounted = true;
const dropdown = document.querySelector('#filterDropdown-' + hash);
setupDropdown();
document.body.appendChild(dropdown);
});
function setupDropdown() {
// set position of the popup just below the button
const dropdown = document.querySelector('#filterDropdown-' + hash);
const rect = document.getElementById('filterDropdownButton').getBoundingClientRect();
dropdown.style.top = 'calc(' + rect.bottom + 'px + 0.5rem)';
if (window.innerWidth < 768) {
dropdown.style.left = rect.left + 'px';
dropdown.style.width = rect.width + 'px';
} else {
dropdown.style.left = 'calc(' + rect.left + 'px - 1.5rem)';
}
}
onresize = () => {
setupDropdown();
};
onDestroy(() => {
const dropdown = document.querySelector('#filterDropdown-' + hash);
dropdown.remove();
});
</script>
<section class="bg-gray-50 dark:bg-gray-900 sm:p-5">
<div class="max-w-screen-xl mx-auto sm:px-4 lg:px-12">
<div class="relative bg-white rounded-lg shadow-md dark:bg-gray-800">
<div
class="flex flex-col items-center justify-between p-4 space-y-3 md:flex-row md:space-y-0 md:space-x-4"
>
<div class="w-full md:w-1/2">
<form class="flex items-center">
<label for="simple-search" class="sr-only">Rechercher</label>
<div class="relative w-full">
<div class="absolute inset-y-0 left-0 flex items-center pl-3 pointer-events-none">
<svg
aria-hidden="true"
class="w-5 h-5 text-gray-500 dark:text-gray-400"
fill="currentColor"
viewbox="0 0 20 20"
xmlns="http://www.w3.org/2000/svg"
>
<path
fill-rule="evenodd"
d="M8 4a4 4 0 100 8 4 4 0 000-8zM2 8a6 6 0 1110.89 3.476l4.817 4.817a1 1 0 01-1.414 1.414l-4.816-4.816A6 6 0 012 8z"
clip-rule="evenodd"
/>
</svg>
</div>
<input
type="text"
id="simple-search"
class="block w-full p-2 pl-10 text-sm text-gray-900 border border-gray-300 rounded-lg bg-gray-50 focus:ring-primary-500 focus:border-primary-500 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-primary-500 dark:focus:border-primary-500"
placeholder="Search"
required=""
/>
</div>
</form>
</div>
<div
class="flex flex-col items-stretch justify-end flex-shrink-0 w-full space-y-2 md:w-auto md:flex-row md:space-y-0 md:items-center md:space-x-3"
>
{#if addNew != null}
<button
type="button"
class="flex items-center justify-center px-4 py-2 text-sm font-medium text-white rounded-lg bg-primary-700 hover:bg-primary-800 focus:ring-4 focus:ring-primary-300 dark:bg-primary-600 dark:hover:bg-primary-700 focus:outline-none dark:focus:ring-primary-800"
id="addNewButton"
on:click={addNew}
>
<svg
class="h-3.5 w-3.5 mr-2"
fill="currentColor"
viewbox="0 0 20 20"
xmlns="http://www.w3.org/2000/svg"
aria-hidden="true"
>
<path
clip-rule="evenodd"
fill-rule="evenodd"
d="M10 3a1 1 0 011 1v5h5a1 1 0 110 2h-5v5a1 1 0 11-2 0v-5H4a1 1 0 110-2h5V4a1 1 0 011-1z"
/>
</svg>
Ajouter {type_accord}
{type}
</button>
{/if}
<div class="flex items-center w-full space-x-3 md:w-auto">
<button
id="filterDropdownButton"
class="flex items-center justify-center w-full px-4 py-2 text-sm font-medium text-gray-900 bg-white border border-gray-200 rounded-lg md:w-auto focus:outline-none hover:bg-gray-100 hover:text-primary-700 focus:z-10 focus:ring-4 focus:ring-gray-200 dark:focus:ring-gray-700 dark:bg-gray-800 dark:text-gray-400 dark:border-gray-600 dark:hover:text-white dark:hover:bg-gray-700"
type="button"
on:click={(e) => {
const el = document.querySelector('#filterDropdown-' + hash);
el.classList.toggle('hidden');
e.stopPropagation();
hideOnClickOutside(el);
}}
>
<svg
xmlns="http://www.w3.org/2000/svg"
aria-hidden="true"
class="w-4 h-4 mr-2 text-gray-400"
viewbox="0 0 20 20"
fill="currentColor"
>
<path
fill-rule="evenodd"
d="M3 3a1 1 0 011-1h12a1 1 0 011 1v3a1 1 0 01-.293.707L12 11.414V15a1 1 0 01-.293.707l-2 2A1 1 0 018 17v-5.586L3.293 6.707A1 1 0 013 6V3z"
clip-rule="evenodd"
/>
</svg>
Filtres
<svg
class="-mr-1 ml-1.5 w-5 h-5"
fill="currentColor"
viewbox="0 0 20 20"
xmlns="http://www.w3.org/2000/svg"
aria-hidden="true"
>
<path
clip-rule="evenodd"
fill-rule="evenodd"
d="M5.293 7.293a1 1 0 011.414 0L10 10.586l3.293-3.293a1 1 0 111.414 1.414l-4 4a1 1 0 01-1.414 0l-4-4a1 1 0 010-1.414z"
/>
</svg>
</button>
<div
id="filterDropdown-{hash}"
class="absolute z-10 hidden p-3 bg-white rounded-lg shadow md:w-48 dark:bg-gray-700 w-72"
>
{#each filters as filter, i}
{#if filter.category != 'hidden'}
{#if i > 0}
<hr class="my-3 border-gray-200 dark:border-gray-600" />
{/if}
<h6 class="mb-3 text-sm font-medium text-gray-900 dark:text-white">
{filter.category}
</h6>
<ul class="space-y-2 text-sm" aria-labelledby="filterDropdownButton">
{#each filter.options as option}
<li class="flex items-center">
<input
id={option.name}
type="checkbox"
value={option.value}
checked={option.active}
class="w-4 h-4 bg-gray-100 border-gray-300 rounded text-primary-600 focus:ring-primary-500 dark:focus:ring-primary-600 dark:ring-offset-gray-700 focus:ring-2 dark:bg-gray-600 dark:border-gray-500"
on:change={(e) => {
e.preventDefault();
can_update_settings = true;
console.log('can update localstorage');
option.active = e.target.checked;
filtersStore.set(filters);
}}
/>
<label
for={option.name}
class="ml-2 text-sm font-medium text-gray-900 dark:text-gray-100"
>{option.name}</label
>
</li>
{/each}
</ul>
{/if}
{/each}
</div>
</div>
</div>
</div>
<div class="overflow-x-auto">
<table class="w-full text-sm text-left text-gray-500 dark:text-gray-400">
<thead
class="text-xs text-gray-700 uppercase bg-gray-50 dark:bg-gray-700 dark:text-gray-400"
>
<tr>
{#each headers as item}
{#if item === 'Actions'}
<th scope="col" class="px-4 py-3">
<span class="sr-only">Actions</span>
</th>
{:else}
<th scope="col" class="px-4 py-3">{item}</th>
{/if}
{/each}
</tr>
</thead>
<tbody>
{#each items as item, i}
<tr class="border-b dark:border-gray-700">
{#each item as key}
{#if key.value === item[0].value && headers[0] === 'Nom'}
<th
scope="row"
class="flex items-center px-4 py-3 font-medium text-gray-900 whitespace-nowrap dark:text-white"
data-utils={key.data || ''}
>
{#if key.avatar}
<div class="flex items-center mr-2 space-x-2">
<img src={key.avatar} class="w-8 h-8 rounded-full" alt="user face" />
</div>
{/if}
{key.value}</th
>
{:else}
<td class="px-4 py-3" data-utils={key.data || ''}>{key.value}</td>
{/if}
{/each}
{#if actions.length > 0}
<td class="flex items-center justify-end px-4 py-3">
<button
id="{i}-dropdown-button"
data-dropdown-toggle="{i}-dropdown"
class="inline-flex items-center p-0.5 text-sm font-medium text-center text-gray-500 hover:text-gray-800 rounded-lg focus:outline-none dark:text-gray-400 dark:hover:text-gray-100"
type="button"
on:click={(e) => {
actions.find((el) => el.type == 'view').handler(e);
}}
>
<svg
class="w-5 h-5"
aria-hidden="true"
fill="currentColor"
viewbox="0 0 20 20"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M6 10a2 2 0 11-4 0 2 2 0 014 0zM12 10a2 2 0 11-4 0 2 2 0 014 0zM16 12a2 2 0 100-4 2 2 0 000 4z"
/>
</svg>
</button>
</td>
{/if}
</tr>{/each}
</tbody>
</table>
</div>
<nav
class="flex flex-col items-start justify-between p-4 space-y-3 md:flex-row md:items-center md:space-y-0"
aria-label="Table navigation"
>
<span class="text-sm font-normal text-gray-500 dark:text-gray-400">
Showing
<span class="font-semibold text-gray-900 dark:text-white">{items.length}</span>
of
<span class="font-semibold text-gray-900 dark:text-white">{total_items}</span>
</span>
<ul class="inline-flex items-stretch -space-x-px">
<li>
<a
href="#"
class="flex items-center justify-center h-full py-1.5 px-3 ml-0 text-gray-500 bg-white rounded-l-lg border border-gray-300 hover:bg-gray-100 hover:text-gray-700 dark:bg-gray-800 dark:border-gray-700 dark:text-gray-400 dark:hover:bg-gray-700 dark:hover:text-white"
on:click={async (e) => {
e.preventDefault();
current_page--;
if (current_page < 0) current_page = 0;
items = await loadPage(current_page, getFiltersString(filters));
}}
>
<span class="sr-only">Previous</span>
<svg
class="w-5 h-5"
aria-hidden="true"
fill="currentColor"
viewbox="0 0 20 20"
xmlns="http://www.w3.org/2000/svg"
>
<path
fill-rule="evenodd"
d="M12.707 5.293a1 1 0 010 1.414L9.414 10l3.293 3.293a1 1 0 01-1.414 1.414l-4-4a1 1 0 010-1.414l4-4a1 1 0 011.414 0z"
clip-rule="evenodd"
/>
</svg>
</a>
</li>
{#if page.length < 5}
{#each page as p}
{#if p == current_page + 1}
<li>
<a
href="#"
aria-current="page"
class="z-10 flex items-center justify-center px-3 py-2 text-sm leading-tight border text-primary-600 bg-primary-50 border-primary-300 hover:bg-primary-100 hover:text-primary-700 dark:border-gray-700 dark:bg-gray-700 dark:text-white"
>{p}</a
>
</li>
{:else}
<li>
<a
href="#"
class="flex items-center justify-center px-3 py-2 text-sm leading-tight text-gray-500 bg-white border border-gray-300 hover:bg-gray-100 hover:text-gray-700 dark:bg-gray-800 dark:border-gray-700 dark:text-gray-400 dark:hover:bg-gray-700 dark:hover:text-white"
on:click={async (e) => {
e.preventDefault();
current_page = p - 1;
items = await loadPage(current_page, getFiltersString(filters));
}}>{p}</a
>
</li>
{/if}
{/each}
{:else}
{#if current_page != 0}
<li>
<a
href="#"
class="flex items-center justify-center px-3 py-2 text-sm leading-tight text-gray-500 bg-white border border-gray-300 hover:bg-gray-100 hover:text-gray-700 dark:bg-gray-800 dark:border-gray-700 dark:text-gray-400 dark:hover:bg-gray-700 dark:hover:text-white"
on:click={async (e) => {
e.preventDefault();
current_page = 0;
items = await loadPage(current_page, getFiltersString(filters));
}}>{1}</a
>
</li>
{/if}
<li>
<a
href="#"
aria-current="page"
class="z-10 flex items-center justify-center px-3 py-2 text-sm leading-tight border text-primary-600 bg-primary-50 border-primary-300 hover:bg-primary-100 hover:text-primary-700 dark:border-gray-700 dark:bg-gray-700 dark:text-white"
>{current_page + 1}</a
>
</li>
{#if current_page != page.length - 1}
<li>
<a
href="#"
class="flex items-center justify-center px-3 py-2 text-sm leading-tight text-gray-500 bg-white border border-gray-300 hover:bg-gray-100 hover:text-gray-700 dark:bg-gray-800 dark:border-gray-700 dark:text-gray-400 dark:hover:bg-gray-700 dark:hover:text-white"
on:click={async (e) => {
e.preventDefault();
current_page = page.length - 1;
items = await loadPage(current_page, getFiltersString(filters));
}}>{page.length}</a
>
</li>
{/if}
{/if}
<li>
<a
href="#"
class="flex items-center justify-center h-full py-1.5 px-3 leading-tight text-gray-500 bg-white rounded-r-lg border border-gray-300 hover:bg-gray-100 hover:text-gray-700 dark:bg-gray-800 dark:border-gray-700 dark:text-gray-400 dark:hover:bg-gray-700 dark:hover:text-white"
on:click={async (e) => {
e.preventDefault();
current_page++;
if (current_page >= total_items / size) current_page = total_items / size - 1;
items = await loadPage(current_page, getFiltersString(filters));
}}
>
<span class="sr-only">Next</span>
<svg
class="w-5 h-5"
aria-hidden="true"
fill="currentColor"
viewbox="0 0 20 20"
xmlns="http://www.w3.org/2000/svg"
>
<path
fill-rule="evenodd"
d="M7.293 14.707a1 1 0 010-1.414L10.586 10 7.293 6.707a1 1 0 011.414-1.414l4 4a1 1 0 010 1.414l-4 4a1 1 0 01-1.414 0z"
clip-rule="evenodd"
/>
</svg>
</a>
</li>
</ul>
</nav>
</div>
</div>
</section>
<style></style>