@itrocks/list 0.29.0 → 0.30.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/README.md +8 -2
- package/build.js +58 -6
- package/cjs/column-widths.d.ts +12 -0
- package/cjs/column-widths.js +54 -0
- package/cjs/list.d.ts +3 -0
- package/cjs/list.html +3 -2
- package/cjs/list.js +17 -2
- package/cjs/projection.d.ts +1 -0
- package/cjs/projection.js +1 -0
- package/cjs/session-state.d.ts +1 -0
- package/cjs/session-state.js +5 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -363,6 +363,12 @@ The same projection is used when reloading the list or adding an individual comp
|
|
|
363
363
|
rendered values, mandatory row scope, partial selection, removal and session restoration. It creates and cleans up
|
|
364
364
|
its own temporary database and server using the existing selector integration recipe.
|
|
365
365
|
|
|
366
|
+
## Column widths
|
|
367
|
+
|
|
368
|
+
Drag the edge of a column heading to set its width. Double-click the same edge to return to automatic sizing.
|
|
369
|
+
List actions can override `canResizeColumns(request)` to disable the gesture while displaying saved widths.
|
|
370
|
+
Only explicit pixel widths are stored in `columnWidths`, by property path. Removing a column clears its width.
|
|
371
|
+
|
|
366
372
|
## Session state integration
|
|
367
373
|
|
|
368
374
|
`@itrocks/list/session-state` exposes the settings independently of rendering:
|
|
@@ -376,8 +382,8 @@ replaceListState(request.request, request.type, settings)
|
|
|
376
382
|
replaceListState(request.request, request.type)
|
|
377
383
|
```
|
|
378
384
|
|
|
379
|
-
`listState(request: ServerRequest, type: Type): ListState` returns a deep copy of columns,
|
|
380
|
-
and ordered sort tuples.
|
|
385
|
+
`listState(request: ServerRequest, type: Type): ListState` returns a deep copy of columns, explicit column widths,
|
|
386
|
+
filters and ordered sort tuples.
|
|
381
387
|
`replaceListState(request, type, state?)` replaces the complete session entry;
|
|
382
388
|
omitting state restores reflected defaults on the next render.
|
|
383
389
|
Neither function queries business rows or changes mandatory scopes.
|
package/build.js
CHANGED
|
@@ -2,6 +2,7 @@ import { build } from '@itrocks/build/build.js';
|
|
|
2
2
|
import { TableColumnAdd } from '@itrocks/table/column-add.js';
|
|
3
3
|
import { TableColumnReorderFreeze } from '@itrocks/table/column-reorder-freeze.js';
|
|
4
4
|
import { TableColumnReorder } from '@itrocks/table/column-reorder.js';
|
|
5
|
+
import { TableColumnResize } from '@itrocks/table/column-resize.js';
|
|
5
6
|
import { TableFeed } from '@itrocks/table/feed.js';
|
|
6
7
|
import { TableFreeze } from '@itrocks/table/freeze.js';
|
|
7
8
|
import { TableFreezeInheritBackground } from '@itrocks/table/freeze/inherit-background.js';
|
|
@@ -13,16 +14,28 @@ import './column-filter.js';
|
|
|
13
14
|
import './search-focus.js';
|
|
14
15
|
import './toolbar.js';
|
|
15
16
|
build('article[data-action="list"] > form > table.objects', table => {
|
|
16
|
-
|
|
17
|
-
|
|
17
|
+
const headers = [...table.querySelectorAll('thead th[data-property]')];
|
|
18
|
+
const persistedWidths = new Map();
|
|
19
|
+
const pendingWidths = new Map();
|
|
20
|
+
let persistedColumns = headers.map(header => header.dataset.property).join(',');
|
|
21
|
+
for (const header of headers) {
|
|
22
|
+
if (header.dataset.columnWidth) {
|
|
23
|
+
persistedWidths.set(header.dataset.property, Number(header.dataset.columnWidth));
|
|
24
|
+
}
|
|
25
|
+
}
|
|
18
26
|
let saving = Promise.resolve();
|
|
19
|
-
const request = (query, signal) => {
|
|
27
|
+
const request = (query, signal, method = 'GET') => {
|
|
20
28
|
const form = table.closest('form');
|
|
21
|
-
const result = saving.then(() => fetch(`${form.action}?${query}`, { keepalive: true, signal }));
|
|
29
|
+
const result = saving.then(() => fetch(`${form.action}?${query}`, { keepalive: true, method, signal }));
|
|
22
30
|
saving = result.then(() => { }, () => { });
|
|
23
31
|
void result.then(response => {
|
|
24
32
|
if (response.ok && query.has('columns') && (query.get('columns') !== persistedColumns)) {
|
|
25
33
|
persistedColumns = query.get('columns');
|
|
34
|
+
const paths = new Set(persistedColumns.split(','));
|
|
35
|
+
for (const path of persistedWidths.keys()) {
|
|
36
|
+
if (!paths.has(path))
|
|
37
|
+
persistedWidths.delete(path);
|
|
38
|
+
}
|
|
26
39
|
table.dispatchEvent(new CustomEvent('itrocks:list-change', { bubbles: true }));
|
|
27
40
|
}
|
|
28
41
|
}, () => { });
|
|
@@ -39,6 +52,45 @@ build('article[data-action="list"] > form > table.objects', table => {
|
|
|
39
52
|
throw new Error('Unable to save columns');
|
|
40
53
|
});
|
|
41
54
|
};
|
|
55
|
+
const saveWidth = (header, width) => {
|
|
56
|
+
const path = header.dataset.property;
|
|
57
|
+
const query = new URLSearchParams({ resizeColumn: path, columnWidth: String(width) });
|
|
58
|
+
const result = request(query, undefined, 'POST');
|
|
59
|
+
pendingWidths.set(path, result);
|
|
60
|
+
void result.then(response => {
|
|
61
|
+
if (response.status !== 204)
|
|
62
|
+
throw new Error('Unable to save column width');
|
|
63
|
+
const previous = persistedWidths.get(path) ?? 'auto';
|
|
64
|
+
if (width === 'auto')
|
|
65
|
+
persistedWidths.delete(path);
|
|
66
|
+
else
|
|
67
|
+
persistedWidths.set(path, width);
|
|
68
|
+
if (width !== previous) {
|
|
69
|
+
table.dispatchEvent(new CustomEvent('itrocks:list-change', { bubbles: true }));
|
|
70
|
+
}
|
|
71
|
+
}).catch(error => {
|
|
72
|
+
if (pendingWidths.get(path) === result) {
|
|
73
|
+
resize.setWidth(header, persistedWidths.get(path) ?? 'auto');
|
|
74
|
+
}
|
|
75
|
+
console.error(error);
|
|
76
|
+
}).finally(() => {
|
|
77
|
+
if (pendingWidths.get(path) === result)
|
|
78
|
+
pendingWidths.delete(path);
|
|
79
|
+
});
|
|
80
|
+
};
|
|
81
|
+
const resize = new TableColumnResize({
|
|
82
|
+
identify: header => header.dataset.property,
|
|
83
|
+
isInResizeZone: (header, y) => {
|
|
84
|
+
const filter = header.querySelector(':scope > input[data-property-filter]');
|
|
85
|
+
if (!filter)
|
|
86
|
+
return true;
|
|
87
|
+
const bottom = filter.getBoundingClientRect().top;
|
|
88
|
+
const lineHeight = parseFloat(getComputedStyle(header).lineHeight);
|
|
89
|
+
return (y < bottom) && (!Number.isFinite(lineHeight) || (y >= bottom - lineHeight));
|
|
90
|
+
},
|
|
91
|
+
isResizable: header => table.hasAttribute('data-column-resize') && (header.dataset.property !== undefined),
|
|
92
|
+
onChange: saveWidth
|
|
93
|
+
});
|
|
42
94
|
const tableColumnReorder = new TableColumnReorder({
|
|
43
95
|
isReorderable: header => header.dataset.property !== undefined,
|
|
44
96
|
onRemove: () => saveColumns(),
|
|
@@ -55,8 +107,8 @@ build('article[data-action="list"] > form > table.objects', table => {
|
|
|
55
107
|
const addition = new TableColumnAdd({ identify: header => header.dataset.property });
|
|
56
108
|
const instance = tableByElement(table, {
|
|
57
109
|
plugins: [
|
|
58
|
-
tableColumnReorder, addition, TableColumnReorderFreeze, TableFeed, TableFreeze,
|
|
59
|
-
TableFreezeInheritBorder, tableLink
|
|
110
|
+
tableColumnReorder, resize, addition, TableColumnReorderFreeze, TableFeed, TableFreeze,
|
|
111
|
+
TableFreezeInheritBackground, TableFreezeInheritBorder, tableLink
|
|
60
112
|
]
|
|
61
113
|
});
|
|
62
114
|
table.dispatchEvent(new CustomEvent('itrocks:list-ready', {
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Request } from '@itrocks/request-response';
|
|
2
|
+
import type { Columns } from './columns';
|
|
3
|
+
import { Parameter } from './parameter';
|
|
4
|
+
export declare function isColumnWidth(width: unknown): width is number;
|
|
5
|
+
export declare class ColumnWidths<T extends object> extends Parameter {
|
|
6
|
+
private readonly columns;
|
|
7
|
+
private widths;
|
|
8
|
+
constructor(columns: Columns<T>);
|
|
9
|
+
getParams(request: Request): boolean;
|
|
10
|
+
serialize(): Record<string, number> | undefined;
|
|
11
|
+
unserialize(data: unknown): void;
|
|
12
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ColumnWidths = void 0;
|
|
4
|
+
exports.isColumnWidth = isColumnWidth;
|
|
5
|
+
const parameter_1 = require("./parameter");
|
|
6
|
+
function isColumnWidth(width) {
|
|
7
|
+
return (typeof width === 'number') && Number.isSafeInteger(width) && (width > 0);
|
|
8
|
+
}
|
|
9
|
+
class ColumnWidths extends parameter_1.Parameter {
|
|
10
|
+
columns;
|
|
11
|
+
widths = new Map();
|
|
12
|
+
constructor(columns) {
|
|
13
|
+
super();
|
|
14
|
+
this.columns = columns;
|
|
15
|
+
}
|
|
16
|
+
getParams(request) {
|
|
17
|
+
const paths = new Set(this.columns.map(column => column.name));
|
|
18
|
+
const { columnWidth, resizeColumn } = request.data;
|
|
19
|
+
if (resizeColumn !== undefined || columnWidth !== undefined) {
|
|
20
|
+
if ((typeof resizeColumn !== 'string') || !paths.has(resizeColumn))
|
|
21
|
+
return false;
|
|
22
|
+
if (columnWidth === 'auto') {
|
|
23
|
+
this.widths.delete(resizeColumn);
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
if ((typeof columnWidth !== 'string') || !/^\d+$/.test(columnWidth))
|
|
27
|
+
return false;
|
|
28
|
+
const width = Number(columnWidth);
|
|
29
|
+
if (!isColumnWidth(width))
|
|
30
|
+
return false;
|
|
31
|
+
this.widths.set(resizeColumn, width);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
for (const path of this.widths.keys()) {
|
|
35
|
+
if (!paths.has(path)) {
|
|
36
|
+
this.widths.delete(path);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
for (const column of this.columns) {
|
|
40
|
+
column.width = this.widths.get(column.name);
|
|
41
|
+
}
|
|
42
|
+
return true;
|
|
43
|
+
}
|
|
44
|
+
serialize() {
|
|
45
|
+
return this.widths.size ? Object.fromEntries(this.widths) : undefined;
|
|
46
|
+
}
|
|
47
|
+
unserialize(data) {
|
|
48
|
+
this.widths = new Map((data && (typeof data === 'object') && !Array.isArray(data))
|
|
49
|
+
? Object.entries(data).filter((entry) => isColumnWidth(entry[1]))
|
|
50
|
+
: []);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
exports.ColumnWidths = ColumnWidths;
|
|
54
|
+
//# sourceMappingURL=column-widths.js.map
|
package/cjs/list.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { Action } from '@itrocks/action';
|
|
|
2
2
|
import { Request } from '@itrocks/action-request';
|
|
3
3
|
import { SearchType } from '@itrocks/storage';
|
|
4
4
|
import { ListView } from './access';
|
|
5
|
+
import { ColumnWidths } from './column-widths';
|
|
5
6
|
import { Columns } from './columns';
|
|
6
7
|
import { Feed } from './feed';
|
|
7
8
|
import { Filter } from './filter';
|
|
@@ -10,9 +11,11 @@ export { listAccess, listDependsOn } from './access';
|
|
|
10
11
|
export type { ListAccess, ListDependencies, ListView } from './access';
|
|
11
12
|
export declare class List<T extends object = object> extends Action<T> {
|
|
12
13
|
columns: Columns<object>;
|
|
14
|
+
columnWidths: ColumnWidths<object>;
|
|
13
15
|
feed: Feed;
|
|
14
16
|
filter: Filter<object>;
|
|
15
17
|
sort: Sort<object>;
|
|
18
|
+
protected canResizeColumns(_request: Request<T>): boolean;
|
|
16
19
|
protected filterBar(request: Request<T>): ListView;
|
|
17
20
|
html(request: Request<T>, scope?: SearchType<T>): Promise<import("@itrocks/request-response").Response>;
|
|
18
21
|
json(request: Request<T>, scope?: SearchType<T>): Promise<import("@itrocks/core-responses").JsonResponse>;
|
package/cjs/list.html
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
<html lang="en">
|
|
3
3
|
<head>
|
|
4
4
|
<link href="../../action-bar/action.css" rel="stylesheet">
|
|
5
|
+
<link href="../../table/column-resize.css" rel="stylesheet">
|
|
5
6
|
<link href="../../ux-core/css/app.css" rel="stylesheet">
|
|
6
7
|
<link href="../../ux-core/css/article.css" rel="stylesheet">
|
|
7
8
|
<link href="../list.css" rel="stylesheet">
|
|
@@ -37,7 +38,7 @@
|
|
|
37
38
|
<form action="app://(type.@route)/list" autocomplete="off" data-focus-column="{focusColumn}"
|
|
38
39
|
method="post" target="main">
|
|
39
40
|
<!--filterBar--><!--formFields-->{{template}}<!--end--><!--end-->
|
|
40
|
-
<table class="objects" data-link="{type.@route}">
|
|
41
|
+
<table class="objects" data-column-resize="{?canResize}" data-link="{type.@route}">
|
|
41
42
|
<thead>
|
|
42
43
|
<tr class="properties">
|
|
43
44
|
<th class="select" data-freeze><!--selection-->{{template}}<!--end-->
|
|
@@ -45,7 +46,7 @@
|
|
|
45
46
|
</th>
|
|
46
47
|
<!--columns-->
|
|
47
48
|
<th data-property="{name}" data-reverse="{?reverse}" data-sort="{?sort}"
|
|
48
|
-
data-sortable="{sortable}">{label}
|
|
49
|
+
data-sortable="{sortable}" data-column-width="{?width}">{label}
|
|
49
50
|
<input aria-label="Filter {label}" data-property-filter="{name}" name="search.{field}" value="{escapedSearch}">
|
|
50
51
|
</th>
|
|
51
52
|
<!--end-->
|
package/cjs/list.js
CHANGED
|
@@ -46,6 +46,7 @@ const storage_3 = require("@itrocks/storage");
|
|
|
46
46
|
const access_1 = require("./access");
|
|
47
47
|
const access_2 = require("./access");
|
|
48
48
|
const access_3 = require("./access");
|
|
49
|
+
const column_widths_1 = require("./column-widths");
|
|
49
50
|
const columns_1 = require("./columns");
|
|
50
51
|
const columns_2 = require("./columns");
|
|
51
52
|
const feed_1 = require("./feed");
|
|
@@ -80,9 +81,13 @@ let List = (() => {
|
|
|
80
81
|
__runInitializers(_classThis, _classExtraInitializers);
|
|
81
82
|
}
|
|
82
83
|
columns = new columns_1.Columns;
|
|
84
|
+
columnWidths = new column_widths_1.ColumnWidths(this.columns);
|
|
83
85
|
feed = new feed_1.Feed;
|
|
84
86
|
filter = new filter_1.Filter(this.columns);
|
|
85
87
|
sort = new sort_1.Sort(this.columns);
|
|
88
|
+
canResizeColumns(_request) {
|
|
89
|
+
return true;
|
|
90
|
+
}
|
|
86
91
|
filterBar(request) {
|
|
87
92
|
return (0, filter_summary_2.filterSummary)(this.filter.search, (0, filter_summary_1.filterAliases)(request.type, this.columns));
|
|
88
93
|
}
|
|
@@ -99,6 +104,7 @@ let List = (() => {
|
|
|
99
104
|
const prepared = await (0, session_state_1.prepareListSession)(request);
|
|
100
105
|
if (prepared)
|
|
101
106
|
return prepared;
|
|
107
|
+
this.columnWidths.unserialize(undefined);
|
|
102
108
|
(0, persist_1.load)(this, request.request, type);
|
|
103
109
|
columns.getParams(request.request, type);
|
|
104
110
|
const access = await (0, access_1.listAccess)(request);
|
|
@@ -107,8 +113,17 @@ let List = (() => {
|
|
|
107
113
|
for (const field of column.fields ?? [])
|
|
108
114
|
await access.authorise(`${column.name}.${field}`);
|
|
109
115
|
}
|
|
116
|
+
const canResize = this.canResizeColumns(request);
|
|
117
|
+
const resizing = ('resizeColumn' in request.request.data) || ('columnWidth' in request.request.data);
|
|
118
|
+
if (resizing && (request.request.method !== 'POST'))
|
|
119
|
+
return this.htmlResponse('', 405);
|
|
120
|
+
if (resizing && !canResize)
|
|
121
|
+
return this.htmlResponse('', 403);
|
|
122
|
+
if (!this.columnWidths.getParams(request.request))
|
|
123
|
+
return this.htmlResponse('', 400);
|
|
110
124
|
const batch = typeof request.request.data.loadColumns === 'string';
|
|
111
|
-
if (request.request.data.columns !== undefined && typeof request.request.data.column !== 'string'
|
|
125
|
+
if (resizing || (request.request.data.columns !== undefined && typeof request.request.data.column !== 'string'
|
|
126
|
+
&& !batch)) {
|
|
112
127
|
(0, persist_2.save)(this, request.request, type);
|
|
113
128
|
return this.htmlResponse('', 204);
|
|
114
129
|
}
|
|
@@ -204,7 +219,7 @@ let List = (() => {
|
|
|
204
219
|
const focus = request.request.method === 'POST' ? request.request.data.focusListColumn : undefined;
|
|
205
220
|
const focusColumn = [...columns].find(column => column.field === focus)?.field ?? '';
|
|
206
221
|
return this.htmlTemplateResponse({
|
|
207
|
-
columns, count, filterBar: this.filterBar(request), focusColumn, generalActions, objects,
|
|
222
|
+
canResize, columns, count, filterBar: this.filterBar(request), focusColumn, generalActions, objects,
|
|
208
223
|
root: access.root, search: filter.search,
|
|
209
224
|
selectionActions, selectorBootstrap, selectorKey: (0, selector_state_2.selectorStateKey)(request), type,
|
|
210
225
|
selection: selectionActions.length ? await selection?.view(request, activeSearch, count) : undefined,
|
package/cjs/projection.d.ts
CHANGED
package/cjs/projection.js
CHANGED
package/cjs/session-state.d.ts
CHANGED
package/cjs/session-state.js
CHANGED
|
@@ -7,6 +7,7 @@ exports.prepareListSession = prepareListSession;
|
|
|
7
7
|
exports.listSessionChanged = listSessionChanged;
|
|
8
8
|
exports.comparableListState = comparableListState;
|
|
9
9
|
const route_1 = require("@itrocks/route");
|
|
10
|
+
const column_widths_1 = require("./column-widths");
|
|
10
11
|
const dependencies = {
|
|
11
12
|
changed: () => { },
|
|
12
13
|
prepare: async () => { }
|
|
@@ -18,6 +19,7 @@ function listState(request, type) {
|
|
|
18
19
|
const state = request.session.list?.[(0, route_1.routeOf)(type)] ?? {};
|
|
19
20
|
return structuredClone({
|
|
20
21
|
columns: state.columns,
|
|
22
|
+
...(state.columnWidths && Object.keys(state.columnWidths).length ? { columnWidths: state.columnWidths } : {}),
|
|
21
23
|
filter: state.filter,
|
|
22
24
|
...(state.listQuery ? { listQuery: state.listQuery } : {}),
|
|
23
25
|
sort: state.sort
|
|
@@ -37,6 +39,9 @@ function listSessionChanged(request, type) {
|
|
|
37
39
|
function comparableListState(state) {
|
|
38
40
|
return JSON.stringify({
|
|
39
41
|
columns: state.columns,
|
|
42
|
+
columnWidths: Object.entries(state.columnWidths ?? {})
|
|
43
|
+
.filter(([path, width]) => (0, column_widths_1.isColumnWidth)(width) && (state.columns?.includes(path) ?? true))
|
|
44
|
+
.sort(([left], [right]) => left.localeCompare(right)),
|
|
40
45
|
filter: Object.entries(state.filter ?? {})
|
|
41
46
|
.filter(([, value]) => value !== '')
|
|
42
47
|
.sort(([left], [right]) => left.localeCompare(right)),
|
package/package.json
CHANGED