@isoftdata/svelte-table 2.9.6 → 2.10.0-beta.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.
package/dist/Table.svelte CHANGED
@@ -1,1074 +1,1135 @@
1
- <script module>
2
- import { getBootstrapCdnVersion } from '@isoftdata/utility-bootstrap'
3
- let bs5 = $state(false)
4
- try {
5
- bs5 = getBootstrapCdnVersion() === 5
6
- } catch (e) {
7
- onMount(() => {
8
- bs5 = getBootstrapCdnVersion() === 5
9
- })
10
- }
11
- </script>
12
-
13
- <script
14
- lang="ts"
15
- generics="R extends UuidRowProps"
16
- >
17
- import type { Snippet } from 'svelte'
18
-
19
- import { ColumnInfoRunicStore } from './column-info-store.svelte'
20
-
21
- import type { i18n } from 'i18next'
22
- import type { Get } from 'type-fest'
23
- import {
24
- type Column as GenericColumn,
25
- type SortDirection,
26
- type UuidRowProps,
27
- type RowProperties,
28
- type FooterReducerValue,
29
- ARBITRARY_PROPERTY_PREFIX,
30
- type ArbitraryProperty,
31
- } from './'
32
-
33
- import { writable, type Writable } from 'svelte/store'
34
-
35
- import Pagination from './Pagination.svelte'
36
- import Td from './Td.svelte'
37
- import Input from '@isoftdata/svelte-input'
38
- import ContextMenu, { DropdownItem } from '@isoftdata/svelte-context-menu'
39
- import { v4 as uuid } from '@lukeed/uuid'
40
- import { format as formatCurrency } from '@isoftdata/utility-currency'
41
- import { setContext, tick, onMount, getContext, untrack } from 'svelte'
42
- import { on } from 'svelte/events'
43
- import naturalCompare from 'natural-compare-lite'
44
- import type { HTMLTableAttributes, ClassValue } from 'svelte/elements'
45
- import { translate as defaultTranslate } from '@isoftdata/utility-string'
46
-
47
- const { t: translate } = getContext<i18n>('i18next') || { t: defaultTranslate }
48
-
49
- type K = keyof R
50
- type KeyPath = RowProperties<R>
51
- type ColumnProperty = KeyPath | ArbitraryProperty
52
-
53
- type IndexedRow = R & { originalIndex: number; uuid: string }
54
- type GetRowProperty = Get<R, KeyPath>
55
- type GetIndexedRowProperty = Get<IndexedRow, RowProperties<IndexedRow>>
56
- type FooterValue = {
57
- property: ColumnProperty
58
- value: FooterReducerValue<R>
59
- }
60
- // Type hack so passing an array of columns without the type argument doesn't reduce the type of R to any
61
- type Column = GenericColumn<R & {}>
62
-
63
- interface Props extends Omit<HTMLTableAttributes, 'children' | `aria-${string}` | `bind:${string}` | `on:${string}`> {
64
- filterEnabled?: boolean
65
- columns: Array<Column>
66
- tableId?: string
67
- rows: Array<R>
68
- sortDirection?: SortDirection
69
- sortColumn?: Column | undefined
70
- class?: ClassValue
71
- perPageCount?: number
72
- currentPageNumber?: number
73
- showFooter?: boolean
74
- footers?: Array<FooterValue>
75
- lazySort?: boolean //when true, the table can only be resorted when the user clicks on a column header(not real time as data changes)
76
- idProp?: K | 'uuid' //required for lazy sorting.
77
- previousSortOrder?: Array<IndexedRow & { order: number }>
78
- filterProps?: Array<KeyPath> | false
79
- filter?: string
80
- filterLabel?: string
81
- filterPlaceholder?: string
82
- filterDisabled?: boolean
83
- filterReadonly?: boolean
84
- filterLazy?: boolean | number
85
- showFilterLabel?: boolean
86
- headerRowClass?: ClassValue
87
- headerColumnClass?: ClassValue
88
- filterColumnClass?: ClassValue
89
- columnHidingEnabled?: boolean
90
- selectionEnabled?: boolean
91
- // Array<number> | Array<string> | Array<R[K]> ???
92
- selectedRowIds?: Array<GetIndexedRowProperty>
93
- rowSelectionIdProp?: KeyPath | 'uuid'
94
- rowSelectionRequiresModKey?: boolean
95
- selectionMode?: 'SINGLE' | 'RANGE' | null
96
- multiSelectEnabled?: boolean
97
- lastClickedIdForRange?: GetIndexedRowProperty | undefined
98
- totalItemsCount?: number
99
- stickyHeader?: boolean
100
- responsive?: boolean | 'sm' | 'md' | 'lg' | 'xl'
101
- size?: 'sm' | ''
102
- striped?: boolean
103
- /** When true, enables tree-specific features. Required if you want to use `TreeRow`s. */
104
- tree?: boolean
105
- bordered?: boolean
106
- hover?: boolean
107
- previousSortColumn?: Column | undefined
108
- previousSortDirection?: SortDirection | undefined
109
- parentClass?: ClassValue
110
- parentStyle?: string
111
- hideButtonClass?: ClassValue
112
- columnResizingEnabled?: boolean
113
- /** If specified, where to serialize the columnInfo store */
114
- localStorageKey?: string | undefined
115
- columnClickedMethod?: typeof defaultColumnClicked
116
- filterMethod?: typeof defaultFilter
117
- rowMatchesFilterMethod?: typeof defaultRowMatchesFilter
118
- filteredRows?: Array<IndexedRow>
119
- sortedRows?: Array<IndexedRow>
120
- currentPageRows?: Array<IndexedRow>
121
- // snippets
122
- header?: Snippet
123
- body?: Snippet<[{ rows: Array<R & { originalIndex: number; uuid: string }>; visibleColumnsCount: number }]>
124
- children?: Snippet<[{ row: R & { originalIndex: number; uuid: string }; index: number }]>
125
- noRows?: Snippet<[{ visibleColumnsCount: number }]>
126
- footerRow?: Snippet<[{ footers: Array<FooterValue> }]>
127
- // callbacks
128
- pageChange?: (context: { pageNumber: number }) => void
129
- columnResizeEnd?: (context: { column: Column }) => void
130
- }
131
-
132
- const uid = $props.id()
133
-
134
- let {
135
- filterEnabled = false,
136
- columns,
137
- tableId = uid,
138
- rows,
139
- sortDirection = $bindable('ASC'),
140
- sortColumn = $bindable(undefined),
141
- class: tableClass = '',
142
- perPageCount = 0,
143
- currentPageNumber = $bindable(1),
144
- showFooter = false,
145
- lazySort = false,
146
- idProp = 'uuid',
147
- previousSortOrder = $bindable([]),
148
- filterProps = false,
149
- filter = $bindable(''),
150
- filterLabel = translate('common:filter', 'Filter'),
151
- filterPlaceholder = translate('common:filter', 'Filter'),
152
- filterDisabled = false,
153
- filterReadonly = false,
154
- filterLazy = false,
155
- showFilterLabel = false,
156
- headerRowClass = bs5 ? 'row' : 'form-row',
157
- headerColumnClass = 'col',
158
- filterColumnClass = 'col-lg-2 col-md-4 col-sm-6 align-self-end',
159
- columnHidingEnabled = false,
160
- selectionEnabled = false,
161
- selectedRowIds = $bindable([]),
162
- rowSelectionIdProp = 'uuid',
163
- rowSelectionRequiresModKey = false,
164
- selectionMode = $bindable(null),
165
- multiSelectEnabled = true,
166
- lastClickedIdForRange = $bindable(undefined),
167
- totalItemsCount = 0,
168
- stickyHeader = false,
169
- responsive = false,
170
- size = 'sm',
171
- striped = true,
172
- tree = false,
173
- bordered = true,
174
- hover = true,
175
- previousSortColumn = $bindable(undefined),
176
- previousSortDirection = $bindable(undefined),
177
- parentClass = '',
178
- parentStyle = '',
179
- hideButtonClass = '',
180
- columnResizingEnabled = false,
181
- localStorageKey = undefined,
182
- columnClickedMethod = defaultColumnClicked,
183
- filterMethod = defaultFilter,
184
- rowMatchesFilterMethod = defaultRowMatchesFilter,
185
- filteredRows = $bindable(filterMethod('', rows, columns)),
186
- sortedRows = $bindable(filteredRows),
187
- currentPageRows = $bindable(sortedRows.slice(0, perPageCount)),
188
- // snippets
189
- header,
190
- body,
191
- children,
192
- noRows,
193
- footerRow,
194
- // callbacks
195
- pageChange,
196
- columnResizeEnd,
197
- ...rest
198
- }: Props = $props()
199
-
200
- let lastPageNumber: number = $state(1)
201
- let paginationComponent: Pagination<IndexedRow> | undefined = $state()
202
- let contextMenu: ContextMenu | undefined = $state()
203
- let contextMenuColumn: Column | undefined = $state()
204
- let columnResizingIsActive: boolean = $state(false)
205
- /** Set to true after getting initial table widths when resizing enabled*/
206
- let useFixedLayout = $state(false)
207
- let tableParent: HTMLDivElement | undefined = $state()
208
-
209
- // Do the initial sort if they specified a default sort column
210
- const defaultSortColumn = columns.find(column => column.defaultSortColumn)
211
- if (defaultSortColumn) {
212
- sortColumn = defaultSortColumn
213
- sortDirection = defaultSortColumn.defaultSortDirection || 'ASC'
214
-
215
- doSort({
216
- rows: filteredRows,
217
- sortColumn,
218
- sortDirection,
219
- sameSortOrder: false,
220
- })
221
- }
222
-
223
- // Need to track which rows are expanded so when the rows reorder, they correctly keep that state.
224
- const expandedRows = writable<Record<number | string, boolean>>({})
225
- if (tree) {
226
- setContext('expandedRows', expandedRows)
227
- }
228
-
229
- // TODO: bad things will happen if we update any of our apps to use Runes for the store if we don't account for that here
230
- const session = getContext<Writable<{ userAccountId?: number }> | undefined>('session')
231
- // This will be accessible on the component instance, ie `const columnInfo = $derived(table?.columnInfo)` if you need it
232
- export const columnInfo = new ColumnInfoRunicStore<R>(() => columns, {
233
- key: localStorageKey,
234
- userAccountId: session && $session ? $session.userAccountId : undefined,
235
- })
236
- // Set as context so we can retrieve it in the Td component
237
- setContext('columnInfo', columnInfo)
238
- // Set as context so we can retrieve it in the TreeRow component
239
- const selectedRowIdsStore = writable(selectedRowIds)
240
- setContext('selectedRowIds', selectedRowIdsStore)
241
- setContext('idProp', idProp)
242
- setContext('columnResizingEnabled', writable(columnResizingEnabled))
243
- setContext('bs5', bs5)
244
-
245
- // #endregion
246
- // #region Functions
247
-
248
- function isArbitraryProperty(property: string | undefined): property is ArbitraryProperty {
249
- return !!property?.startsWith(ARBITRARY_PROPERTY_PREFIX)
250
- }
251
-
252
- export async function defaultColumnClicked(clickedColumn: Column, sortDirection: SortDirection) {
253
- if (currentPageNumber !== lastPageNumber) {
254
- paginationComponent?.setPageNumber(1)
255
- }
256
-
257
- doSort({
258
- rows: filteredRows,
259
- sortColumn: clickedColumn,
260
- sortDirection,
261
- sameSortOrder: false,
262
- })
263
- }
264
-
265
- function defaultFilter(filter: string, rows: R[], columns: Column[]): IndexedRow[] {
266
- const columnProps = columns
267
- ?.map(({ property }) => property)
268
- .filter((property): property is KeyPath => !isArbitraryProperty(property))
269
-
270
- if (filterEnabled) {
271
- const props = filterProps || columnProps
272
- return getFilterMatches(filter, rows, props)
273
- }
274
-
275
- return rows.map((row, index): IndexedRow => ({ ...row, originalIndex: index, uuid: row.uuid || uuid() }))
276
- }
277
-
278
- /** In normal mode, returns all rows matching the filter.
279
- *
280
- * In tree mode, shows all children if parent matches, or shows matching children and their children
281
- */
282
- function getFilterMatches(filter: string, theRows: Array<R>, props: KeyPath[]) {
283
- return theRows.reduce((rows, row, index): IndexedRow[] => {
284
- if (rowMatchesFilterMethod(filter, row, props)) {
285
- rows.push({ ...row, originalIndex: index, uuid: 'uuid' in row ? (row.uuid as string) : uuid() })
286
- } else if (tree && Array.isArray(row.children) && row.children.length) {
287
- const children = getFilterMatches(filter, row.children, props)
288
- if (children.length) {
289
- rows.push({ ...row, children, originalIndex: index, uuid: 'uuid' in row ? (row.uuid as string) : uuid() })
290
- }
291
- }
292
-
293
- return rows
294
- }, new Array<IndexedRow>())
295
- }
296
-
297
- export function defaultRowMatchesFilter(filter: string, row: R, props: Array<RowProperties<R>>) {
298
- return (
299
- !filter ||
300
- props.some(prop => {
301
- const value = getNestedProperty(row, prop)
302
- if (typeof value === 'string' || typeof value === 'number') {
303
- return value.toString().toUpperCase().indexOf(filter.toUpperCase()) > -1
304
- }
305
- return false
306
- })
307
- )
308
- }
309
-
310
- export function getNestedProperty(
311
- row: R & { uuid: string },
312
- path: KeyPath | 'uuid',
313
- defaultValue?: string | number,
314
- ): GetIndexedRowProperty
315
- export function getNestedProperty(row: R, path: KeyPath, defaultValue?: string | number): GetRowProperty
316
- export function getNestedProperty(
317
- row: R | (R & { uuid: string }),
318
- path: KeyPath | 'uuid',
319
- defaultValue?: string | number,
320
- ): GetRowProperty | GetIndexedRowProperty {
321
- if (!path) {
322
- return defaultValue as GetRowProperty
323
- }
324
-
325
- if (row[path]) {
326
- return row[path] as GetRowProperty
327
- }
328
-
329
- const val =
330
- path
331
- .split('[')
332
- // TODO figure out if we can do this less evilly
333
- .reduce((obj, prop) => (obj as any)?.[prop.replace(/\]/g, '')], row as any) ?? defaultValue
334
- return val
335
- }
336
-
337
- async function columnClickHandler(clickedColumn: Column) {
338
- if (clickedColumn.sortType === false || columnResizingIsActive || isArbitraryProperty(clickedColumn.property)) {
339
- return
340
- }
341
- sortDirection = sortDirection === 'ASC' && clickedColumn.property === sortColumn?.property ? 'DESC' : 'ASC'
342
-
343
- columnClickedMethod(clickedColumn, sortDirection)
344
-
345
- sortColumn = clickedColumn
346
- await tick()
347
- }
348
-
349
- export function expandRow(rowId: string | number, expanded = true) {
350
- if (tree) {
351
- $expandedRows[rowId] = expanded
352
- } else {
353
- console.warn('expandRow should only be used when the `tree` prop is set to true on the Table component.')
354
- }
355
- }
356
-
357
- export function rowIsSelected(
358
- row: IndexedRow,
359
- rowSelectionIdProp: KeyPath | 'uuid',
360
- selectedRowIds: Array<GetIndexedRowProperty>,
361
- ) {
362
- const selectionId = getNestedProperty(row, rowSelectionIdProp, row.uuid)
363
- return selectedRowIds.includes(selectionId)
364
- }
365
-
366
- export function rowClick(row: IndexedRow) {
367
- if (!selectionEnabled || !rowSelectionIdProp) {
368
- return
369
- }
370
- const clickedId = getNestedProperty(row, rowSelectionIdProp || 'uuid')
371
-
372
- const selectedIds = selectedRowIds
373
-
374
- let newSelectedIds: Array<GetIndexedRowProperty> = []
375
-
376
- if (!selectionMode && !rowSelectionRequiresModKey) {
377
- selectionMode = 'SINGLE'
378
- }
379
-
380
- if (selectionMode === 'SINGLE') {
381
- newSelectedIds = selectedIds
382
- const foundInExistingSelections = selectedIds.some(id => id === clickedId)
383
-
384
- if (foundInExistingSelections) {
385
- newSelectedIds = newSelectedIds.filter(id => id !== clickedId)
386
- } else if (multiSelectEnabled) {
387
- newSelectedIds = newSelectedIds.concat(clickedId)
388
- } else {
389
- newSelectedIds = [clickedId]
390
- }
391
- } else if (selectionMode === 'RANGE') {
392
- if (selectedIds.length < 1) {
393
- newSelectedIds = [clickedId]
394
- } else {
395
- newSelectedIds = selectedIds
396
- const clickedItemIndex = sortedRows.findIndex(item => {
397
- return item[rowSelectionIdProp] === clickedId
398
- })
399
-
400
- const lastSelectedIndex = sortedRows.findIndex(item => {
401
- return item[rowSelectionIdProp] === lastClickedIdForRange
402
- })
403
-
404
- let selectionDirection: 'UP' | 'DOWN' | undefined
405
-
406
- if (lastSelectedIndex > clickedItemIndex) {
407
- selectionDirection = 'UP'
408
- } else if (lastSelectedIndex < clickedItemIndex) {
409
- selectionDirection = 'DOWN'
410
- }
411
-
412
- if (selectionDirection) {
413
- sortedRows.forEach((item, index) => {
414
- if (selectionDirection === 'UP' && index <= lastSelectedIndex && index >= clickedItemIndex) {
415
- newSelectedIds = newSelectedIds.concat(getNestedProperty(item, rowSelectionIdProp))
416
- } else if (selectionDirection === 'DOWN' && index >= lastSelectedIndex && index <= clickedItemIndex) {
417
- newSelectedIds = newSelectedIds.concat(getNestedProperty(item, rowSelectionIdProp))
418
- }
419
- })
420
- }
421
- }
422
- } else {
423
- return
424
- }
425
-
426
- const clickedIdWasAdded = newSelectedIds.find(id => id === clickedId)
427
-
428
- let lastClickedId: GetIndexedRowProperty | undefined
429
-
430
- if (newSelectedIds.length === 0) {
431
- lastClickedId = undefined
432
- } else if (selectionMode === 'SINGLE' && clickedIdWasAdded) {
433
- lastClickedId = clickedId
434
- } else {
435
- lastClickedId = lastClickedIdForRange
436
- }
437
-
438
- selectedRowIds = [...new Set(newSelectedIds)]
439
- lastClickedIdForRange = lastClickedId
440
- }
441
- export function setPageVisibleByItemIndex(index: number) {
442
- if (index > -1) {
443
- paginationComponent?.setPageVisibleByItemIndex(index)
444
- }
445
- }
446
- export function setPageVisibleByItemId({ id, keyName }: { id: number | string; keyName: K }) {
447
- const index = sortedRows.findIndex(item => item[keyName] == id)
448
-
449
- if (index > -1) {
450
- paginationComponent?.setPageVisibleByItemIndex(index)
451
- }
452
- }
453
- export function doSort({
454
- rows,
455
- sortColumn,
456
- sortDirection,
457
- sameSortOrder,
458
- }: {
459
- rows: Array<IndexedRow>
460
- sortColumn: Column | undefined
461
- sortDirection: SortDirection
462
- sameSortOrder: boolean
463
- }) {
464
- const keepSameSortOrder = !!(lazySort && sameSortOrder && idProp)
465
- const hasPreviousSort = previousSortColumn && previousSortOrder.length > 0
466
-
467
- let sortProp: KeyPath
468
- if (
469
- keepSameSortOrder &&
470
- hasPreviousSort &&
471
- previousSortColumn &&
472
- idProp &&
473
- !isArbitraryProperty(previousSortColumn.property)
474
- ) {
475
- sortColumn = previousSortColumn
476
- sortProp = previousSortColumn.property
477
- sortDirection = 'ASC' //force ASC for keeping the same order because we defined the order in previousSortOrder
478
-
479
- rows = rows.map(row => {
480
- const foundItem = previousSortOrder.find(
481
- previousSortOrderRow => !!idProp && previousSortOrderRow[idProp] === row[idProp],
482
- )
483
-
484
- return { ...row, order: foundItem ? foundItem.order : 0 }
485
- })
486
- } else if (sortColumn?.property && !isArbitraryProperty(sortColumn.property)) {
487
- sortProp = sortColumn.property
488
- } else {
489
- // no sort property, can't sort
490
- sortedRows = rows
491
- return
492
- }
493
-
494
- function doTheSort(theRows: Array<IndexedRow>): Array<IndexedRow> {
495
- if (tree) {
496
- return theRows
497
- .slice()
498
- .map(row => {
499
- if (Array.isArray(row.children)) {
500
- return {
501
- ...row,
502
- children: doTheSort(row.children),
503
- }
504
- }
505
- return row
506
- })
507
- .sort((a: IndexedRow, b: IndexedRow) =>
508
- sortColumn?.sortType === 'ALPHA_NUM' ? alphaNumSort(a, b) : standardSort(a, b),
509
- )
510
- }
511
- return theRows.slice().sort(sortColumn?.sortType === 'ALPHA_NUM' ? alphaNumSort : standardSort)
512
- }
513
-
514
- const theSortedRows = doTheSort(rows)
515
-
516
- previousSortOrder = theSortedRows.map((row, index) => ({
517
- ...row,
518
- order: index,
519
- }))
520
- previousSortDirection = sortDirection
521
- previousSortColumn = sortColumn
522
- sortedRows = theSortedRows
523
- return
524
-
525
- function standardSort(a: R, b: R) {
526
- let aValue: Get<R, KeyPath> | string = getNestedProperty(a, sortProp, '')
527
- let bValue: Get<R, KeyPath> | string = getNestedProperty(b, sortProp, '')
528
-
529
- if (typeof aValue === 'string') {
530
- aValue = aValue.toUpperCase()
531
- }
532
- if (typeof bValue === 'string') {
533
- bValue = bValue.toUpperCase()
534
- }
535
-
536
- if (aValue < bValue) {
537
- return sortDirection === 'ASC' ? -1 : 1
538
- }
539
- if (aValue > bValue) {
540
- return sortDirection === 'ASC' ? 1 : -1
541
- }
542
-
543
- return 0
544
- }
545
-
546
- function alphaNumSort(a: R, b: R) {
547
- const aValue = getNestedProperty(a, sortProp, '')
548
- const bValue = getNestedProperty(b, sortProp, '')
549
- let compareResults = 0
550
- if (
551
- (typeof aValue === 'number' || typeof aValue === 'string') &&
552
- (typeof bValue === 'number' || typeof bValue === 'string')
553
- ) {
554
- compareResults = naturalCompare(aValue.toString(), bValue.toString())
555
- } else if (typeof aValue === 'number' || typeof aValue === 'string') {
556
- compareResults = 1
557
- } else if (typeof bValue === 'number' || typeof bValue === 'string') {
558
- compareResults = -1
559
- }
560
-
561
- return sortDirection === 'ASC' ? compareResults : compareResults * -1
562
- }
563
- }
564
-
565
- export function setColumnVisibility(columnProperties: Array<ColumnProperty> | ColumnProperty, visible: boolean) {
566
- if (!Array.isArray(columnProperties)) {
567
- columnProperties = [columnProperties]
568
- }
569
-
570
- try {
571
- untrack(() => {
572
- columnInfo.updateColumns(
573
- columnProperties.map((property): { property: ColumnProperty; visible: boolean } => ({
574
- property,
575
- visible,
576
- })),
577
- )
578
- })
579
- } catch (err) {
580
- console.error(`Error setting column visibility for columns ${JSON.stringify(columnProperties)}`, err)
581
- }
582
- }
583
-
584
- /** When called in `onMount` or an effect, will set the visibility of the specified columns whenever the value of `getVisible()` changes*/
585
- export function setColumnVisibilityWatch(
586
- columnProperties: Array<ColumnProperty> | ColumnProperty,
587
- getVisible: () => boolean,
588
- ) {
589
- $effect(() => {
590
- const visible = getVisible()
591
- setColumnVisibility(columnProperties, visible)
592
- })
593
- }
594
-
595
- function hideColumn(column: Column) {
596
- if (column) {
597
- columnInfo.updateColumn(column.property, { visible: false })
598
- }
599
- contextMenuColumn = undefined
600
- }
601
-
602
- function showColumn(column: Column) {
603
- if (column) {
604
- columnInfo.updateColumn(column.property, { visible: true })
605
- }
606
- contextMenuColumn = undefined
607
- }
608
-
609
- function contextMenuHandler(event: MouseEvent, column: string) {
610
- if ((columnHidingEnabled || columnResizingEnabled) && !event.ctrlKey) {
611
- event.preventDefault()
612
- contextMenuColumn = columnInfo.current[column]
613
- return contextMenu?.open(event, `col-${tableId}-${CSS.escape(column)}`)
614
- }
615
- return Promise.resolve()
616
- }
617
- // #endregion
618
- // #region Tree-specific functions
619
-
620
- // #endregion
621
-
622
- function onColumnMouseDown(event: MouseEvent, column: Column) {
623
- event.stopPropagation()
624
- if (!event.target || !(event.target instanceof HTMLDivElement)) {
625
- return
626
- }
627
-
628
- const htmlColumn = tableParent?.querySelector<HTMLTableCellElement>(
629
- `#col-${tableId}-${CSS.escape(column.property)}`,
630
- )
631
- const columnIndex = columns.findIndex(col => col.property === column.property)
632
- if (!htmlColumn || columnIndex === -1) {
633
- return
634
- }
635
-
636
- const mouseDownPageX = event.pageX
637
- const mouseDownColumnWidth = htmlColumn.offsetWidth
638
-
639
- event.target.classList.add('resizing')
640
- columnResizingIsActive = true
641
-
642
- const mouseMoveHandler = (mouseMoveEvent: MouseEvent) => {
643
- const widthDifference = mouseMoveEvent.pageX - mouseDownPageX
644
- const newWidth = `${mouseDownColumnWidth + widthDifference}px`
645
- htmlColumn.style.width = column.minWidth ? `max(${newWidth}, ${column.minWidth})` : newWidth
646
- }
647
-
648
- const mouseUpHandler = () => {
649
- if (!(event.target instanceof HTMLDivElement)) {
650
- return
651
- }
652
- event.target.classList.remove('resizing')
653
- columnInfo.updateColumn(column.property, { userWidth: htmlColumn.style.width })
654
-
655
- removeMouseMoveListener?.()
656
- removeMouseUpListener?.()
657
- columnResizeEnd?.({ column: columns[columnIndex] })
658
- // We want to ensure the user's mouseup event doesn't trigger a click event on the column header
659
- setTimeout(() => (columnResizingIsActive = false), 50)
660
- }
661
-
662
- const removeMouseMoveListener = tableParent ? on(tableParent, 'mousemove', mouseMoveHandler) : undefined
663
- const removeMouseUpListener = tableParent ? on(tableParent, 'mouseup', mouseUpHandler) : undefined
664
- }
665
-
666
- async function setDefaultColumnWidths() {
667
- useFixedLayout = false
668
- await tick()
669
- if (columnResizingEnabled) {
670
- columnInfo.updateColumns(
671
- columns.map((column): { property: ColumnProperty; userWidth: string } => {
672
- const thisColumnInfo = columnInfo.current[column.property]
673
- const prevUserWidth = thisColumnInfo?.userWidth
674
- // Minimum width will be inherited from the layout on reset, but this won't, so re-set it here if needed
675
- const explicitWidth = thisColumnInfo?.width
676
-
677
- return {
678
- property: column.property,
679
- userWidth: prevUserWidth ?? explicitWidth ?? getColumnThWidth(column.property),
680
- }
681
- }),
682
- )
683
- useFixedLayout = true
684
- }
685
- }
686
-
687
- function getColumnThWidth(property: string) {
688
- const thWidth =
689
- tableParent?.querySelector<HTMLTableCellElement>(`#col-${tableId}-${CSS.escape(property)}`)?.offsetWidth ?? 25
690
- return `${thWidth}px`
691
- }
692
- // #region Computed
693
- $effect(() => {
694
- selectedRowIdsStore.set(selectedRowIds)
695
- })
696
-
697
- $effect(() => {
698
- filteredRows = filterMethod(filter, rows, columns)
699
- // Should only run when filter/rows/columns changes since everything else is untracked
700
- untrack(() =>
701
- doSort({
702
- rows: filteredRows && Array.isArray(filteredRows) ? filteredRows : [],
703
- sortColumn: sortColumn,
704
- sortDirection: sortDirection,
705
- sameSortOrder: true,
706
- }),
707
- )
708
- })
709
-
710
- const responsiveClass = $derived(
711
- responsive === true ? 'table-responsive' : !!responsive ? `table-responsive-${responsive}` : '',
712
- )
713
- /** Includes information computed by the columnInfo store, including what we serialize to localstorage */
714
- const computedColumns = $derived(columnInfo.columns)
715
- const visibleColumns = $derived(computedColumns.filter(column => column.unhidable || column.visible))
716
- const hiddenColumns = $derived(computedColumns.filter(column => !column.unhidable && !column.visible))
717
- const showPagination = $derived((perPageCount && rows.length > perPageCount) || totalItemsCount > perPageCount)
718
-
719
- const footers = $derived(
720
- columns.map(column => {
721
- if (column.footer && showFooter) {
722
- const property = column.footer.altProperty ? column.footer.altProperty : column.property
723
- const mathFunctions = ['AVG', 'SUM']
724
-
725
- let columnValues: Array<number | GetRowProperty> = rows.map(row => {
726
- const value = isArbitraryProperty(property)
727
- ? ('' as Get<R, RowProperties<R>>)
728
- : getNestedProperty(row, property)
729
- if (
730
- column.footer?.fn &&
731
- typeof column.footer.fn === 'string' &&
732
- mathFunctions.includes(column.footer.fn) &&
733
- typeof value === 'string'
734
- ) {
735
- return parseFloat(value) || 0
736
- }
737
- return value
738
- })
739
-
740
- if (column.footer.requiredValue) {
741
- columnValues = columnValues.filter(value => value === column.footer?.requiredValue)
742
- }
743
-
744
- let value: FooterReducerValue<R>
745
-
746
- function sum(sum: number, val: unknown): number {
747
- if (typeof val === 'number') {
748
- return sum + val
749
- }
750
-
751
- if (typeof val === 'string') {
752
- const parsedVal = parseFloat(val)
753
- if (isNaN(parsedVal)) {
754
- return sum
755
- }
756
- return sum + parsedVal
757
- }
758
-
759
- return sum
760
- }
761
-
762
- switch (column.footer.fn) {
763
- case 'COUNT':
764
- value = columnValues.length
765
- break
766
- case 'AVG':
767
- value = (columnValues.reduce(sum, 0) / columnValues.length).toFixed(2)
768
- break
769
- case 'SUM':
770
- value = columnValues.reduce(sum, 0)
771
- break
772
- default:
773
- if (typeof column.footer.fn === 'function') {
774
- value = columnValues.reduce(column.footer.fn, column.footer.initialValue)
775
- } else {
776
- value = ''
777
- }
778
- break
779
- }
780
-
781
- return {
782
- property: column.property,
783
- value:
784
- column.footer.formatCurrency && (typeof value === 'string' || typeof value === 'number')
785
- ? formatCurrency(value)
786
- : value,
787
- }
788
- }
789
- return {
790
- property: column.property,
791
- value: '',
792
- }
793
- }),
794
- )
795
- //#endregion
796
- onMount(() => {
797
- setDefaultColumnWidths()
798
- })
799
- </script>
800
-
801
- {#if filterEnabled}
802
- <div class={headerRowClass}>
803
- <div class={headerColumnClass}>
804
- {@render header?.()}
805
- </div>
806
- <div class={filterColumnClass}>
807
- <Input
808
- label={filterLabel}
809
- showLabel={showFilterLabel}
810
- bind:value={filter}
811
- placeholder={filterPlaceholder}
812
- disabled={filterDisabled}
813
- readonly={filterReadonly || null}
814
- lazy={filterLazy}
815
- />
816
- <!-- || null is so svelte doesn't include readonly when it's falsy, probably a result of it being `restProps`'d on the input -->
817
- </div>
818
- </div>
819
- {/if}
820
-
821
- <!-- webkit-user-select: none; here fixes an issue in Safari where text selection of the column headers would happen while resizing the columns -->
822
- <div
823
- class={[parentClass, responsiveClass]}
824
- style={parentStyle}
825
- class:mb-3={showPagination}
826
- style:user-select={columnResizingIsActive ? 'none' : undefined}
827
- style:-webkit-user-select={columnResizingIsActive ? 'none' : undefined}
828
- style:cursor={columnResizingIsActive ? 'col-resize' : undefined}
829
- bind:this={tableParent}
830
- >
831
- <table
832
- id={tableId}
833
- class={['table mb-0', tableClass]}
834
- class:table-sm={size === 'sm'}
835
- class:table-layout-fixed={columnResizingEnabled && useFixedLayout}
836
- class:sticky={stickyHeader}
837
- class:table-striped={striped}
838
- class:table-bordered={bordered}
839
- class:table-hover={hover}
840
- {...rest}
841
- >
842
- <thead>
843
- <tr>
844
- {#each visibleColumns as column, index}
845
- {@const isSortColumn = sortColumn?.property === column.property}
846
- {@const doEllipsis = column.ellipsis || columnResizingEnabled}
847
-
848
- <th
849
- id="col-{tableId}-{column.property}"
850
- title={column.title ?? column.name}
851
- style:cursor={column.sortType !== false && !isArbitraryProperty(column.property) ? 'pointer' : ''}
852
- style:width={column.userWidth ?? column.width}
853
- style:min-width={column.minWidth}
854
- data-sort-specified={isSortColumn ? sortDirection : ''}
855
- class:text-nowrap={!column.wrap}
856
- class="unselectable {column.class ?? ''}"
857
- class:d-print-none={column.hideForPrint}
858
- class:text-left={!bs5 && column.align === 'left'}
859
- class:text-start={column.align === 'start' || (bs5 && column.align === 'left')}
860
- class:text-right={!bs5 && (column.align === 'right' || column.numeric)}
861
- class:text-end={column.align === 'end' || (bs5 && (column.align === 'right' || column.numeric))}
862
- class:text-center={column.align === 'center'}
863
- class:text-truncate={doEllipsis}
864
- class:bg-white={!bs5 && stickyHeader}
865
- onclick={() => columnClickHandler(column)}
866
- oncontextmenu={event => contextMenuHandler(event, column.property)}
867
- >
868
- <span
869
- class="text-truncate d-inline-block"
870
- style="vertical-align: bottom;"
871
- style:max-width={doEllipsis && isSortColumn ? 'calc(100% - 20px)' : '100%'}
872
- >
873
- <!-- Don't add any whitespace btwn these if blocks to keep the spaces correct -->
874
- {#if column.icon && column.iconLeft}
875
- <i class="fa-fw {column.iconPrefix || 'fas'} fa-{column.icon}"></i>{#if column.name}&nbsp;{/if}
876
- {/if}{column.name}{#if column.icon && !column.iconLeft}
877
- {#if column.name}&nbsp;{/if}<i class="fa-fw {column.iconPrefix || 'fas'} fa-{column.icon}"></i>
878
- {/if}
879
- </span>
880
- {#if isSortColumn}
881
- {sortDirection === 'ASC' ? '▲' : '▼'}
882
- {/if}
883
- {#if columnResizingEnabled && visibleColumns.length - 1 !== index}
884
- <!-- I think setting the role and aria-orientation attributes is the best we can do here -->
885
- <!-- svelte-ignore a11y_no_noninteractive_element_interactions -->
886
- <!-- svelte-ignore a11y_no_noninteractive_tabindex -->
887
- <!-- svelte-ignore a11y_click_events_have_key_events -->
888
- <div
889
- id="col-resizer-{tableId}-{column.property}"
890
- tabindex={0}
891
- role="separator"
892
- aria-orientation="horizontal"
893
- class="resizer"
894
- style="height: 100%;"
895
- onmousedown={e => onColumnMouseDown(e, column)}
896
- >
897
- &nbsp;
898
- </div>
899
- {/if}
900
- </th>
901
- {/each}
902
- </tr>
903
- </thead>
904
- <tbody class:unselectable={selectionMode === 'RANGE'}>
905
- {#snippet tableRow(row: R & { originalIndex: number; uuid: string }, index: number)}
906
- {#if children}
907
- {@render children({ row, index })}
908
- {:else}
909
- <tr
910
- class:table-primary={rowIsSelected(row, rowSelectionIdProp, selectedRowIds)}
911
- onclick={() => rowClick(row)}
912
- >
913
- {#each computedColumns as column (column.property)}
914
- <Td
915
- align={column.align}
916
- property={column.property}
917
- >
918
- {isArbitraryProperty(column.property)
919
- ? ''
920
- : column.formatter
921
- ? column.formatter(getNestedProperty(row, column.property, ''))
922
- : getNestedProperty(row, column.property, '')}
923
- </Td>
924
- {/each}
925
- </tr>
926
- {/if}
927
- {/snippet}
928
- {#if body}
929
- {@render body?.({ rows: currentPageRows, visibleColumnsCount: visibleColumns.length })}
930
- {:else if !currentPageRows.length}
931
- {@render noRows?.({ visibleColumnsCount: visibleColumns.length })}
932
- <!--
933
- Only key the #each if the idProp is in the original row.
934
- Otherwise, the table will re-render all rows if idProp is 'uuid' and your original row doesn't have one
935
- -->
936
- {:else if idProp in (rows[currentPageRows[0]?.originalIndex] ?? {})}
937
- {#each currentPageRows as row, index (row[idProp])}
938
- {@render tableRow(row, index)}
939
- {/each}
940
- {:else}
941
- {#each currentPageRows as row, index}
942
- {@render tableRow(row, index)}
943
- {/each}
944
- {/if}
945
- </tbody>
946
- {#if showFooter && footers}
947
- <tfoot>
948
- <tr class="table-secondary">
949
- {#if footerRow}
950
- {@render footerRow({ footers })}
951
- {:else}
952
- {#each footers as { property, value } (property)}
953
- <Td
954
- {property}
955
- tagName="th">{value ?? ''}</Td
956
- >
957
- {/each}
958
- {/if}
959
- </tr>
960
- </tfoot>
961
- {/if}
962
- </table>
963
- </div>
964
-
965
- <Pagination
966
- {perPageCount}
967
- {totalItemsCount}
968
- items={sortedRows}
969
- bind:currentPageItems={() => untrack(() => currentPageRows), items => (currentPageRows = items)}
970
- bind:currentPageNumber
971
- bind:lastPageNumber
972
- {pageChange}
973
- bind:this={paginationComponent}
974
- />
975
-
976
- <ContextMenu
977
- id="hide-column-context-menu"
978
- bind:this={contextMenu}
979
- >
980
- <button
981
- class={['dropdown-item', hideButtonClass]}
982
- class:disabled={contextMenuColumn?.unhidable}
983
- onclick={() => contextMenuColumn && hideColumn(contextMenuColumn)}
984
- >
985
- <i class="fas fa-eye-slash"></i>
986
- {#if contextMenuColumn?.name}
987
- {translate('common:tableHideColumn', 'Hide {{- columnName}}', { columnName: contextMenuColumn.name })}
988
- {:else if contextMenuColumn?.icon}
989
- {translate('common:hide', 'Hide')}
990
- <i class="{contextMenuColumn?.iconPrefix || 'fas'} fa-{contextMenuColumn?.icon}"></i>
991
- {/if}
992
- </button>
993
-
994
- {#if hiddenColumns.length > 0}
995
- <div class="dropdown-divider"></div>
996
- <h5 class="dropdown-header"><i class="fas fa-eye"></i> {translate('common:tableShowColumns', 'Show Column')}</h5>
997
- {/if}
998
- {#each hiddenColumns as hiddenColumn (hiddenColumn.property)}
999
- <button
1000
- class="dropdown-item"
1001
- onclick={() => showColumn(hiddenColumn)}
1002
- >
1003
- {#if hiddenColumn.name}
1004
- {hiddenColumn.name}
1005
- {:else if hiddenColumn.icon}
1006
- <i class="{hiddenColumn.iconPrefix || 'fas'} fa-{hiddenColumn.icon}"></i>
1007
- {/if}
1008
- </button>
1009
- {/each}
1010
- {#if columnResizingEnabled}
1011
- <div class="dropdown-divider"></div>
1012
- <DropdownItem
1013
- icon="broom"
1014
- onclick={() => {
1015
- columnInfo.updateColumns(
1016
- computedColumns.map(col => ({
1017
- property: col.property,
1018
- userWidth: undefined,
1019
- })),
1020
- )
1021
- setDefaultColumnWidths()
1022
- }}>{translate('common:tableResetColumnSizes', 'Reset Column Sizes')}</DropdownItem
1023
- >
1024
- {/if}
1025
- </ContextMenu>
1026
-
1027
- <style>
1028
- table.sticky {
1029
- position: relative;
1030
- }
1031
-
1032
- table.sticky th {
1033
- position: sticky;
1034
- top: 0;
1035
- box-shadow: 0 2px 2px -1px rgba(0, 0, 0, 0.4);
1036
- }
1037
-
1038
- table.sticky > thead > tr > th {
1039
- z-index: 3;
1040
- }
1041
-
1042
- /* Remove bottom margins created by form-groups in the table (BS4 only) */
1043
- table > tbody :global(.form-group) {
1044
- margin-bottom: 0%;
1045
- }
1046
-
1047
- /* BS5 removed .form-group, which makes it a little trickier to target the label's parent div, but this should do it
1048
- - The Label component adds mb-3 to the parent div if the label is hidden or mb-1 if it's shown (unlikely if it's in a table)
1049
- - That div will have a .input-group, .form-control, or .form-select as a direct descendant
1050
- */
1051
- table
1052
- > tbody
1053
- :global(
1054
- td div:where(.mb-3, .mb-1):not(.form-group):has(> :where(.input-group, .form-control, .form-select, .btn-group))
1055
- ) {
1056
- margin-bottom: 0% !important;
1057
- }
1058
-
1059
- .resizer {
1060
- position: absolute;
1061
- top: 0;
1062
- right: 0;
1063
- width: 10px;
1064
- cursor: col-resize;
1065
- user-select: none;
1066
- }
1067
- .resizer:hover,
1068
- .resizing {
1069
- border-right: 5px solid darkslategray;
1070
- }
1071
- .table-layout-fixed {
1072
- table-layout: fixed;
1073
- }
1074
- </style>
1
+ <script module>
2
+ import { getBootstrapCdnVersion } from '@isoftdata/utility-bootstrap'
3
+ let bs5 = $state(false)
4
+ try {
5
+ bs5 = getBootstrapCdnVersion() === 5
6
+ } catch (e) {
7
+ onMount(() => {
8
+ bs5 = getBootstrapCdnVersion() === 5
9
+ })
10
+ }
11
+ </script>
12
+
13
+ <script
14
+ lang="ts"
15
+ generics="R extends UuidRowProps"
16
+ >
17
+ import type { Snippet } from 'svelte'
18
+
19
+ import { ColumnInfoRunicStore } from './column-info-store.svelte'
20
+
21
+ import type { i18n } from 'i18next'
22
+ import type { Get } from 'type-fest'
23
+ import {
24
+ type Column as GenericColumn,
25
+ type SortDirection,
26
+ type UuidRowProps,
27
+ type RowProperties,
28
+ type FooterReducerValue,
29
+ ARBITRARY_PROPERTY_PREFIX,
30
+ type ArbitraryProperty,
31
+ } from './'
32
+
33
+ import { writable, type Writable } from 'svelte/store'
34
+
35
+ import Pagination from './Pagination.svelte'
36
+ import Td from './Td.svelte'
37
+ import Input from '@isoftdata/svelte-input'
38
+ import ContextMenu, { DropdownItem } from '@isoftdata/svelte-context-menu'
39
+ import { v4 as uuid } from '@lukeed/uuid'
40
+ import { format as formatCurrency } from '@isoftdata/utility-currency'
41
+ import { setContext, tick, onMount, getContext, untrack } from 'svelte'
42
+ import { on } from 'svelte/events'
43
+ import naturalCompare from 'natural-compare-lite'
44
+ import type { HTMLTableAttributes, ClassValue } from 'svelte/elements'
45
+ import { translate as defaultTranslate } from '@isoftdata/utility-string'
46
+ import Icon from '@isoftdata/svelte-icon'
47
+
48
+ const { t: translate } = getContext<i18n>('i18next') || { t: defaultTranslate }
49
+
50
+ type K = keyof R
51
+ type KeyPath = RowProperties<R>
52
+ type ColumnProperty = KeyPath | ArbitraryProperty
53
+
54
+ type IndexedRow = R & { originalIndex: number; uuid: string }
55
+ type GetRowProperty = Get<R, KeyPath>
56
+ type GetIndexedRowProperty = Get<IndexedRow, RowProperties<IndexedRow>>
57
+ type FooterValue = {
58
+ property: ColumnProperty
59
+ value: FooterReducerValue<R>
60
+ }
61
+ // Type hack so passing an array of columns without the type argument doesn't reduce the type of R to any
62
+ type Column = GenericColumn<R & {}>
63
+
64
+ interface Props extends Omit<HTMLTableAttributes, 'children' | `aria-${string}` | `bind:${string}` | `on:${string}`> {
65
+ filterEnabled?: boolean
66
+ columns: Array<Column>
67
+ tableId?: string
68
+ rows: Array<R>
69
+ sortDirection?: SortDirection
70
+ sortColumn?: Column | undefined
71
+ class?: ClassValue
72
+ perPageCount?: number
73
+ currentPageNumber?: number
74
+ showFooter?: boolean
75
+ footers?: Array<FooterValue>
76
+ lazySort?: boolean //when true, the table can only be resorted when the user clicks on a column header(not real time as data changes)
77
+ idProp?: K | 'uuid' //required for lazy sorting.
78
+ previousSortOrder?: Array<IndexedRow & { order: number }>
79
+ filterProps?: Array<KeyPath> | false
80
+ filter?: string
81
+ filterLabel?: string
82
+ filterPlaceholder?: string
83
+ filterDisabled?: boolean
84
+ filterReadonly?: boolean
85
+ filterLazy?: boolean | number
86
+ showFilterLabel?: boolean
87
+ headerRowClass?: ClassValue
88
+ headerColumnClass?: ClassValue
89
+ filterColumnClass?: ClassValue
90
+ columnHidingEnabled?: boolean
91
+ selectionEnabled?: boolean
92
+ // Array<number> | Array<string> | Array<R[K]> ???
93
+ selectedRowIds?: Array<GetIndexedRowProperty>
94
+ rowSelectionIdProp?: KeyPath | 'uuid'
95
+ rowSelectionRequiresModKey?: boolean
96
+ selectionMode?: 'SINGLE' | 'RANGE' | null
97
+ multiSelectEnabled?: boolean
98
+ lastClickedIdForRange?: GetIndexedRowProperty | undefined
99
+ totalItemsCount?: number
100
+ stickyHeader?: boolean
101
+ responsive?: boolean | 'sm' | 'md' | 'lg' | 'xl'
102
+ size?: 'sm' | ''
103
+ striped?: boolean
104
+ /** When true, enables tree-specific features. Required if you want to use `TreeRow`s. */
105
+ tree?: boolean
106
+ bordered?: boolean
107
+ hover?: boolean
108
+ previousSortColumn?: Column | undefined
109
+ previousSortDirection?: SortDirection | undefined
110
+ parentClass?: ClassValue
111
+ parentStyle?: string
112
+ hideButtonClass?: ClassValue
113
+ columnResizingEnabled?: boolean
114
+ columnPinningEnabled?: boolean
115
+ /** If specified, where to serialize the columnInfo store */
116
+ localStorageKey?: string | undefined
117
+ columnClickedMethod?: typeof defaultColumnClicked
118
+ filterMethod?: typeof defaultFilter
119
+ rowMatchesFilterMethod?: typeof defaultRowMatchesFilter
120
+ filteredRows?: Array<IndexedRow>
121
+ sortedRows?: Array<IndexedRow>
122
+ currentPageRows?: Array<IndexedRow>
123
+ // snippets
124
+ header?: Snippet
125
+ body?: Snippet<[{ rows: Array<R & { originalIndex: number; uuid: string }>; visibleColumnsCount: number }]>
126
+ children?: Snippet<[{ row: R & { originalIndex: number; uuid: string }; index: number }]>
127
+ noRows?: Snippet<[{ visibleColumnsCount: number }]>
128
+ footerRow?: Snippet<[{ footers: Array<FooterValue> }]>
129
+ // callbacks
130
+ pageChange?: (context: { pageNumber: number }) => void
131
+ columnResizeEnd?: (context: { column: Column }) => void
132
+ }
133
+
134
+ const uid = $props.id()
135
+
136
+ let {
137
+ filterEnabled = false,
138
+ columns,
139
+ tableId = uid,
140
+ rows,
141
+ sortDirection = $bindable('ASC'),
142
+ sortColumn = $bindable(undefined),
143
+ class: tableClass = '',
144
+ perPageCount = 0,
145
+ currentPageNumber = $bindable(1),
146
+ showFooter = false,
147
+ lazySort = false,
148
+ idProp = 'uuid',
149
+ previousSortOrder = $bindable([]),
150
+ filterProps = false,
151
+ filter = $bindable(''),
152
+ filterLabel = translate('common:filter', 'Filter'),
153
+ filterPlaceholder = translate('common:filter', 'Filter'),
154
+ filterDisabled = false,
155
+ filterReadonly = false,
156
+ filterLazy = false,
157
+ showFilterLabel = false,
158
+ headerRowClass = bs5 ? 'row' : 'form-row',
159
+ headerColumnClass = 'col',
160
+ filterColumnClass = 'col-lg-2 col-md-4 col-sm-6 align-self-end',
161
+ columnHidingEnabled = false,
162
+ selectionEnabled = false,
163
+ selectedRowIds = $bindable([]),
164
+ rowSelectionIdProp = 'uuid',
165
+ rowSelectionRequiresModKey = false,
166
+ selectionMode = $bindable(null),
167
+ multiSelectEnabled = true,
168
+ lastClickedIdForRange = $bindable(undefined),
169
+ totalItemsCount = 0,
170
+ stickyHeader = false,
171
+ responsive = false,
172
+ size = 'sm',
173
+ striped = true,
174
+ tree = false,
175
+ bordered = true,
176
+ hover = true,
177
+ previousSortColumn = $bindable(undefined),
178
+ previousSortDirection = $bindable(undefined),
179
+ parentClass = '',
180
+ parentStyle = '',
181
+ hideButtonClass = '',
182
+ columnResizingEnabled = false,
183
+ columnPinningEnabled = false,
184
+ localStorageKey = undefined,
185
+ columnClickedMethod = defaultColumnClicked,
186
+ filterMethod = defaultFilter,
187
+ rowMatchesFilterMethod = defaultRowMatchesFilter,
188
+ filteredRows = $bindable(filterMethod('', rows, columns)),
189
+ sortedRows = $bindable(filteredRows),
190
+ currentPageRows = $bindable(sortedRows.slice(0, perPageCount)),
191
+ // snippets
192
+ header,
193
+ body,
194
+ children,
195
+ noRows,
196
+ footerRow,
197
+ // callbacks
198
+ pageChange,
199
+ columnResizeEnd,
200
+ ...rest
201
+ }: Props = $props()
202
+
203
+ let lastPageNumber: number = $state(1)
204
+ let paginationComponent: Pagination<IndexedRow> | undefined = $state()
205
+ let contextMenu: ContextMenu | undefined = $state()
206
+ let contextMenuColumn: Column | undefined = $state()
207
+ let columnResizingIsActive: boolean = $state(false)
208
+ /** Set to true after getting initial table widths when resizing enabled*/
209
+ let useFixedLayout = $state(false)
210
+ let tableParent: HTMLDivElement | undefined = $state()
211
+
212
+ // Do the initial sort if they specified a default sort column
213
+ // svelte-ignore state_referenced_locally
214
+ const defaultSortColumn = columns.find(column => column.defaultSortColumn)
215
+ if (defaultSortColumn) {
216
+ sortColumn = defaultSortColumn
217
+ sortDirection = defaultSortColumn.defaultSortDirection || 'ASC'
218
+
219
+ doSort({
220
+ rows: filteredRows,
221
+ sortColumn,
222
+ sortDirection,
223
+ sameSortOrder: false,
224
+ })
225
+ }
226
+
227
+ // Need to track which rows are expanded so when the rows reorder, they correctly keep that state.
228
+ const expandedRows = writable<Record<number | string, boolean>>({})
229
+ // svelte-ignore state_referenced_locally
230
+ if (tree) {
231
+ setContext('expandedRows', expandedRows)
232
+ }
233
+
234
+ // TODO: bad things will happen if we update any of our apps to use Runes for the store if we don't account for that here
235
+ const session = getContext<Writable<{ userAccountId?: number }> | undefined>('session')
236
+ // This will be accessible on the component instance, ie `const columnInfo = $derived(table?.columnInfo)` if you need it
237
+ export const columnInfo = new ColumnInfoRunicStore<R>(() => columns, {
238
+ // svelte-ignore state_referenced_locally
239
+ key: localStorageKey,
240
+ userAccountId: session && $session ? $session.userAccountId : undefined,
241
+ })
242
+ // Set as context so we can retrieve it in the Td component
243
+ setContext('columnInfo', columnInfo)
244
+ // Set as context so we can retrieve it in the TreeRow component
245
+ const selectedRowIdsStore = writable(selectedRowIds)
246
+ setContext('selectedRowIds', selectedRowIdsStore)
247
+ // svelte-ignore state_referenced_locally
248
+ setContext('idProp', idProp)
249
+ // svelte-ignore state_referenced_locally
250
+ setContext('columnResizingEnabled', writable(columnResizingEnabled))
251
+ setContext('bs5', bs5)
252
+
253
+ // #endregion
254
+ // #region Functions
255
+
256
+ function isArbitraryProperty(property: string | undefined): property is ArbitraryProperty {
257
+ return !!property?.startsWith(ARBITRARY_PROPERTY_PREFIX)
258
+ }
259
+
260
+ export async function defaultColumnClicked(clickedColumn: Column, sortDirection: SortDirection) {
261
+ if (currentPageNumber !== lastPageNumber) {
262
+ paginationComponent?.setPageNumber(1)
263
+ }
264
+
265
+ doSort({
266
+ rows: filteredRows,
267
+ sortColumn: clickedColumn,
268
+ sortDirection,
269
+ sameSortOrder: false,
270
+ })
271
+ }
272
+
273
+ function defaultFilter(filter: string, rows: R[], columns: Column[]): IndexedRow[] {
274
+ const columnProps = columns
275
+ ?.map(({ property }) => property)
276
+ .filter((property): property is KeyPath => !isArbitraryProperty(property))
277
+
278
+ if (filterEnabled) {
279
+ const props = filterProps || columnProps
280
+ return getFilterMatches(filter, rows, props)
281
+ }
282
+
283
+ return rows.map((row, index): IndexedRow => ({ ...row, originalIndex: index, uuid: row.uuid || uuid() }))
284
+ }
285
+
286
+ /** In normal mode, returns all rows matching the filter.
287
+ *
288
+ * In tree mode, shows all children if parent matches, or shows matching children and their children
289
+ */
290
+ function getFilterMatches(filter: string, theRows: Array<R>, props: KeyPath[]) {
291
+ return theRows.reduce((rows, row, index): IndexedRow[] => {
292
+ if (rowMatchesFilterMethod(filter, row, props)) {
293
+ rows.push({ ...row, originalIndex: index, uuid: 'uuid' in row ? (row.uuid as string) : uuid() })
294
+ } else if (tree && Array.isArray(row.children) && row.children.length) {
295
+ const children = getFilterMatches(filter, row.children, props)
296
+ if (children.length) {
297
+ rows.push({ ...row, children, originalIndex: index, uuid: 'uuid' in row ? (row.uuid as string) : uuid() })
298
+ }
299
+ }
300
+
301
+ return rows
302
+ }, new Array<IndexedRow>())
303
+ }
304
+
305
+ export function defaultRowMatchesFilter(filter: string, row: R, props: Array<RowProperties<R>>) {
306
+ return (
307
+ !filter ||
308
+ props.some(prop => {
309
+ const value = getNestedProperty(row, prop)
310
+ if (typeof value === 'string' || typeof value === 'number') {
311
+ return value.toString().toUpperCase().indexOf(filter.toUpperCase()) > -1
312
+ }
313
+ return false
314
+ })
315
+ )
316
+ }
317
+
318
+ export function getNestedProperty(
319
+ row: R & { uuid: string },
320
+ path: KeyPath | 'uuid',
321
+ defaultValue?: string | number,
322
+ ): GetIndexedRowProperty
323
+ export function getNestedProperty(row: R, path: KeyPath, defaultValue?: string | number): GetRowProperty
324
+ export function getNestedProperty(
325
+ row: R | (R & { uuid: string }),
326
+ path: KeyPath | 'uuid',
327
+ defaultValue?: string | number,
328
+ ): GetRowProperty | GetIndexedRowProperty {
329
+ if (!path) {
330
+ return defaultValue as GetRowProperty
331
+ }
332
+
333
+ if (row[path]) {
334
+ return row[path] as GetRowProperty
335
+ }
336
+
337
+ const val =
338
+ path
339
+ .split('[')
340
+ // TODO figure out if we can do this less evilly
341
+ .reduce((obj, prop) => (obj as any)?.[prop.replace(/\]/g, '')], row as any) ?? defaultValue
342
+ return val
343
+ }
344
+
345
+ async function columnClickHandler(clickedColumn: Column) {
346
+ if (clickedColumn.sortType === false || columnResizingIsActive || isArbitraryProperty(clickedColumn.property)) {
347
+ return
348
+ }
349
+ sortDirection = sortDirection === 'ASC' && clickedColumn.property === sortColumn?.property ? 'DESC' : 'ASC'
350
+
351
+ columnClickedMethod(clickedColumn, sortDirection)
352
+
353
+ sortColumn = clickedColumn
354
+ await tick()
355
+ }
356
+
357
+ export function expandRow(rowId: string | number, expanded = true) {
358
+ if (tree) {
359
+ $expandedRows[rowId] = expanded
360
+ } else {
361
+ console.warn('expandRow should only be used when the `tree` prop is set to true on the Table component.')
362
+ }
363
+ }
364
+
365
+ export function rowIsSelected(
366
+ row: IndexedRow,
367
+ rowSelectionIdProp: KeyPath | 'uuid',
368
+ selectedRowIds: Array<GetIndexedRowProperty>,
369
+ ) {
370
+ const selectionId = getNestedProperty(row, rowSelectionIdProp, row.uuid)
371
+ return selectedRowIds.includes(selectionId)
372
+ }
373
+
374
+ export function rowClick(row: IndexedRow) {
375
+ if (!selectionEnabled || !rowSelectionIdProp) {
376
+ return
377
+ }
378
+ const clickedId = getNestedProperty(row, rowSelectionIdProp || 'uuid')
379
+
380
+ const selectedIds = selectedRowIds
381
+
382
+ let newSelectedIds: Array<GetIndexedRowProperty> = []
383
+
384
+ if (!selectionMode && !rowSelectionRequiresModKey) {
385
+ selectionMode = 'SINGLE'
386
+ }
387
+
388
+ if (selectionMode === 'SINGLE') {
389
+ newSelectedIds = selectedIds
390
+ const foundInExistingSelections = selectedIds.some(id => id === clickedId)
391
+
392
+ if (foundInExistingSelections) {
393
+ newSelectedIds = newSelectedIds.filter(id => id !== clickedId)
394
+ } else if (multiSelectEnabled) {
395
+ newSelectedIds = newSelectedIds.concat(clickedId)
396
+ } else {
397
+ newSelectedIds = [clickedId]
398
+ }
399
+ } else if (selectionMode === 'RANGE') {
400
+ if (selectedIds.length < 1) {
401
+ newSelectedIds = [clickedId]
402
+ } else {
403
+ newSelectedIds = selectedIds
404
+ const clickedItemIndex = sortedRows.findIndex(item => {
405
+ return item[rowSelectionIdProp] === clickedId
406
+ })
407
+
408
+ const lastSelectedIndex = sortedRows.findIndex(item => {
409
+ return item[rowSelectionIdProp] === lastClickedIdForRange
410
+ })
411
+
412
+ let selectionDirection: 'UP' | 'DOWN' | undefined
413
+
414
+ if (lastSelectedIndex > clickedItemIndex) {
415
+ selectionDirection = 'UP'
416
+ } else if (lastSelectedIndex < clickedItemIndex) {
417
+ selectionDirection = 'DOWN'
418
+ }
419
+
420
+ if (selectionDirection) {
421
+ sortedRows.forEach((item, index) => {
422
+ if (selectionDirection === 'UP' && index <= lastSelectedIndex && index >= clickedItemIndex) {
423
+ newSelectedIds = newSelectedIds.concat(getNestedProperty(item, rowSelectionIdProp))
424
+ } else if (selectionDirection === 'DOWN' && index >= lastSelectedIndex && index <= clickedItemIndex) {
425
+ newSelectedIds = newSelectedIds.concat(getNestedProperty(item, rowSelectionIdProp))
426
+ }
427
+ })
428
+ }
429
+ }
430
+ } else {
431
+ return
432
+ }
433
+
434
+ const clickedIdWasAdded = newSelectedIds.find(id => id === clickedId)
435
+
436
+ let lastClickedId: GetIndexedRowProperty | undefined
437
+
438
+ if (newSelectedIds.length === 0) {
439
+ lastClickedId = undefined
440
+ } else if (selectionMode === 'SINGLE' && clickedIdWasAdded) {
441
+ lastClickedId = clickedId
442
+ } else {
443
+ lastClickedId = lastClickedIdForRange
444
+ }
445
+
446
+ selectedRowIds = [...new Set(newSelectedIds)]
447
+ lastClickedIdForRange = lastClickedId
448
+ }
449
+ export function setPageVisibleByItemIndex(index: number) {
450
+ if (index > -1) {
451
+ paginationComponent?.setPageVisibleByItemIndex(index)
452
+ }
453
+ }
454
+ export function setPageVisibleByItemId({ id, keyName }: { id: number | string; keyName: K }) {
455
+ const index = sortedRows.findIndex(item => item[keyName] == id)
456
+
457
+ if (index > -1) {
458
+ paginationComponent?.setPageVisibleByItemIndex(index)
459
+ }
460
+ }
461
+ export function doSort({
462
+ rows,
463
+ sortColumn,
464
+ sortDirection,
465
+ sameSortOrder,
466
+ }: {
467
+ rows: Array<IndexedRow>
468
+ sortColumn: Column | undefined
469
+ sortDirection: SortDirection
470
+ sameSortOrder: boolean
471
+ }) {
472
+ const keepSameSortOrder = !!(lazySort && sameSortOrder && idProp)
473
+ const hasPreviousSort = previousSortColumn && previousSortOrder.length > 0
474
+
475
+ let sortProp: KeyPath
476
+ if (
477
+ keepSameSortOrder &&
478
+ hasPreviousSort &&
479
+ previousSortColumn &&
480
+ idProp &&
481
+ !isArbitraryProperty(previousSortColumn.property)
482
+ ) {
483
+ sortColumn = previousSortColumn
484
+ sortProp = previousSortColumn.property
485
+ sortDirection = 'ASC' //force ASC for keeping the same order because we defined the order in previousSortOrder
486
+
487
+ rows = rows.map(row => {
488
+ const foundItem = previousSortOrder.find(
489
+ previousSortOrderRow => !!idProp && previousSortOrderRow[idProp] === row[idProp],
490
+ )
491
+
492
+ return { ...row, order: foundItem ? foundItem.order : 0 }
493
+ })
494
+ } else if (sortColumn?.property && !isArbitraryProperty(sortColumn.property)) {
495
+ sortProp = sortColumn.property
496
+ } else {
497
+ // no sort property, can't sort
498
+ sortedRows = rows
499
+ return
500
+ }
501
+
502
+ function doTheSort(theRows: Array<IndexedRow>): Array<IndexedRow> {
503
+ if (tree) {
504
+ return theRows
505
+ .slice()
506
+ .map(row => {
507
+ if (Array.isArray(row.children)) {
508
+ return {
509
+ ...row,
510
+ children: doTheSort(row.children),
511
+ }
512
+ }
513
+ return row
514
+ })
515
+ .sort((a: IndexedRow, b: IndexedRow) =>
516
+ sortColumn?.sortType === 'ALPHA_NUM' ? alphaNumSort(a, b) : standardSort(a, b),
517
+ )
518
+ }
519
+ return theRows.slice().sort(sortColumn?.sortType === 'ALPHA_NUM' ? alphaNumSort : standardSort)
520
+ }
521
+
522
+ const theSortedRows = doTheSort(rows)
523
+
524
+ previousSortOrder = theSortedRows.map((row, index) => ({
525
+ ...row,
526
+ order: index,
527
+ }))
528
+ previousSortDirection = sortDirection
529
+ previousSortColumn = sortColumn
530
+ sortedRows = theSortedRows
531
+ return
532
+
533
+ function standardSort(a: R, b: R) {
534
+ let aValue: Get<R, KeyPath> | string = getNestedProperty(a, sortProp, '')
535
+ let bValue: Get<R, KeyPath> | string = getNestedProperty(b, sortProp, '')
536
+
537
+ if (typeof aValue === 'string') {
538
+ aValue = aValue.toUpperCase()
539
+ }
540
+ if (typeof bValue === 'string') {
541
+ bValue = bValue.toUpperCase()
542
+ }
543
+
544
+ if (aValue < bValue) {
545
+ return sortDirection === 'ASC' ? -1 : 1
546
+ }
547
+ if (aValue > bValue) {
548
+ return sortDirection === 'ASC' ? 1 : -1
549
+ }
550
+
551
+ return 0
552
+ }
553
+
554
+ function alphaNumSort(a: R, b: R) {
555
+ const aValue = getNestedProperty(a, sortProp, '')
556
+ const bValue = getNestedProperty(b, sortProp, '')
557
+ let compareResults = 0
558
+ if (
559
+ (typeof aValue === 'number' || typeof aValue === 'string') &&
560
+ (typeof bValue === 'number' || typeof bValue === 'string')
561
+ ) {
562
+ compareResults = naturalCompare(aValue.toString(), bValue.toString())
563
+ } else if (typeof aValue === 'number' || typeof aValue === 'string') {
564
+ compareResults = 1
565
+ } else if (typeof bValue === 'number' || typeof bValue === 'string') {
566
+ compareResults = -1
567
+ }
568
+
569
+ return sortDirection === 'ASC' ? compareResults : compareResults * -1
570
+ }
571
+ }
572
+
573
+ export function setColumnVisibility(columnProperties: Array<ColumnProperty> | ColumnProperty, visible: boolean) {
574
+ if (!Array.isArray(columnProperties)) {
575
+ columnProperties = [columnProperties]
576
+ }
577
+
578
+ try {
579
+ untrack(() => {
580
+ columnInfo.updateColumns(
581
+ columnProperties.map((property): { property: ColumnProperty; visible: boolean } => ({
582
+ property,
583
+ visible,
584
+ })),
585
+ )
586
+ })
587
+ } catch (err) {
588
+ console.error(`Error setting column visibility for columns ${JSON.stringify(columnProperties)}`, err)
589
+ }
590
+ }
591
+
592
+ /** When called in `onMount` or an effect, will set the visibility of the specified columns whenever the value of `getVisible()` changes*/
593
+ export function setColumnVisibilityWatch(
594
+ columnProperties: Array<ColumnProperty> | ColumnProperty,
595
+ getVisible: () => boolean,
596
+ ) {
597
+ $effect(() => {
598
+ const visible = getVisible()
599
+ setColumnVisibility(columnProperties, visible)
600
+ })
601
+ }
602
+
603
+ function hideColumn(column: Column) {
604
+ if (column) {
605
+ columnInfo.updateColumn(column.property, { visible: false })
606
+ }
607
+ contextMenuColumn = undefined
608
+ }
609
+
610
+ function showColumn(column: Column) {
611
+ if (column) {
612
+ columnInfo.updateColumn(column.property, { visible: true })
613
+ }
614
+ contextMenuColumn = undefined
615
+ }
616
+
617
+ function pinUnpinColumn(column: Column) {
618
+ columnInfo.updateColumn(column.property, { pinned: !column.pinned })
619
+ }
620
+
621
+ function contextMenuHandler(event: MouseEvent, column: string) {
622
+ if ((columnHidingEnabled || columnResizingEnabled || columnPinningEnabled) && !event.ctrlKey) {
623
+ event.preventDefault()
624
+ contextMenuColumn = columnInfo.current[column]
625
+ return contextMenu?.open(event, `col-${tableId}-${CSS.escape(column)}`)
626
+ }
627
+ return Promise.resolve()
628
+ }
629
+ // #endregion
630
+ // #region Tree-specific functions
631
+
632
+ // #endregion
633
+
634
+ function onColumnMouseDown(event: MouseEvent, column: Column) {
635
+ event.stopPropagation()
636
+ if (!event.target || !(event.target instanceof HTMLDivElement)) {
637
+ return
638
+ }
639
+
640
+ const htmlColumn = tableParent?.querySelector<HTMLTableCellElement>(
641
+ `#col-${tableId}-${CSS.escape(column.property)}`,
642
+ )
643
+ const columnIndex = columns.findIndex(col => col.property === column.property)
644
+ if (!htmlColumn || columnIndex === -1) {
645
+ return
646
+ }
647
+
648
+ const mouseDownPageX = event.pageX
649
+ const mouseDownColumnWidth = htmlColumn.offsetWidth
650
+
651
+ event.target.classList.add('resizing')
652
+ columnResizingIsActive = true
653
+
654
+ const mouseMoveHandler = (mouseMoveEvent: MouseEvent) => {
655
+ const widthDifference = mouseMoveEvent.pageX - mouseDownPageX
656
+ const newWidth = `${mouseDownColumnWidth + widthDifference}px`
657
+ htmlColumn.style.width = column.minWidth ? `max(${newWidth}, ${column.minWidth})` : newWidth
658
+ }
659
+
660
+ const mouseUpHandler = () => {
661
+ if (!(event.target instanceof HTMLDivElement)) {
662
+ return
663
+ }
664
+ event.target.classList.remove('resizing')
665
+ columnInfo.updateColumn(column.property, { userWidth: htmlColumn.style.width })
666
+
667
+ removeMouseMoveListener?.()
668
+ removeMouseUpListener?.()
669
+ columnResizeEnd?.({ column: columns[columnIndex] })
670
+ // We want to ensure the user's mouseup event doesn't trigger a click event on the column header
671
+ setTimeout(() => (columnResizingIsActive = false), 50)
672
+ }
673
+
674
+ const removeMouseMoveListener = tableParent ? on(tableParent, 'mousemove', mouseMoveHandler) : undefined
675
+ const removeMouseUpListener = tableParent ? on(tableParent, 'mouseup', mouseUpHandler) : undefined
676
+ }
677
+
678
+ async function setDefaultColumnWidths() {
679
+ useFixedLayout = false
680
+ await tick()
681
+ if (columnResizingEnabled) {
682
+ columnInfo.updateColumns(
683
+ columns.map((column): { property: ColumnProperty; userWidth: string } => {
684
+ const thisColumnInfo = columnInfo.current[column.property]
685
+ const prevUserWidth = thisColumnInfo?.userWidth
686
+ // Minimum width will be inherited from the layout on reset, but this won't, so re-set it here if needed
687
+ const explicitWidth = thisColumnInfo?.width
688
+
689
+ return {
690
+ property: column.property,
691
+ userWidth: prevUserWidth ?? explicitWidth ?? getColumnThWidth(column.property),
692
+ }
693
+ }),
694
+ )
695
+ useFixedLayout = true
696
+ }
697
+ }
698
+
699
+ function getColumnThWidth(property: string) {
700
+ const thWidth =
701
+ tableParent?.querySelector<HTMLTableCellElement>(`#col-${tableId}-${CSS.escape(property)}`)?.offsetWidth ?? 25
702
+ return `${thWidth}px`
703
+ }
704
+ // #region Computed
705
+ $effect(() => {
706
+ selectedRowIdsStore.set(selectedRowIds)
707
+ })
708
+
709
+ $effect(() => {
710
+ filteredRows = filterMethod(filter, rows, columns)
711
+ // Should only run when filter/rows/columns changes since everything else is untracked
712
+ untrack(() =>
713
+ doSort({
714
+ rows: filteredRows && Array.isArray(filteredRows) ? filteredRows : [],
715
+ sortColumn: sortColumn,
716
+ sortDirection: sortDirection,
717
+ sameSortOrder: true,
718
+ }),
719
+ )
720
+ })
721
+
722
+ const responsiveClass = $derived(
723
+ responsive === true ? 'table-responsive' : !!responsive ? `table-responsive-${responsive}` : '',
724
+ )
725
+ /** Includes information computed by the columnInfo store, including what we serialize to localstorage */
726
+ const computedColumns = $derived(columnInfo.columns)
727
+ const visibleColumns = $derived(computedColumns.filter(column => column.unhidable || column.visible))
728
+ const hiddenColumns = $derived(computedColumns.filter(column => !column.unhidable && !column.visible))
729
+ const showPagination = $derived((perPageCount && rows.length > perPageCount) || totalItemsCount > perPageCount)
730
+
731
+ const footers = $derived(
732
+ columns.map(column => {
733
+ if (column.footer && showFooter) {
734
+ const property = column.footer.altProperty ? column.footer.altProperty : column.property
735
+ const mathFunctions = ['AVG', 'SUM']
736
+
737
+ let columnValues: Array<number | GetRowProperty> = rows.map(row => {
738
+ const value = isArbitraryProperty(property)
739
+ ? ('' as Get<R, RowProperties<R>>)
740
+ : getNestedProperty(row, property)
741
+ if (
742
+ column.footer?.fn &&
743
+ typeof column.footer.fn === 'string' &&
744
+ mathFunctions.includes(column.footer.fn) &&
745
+ typeof value === 'string'
746
+ ) {
747
+ return parseFloat(value) || 0
748
+ }
749
+ return value
750
+ })
751
+
752
+ if (column.footer.requiredValue) {
753
+ columnValues = columnValues.filter(value => value === column.footer?.requiredValue)
754
+ }
755
+
756
+ let value: FooterReducerValue<R>
757
+
758
+ function sum(sum: number, val: unknown): number {
759
+ if (typeof val === 'number') {
760
+ return sum + val
761
+ }
762
+
763
+ if (typeof val === 'string') {
764
+ const parsedVal = parseFloat(val)
765
+ if (isNaN(parsedVal)) {
766
+ return sum
767
+ }
768
+ return sum + parsedVal
769
+ }
770
+
771
+ return sum
772
+ }
773
+
774
+ switch (column.footer.fn) {
775
+ case 'COUNT':
776
+ value = columnValues.length
777
+ break
778
+ case 'AVG':
779
+ value = (columnValues.reduce(sum, 0) / columnValues.length).toFixed(2)
780
+ break
781
+ case 'SUM':
782
+ value = columnValues.reduce(sum, 0)
783
+ break
784
+ default:
785
+ if (typeof column.footer.fn === 'function') {
786
+ value = columnValues.reduce(column.footer.fn, column.footer.initialValue)
787
+ } else {
788
+ value = ''
789
+ }
790
+ break
791
+ }
792
+
793
+ return {
794
+ property: column.property,
795
+ value:
796
+ column.footer.formatCurrency && (typeof value === 'string' || typeof value === 'number')
797
+ ? formatCurrency(value)
798
+ : value,
799
+ }
800
+ }
801
+ return {
802
+ property: column.property,
803
+ value: '',
804
+ }
805
+ }),
806
+ )
807
+ //#endregion
808
+ onMount(() => {
809
+ setDefaultColumnWidths()
810
+ })
811
+ </script>
812
+
813
+ {#if filterEnabled}
814
+ <div class={headerRowClass}>
815
+ <div class={headerColumnClass}>
816
+ {@render header?.()}
817
+ </div>
818
+ <div class={filterColumnClass}>
819
+ <Input
820
+ label={filterLabel}
821
+ showLabel={showFilterLabel}
822
+ bind:value={filter}
823
+ placeholder={filterPlaceholder}
824
+ disabled={filterDisabled}
825
+ readonly={filterReadonly || null}
826
+ lazy={filterLazy}
827
+ />
828
+ <!-- || null is so svelte doesn't include readonly when it's falsy, probably a result of it being `restProps`'d on the input -->
829
+ </div>
830
+ </div>
831
+ {/if}
832
+
833
+ <!-- webkit-user-select: none; here fixes an issue in Safari where text selection of the column headers would happen while resizing the columns -->
834
+ <div
835
+ class={[parentClass, responsiveClass]}
836
+ style={parentStyle}
837
+ class:mb-3={showPagination}
838
+ style:user-select={columnResizingIsActive ? 'none' : undefined}
839
+ style:-webkit-user-select={columnResizingIsActive ? 'none' : undefined}
840
+ style:cursor={columnResizingIsActive ? 'col-resize' : undefined}
841
+ bind:this={tableParent}
842
+ >
843
+ <table
844
+ id={tableId}
845
+ class={['table mb-0', tableClass]}
846
+ class:table-sm={size === 'sm'}
847
+ class:table-layout-fixed={columnResizingEnabled && useFixedLayout}
848
+ class:sticky={stickyHeader}
849
+ class:table-striped={striped}
850
+ class:table-bordered={bordered}
851
+ class:table-hover={hover}
852
+ {...rest}
853
+ >
854
+ <thead>
855
+ <tr>
856
+ {#each visibleColumns as column, index}
857
+ {@const isSortColumn = sortColumn?.property === column.property}
858
+ {@const doEllipsis = column.ellipsis || columnResizingEnabled}
859
+
860
+ <th
861
+ id="col-{tableId}-{column.property}"
862
+ title={column.title ?? column.name}
863
+ style:cursor={column.sortType !== false && !isArbitraryProperty(column.property) ? 'pointer' : ''}
864
+ style:width={column.userWidth ?? column.width}
865
+ style:min-width={column.minWidth}
866
+ style:position={column.pinned ? 'sticky' : undefined}
867
+ style:left={column.pinned ? columnInfo.getPinnedColumnOffset(column.property) : undefined}
868
+ style:z-index={column.pinned ? 5 : undefined}
869
+ data-sort-specified={isSortColumn ? sortDirection : ''}
870
+ class:text-nowrap={!column.wrap}
871
+ class="unselectable {column.class ?? ''}"
872
+ class:d-print-none={column.hideForPrint}
873
+ class:text-left={!bs5 && column.align === 'left'}
874
+ class:text-start={column.align === 'start' || (bs5 && column.align === 'left')}
875
+ class:text-right={!bs5 && (column.align === 'right' || column.numeric)}
876
+ class:text-end={column.align === 'end' || (bs5 && (column.align === 'right' || column.numeric))}
877
+ class:text-center={column.align === 'center'}
878
+ class:text-truncate={doEllipsis}
879
+ class:bg-white={!bs5 && stickyHeader}
880
+ class:table-secondary={column.pinned}
881
+ onclick={() => columnClickHandler(column)}
882
+ oncontextmenu={event => contextMenuHandler(event, column.property)}
883
+ >
884
+ <span
885
+ class="text-truncate d-inline-block"
886
+ style="vertical-align: bottom;"
887
+ style:max-width={doEllipsis && isSortColumn ? 'calc(100% - 20px)' : '100%'}
888
+ >
889
+ {@render columnNameAndIcon(column)}
890
+ </span>
891
+ <!-- I wonder how to make everything after this move to the right...? -->
892
+ {#if isSortColumn}
893
+ {sortDirection === 'ASC' ? '▲' : '▼'}
894
+ {/if}
895
+ {#if column.pinned}
896
+ <Icon
897
+ class=""
898
+ icon="thumbtack-angle"
899
+ />
900
+ {/if}
901
+ {#if columnResizingEnabled && visibleColumns.length - 1 !== index}
902
+ <!-- I think setting the role and aria-orientation attributes is the best we can do here -->
903
+ <!-- svelte-ignore a11y_no_noninteractive_element_interactions -->
904
+ <!-- svelte-ignore a11y_no_noninteractive_tabindex -->
905
+ <!-- svelte-ignore a11y_click_events_have_key_events -->
906
+ <div
907
+ id="col-resizer-{tableId}-{column.property}"
908
+ tabindex={0}
909
+ role="separator"
910
+ aria-orientation="horizontal"
911
+ class="resizer"
912
+ style="height: 100%;"
913
+ onmousedown={e => onColumnMouseDown(e, column)}
914
+ >
915
+ &nbsp;
916
+ </div>
917
+ {/if}
918
+ </th>
919
+ {/each}
920
+ </tr>
921
+ </thead>
922
+ <tbody class:unselectable={selectionMode === 'RANGE'}>
923
+ {#snippet tableRow(row: R & { originalIndex: number; uuid: string }, index: number)}
924
+ {#if children}
925
+ {@render children({ row, index })}
926
+ {:else}
927
+ <tr
928
+ class:table-primary={rowIsSelected(row, rowSelectionIdProp, selectedRowIds)}
929
+ onclick={() => rowClick(row)}
930
+ >
931
+ {#each computedColumns as column (column.property)}
932
+ <Td
933
+ align={column.align}
934
+ property={column.property}
935
+ >
936
+ {isArbitraryProperty(column.property)
937
+ ? ''
938
+ : column.formatter
939
+ ? column.formatter(getNestedProperty(row, column.property, ''))
940
+ : getNestedProperty(row, column.property, '')}
941
+ </Td>
942
+ {/each}
943
+ </tr>
944
+ {/if}
945
+ {/snippet}
946
+ {#if body}
947
+ {@render body?.({ rows: currentPageRows, visibleColumnsCount: visibleColumns.length })}
948
+ {:else if !currentPageRows.length}
949
+ {@render noRows?.({ visibleColumnsCount: visibleColumns.length })}
950
+ <!--
951
+ Only key the #each if the idProp is in the original row.
952
+ Otherwise, the table will re-render all rows if idProp is 'uuid' and your original row doesn't have one
953
+ -->
954
+ {:else if idProp in (rows[currentPageRows[0]?.originalIndex] ?? {})}
955
+ {#each currentPageRows as row, index (row[idProp])}
956
+ {@render tableRow(row, index)}
957
+ {/each}
958
+ {:else}
959
+ {#each currentPageRows as row, index}
960
+ {@render tableRow(row, index)}
961
+ {/each}
962
+ {/if}
963
+ </tbody>
964
+ {#if showFooter && footers}
965
+ <tfoot>
966
+ <tr class="table-secondary">
967
+ {#if footerRow}
968
+ {@render footerRow({ footers })}
969
+ {:else}
970
+ {#each footers as { property, value } (property)}
971
+ <Td
972
+ {property}
973
+ tagName="th">{value ?? ''}</Td
974
+ >
975
+ {/each}
976
+ {/if}
977
+ </tr>
978
+ </tfoot>
979
+ {/if}
980
+ </table>
981
+ </div>
982
+
983
+ <Pagination
984
+ {perPageCount}
985
+ {totalItemsCount}
986
+ items={sortedRows}
987
+ bind:currentPageItems={() => untrack(() => currentPageRows), items => (currentPageRows = items)}
988
+ bind:currentPageNumber
989
+ bind:lastPageNumber
990
+ {pageChange}
991
+ bind:this={paginationComponent}
992
+ />
993
+
994
+ <ContextMenu
995
+ id="hide-column-context-menu"
996
+ bind:this={contextMenu}
997
+ >
998
+ {#if columnPinningEnabled}
999
+ {@const isPinned = !!contextMenuColumn?.pinned}
1000
+ <DropdownItem
1001
+ icon={isPinned ? 'thumbtack-angle-slash' : 'thumbtack-angle'}
1002
+ title={isPinned
1003
+ ? 'Unpinning this column will make it scroll normally'
1004
+ : 'Pinning this column will keep it visible as you scroll to the right'}
1005
+ onclick={() => {
1006
+ if (contextMenuColumn) {
1007
+ pinUnpinColumn(contextMenuColumn)
1008
+ }
1009
+ }}
1010
+ >{isPinned ? `Unpin ${contextMenuColumn?.name}` : `Pin ${contextMenuColumn?.name}`}
1011
+ </DropdownItem>
1012
+
1013
+ {#if columnHidingEnabled || columnResizingEnabled}
1014
+ <div class="dropdown-divider"></div>
1015
+ {/if}
1016
+ {/if}
1017
+
1018
+ {#if columnHidingEnabled}
1019
+ <DropdownItem
1020
+ class={hideButtonClass}
1021
+ disabled={contextMenuColumn?.unhidable}
1022
+ onclick={() => contextMenuColumn && hideColumn(contextMenuColumn)}
1023
+ >
1024
+ <Icon icon="eye-slash"></Icon>
1025
+ {#if contextMenuColumn?.name}
1026
+ {translate('common:tableHideColumn', 'Hide {{- columnName}}', { columnName: contextMenuColumn.name })}
1027
+ {:else if contextMenuColumn?.icon}
1028
+ {translate('common:hide', 'Hide')}
1029
+
1030
+ <Icon
1031
+ prefix={contextMenuColumn.iconPrefix}
1032
+ icon={contextMenuColumn.icon}
1033
+ ></Icon>
1034
+ {/if}
1035
+ </DropdownItem>
1036
+
1037
+ {#if hiddenColumns.length > 0}
1038
+ <div class="dropdown-divider"></div>
1039
+ <h5 class="dropdown-header">
1040
+ <Icon icon="eye"></Icon>
1041
+ {translate('common:tableShowColumns', 'Show Column')}
1042
+ </h5>
1043
+ {/if}
1044
+ <div style="max-height: 300px; overflow-y: auto;">
1045
+ {#each hiddenColumns as hiddenColumn (hiddenColumn.property)}
1046
+ <DropdownItem onclick={() => showColumn(hiddenColumn)}>
1047
+ {@render columnNameAndIcon(hiddenColumn)}
1048
+ </DropdownItem>
1049
+ {/each}
1050
+ </div>
1051
+
1052
+ {#if columnResizingEnabled}
1053
+ <div class="dropdown-divider"></div>
1054
+ {/if}
1055
+ {/if}
1056
+
1057
+ {#if columnResizingEnabled}
1058
+ <DropdownItem
1059
+ icon="broom"
1060
+ onclick={() => {
1061
+ columnInfo.updateColumns(
1062
+ computedColumns.map(col => ({
1063
+ property: col.property,
1064
+ userWidth: undefined,
1065
+ })),
1066
+ )
1067
+ setDefaultColumnWidths()
1068
+ }}>{translate('common:tableResetColumnSizes', 'Reset Column Sizes')}</DropdownItem
1069
+ >
1070
+ {/if}
1071
+ </ContextMenu>
1072
+
1073
+ {#snippet columnNameAndIcon(column: GenericColumn)}
1074
+ <!-- Don't add any whitespace btwn these if blocks to keep the spaces correct -->
1075
+ {#if column.icon && column.iconLeft}
1076
+ <Icon
1077
+ prefix={column.iconPrefix}
1078
+ icon={column.icon}
1079
+ />{#if column.name}&nbsp;{/if}
1080
+ {/if}{column.name}{#if column.icon && !column.iconLeft}
1081
+ {#if column.name}&nbsp;{/if}<Icon
1082
+ prefix={column.iconPrefix}
1083
+ icon={column.icon}
1084
+ />
1085
+ {/if}
1086
+ {/snippet}
1087
+
1088
+ <style>
1089
+ table.sticky {
1090
+ position: relative;
1091
+ }
1092
+
1093
+ table.sticky th {
1094
+ position: sticky;
1095
+ top: 0;
1096
+ box-shadow: 0 2px 2px -1px rgba(0, 0, 0, 0.4);
1097
+ }
1098
+
1099
+ table.sticky > thead > tr > th {
1100
+ z-index: 3;
1101
+ }
1102
+
1103
+ /* Remove bottom margins created by form-groups in the table (BS4 only) */
1104
+ table > tbody :global(.form-group) {
1105
+ margin-bottom: 0%;
1106
+ }
1107
+
1108
+ /* BS5 removed .form-group, which makes it a little trickier to target the label's parent div, but this should do it
1109
+ - The Label component adds mb-3 to the parent div if the label is hidden or mb-1 if it's shown (unlikely if it's in a table)
1110
+ - That div will have a .input-group, .form-control, or .form-select as a direct descendant
1111
+ */
1112
+ table
1113
+ > tbody
1114
+ :global(
1115
+ td div:where(.mb-3, .mb-1):not(.form-group):has(> :where(.input-group, .form-control, .form-select, .btn-group))
1116
+ ) {
1117
+ margin-bottom: 0% !important;
1118
+ }
1119
+
1120
+ .resizer {
1121
+ position: absolute;
1122
+ top: 0;
1123
+ right: 0;
1124
+ width: 10px;
1125
+ cursor: col-resize;
1126
+ user-select: none;
1127
+ }
1128
+ .resizer:hover,
1129
+ .resizing {
1130
+ border-right: 5px solid darkslategray;
1131
+ }
1132
+ .table-layout-fixed {
1133
+ table-layout: fixed;
1134
+ }
1135
+ </style>