@vobs/table 1.0.0
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/LICENSE +21 -0
- package/README.md +57 -0
- package/package.json +33 -0
- package/src/column-settings.ts +381 -0
- package/src/data-table.test.ts +536 -0
- package/src/data-table.ts +686 -0
- package/src/index.ts +28 -0
- package/src/persistence.ts +99 -0
- package/src/styles/styles.css +1 -0
- package/src/styles/table.css +209 -0
- package/src/types.ts +183 -0
- package/src/utils.ts +172 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 vobs contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# @vobs/table
|
|
2
|
+
|
|
3
|
+
Data table for vobs: client or server sorting, externally owned filters, pagination, column settings persistence, resource binding, and virtualized rows.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @vobs/table
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick start
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { createDOMRenderer, createVobs, setRenderer } from '@vobs/vobs'
|
|
15
|
+
import { KitDataTable } from '@vobs/table'
|
|
16
|
+
import type { DataTableColumn } from '@vobs/table'
|
|
17
|
+
|
|
18
|
+
setRenderer(createDOMRenderer())
|
|
19
|
+
|
|
20
|
+
interface User { id: number, name: string, role: string }
|
|
21
|
+
|
|
22
|
+
const columns: readonly DataTableColumn<User>[] = [
|
|
23
|
+
{ id: 'name', label: 'Name', key: 'name', sortable: true },
|
|
24
|
+
{ id: 'role', label: 'Role', key: 'role' }
|
|
25
|
+
]
|
|
26
|
+
|
|
27
|
+
const app = createVobs({
|
|
28
|
+
render: () => KitDataTable<User>({
|
|
29
|
+
columns,
|
|
30
|
+
rows: [
|
|
31
|
+
{ id: 1, name: 'Ada', role: 'Admin' },
|
|
32
|
+
{ id: 2, name: 'Lin', role: 'Viewer' }
|
|
33
|
+
],
|
|
34
|
+
pageSize: 1,
|
|
35
|
+
onQueryChange: query => {
|
|
36
|
+
// { page, pageSize, sort: { columnId, direction, sortKey? }, filters }
|
|
37
|
+
}
|
|
38
|
+
})
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
app.mount(document.getElementById('app')!)
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## API
|
|
45
|
+
|
|
46
|
+
| Signature | Description |
|
|
47
|
+
| --- | --- |
|
|
48
|
+
| `KitDataTable<Row>(props: KitDataTableProps<Row>)` | Rows come from `rows` or a bound `resource`. Clicking a sortable header cycles asc, desc, then unsorted and emits the query through `onQueryChange`. `sortingMode: 'client'` reorders the loaded rows (`sortType`: text/number/date/boolean, `sortValue`, `compare`, null values last); `'server'` only emits the query including the column's `sortKey` and leaves row order untouched. Filtering is driven by the `filters` prop applied through per-column `filter` predicates — no filter inputs are rendered inside the table. Pagination uses `page`/`pageSize`/`total` and `paginationMode`; `stickyHeader` fixes the header; `virtual` enables windowed rows via `virtualHeight`/`rowHeight`/`overscan`. |
|
|
49
|
+
| `KitColumnSettings(props: KitColumnSettingsProps)` | Panel for column visibility, order, and widths. |
|
|
50
|
+
| `createColumnSettingsPersistence(storage, key)` | `DataTableColumnSettingsPersistence` over any `DataTableColumnSettingsStorage` (`get`/`set`/`remove`). |
|
|
51
|
+
| `createLocalColumnSettingsPersistence(key, storage?)` | Same interface over `localStorage`, falling back to in-memory storage. |
|
|
52
|
+
| `createDefaultColumnSettings(columns, visibleColumnIds?)` | Initial `DataTableColumnSettings` derived from column definitions. |
|
|
53
|
+
| `normalizeColumnSettings(columns, settings?, visibleColumnIds?)` | Drops unknown ids and repairs invalid settings against the columns. |
|
|
54
|
+
|
|
55
|
+
## Types
|
|
56
|
+
|
|
57
|
+
DataTableAlign, DataTableSortDirection, DataTableSortType, DataTableSortingMode, DataTablePaginationMode, DataTableSort, DataTableQuery, DataTablePage, DataTableResourceData, DataTableResource, DataTableChildren, DataTableColumn, DataTableColumnSettings, DataTableColumnSettingsPersistence, DataTableColumnSettingsStorage, DataTableIcons, DataTablePaginationContext, DataTablePagination, DataTableCommonProps, KitDataTableProps, KitColumnSettingsProps
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"license": "MIT",
|
|
3
|
+
"files": [
|
|
4
|
+
"src",
|
|
5
|
+
"README.md",
|
|
6
|
+
"LICENSE"
|
|
7
|
+
],
|
|
8
|
+
"name": "@vobs/table",
|
|
9
|
+
"version": "1.0.0",
|
|
10
|
+
"description": "Optional data table primitives for Vobs.",
|
|
11
|
+
"type": "module",
|
|
12
|
+
"main": "src/index.ts",
|
|
13
|
+
"types": "src/index.ts",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": "./src/index.ts",
|
|
16
|
+
"./data-table": "./src/data-table.ts",
|
|
17
|
+
"./column-settings": "./src/column-settings.ts",
|
|
18
|
+
"./styles.css": "./src/styles/styles.css",
|
|
19
|
+
"./table.css": "./src/styles/table.css",
|
|
20
|
+
"./package.json": "./package.json"
|
|
21
|
+
},
|
|
22
|
+
"sideEffects": [
|
|
23
|
+
"./src/styles/*.css"
|
|
24
|
+
],
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"@vobs/vobs": "1.0.0",
|
|
27
|
+
"@vobs/reactivity": "1.0.0",
|
|
28
|
+
"@vobs/resource": "1.0.0"
|
|
29
|
+
},
|
|
30
|
+
"scripts": {
|
|
31
|
+
"test": "vitest --environment jsdom"
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,381 @@
|
|
|
1
|
+
import { effect, state, untrack, type Signal } from '@vobs/reactivity'
|
|
2
|
+
import {
|
|
3
|
+
addEventListener,
|
|
4
|
+
createElement,
|
|
5
|
+
createFragment,
|
|
6
|
+
createText,
|
|
7
|
+
insertBefore,
|
|
8
|
+
insertDynamic,
|
|
9
|
+
setAttribute,
|
|
10
|
+
setProperty,
|
|
11
|
+
type VobsNode
|
|
12
|
+
} from '@vobs/vobs'
|
|
13
|
+
import {
|
|
14
|
+
bindClassList,
|
|
15
|
+
bindCommonAttributes,
|
|
16
|
+
bindText,
|
|
17
|
+
createDefaultColumnSettings,
|
|
18
|
+
hasProp,
|
|
19
|
+
isColumnWidthValue,
|
|
20
|
+
normalizeColumnSettings,
|
|
21
|
+
orderColumns,
|
|
22
|
+
readProp,
|
|
23
|
+
resolveSlot
|
|
24
|
+
} from './utils'
|
|
25
|
+
import type {
|
|
26
|
+
DataTableColumn,
|
|
27
|
+
DataTableColumnSettings,
|
|
28
|
+
KitColumnSettingsProps
|
|
29
|
+
} from './types'
|
|
30
|
+
|
|
31
|
+
export function KitColumnSettings<Row = Record<string, unknown>>(
|
|
32
|
+
props: KitColumnSettingsProps<Row> = {}
|
|
33
|
+
): VobsNode {
|
|
34
|
+
const root = createElement('details')
|
|
35
|
+
const summary = createElement('summary')
|
|
36
|
+
const panel = createElement('div')
|
|
37
|
+
const columns = readColumns(props)
|
|
38
|
+
const internalSettings = state(untrack(() => initialSettings(props, columns)))
|
|
39
|
+
const panelOpen = state(false)
|
|
40
|
+
|
|
41
|
+
untrack(() => restoreSettings(props, columns, internalSettings))
|
|
42
|
+
|
|
43
|
+
effect(() => {
|
|
44
|
+
currentSettings(props, internalSettings, columns)
|
|
45
|
+
if (panelOpen.value) setProperty(root, 'open', true)
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
bindClassList(root, props, () => ['vobs-column-settings'])
|
|
49
|
+
bindCommonAttributes(root, props, [
|
|
50
|
+
'columns',
|
|
51
|
+
'settings',
|
|
52
|
+
'persistence',
|
|
53
|
+
'visibleColumnIds',
|
|
54
|
+
'trigger',
|
|
55
|
+
'label',
|
|
56
|
+
'closeLabel',
|
|
57
|
+
'resetLabel',
|
|
58
|
+
'onChange',
|
|
59
|
+
'onSettingsChange',
|
|
60
|
+
'onReset',
|
|
61
|
+
'onPersistenceError'
|
|
62
|
+
])
|
|
63
|
+
setAttribute(summary, 'class', 'vobs-column-settings__trigger')
|
|
64
|
+
setAttribute(panel, 'class', 'vobs-column-settings__panel')
|
|
65
|
+
setAttribute(panel, 'role', 'group')
|
|
66
|
+
addEventListener(summary, 'click', event => {
|
|
67
|
+
event.preventDefault()
|
|
68
|
+
panelOpen.value = !(root as HTMLDetailsElement).open
|
|
69
|
+
setProperty(root, 'open', panelOpen.value)
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
if (hasProp(props, 'trigger')) {
|
|
73
|
+
insertDynamic(summary, null, () => resolveSlot(readProp(props, 'trigger', undefined)))
|
|
74
|
+
} else {
|
|
75
|
+
const label = createText('')
|
|
76
|
+
bindText(label, () => readProp(props, 'label', 'Columns'))
|
|
77
|
+
insertBefore(summary, label, null)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
insertDynamic(panel, null, () => createColumnOptions(props, internalSettings, columns, root))
|
|
81
|
+
insertBefore(root, summary, null)
|
|
82
|
+
insertBefore(root, panel, null)
|
|
83
|
+
return root
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function createColumnOptions<Row>(
|
|
87
|
+
props: KitColumnSettingsProps<Row>,
|
|
88
|
+
internalSettings: Signal<DataTableColumnSettings>,
|
|
89
|
+
columns: readonly DataTableColumn<Row>[],
|
|
90
|
+
root: Element
|
|
91
|
+
): VobsNode {
|
|
92
|
+
const settings = currentSettings(props, internalSettings, columns)
|
|
93
|
+
return createFragment((parent, anchor) => {
|
|
94
|
+
const reset = createElement('button')
|
|
95
|
+
const resetLabel = createText('')
|
|
96
|
+
setAttribute(reset, 'type', 'button')
|
|
97
|
+
setAttribute(reset, 'class', 'vobs-column-settings__reset')
|
|
98
|
+
bindText(resetLabel, () => readProp(props, 'resetLabel', 'Reset columns'))
|
|
99
|
+
addEventListener(reset, 'click', event => {
|
|
100
|
+
event.preventDefault()
|
|
101
|
+
event.stopPropagation()
|
|
102
|
+
resetSettings(props, internalSettings, columns, root)
|
|
103
|
+
})
|
|
104
|
+
insertBefore(reset, resetLabel, null)
|
|
105
|
+
insertBefore(parent, reset, anchor)
|
|
106
|
+
|
|
107
|
+
const visibility = createElement('div')
|
|
108
|
+
setAttribute(visibility, 'class', 'vobs-column-settings__visibility')
|
|
109
|
+
for (const column of columns) {
|
|
110
|
+
insertBefore(visibility, createVisibilityOption(props, internalSettings, columns, column), null)
|
|
111
|
+
}
|
|
112
|
+
insertBefore(parent, visibility, anchor)
|
|
113
|
+
|
|
114
|
+
const layout = createElement('div')
|
|
115
|
+
setAttribute(layout, 'class', 'vobs-column-settings__layout')
|
|
116
|
+
const ordered = orderColumns(columns, settings.columnOrder)
|
|
117
|
+
for (const [index, column] of ordered.entries()) {
|
|
118
|
+
insertBefore(layout, createLayoutOption(
|
|
119
|
+
props,
|
|
120
|
+
internalSettings,
|
|
121
|
+
columns,
|
|
122
|
+
column,
|
|
123
|
+
root,
|
|
124
|
+
index === 0,
|
|
125
|
+
index === ordered.length - 1
|
|
126
|
+
), null)
|
|
127
|
+
}
|
|
128
|
+
insertBefore(parent, layout, anchor)
|
|
129
|
+
})
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function createVisibilityOption<Row>(
|
|
133
|
+
props: KitColumnSettingsProps<Row>,
|
|
134
|
+
internalSettings: Signal<DataTableColumnSettings>,
|
|
135
|
+
columns: readonly DataTableColumn<Row>[],
|
|
136
|
+
column: DataTableColumn<Row>
|
|
137
|
+
): VobsNode {
|
|
138
|
+
const option = createElement('label')
|
|
139
|
+
const input = createElement('input') as HTMLInputElement
|
|
140
|
+
const text = createText(column.label)
|
|
141
|
+
const settings = currentSettings(props, internalSettings, columns)
|
|
142
|
+
setAttribute(option, 'class', 'vobs-column-settings__option')
|
|
143
|
+
setAttribute(input, 'type', 'checkbox')
|
|
144
|
+
setProperty(input, 'checked', settings.visibleColumnIds.includes(column.id))
|
|
145
|
+
setProperty(input, 'disabled', settings.visibleColumnIds.length <= 1 && input.checked)
|
|
146
|
+
addEventListener(input, 'change', () => {
|
|
147
|
+
const current = currentSettings(props, internalSettings, columns)
|
|
148
|
+
const visible = new Set(current.visibleColumnIds)
|
|
149
|
+
if (input.checked) visible.add(column.id)
|
|
150
|
+
else if (visible.size > 1) visible.delete(column.id)
|
|
151
|
+
const ordered = orderColumns(columns, current.columnOrder)
|
|
152
|
+
const nextIds = ordered.filter(candidate => visible.has(candidate.id)).map(candidate => candidate.id)
|
|
153
|
+
commitSettings(props, internalSettings, columns, {
|
|
154
|
+
...current,
|
|
155
|
+
visibleColumnIds: nextIds
|
|
156
|
+
})
|
|
157
|
+
})
|
|
158
|
+
insertBefore(option, input, null)
|
|
159
|
+
insertBefore(option, text, null)
|
|
160
|
+
return option
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function createLayoutOption<Row>(
|
|
164
|
+
props: KitColumnSettingsProps<Row>,
|
|
165
|
+
internalSettings: Signal<DataTableColumnSettings>,
|
|
166
|
+
columns: readonly DataTableColumn<Row>[],
|
|
167
|
+
column: DataTableColumn<Row>,
|
|
168
|
+
root: Element,
|
|
169
|
+
isFirst: boolean,
|
|
170
|
+
isLast: boolean
|
|
171
|
+
): VobsNode {
|
|
172
|
+
const option = createElement('div')
|
|
173
|
+
const label = createElement('span')
|
|
174
|
+
const controls = createElement('div')
|
|
175
|
+
const up = createMoveButton('up', column.label, isFirst)
|
|
176
|
+
const down = createMoveButton('down', column.label, isLast)
|
|
177
|
+
const width = createElement('input') as HTMLInputElement
|
|
178
|
+
const current = currentSettings(props, internalSettings, columns)
|
|
179
|
+
const configuredWidth = current.columnWidths[column.id] ?? column.width
|
|
180
|
+
|
|
181
|
+
setAttribute(option, 'class', 'vobs-column-settings__layout-option')
|
|
182
|
+
setAttribute(option, 'data-column-id', column.id)
|
|
183
|
+
setAttribute(label, 'class', 'vobs-column-settings__layout-label')
|
|
184
|
+
setProperty(label, 'textContent', column.label)
|
|
185
|
+
setAttribute(controls, 'class', 'vobs-column-settings__layout-controls')
|
|
186
|
+
setAttribute(width, 'type', 'text')
|
|
187
|
+
setAttribute(width, 'class', 'vobs-column-settings__width')
|
|
188
|
+
setAttribute(width, 'placeholder', 'auto')
|
|
189
|
+
setAttribute(width, 'aria-label', `${column.label} width`)
|
|
190
|
+
setProperty(width, 'value', displayWidth(configuredWidth))
|
|
191
|
+
|
|
192
|
+
addEventListener(up, 'click', event => {
|
|
193
|
+
event.preventDefault()
|
|
194
|
+
event.stopPropagation()
|
|
195
|
+
moveColumn(props, internalSettings, columns, column.id, -1, root)
|
|
196
|
+
})
|
|
197
|
+
addEventListener(down, 'click', event => {
|
|
198
|
+
event.preventDefault()
|
|
199
|
+
event.stopPropagation()
|
|
200
|
+
moveColumn(props, internalSettings, columns, column.id, 1, root)
|
|
201
|
+
})
|
|
202
|
+
const commitWidth = (): void => {
|
|
203
|
+
const value = width.value.trim()
|
|
204
|
+
const currentSettingsValue = currentSettings(props, internalSettings, columns)
|
|
205
|
+
const currentWidth = displayWidth(currentSettingsValue.columnWidths[column.id] ?? column.width)
|
|
206
|
+
if (value === currentWidth) return
|
|
207
|
+
if (value && !isColumnWidthValue(value)) {
|
|
208
|
+
width.value = currentWidth
|
|
209
|
+
return
|
|
210
|
+
}
|
|
211
|
+
const nextWidths = { ...currentSettingsValue.columnWidths }
|
|
212
|
+
if (value) nextWidths[column.id] = value
|
|
213
|
+
else delete nextWidths[column.id]
|
|
214
|
+
commitSettings(props, internalSettings, columns, {
|
|
215
|
+
...currentSettingsValue,
|
|
216
|
+
columnWidths: nextWidths
|
|
217
|
+
})
|
|
218
|
+
}
|
|
219
|
+
addEventListener(width, 'change', commitWidth)
|
|
220
|
+
addEventListener(width, 'blur', commitWidth)
|
|
221
|
+
addEventListener(width, 'keydown', event => {
|
|
222
|
+
if ((event as KeyboardEvent).key !== 'Enter') return
|
|
223
|
+
event.preventDefault()
|
|
224
|
+
commitWidth()
|
|
225
|
+
})
|
|
226
|
+
|
|
227
|
+
insertBefore(controls, up, null)
|
|
228
|
+
insertBefore(controls, down, null)
|
|
229
|
+
insertBefore(controls, width, null)
|
|
230
|
+
insertBefore(option, label, null)
|
|
231
|
+
insertBefore(option, controls, null)
|
|
232
|
+
return option
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
function createMoveButton(direction: 'up' | 'down', columnLabel: string, disabled: boolean): Element {
|
|
236
|
+
const button = createElement('button')
|
|
237
|
+
setAttribute(button, 'type', 'button')
|
|
238
|
+
setAttribute(button, 'class', `vobs-column-settings__move vobs-column-settings__move--${direction}`)
|
|
239
|
+
setAttribute(button, 'aria-label', `Move ${columnLabel} ${direction}`)
|
|
240
|
+
setAttribute(button, 'title', `Move ${columnLabel} ${direction}`)
|
|
241
|
+
setProperty(button, 'disabled', disabled)
|
|
242
|
+
insertBefore(button, createText(direction === 'up' ? '↑' : '↓'), null)
|
|
243
|
+
return button
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
function moveColumn<Row>(
|
|
247
|
+
props: KitColumnSettingsProps<Row>,
|
|
248
|
+
internalSettings: Signal<DataTableColumnSettings>,
|
|
249
|
+
columns: readonly DataTableColumn<Row>[],
|
|
250
|
+
columnId: string,
|
|
251
|
+
offset: -1 | 1,
|
|
252
|
+
root: Element
|
|
253
|
+
): void {
|
|
254
|
+
const current = currentSettings(props, internalSettings, columns)
|
|
255
|
+
const order = [...current.columnOrder]
|
|
256
|
+
const index = order.indexOf(columnId)
|
|
257
|
+
const target = index + offset
|
|
258
|
+
if (index < 0 || target < 0 || target >= order.length) return
|
|
259
|
+
;[order[index], order[target]] = [order[target], order[index]]
|
|
260
|
+
commitSettings(props, internalSettings, columns, { ...current, columnOrder: order })
|
|
261
|
+
reopenPanel(root)
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
function resetSettings<Row>(
|
|
265
|
+
props: KitColumnSettingsProps<Row>,
|
|
266
|
+
internalSettings: Signal<DataTableColumnSettings>,
|
|
267
|
+
columns: readonly DataTableColumn<Row>[],
|
|
268
|
+
root: Element
|
|
269
|
+
): void {
|
|
270
|
+
const persistence = readProp<KitColumnSettingsProps<Row>['persistence'] | undefined>(props, 'persistence', undefined)
|
|
271
|
+
let cleared = false
|
|
272
|
+
if (persistence?.reset) {
|
|
273
|
+
try {
|
|
274
|
+
persistence.reset()
|
|
275
|
+
cleared = true
|
|
276
|
+
} catch (error) {
|
|
277
|
+
reportPersistenceError(props, error)
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
const defaults = createDefaultColumnSettings(columns)
|
|
281
|
+
commitSettings(props, internalSettings, columns, defaults, !persistence || !cleared)
|
|
282
|
+
reopenPanel(root)
|
|
283
|
+
readProp<KitColumnSettingsProps<Row>['onReset'] | undefined>(props, 'onReset', undefined)?.()
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
function commitSettings<Row>(
|
|
287
|
+
props: KitColumnSettingsProps<Row>,
|
|
288
|
+
internalSettings: Signal<DataTableColumnSettings>,
|
|
289
|
+
columns: readonly DataTableColumn<Row>[],
|
|
290
|
+
settings: DataTableColumnSettings,
|
|
291
|
+
persist = true
|
|
292
|
+
): void {
|
|
293
|
+
const normalized = normalizeColumnSettings(columns, settings)
|
|
294
|
+
if (!isControlled(props)) internalSettings.value = normalized
|
|
295
|
+
if (persist) saveSettings(props, normalized)
|
|
296
|
+
readProp<KitColumnSettingsProps<Row>['onSettingsChange'] | undefined>(props, 'onSettingsChange', undefined)?.(normalized)
|
|
297
|
+
readProp<KitColumnSettingsProps<Row>['onChange'] | undefined>(props, 'onChange', undefined)?.(normalized.visibleColumnIds)
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
function initialSettings<Row>(
|
|
301
|
+
props: KitColumnSettingsProps<Row>,
|
|
302
|
+
columns: readonly DataTableColumn<Row>[]
|
|
303
|
+
): DataTableColumnSettings {
|
|
304
|
+
const settings = readProp<DataTableColumnSettings | undefined>(props, 'settings', undefined)
|
|
305
|
+
if (settings !== undefined) return normalizeColumnSettings(columns, settings)
|
|
306
|
+
return normalizeColumnSettings(
|
|
307
|
+
columns,
|
|
308
|
+
undefined,
|
|
309
|
+
readProp<readonly string[] | undefined>(props, 'visibleColumnIds', undefined)
|
|
310
|
+
)
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
function restoreSettings<Row>(
|
|
314
|
+
props: KitColumnSettingsProps<Row>,
|
|
315
|
+
columns: readonly DataTableColumn<Row>[],
|
|
316
|
+
internalSettings: Signal<DataTableColumnSettings>
|
|
317
|
+
): void {
|
|
318
|
+
if (isControlled(props)) return
|
|
319
|
+
const persistence = readProp<KitColumnSettingsProps<Row>['persistence'] | undefined>(props, 'persistence', undefined)
|
|
320
|
+
if (!persistence) return
|
|
321
|
+
try {
|
|
322
|
+
const stored = persistence.load()
|
|
323
|
+
if (stored) internalSettings.value = normalizeColumnSettings(columns, stored)
|
|
324
|
+
} catch (error) {
|
|
325
|
+
reportPersistenceError(props, error)
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
function currentSettings<Row>(
|
|
330
|
+
props: KitColumnSettingsProps<Row>,
|
|
331
|
+
internalSettings: Signal<DataTableColumnSettings>,
|
|
332
|
+
columns: readonly DataTableColumn<Row>[]
|
|
333
|
+
): DataTableColumnSettings {
|
|
334
|
+
const external = readProp<DataTableColumnSettings | undefined>(props, 'settings', undefined)
|
|
335
|
+
if (external !== undefined) return normalizeColumnSettings(columns, external)
|
|
336
|
+
const visibleColumnIds = readProp<readonly string[] | undefined>(props, 'visibleColumnIds', undefined)
|
|
337
|
+
if (visibleColumnIds !== undefined) {
|
|
338
|
+
return normalizeColumnSettings(columns, {
|
|
339
|
+
...internalSettings.value,
|
|
340
|
+
visibleColumnIds
|
|
341
|
+
})
|
|
342
|
+
}
|
|
343
|
+
return normalizeColumnSettings(columns, internalSettings.value)
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
function saveSettings<Row>(props: KitColumnSettingsProps<Row>, settings: DataTableColumnSettings): void {
|
|
347
|
+
const persistence = readProp<KitColumnSettingsProps<Row>['persistence'] | undefined>(props, 'persistence', undefined)
|
|
348
|
+
if (!persistence) return
|
|
349
|
+
try {
|
|
350
|
+
persistence.save(settings)
|
|
351
|
+
} catch (error) {
|
|
352
|
+
reportPersistenceError(props, error)
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
function readColumns<Row>(props: KitColumnSettingsProps<Row>): readonly DataTableColumn<Row>[] {
|
|
357
|
+
return readProp<readonly DataTableColumn<Row>[]>(props, 'columns', [])
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
function displayWidth(value: string | number | undefined): string {
|
|
361
|
+
if (value === undefined) return ''
|
|
362
|
+
return typeof value === 'number' ? `${value}px` : value
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
function isControlled<Row>(props: KitColumnSettingsProps<Row>): boolean {
|
|
366
|
+
return isProvided(props, 'settings') || isProvided(props, 'visibleColumnIds')
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
function isProvided(props: object, name: string): boolean {
|
|
370
|
+
return hasProp(props, name) && Reflect.get(props, name) !== undefined
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
function reportPersistenceError<Row>(props: KitColumnSettingsProps<Row>, error: unknown): void {
|
|
374
|
+
readProp<KitColumnSettingsProps<Row>['onPersistenceError'] | undefined>(props, 'onPersistenceError', undefined)?.(error)
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
function reopenPanel(root: Element): void {
|
|
378
|
+
setTimeout(() => {
|
|
379
|
+
setProperty(root, 'open', true)
|
|
380
|
+
}, 0)
|
|
381
|
+
}
|