@mapomodule/uikit 2.0.0-beta.13 → 2.0.0-beta.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +46 -184
- package/dist/__tests__/MapoConfirmDialog.component.test.d.ts +2 -0
- package/dist/__tests__/MapoConfirmDialog.component.test.d.ts.map +1 -0
- package/dist/__tests__/MapoDetail.component.test.d.ts +2 -0
- package/dist/__tests__/MapoDetail.component.test.d.ts.map +1 -0
- package/dist/__tests__/MapoDetailLangSwitch.component.test.d.ts +2 -0
- package/dist/__tests__/MapoDetailLangSwitch.component.test.d.ts.map +1 -0
- package/dist/__tests__/MapoListTable.component.test.d.ts +2 -0
- package/dist/__tests__/MapoListTable.component.test.d.ts.map +1 -0
- package/dist/__tests__/__mocks__/nuxt-app.d.ts +9 -0
- package/dist/__tests__/__mocks__/nuxt-app.d.ts.map +1 -0
- package/dist/__tests__/listTable.test.d.ts +2 -0
- package/dist/__tests__/listTable.test.d.ts.map +1 -0
- package/dist/module.json +1 -1
- package/dist/runtime/assets/main.css +1 -1
- package/dist/runtime/components/MapoConfirmDialog.vue +18 -1
- package/dist/runtime/components/MapoConfirmDialog.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoDetail.vue +43 -11
- package/dist/runtime/components/MapoDetail.vue.d.ts +76 -0
- package/dist/runtime/components/MapoDetail.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoDetailLangSwitch.vue +21 -25
- package/dist/runtime/components/MapoDetailLangSwitch.vue.d.ts +4 -3
- package/dist/runtime/components/MapoDetailLangSwitch.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoList.vue +3 -2
- package/dist/runtime/components/MapoList.vue.d.ts +9 -4
- package/dist/runtime/components/MapoList.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoListTable.vue +98 -32
- package/dist/runtime/components/MapoListTable.vue.d.ts +11 -6
- package/dist/runtime/components/MapoListTable.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoMediaPreview.vue.d.ts +1 -1
- package/dist/runtime/components/MapoMediaPreview.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoMediaUploader.vue +21 -21
- package/dist/runtime/components/MapoMediaUploader.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoMenuManager.vue.d.ts +1 -1
- package/dist/runtime/components/MapoMenuManager.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoMenuTreeview.vue.d.ts +1 -1
- package/dist/runtime/components/MapoMenuTreeview.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoMenuTreeviewNode.vue.d.ts +2 -2
- package/dist/runtime/stores/media.d.ts +2 -2
- package/dist/runtime/types/list.d.ts +10 -2
- package/dist/runtime/types/list.d.ts.map +1 -1
- package/dist/runtime/utils/listTable.d.ts +29 -0
- package/dist/runtime/utils/listTable.d.ts.map +1 -0
- package/dist/runtime/utils/listTable.js +21 -0
- package/package.json +6 -6
- package/src/__tests__/MapoConfirmDialog.component.test.ts +165 -0
- package/src/__tests__/MapoDetail.component.test.ts +293 -0
- package/src/__tests__/MapoDetailLangSwitch.component.test.ts +145 -0
- package/src/__tests__/MapoListTable.component.test.ts +504 -0
- package/src/__tests__/__mocks__/nuxt-app.ts +8 -0
- package/src/__tests__/listTable.test.ts +82 -0
- package/src/runtime/assets/main.css +9 -0
- package/src/runtime/components/MapoConfirmDialog.vue +28 -1
- package/src/runtime/components/MapoDetail.vue +78 -11
- package/src/runtime/components/MapoDetailLangSwitch.vue +26 -27
- package/src/runtime/components/MapoList.vue +13 -5
- package/src/runtime/components/MapoListTable.vue +128 -41
- package/src/runtime/components/MapoMediaUploader.vue +25 -21
- package/src/runtime/types/list.ts +10 -2
- package/src/runtime/utils/listTable.ts +54 -0
|
@@ -65,6 +65,12 @@ const props = withDefaults(
|
|
|
65
65
|
sidebarFields?: AnyFieldDescriptor<T>[];
|
|
66
66
|
/** Translation language codes (e.g. ['it', 'en']). */
|
|
67
67
|
languages?: string[];
|
|
68
|
+
/**
|
|
69
|
+
* Number of languages above which the language switcher adds a searchable
|
|
70
|
+
* select next to its tabs. Default `8` (from MapoDetailLangSwitch); `0`
|
|
71
|
+
* always shows it.
|
|
72
|
+
*/
|
|
73
|
+
langThreshold?: number;
|
|
68
74
|
/** Human readable model name shown in the page title. */
|
|
69
75
|
modelName?: string | null;
|
|
70
76
|
/** Sidebar column span (1–11 in a 12-col grid). */
|
|
@@ -123,6 +129,7 @@ const props = withDefaults(
|
|
|
123
129
|
schemaFieldOptions: undefined,
|
|
124
130
|
sidebarFields: () => [],
|
|
125
131
|
languages: () => [],
|
|
132
|
+
langThreshold: undefined,
|
|
126
133
|
sidebarCols: 4,
|
|
127
134
|
sticky: true,
|
|
128
135
|
usePatch: true,
|
|
@@ -195,8 +202,21 @@ const errors = ref<Record<string, string[]>>({});
|
|
|
195
202
|
// Start in loading state for existing items so MapoForm is never mounted with
|
|
196
203
|
// empty data and then immediately unmounted when the spinner appears on mount.
|
|
197
204
|
const isLoading = ref(String(props.id) !== "new");
|
|
198
|
-
|
|
199
|
-
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* The action under way, from the click until its work is done, navigation
|
|
208
|
+
* included: one at a time, so a second click cannot save again or go back one
|
|
209
|
+
* more step in the history. Named after the buttons.
|
|
210
|
+
*/
|
|
211
|
+
type DetailAction = "save" | "saveContinue" | "delete" | "back";
|
|
212
|
+
const pendingAction = ref<DetailAction | null>(null);
|
|
213
|
+
const isBusy = computed(() => pendingAction.value !== null);
|
|
214
|
+
const isSaving = computed(() => {
|
|
215
|
+
const savingAndLeaving = pendingAction.value === "save";
|
|
216
|
+
const savingAndStaying = pendingAction.value === "saveContinue";
|
|
217
|
+
return savingAndLeaving || savingAndStaying;
|
|
218
|
+
});
|
|
219
|
+
const isDeleting = computed(() => pendingAction.value === "delete");
|
|
200
220
|
const currentLang = ref(props.languages[0] ?? "");
|
|
201
221
|
const draftBanner = ref<{
|
|
202
222
|
savedAt: Date;
|
|
@@ -428,6 +448,10 @@ async function fetchModel() {
|
|
|
428
448
|
// ─── Save ────────────────────────────────────────────────────────────────────
|
|
429
449
|
|
|
430
450
|
async function save(andBack = false) {
|
|
451
|
+
// The built-in buttons are disabled meanwhile; a custom one bound to `save`
|
|
452
|
+
// may not be.
|
|
453
|
+
if (isBusy.value) return;
|
|
454
|
+
|
|
431
455
|
const validationResults = formValidators.map((fn) => fn());
|
|
432
456
|
const allValid = validationResults.every((r) => r.valid);
|
|
433
457
|
if (!allValid) {
|
|
@@ -442,7 +466,8 @@ async function save(andBack = false) {
|
|
|
442
466
|
}
|
|
443
467
|
|
|
444
468
|
errors.value = {};
|
|
445
|
-
|
|
469
|
+
pendingAction.value = andBack ? "save" : "saveContinue";
|
|
470
|
+
let leaving = false;
|
|
446
471
|
try {
|
|
447
472
|
const multipartOpts = { multipart: props.multipart };
|
|
448
473
|
let result: T;
|
|
@@ -478,7 +503,7 @@ async function save(andBack = false) {
|
|
|
478
503
|
params: { id },
|
|
479
504
|
});
|
|
480
505
|
}
|
|
481
|
-
if (andBack)
|
|
506
|
+
if (andBack) leaving = leave();
|
|
482
507
|
} catch (err: unknown) {
|
|
483
508
|
const data = getErrorData(err);
|
|
484
509
|
if (getErrorStatus(err) === 400 && data && typeof data === "object") {
|
|
@@ -492,7 +517,8 @@ async function save(andBack = false) {
|
|
|
492
517
|
snack.show(detail ?? t("mapo.saveError"), "error");
|
|
493
518
|
}
|
|
494
519
|
} finally {
|
|
495
|
-
|
|
520
|
+
// Going back: still busy until the page is gone (see leave()).
|
|
521
|
+
if (!leaving) pendingAction.value = null;
|
|
496
522
|
}
|
|
497
523
|
}
|
|
498
524
|
|
|
@@ -504,6 +530,7 @@ function getPatch(): Partial<T> {
|
|
|
504
530
|
// ─── Delete ──────────────────────────────────────────────────────────────────
|
|
505
531
|
|
|
506
532
|
async function deleteItem() {
|
|
533
|
+
if (isBusy.value) return;
|
|
507
534
|
const ok = await confirm.ask({
|
|
508
535
|
title: t("mapo.delete"),
|
|
509
536
|
message: t("mapo.detail.deleteQuestion"),
|
|
@@ -511,24 +538,52 @@ async function deleteItem() {
|
|
|
511
538
|
dangerous: true,
|
|
512
539
|
});
|
|
513
540
|
if (!ok) return;
|
|
514
|
-
|
|
541
|
+
pendingAction.value = "delete";
|
|
542
|
+
let leaving = false;
|
|
515
543
|
try {
|
|
516
544
|
await crud.delete(recordId());
|
|
517
545
|
backup.value = deepClone(model.value) as T; // clear dirty so guard doesn't fire
|
|
518
546
|
snack.show(t("mapo.deleteSuccess"), "success");
|
|
519
547
|
emit("deleted");
|
|
520
|
-
|
|
548
|
+
leaving = leave();
|
|
521
549
|
} catch (err: unknown) {
|
|
522
550
|
snack.show(getErrorDetail(err) ?? t("mapo.deleteError"), "error");
|
|
523
551
|
} finally {
|
|
524
|
-
|
|
552
|
+
if (!leaving) pendingAction.value = null;
|
|
525
553
|
}
|
|
526
554
|
}
|
|
527
555
|
|
|
528
556
|
// ─── Navigation guard ────────────────────────────────────────────────────────
|
|
529
557
|
|
|
530
558
|
function back() {
|
|
559
|
+
if (isBusy.value) return;
|
|
560
|
+
pendingAction.value = "back";
|
|
561
|
+
const leaving = leave();
|
|
562
|
+
if (!leaving) pendingAction.value = null;
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
/**
|
|
566
|
+
* Goes back one step and tells whether that is a navigation inside the app.
|
|
567
|
+
*
|
|
568
|
+
* router.back() does not wait for the navigation, and this page stays on screen
|
|
569
|
+
* until the previous route is ready. So the caller keeps its action pending
|
|
570
|
+
* until the page is gone; if the navigation fails (a guard kept the page, such
|
|
571
|
+
* as the unsaved-changes one), the action is released and the buttons work
|
|
572
|
+
* again.
|
|
573
|
+
*/
|
|
574
|
+
function leave(): boolean {
|
|
575
|
+
// Opened directly (a new tab, a bookmark): no page of the app to go back to,
|
|
576
|
+
// router.back() leaves the app or does nothing, and nothing would release
|
|
577
|
+
// the buttons.
|
|
578
|
+
const hasPreviousPage = router.options.history.state.back != null;
|
|
579
|
+
if (hasPreviousPage) {
|
|
580
|
+
const stopWatching = router.afterEach((_to, _from, failure) => {
|
|
581
|
+
stopWatching();
|
|
582
|
+
if (failure) pendingAction.value = null;
|
|
583
|
+
});
|
|
584
|
+
}
|
|
531
585
|
router.back();
|
|
586
|
+
return hasPreviousPage;
|
|
532
587
|
}
|
|
533
588
|
|
|
534
589
|
async function guardUnsaved(): Promise<boolean> {
|
|
@@ -626,14 +681,19 @@ type SlotBindings = {
|
|
|
626
681
|
currentLang: string;
|
|
627
682
|
isNew: boolean;
|
|
628
683
|
isLoading: boolean;
|
|
684
|
+
/** From the click until the save is done; after `save(true)`, until the page has gone back. */
|
|
629
685
|
isSaving: boolean;
|
|
686
|
+
/** From the confirmation until the delete is done and the page has gone back. */
|
|
630
687
|
isDeleting: boolean;
|
|
631
688
|
isDirty: boolean;
|
|
632
689
|
draftBanner: DraftBannerState;
|
|
633
690
|
readonly: boolean;
|
|
634
691
|
canDelete: boolean;
|
|
692
|
+
/** Ignored while a save, a delete or a navigation back is under way. */
|
|
635
693
|
save: (andBack?: boolean) => Promise<void>;
|
|
694
|
+
/** Ignored while a save, a delete or a navigation back is under way. */
|
|
636
695
|
deleteItem: () => Promise<void>;
|
|
696
|
+
/** Ignored while a save, a delete or a navigation back is under way. */
|
|
637
697
|
back: () => void;
|
|
638
698
|
};
|
|
639
699
|
|
|
@@ -747,6 +807,7 @@ defineSlots<{
|
|
|
747
807
|
v-model="currentLang"
|
|
748
808
|
:langs="activeLangs"
|
|
749
809
|
:errors="errors"
|
|
810
|
+
:lang-threshold="langThreshold"
|
|
750
811
|
/>
|
|
751
812
|
</slot>
|
|
752
813
|
|
|
@@ -779,7 +840,8 @@ defineSlots<{
|
|
|
779
840
|
<!-- Sidebar column -->
|
|
780
841
|
<div :class="sidebarColsClass">
|
|
781
842
|
<div :style="sidebarStyle" class="space-y-4">
|
|
782
|
-
<!-- Action buttons (Save / Save&continue / Back — no delete here)
|
|
843
|
+
<!-- Action buttons (Save / Save&continue / Back — no delete here).
|
|
844
|
+
While one works, it spins and all of them are disabled. -->
|
|
783
845
|
<slot name="side-buttons" v-bind="slotBindings">
|
|
784
846
|
<UCard>
|
|
785
847
|
<div class="flex flex-col gap-2">
|
|
@@ -787,7 +849,8 @@ defineSlots<{
|
|
|
787
849
|
<UButton
|
|
788
850
|
v-if="!effectiveReadonly"
|
|
789
851
|
block
|
|
790
|
-
:loading="
|
|
852
|
+
:loading="pendingAction === 'save'"
|
|
853
|
+
:disabled="isBusy"
|
|
791
854
|
icon="i-lucide-save"
|
|
792
855
|
@click="save(true)"
|
|
793
856
|
>
|
|
@@ -800,7 +863,8 @@ defineSlots<{
|
|
|
800
863
|
v-if="!effectiveReadonly"
|
|
801
864
|
block
|
|
802
865
|
variant="soft"
|
|
803
|
-
:loading="
|
|
866
|
+
:loading="pendingAction === 'saveContinue'"
|
|
867
|
+
:disabled="isBusy"
|
|
804
868
|
icon="i-lucide-save"
|
|
805
869
|
@click="save(false)"
|
|
806
870
|
>
|
|
@@ -815,6 +879,8 @@ defineSlots<{
|
|
|
815
879
|
block
|
|
816
880
|
variant="ghost"
|
|
817
881
|
icon="i-lucide-arrow-left"
|
|
882
|
+
:loading="pendingAction === 'back'"
|
|
883
|
+
:disabled="isBusy"
|
|
818
884
|
@click="back"
|
|
819
885
|
>
|
|
820
886
|
{{ t("mapo.back") }}
|
|
@@ -892,6 +958,7 @@ defineSlots<{
|
|
|
892
958
|
color="error"
|
|
893
959
|
variant="soft"
|
|
894
960
|
:loading="isDeleting"
|
|
961
|
+
:disabled="isBusy"
|
|
895
962
|
icon="i-lucide-trash-2"
|
|
896
963
|
@click="deleteItem"
|
|
897
964
|
>
|
|
@@ -9,8 +9,9 @@ const props = withDefaults(
|
|
|
9
9
|
errors?: Record<string, string[]>;
|
|
10
10
|
noRouteChange?: boolean;
|
|
11
11
|
/**
|
|
12
|
-
* Number of languages above which
|
|
13
|
-
*
|
|
12
|
+
* Number of languages above which a searchable select appears next to the
|
|
13
|
+
* tab bar, for quick navigation. Default: `8`. `0` always shows it,
|
|
14
|
+
* `Infinity` never does.
|
|
14
15
|
*/
|
|
15
16
|
langThreshold?: number;
|
|
16
17
|
}>(),
|
|
@@ -69,6 +70,9 @@ function onSelectChange(v: unknown) {
|
|
|
69
70
|
|
|
70
71
|
// ─── Select menu items ────────────────────────────────────────────────────────
|
|
71
72
|
|
|
73
|
+
// A few languages fit in the tab bar; the select only helps once they overflow it.
|
|
74
|
+
const showSelect = computed(() => props.langs.length > props.langThreshold);
|
|
75
|
+
|
|
72
76
|
const selectItems = computed(() =>
|
|
73
77
|
props.langs.map((lang) => ({
|
|
74
78
|
label: lang.toUpperCase(),
|
|
@@ -106,26 +110,21 @@ watch(
|
|
|
106
110
|
<div
|
|
107
111
|
class="flex items-end gap-0 border-b border-gray-200 dark:border-gray-700 mt-2 mb-4"
|
|
108
112
|
>
|
|
109
|
-
<!-- Language select —
|
|
110
|
-
<
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
:trailing-icon="
|
|
125
|
-
open ? 'i-lucide-chevron-up' : 'i-lucide-chevron-down'
|
|
126
|
-
"
|
|
127
|
-
class="w-full justify-between font-medium"
|
|
128
|
-
>
|
|
113
|
+
<!-- Language select — above langThreshold only, aligned at bottom of row -->
|
|
114
|
+
<template v-if="showSelect">
|
|
115
|
+
<USelectMenu
|
|
116
|
+
:model-value="modelValue"
|
|
117
|
+
:items="selectItems"
|
|
118
|
+
value-key="value"
|
|
119
|
+
label-key="label"
|
|
120
|
+
size="sm"
|
|
121
|
+
variant="ghost"
|
|
122
|
+
class="w-28 shrink-0 mb-px font-medium"
|
|
123
|
+
@update:model-value="onSelectChange"
|
|
124
|
+
>
|
|
125
|
+
<!-- The trigger is already a button with its own chevron: the slot
|
|
126
|
+
holds only what it shows, never another button. -->
|
|
127
|
+
<template #default>
|
|
129
128
|
<span class="flex items-center gap-1">
|
|
130
129
|
<span
|
|
131
130
|
v-if="hasErrors(modelValue)"
|
|
@@ -133,12 +132,12 @@ watch(
|
|
|
133
132
|
/>
|
|
134
133
|
{{ modelValue.toUpperCase() }}
|
|
135
134
|
</span>
|
|
136
|
-
</
|
|
137
|
-
</
|
|
138
|
-
</USelectMenu>
|
|
135
|
+
</template>
|
|
136
|
+
</USelectMenu>
|
|
139
137
|
|
|
140
|
-
|
|
141
|
-
|
|
138
|
+
<!-- Divider -->
|
|
139
|
+
<div class="h-5 w-px bg-gray-200 dark:bg-gray-700 mx-2 shrink-0" />
|
|
140
|
+
</template>
|
|
142
141
|
|
|
143
142
|
<!-- Tab bar — always shown, scrollable -->
|
|
144
143
|
<div class="flex gap-1 overflow-x-auto scrollbar-none flex-1">
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
<script setup lang="ts" generic="T extends object">
|
|
2
|
-
import { ref, computed, watch, useSlots } from "vue";
|
|
2
|
+
import { ref, computed, watch, useSlots, onBeforeUnmount } from "vue";
|
|
3
3
|
import type { Ref } from "vue";
|
|
4
4
|
import type {
|
|
5
5
|
ListColumn,
|
|
@@ -30,6 +30,7 @@ const props = withDefaults(
|
|
|
30
30
|
* Ignored when `items` is provided.
|
|
31
31
|
*/
|
|
32
32
|
clientSide?: boolean;
|
|
33
|
+
/** Column definitions. A dotted `key` (`category.name`) reads a field of a related object. */
|
|
33
34
|
columns: ListColumn<T>[];
|
|
34
35
|
lookup?: string;
|
|
35
36
|
// Filters
|
|
@@ -57,10 +58,14 @@ const props = withDefaults(
|
|
|
57
58
|
*/
|
|
58
59
|
permissionModel?: string;
|
|
59
60
|
// Pagination
|
|
60
|
-
/** Initial page size
|
|
61
|
-
defaultPageSize?: number;
|
|
62
|
-
/**
|
|
63
|
-
|
|
61
|
+
/** Initial page size: a number, or `"all"`. Default: 20. */
|
|
62
|
+
defaultPageSize?: number | "all";
|
|
63
|
+
/**
|
|
64
|
+
* Options of the per-page selector. `"all"` adds an option that requests
|
|
65
|
+
* the list without pagination params, like `clientSide` does: the backend
|
|
66
|
+
* has to return the whole collection then. Default: [10, 20, 50, 100].
|
|
67
|
+
*/
|
|
68
|
+
pageSizeOptions?: (number | "all")[];
|
|
64
69
|
/**
|
|
65
70
|
* Custom response parser. Replaces the built-in DRF / flat-array detection.
|
|
66
71
|
* Return `{ items, total }` from any shape your backend returns.
|
|
@@ -169,6 +174,9 @@ const writeUrlState = debounce(() => {
|
|
|
169
174
|
|
|
170
175
|
watch([activeTab, activeFilters], writeUrlState, { deep: true });
|
|
171
176
|
|
|
177
|
+
// A URL write still pending when the list goes away would land on the next page.
|
|
178
|
+
onBeforeUnmount(() => writeUrlState.cancel());
|
|
179
|
+
|
|
172
180
|
// --- crudEndpoint: base path for mutations (no query string). ---
|
|
173
181
|
const crudEndpoint = computed(
|
|
174
182
|
() => (props.endpoint ?? "").split("?")[0] || (props.endpoint ?? ""),
|
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
h,
|
|
8
8
|
resolveComponent,
|
|
9
9
|
useSlots,
|
|
10
|
+
onBeforeUnmount,
|
|
10
11
|
} from "vue";
|
|
11
12
|
import type { Ref } from "vue";
|
|
12
13
|
import { useI18n } from "vue-i18n";
|
|
@@ -18,8 +19,20 @@ import { useConfirmStore } from "@mapomodule/store/runtime/stores/confirm";
|
|
|
18
19
|
import { usePermissions } from "@mapomodule/store/runtime/composables/usePermissions";
|
|
19
20
|
import { useCrud } from "@mapomodule/core/runtime/api/crud";
|
|
20
21
|
import type { AnyFieldDescriptor, FieldRegistry } from "@mapomodule/form/types";
|
|
21
|
-
import {
|
|
22
|
+
import {
|
|
23
|
+
debounce,
|
|
24
|
+
getNestedValue,
|
|
25
|
+
splitEndpointParams,
|
|
26
|
+
} from "@mapomodule/utils";
|
|
22
27
|
import { useRoute, useRouter } from "#app";
|
|
28
|
+
import {
|
|
29
|
+
ALL_ROWS,
|
|
30
|
+
formatPageSize,
|
|
31
|
+
nextSorting,
|
|
32
|
+
orderingParam,
|
|
33
|
+
parsePageSize,
|
|
34
|
+
toPageSize,
|
|
35
|
+
} from "../utils/listTable.js";
|
|
23
36
|
|
|
24
37
|
const props = withDefaults(
|
|
25
38
|
defineProps<{
|
|
@@ -44,10 +57,14 @@ const props = withDefaults(
|
|
|
44
57
|
* and tab params without baking them into the endpoint string.
|
|
45
58
|
*/
|
|
46
59
|
filterParams?: Record<string, unknown>;
|
|
47
|
-
/** Initial page size
|
|
48
|
-
defaultPageSize?: number;
|
|
49
|
-
/**
|
|
50
|
-
|
|
60
|
+
/** Initial page size: a number, or `"all"`. Default: 20. */
|
|
61
|
+
defaultPageSize?: number | "all";
|
|
62
|
+
/**
|
|
63
|
+
* Options of the per-page selector. `"all"` adds an option that requests
|
|
64
|
+
* the list without pagination params, like `clientSide` does: the backend
|
|
65
|
+
* has to return the whole collection then. Default: [10, 20, 50, 100].
|
|
66
|
+
*/
|
|
67
|
+
pageSizeOptions?: (number | "all")[];
|
|
51
68
|
/**
|
|
52
69
|
* Custom response parser. When provided it replaces the built-in DRF / flat-array
|
|
53
70
|
* detection. Return `{ items, total }` from any shape your backend returns.
|
|
@@ -70,6 +87,7 @@ const props = withDefaults(
|
|
|
70
87
|
* still hit the backend, then re-fetch. Ignored when `items` is provided.
|
|
71
88
|
*/
|
|
72
89
|
clientSide?: boolean;
|
|
90
|
+
/** Column definitions. A dotted `key` (`category.name`) reads a field of a related object. */
|
|
73
91
|
columns: ListColumn<T>[];
|
|
74
92
|
lookup?: string;
|
|
75
93
|
searchable?: boolean;
|
|
@@ -135,10 +153,11 @@ defineSlots<
|
|
|
135
153
|
|
|
136
154
|
const slots = useSlots();
|
|
137
155
|
|
|
138
|
-
//
|
|
139
|
-
//
|
|
140
|
-
//
|
|
141
|
-
const atKey = (item: T, key: string): T[keyof T] =>
|
|
156
|
+
// A key can be a dotted path into a related object (`category.name`). The cast to
|
|
157
|
+
// T[keyof T] gives the call sites the union of the row's property types rather
|
|
158
|
+
// than `unknown`.
|
|
159
|
+
const atKey = (item: T, key: string): T[keyof T] =>
|
|
160
|
+
getNestedValue(item, key) as T[keyof T];
|
|
142
161
|
|
|
143
162
|
const { t } = useI18n();
|
|
144
163
|
const snack = useSnackStore();
|
|
@@ -187,14 +206,24 @@ function toPositiveNumber(value: unknown, fallback: number): number {
|
|
|
187
206
|
}
|
|
188
207
|
|
|
189
208
|
// --- Pagination (restored from URL) ---
|
|
209
|
+
// "All rows" is stored as the ALL_ROWS page size and written as `page_size=all`.
|
|
210
|
+
const defaultPageSize = toPageSize(props.defaultPageSize);
|
|
190
211
|
const pagination = ref<PaginationState>({
|
|
191
212
|
pageIndex: route.query.page
|
|
192
213
|
? Math.max(0, toPositiveNumber(route.query.page, 1) - 1)
|
|
193
214
|
: 0,
|
|
194
215
|
pageSize: route.query.page_size
|
|
195
|
-
?
|
|
196
|
-
:
|
|
216
|
+
? parsePageSize(route.query.page_size, defaultPageSize)
|
|
217
|
+
: defaultPageSize,
|
|
197
218
|
});
|
|
219
|
+
const showsAllRows = computed(() => pagination.value.pageSize === ALL_ROWS);
|
|
220
|
+
const pageSizeItems = computed(() =>
|
|
221
|
+
props.pageSizeOptions.map((option) =>
|
|
222
|
+
option === "all"
|
|
223
|
+
? { label: t("mapo.listTable.allRows"), value: ALL_ROWS }
|
|
224
|
+
: { label: t("mapo.listTable.perPage", { n: option }), value: option },
|
|
225
|
+
),
|
|
226
|
+
);
|
|
198
227
|
|
|
199
228
|
// --- Sorting (restored from URL) ---
|
|
200
229
|
const sorting = ref<SortingState>(
|
|
@@ -226,8 +255,8 @@ const writeUrlState = debounce(() => {
|
|
|
226
255
|
const page = pagination.value.pageIndex + 1;
|
|
227
256
|
if (page > 1) query.page = String(page);
|
|
228
257
|
else delete query.page;
|
|
229
|
-
if (pagination.value.pageSize !==
|
|
230
|
-
query.page_size =
|
|
258
|
+
if (pagination.value.pageSize !== defaultPageSize)
|
|
259
|
+
query.page_size = formatPageSize(pagination.value.pageSize);
|
|
231
260
|
else delete query.page_size;
|
|
232
261
|
if (search.value) query.search = search.value;
|
|
233
262
|
else delete query.search;
|
|
@@ -241,6 +270,13 @@ const writeUrlState = debounce(() => {
|
|
|
241
270
|
|
|
242
271
|
watch([pagination, search, sorting], writeUrlState, { deep: true });
|
|
243
272
|
|
|
273
|
+
// A debounced call still pending when the list goes away would act on the next
|
|
274
|
+
// page: the URL write would put this list's query on it.
|
|
275
|
+
onBeforeUnmount(() => {
|
|
276
|
+
onSearchInput.cancel();
|
|
277
|
+
writeUrlState.cancel();
|
|
278
|
+
});
|
|
279
|
+
|
|
244
280
|
// --- Row selection ---
|
|
245
281
|
// Selection is keyed by primary key (lookup), not by row index, so pagination/sort/refresh
|
|
246
282
|
// changes do not affect which rows are marked as selected.
|
|
@@ -266,11 +302,12 @@ watch(selectedRows, (rows) => {
|
|
|
266
302
|
|
|
267
303
|
function buildSelectionQuery(): URLSearchParams {
|
|
268
304
|
const params = new URLSearchParams();
|
|
269
|
-
|
|
305
|
+
if (!showsAllRows.value)
|
|
306
|
+
params.set("page_size", String(pagination.value.pageSize));
|
|
270
307
|
if (search.value) params.set("search", search.value);
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
308
|
+
// The same ordering the list request sends.
|
|
309
|
+
if (sorting.value.length)
|
|
310
|
+
params.set("ordering", orderingParam(sorting.value));
|
|
274
311
|
return params;
|
|
275
312
|
}
|
|
276
313
|
|
|
@@ -476,7 +513,7 @@ function applyOffline(source: T[]) {
|
|
|
476
513
|
const q = search.value.toLowerCase();
|
|
477
514
|
arr = arr.filter((row) =>
|
|
478
515
|
props.columns.some((c) =>
|
|
479
|
-
String(row
|
|
516
|
+
String(atKey(row, c.key) ?? "")
|
|
480
517
|
.toLowerCase()
|
|
481
518
|
.includes(q),
|
|
482
519
|
),
|
|
@@ -589,13 +626,11 @@ async function refresh() {
|
|
|
589
626
|
const params: Record<string, unknown> = {
|
|
590
627
|
...endpointParams,
|
|
591
628
|
...(props.filterParams ?? {}),
|
|
592
|
-
|
|
629
|
+
// "All rows" sends no pagination params: the backend returns the whole collection.
|
|
630
|
+
...(showsAllRows.value ? {} : paginationP),
|
|
593
631
|
};
|
|
594
632
|
if (search.value) params.search = search.value;
|
|
595
|
-
if (sorting.value.length)
|
|
596
|
-
params.ordering = sorting.value
|
|
597
|
-
.map((s) => `${s.desc ? "-" : ""}${s.id}`)
|
|
598
|
-
.join(",");
|
|
633
|
+
if (sorting.value.length) params.ordering = orderingParam(sorting.value);
|
|
599
634
|
|
|
600
635
|
const res = await crud.list(params as Record<string, string>);
|
|
601
636
|
|
|
@@ -657,13 +692,23 @@ watch(
|
|
|
657
692
|
search,
|
|
658
693
|
],
|
|
659
694
|
([newEndpoint, , newFilters], [oldEndpoint, , oldFilters]) => {
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
} else {
|
|
695
|
+
// The immediate first run has no old values: it is the initial load, so it
|
|
696
|
+
// fetches the page restored from the URL.
|
|
697
|
+
const isFirstRun = oldFilters === undefined;
|
|
698
|
+
if (isFirstRun) {
|
|
665
699
|
refresh();
|
|
700
|
+
return;
|
|
666
701
|
}
|
|
702
|
+
const endpointChanged = newEndpoint !== oldEndpoint;
|
|
703
|
+
const filtersChanged = newFilters !== oldFilters;
|
|
704
|
+
const contextChanged = endpointChanged || filtersChanged;
|
|
705
|
+
const isOnFirstPage = pagination.value.pageIndex === 0;
|
|
706
|
+
// Moving to the first page runs this watcher again, and that run fetches.
|
|
707
|
+
if (contextChanged && !isOnFirstPage) {
|
|
708
|
+
pagination.value = { ...pagination.value, pageIndex: 0 };
|
|
709
|
+
return;
|
|
710
|
+
}
|
|
711
|
+
refresh();
|
|
667
712
|
},
|
|
668
713
|
{ immediate: true, deep: true },
|
|
669
714
|
);
|
|
@@ -673,6 +718,35 @@ const UCheckbox = resolveComponent("UCheckbox");
|
|
|
673
718
|
const UButton = resolveComponent("UButton");
|
|
674
719
|
const UIcon = resolveComponent("UIcon");
|
|
675
720
|
|
|
721
|
+
// A sortable header is a button: the icon shows the current direction, the
|
|
722
|
+
// accessible name says what the next click does (UTable sets no `aria-sort`).
|
|
723
|
+
const SORT_ICONS = {
|
|
724
|
+
none: "i-lucide-arrow-up-down",
|
|
725
|
+
asc: "i-lucide-arrow-up-narrow-wide",
|
|
726
|
+
desc: "i-lucide-arrow-down-wide-narrow",
|
|
727
|
+
};
|
|
728
|
+
const SORT_NEXT_ACTION = {
|
|
729
|
+
none: "mapo.listTable.sortAscending",
|
|
730
|
+
asc: "mapo.listTable.sortDescending",
|
|
731
|
+
desc: "mapo.listTable.clearSorting",
|
|
732
|
+
};
|
|
733
|
+
|
|
734
|
+
function sortHeader(col: ListColumn<T>) {
|
|
735
|
+
const sort = sorting.value.find((s) => s.id === col.key);
|
|
736
|
+
const direction = !sort ? "none" : sort.desc ? "desc" : "asc";
|
|
737
|
+
return h(UButton, {
|
|
738
|
+
color: "neutral",
|
|
739
|
+
variant: "ghost",
|
|
740
|
+
label: col.label,
|
|
741
|
+
icon: SORT_ICONS[direction],
|
|
742
|
+
class: "-mx-2.5",
|
|
743
|
+
"aria-label": t(SORT_NEXT_ACTION[direction], { column: col.label }),
|
|
744
|
+
onClick: () => {
|
|
745
|
+
sorting.value = nextSorting(sorting.value, col.key);
|
|
746
|
+
},
|
|
747
|
+
});
|
|
748
|
+
}
|
|
749
|
+
|
|
676
750
|
const tableColumns = computed<TableColumn<T>[]>(() => {
|
|
677
751
|
const cols: TableColumn<T>[] = [];
|
|
678
752
|
|
|
@@ -731,8 +805,11 @@ const tableColumns = computed<TableColumn<T>[]>(() => {
|
|
|
731
805
|
const col = props.columns[i]!;
|
|
732
806
|
const isFirst = i === 0 && !!props.detailBase;
|
|
733
807
|
cols.push({
|
|
808
|
+
// TanStack would turn a dotted key into `category_name`; the sorting
|
|
809
|
+
// state uses the column key, so the id must be the key itself.
|
|
810
|
+
id: col.key,
|
|
734
811
|
accessorKey: col.key,
|
|
735
|
-
header: col.label,
|
|
812
|
+
header: col.sortable ? () => sortHeader(col) : col.label,
|
|
736
813
|
cell: ({ row }: { row: { original: T } }) => {
|
|
737
814
|
const cellSlot = slots[`cell.${col.key}`];
|
|
738
815
|
if (cellSlot)
|
|
@@ -770,6 +847,7 @@ const tableColumns = computed<TableColumn<T>[]>(() => {
|
|
|
770
847
|
variant: "ghost",
|
|
771
848
|
color: "neutral",
|
|
772
849
|
icon: "i-lucide-pencil",
|
|
850
|
+
"aria-label": t("mapo.edit"),
|
|
773
851
|
onClick: () =>
|
|
774
852
|
openQuickEdit(
|
|
775
853
|
atKey(row.original, props.lookup) as string | number,
|
|
@@ -784,6 +862,7 @@ const tableColumns = computed<TableColumn<T>[]>(() => {
|
|
|
784
862
|
variant: "ghost",
|
|
785
863
|
color: "error",
|
|
786
864
|
icon: "i-lucide-trash-2",
|
|
865
|
+
"aria-label": t("mapo.delete"),
|
|
787
866
|
onClick: () => deleteRow(row.original),
|
|
788
867
|
}),
|
|
789
868
|
);
|
|
@@ -800,6 +879,19 @@ const tableColumns = computed<TableColumn<T>[]>(() => {
|
|
|
800
879
|
return cols;
|
|
801
880
|
});
|
|
802
881
|
|
|
882
|
+
// A search or a filter with no match reads differently from an empty list.
|
|
883
|
+
const emptyText = computed(() => {
|
|
884
|
+
const isSearching = !!search.value;
|
|
885
|
+
const isFiltering = Object.values(props.filterParams ?? {}).some(
|
|
886
|
+
(value) => value != null && value !== "",
|
|
887
|
+
);
|
|
888
|
+
return t(
|
|
889
|
+
isSearching || isFiltering
|
|
890
|
+
? "mapo.listTable.noResults"
|
|
891
|
+
: "mapo.listTable.noItems",
|
|
892
|
+
);
|
|
893
|
+
});
|
|
894
|
+
|
|
803
895
|
// --- Pagination for UTable ---
|
|
804
896
|
const paginationOptions = computed(() => ({
|
|
805
897
|
manualPagination: true,
|
|
@@ -841,7 +933,7 @@ const sortingOptions = computed(() => ({
|
|
|
841
933
|
:pagination-options="paginationOptions"
|
|
842
934
|
:sorting="sorting"
|
|
843
935
|
:sorting-options="sortingOptions"
|
|
844
|
-
:empty="
|
|
936
|
+
:empty="emptyText"
|
|
845
937
|
@update:pagination="pagination = $event ?? pagination"
|
|
846
938
|
@update:sorting="sorting = $event ?? sorting"
|
|
847
939
|
>
|
|
@@ -849,7 +941,7 @@ const sortingOptions = computed(() => ({
|
|
|
849
941
|
<slot name="dtable.empty">
|
|
850
942
|
<div class="flex flex-col items-center gap-2 py-8 text-muted">
|
|
851
943
|
<UIcon name="i-lucide-inbox" class="h-8 w-8" />
|
|
852
|
-
<span class="text-sm">{{
|
|
944
|
+
<span class="text-sm">{{ emptyText }}</span>
|
|
853
945
|
</div>
|
|
854
946
|
</slot>
|
|
855
947
|
</template>
|
|
@@ -870,23 +962,18 @@ const sortingOptions = computed(() => ({
|
|
|
870
962
|
<!-- Pagination controls -->
|
|
871
963
|
<div class="flex items-center justify-between text-sm text-muted">
|
|
872
964
|
<span>{{ t("mapo.listTable.totalItems", { total }) }}</span>
|
|
965
|
+
<!-- Nuxt UI 4's UPagination models `page`: with `modelValue` it runs uncontrolled. -->
|
|
873
966
|
<UPagination
|
|
874
|
-
|
|
967
|
+
v-if="!showsAllRows"
|
|
968
|
+
:page="pagination.pageIndex + 1"
|
|
875
969
|
:total="total"
|
|
876
970
|
:items-per-page="pagination.pageSize"
|
|
877
971
|
size="sm"
|
|
878
|
-
@update:
|
|
879
|
-
pagination = { ...pagination, pageIndex: $event - 1 }
|
|
880
|
-
"
|
|
972
|
+
@update:page="pagination = { ...pagination, pageIndex: $event - 1 }"
|
|
881
973
|
/>
|
|
882
974
|
<USelect
|
|
883
975
|
:model-value="pagination.pageSize"
|
|
884
|
-
:items="
|
|
885
|
-
pageSizeOptions.map((n) => ({
|
|
886
|
-
label: t('mapo.listTable.perPage', { n }),
|
|
887
|
-
value: n,
|
|
888
|
-
}))
|
|
889
|
-
"
|
|
976
|
+
:items="pageSizeItems"
|
|
890
977
|
size="xs"
|
|
891
978
|
class="w-28"
|
|
892
979
|
@update:model-value="
|