adminforth 2.1.0 → 2.2.0-next.1

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.
Files changed (44) hide show
  1. package/commands/bundle.js +1 -1
  2. package/commands/createApp/templates/Dockerfile.hbs +1 -1
  3. package/commands/createCustomComponent/configUpdater.js +60 -4
  4. package/commands/createCustomComponent/main.js +40 -13
  5. package/commands/createResource/generateResourceFile.js +37 -8
  6. package/commands/createResource/main.js +25 -14
  7. package/dist/dataConnectors/clickhouse.d.ts +2 -0
  8. package/dist/dataConnectors/clickhouse.d.ts.map +1 -1
  9. package/dist/dataConnectors/clickhouse.js +32 -1
  10. package/dist/dataConnectors/clickhouse.js.map +1 -1
  11. package/dist/dataConnectors/mongo.d.ts +2 -0
  12. package/dist/dataConnectors/mongo.d.ts.map +1 -1
  13. package/dist/dataConnectors/mongo.js +27 -0
  14. package/dist/dataConnectors/mongo.js.map +1 -1
  15. package/dist/dataConnectors/mysql.d.ts +2 -0
  16. package/dist/dataConnectors/mysql.d.ts.map +1 -1
  17. package/dist/dataConnectors/mysql.js +16 -0
  18. package/dist/dataConnectors/mysql.js.map +1 -1
  19. package/dist/dataConnectors/postgres.d.ts +2 -0
  20. package/dist/dataConnectors/postgres.d.ts.map +1 -1
  21. package/dist/dataConnectors/postgres.js +16 -0
  22. package/dist/dataConnectors/postgres.js.map +1 -1
  23. package/dist/servers/express.js +1 -1
  24. package/dist/spa/package-lock.json +13 -0
  25. package/dist/spa/package.json +1 -0
  26. package/dist/spa/src/afcl/CountryFlag.vue +26 -0
  27. package/dist/spa/src/afcl/PieChart.vue +20 -1
  28. package/dist/spa/src/afcl/index.ts +2 -0
  29. package/dist/spa/src/components/Filters.vue +1 -1
  30. package/dist/spa/src/components/MenuLink.vue +5 -4
  31. package/dist/spa/src/components/ResourceListTable.vue +35 -33
  32. package/dist/spa/src/components/ResourceListTableVirtual.vue +738 -0
  33. package/dist/spa/src/components/ValueRenderer.vue +5 -5
  34. package/dist/spa/src/renderers/CountryFlag.vue +2 -20
  35. package/dist/spa/src/renderers/ZeroStylesRichText.vue +15 -3
  36. package/dist/spa/src/stores/core.ts +3 -1
  37. package/dist/spa/src/types/Common.ts +10 -0
  38. package/dist/spa/src/views/EditView.vue +9 -1
  39. package/dist/spa/src/views/ListView.vue +23 -1
  40. package/dist/spa/src/views/ResourceParent.vue +1 -1
  41. package/dist/types/Common.d.ts +8 -0
  42. package/dist/types/Common.d.ts.map +1 -1
  43. package/dist/types/Common.js.map +1 -1
  44. package/package.json +1 -1
@@ -0,0 +1,738 @@
1
+ <template>
2
+ <!-- table -->
3
+ <div class="relative shadow-listTableShadow dark:shadow-darkListTableShadow overflow-auto "
4
+ :class="{'rounded-default': !noRoundings}"
5
+ :style="`height: ${containerHeight}px; will-change: transform;`"
6
+ @scroll="handleScroll"
7
+ ref="containerRef"
8
+ >
9
+ <!-- skelet loader -->
10
+ <div role="status" v-if="!resource || !resource.columns"
11
+ class="max-w p-4 space-y-4 divide-y divide-gray-200 rounded shadow animate-pulse dark:divide-gray-700 md:p-6 dark:border-gray-700">
12
+
13
+ <div role="status" class="max-w-sm animate-pulse">
14
+ <div class="h-2 bg-gray-200 rounded-full dark:bg-gray-700 max-w-[360px]"></div>
15
+ </div>
16
+ </div>
17
+ <table v-else class=" w-full text-sm text-left rtl:text-right text-gray-500 dark:text-gray-400 rounded-default">
18
+
19
+ <tbody>
20
+ <!-- table header -->
21
+ <tr class="t-header sticky z-10 top-0 text-xs bg-lightListTableHeading dark:bg-darkListTableHeading dark:text-gray-400">
22
+ <td scope="col" class="p-4">
23
+ <div class="flex items-center">
24
+ <input id="checkbox-all-search" type="checkbox" :checked="allFromThisPageChecked" @change="selectAll()"
25
+ :disabled="!rows || !rows.length"
26
+ class="w-4 h-4 text-blue-600 bg-gray-100 border-gray-300 rounded
27
+ focus:ring-blue-500 dark:focus:ring-blue-600 dark:ring-offset-gray-800 dark:focus:ring-offset-gray-800 focus:ring-2 dark:bg-gray-700 dark:border-gray-600">
28
+ <label for="checkbox-all-search" class="sr-only">{{ $t('checkbox') }}</label>
29
+ </div>
30
+ </td>
31
+
32
+ <td v-for="c in columnsListed" ref="headerRefs" scope="col" class="px-2 md:px-3 lg:px-6 py-3">
33
+
34
+ <div @click="(evt) => c.sortable && onSortButtonClick(evt, c.name)"
35
+ class="flex items-center " :class="{'cursor-pointer':c.sortable}">
36
+ {{ c.label }}
37
+
38
+ <div v-if="c.sortable">
39
+ <svg v-if="ascArr.includes(c.name) || descArr.includes(c.name)" class="w-3 h-3 ms-1.5"
40
+ fill='currentColor'
41
+ :class="{'rotate-180':descArr.includes(c.name)}" viewBox="0 0 24 24">
42
+ <path
43
+ d="M8.574 11.024h6.852a2.075 2.075 0 0 0 1.847-1.086 1.9 1.9 0 0 0-.11-1.986L13.736 2.9a2.122 2.122 0 0 0-3.472 0L6.837 7.952a1.9 1.9 0 0 0-.11 1.986 2.074 2.074 0 0 0 1.847 0z"/>
44
+ </svg>
45
+ <svg v-else class="w-3 h-3 ms-1.5 opacity-30" aria-hidden="true" xmlns="http://www.w3.org/2000/svg"
46
+ fill='currentColor'
47
+ viewBox="0 0 24 24">
48
+ <path
49
+ d="M8.574 11.024h6.852a2.075 2.075 0 0 0 1.847-1.086 1.9 1.9 0 0 0-.11-1.986L13.736 2.9a2.122 2.122 0 0 0-3.472 0L6.837 7.952a1.9 1.9 0 0 0-.11 1.986 2.074 2.074 0 0 0 1.847 1.086Zm6.852 1.952H8.574a2.072 2.072 0 0 0-1.847 1.087 1.9 1.9 0 0 0 .11 1.985l3.426 5.05a2.123 2.123 0 0 0 3.472 0l3.427-5.05a1.9 1.9 0 0 0 .11-1.985 2.074 2.074 0 0 0-1.846-1.087Z"/>
50
+ </svg>
51
+ </div>
52
+ <span
53
+ class="bg-red-100 text-red-800 text-xs font-medium me-1 px-1 py-0.5 rounded dark:bg-gray-700 dark:text-red-400 border border-red-400"
54
+ v-if="sort.findIndex((s) => s.field === c.name) !== -1 && sort?.length > 1">
55
+ {{ sort.findIndex((s) => s.field === c.name) + 1 }}
56
+ </span>
57
+
58
+ </div>
59
+ </td>
60
+
61
+ <td scope="col" class="px-6 py-3">
62
+ {{ $t('Actions') }}
63
+ </td>
64
+ </tr>
65
+ <tr v-for="c in tableBodyStartInjection" :key="c.id" class="align-top border-b border-lightListBorder dark:border-darkListBorder dark:bg-darkListTable">
66
+ <component :is="getCustomComponent(c)" :meta="c.meta" :resource="resource" :adminUser="coreStore.adminUser" />
67
+ </tr>
68
+ <!-- table header end -->
69
+ <SkeleteLoader
70
+ v-if="!rows"
71
+ :columns="resource?.columns.filter(c => c.showIn.list).length + 2"
72
+ :rows="rowHeights.length || 20"
73
+ :row-heights="rowHeights"
74
+ :column-widths="columnWidths"
75
+ />
76
+
77
+ <tr v-else-if="rows.length === 0" class="bg-lightListTable dark:bg-darkListTable dark:border-darkListTableBorder">
78
+ <td :colspan="resource?.columns.length + 2">
79
+
80
+ <div id="toast-simple"
81
+ class=" mx-auto my-5 flex items-center w-full max-w-xs p-4 space-x-4 rtl:space-x-reverse text-gray-500 divide-x rtl:divide-x-reverse divide-gray-200 dark:text-gray-400 dark:divide-gray-700 space-x dark:bg-gray-800"
82
+ role="alert">
83
+ <IconInboxOutline class="w-6 h-6 text-gray-500 dark:text-gray-400"/>
84
+ <div class="ps-4 text-sm font-normal">{{ $t('No items here yet') }}</div>
85
+ </div>
86
+
87
+ </td>
88
+ </tr>
89
+
90
+ <!-- Virtual scroll spacer -->
91
+ <tr v-if="spacerHeight > 0">
92
+ <td :colspan="resource?.columns.length + 2" :style="{ height: `${spacerHeight}px` }"></td>
93
+ </tr>
94
+
95
+ <!-- Visible rows -->
96
+ <tr @click="onClick($event,row)"
97
+ v-for="(row, rowI) in visibleRows"
98
+ :key="`row_${row._primaryKeyValue}`"
99
+ ref="rowRefs"
100
+ class="bg-lightListTable dark:bg-darkListTable border-lightListBorder dark:border-gray-700 hover:bg-lightListTableRowHover dark:hover:bg-darkListTableRowHover"
101
+ :class="{'border-b': rowI !== visibleRows.length - 1, 'cursor-pointer': row._clickUrl !== null}"
102
+ @mounted="(el) => updateRowHeight(`row_${row._primaryKeyValue}`, el.offsetHeight)"
103
+ >
104
+ <td class="w-4 p-4 cursor-default" @click="(e)=>{e.stopPropagation()}">
105
+ <div class="flex items center ">
106
+ <input
107
+ @click="(e)=>{e.stopPropagation()}"
108
+ id="checkbox-table-search-1"
109
+ type="checkbox"
110
+ :checked="checkboxesInternal.includes(row._primaryKeyValue)"
111
+ @change="(e)=>{addToCheckedValues(row._primaryKeyValue)}"
112
+ class="w-4 h-4 text-blue-600 bg-gray-100 border-gray-300 rounded focus:ring-blue-500 dark:focus:ring-blue-600 dark:ring-offset-gray-800 dark:focus:ring-offset-gray-800 focus:ring-2 dark:bg-gray-700 dark:border-gray-600 cursor-pointer">
113
+ <label for="checkbox-table-search-1" class="sr-only">{{ $t('checkbox') }}</label>
114
+ </div>
115
+ </td>
116
+ <td v-for="c in columnsListed" class="px-2 md:px-3 lg:px-6 py-4">
117
+ <!-- if c.name in listComponentsPerColumn, render it. If not, render ValueRenderer -->
118
+ <component
119
+ :is="c?.components?.list ? getCustomComponent(c.components.list) : ValueRenderer"
120
+ :meta="c?.components?.list?.meta"
121
+ :column="c"
122
+ :record="row"
123
+ :adminUser="coreStore.adminUser"
124
+ :resource="resource"
125
+ />
126
+ </td>
127
+ <td class=" items-center px-2 md:px-3 lg:px-6 py-4 cursor-default" @click="(e)=>{e.stopPropagation()}">
128
+ <div class="flex text-lightPrimary dark:text-darkPrimary items-center">
129
+ <Tooltip>
130
+ <RouterLink
131
+ v-if="resource.options?.allowedActions.show"
132
+ :to="{
133
+ name: 'resource-show',
134
+ params: {
135
+ resourceId: resource.resourceId,
136
+ primaryKey: row._primaryKeyValue,
137
+ }
138
+ }"
139
+
140
+ >
141
+ <IconEyeSolid class="w-5 h-5 me-2"/>
142
+ </RouterLink>
143
+
144
+ <template v-slot:tooltip>
145
+ {{ $t('Show item') }}
146
+ </template>
147
+ </Tooltip>
148
+
149
+ <Tooltip>
150
+ <RouterLink
151
+ v-if="resource.options?.allowedActions.edit"
152
+ :to="{
153
+ name: 'resource-edit',
154
+ params: {
155
+ resourceId: resource.resourceId,
156
+ primaryKey: row._primaryKeyValue,
157
+ }
158
+ }"
159
+ >
160
+ <IconPenSolid class="w-5 h-5 me-2"/>
161
+ </RouterLink>
162
+ <template v-slot:tooltip>
163
+ {{ $t('Edit item') }}
164
+ </template>
165
+ </Tooltip>
166
+
167
+ <Tooltip>
168
+ <button
169
+ v-if="resource.options?.allowedActions.delete"
170
+ @click="deleteRecord(row)"
171
+ >
172
+ <IconTrashBinSolid class="w-5 h-5 me-2"/>
173
+ </button>
174
+
175
+ <template v-slot:tooltip>
176
+ {{ $t('Delete item') }}
177
+ </template>
178
+ </Tooltip>
179
+
180
+ <template v-if="customActionsInjection">
181
+ <component
182
+ v-for="c in customActionsInjection"
183
+ :is="getCustomComponent(c)"
184
+ :meta="c.meta"
185
+ :resource="coreStore.resource"
186
+ :adminUser="coreStore.adminUser"
187
+ :record="row"
188
+ />
189
+ </template>
190
+
191
+ <template v-if="resource.options?.actions">
192
+ <Tooltip v-for="action in resource.options.actions.filter(a => a.showIn?.list)" :key="action.id">
193
+ <button
194
+ @click="startCustomAction(action.id, row)"
195
+ >
196
+ <component v-if="action.icon" :is="getIcon(action.icon)" class="w-5 h-5 mr-2 text-lightPrimary dark:text-darkPrimary"></component>
197
+ </button>
198
+ <template v-slot:tooltip>
199
+ {{ action.name }}
200
+ </template>
201
+ </Tooltip>
202
+ </template>
203
+ </div>
204
+ </td>
205
+ </tr>
206
+
207
+ <!-- Bottom spacer -->
208
+ <tr v-if="totalHeight > 0">
209
+ <td :colspan="resource?.columns.length + 2"
210
+ :style="{ height: `${Math.max(0, totalHeight - (endIndex + 1) * (props.itemHeight || 52.5))}px` }">
211
+ </td>
212
+ </tr>
213
+ </tbody>
214
+ </table>
215
+ </div>
216
+ <!-- pagination
217
+ totalRows in v-if is used to not hide page input during loading when user puts cursor into it and edit directly (rows gets null there during edit)
218
+ -->
219
+ <div class="flex flex-row items-center mt-4 xs:flex-row xs:justify-between xs:items-center gap-3"
220
+ v-if="(rows || totalRows) && totalRows >= pageSize && totalRows > 0"
221
+ >
222
+
223
+ <div class="inline-flex ">
224
+ <!-- Buttons -->
225
+ <button
226
+ class="flex items-center py-1 px-3 gap-1 text-sm font-medium text-gray-900 focus:outline-none bg-white border-r-0 rounded-s border border-gray-300 hover:bg-gray-100 hover:text-lightPrimary focus:z-10 focus:ring-4 focus:ring-gray-100 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 disabled:opacity-50"
227
+ @click="page--; pageInput = page.toString();" :disabled="page <= 1">
228
+ <svg class="w-3.5 h-3.5 rtl:rotate-180" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="none"
229
+ viewBox="0 0 14 10">
230
+ <path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
231
+ d="M13 5H1m0 0 4 4M1 5l4-4"/>
232
+ </svg>
233
+ <span class="hidden sm:inline">
234
+ {{ $t('Prev') }}
235
+ </span>
236
+ </button>
237
+ <button
238
+ class="flex items-center py-1 px-3 text-sm font-medium text-gray-900 focus:outline-none bg-white border-r-0 border border-gray-300 hover:bg-gray-100 hover:text-lightPrimary focus:z-10 focus:ring-4 focus:ring-gray-100 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 disabled:opacity-50"
239
+ @click="page = 1; pageInput = page.toString();" :disabled="page <= 1">
240
+ <!-- <IconChevronDoubleLeftOutline class="w-4 h-4" /> -->
241
+ 1
242
+ </button>
243
+ <div
244
+ contenteditable="true"
245
+ class="min-w-10 outline-none inline-block w-auto min-w-10 py-1.5 px-3 text-sm text-center text-gray-700 border border-gray-300 dark:border-gray-700 dark:text-gray-400 dark:bg-gray-800 z-10"
246
+ @keydown="onPageKeydown($event)"
247
+ @input="onPageInput($event)"
248
+ @blur="validatePageInput()"
249
+ >
250
+ {{ pageInput }}
251
+ </div>
252
+
253
+ <button
254
+ class="flex items-center py-1 px-3 text-sm font-medium text-gray-900 focus:outline-none bg-white border-l-0 border border-gray-300 hover:bg-gray-100 hover:text-lightPrimary focus:z-10 focus:ring-4 focus:ring-gray-100 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 disabled:opacity-50"
255
+ @click="page = totalPages; pageInput = page.toString();" :disabled="page >= totalPages">
256
+ {{ totalPages }}
257
+
258
+ <!-- <IconChevronDoubleRightOutline class="w-4 h-4" /> -->
259
+ </button>
260
+ <button
261
+ class="flex items-center py-1 px-3 gap-1 text-sm font-medium text-gray-900 focus:outline-none bg-white border-l-0 rounded-e border border-gray-300 hover:bg-gray-100 hover:text-lightPrimary focus:z-10 focus:ring-4 focus:ring-gray-100 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 disabled:opacity-50"
262
+ @click="page++; pageInput = page.toString();" :disabled="page >= totalPages">
263
+ <span class="hidden sm:inline">{{ $t('Next') }}</span>
264
+ <svg class="w-3.5 h-3.5 rtl:rotate-180" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="none"
265
+ viewBox="0 0 14 10">
266
+ <path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
267
+ d="M1 5h12m0 0L9 1m4 4L9 9"/>
268
+ </svg>
269
+ </button>
270
+ </div>
271
+
272
+ <!-- Help text -->
273
+ <span class="text-sm text-gray-700 dark:text-gray-400">
274
+ <span v-if="((page || 1) - 1) * pageSize + 1 > totalRows">{{ $t('Wrong Page') }} </span>
275
+ <template v-else>
276
+
277
+ <span class="hidden sm:inline">
278
+ <i18n-t keypath="Showing {from} to {to} of {total} Entries" tag="p" >
279
+ <template v-slot:from>
280
+ <strong>{{ from }}</strong>
281
+ </template>
282
+ <template v-slot:to>
283
+ <strong>{{ to }}</strong>
284
+ </template>
285
+ <template v-slot:total>
286
+ <strong>{{ totalRows }}</strong>
287
+ </template>
288
+ </i18n-t>
289
+ </span>
290
+ <span class="sm:hidden">
291
+ <i18n-t keypath="{from} - {to} of {total}" tag="p" >
292
+ <template v-slot:from>
293
+ <strong>{{ from }}</strong>
294
+ </template>
295
+ <template v-slot:to>
296
+ <strong>{{ to }}</strong>
297
+ </template>
298
+ <template v-slot:total>
299
+ <strong>{{ totalRows }}</strong>
300
+ </template>
301
+ </i18n-t>
302
+ </span>
303
+ </template>
304
+ </span>
305
+ </div>
306
+ </template>
307
+
308
+ <script setup lang="ts">
309
+
310
+
311
+ import { computed, onMounted, ref, watch, useTemplateRef, nextTick, type Ref, onUnmounted } from 'vue';
312
+ import { callAdminForthApi } from '@/utils';
313
+ import { useI18n } from 'vue-i18n';
314
+ import ValueRenderer from '@/components/ValueRenderer.vue';
315
+ import { getCustomComponent } from '@/utils';
316
+ import { useCoreStore } from '@/stores/core';
317
+ import { showSuccesTost, showErrorTost } from '@/composables/useFrontendApi';
318
+ import SkeleteLoader from '@/components/SkeleteLoader.vue';
319
+ import { getIcon } from '@/utils';
320
+ import {
321
+ IconInboxOutline,
322
+ } from '@iconify-prerendered/vue-flowbite';
323
+
324
+ import {
325
+ IconEyeSolid,
326
+ IconPenSolid,
327
+ IconTrashBinSolid
328
+ } from '@iconify-prerendered/vue-flowbite';
329
+ import router from '@/router';
330
+ import { Tooltip } from '@/afcl';
331
+ import type { AdminForthResourceCommon } from '@/types/Common';
332
+ import adminforth from '@/adminforth';
333
+
334
+ const coreStore = useCoreStore();
335
+ const { t } = useI18n();
336
+ const props = defineProps<{
337
+ page: number,
338
+ resource: AdminForthResourceCommon | null,
339
+ rows: any[] | null,
340
+ totalRows: number,
341
+ pageSize: number,
342
+ checkboxes: any[],
343
+ sort: any[],
344
+ noRoundings?: boolean,
345
+ customActionsInjection?: any[],
346
+ tableBodyStartInjection?: any[],
347
+ containerHeight?: number,
348
+ itemHeight?: number,
349
+ bufferSize?: number,
350
+ }>();
351
+
352
+ // emits, update page
353
+ const emits = defineEmits([
354
+ 'update:page',
355
+ 'update:sort',
356
+ 'update:checkboxes',
357
+ 'update:records'
358
+
359
+ ]);
360
+
361
+ const checkboxesInternal: Ref<any[]> = ref([]);
362
+ const pageInput = ref('1');
363
+ const page = ref(1);
364
+ const sort = ref([]);
365
+
366
+
367
+ const from = computed(() => ((page.value || 1) - 1) * props.pageSize + 1);
368
+ const to = computed(() => Math.min((page.value || 1) * props.pageSize, props.totalRows));
369
+
370
+ watch(() => page.value, (newPage) => {
371
+ emits('update:page', newPage);
372
+ });
373
+ async function onPageKeydown(event) {
374
+ // page input should accept only numbers, arrow keys and backspace
375
+ if (['Enter', 'Space'].includes(event.code) ||
376
+ (!['Backspace', 'ArrowRight', 'ArrowLeft'].includes(event.code)
377
+ && isNaN(String.fromCharCode(event.keyCode)))) {
378
+ event.preventDefault();
379
+ if (event.code === 'Enter') {
380
+ validatePageInput();
381
+ event.target.blur();
382
+ }
383
+ }
384
+ }
385
+
386
+ watch(() => sort.value, (newSort) => {
387
+ emits('update:sort', newSort);
388
+ });
389
+
390
+ watch(() => checkboxesInternal.value, (newCheckboxes) => {
391
+ emits('update:checkboxes', newCheckboxes);
392
+ });
393
+
394
+ watch(() => props.checkboxes, (newCheckboxes) => {
395
+ checkboxesInternal.value = newCheckboxes;
396
+ });
397
+
398
+ watch(() => props.sort, (newSort) => {
399
+ sort.value = newSort;
400
+ });
401
+
402
+ watch(() => props.page, (newPage) => {
403
+ // page.value and newPage will not be equal only on page load
404
+ // this check prevents cursor jumping on manual input
405
+ if (page.value !== newPage) pageInput.value = newPage.toString();
406
+ page.value = newPage;
407
+ });
408
+
409
+ const rowRefs = useTemplateRef('rowRefs');
410
+ const headerRefs = useTemplateRef('headerRefs');
411
+ const rowHeights = ref([]);
412
+ const columnWidths = ref([]);
413
+ watch(() => props.rows, (newRows) => {
414
+ // rows are set to null when new records are loading
415
+ rowHeights.value = newRows || !rowRefs.value ? [] : rowRefs.value.map((el) => el.offsetHeight);
416
+ columnWidths.value = newRows || !headerRefs.value ? [] : [48, ...headerRefs.value.map((el) => el.offsetWidth)];
417
+ });
418
+
419
+ function addToCheckedValues(id) {
420
+ if (checkboxesInternal.value.includes(id)) {
421
+ checkboxesInternal.value = checkboxesInternal.value.filter((item) => item !== id);
422
+ } else {
423
+ checkboxesInternal.value.push(id);
424
+ }
425
+ checkboxesInternal.value = [ ...checkboxesInternal.value ]
426
+ }
427
+
428
+ const columnsListed = computed(() => props.resource?.columns?.filter(c => c.showIn.list));
429
+
430
+ async function selectAll(value) {
431
+ if (!allFromThisPageChecked.value) {
432
+ props.rows.forEach((r) => {
433
+ if (!checkboxesInternal.value.includes(r._primaryKeyValue)) {
434
+ checkboxesInternal.value.push(r._primaryKeyValue)
435
+ }
436
+ });
437
+ } else {
438
+ props.rows.forEach((r) => {
439
+ checkboxesInternal.value = checkboxesInternal.value.filter((item) => item !== r._primaryKeyValue);
440
+ });
441
+ }
442
+ checkboxesInternal.value = [ ...checkboxesInternal.value ];
443
+ }
444
+
445
+ const totalPages = computed(() => Math.ceil(props.totalRows / props.pageSize));
446
+
447
+ const allFromThisPageChecked = computed(() => {
448
+ if (!props.rows || !props.rows.length) return false;
449
+ return props.rows.every((r) => checkboxesInternal.value.includes(r._primaryKeyValue));
450
+ });
451
+ const ascArr = computed(() => sort.value.filter((s) => s.direction === 'asc').map((s) => s.field));
452
+ const descArr = computed(() => sort.value.filter((s) => s.direction === 'desc').map((s) => s.field));
453
+
454
+
455
+ function onSortButtonClick(event, field) {
456
+ // if ctrl key is pressed, add to sort otherwise sort by this field
457
+ // in any case if field is already in sort, toggle direction
458
+
459
+ const sortIndex = sort.value.findIndex((s) => s.field === field);
460
+ if (sortIndex === -1) {
461
+ // field is not in sort, add it
462
+ if (event.ctrlKey) {
463
+ sort.value = [...sort.value,{field, direction: 'asc'}];
464
+ } else {
465
+ sort.value = [{field, direction: 'asc'}];
466
+ }
467
+ } else {
468
+ const sortField = sort.value[sortIndex];
469
+ if (sortField.direction === 'asc') {
470
+ sort.value = sort.value.map((s) => s.field === field ? {field, direction: 'desc'} : s);
471
+ } else {
472
+ sort.value = sort.value.filter((s) => s.field !== field);
473
+ }
474
+ }
475
+ }
476
+
477
+
478
+ const clickTarget = ref(null);
479
+
480
+ async function onClick(e,row) {
481
+ if(clickTarget.value === e.target) return;
482
+ clickTarget.value = e.target;
483
+ await new Promise((resolve) => setTimeout(resolve, 100));
484
+ if (window.getSelection().toString()) return;
485
+ else {
486
+ if (row._clickUrl === null) {
487
+ // user asked to nothing on click
488
+ return;
489
+ }
490
+ if (e.ctrlKey || e.metaKey || row._clickUrl?.includes('target=_blank')) {
491
+
492
+ if (row._clickUrl) {
493
+ window.open(row._clickUrl, '_blank');
494
+ } else {
495
+ window.open(
496
+ router.resolve({
497
+ name: 'resource-show',
498
+ params: {
499
+ resourceId: props.resource.resourceId,
500
+ primaryKey: row._primaryKeyValue,
501
+ },
502
+ }).href,
503
+ '_blank'
504
+ );
505
+ }
506
+ } else {
507
+ if (row._clickUrl) {
508
+ if (row._clickUrl.startsWith('http')) {
509
+ document.location.href = row._clickUrl;
510
+ } else {
511
+ router.push(row._clickUrl);
512
+ }
513
+ } else {
514
+ router.push({
515
+ name: 'resource-show',
516
+ params: {
517
+ resourceId: props.resource.resourceId,
518
+ primaryKey: row._primaryKeyValue,
519
+ },
520
+ });
521
+ }
522
+ }
523
+ }
524
+ }
525
+
526
+ async function deleteRecord(row) {
527
+ const data = await adminforth.confirm({
528
+ message: t('Are you sure you want to delete this item?'),
529
+ yes: t('Delete'),
530
+ no: t('Cancel'),
531
+ });
532
+ if (data) {
533
+ try {
534
+ const res = await callAdminForthApi({
535
+ path: '/delete_record',
536
+ method: 'POST',
537
+ body: {
538
+ resourceId: props.resource.resourceId,
539
+ primaryKey: row._primaryKeyValue,
540
+ }
541
+ });
542
+ if (!res.error){
543
+ emits('update:records', true)
544
+ showSuccesTost(t('Record deleted successfully'))
545
+ } else {
546
+ showErrorTost(res.error)
547
+ }
548
+
549
+ } catch (e) {
550
+ showErrorTost(t('Something went wrong, please try again later'));
551
+ console.error(e);
552
+ };
553
+ }
554
+ }
555
+
556
+ const actionLoadingStates = ref({});
557
+
558
+ async function startCustomAction(actionId, row) {
559
+ actionLoadingStates.value[actionId] = true;
560
+
561
+ const data = await callAdminForthApi({
562
+ path: '/start_custom_action',
563
+ method: 'POST',
564
+ body: {
565
+ resourceId: props.resource.resourceId,
566
+ actionId: actionId,
567
+ recordId: row._primaryKeyValue
568
+ }
569
+ });
570
+
571
+ actionLoadingStates.value[actionId] = false;
572
+
573
+ if (data?.redirectUrl) {
574
+ // Check if the URL should open in a new tab
575
+ if (data.redirectUrl.includes('target=_blank')) {
576
+ window.open(data.redirectUrl.replace('&target=_blank', '').replace('?target=_blank', ''), '_blank');
577
+ } else {
578
+ // Navigate within the app
579
+ if (data.redirectUrl.startsWith('http')) {
580
+ window.location.href = data.redirectUrl;
581
+ } else {
582
+ router.push(data.redirectUrl);
583
+ }
584
+ }
585
+ return;
586
+ }
587
+ if (data?.ok) {
588
+ emits('update:records', true);
589
+
590
+ if (data.successMessage) {
591
+ adminforth.alert({
592
+ message: data.successMessage,
593
+ variant: 'success'
594
+ });
595
+ }
596
+ }
597
+
598
+ if (data?.error) {
599
+ showErrorTost(data.error);
600
+ }
601
+ }
602
+
603
+ function onPageInput(event) {
604
+ pageInput.value = event.target.innerText;
605
+ }
606
+
607
+ function validatePageInput() {
608
+ const newPage = parseInt(pageInput.value) || 1;
609
+ const validPage = Math.max(1, Math.min(newPage, totalPages.value));
610
+ page.value = validPage;
611
+ pageInput.value = validPage.toString();
612
+ }
613
+
614
+ // Add throttle utility
615
+ const throttle = (fn: Function, delay: number) => {
616
+ let lastCall = 0;
617
+ return (...args: any[]) => {
618
+ const now = Date.now();
619
+ if (now - lastCall >= delay) {
620
+ lastCall = now;
621
+ fn(...args);
622
+ }
623
+ };
624
+ };
625
+
626
+ // Virtual scroll state
627
+ const containerRef = ref<HTMLElement | null>(null);
628
+ const scrollTop = ref(0);
629
+ const visibleRows = ref<any[]>([]);
630
+ const startIndex = ref(0);
631
+ const endIndex = ref(0);
632
+ const totalHeight = ref(0);
633
+ const spacerHeight = ref(0);
634
+ const rowHeightsMap = ref<{[key: string]: number}>({});
635
+ const rowPositions = ref<number[]>([]);
636
+
637
+ // Calculate row positions based on heights
638
+ const calculateRowPositions = () => {
639
+ if (!props.rows) return;
640
+
641
+ let currentPosition = 0;
642
+ rowPositions.value = props.rows.map((row) => {
643
+ const height = rowHeightsMap.value[`row_${row._primaryKeyValue}`] || props.itemHeight || 52.5;
644
+ const position = currentPosition;
645
+ currentPosition += height;
646
+ return position;
647
+ });
648
+ totalHeight.value = currentPosition;
649
+ };
650
+
651
+ // Calculate visible rows based on scroll position
652
+ const calculateVisibleRows = () => {
653
+ if (!props.rows?.length) {
654
+ visibleRows.value = props.rows || [];
655
+ return;
656
+ }
657
+
658
+ const buffer = props.bufferSize || 5;
659
+ const containerHeight = props.containerHeight || 900;
660
+
661
+ // For single item or small datasets, show all rows
662
+ if (props.rows.length <= buffer * 2 + 1) {
663
+ startIndex.value = 0;
664
+ endIndex.value = props.rows.length - 1;
665
+ visibleRows.value = props.rows;
666
+ spacerHeight.value = 0;
667
+ return;
668
+ }
669
+
670
+ // Binary search for start index
671
+ let low = 0;
672
+ let high = rowPositions.value.length - 1;
673
+ const targetPosition = scrollTop.value;
674
+
675
+ while (low <= high) {
676
+ const mid = Math.floor((low + high) / 2);
677
+ if (rowPositions.value[mid] <= targetPosition) {
678
+ low = mid + 1;
679
+ } else {
680
+ high = mid - 1;
681
+ }
682
+ }
683
+
684
+ const newStartIndex = Math.max(0, low - 1 - buffer);
685
+ const newEndIndex = Math.min(
686
+ props.rows.length - 1,
687
+ newStartIndex + Math.ceil(containerHeight / (props.itemHeight || 52.5)) + buffer * 2
688
+ );
689
+
690
+ // Ensure at least one row is visible
691
+ if (newEndIndex < newStartIndex) {
692
+ startIndex.value = 0;
693
+ endIndex.value = Math.min(props.rows.length - 1, Math.ceil(containerHeight / (props.itemHeight || 52.5)));
694
+ } else {
695
+ startIndex.value = newStartIndex;
696
+ endIndex.value = newEndIndex;
697
+ }
698
+
699
+ visibleRows.value = props.rows.slice(startIndex.value, endIndex.value + 1);
700
+ spacerHeight.value = startIndex.value > 0 ? rowPositions.value[startIndex.value - 1] : 0;
701
+ };
702
+
703
+ // Throttled scroll handler
704
+ const handleScroll = throttle((e: Event) => {
705
+ const target = e.target as HTMLElement;
706
+ scrollTop.value = target.scrollTop;
707
+ calculateVisibleRows();
708
+ }, 16);
709
+
710
+ // Update row height when it changes
711
+ const updateRowHeight = (rowId: string, height: number) => {
712
+ if (rowHeightsMap.value[rowId] !== height) {
713
+ rowHeightsMap.value[rowId] = height;
714
+ calculateRowPositions();
715
+ calculateVisibleRows();
716
+ }
717
+ };
718
+
719
+ // Watch for changes in rows
720
+ watch(() => props.rows, () => {
721
+ if (props.rows) {
722
+ calculateRowPositions();
723
+ calculateVisibleRows();
724
+ }
725
+ }, { immediate: true });
726
+
727
+
728
+
729
+ </script>
730
+
731
+ <style lang="scss" scoped>
732
+ input[type="checkbox"][disabled] {
733
+ @apply opacity-50;
734
+ }
735
+ input[type="checkbox"]:not([disabled]) {
736
+ @apply cursor-pointer;
737
+ }
738
+ </style>