@mapomodule/uikit 2.0.0-beta.1 → 2.0.0-beta.3
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/dist/index.d.mts +3 -2
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -0
- package/dist/index.mjs +0 -17
- package/dist/module.d.mts +30 -2
- package/dist/module.d.ts +27 -2
- package/dist/module.d.ts.map +1 -0
- package/dist/module.json +1 -1
- package/dist/module.mjs +109 -13
- package/dist/runtime/assets/main.css +1 -0
- package/dist/runtime/components/MapoConfirmDialog.vue +33 -0
- package/dist/runtime/components/MapoConfirmDialog.vue.d.ts +4 -0
- package/dist/runtime/components/MapoConfirmDialog.vue.d.ts.map +1 -0
- package/dist/runtime/components/MapoDetail.vue +598 -0
- package/dist/runtime/components/MapoDetail.vue.d.ts +401 -0
- package/dist/runtime/components/MapoDetail.vue.d.ts.map +1 -0
- package/dist/runtime/components/MapoDetailLangSwitch.vue +126 -0
- package/dist/runtime/components/MapoDetailLangSwitch.vue.d.ts +23 -0
- package/dist/runtime/components/MapoDetailLangSwitch.vue.d.ts.map +1 -0
- package/dist/runtime/components/MapoList.vue +191 -0
- package/dist/runtime/components/MapoList.vue.d.ts +111 -0
- package/dist/runtime/components/MapoList.vue.d.ts.map +1 -0
- package/dist/runtime/components/MapoListActions.vue +112 -0
- package/dist/runtime/components/MapoListActions.vue.d.ts +33 -0
- package/dist/runtime/components/MapoListActions.vue.d.ts.map +1 -0
- package/dist/runtime/components/MapoListFilters.vue +244 -0
- package/dist/runtime/components/MapoListFilters.vue.d.ts +38 -0
- package/dist/runtime/components/MapoListFilters.vue.d.ts.map +1 -0
- package/dist/runtime/components/MapoListHead.vue +9 -0
- package/dist/runtime/components/MapoListHead.vue.d.ts +14 -0
- package/dist/runtime/components/MapoListHead.vue.d.ts.map +1 -0
- package/dist/runtime/components/MapoListQuickEdit.vue +119 -0
- package/dist/runtime/components/MapoListQuickEdit.vue.d.ts +47 -0
- package/dist/runtime/components/MapoListQuickEdit.vue.d.ts.map +1 -0
- package/dist/runtime/components/MapoListTable.vue +660 -0
- package/dist/runtime/components/MapoListTable.vue.d.ts +105 -0
- package/dist/runtime/components/MapoListTable.vue.d.ts.map +1 -0
- package/dist/runtime/components/MapoListTabs.vue +38 -0
- package/dist/runtime/components/MapoListTabs.vue.d.ts +26 -0
- package/dist/runtime/components/MapoListTabs.vue.d.ts.map +1 -0
- package/dist/runtime/components/MapoLogin.vue +193 -0
- package/dist/runtime/components/MapoLogin.vue.d.ts +22 -0
- package/dist/runtime/components/MapoLogin.vue.d.ts.map +1 -0
- package/dist/runtime/components/MapoLogoutButton.vue +25 -0
- package/dist/runtime/components/MapoLogoutButton.vue.d.ts +29 -0
- package/dist/runtime/components/MapoLogoutButton.vue.d.ts.map +1 -0
- package/dist/runtime/components/MapoRootComponents.vue +9 -0
- package/dist/runtime/components/MapoRootComponents.vue.d.ts +4 -0
- package/dist/runtime/components/MapoRootComponents.vue.d.ts.map +1 -0
- package/dist/runtime/components/MapoSidebar.vue +87 -0
- package/dist/runtime/components/MapoSidebar.vue.d.ts +22 -0
- package/dist/runtime/components/MapoSidebar.vue.d.ts.map +1 -0
- package/dist/runtime/components/MapoSidebarList.vue +27 -0
- package/dist/runtime/components/MapoSidebarList.vue.d.ts +8 -0
- package/dist/runtime/components/MapoSidebarList.vue.d.ts.map +1 -0
- package/dist/runtime/components/MapoSidebarListItem.vue +74 -0
- package/dist/runtime/components/MapoSidebarListItem.vue.d.ts +9 -0
- package/dist/runtime/components/MapoSidebarListItem.vue.d.ts.map +1 -0
- package/dist/runtime/components/MapoSidebarProfile.vue +51 -0
- package/dist/runtime/components/MapoSidebarProfile.vue.d.ts +10 -0
- package/dist/runtime/components/MapoSidebarProfile.vue.d.ts.map +1 -0
- package/dist/runtime/components/MapoSnackBar.vue +38 -0
- package/dist/runtime/components/MapoSnackBar.vue.d.ts +4 -0
- package/dist/runtime/components/MapoSnackBar.vue.d.ts.map +1 -0
- package/dist/runtime/components/MapoThemeToggle.vue +19 -0
- package/dist/runtime/components/MapoThemeToggle.vue.d.ts +4 -0
- package/dist/runtime/components/MapoThemeToggle.vue.d.ts.map +1 -0
- package/dist/runtime/components/MapoTopbar.vue +30 -0
- package/dist/runtime/components/MapoTopbar.vue.d.ts +15 -0
- package/dist/runtime/components/MapoTopbar.vue.d.ts.map +1 -0
- package/dist/runtime/layouts/default.vue +43 -0
- package/dist/runtime/layouts/default.vue.d.ts +20 -0
- package/dist/runtime/layouts/default.vue.d.ts.map +1 -0
- package/dist/runtime/layouts/empty.vue +9 -0
- package/dist/runtime/layouts/empty.vue.d.ts +14 -0
- package/dist/runtime/layouts/empty.vue.d.ts.map +1 -0
- package/dist/runtime/page-meta.nuxt.d.ts +45 -0
- package/dist/runtime/pages/index.vue +13 -0
- package/dist/runtime/pages/index.vue.d.ts +4 -0
- package/dist/runtime/pages/index.vue.d.ts.map +1 -0
- package/dist/runtime/pages/login.vue +7 -0
- package/dist/runtime/pages/login.vue.d.ts +4 -0
- package/dist/runtime/pages/login.vue.d.ts.map +1 -0
- package/dist/runtime/types/list.d.ts +59 -0
- package/dist/runtime/types/list.d.ts.map +1 -0
- package/dist/runtime/types/list.js +0 -0
- package/dist/types.d.mts +6 -10
- package/package.json +6 -6
- package/dist/index.d.cts +0 -2
- package/dist/module.d.cts +0 -2
|
@@ -0,0 +1,660 @@
|
|
|
1
|
+
<script setup>
|
|
2
|
+
import {
|
|
3
|
+
ref,
|
|
4
|
+
shallowRef,
|
|
5
|
+
computed,
|
|
6
|
+
watch,
|
|
7
|
+
h,
|
|
8
|
+
resolveComponent,
|
|
9
|
+
useSlots
|
|
10
|
+
} from "vue";
|
|
11
|
+
import { useSnackStore } from "@mapomodule/store/runtime/stores/snack";
|
|
12
|
+
import { useConfirmStore } from "@mapomodule/store/runtime/stores/confirm";
|
|
13
|
+
import { usePermissions } from "@mapomodule/store/runtime/composables/usePermissions";
|
|
14
|
+
import { useCrud } from "@mapomodule/core/runtime/api/crud";
|
|
15
|
+
import { debounce, splitEndpointParams } from "@mapomodule/utils";
|
|
16
|
+
import { useRoute, useRouter } from "vue-router";
|
|
17
|
+
const props = defineProps({
|
|
18
|
+
endpoint: { type: String, required: false, default: "" },
|
|
19
|
+
items: { type: Array, required: false, default: void 0 },
|
|
20
|
+
crudEndpoint: { type: String, required: false },
|
|
21
|
+
filterParams: { type: Object, required: false },
|
|
22
|
+
defaultPageSize: { type: Number, required: false, default: 20 },
|
|
23
|
+
pageSizeOptions: { type: Array, required: false, default: () => [10, 20, 50, 100] },
|
|
24
|
+
responseAdapter: { type: Function, required: false },
|
|
25
|
+
paginationParams: { type: Function, required: false },
|
|
26
|
+
clientSide: { type: Boolean, required: false, default: false },
|
|
27
|
+
columns: { type: Array, required: true },
|
|
28
|
+
lookup: { type: String, required: false, default: "id" },
|
|
29
|
+
searchable: { type: Boolean, required: false, default: true },
|
|
30
|
+
draggable: { type: Boolean, required: false, default: false },
|
|
31
|
+
positionField: { type: String, required: false, default: "position" },
|
|
32
|
+
editFields: { type: Array, required: false, default: () => [] },
|
|
33
|
+
languages: { type: Array, required: false },
|
|
34
|
+
registry: { type: null, required: false },
|
|
35
|
+
detailBase: { type: String, required: false },
|
|
36
|
+
permissionModel: { type: String, required: false }
|
|
37
|
+
});
|
|
38
|
+
const offlineMode = computed(() => Array.isArray(props.items));
|
|
39
|
+
const hybridMode = computed(() => !offlineMode.value && props.clientSide);
|
|
40
|
+
const emit = defineEmits(["update:selection", "update:selectionQuery", "update:items"]);
|
|
41
|
+
defineExpose({ refresh });
|
|
42
|
+
defineSlots();
|
|
43
|
+
const slots = useSlots();
|
|
44
|
+
const atKey = (item, key) => item[key];
|
|
45
|
+
const snack = useSnackStore();
|
|
46
|
+
const confirm = useConfirmStore();
|
|
47
|
+
const route = useRoute();
|
|
48
|
+
const router = useRouter();
|
|
49
|
+
const crudBase = props.crudEndpoint ?? (splitEndpointParams(props.endpoint).path || props.endpoint);
|
|
50
|
+
const crud = useCrud(crudBase);
|
|
51
|
+
const { canChange, canDelete: canDeletePerm } = usePermissions();
|
|
52
|
+
const canEditRow = computed(
|
|
53
|
+
() => !props.permissionModel || canChange(props.permissionModel)
|
|
54
|
+
);
|
|
55
|
+
const canDeleteRow = computed(
|
|
56
|
+
() => !props.permissionModel || canDeletePerm(props.permissionModel)
|
|
57
|
+
);
|
|
58
|
+
const rows = shallowRef([]);
|
|
59
|
+
const fullDataset = shallowRef([]);
|
|
60
|
+
const hybridLoaded = ref(false);
|
|
61
|
+
const total = ref(0);
|
|
62
|
+
const loading = ref(false);
|
|
63
|
+
function toPositiveNumber(value, fallback) {
|
|
64
|
+
const n = Number(value);
|
|
65
|
+
return Number.isFinite(n) && n > 0 ? n : fallback;
|
|
66
|
+
}
|
|
67
|
+
const pagination = ref({
|
|
68
|
+
pageIndex: route.query.page ? Math.max(0, toPositiveNumber(route.query.page, 1) - 1) : 0,
|
|
69
|
+
pageSize: route.query.page_size ? toPositiveNumber(route.query.page_size, props.defaultPageSize) : props.defaultPageSize
|
|
70
|
+
});
|
|
71
|
+
const sorting = ref(
|
|
72
|
+
route.query.ordering ? String(route.query.ordering).split(",").map((o) => ({
|
|
73
|
+
id: o.startsWith("-") ? o.slice(1) : o,
|
|
74
|
+
desc: o.startsWith("-")
|
|
75
|
+
})) : []
|
|
76
|
+
);
|
|
77
|
+
const search = ref(route.query.search ? String(route.query.search) : "");
|
|
78
|
+
const onSearchInput = debounce((val) => {
|
|
79
|
+
search.value = String(val ?? "");
|
|
80
|
+
pagination.value = { ...pagination.value, pageIndex: 0 };
|
|
81
|
+
}, 350);
|
|
82
|
+
const writeUrlState = debounce(() => {
|
|
83
|
+
const query = {
|
|
84
|
+
...route.query
|
|
85
|
+
};
|
|
86
|
+
const page = pagination.value.pageIndex + 1;
|
|
87
|
+
if (page > 1) query.page = String(page);
|
|
88
|
+
else delete query.page;
|
|
89
|
+
if (pagination.value.pageSize !== props.defaultPageSize)
|
|
90
|
+
query.page_size = String(pagination.value.pageSize);
|
|
91
|
+
else delete query.page_size;
|
|
92
|
+
if (search.value) query.search = search.value;
|
|
93
|
+
else delete query.search;
|
|
94
|
+
if (sorting.value.length)
|
|
95
|
+
query.ordering = sorting.value.map((s) => `${s.desc ? "-" : ""}${s.id}`).join(",");
|
|
96
|
+
else delete query.ordering;
|
|
97
|
+
router.replace({ query });
|
|
98
|
+
}, 150);
|
|
99
|
+
watch([pagination, search, sorting], writeUrlState, { deep: true });
|
|
100
|
+
const rowSelection = ref({});
|
|
101
|
+
const pkOf = (row) => String(atKey(row, props.lookup));
|
|
102
|
+
const isAllSelected = computed(
|
|
103
|
+
() => rows.value.length > 0 && rows.value.every((r) => rowSelection.value[pkOf(r)])
|
|
104
|
+
);
|
|
105
|
+
const isIndeterminate = computed(() => {
|
|
106
|
+
const some = rows.value.some((r) => rowSelection.value[pkOf(r)]);
|
|
107
|
+
return some && !isAllSelected.value;
|
|
108
|
+
});
|
|
109
|
+
const selectedRows = computed(
|
|
110
|
+
() => rows.value.filter((r) => rowSelection.value[pkOf(r)])
|
|
111
|
+
);
|
|
112
|
+
watch(selectedRows, (rows2) => {
|
|
113
|
+
emit("update:selection", rows2);
|
|
114
|
+
emit("update:selectionQuery", buildSelectionQuery());
|
|
115
|
+
});
|
|
116
|
+
function buildSelectionQuery() {
|
|
117
|
+
const params = new URLSearchParams();
|
|
118
|
+
params.set("page_size", String(pagination.value.pageSize));
|
|
119
|
+
if (search.value) params.set("search", search.value);
|
|
120
|
+
sorting.value.forEach(
|
|
121
|
+
(s) => params.set("ordering", `${s.desc ? "-" : ""}${s.id}`)
|
|
122
|
+
);
|
|
123
|
+
return params;
|
|
124
|
+
}
|
|
125
|
+
function toggleAll() {
|
|
126
|
+
if (isAllSelected.value) {
|
|
127
|
+
const next = { ...rowSelection.value };
|
|
128
|
+
for (const r of rows.value) delete next[pkOf(r)];
|
|
129
|
+
rowSelection.value = next;
|
|
130
|
+
} else {
|
|
131
|
+
const next = { ...rowSelection.value };
|
|
132
|
+
for (const r of rows.value) next[pkOf(r)] = true;
|
|
133
|
+
rowSelection.value = next;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
function toggleRowByKey(key) {
|
|
137
|
+
const next = { ...rowSelection.value };
|
|
138
|
+
if (next[key]) delete next[key];
|
|
139
|
+
else next[key] = true;
|
|
140
|
+
rowSelection.value = next;
|
|
141
|
+
}
|
|
142
|
+
const quickEditOpen = ref(false);
|
|
143
|
+
const quickEditId = ref(null);
|
|
144
|
+
function openQuickEdit(id) {
|
|
145
|
+
quickEditId.value = id;
|
|
146
|
+
quickEditOpen.value = true;
|
|
147
|
+
}
|
|
148
|
+
function onQuickEditSaved(updated) {
|
|
149
|
+
if (offlineMode.value) {
|
|
150
|
+
const lk = props.lookup;
|
|
151
|
+
const next = (props.items ?? []).map(
|
|
152
|
+
(r) => String(atKey(r, lk)) === String(atKey(updated, lk)) ? updated : r
|
|
153
|
+
);
|
|
154
|
+
emit("update:items", next);
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
if (hybridMode.value) hybridLoaded.value = false;
|
|
158
|
+
refresh();
|
|
159
|
+
}
|
|
160
|
+
const quickEditLocalItem = computed(() => {
|
|
161
|
+
if (!offlineMode.value || quickEditId.value == null) return null;
|
|
162
|
+
return (props.items ?? []).find(
|
|
163
|
+
(r) => String(atKey(r, props.lookup)) === String(quickEditId.value)
|
|
164
|
+
) ?? null;
|
|
165
|
+
});
|
|
166
|
+
async function deleteRow(item) {
|
|
167
|
+
const ok = await confirm.ask({
|
|
168
|
+
title: "Delete item",
|
|
169
|
+
message: "Are you sure you want to delete this item?",
|
|
170
|
+
confirmText: "Delete",
|
|
171
|
+
dangerous: true
|
|
172
|
+
});
|
|
173
|
+
if (!ok) return;
|
|
174
|
+
if (offlineMode.value) {
|
|
175
|
+
const next = (props.items ?? []).filter(
|
|
176
|
+
(r) => String(atKey(r, props.lookup)) !== String(atKey(item, props.lookup))
|
|
177
|
+
);
|
|
178
|
+
emit("update:items", next);
|
|
179
|
+
snack.show("Item deleted", "success");
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
try {
|
|
183
|
+
await crud.delete(atKey(item, props.lookup));
|
|
184
|
+
snack.show("Item deleted", "success");
|
|
185
|
+
if (hybridMode.value) hybridLoaded.value = false;
|
|
186
|
+
refresh();
|
|
187
|
+
} catch {
|
|
188
|
+
snack.show("Failed to delete item", "error");
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
const canReorder = computed(
|
|
192
|
+
() => props.draggable && !search.value && sorting.value.length === 0
|
|
193
|
+
);
|
|
194
|
+
const tableWrapper = ref(null);
|
|
195
|
+
const dragFrom = ref(null);
|
|
196
|
+
const dragOverIndex = ref(null);
|
|
197
|
+
function rowElements() {
|
|
198
|
+
return tableWrapper.value ? Array.from(tableWrapper.value.querySelectorAll("tbody tr")) : [];
|
|
199
|
+
}
|
|
200
|
+
function clearRowClasses() {
|
|
201
|
+
for (const el of rowElements())
|
|
202
|
+
el.classList.remove(
|
|
203
|
+
"mapo-drop-before",
|
|
204
|
+
"mapo-drop-after",
|
|
205
|
+
"mapo-drag-source"
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
function clearDragState() {
|
|
209
|
+
clearRowClasses();
|
|
210
|
+
dragFrom.value = null;
|
|
211
|
+
dragOverIndex.value = null;
|
|
212
|
+
}
|
|
213
|
+
function onDragStart(idx) {
|
|
214
|
+
if (!canReorder.value) return;
|
|
215
|
+
dragFrom.value = idx;
|
|
216
|
+
}
|
|
217
|
+
function onRowDragOver(e) {
|
|
218
|
+
if (dragFrom.value === null) return;
|
|
219
|
+
e.preventDefault();
|
|
220
|
+
const tr = e.target?.closest(
|
|
221
|
+
"tbody tr"
|
|
222
|
+
);
|
|
223
|
+
if (!tr) return;
|
|
224
|
+
const els = rowElements();
|
|
225
|
+
const idx = els.indexOf(tr);
|
|
226
|
+
if (idx < 0) return;
|
|
227
|
+
const rect = tr.getBoundingClientRect();
|
|
228
|
+
const before = e.clientY < rect.top + rect.height / 2;
|
|
229
|
+
clearRowClasses();
|
|
230
|
+
tr.classList.add(before ? "mapo-drop-before" : "mapo-drop-after");
|
|
231
|
+
els[dragFrom.value]?.classList.add("mapo-drag-source");
|
|
232
|
+
dragOverIndex.value = idx;
|
|
233
|
+
}
|
|
234
|
+
function onRowDrop(e) {
|
|
235
|
+
e.preventDefault();
|
|
236
|
+
const from = dragFrom.value;
|
|
237
|
+
const to = dragOverIndex.value;
|
|
238
|
+
clearDragState();
|
|
239
|
+
if (from !== null && to !== null) void onDrop(from, to);
|
|
240
|
+
}
|
|
241
|
+
function onDragEnd() {
|
|
242
|
+
clearDragState();
|
|
243
|
+
}
|
|
244
|
+
async function onDrop(fromIdx, toIdx) {
|
|
245
|
+
if (fromIdx === toIdx) return;
|
|
246
|
+
const arr = [...rows.value];
|
|
247
|
+
const moved = arr[fromIdx];
|
|
248
|
+
const target = arr[toIdx];
|
|
249
|
+
arr.splice(fromIdx, 1);
|
|
250
|
+
arr.splice(toIdx, 0, moved);
|
|
251
|
+
rows.value = arr;
|
|
252
|
+
if (offlineMode.value) {
|
|
253
|
+
const lk = props.lookup;
|
|
254
|
+
const full = [...props.items ?? []];
|
|
255
|
+
const fIdx = full.findIndex(
|
|
256
|
+
(r) => String(atKey(r, lk)) === String(atKey(moved, lk))
|
|
257
|
+
);
|
|
258
|
+
const tIdx = full.findIndex(
|
|
259
|
+
(r) => String(atKey(r, lk)) === String(atKey(target, lk))
|
|
260
|
+
);
|
|
261
|
+
if (fIdx >= 0 && tIdx >= 0) {
|
|
262
|
+
const [m] = full.splice(fIdx, 1);
|
|
263
|
+
full.splice(tIdx, 0, m);
|
|
264
|
+
emit("update:items", full);
|
|
265
|
+
}
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
try {
|
|
269
|
+
await crud.updateOrder(
|
|
270
|
+
atKey(moved, props.lookup),
|
|
271
|
+
atKey(target, props.lookup)
|
|
272
|
+
);
|
|
273
|
+
snack.show("Order saved", "success");
|
|
274
|
+
if (hybridMode.value) {
|
|
275
|
+
hybridLoaded.value = false;
|
|
276
|
+
refresh();
|
|
277
|
+
}
|
|
278
|
+
} catch {
|
|
279
|
+
snack.show("Failed to save order", "error");
|
|
280
|
+
refresh();
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
function applyOffline(source) {
|
|
284
|
+
let arr = [...source];
|
|
285
|
+
if (search.value) {
|
|
286
|
+
const q = search.value.toLowerCase();
|
|
287
|
+
arr = arr.filter(
|
|
288
|
+
(row) => props.columns.some(
|
|
289
|
+
(c) => String(row[c.key] ?? "").toLowerCase().includes(q)
|
|
290
|
+
)
|
|
291
|
+
);
|
|
292
|
+
}
|
|
293
|
+
for (const [key, rawVal] of Object.entries(props.filterParams ?? {})) {
|
|
294
|
+
if (rawVal == null || rawVal === "") continue;
|
|
295
|
+
const parts = String(rawVal).split(",");
|
|
296
|
+
const isDateRange = parts.length === 2 && parts.every((p) => !Number.isNaN(Date.parse(p)));
|
|
297
|
+
if (isDateRange) {
|
|
298
|
+
const [start2, end] = parts;
|
|
299
|
+
arr = arr.filter((row) => {
|
|
300
|
+
const v = atKey(row, key);
|
|
301
|
+
if (v == null) return false;
|
|
302
|
+
const s = String(v);
|
|
303
|
+
return s >= start2 && s <= end;
|
|
304
|
+
});
|
|
305
|
+
} else if (parts.length > 1) {
|
|
306
|
+
const set = new Set(parts);
|
|
307
|
+
arr = arr.filter((row) => set.has(String(atKey(row, key))));
|
|
308
|
+
} else {
|
|
309
|
+
arr = arr.filter((row) => String(atKey(row, key)) === parts[0]);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
if (sorting.value.length) {
|
|
313
|
+
const sortDefs = sorting.value;
|
|
314
|
+
arr.sort((a, b) => {
|
|
315
|
+
for (const s of sortDefs) {
|
|
316
|
+
const av = atKey(a, s.id);
|
|
317
|
+
const bv = atKey(b, s.id);
|
|
318
|
+
if (av == null && bv == null) continue;
|
|
319
|
+
if (av == null) return 1;
|
|
320
|
+
if (bv == null) return -1;
|
|
321
|
+
if (av < bv) return s.desc ? 1 : -1;
|
|
322
|
+
if (av > bv) return s.desc ? -1 : 1;
|
|
323
|
+
}
|
|
324
|
+
return 0;
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
total.value = arr.length;
|
|
328
|
+
const start = pagination.value.pageIndex * pagination.value.pageSize;
|
|
329
|
+
if (arr.length > 0 && start >= arr.length) {
|
|
330
|
+
pagination.value = { ...pagination.value, pageIndex: 0 };
|
|
331
|
+
rows.value = arr.slice(0, pagination.value.pageSize);
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
rows.value = arr.slice(start, start + pagination.value.pageSize);
|
|
335
|
+
}
|
|
336
|
+
async function fetchAll() {
|
|
337
|
+
loading.value = true;
|
|
338
|
+
try {
|
|
339
|
+
const endpointParams = splitEndpointParams(props.endpoint).params;
|
|
340
|
+
const res = await crud.list(endpointParams);
|
|
341
|
+
let nextItems = [];
|
|
342
|
+
if (props.responseAdapter) {
|
|
343
|
+
nextItems = props.responseAdapter(res).items;
|
|
344
|
+
} else if (res && typeof res === "object" && "results" in res) {
|
|
345
|
+
nextItems = res.results;
|
|
346
|
+
} else {
|
|
347
|
+
nextItems = res;
|
|
348
|
+
}
|
|
349
|
+
fullDataset.value = nextItems;
|
|
350
|
+
hybridLoaded.value = true;
|
|
351
|
+
} catch {
|
|
352
|
+
snack.show("Failed to load data", "error");
|
|
353
|
+
} finally {
|
|
354
|
+
loading.value = false;
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
async function refresh() {
|
|
358
|
+
if (offlineMode.value) {
|
|
359
|
+
applyOffline(props.items ?? []);
|
|
360
|
+
return;
|
|
361
|
+
}
|
|
362
|
+
if (hybridMode.value) {
|
|
363
|
+
if (!hybridLoaded.value) await fetchAll();
|
|
364
|
+
applyOffline(fullDataset.value);
|
|
365
|
+
return;
|
|
366
|
+
}
|
|
367
|
+
loading.value = true;
|
|
368
|
+
try {
|
|
369
|
+
const endpointParams = splitEndpointParams(props.endpoint).params;
|
|
370
|
+
const pageState = {
|
|
371
|
+
page: pagination.value.pageIndex + 1,
|
|
372
|
+
pageSize: pagination.value.pageSize
|
|
373
|
+
};
|
|
374
|
+
const paginationP = props.paginationParams ? props.paginationParams(pageState) : { page: pageState.page, page_size: pageState.pageSize };
|
|
375
|
+
const params = {
|
|
376
|
+
...endpointParams,
|
|
377
|
+
...props.filterParams ?? {},
|
|
378
|
+
...paginationP
|
|
379
|
+
};
|
|
380
|
+
if (search.value) params.search = search.value;
|
|
381
|
+
if (sorting.value.length)
|
|
382
|
+
params.ordering = sorting.value.map((s) => `${s.desc ? "-" : ""}${s.id}`).join(",");
|
|
383
|
+
const res = await crud.list(params);
|
|
384
|
+
let nextItems = [];
|
|
385
|
+
let nextTotal = 0;
|
|
386
|
+
if (props.responseAdapter) {
|
|
387
|
+
const adapted = props.responseAdapter(res);
|
|
388
|
+
nextItems = adapted.items;
|
|
389
|
+
nextTotal = adapted.total;
|
|
390
|
+
} else if (res && typeof res === "object" && "results" in res) {
|
|
391
|
+
nextItems = res.results;
|
|
392
|
+
nextTotal = res.count;
|
|
393
|
+
} else {
|
|
394
|
+
nextItems = res;
|
|
395
|
+
nextTotal = res.length;
|
|
396
|
+
}
|
|
397
|
+
if (nextItems.length === 0 && nextTotal > 0 && pagination.value.pageIndex > 0) {
|
|
398
|
+
pagination.value = { ...pagination.value, pageIndex: 0 };
|
|
399
|
+
return;
|
|
400
|
+
}
|
|
401
|
+
rows.value = nextItems;
|
|
402
|
+
total.value = nextTotal;
|
|
403
|
+
} catch {
|
|
404
|
+
snack.show("Failed to load data", "error");
|
|
405
|
+
} finally {
|
|
406
|
+
loading.value = false;
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
watch([() => props.endpoint, () => props.clientSide], () => {
|
|
410
|
+
hybridLoaded.value = false;
|
|
411
|
+
});
|
|
412
|
+
const filterParamsKey = computed(
|
|
413
|
+
() => JSON.stringify(props.filterParams ?? {})
|
|
414
|
+
);
|
|
415
|
+
watch(
|
|
416
|
+
[
|
|
417
|
+
() => props.endpoint,
|
|
418
|
+
() => props.items,
|
|
419
|
+
filterParamsKey,
|
|
420
|
+
() => pagination.value.pageIndex,
|
|
421
|
+
() => pagination.value.pageSize,
|
|
422
|
+
sorting,
|
|
423
|
+
search
|
|
424
|
+
],
|
|
425
|
+
([newEndpoint, , newFilters], [oldEndpoint, , oldFilters]) => {
|
|
426
|
+
const contextChanged = newEndpoint !== oldEndpoint || newFilters !== oldFilters;
|
|
427
|
+
if (contextChanged && pagination.value.pageIndex !== 0) {
|
|
428
|
+
pagination.value = { ...pagination.value, pageIndex: 0 };
|
|
429
|
+
} else {
|
|
430
|
+
refresh();
|
|
431
|
+
}
|
|
432
|
+
},
|
|
433
|
+
{ immediate: true, deep: true }
|
|
434
|
+
);
|
|
435
|
+
const UCheckbox = resolveComponent("UCheckbox");
|
|
436
|
+
const UButton = resolveComponent("UButton");
|
|
437
|
+
const UIcon = resolveComponent("UIcon");
|
|
438
|
+
const tableColumns = computed(() => {
|
|
439
|
+
const cols = [];
|
|
440
|
+
cols.push({
|
|
441
|
+
id: "__select__",
|
|
442
|
+
header: () => h(UCheckbox, {
|
|
443
|
+
modelValue: isAllSelected.value,
|
|
444
|
+
indeterminate: isIndeterminate.value,
|
|
445
|
+
"onUpdate:modelValue": toggleAll,
|
|
446
|
+
ariaLabel: "Select all rows"
|
|
447
|
+
}),
|
|
448
|
+
cell: ({ row }) => {
|
|
449
|
+
const k = pkOf(row.original);
|
|
450
|
+
return h(UCheckbox, {
|
|
451
|
+
modelValue: !!rowSelection.value[k],
|
|
452
|
+
"onUpdate:modelValue": () => toggleRowByKey(k),
|
|
453
|
+
ariaLabel: "Select row"
|
|
454
|
+
});
|
|
455
|
+
},
|
|
456
|
+
meta: { class: { th: "w-10", td: "w-10" } }
|
|
457
|
+
});
|
|
458
|
+
if (props.draggable) {
|
|
459
|
+
cols.push({
|
|
460
|
+
id: "__drag__",
|
|
461
|
+
header: "",
|
|
462
|
+
cell: ({ row }) => h(
|
|
463
|
+
"span",
|
|
464
|
+
{
|
|
465
|
+
class: canReorder.value ? "cursor-grab text-muted" : "cursor-not-allowed text-muted opacity-30",
|
|
466
|
+
draggable: canReorder.value,
|
|
467
|
+
title: canReorder.value ? "Drag to reorder" : "Clear search & sorting to reorder",
|
|
468
|
+
onDragstart: (e) => {
|
|
469
|
+
e.dataTransfer?.setData("text/plain", "");
|
|
470
|
+
onDragStart(row.index);
|
|
471
|
+
}
|
|
472
|
+
},
|
|
473
|
+
h(UIcon, { name: "i-lucide-grip-vertical", class: "h-4 w-4" })
|
|
474
|
+
),
|
|
475
|
+
meta: { class: { th: "w-8", td: "w-8" } }
|
|
476
|
+
});
|
|
477
|
+
}
|
|
478
|
+
const NuxtLink = resolveComponent("NuxtLink");
|
|
479
|
+
for (let i = 0; i < props.columns.length; i++) {
|
|
480
|
+
const col = props.columns[i];
|
|
481
|
+
const isFirst = i === 0 && !!props.detailBase;
|
|
482
|
+
cols.push({
|
|
483
|
+
accessorKey: col.key,
|
|
484
|
+
header: col.label,
|
|
485
|
+
cell: ({ row }) => {
|
|
486
|
+
const cellSlot = slots[`cell.${col.key}`];
|
|
487
|
+
if (cellSlot)
|
|
488
|
+
return cellSlot({
|
|
489
|
+
item: row.original,
|
|
490
|
+
value: atKey(row.original, col.key)
|
|
491
|
+
});
|
|
492
|
+
const cellVal = atKey(row.original, col.key);
|
|
493
|
+
if (isFirst) {
|
|
494
|
+
return h(
|
|
495
|
+
NuxtLink,
|
|
496
|
+
{
|
|
497
|
+
to: `${props.detailBase}/${atKey(row.original, props.lookup)}`,
|
|
498
|
+
class: "font-medium hover:underline"
|
|
499
|
+
},
|
|
500
|
+
() => String(cellVal ?? "\u2014")
|
|
501
|
+
);
|
|
502
|
+
}
|
|
503
|
+
return String(cellVal ?? "\u2014");
|
|
504
|
+
},
|
|
505
|
+
meta: { class: { td: col.class ?? "" } }
|
|
506
|
+
});
|
|
507
|
+
}
|
|
508
|
+
cols.push({
|
|
509
|
+
id: "__actions__",
|
|
510
|
+
header: "",
|
|
511
|
+
cell: ({ row }) => {
|
|
512
|
+
const buttons = [];
|
|
513
|
+
if (props.editFields.length > 0 && canEditRow.value) {
|
|
514
|
+
buttons.push(
|
|
515
|
+
h(UButton, {
|
|
516
|
+
size: "xs",
|
|
517
|
+
variant: "ghost",
|
|
518
|
+
color: "neutral",
|
|
519
|
+
icon: "i-lucide-pencil",
|
|
520
|
+
onClick: () => openQuickEdit(
|
|
521
|
+
atKey(row.original, props.lookup)
|
|
522
|
+
)
|
|
523
|
+
})
|
|
524
|
+
);
|
|
525
|
+
}
|
|
526
|
+
if (canDeleteRow.value) {
|
|
527
|
+
buttons.push(
|
|
528
|
+
h(UButton, {
|
|
529
|
+
size: "xs",
|
|
530
|
+
variant: "ghost",
|
|
531
|
+
color: "error",
|
|
532
|
+
icon: "i-lucide-trash-2",
|
|
533
|
+
onClick: () => deleteRow(row.original)
|
|
534
|
+
})
|
|
535
|
+
);
|
|
536
|
+
}
|
|
537
|
+
return h(
|
|
538
|
+
"div",
|
|
539
|
+
{ class: "flex items-center gap-1 justify-end" },
|
|
540
|
+
buttons
|
|
541
|
+
);
|
|
542
|
+
},
|
|
543
|
+
meta: { class: { th: "text-right", td: "text-right" } }
|
|
544
|
+
});
|
|
545
|
+
return cols;
|
|
546
|
+
});
|
|
547
|
+
const paginationOptions = computed(() => ({
|
|
548
|
+
manualPagination: true,
|
|
549
|
+
rowCount: total.value
|
|
550
|
+
}));
|
|
551
|
+
const sortingOptions = computed(() => ({
|
|
552
|
+
manualSorting: true
|
|
553
|
+
}));
|
|
554
|
+
</script>
|
|
555
|
+
|
|
556
|
+
<template>
|
|
557
|
+
<div class="mapo-list-table space-y-3">
|
|
558
|
+
<!-- Search -->
|
|
559
|
+
<div v-if="searchable" class="flex items-center gap-2">
|
|
560
|
+
<UInput
|
|
561
|
+
:model-value="search"
|
|
562
|
+
placeholder="Search..."
|
|
563
|
+
icon="i-lucide-search"
|
|
564
|
+
size="sm"
|
|
565
|
+
class="max-w-xs"
|
|
566
|
+
@update:model-value="onSearchInput"
|
|
567
|
+
/>
|
|
568
|
+
<slot name="dtable.toolbar" />
|
|
569
|
+
</div>
|
|
570
|
+
|
|
571
|
+
<!-- Table — the drop area spans the whole row via delegated DnD on this wrapper. -->
|
|
572
|
+
<div
|
|
573
|
+
ref="tableWrapper"
|
|
574
|
+
@dragover="onRowDragOver"
|
|
575
|
+
@drop="onRowDrop"
|
|
576
|
+
@dragend="onDragEnd"
|
|
577
|
+
>
|
|
578
|
+
<UTable
|
|
579
|
+
:data="rows"
|
|
580
|
+
:columns="tableColumns"
|
|
581
|
+
:loading="loading"
|
|
582
|
+
:pagination="pagination"
|
|
583
|
+
:pagination-options="paginationOptions"
|
|
584
|
+
:sorting="sorting"
|
|
585
|
+
:sorting-options="sortingOptions"
|
|
586
|
+
empty="No items found"
|
|
587
|
+
@update:pagination="pagination = $event ?? pagination"
|
|
588
|
+
@update:sorting="sorting = $event ?? sorting"
|
|
589
|
+
>
|
|
590
|
+
<template #empty>
|
|
591
|
+
<slot name="dtable.empty">
|
|
592
|
+
<div class="flex flex-col items-center gap-2 py-8 text-muted">
|
|
593
|
+
<UIcon name="i-lucide-inbox" class="h-8 w-8" />
|
|
594
|
+
<span class="text-sm">No items found</span>
|
|
595
|
+
</div>
|
|
596
|
+
</slot>
|
|
597
|
+
</template>
|
|
598
|
+
|
|
599
|
+
<template #loading>
|
|
600
|
+
<slot name="dtable.loading">
|
|
601
|
+
<div class="flex justify-center py-8">
|
|
602
|
+
<UIcon
|
|
603
|
+
name="i-lucide-loader-circle"
|
|
604
|
+
class="h-6 w-6 animate-spin text-muted"
|
|
605
|
+
/>
|
|
606
|
+
</div>
|
|
607
|
+
</slot>
|
|
608
|
+
</template>
|
|
609
|
+
</UTable>
|
|
610
|
+
</div>
|
|
611
|
+
|
|
612
|
+
<!-- Pagination controls -->
|
|
613
|
+
<div class="flex items-center justify-between text-sm text-muted">
|
|
614
|
+
<span>{{ total }} items</span>
|
|
615
|
+
<UPagination
|
|
616
|
+
:model-value="pagination.pageIndex + 1"
|
|
617
|
+
:total="total"
|
|
618
|
+
:page-size="pagination.pageSize"
|
|
619
|
+
size="sm"
|
|
620
|
+
@update:model-value="
|
|
621
|
+
pagination = { ...pagination, pageIndex: $event - 1 }
|
|
622
|
+
"
|
|
623
|
+
/>
|
|
624
|
+
<USelect
|
|
625
|
+
:model-value="pagination.pageSize"
|
|
626
|
+
:items="
|
|
627
|
+
pageSizeOptions.map((n) => ({ label: `${n} / page`, value: n }))
|
|
628
|
+
"
|
|
629
|
+
size="xs"
|
|
630
|
+
class="w-28"
|
|
631
|
+
@update:model-value="
|
|
632
|
+
pagination = { pageIndex: 0, pageSize: Number($event) }
|
|
633
|
+
"
|
|
634
|
+
/>
|
|
635
|
+
</div>
|
|
636
|
+
|
|
637
|
+
<!-- Quick edit modal -->
|
|
638
|
+
<MapoListQuickEdit
|
|
639
|
+
v-if="editFields.length > 0"
|
|
640
|
+
v-model:open="quickEditOpen"
|
|
641
|
+
:endpoint="crudBase"
|
|
642
|
+
:item-id="quickEditId"
|
|
643
|
+
:edit-fields="editFields"
|
|
644
|
+
:lookup="lookup"
|
|
645
|
+
:languages="languages"
|
|
646
|
+
:registry="registry"
|
|
647
|
+
:offline="offlineMode"
|
|
648
|
+
:local-item="quickEditLocalItem"
|
|
649
|
+
@saved="onQuickEditSaved"
|
|
650
|
+
>
|
|
651
|
+
<template v-for="(_, name) in $slots" #[name]="slotProps">
|
|
652
|
+
<slot :name="`qedit.${String(name)}`" v-bind="slotProps ?? {}" />
|
|
653
|
+
</template>
|
|
654
|
+
</MapoListQuickEdit>
|
|
655
|
+
</div>
|
|
656
|
+
</template>
|
|
657
|
+
|
|
658
|
+
<style scoped>
|
|
659
|
+
.mapo-list-table :deep(tbody tr.mapo-drag-source){opacity:.4}.mapo-list-table :deep(tbody tr.mapo-drop-before>td){box-shadow:inset 0 2px 0 0 var(--ui-primary,#3b82f6)}.mapo-list-table :deep(tbody tr.mapo-drop-after>td){box-shadow:inset 0 -2px 0 0 var(--ui-primary,#3b82f6)}
|
|
660
|
+
</style>
|