@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.
Files changed (51) hide show
  1. package/LICENSE +21 -0
  2. package/NOTICE +52 -0
  3. package/README.md +618 -2
  4. package/dist/DataTable.svelte +313 -0
  5. package/dist/DataTable.svelte.d.ts +26 -0
  6. package/dist/Pagination.svelte +125 -0
  7. package/dist/Pagination.svelte.d.ts +5 -0
  8. package/dist/checks.d.ts +5 -0
  9. package/dist/checks.js +7 -0
  10. package/dist/connection/answer.d.ts +6 -0
  11. package/dist/connection/answer.js +47 -0
  12. package/dist/connection/connection.svelte.d.ts +16 -0
  13. package/dist/connection/connection.svelte.js +214 -0
  14. package/dist/connection/index.d.ts +8 -0
  15. package/dist/connection/index.js +7 -0
  16. package/dist/connection/request.d.ts +4 -0
  17. package/dist/connection/request.js +71 -0
  18. package/dist/connection/types.d.ts +182 -0
  19. package/dist/connection/types.js +1 -0
  20. package/dist/faults.d.ts +6 -0
  21. package/dist/faults.js +64 -0
  22. package/dist/filters.d.ts +23 -0
  23. package/dist/filters.js +65 -0
  24. package/dist/index.d.ts +9 -0
  25. package/dist/index.js +8 -0
  26. package/dist/messages.d.ts +4 -0
  27. package/dist/messages.js +62 -0
  28. package/dist/pager.d.ts +19 -0
  29. package/dist/pager.js +80 -0
  30. package/dist/parts/ColumnFilter.svelte +213 -0
  31. package/dist/parts/ColumnFilter.svelte.d.ts +11 -0
  32. package/dist/parts/HeaderCell.svelte +90 -0
  33. package/dist/parts/HeaderCell.svelte.d.ts +34 -0
  34. package/dist/parts/Search.svelte +79 -0
  35. package/dist/parts/Search.svelte.d.ts +9 -0
  36. package/dist/parts/Toolbar.svelte +70 -0
  37. package/dist/parts/Toolbar.svelte.d.ts +29 -0
  38. package/dist/picks.svelte.d.ts +6 -0
  39. package/dist/picks.svelte.js +42 -0
  40. package/dist/search.svelte.d.ts +15 -0
  41. package/dist/search.svelte.js +76 -0
  42. package/dist/sort.d.ts +6 -0
  43. package/dist/sort.js +32 -0
  44. package/dist/styles.css +230 -0
  45. package/dist/types.d.ts +323 -0
  46. package/dist/types.js +1 -0
  47. package/dist/validate.d.ts +5 -0
  48. package/dist/validate.js +201 -0
  49. package/dist/view.svelte.d.ts +7 -0
  50. package/dist/view.svelte.js +75 -0
  51. package/package.json +87 -3
@@ -0,0 +1,323 @@
1
+ import type { Snippet } from "svelte";
2
+ /**
3
+ * One column of the table. A cell shows the column's `cell` snippet when it has one, else the text that `value`
4
+ * returns for the row; a column has at least one of the two (the table refuses a column with neither, for callers the
5
+ * type does not reach).
6
+ *
7
+ * - `value({ row })`: the cell's text for the row.
8
+ * - `cell`: a snippet that renders the cell; its argument holds the `row` and this `column`.
9
+ */
10
+ export type DataTableColumn<Row> = {
11
+ /** The column's id, unique among the columns; sort entries and filters name the column by it. */
12
+ readonly id: string;
13
+ /** The column's name, shown in its header cell. */
14
+ readonly label: string;
15
+ /** The directions a sort press steps through, in this order, at least one; absent: the column is not sortable. */
16
+ readonly sortable?: readonly [SortDirection, ...SortDirection[]];
17
+ /** The filter the column offers; absent or null: none. */
18
+ readonly filter?: DataTableFilter | null;
19
+ } & ({
20
+ /** The cell's text for the row. */
21
+ readonly value: (context: {
22
+ readonly row: Row;
23
+ }) => string;
24
+ /** Renders the cell in place of `value`'s text. */
25
+ readonly cell?: Snippet<[
26
+ {
27
+ /** The row of the cell. */
28
+ readonly row: Row;
29
+ /** This column. */
30
+ readonly column: DataTableColumn<Row>;
31
+ }
32
+ ]>;
33
+ } | {
34
+ /** The cell's text for the row. */
35
+ readonly value?: (context: {
36
+ readonly row: Row;
37
+ }) => string;
38
+ /** Renders the cell in place of `value`'s text. */
39
+ readonly cell: Snippet<[
40
+ {
41
+ /** The row of the cell. */
42
+ readonly row: Row;
43
+ /** This column. */
44
+ readonly column: DataTableColumn<Row>;
45
+ }
46
+ ]>;
47
+ });
48
+ /** A sort direction: ascending or descending. */
49
+ export type SortDirection = "asc" | "desc";
50
+ /** One option of a categorical filter; neither its value nor its label is empty. */
51
+ export interface DataTableFilterOption {
52
+ /** What the filter state holds while the option is selected. */
53
+ readonly value: string;
54
+ /** What the option shows. */
55
+ readonly label: string;
56
+ }
57
+ /** A column's filter: a text filter, or a categorical one that may offer one more option for "no value". */
58
+ export type DataTableFilter = {
59
+ /** A text filter: one field. */
60
+ readonly kind: "text";
61
+ } | {
62
+ /** A categorical filter: a list of options to select. */
63
+ readonly kind: "categorical";
64
+ /** The options offered, in this order. */
65
+ readonly options: readonly DataTableFilterOption[];
66
+ /** Offers one more option, for rows with no value, whose value is `''`; default false. */
67
+ readonly emptyOption?: boolean;
68
+ };
69
+ /** The applied filter of one column. */
70
+ export type DataTableFilterState = {
71
+ /** A text filter's state. */
72
+ readonly kind: "text";
73
+ /** The text applied. */
74
+ readonly text: string;
75
+ } | {
76
+ /** A categorical filter's state. */
77
+ readonly kind: "categorical";
78
+ /** The values of the selected options; `''` for the option for "no value". */
79
+ readonly selected: readonly string[];
80
+ };
81
+ /** One entry of the sort order; the order lists the lead column first. */
82
+ export interface DataTableSortEntry {
83
+ /** The id of the sorted column. */
84
+ readonly id: string;
85
+ /** The column's direction. */
86
+ readonly direction: SortDirection;
87
+ }
88
+ /**
89
+ * Every fixed word of the table: the state messages, the live count, the sort words, the filter and search words and
90
+ * the alert texts. The `messages` prop replaces any of the English defaults.
91
+ */
92
+ export interface DataTableMessages {
93
+ /** The status message while the rows load. */
94
+ readonly loading: string;
95
+ /** The alert when the rows could not be loaded (status `'failed'`). */
96
+ readonly failed: string;
97
+ /** The body's message when the table is ready with no rows. */
98
+ readonly empty: string;
99
+ /** The message when the user stopped the load (status `'stopped'`). */
100
+ readonly stopped: string;
101
+ /**
102
+ * Added to the status message once a load or a busy refresh has shown its progress indicator for 5 seconds (the
103
+ * indicator shows after 1 second), while it still runs.
104
+ */
105
+ readonly stillLoading: string;
106
+ /** The label of the button that stops a running load (`oncancel`). */
107
+ readonly cancel: string;
108
+ /** The label of the button that loads again (`onretry`). */
109
+ readonly retry: string;
110
+ /** The live count that announces the rows shown, called with their number. */
111
+ readonly rowCount: (count: number) => string;
112
+ /** The one text of the alert for invalid input (props the table cannot show). */
113
+ readonly invalidInput: string;
114
+ /** Added to the caption while any column is sortable: says that the column names that are buttons sort. */
115
+ readonly sortableHint: string;
116
+ /** The label of a categorical filter's option for "no value". */
117
+ readonly emptyOption: string;
118
+ /** The name of a column's filter button, called with the column's label. */
119
+ readonly filterButton: (label: string) => string;
120
+ /** The name of a column's filter dialog, called with the column's label. */
121
+ readonly filterDialog: (label: string) => string;
122
+ /** The visible label and the name of a text filter's field, called with the column's label. */
123
+ readonly filterText: (label: string) => string;
124
+ /** The name of a categorical filter's find field, called with the column's label. */
125
+ readonly findOptions: (label: string) => string;
126
+ /** The status message in place of a categorical filter's options while its find field matches none. */
127
+ readonly noOptions: string;
128
+ /** The visible words of a text filter's badge, called with the column's label and the applied text. */
129
+ readonly badgeText: (filter: Readonly<{
130
+ label: string;
131
+ text: string;
132
+ }>) => string;
133
+ /**
134
+ * The visible words of a categorical filter's badge, called with the column's label and the selected options'
135
+ * labels.
136
+ */
137
+ readonly badgeOptions: (filter: Readonly<{
138
+ label: string;
139
+ options: readonly string[];
140
+ }>) => string;
141
+ /** The name of a badge's button, called with the badge's words. */
142
+ readonly clearFilter: (words: string) => string;
143
+ /** The label of the button that clears every applied filter. */
144
+ readonly clearFilters: string;
145
+ /** The label of the button that asks the application to restore the order and the filters (`onreset`). */
146
+ readonly reset: string;
147
+ /** The visible label and the name of the global search field. */
148
+ readonly searchLabel: string;
149
+ /** The hint under the search field and a text filter's field while `searchOnEnter` is set. */
150
+ readonly pressEnter: string;
151
+ /** The refusal of a search or filter text that holds a control character. */
152
+ readonly textHiddenCharacter: string;
153
+ /** The refusal of a search or filter text longer than `maxLength`, called with `maxLength`. */
154
+ readonly textTooLong: (maxLength: number) => string;
155
+ /** The rank shown beside a sorted column's name while two or more columns are sorted, called with the rank. */
156
+ readonly sortRank: (rank: number) => string;
157
+ /**
158
+ * The description of a sort button, called with the column's direction (null: not sorted) and its rank (null
159
+ * unless two or more columns are sorted).
160
+ */
161
+ readonly sortDescription: (state: Readonly<{
162
+ direction: SortDirection | null;
163
+ rank: number | null;
164
+ }>) => string;
165
+ }
166
+ /**
167
+ * Where the rows stand: on their way (`loading`), shown (`ready`), not arrived (`failed`) or the load ended by the
168
+ * user before an answer (`stopped`).
169
+ */
170
+ export type DataTableStatus = "loading" | "ready" | "failed" | "stopped";
171
+ /**
172
+ * The props of `DataTable`. The table shows the rows, the order and the filters it is given and never sorts or
173
+ * filters the rows itself; the application (or the connection helper) answers each callback.
174
+ */
175
+ export interface DataTableProps<Row> {
176
+ /** The rows to show, in this order. */
177
+ rows: readonly Row[];
178
+ /** The columns, in this order. */
179
+ columns: readonly DataTableColumn<Row>[];
180
+ /** The row's id: a string or a finite number, unique among the rows; 1 and "1" are the same id. */
181
+ getRowId: (context: {
182
+ readonly row: Row;
183
+ }) => string | number;
184
+ /** The table's caption, which names it. */
185
+ caption: string;
186
+ /** The name of the scrolling region that holds the table. */
187
+ regionLabel: string;
188
+ /** Where the rows stand; default `'ready'`. */
189
+ status?: DataTableStatus;
190
+ /** A newer answer is on its way while the rows shown stay; default false. */
191
+ busy?: boolean;
192
+ /**
193
+ * Replaces any of the English defaults; an entry that is undefined, null, not of its default's type or a blank text
194
+ * keeps the default.
195
+ */
196
+ messages?: Partial<DataTableMessages>;
197
+ /** The order the server applied, the lead first; null or absent: no order. */
198
+ sort?: readonly DataTableSortEntry[] | null;
199
+ /** Called with the whole next order on a sort press; required while any column is sortable. */
200
+ onsort?: (next: DataTableSortEntry[]) => void;
201
+ /**
202
+ * Called with no argument on a press of "Cancel", which shows beside the status message while the still-loading
203
+ * text shows, a busy refresh included; the table stops nothing itself and ignores what this returns.
204
+ */
205
+ oncancel?: () => void;
206
+ /**
207
+ * Called with no argument on a press of "Try again", which shows while the status is stopped or failed and the
208
+ * input is valid; the table ignores what this returns.
209
+ */
210
+ onretry?: () => void;
211
+ /** The filters the server applied, by column id; null or absent: none. */
212
+ filters?: Readonly<Record<string, DataTableFilterState>> | null;
213
+ /**
214
+ * Called with `{ id, state }`: the column id and its next filter state; it, `onclearfilter` and `onclearfilters`
215
+ * are required while any column has a filter.
216
+ */
217
+ onfilter?: (change: Readonly<{
218
+ id: string;
219
+ state: DataTableFilterState;
220
+ }>) => void;
221
+ /** Called with `{ id }`, the column id, when a filter's badge is cleared. */
222
+ onclearfilter?: (change: Readonly<{
223
+ id: string;
224
+ }>) => void;
225
+ /** Called with no argument on a press of "Clear column filters". */
226
+ onclearfilters?: () => void;
227
+ /**
228
+ * Called with no argument on a press of "Reset table"; the button shows while this is given and a column is
229
+ * sortable or offers a filter (applied or not), and no invalid-input alert shows but one about the sort, filter or
230
+ * search props.
231
+ */
232
+ onreset?: () => void;
233
+ /**
234
+ * The application's own criteria controls: between the filter badges and "Clear column filters"; shown also while
235
+ * the invalid-input alert shows.
236
+ */
237
+ criteria?: Snippet;
238
+ /** Called with the search text; the search field shows while this is given and the input is valid. */
239
+ onsearch?: (text: string) => void;
240
+ /** The applied global search; null or absent: none. */
241
+ search?: string | null;
242
+ /**
243
+ * Milliseconds of typing pause before a filter field or the search field emits; a whole number, 0 or more; default
244
+ * 400.
245
+ */
246
+ searchDelay?: number;
247
+ /** A text filter's field and the search field emit only on Enter, not after a pause; default false. */
248
+ searchOnEnter?: boolean;
249
+ /**
250
+ * The longest text a filter field or the search field accepts, in code points; a whole number, 1 or more; default
251
+ * 255.
252
+ */
253
+ maxLength?: number;
254
+ /** Rendered first, above the search field and the toolbar: the application's heading for the table. */
255
+ heading?: Snippet;
256
+ /** Rendered after `heading`: the application's own actions. */
257
+ actions?: Snippet;
258
+ /** The application's own panels: after the toolbar and before the status line, in every state. */
259
+ panels?: Snippet;
260
+ }
261
+ /**
262
+ * Every fixed word of the pager: the step names, the page and size words, the information line and the alert text.
263
+ * The keys are the pager's own: `invalidInput` is also a key of {@link DataTableMessages}, so one object passed to both
264
+ * components shows the same alert text in both; pass each its own text to tell them apart.
265
+ */
266
+ export interface PaginationMessages {
267
+ /** The name of the step to the first page. */
268
+ readonly firstPage: string;
269
+ /** The name of the step to the previous page. */
270
+ readonly previousPage: string;
271
+ /** The name of the step to the next page. */
272
+ readonly nextPage: string;
273
+ /** The name of the step to the last page. */
274
+ readonly lastPage: string;
275
+ /** The name of a page's button, called with the page number. */
276
+ readonly pageLabel: (page: number) => string;
277
+ /** The visible text of a page's button, called with the page number. */
278
+ readonly pageNumber: (page: number) => string;
279
+ /** The label of the page-size select. */
280
+ readonly sizeLabel: string;
281
+ /** The text of a page-size option, called with the size. */
282
+ readonly sizeOption: (size: number) => string;
283
+ /**
284
+ * The information line, called with the first and the last row shown (both 0 when the page holds no row) and the
285
+ * total.
286
+ */
287
+ readonly info: (range: Readonly<{
288
+ first: number;
289
+ last: number;
290
+ total: number;
291
+ }>) => string;
292
+ /** Added to the information line while the total differs from `unfilteredTotal`, called with `unfilteredTotal`. */
293
+ readonly infoFiltered: (unfilteredTotal: number) => string;
294
+ /** The one text of the alert for invalid input (props the pager cannot show). */
295
+ readonly invalidInput: string;
296
+ }
297
+ /** The props of `Pagination`. */
298
+ export interface PaginationProps {
299
+ /** Where the shown page starts, a row index. */
300
+ offset: number;
301
+ /** The page size. */
302
+ limit: number;
303
+ /** The row count the pages cover. */
304
+ total: number;
305
+ /** Names the pager's nav landmark. */
306
+ label: string;
307
+ /** The page sizes offered; absent or empty: no page-size select. */
308
+ sizes?: readonly number[];
309
+ /** Called with the offset of the page pressed. */
310
+ onpage: (offset: number) => void;
311
+ /** Called with the size chosen; required while `sizes` is not empty. */
312
+ onsize?: (size: number) => void;
313
+ /**
314
+ * The row count before filtering, as the server reports it; the information line names it while it differs from
315
+ * `total`; null (from a JSON answer) is taken as not given.
316
+ */
317
+ unfilteredTotal?: number;
318
+ /**
319
+ * Replaces any of the English defaults; an entry that is undefined, null, not of its default's type or a blank text
320
+ * keeps the default.
321
+ */
322
+ messages?: Partial<PaginationMessages>;
323
+ }
package/dist/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,5 @@
1
+ import type { TableFault } from "./faults.ts";
2
+ import type { DataTableProps } from "./types.ts";
3
+ export declare function validateTable<Row>(input: Pick<DataTableProps<Row>, "rows" | "columns" | "getRowId" | "caption" | "regionLabel" | "status" | "sort" | "onsort" | "filters" | "onfilter" | "onclearfilter" | "onclearfilters" | "search" | "searchDelay" | "maxLength"> & {
4
+ emptyOption: string;
5
+ }): TableFault | null;
@@ -0,0 +1,201 @@
1
+ import { firstFault, hasDuplicate, isEmpty } from "./checks.js";
2
+ // undefined never reaches here: DataTable.svelte defaults status to 'ready' first; null does, and is refused (RND-05)
3
+ const STATUSES = ["loading", "ready", "failed", "stopped"];
4
+ // An entry of the order as its type says: an object with a string id and a direction, asc or desc (SORT-15)
5
+ const isSortEntry = (entry) => typeof entry === "object" &&
6
+ entry !== null &&
7
+ typeof entry.id === "string" &&
8
+ (entry.direction === "asc" ||
9
+ entry.direction === "desc");
10
+ // No type guard on purpose: Array.isArray would widen a readonly array to any[]
11
+ const isList = (value) => Array.isArray(value);
12
+ // The code of the first fault, in the order of RND-05's table, SORT-15's and then FILT-32's, or null when the input is valid
13
+ export function validateTable(input) {
14
+ const { rows, columns, getRowId, caption, regionLabel, status } = input;
15
+ // The sort faults come after every fault of RND-05's table (SORT-15), the filter faults after those (FILT-32)
16
+ return (firstFault([
17
+ ["captionEmpty", () => isEmpty(caption)],
18
+ ["regionLabelEmpty", () => isEmpty(regionLabel)],
19
+ ["noColumns", () => !isList(columns) || columns.length === 0],
20
+ // A column that is not an object has no id (RND-05)
21
+ [
22
+ "columnIdEmpty",
23
+ () => columns.some((column) => isEmpty(column?.id)),
24
+ ],
25
+ [
26
+ "columnIdDuplicate",
27
+ () => hasDuplicate(columns.map((column) => column.id)),
28
+ ],
29
+ [
30
+ "columnLabelEmpty",
31
+ () => columns.some((column) => isEmpty(column.label)),
32
+ ],
33
+ // Every label is a string here: isEmpty above refused the rest
34
+ [
35
+ "columnLabelDuplicate",
36
+ () => hasDuplicate(columns.map((column) => column.label.trim())),
37
+ ],
38
+ // A cell snippet is a function too
39
+ [
40
+ "columnContentMissing",
41
+ () => columns.some((column) => typeof column.value !== "function" &&
42
+ typeof column.cell !== "function"),
43
+ ],
44
+ ["statusUnknown", () => !STATUSES.includes(status)],
45
+ // Only with status ready are rows that are not a list a fault; with loading, failed or stopped they are not
46
+ // (STATE-08, STATE-22), and a sort fault still counts
47
+ ["rowsNotList", () => !isList(rows) && status === "ready"],
48
+ ]) ??
49
+ rowFault(rows, getRowId) ??
50
+ sortFault(input.columns, input.sort, input.onsort) ??
51
+ filterFault(input) ??
52
+ searchFault(input));
53
+ }
54
+ // A row's id in its text form, so the number 1 and the string "1" are one id; null when it cannot be read
55
+ function readId(getRowId, row) {
56
+ try {
57
+ const id = getRowId({ row });
58
+ return typeof id === "string" || Number.isFinite(id) ? String(id) : null;
59
+ }
60
+ catch {
61
+ return null;
62
+ }
63
+ }
64
+ // An unreadable row id is reported last: an empty or shared id in another row comes first in RND-05's table. Rows
65
+ // that are not a list have no ids to check.
66
+ function rowFault(rows, getRowId) {
67
+ if (!isList(rows))
68
+ return null;
69
+ const ids = Array.from(rows, (row) => readId(getRowId, row));
70
+ const read = ids.filter((id) => id !== null);
71
+ return firstFault([
72
+ ["rowIdEmpty", () => read.some(isEmpty)],
73
+ ["rowIdDuplicate", () => hasDuplicate(read)],
74
+ ["rowIdUnreadable", () => read.length < ids.length],
75
+ ]);
76
+ }
77
+ // The first row of SORT-15's table that applies; each row is checked across all columns or entries before the next
78
+ function sortFault(columns, sort, onsort) {
79
+ const invalid = (sortable) => !isList(sortable) ||
80
+ sortable.length === 0 ||
81
+ sortable.some((d) => d !== "asc" && d !== "desc") ||
82
+ // A direction named twice would make a press repeat it, and the column would never leave the order
83
+ new Set(sortable).size !== sortable.length;
84
+ // A sortable given as null is not sortable, as left out (SORT-15)
85
+ const sortables = columns.filter((column) => (column.sortable ?? undefined) !== undefined);
86
+ const order = sort ?? [];
87
+ const columnOf = (id) => columns.find((column) => column.id === id);
88
+ return firstFault([
89
+ [
90
+ "sortableInvalid",
91
+ () => sortables.some((column) => invalid(column.sortable)),
92
+ ],
93
+ [
94
+ "sortHandlerMissing",
95
+ () => sortables.length > 0 && typeof onsort !== "function",
96
+ ],
97
+ [
98
+ "sortInvalid",
99
+ () => sort != null &&
100
+ (!isList(sort) || !sort.every(isSortEntry)),
101
+ ],
102
+ ["sortColumnUnknown", () => order.some((entry) => !columnOf(entry.id))],
103
+ // Stryker disable OptionalChaining: the row above leaves every entry a known column, the next row a sortable one
104
+ [
105
+ "sortColumnNotSortable",
106
+ () => order.some((entry) => !columnOf(entry.id)?.sortable),
107
+ ],
108
+ [
109
+ "sortDirectionNotAllowed",
110
+ () => order.some((entry) => !columnOf(entry.id)?.sortable?.includes(entry.direction)),
111
+ ],
112
+ // Stryker restore OptionalChaining
113
+ ["sortColumnDuplicate", () => hasDuplicate(order.map((entry) => entry.id))],
114
+ ]);
115
+ }
116
+ const isObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
117
+ // The values a categorical filter accepts as selected: its options' values, and '' with the option of FILT-53
118
+ function optionValues(filter) {
119
+ const values = filter["options"].map((option) => option.value);
120
+ return filter["emptyOption"] === true ? [...values, ""] : values;
121
+ }
122
+ // One filter as its type says (the filterInvalid rows of FILT-32); the label of FILT-53's option counts for the duplicate check
123
+ function filterShapeFault(filter, emptyLabel) {
124
+ // Not null here; a filter that is no object has no kind, and is refused with the kinds that are none of the two
125
+ const { kind, emptyOption, options } = filter;
126
+ if (kind === "text")
127
+ return emptyOption === true;
128
+ const given = options;
129
+ return [
130
+ // Neither a text nor a categorical filter
131
+ () => kind !== "categorical",
132
+ // No options, or not a list
133
+ () => !isList(options) || given.length === 0,
134
+ // An option that is no object, or has an empty value or label
135
+ () => given.some((option) => !isObject(option) ||
136
+ isEmpty(option["value"]) ||
137
+ isEmpty(option["label"])),
138
+ // An emptyOption that is neither a boolean nor left out
139
+ () => emptyOption != null && typeof emptyOption !== "boolean",
140
+ // Two options with one value
141
+ () => hasDuplicate(given.map((option) => option.value)),
142
+ // Two options with one label, the option of FILT-53 included; every label is a string here (the option row above)
143
+ () => {
144
+ const labels = given.map((option) => option.label.trim());
145
+ return hasDuplicate(emptyOption === true ? [...labels, emptyLabel.trim()] : labels);
146
+ },
147
+ ].some((holds) => holds());
148
+ }
149
+ // A categorical entry's selection as its filter allows: a list of its option values, none twice
150
+ const selectionFault = (selected, allowed) => !isList(selected) ||
151
+ selected.some((value) => !allowed.includes(value)) ||
152
+ hasDuplicate(selected);
153
+ // An applied entry as its column's filter allows (FILT-32, filterStateInvalid)
154
+ function entryFault(entry, filter) {
155
+ if (!isObject(entry) || !isObject(filter) || entry["kind"] !== filter["kind"])
156
+ return true;
157
+ return entry["kind"] === "text"
158
+ ? typeof entry["text"] !== "string"
159
+ : selectionFault(entry["selected"], optionValues(filter));
160
+ }
161
+ // The filter faults of FILT-32's table, the first row that applies; a filter given as null is no filter, as left out
162
+ function filterFault(input) {
163
+ const { columns, filters, emptyOption } = input;
164
+ const filtered = columns.filter((column) => (column.filter ?? undefined) !== undefined);
165
+ return firstFault([
166
+ [
167
+ "filterInvalid",
168
+ () => filtered.some((column) => filterShapeFault(column.filter, emptyOption)),
169
+ ],
170
+ [
171
+ "filterHandlerMissing",
172
+ () => filtered.length > 0 &&
173
+ [input.onfilter, input.onclearfilter, input.onclearfilters].some((handler) => typeof handler !== "function"),
174
+ ],
175
+ // `filters` given and not an object
176
+ ["filterStateInvalid", () => filters != null && !isObject(filters)],
177
+ // An entry that is not as its column's filter allows
178
+ [
179
+ "filterStateInvalid",
180
+ () => isObject(filters) &&
181
+ Object.entries(filters).some(([id, entry]) => entryFault(entry, columns.find((column) => column.id === id)?.filter)),
182
+ ],
183
+ ]);
184
+ }
185
+ // A whole number of at least `min`; Number.isInteger refuses everything that is no number, NaN and Infinity included
186
+ const wholeFrom = (value, min) => Number.isInteger(value) && value >= min;
187
+ // The search, its delay and its length limit; the application sets the last two, so null is a fault for them
188
+ function searchFault(input) {
189
+ const { search, searchDelay, maxLength } = input;
190
+ return firstFault([
191
+ ["searchInvalid", () => search != null && typeof search !== "string"],
192
+ [
193
+ "searchDelayInvalid",
194
+ () => searchDelay !== undefined && !wholeFrom(searchDelay, 0),
195
+ ],
196
+ [
197
+ "maxLengthInvalid",
198
+ () => maxLength !== undefined && !wholeFrom(maxLength, 1),
199
+ ],
200
+ ]);
201
+ }
@@ -0,0 +1,7 @@
1
+ export declare class TableView {
2
+ shown: boolean;
3
+ held: boolean;
4
+ still: boolean;
5
+ started: boolean;
6
+ constructor(pending: () => boolean, ending: () => void, region: () => HTMLElement | null);
7
+ }
@@ -0,0 +1,75 @@
1
+ import { untrack } from "svelte";
2
+ // The slow-load state of PRD-B2, one per table: `shown` is the progress indicator being in the component, `held` its
3
+ // 500 ms minimum still running, `still` the still-loading text being shown, `started` whether a load or refetch has
4
+ // started since mount. Read as `view.still`, never destructured, so the reads stay reactive. The class also keeps the
5
+ // region's Tab stop (A11Y-12), which only the browser reads, so it has no field
6
+ export class TableView {
7
+ shown = $state(false);
8
+ // Stryker disable next-line BooleanLiteral: equivalent, a wrongly true start only skips a reset of shown and still, which are false then
9
+ held = $state(false);
10
+ still = $state(false);
11
+ // Set by the first load or refetch with valid input and never cleared: from then on the status line keeps its room
12
+ // until unmount (STATE-15); before it, none (STATE-29). The table's markup also reads `pending`, because this field
13
+ // is set by an effect, which does not run on the server, where a loading table must already have its room
14
+ started = $state(false);
15
+ // Built only while a component initialises (its effects need that context), at the place the table's script gives
16
+ // it. `pending` is read in the first effect, so a change of it reruns that one; `ending` is the table's focus
17
+ // hand-off; `region` is the element the Tab stop belongs to, null while there is none
18
+ constructor(pending, ending, region) {
19
+ // 1 s of pending without a gap shows the indicator (STATE-09); a gap clears the delay. Once nothing is pending and
20
+ // the minimum has passed, the indicator goes (STATE-10) and the still-loading text with it (STATE-12)
21
+ $effect(() => {
22
+ if (!pending()) {
23
+ if (!this.held) {
24
+ // A block that depends on state a plain $effect writes is removed in the same flush, before any pre-effect
25
+ // runs (measured with Svelte 5.57.3, an effect-order detail that may change): so focus is kept here, in the
26
+ // effect that writes `still`, before it is written. A11Y-28's browser rows fail without this call
27
+ untrack(ending);
28
+ this.shown = false;
29
+ this.still = false;
30
+ }
31
+ return;
32
+ }
33
+ this.started = true;
34
+ if (this.shown)
35
+ return;
36
+ const delay = setTimeout(() => {
37
+ this.shown = true;
38
+ this.held = true;
39
+ }, 1000);
40
+ return () => clearTimeout(delay);
41
+ });
42
+ // While the indicator is shown: its 500 ms minimum (STATE-10) and the still-loading text 5 s after it appeared
43
+ // (STATE-12), both ended when it goes or the table unmounts (FOOT-05)
44
+ $effect(() => {
45
+ if (!this.shown)
46
+ return;
47
+ const minimum = setTimeout(() => (this.held = false), 500);
48
+ const long = setTimeout(() => (this.still = true), 5000);
49
+ return () => {
50
+ clearTimeout(minimum);
51
+ clearTimeout(long);
52
+ };
53
+ });
54
+ // The region is a Tab stop exactly while its content is wider than it (A11Y-12, PRD section 5.1): sideways only,
55
+ // as the region scrolls sideways only (overflow-y computes to auto beside overflow-x, so a height cap on the region
56
+ // is not supported: it would scroll vertically without a stop). Watched on the region (the viewport's width) and on
57
+ // the region's children, the table (its columns and rows). The attribute is written here, not in the markup, where
58
+ // Svelte flags a dynamic tabindex on a region (a11y_no_noninteractive_tabindex). Without a ResizeObserver (an
59
+ // adopter's jsdom test) there is no stop.
60
+ $effect(() => {
61
+ const box = region();
62
+ if (!box || typeof ResizeObserver === "undefined")
63
+ return;
64
+ const observer = new ResizeObserver(() => {
65
+ if (box.scrollWidth > box.clientWidth)
66
+ box.tabIndex = 0;
67
+ else
68
+ box.removeAttribute("tabindex");
69
+ });
70
+ for (const element of [box, ...box.children])
71
+ observer.observe(element);
72
+ return () => observer.disconnect();
73
+ });
74
+ }
75
+ }