@coding_doc/sveltedt 0.0.0-stage → 0.1.0-rehearsal.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/NOTICE +52 -0
- package/README.md +618 -2
- package/dist/DataTable.svelte +313 -0
- package/dist/DataTable.svelte.d.ts +26 -0
- package/dist/Pagination.svelte +125 -0
- package/dist/Pagination.svelte.d.ts +5 -0
- package/dist/checks.d.ts +5 -0
- package/dist/checks.js +7 -0
- package/dist/connection/answer.d.ts +6 -0
- package/dist/connection/answer.js +47 -0
- package/dist/connection/connection.svelte.d.ts +16 -0
- package/dist/connection/connection.svelte.js +214 -0
- package/dist/connection/index.d.ts +8 -0
- package/dist/connection/index.js +7 -0
- package/dist/connection/request.d.ts +4 -0
- package/dist/connection/request.js +71 -0
- package/dist/connection/types.d.ts +182 -0
- package/dist/connection/types.js +1 -0
- package/dist/faults.d.ts +6 -0
- package/dist/faults.js +64 -0
- package/dist/filters.d.ts +23 -0
- package/dist/filters.js +65 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +8 -0
- package/dist/messages.d.ts +4 -0
- package/dist/messages.js +62 -0
- package/dist/pager.d.ts +19 -0
- package/dist/pager.js +80 -0
- package/dist/parts/ColumnFilter.svelte +213 -0
- package/dist/parts/ColumnFilter.svelte.d.ts +11 -0
- package/dist/parts/HeaderCell.svelte +90 -0
- package/dist/parts/HeaderCell.svelte.d.ts +34 -0
- package/dist/parts/Search.svelte +79 -0
- package/dist/parts/Search.svelte.d.ts +9 -0
- package/dist/parts/Toolbar.svelte +70 -0
- package/dist/parts/Toolbar.svelte.d.ts +29 -0
- package/dist/picks.svelte.d.ts +6 -0
- package/dist/picks.svelte.js +42 -0
- package/dist/search.svelte.d.ts +15 -0
- package/dist/search.svelte.js +76 -0
- package/dist/sort.d.ts +6 -0
- package/dist/sort.js +32 -0
- package/dist/styles.css +230 -0
- package/dist/types.d.ts +323 -0
- package/dist/types.js +1 -0
- package/dist/validate.d.ts +5 -0
- package/dist/validate.js +201 -0
- package/dist/view.svelte.d.ts +7 -0
- package/dist/view.svelte.js +75 -0
- package/package.json +87 -3
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
import { onMount } from "svelte";
|
|
2
|
+
import { readAnswer } from "./answer.js";
|
|
3
|
+
import { buildRequest, servedIndex, toParams } from "./request.js";
|
|
4
|
+
/**
|
|
5
|
+
* Connects `DataTable` and `Pagination` to a server that speaks the DataTables server-side protocol.
|
|
6
|
+
*
|
|
7
|
+
* Call it in a component's script: it registers an `onMount`, which must run while that component initialises, and
|
|
8
|
+
* Svelte does not run it on the server. It sends one request on mount and one more for each `pager.onpage`,
|
|
9
|
+
* `pager.onsize`, `table.onsort`, `table.onfilter`, `table.onclearfilter`, `table.onclearfilters`, `table.onsearch`,
|
|
10
|
+
* `table.onreset`, `table.onretry` and `reload()` (none when a sort or a filter is refused), and maps each answer; a
|
|
11
|
+
* later request, `table.oncancel` and unmounting abort the one still running. It reads `options` when it builds each
|
|
12
|
+
* request, except the query: it is read once, at creation, and then held by the helper.
|
|
13
|
+
*
|
|
14
|
+
* Read `pager` from the returned object, never destructured from it: it is a getter, so a copy would hold the value it
|
|
15
|
+
* had when it was copied. `table` is a plain object, so `const { table } = connection` is fine; read `table.rows`,
|
|
16
|
+
* `table.status`, `table.busy`, `table.sort`, `table.filters` and `table.search` from it, not destructured.
|
|
17
|
+
*/
|
|
18
|
+
export function createConnection(options) {
|
|
19
|
+
// The last answer mapped, with the query its request was sent with; none before the first
|
|
20
|
+
let answer = $state.raw();
|
|
21
|
+
let status = $state("loading");
|
|
22
|
+
// True while a request runs with an answer on screen to keep
|
|
23
|
+
let busy = $state(false);
|
|
24
|
+
let offset = $state(options.query.offset ?? 0);
|
|
25
|
+
let limit = $state(options.query.limit);
|
|
26
|
+
// The sort of the query, which a reset restores (CONN-14)
|
|
27
|
+
const initialSort = options.query.sort ?? [];
|
|
28
|
+
let sort = $state.raw(initialSort);
|
|
29
|
+
let filters = $state.raw(options.query.filters ?? {});
|
|
30
|
+
let search = $state(options.query.search ?? "");
|
|
31
|
+
// The draw of the last request sent, 0 before the first
|
|
32
|
+
let draw = 0;
|
|
33
|
+
// The controller of the request now running; none once it settled
|
|
34
|
+
let controller;
|
|
35
|
+
// The refusal when an entry of `entries`, or an id of `ids`, names a column that no map entry serves; none otherwise
|
|
36
|
+
function refusal(entries, ids = []) {
|
|
37
|
+
const entry = entries.find((e) => servedIndex(options.columns, e.id) < 0);
|
|
38
|
+
if (entry !== undefined)
|
|
39
|
+
return new Error(`No column map entry serves the sort's column id ${JSON.stringify(entry.id)}.`);
|
|
40
|
+
const id = ids.find((key) => servedIndex(options.columns, key) < 0);
|
|
41
|
+
if (id === undefined)
|
|
42
|
+
return undefined;
|
|
43
|
+
return new Error(`No column map entry serves the filter's column id ${JSON.stringify(id)}.`);
|
|
44
|
+
}
|
|
45
|
+
// Sends the request in the connection's form (the object for JSON, else its bracketed or dotted pairs) and returns the
|
|
46
|
+
// promise of the body
|
|
47
|
+
const send = (request, signal) => options.form === "json"
|
|
48
|
+
? options.send({ request, signal })
|
|
49
|
+
: options.send({
|
|
50
|
+
request: toParams(request, options.form === "dotted"),
|
|
51
|
+
signal,
|
|
52
|
+
});
|
|
53
|
+
function load() {
|
|
54
|
+
const refused = refusal(sort, Object.keys(filters));
|
|
55
|
+
if (refused !== undefined) {
|
|
56
|
+
status = "failed";
|
|
57
|
+
options.onerror(refused);
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
const sent = ++draw;
|
|
61
|
+
controller?.abort();
|
|
62
|
+
controller = new AbortController();
|
|
63
|
+
if (status === "ready")
|
|
64
|
+
busy = true;
|
|
65
|
+
else {
|
|
66
|
+
// Not busy, even when a load refused while a busy request ran left it set (CONN-23)
|
|
67
|
+
status = "loading";
|
|
68
|
+
busy = false;
|
|
69
|
+
}
|
|
70
|
+
const asked = { sort, filters, search, offset, limit };
|
|
71
|
+
const request = buildRequest(options.columns, asked, sent);
|
|
72
|
+
const { signal } = controller;
|
|
73
|
+
// A request that fails for any reason ends the same way: not busy, `'failed'`, then one call of `onerror`. The
|
|
74
|
+
// outcome of a request whose signal was aborted (superseded, or running at unmount) is dropped.
|
|
75
|
+
const fail = (reason) => {
|
|
76
|
+
if (signal.aborted)
|
|
77
|
+
return;
|
|
78
|
+
controller = undefined;
|
|
79
|
+
busy = false;
|
|
80
|
+
status = "failed";
|
|
81
|
+
options.onerror(reason);
|
|
82
|
+
};
|
|
83
|
+
try {
|
|
84
|
+
void send(request, signal).then((body) => {
|
|
85
|
+
if (signal.aborted)
|
|
86
|
+
return;
|
|
87
|
+
const read = readAnswer(body, sent);
|
|
88
|
+
if (read instanceof Error)
|
|
89
|
+
return fail(read);
|
|
90
|
+
controller = undefined;
|
|
91
|
+
busy = false;
|
|
92
|
+
answer = { ...read, asked };
|
|
93
|
+
status = "ready";
|
|
94
|
+
}, fail);
|
|
95
|
+
}
|
|
96
|
+
catch (thrown) {
|
|
97
|
+
fail(thrown);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
function onpage(next) {
|
|
101
|
+
offset = next;
|
|
102
|
+
load();
|
|
103
|
+
}
|
|
104
|
+
function onsort(next) {
|
|
105
|
+
const refused = refusal(next);
|
|
106
|
+
if (refused !== undefined)
|
|
107
|
+
return options.onerror(refused);
|
|
108
|
+
sort = next;
|
|
109
|
+
offset = 0;
|
|
110
|
+
load();
|
|
111
|
+
}
|
|
112
|
+
function onfilter(change) {
|
|
113
|
+
const refused = refusal([], [change.id]);
|
|
114
|
+
if (refused !== undefined)
|
|
115
|
+
return options.onerror(refused);
|
|
116
|
+
filters = { ...filters, [change.id]: change.state };
|
|
117
|
+
offset = 0;
|
|
118
|
+
load();
|
|
119
|
+
}
|
|
120
|
+
function onclearfilter(change) {
|
|
121
|
+
filters = Object.fromEntries(Object.entries(filters).filter(([id]) => id !== change.id));
|
|
122
|
+
offset = 0;
|
|
123
|
+
load();
|
|
124
|
+
}
|
|
125
|
+
function onclearfilters() {
|
|
126
|
+
filters = {};
|
|
127
|
+
offset = 0;
|
|
128
|
+
load();
|
|
129
|
+
}
|
|
130
|
+
function onsearch(text) {
|
|
131
|
+
search = text;
|
|
132
|
+
offset = 0;
|
|
133
|
+
load();
|
|
134
|
+
}
|
|
135
|
+
// Restores the initial sort and clears the filters and the search; the page size is kept (CONN-14)
|
|
136
|
+
function onreset() {
|
|
137
|
+
sort = initialSort;
|
|
138
|
+
filters = {};
|
|
139
|
+
search = "";
|
|
140
|
+
offset = 0;
|
|
141
|
+
load();
|
|
142
|
+
}
|
|
143
|
+
function onsize(size) {
|
|
144
|
+
offset -= offset % size;
|
|
145
|
+
limit = size;
|
|
146
|
+
load();
|
|
147
|
+
}
|
|
148
|
+
// Ends the running request, whose outcome is then dropped as aborted: with an answer on screen the query returns to
|
|
149
|
+
// the one that answer was asked with and no request is sent (CONN-33); with none, the load stops and keeps the query
|
|
150
|
+
// (CONN-34). With nothing running it does nothing (CONN-36).
|
|
151
|
+
function oncancel() {
|
|
152
|
+
if (controller === undefined)
|
|
153
|
+
return;
|
|
154
|
+
controller.abort();
|
|
155
|
+
controller = undefined;
|
|
156
|
+
if (busy && answer !== undefined) {
|
|
157
|
+
busy = false;
|
|
158
|
+
// The rows on screen are that answer's again, so a failure written meanwhile (a refused load) no longer holds
|
|
159
|
+
status = "ready";
|
|
160
|
+
sort = answer.asked.sort;
|
|
161
|
+
filters = answer.asked.filters;
|
|
162
|
+
search = answer.asked.search;
|
|
163
|
+
offset = answer.asked.offset;
|
|
164
|
+
limit = answer.asked.limit;
|
|
165
|
+
}
|
|
166
|
+
else
|
|
167
|
+
status = "stopped";
|
|
168
|
+
}
|
|
169
|
+
onMount(() => {
|
|
170
|
+
load();
|
|
171
|
+
return () => controller?.abort();
|
|
172
|
+
});
|
|
173
|
+
return {
|
|
174
|
+
table: {
|
|
175
|
+
get rows() {
|
|
176
|
+
return answer?.rows ?? [];
|
|
177
|
+
},
|
|
178
|
+
get status() {
|
|
179
|
+
return status;
|
|
180
|
+
},
|
|
181
|
+
get busy() {
|
|
182
|
+
return busy;
|
|
183
|
+
},
|
|
184
|
+
get sort() {
|
|
185
|
+
return sort;
|
|
186
|
+
},
|
|
187
|
+
onsort,
|
|
188
|
+
get filters() {
|
|
189
|
+
return filters;
|
|
190
|
+
},
|
|
191
|
+
get search() {
|
|
192
|
+
return search;
|
|
193
|
+
},
|
|
194
|
+
onfilter,
|
|
195
|
+
onclearfilter,
|
|
196
|
+
onclearfilters,
|
|
197
|
+
onsearch,
|
|
198
|
+
onreset,
|
|
199
|
+
oncancel,
|
|
200
|
+
onretry: load,
|
|
201
|
+
},
|
|
202
|
+
get pager() {
|
|
203
|
+
return (answer && {
|
|
204
|
+
offset,
|
|
205
|
+
limit,
|
|
206
|
+
total: answer.total,
|
|
207
|
+
unfilteredTotal: answer.unfilteredTotal,
|
|
208
|
+
onpage,
|
|
209
|
+
onsize,
|
|
210
|
+
});
|
|
211
|
+
},
|
|
212
|
+
reload: load,
|
|
213
|
+
};
|
|
214
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@coding_doc/sveltedt/connection`: the connection helper, which connects `DataTable` and `Pagination` to a server that
|
|
3
|
+
* speaks the DataTables server-side protocol.
|
|
4
|
+
*
|
|
5
|
+
* @module connection
|
|
6
|
+
*/
|
|
7
|
+
export { createConnection } from "./connection.svelte.js";
|
|
8
|
+
export type { Connection, ConnectionColumn, ConnectionJsonOptions, ConnectionOptions, ConnectionParamsOptions, ConnectionQuery, ConnectionRequest, } from "./types.ts";
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { ConnectionColumn, ConnectionQuery, ConnectionRequest } from "./types.ts";
|
|
2
|
+
export declare const servedIndex: (columns: readonly ConnectionColumn[], id: string | undefined) => number;
|
|
3
|
+
export declare function buildRequest(columns: readonly ConnectionColumn[], query: Required<ConnectionQuery>, draw: number): ConnectionRequest;
|
|
4
|
+
export declare function toParams(request: ConnectionRequest, dotted: boolean): URLSearchParams;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
// The name the server knows a column by: none given is the empty text
|
|
2
|
+
const nameOf = (column) => column.name ?? "";
|
|
3
|
+
// The index of the first map entry serving `id`, -1 when none does; an entry without an `id` is served by none
|
|
4
|
+
export const servedIndex = (columns, id) => (id === undefined ? -1 : columns.findIndex((c) => c.id === id));
|
|
5
|
+
// A new empty search for each use: in the JSON form the application holds the request, and an edit must reach no other
|
|
6
|
+
const noSearch = () => ({ value: "", regex: false });
|
|
7
|
+
// One selected value with each of `/ . * + ? | ( ) [ ] { } \ $ ^ -` behind a backslash: the set of DataTables' own
|
|
8
|
+
// escapeRegex (CONN-10)
|
|
9
|
+
const escaped = (value) => value.replace(/[/.*+?|()[\]{}\\$^-]/g, "\\$&");
|
|
10
|
+
// The column search a filter sends: a text as given (CONN-09); a selection as an anchored alternation of its escaped
|
|
11
|
+
// values, in the order selected, with regex true (CONN-10); an empty search ("", false) for no filter and for an empty
|
|
12
|
+
// selection
|
|
13
|
+
function searchOf(filter) {
|
|
14
|
+
if (filter === undefined)
|
|
15
|
+
return noSearch();
|
|
16
|
+
if (filter.kind === "text")
|
|
17
|
+
return { value: filter.text, regex: false };
|
|
18
|
+
if (filter.selected.length === 0)
|
|
19
|
+
return noSearch();
|
|
20
|
+
return {
|
|
21
|
+
value: `^(${filter.selected.map(escaped).join("|")})$`,
|
|
22
|
+
regex: true,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
// The request for one query as the plain object: the paging and search keys, the column entries, then one order entry
|
|
26
|
+
// per sort entry (spec/connection.md, section 1)
|
|
27
|
+
export function buildRequest(columns, query, draw) {
|
|
28
|
+
return {
|
|
29
|
+
draw,
|
|
30
|
+
start: query.offset,
|
|
31
|
+
length: query.limit,
|
|
32
|
+
search: { value: query.search, regex: false },
|
|
33
|
+
columns: columns.map((column, i) => ({
|
|
34
|
+
data: column.data,
|
|
35
|
+
name: nameOf(column),
|
|
36
|
+
searchable: column.searchable ?? true,
|
|
37
|
+
orderable: column.orderable ?? true,
|
|
38
|
+
// The first filter whose column id this entry serves; looked up among the filters' own keys, so an id such as
|
|
39
|
+
// `constructor` reads nothing inherited
|
|
40
|
+
search: searchOf(Object.entries(query.filters).find(([id]) => servedIndex(columns, id) === i)?.[1]),
|
|
41
|
+
})),
|
|
42
|
+
order: query.sort.map((entry) => {
|
|
43
|
+
const column = servedIndex(columns, entry.id);
|
|
44
|
+
return {
|
|
45
|
+
column,
|
|
46
|
+
dir: entry.direction,
|
|
47
|
+
name: nameOf(columns[column]),
|
|
48
|
+
};
|
|
49
|
+
}),
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
// The key of the entry `k` of the value at `key`: `k` itself at the top, else `[k]`, or `.k` when `dot` (dotted, and the
|
|
53
|
+
// value is no list: a list index is always `[i]`)
|
|
54
|
+
const below = (key, k, dot) => key === "" ? k : dot ? `${key}.${k}` : `${key}[${k}]`;
|
|
55
|
+
// The pairs of a request, bracketed or dotted: a list index is always written as `[i]`, an object key below the top as
|
|
56
|
+
// `[key]` or `.key`
|
|
57
|
+
export function toParams(request, dotted) {
|
|
58
|
+
const params = new URLSearchParams();
|
|
59
|
+
const walk = (value, key) => {
|
|
60
|
+
// Stryker disable next-line ConditionalExpression: equivalent, no value of a request is null
|
|
61
|
+
if (typeof value === "object" && value !== null) {
|
|
62
|
+
const dot = dotted && !Array.isArray(value);
|
|
63
|
+
for (const [k, v] of Object.entries(value))
|
|
64
|
+
walk(v, below(key, k, dot));
|
|
65
|
+
}
|
|
66
|
+
else
|
|
67
|
+
params.set(key, String(value));
|
|
68
|
+
};
|
|
69
|
+
walk(request, "");
|
|
70
|
+
return params;
|
|
71
|
+
}
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import type { DataTableFilterState, DataTableProps, DataTableSortEntry, PaginationProps, SortDirection } from "../types.ts";
|
|
2
|
+
/**
|
|
3
|
+
* One entry of the column map: entry `i` describes the backend's column `i`, so the order of the list is the
|
|
4
|
+
* backend's order.
|
|
5
|
+
*/
|
|
6
|
+
export interface ConnectionColumn {
|
|
7
|
+
/** The column's data source on the server, sent as `columns[i][data]`. */
|
|
8
|
+
readonly data: string;
|
|
9
|
+
/** The name the server knows the column by, never a free label; default `""`. */
|
|
10
|
+
readonly name?: string;
|
|
11
|
+
/** The table column id the entry serves; absent: no table column sorts or filters by it. */
|
|
12
|
+
readonly id?: string;
|
|
13
|
+
/**
|
|
14
|
+
* Sent to the server as given, `true` when not given, as in DataTables; the helper does not enforce it: a filter on
|
|
15
|
+
* an entry with `searchable: false` is still sent.
|
|
16
|
+
*/
|
|
17
|
+
readonly searchable?: boolean;
|
|
18
|
+
/**
|
|
19
|
+
* Sent to the server as given, `true` when not given, as in DataTables; the helper does not enforce it: a sort on an
|
|
20
|
+
* entry with `orderable: false` is still sent.
|
|
21
|
+
*/
|
|
22
|
+
readonly orderable?: boolean;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* The query. The helper reads the whole query once, at creation, and then holds the live query: read it back from
|
|
26
|
+
* `table.sort`, `table.filters`, `table.search` and `pager` (`pager` is `undefined` before the first answer).
|
|
27
|
+
*/
|
|
28
|
+
export interface ConnectionQuery {
|
|
29
|
+
/** The order, the lead first; default none. */
|
|
30
|
+
readonly sort?: readonly DataTableSortEntry[];
|
|
31
|
+
/** The applied filter of each column, by table column id; default none. */
|
|
32
|
+
readonly filters?: Readonly<Record<string, DataTableFilterState>>;
|
|
33
|
+
/** The global search text, sent as given; default `""`. */
|
|
34
|
+
readonly search?: string;
|
|
35
|
+
/** Where the first page starts, a row index; default 0. */
|
|
36
|
+
readonly offset?: number;
|
|
37
|
+
/** The page size. */
|
|
38
|
+
readonly limit: number;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* The request as the plain object of the JSON form (`form: "json"`) a DataTables 2.x client builds, without its
|
|
42
|
+
* `fixed` lists; the other forms receive the same fields as a `URLSearchParams`.
|
|
43
|
+
*/
|
|
44
|
+
export interface ConnectionRequest {
|
|
45
|
+
/** The request's number, counting from 1; an answer that gives `draw` must give this one. */
|
|
46
|
+
readonly draw: number;
|
|
47
|
+
/** The query's offset. */
|
|
48
|
+
readonly start: number;
|
|
49
|
+
/** The query's limit. */
|
|
50
|
+
readonly length: number;
|
|
51
|
+
/** The global search: its `value` the search text, its `regex` always false. */
|
|
52
|
+
readonly search: {
|
|
53
|
+
/** The search text. */
|
|
54
|
+
readonly value: string;
|
|
55
|
+
/** Always false: the text is never a regular expression. */
|
|
56
|
+
readonly regex: boolean;
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* One entry per column map entry, in its order: `data`, `name`, `searchable` and `orderable` as the map gives them
|
|
60
|
+
* (`name` `""` and the booleans `true` when not given), and `search`, the column's filter: a text filter's text with
|
|
61
|
+
* `regex` false, a categorical filter's selected values as the pattern `^(v1|v2)$` (each escaped) with `regex` true,
|
|
62
|
+
* or `""` and false without a filter or with no option selected.
|
|
63
|
+
*/
|
|
64
|
+
readonly columns: readonly {
|
|
65
|
+
/** The map entry's `data`. */
|
|
66
|
+
readonly data: string;
|
|
67
|
+
/** The map entry's `name`, `""` when not given. */
|
|
68
|
+
readonly name: string;
|
|
69
|
+
/** The map entry's `searchable`, `true` when not given. */
|
|
70
|
+
readonly searchable: boolean;
|
|
71
|
+
/** The map entry's `orderable`, `true` when not given. */
|
|
72
|
+
readonly orderable: boolean;
|
|
73
|
+
/** The column's filter. */
|
|
74
|
+
readonly search: {
|
|
75
|
+
/** A text filter's text, a categorical filter's pattern, or `""` without a filter or a selected option. */
|
|
76
|
+
readonly value: string;
|
|
77
|
+
/** True for a categorical filter's pattern, else false. */
|
|
78
|
+
readonly regex: boolean;
|
|
79
|
+
};
|
|
80
|
+
}[];
|
|
81
|
+
/**
|
|
82
|
+
* The order, the lead first: each entry's `column` the index of the map entry serving the sorted column id, `dir`
|
|
83
|
+
* its direction and `name` that entry's `name`.
|
|
84
|
+
*/
|
|
85
|
+
readonly order: readonly {
|
|
86
|
+
/** The index of the map entry that serves the sorted column. */
|
|
87
|
+
readonly column: number;
|
|
88
|
+
/** The direction. */
|
|
89
|
+
readonly dir: SortDirection;
|
|
90
|
+
/** The map entry's `name`. */
|
|
91
|
+
readonly name: string;
|
|
92
|
+
}[];
|
|
93
|
+
}
|
|
94
|
+
interface ConnectionBaseOptions {
|
|
95
|
+
/** The column map: entry `i` describes the backend's column `i`. */
|
|
96
|
+
readonly columns: readonly ConnectionColumn[];
|
|
97
|
+
/** The initial query, read once, at creation. */
|
|
98
|
+
readonly query: ConnectionQuery;
|
|
99
|
+
/**
|
|
100
|
+
* Called once for each failure: an answer's failure (an answer that cannot be mapped, its message naming the check
|
|
101
|
+
* that failed, or a truthy `error`), a `send` failure passed as it is (a rejection or a thrown value; a missing
|
|
102
|
+
* promise is a `TypeError` the helper provokes), and a refused sort or filter (a sort entry or a filter names a
|
|
103
|
+
* column id that no map entry serves). For a failure of a sent request, `table.busy` is false and `table.status` is
|
|
104
|
+
* `'failed'` before the call; the same status holds for a refusal on the load path (which leaves `busy` as it was);
|
|
105
|
+
* a refused `onsort` or `onfilter` leaves both as they were.
|
|
106
|
+
*
|
|
107
|
+
* An answer's failure carries `code`, a string or `null`; a rejection is your `send`'s own error: if your
|
|
108
|
+
* transport's errors carry a string `code` (axios does), tell them apart in `send`.
|
|
109
|
+
*
|
|
110
|
+
* A rejection's value is passed as it is and may hold the request's headers: do not forward it to telemetry
|
|
111
|
+
* unredacted. `code` is the server's string, untrusted text: never render it as HTML.
|
|
112
|
+
*
|
|
113
|
+
* Stated limits: `onerror` must not throw; a refused `reload()` while a request runs (reachable only by changing
|
|
114
|
+
* the column map's entries meanwhile) sets `'failed'` and leaves `busy` and that request as they are, the request's
|
|
115
|
+
* own end then applying; a `send` that throws synchronously, with an `onerror` that calls `reload()`, repeats
|
|
116
|
+
* without end, and so does a refused sort or filter on the load path with an `onerror` that calls `reload()`: both
|
|
117
|
+
* are synchronous recursion that ends in a stack overflow.
|
|
118
|
+
*/
|
|
119
|
+
readonly onerror: (error: unknown) => void;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* The options of the bracketed and the dotted form: `send` receives a `URLSearchParams`. Type a separately written
|
|
123
|
+
* `send` by its member, not by `ConnectionOptions["send"]`. Stated limit: a `form` the types do not allow (plain
|
|
124
|
+
* JavaScript) is sent bracketed.
|
|
125
|
+
*/
|
|
126
|
+
export interface ConnectionParamsOptions extends ConnectionBaseOptions {
|
|
127
|
+
/**
|
|
128
|
+
* The application's transport: it resolves with the parsed answer body, not a `Response`. The helper calls it with
|
|
129
|
+
* one object holding the `request` and an abort `signal`, which is aborted when a later request is sent, the request
|
|
130
|
+
* is cancelled (`table.oncancel`) or the component unmounts while this one runs. A rejection, a synchronous throw or
|
|
131
|
+
* a missing promise is a failure, passed to `onerror` as it is.
|
|
132
|
+
*/
|
|
133
|
+
readonly send: (context: {
|
|
134
|
+
readonly request: URLSearchParams;
|
|
135
|
+
readonly signal: AbortSignal;
|
|
136
|
+
}) => Promise<unknown>;
|
|
137
|
+
/**
|
|
138
|
+
* The form of the request: the bracketed one when not given; the dotted one keeps list indexes in brackets and joins
|
|
139
|
+
* object keys with a dot.
|
|
140
|
+
*/
|
|
141
|
+
readonly form?: "bracketed" | "dotted";
|
|
142
|
+
}
|
|
143
|
+
/** The options of the JSON form: `send` receives the plain object, which the application stringifies itself. */
|
|
144
|
+
export interface ConnectionJsonOptions extends ConnectionBaseOptions {
|
|
145
|
+
/**
|
|
146
|
+
* The application's transport, as in {@link ConnectionParamsOptions}, called with the `request` as a
|
|
147
|
+
* {@link ConnectionRequest} and the abort `signal`.
|
|
148
|
+
*/
|
|
149
|
+
readonly send: (context: {
|
|
150
|
+
readonly request: ConnectionRequest;
|
|
151
|
+
readonly signal: AbortSignal;
|
|
152
|
+
}) => Promise<unknown>;
|
|
153
|
+
/** The JSON form. */
|
|
154
|
+
readonly form: "json";
|
|
155
|
+
}
|
|
156
|
+
/** The options of `createConnection`: one of the two forms' options. */
|
|
157
|
+
export type ConnectionOptions = ConnectionParamsOptions | ConnectionJsonOptions;
|
|
158
|
+
/**
|
|
159
|
+
* What `createConnection` returns: the props of `DataTable` and, once an answer is mapped, of `Pagination`. `Row` is
|
|
160
|
+
* the application's claim about the rows of its answers, which the helper passes on unread.
|
|
161
|
+
*/
|
|
162
|
+
export interface Connection<Row> {
|
|
163
|
+
/**
|
|
164
|
+
* The props of `DataTable` the helper holds: pass each to the table's prop of the same name, beside the
|
|
165
|
+
* application's own (columns, caption and the rest).
|
|
166
|
+
*/
|
|
167
|
+
readonly table: Readonly<Required<Pick<DataTableProps<Row>, "rows" | "status" | "busy" | "onfilter" | "onclearfilter" | "onclearfilters" | "onsearch" | "onreset" | "oncancel" | "onretry">>> & {
|
|
168
|
+
/** The applied order, the lead first. */
|
|
169
|
+
readonly sort: readonly DataTableSortEntry[];
|
|
170
|
+
/** Sends the next order. */
|
|
171
|
+
readonly onsort: NonNullable<DataTableProps<Row>["onsort"]>;
|
|
172
|
+
/** The applied filters, by column id. */
|
|
173
|
+
readonly filters: Readonly<Record<string, DataTableFilterState>>;
|
|
174
|
+
/** The applied global search text. */
|
|
175
|
+
readonly search: string;
|
|
176
|
+
};
|
|
177
|
+
/** The props of `Pagination` once an answer is mapped; `undefined` before the first answer. */
|
|
178
|
+
readonly pager: Readonly<Required<Pick<PaginationProps, "offset" | "limit" | "total" | "unfilteredTotal" | "onpage" | "onsize">>> | undefined;
|
|
179
|
+
/** Sends the current query again, the page included; `table.onretry` is the same function. */
|
|
180
|
+
readonly reload: () => void;
|
|
181
|
+
}
|
|
182
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/faults.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { PagerFault } from "./pager.ts";
|
|
2
|
+
export declare const SORT_FILTER_FAULTS: readonly ["sortableInvalid", "sortHandlerMissing", "sortInvalid", "sortColumnUnknown", "sortColumnNotSortable", "sortDirectionNotAllowed", "sortColumnDuplicate", "filterInvalid", "filterHandlerMissing", "filterStateInvalid", "searchInvalid", "searchDelayInvalid", "maxLengthInvalid"];
|
|
3
|
+
export type TableFault = "captionEmpty" | "regionLabelEmpty" | "noColumns" | "columnIdEmpty" | "columnIdDuplicate" | "columnLabelEmpty" | "columnLabelDuplicate" | "columnContentMissing" | "statusUnknown" | "rowsNotList" | "rowIdEmpty" | "rowIdDuplicate" | "rowIdUnreadable" | (typeof SORT_FILTER_FAULTS)[number];
|
|
4
|
+
type FaultCode = TableFault | PagerFault;
|
|
5
|
+
export declare function faultLine(code: FaultCode): string;
|
|
6
|
+
export {};
|
package/dist/faults.js
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { DEV } from "esm-env";
|
|
2
|
+
// The codes of SORT-15 and FILT-32. When one of these is the fault, the columns have passed RND-05's checks (id, label,
|
|
3
|
+
// content); their own sort or filter settings may be the fault. So the controls that read the columns (FILT-30) can
|
|
4
|
+
// still show. A new SORT or FILT code goes in this array, not in the union below.
|
|
5
|
+
export const SORT_FILTER_FAULTS = [
|
|
6
|
+
"sortableInvalid",
|
|
7
|
+
"sortHandlerMissing",
|
|
8
|
+
"sortInvalid",
|
|
9
|
+
"sortColumnUnknown",
|
|
10
|
+
"sortColumnNotSortable",
|
|
11
|
+
"sortDirectionNotAllowed",
|
|
12
|
+
"sortColumnDuplicate",
|
|
13
|
+
"filterInvalid",
|
|
14
|
+
"filterHandlerMissing",
|
|
15
|
+
"filterStateInvalid",
|
|
16
|
+
"searchInvalid",
|
|
17
|
+
"searchDelayInvalid",
|
|
18
|
+
"maxLengthInvalid",
|
|
19
|
+
];
|
|
20
|
+
// The line of RND-17 for the table and of PAGE-41 for the pager. The texts are for the developer: English only, no
|
|
21
|
+
// entry of `messages`, and none holds a value from the input (RND-09, PAGE-41). They sit behind DEV, so a production
|
|
22
|
+
// bundle drops them and keeps the code (CON-14 proves it on the consumer's build).
|
|
23
|
+
export function faultLine(code) {
|
|
24
|
+
if (!DEV)
|
|
25
|
+
return `SvelteDT: ${code}`;
|
|
26
|
+
const texts = {
|
|
27
|
+
captionEmpty: "Table caption must not be empty.",
|
|
28
|
+
regionLabelEmpty: "Table region label must not be empty.",
|
|
29
|
+
noColumns: "Table has no columns.",
|
|
30
|
+
columnIdEmpty: "A column id is empty.",
|
|
31
|
+
columnIdDuplicate: "Two columns share an id.",
|
|
32
|
+
columnLabelEmpty: "A column label is empty.",
|
|
33
|
+
columnLabelDuplicate: "Two columns share a label.",
|
|
34
|
+
columnContentMissing: "A column has no value and no cell.",
|
|
35
|
+
statusUnknown: "Table status is not known.",
|
|
36
|
+
rowsNotList: "Table has no list of rows.",
|
|
37
|
+
rowIdEmpty: "A row id is empty.",
|
|
38
|
+
rowIdDuplicate: "Two rows share an id.",
|
|
39
|
+
rowIdUnreadable: "A row id could not be read.",
|
|
40
|
+
sortableInvalid: "A column's sort directions are invalid.",
|
|
41
|
+
sortHandlerMissing: "A sortable column has no sort handler.",
|
|
42
|
+
sortInvalid: "The sort is not valid.",
|
|
43
|
+
sortColumnUnknown: "The sort names an unknown column.",
|
|
44
|
+
sortColumnNotSortable: "The sort names a column that cannot be sorted.",
|
|
45
|
+
sortDirectionNotAllowed: "The sort uses a direction a column does not allow.",
|
|
46
|
+
sortColumnDuplicate: "The sort names a column twice.",
|
|
47
|
+
filterInvalid: "A column filter is not valid.",
|
|
48
|
+
filterHandlerMissing: "A filter has no handler.",
|
|
49
|
+
filterStateInvalid: "An applied filter is not valid.",
|
|
50
|
+
searchInvalid: "The applied search is not valid.",
|
|
51
|
+
searchDelayInvalid: "The search delay is not a valid number.",
|
|
52
|
+
maxLengthInvalid: "The text length limit is not a valid number.",
|
|
53
|
+
totalInvalid: "The row total is not a valid number.",
|
|
54
|
+
offsetInvalid: "The page offset is not a valid number.",
|
|
55
|
+
limitInvalid: "The page size is not a valid number.",
|
|
56
|
+
labelEmpty: "Pager label must not be empty.",
|
|
57
|
+
sizesInvalid: "The page sizes are not valid.",
|
|
58
|
+
limitNotInSizes: "The page size is not one of the offered sizes.",
|
|
59
|
+
pageHandlerMissing: "The pager has no page handler.",
|
|
60
|
+
sizeHandlerMissing: "The page-size select has no handler.",
|
|
61
|
+
unfilteredTotalInvalid: "The unfiltered row total is not a valid number.",
|
|
62
|
+
};
|
|
63
|
+
return `SvelteDT: ${code}: ${texts[code]}`;
|
|
64
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { TableFault } from "./faults.ts";
|
|
2
|
+
import type { DataTableColumn, DataTableFilter, DataTableMessages, DataTableProps } from "./types.ts";
|
|
3
|
+
type CategoricalFilter = Extract<DataTableFilter, {
|
|
4
|
+
kind: "categorical";
|
|
5
|
+
}>;
|
|
6
|
+
export declare function offered(filter: CategoricalFilter, emptyLabel: string): {
|
|
7
|
+
value: string;
|
|
8
|
+
label: string;
|
|
9
|
+
}[];
|
|
10
|
+
export declare function narrowed<Option extends {
|
|
11
|
+
label: string;
|
|
12
|
+
}>(options: readonly Option[], find: string): Option[];
|
|
13
|
+
export interface Badge {
|
|
14
|
+
id: string;
|
|
15
|
+
text: string;
|
|
16
|
+
}
|
|
17
|
+
export declare function badges<Row>(columns: readonly DataTableColumn<Row>[], filters: DataTableProps<Row>["filters"], words: BadgeWords): Badge[];
|
|
18
|
+
type BadgeWords = Pick<DataTableMessages, "badgeText" | "badgeOptions" | "emptyOption">;
|
|
19
|
+
export declare function toggled(options: readonly {
|
|
20
|
+
value: string;
|
|
21
|
+
}[], isSelected: (value: string) => boolean, pressed: string): string[];
|
|
22
|
+
export declare function resetShown<Row>(fault: TableFault | null, columns: readonly DataTableColumn<Row>[], onreset: DataTableProps<Row>["onreset"]): boolean;
|
|
23
|
+
export {};
|
package/dist/filters.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { SORT_FILTER_FAULTS } from "./faults.js";
|
|
2
|
+
// The options a categorical filter offers: its given ones, then with `emptyOption` one more for no value, last, whose
|
|
3
|
+
// value is '' (FILT-53)
|
|
4
|
+
export function offered(filter, emptyLabel) {
|
|
5
|
+
return filter.emptyOption
|
|
6
|
+
? [...filter.options, { value: "", label: emptyLabel }]
|
|
7
|
+
: [...filter.options];
|
|
8
|
+
}
|
|
9
|
+
// The options whose label contains `find`, ignoring case, in their order (FILT-18). Adapted from ontoprism,
|
|
10
|
+
// frontend/src/lib/components/data-table/DataTableFilterPopover.svelte (Apache License 2.0, the owner's own project)
|
|
11
|
+
export function narrowed(options, find) {
|
|
12
|
+
const text = find.toLowerCase();
|
|
13
|
+
return options.filter((option) => option.label.toLowerCase().includes(text));
|
|
14
|
+
}
|
|
15
|
+
// The badges of the applied filters, in column order whatever the key order of `filters` (FILT-31). A text filter
|
|
16
|
+
// applies while its text is not blank, a categorical one while something is selected; the input is valid here
|
|
17
|
+
// (FILT-32). A categorical badge names the selected options by label in the options' order, the empty option last
|
|
18
|
+
// (FILT-22).
|
|
19
|
+
export function badges(columns, filters, words) {
|
|
20
|
+
const found = [];
|
|
21
|
+
for (const column of columns) {
|
|
22
|
+
const badge = badgeText(column, filters?.[column.id], words);
|
|
23
|
+
if (badge !== null)
|
|
24
|
+
found.push(badge);
|
|
25
|
+
}
|
|
26
|
+
return found;
|
|
27
|
+
}
|
|
28
|
+
// A column's badge, or null while its state applies nothing
|
|
29
|
+
function badgeText(column, state, words) {
|
|
30
|
+
if (state === undefined)
|
|
31
|
+
return null;
|
|
32
|
+
if (state.kind === "text")
|
|
33
|
+
return state.text.trim() === ""
|
|
34
|
+
? null
|
|
35
|
+
: {
|
|
36
|
+
id: column.id,
|
|
37
|
+
text: words.badgeText({ label: column.label, text: state.text }),
|
|
38
|
+
};
|
|
39
|
+
if (state.selected.length === 0)
|
|
40
|
+
return null;
|
|
41
|
+
// An applied categorical state sits on a column whose filter is categorical (FILT-32 refuses the rest)
|
|
42
|
+
const options = offered(column.filter, words.emptyOption)
|
|
43
|
+
.filter((option) => state.selected.includes(option.value))
|
|
44
|
+
.map((option) => option.label);
|
|
45
|
+
return {
|
|
46
|
+
id: column.id,
|
|
47
|
+
text: words.badgeOptions({ label: column.label, options }),
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
// The values a categorical filter selects after a press on `pressed`: its options' values in the options' order, each
|
|
51
|
+
// kept while `isSelected` holds it, the pressed one flipped (FILT-04)
|
|
52
|
+
export function toggled(options, isSelected, pressed) {
|
|
53
|
+
return options
|
|
54
|
+
.map((option) => option.value)
|
|
55
|
+
.filter((value) => value === pressed ? !isSelected(value) : isSelected(value));
|
|
56
|
+
}
|
|
57
|
+
// "Reset table" shows while `onreset` is given and a column is sortable or has a filter. The alert state keeps it for
|
|
58
|
+
// a fault of SORT-15 or FILT-32, where the columns have passed RND-05's checks (their own sort or filter settings may
|
|
59
|
+
// be the fault): it is the application's way back from a server answer the table cannot show (FILT-30).
|
|
60
|
+
export function resetShown(fault, columns, onreset) {
|
|
61
|
+
const keepsReset = SORT_FILTER_FAULTS;
|
|
62
|
+
return (typeof onreset === "function" &&
|
|
63
|
+
(fault === null || keepsReset.includes(fault)) &&
|
|
64
|
+
columns.some((column) => column.sortable || column.filter));
|
|
65
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@coding_doc/sveltedt`: the table and the pager. Their look comes from the stylesheet `@coding_doc/sveltedt/styles.css`,
|
|
3
|
+
* whose tokens and classes are in [the styling guide](https://github.com/hniedner/sveltedt/blob/main/docs/styling.md).
|
|
4
|
+
*
|
|
5
|
+
* @module sveltedt
|
|
6
|
+
*/
|
|
7
|
+
export { default as DataTable } from "./DataTable.svelte";
|
|
8
|
+
export { default as Pagination } from "./Pagination.svelte";
|
|
9
|
+
export type { DataTableColumn, DataTableFilter, DataTableFilterOption, DataTableFilterState, DataTableMessages, DataTableProps, DataTableSortEntry, DataTableStatus, PaginationMessages, PaginationProps, SortDirection, } from "./types.ts";
|