tadabut 0.2.0 → 0.4.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 +354 -45
- package/dist/index-mZisu7MG.d.ts +605 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/react.d.ts +14 -33
- package/dist/react.js +60 -38
- package/dist/src-DWwW61Yo.js +1189 -0
- package/package.json +5 -4
- package/dist/index-D2g9_Fd9.d.ts +0 -603
- package/dist/src-C4vgw-1w.js +0 -982
package/dist/src-C4vgw-1w.js
DELETED
|
@@ -1,982 +0,0 @@
|
|
|
1
|
-
import DataTable from "datatables.net";
|
|
2
|
-
// src/editors/classes.ts
|
|
3
|
-
/**
|
|
4
|
-
* Turns class lists like `'btn btn-sm '` into single classes, like
|
|
5
|
-
* `['btn', 'btn-sm']`.
|
|
6
|
-
*
|
|
7
|
-
* Internal. Each list can hold several classes separated by any spaces.
|
|
8
|
-
* `undefined` lists and empty pieces are skipped, because
|
|
9
|
-
* `classList.add('')` throws.
|
|
10
|
-
*
|
|
11
|
-
* @param lists The class lists, in order. `undefined` is allowed, so
|
|
12
|
-
* optional options can be passed as they are.
|
|
13
|
-
*/
|
|
14
|
-
function splitClasses(...lists) {
|
|
15
|
-
return lists.flatMap((list) => list?.split(/\s+/).filter(Boolean) ?? []);
|
|
16
|
-
}
|
|
17
|
-
/**
|
|
18
|
-
* Adds Tadabut's own classes and the user's `className` to an element.
|
|
19
|
-
*
|
|
20
|
-
* Internal. `className` can hold several classes separated by any spaces,
|
|
21
|
-
* like `'border rounded'`.
|
|
22
|
-
*/
|
|
23
|
-
function addClasses(element, ownClasses, className) {
|
|
24
|
-
element.classList.add(...ownClasses, ...splitClasses(className));
|
|
25
|
-
}
|
|
26
|
-
// src/columns/escape.ts
|
|
27
|
-
/** The characters that mean something in HTML, and what replaces them. */
|
|
28
|
-
const entities = {
|
|
29
|
-
"&": "&",
|
|
30
|
-
"<": "<",
|
|
31
|
-
">": ">",
|
|
32
|
-
"\"": """,
|
|
33
|
-
"'": "'"
|
|
34
|
-
};
|
|
35
|
-
/**
|
|
36
|
-
* Makes text safe to put in HTML: `<b>` shows as the text `<b>`, and can
|
|
37
|
-
* never become a tag or run code.
|
|
38
|
-
*
|
|
39
|
-
* Internal: DataTables puts what a column's `render` returns into the cell
|
|
40
|
-
* as HTML, so every label goes through this first.
|
|
41
|
-
*/
|
|
42
|
-
function escapeHtml(text) {
|
|
43
|
-
return text.replace(/[&<>"']/g, (char) => entities[char] ?? char);
|
|
44
|
-
}
|
|
45
|
-
// src/columns/to-datatables.ts
|
|
46
|
-
/**
|
|
47
|
-
* The last column, with the Edit button. When a row is being edited, its
|
|
48
|
-
* cell holds Save and Cancel instead.
|
|
49
|
-
*
|
|
50
|
-
* DataTables wants the button as an HTML string. The user's classes go
|
|
51
|
-
* inside that string, so they are escaped: a class like `x" onclick="…`
|
|
52
|
-
* stays a (strange) class, and can't close the attribute and add code.
|
|
53
|
-
*/
|
|
54
|
-
function actionsColumn(classNames) {
|
|
55
|
-
return {
|
|
56
|
-
title: "",
|
|
57
|
-
data: null,
|
|
58
|
-
className: "tdb-actions",
|
|
59
|
-
orderable: false,
|
|
60
|
-
searchable: false,
|
|
61
|
-
defaultContent: `<button type="button" class="${escapeHtml([
|
|
62
|
-
"tdb-button",
|
|
63
|
-
"tdb-button-edit",
|
|
64
|
-
...splitClasses(classNames.button, classNames.edit)
|
|
65
|
-
].join(" "))}" data-tdb-action="edit">Edit</button>`
|
|
66
|
-
};
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
|
-
* Turns Tadabut columns into DataTables column settings.
|
|
70
|
-
*
|
|
71
|
-
* Internal: not exported from the package. It is the only place that knows
|
|
72
|
-
* DataTables' column names, so a DataTables change only touches this file.
|
|
73
|
-
*
|
|
74
|
-
* @param columns The user's columns.
|
|
75
|
-
* @param withActions Adds the actions column at the end. It must stay last:
|
|
76
|
-
* the user's columns keep their positions, and Tadabut finds cells by
|
|
77
|
-
* position.
|
|
78
|
-
* @param classNames The user's classes. Only the Edit button uses them here.
|
|
79
|
-
*/
|
|
80
|
-
function toDataTables(columns, withActions, classNames) {
|
|
81
|
-
const converted = columns.map((column) => {
|
|
82
|
-
const label = labelOf(column);
|
|
83
|
-
return {
|
|
84
|
-
title: column.title,
|
|
85
|
-
data: column.prop,
|
|
86
|
-
render: (value, type, row) => {
|
|
87
|
-
switch (type) {
|
|
88
|
-
case "display": return escapeHtml(label(value, row));
|
|
89
|
-
case "filter": return label(value, row);
|
|
90
|
-
default: return value;
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
};
|
|
94
|
-
});
|
|
95
|
-
return withActions ? [...converted, actionsColumn(classNames)] : converted;
|
|
96
|
-
}
|
|
97
|
-
/**
|
|
98
|
-
* Picks how a column's value becomes text: its own `render`, else the label
|
|
99
|
-
* of a fixed select list, else the value itself. Empty values give `''`.
|
|
100
|
-
*/
|
|
101
|
-
function labelOf(column) {
|
|
102
|
-
const { render, editor } = column;
|
|
103
|
-
if (render !== void 0) return render;
|
|
104
|
-
if (editor?.type === "select" && typeof editor.options !== "function") {
|
|
105
|
-
const options = editor.options;
|
|
106
|
-
return (value) => options.find((option) => option.value === value)?.label ?? asText(value);
|
|
107
|
-
}
|
|
108
|
-
return asText;
|
|
109
|
-
}
|
|
110
|
-
/** The value as text. `null` and `undefined` give an empty cell. */
|
|
111
|
-
function asText(value) {
|
|
112
|
-
return value === null || value === void 0 ? "" : String(value);
|
|
113
|
-
}
|
|
114
|
-
// src/editors/checkbox.ts
|
|
115
|
-
/**
|
|
116
|
-
* Creates a live checkbox editor.
|
|
117
|
-
*
|
|
118
|
-
* Internal: Tadabut calls it when a row starts being edited.
|
|
119
|
-
*
|
|
120
|
-
* @param _config The editor config from the column. A checkbox has no
|
|
121
|
-
* options of its own yet; the parameter keeps the same shape as the other
|
|
122
|
-
* editors.
|
|
123
|
-
* @param context The starting value, the classes and the change callback.
|
|
124
|
-
*/
|
|
125
|
-
function createCheckboxEditor(_config, context) {
|
|
126
|
-
const input = document.createElement("input");
|
|
127
|
-
input.type = "checkbox";
|
|
128
|
-
addClasses(input, ["tdb-input", "tdb-input-checkbox"], context.className);
|
|
129
|
-
const setValue = (value) => {
|
|
130
|
-
input.checked = value === true;
|
|
131
|
-
};
|
|
132
|
-
setValue(context.value);
|
|
133
|
-
input.addEventListener("change", () => context.onChange(input.checked));
|
|
134
|
-
return {
|
|
135
|
-
element: input,
|
|
136
|
-
getValue: () => input.checked,
|
|
137
|
-
setValue,
|
|
138
|
-
focus: () => input.focus(),
|
|
139
|
-
destroy: () => input.remove()
|
|
140
|
-
};
|
|
141
|
-
}
|
|
142
|
-
// src/editors/date.ts
|
|
143
|
-
/**
|
|
144
|
-
* Creates a live date editor.
|
|
145
|
-
*
|
|
146
|
-
* Internal: Tadabut calls it when a row starts being edited.
|
|
147
|
-
*
|
|
148
|
-
* @param config The editor config from the column.
|
|
149
|
-
* @param context The starting value, the classes and the change callback.
|
|
150
|
-
*/
|
|
151
|
-
function createDateEditor(config, context) {
|
|
152
|
-
const input = document.createElement("input");
|
|
153
|
-
input.type = "date";
|
|
154
|
-
addClasses(input, ["tdb-input", "tdb-input-date"], context.className);
|
|
155
|
-
if (config.min !== void 0) input.min = config.min;
|
|
156
|
-
if (config.max !== void 0) input.max = config.max;
|
|
157
|
-
const getValue = () => input.value === "" ? null : input.value;
|
|
158
|
-
const setValue = (value) => {
|
|
159
|
-
input.value = value ?? "";
|
|
160
|
-
};
|
|
161
|
-
setValue(context.value ?? null);
|
|
162
|
-
input.addEventListener("input", () => context.onChange(getValue()));
|
|
163
|
-
return {
|
|
164
|
-
element: input,
|
|
165
|
-
getValue,
|
|
166
|
-
setValue,
|
|
167
|
-
focus: () => input.focus(),
|
|
168
|
-
destroy: () => input.remove()
|
|
169
|
-
};
|
|
170
|
-
}
|
|
171
|
-
// src/editors/number.ts
|
|
172
|
-
/**
|
|
173
|
-
* Creates a live number editor.
|
|
174
|
-
*
|
|
175
|
-
* Internal: Tadabut calls it when a row starts being edited.
|
|
176
|
-
*
|
|
177
|
-
* @param config The editor config from the column.
|
|
178
|
-
* @param context The starting value, the classes and the change callback.
|
|
179
|
-
*/
|
|
180
|
-
function createNumberEditor(config, context) {
|
|
181
|
-
const input = document.createElement("input");
|
|
182
|
-
input.type = "number";
|
|
183
|
-
addClasses(input, ["tdb-input", "tdb-input-number"], context.className);
|
|
184
|
-
if (config.min !== void 0) input.min = String(config.min);
|
|
185
|
-
if (config.max !== void 0) input.max = String(config.max);
|
|
186
|
-
if (config.step !== void 0) input.step = String(config.step);
|
|
187
|
-
const getValue = () => input.value === "" ? null : input.valueAsNumber;
|
|
188
|
-
const setValue = (value) => {
|
|
189
|
-
input.value = value === null ? "" : String(value);
|
|
190
|
-
};
|
|
191
|
-
setValue(context.value ?? null);
|
|
192
|
-
input.addEventListener("input", () => context.onChange(getValue()));
|
|
193
|
-
return {
|
|
194
|
-
element: input,
|
|
195
|
-
getValue,
|
|
196
|
-
setValue,
|
|
197
|
-
focus: () => input.focus(),
|
|
198
|
-
destroy: () => input.remove()
|
|
199
|
-
};
|
|
200
|
-
}
|
|
201
|
-
// src/editors/select.ts
|
|
202
|
-
/**
|
|
203
|
-
* Creates a live select editor.
|
|
204
|
-
*
|
|
205
|
-
* Internal: Tadabut calls it when a row starts being edited.
|
|
206
|
-
*
|
|
207
|
-
* HTML `<option>` values are always strings, so the picked value is read
|
|
208
|
-
* from the options list by position, which keeps its real type (`1`, not
|
|
209
|
-
* `'1'`).
|
|
210
|
-
*
|
|
211
|
-
* With an `options` function, the select starts empty and Tadabut calls
|
|
212
|
-
* `load`. Until the options arrive, the starting value is remembered:
|
|
213
|
-
* `getValue` gives it, and it's picked once the options are shown.
|
|
214
|
-
*
|
|
215
|
-
* @param config The editor config from the column.
|
|
216
|
-
* @param context The starting value, the classes and the change callback.
|
|
217
|
-
*/
|
|
218
|
-
function createSelectEditor(config, context) {
|
|
219
|
-
const select = document.createElement("select");
|
|
220
|
-
addClasses(select, ["tdb-input", "tdb-input-select"], context.className);
|
|
221
|
-
/** The options shown now. Empty until a function's options arrive. */
|
|
222
|
-
let options = [];
|
|
223
|
-
/**
|
|
224
|
-
* The value, kept here and not only in the `<select>`: while options are
|
|
225
|
-
* loading, the `<select>` has nothing to pick, but the value must not be
|
|
226
|
-
* lost.
|
|
227
|
-
*/
|
|
228
|
-
let current = context.value;
|
|
229
|
-
/** The load that's running, so a newer one (or destroy) can stop it. */
|
|
230
|
-
let controller;
|
|
231
|
-
/** Picks `current` in the `<select>`. If it's not in the list, it's `null`. */
|
|
232
|
-
const pickCurrent = () => {
|
|
233
|
-
select.selectedIndex = options.findIndex((option) => option.value === current);
|
|
234
|
-
if (select.selectedIndex === -1) current = null;
|
|
235
|
-
};
|
|
236
|
-
/** Replaces the `<option>`s with `list`, enables the select and picks `current`. */
|
|
237
|
-
const fill = (list) => {
|
|
238
|
-
options = list;
|
|
239
|
-
select.replaceChildren(...list.map((option) => {
|
|
240
|
-
const element = document.createElement("option");
|
|
241
|
-
element.textContent = option.label;
|
|
242
|
-
return element;
|
|
243
|
-
}));
|
|
244
|
-
select.disabled = false;
|
|
245
|
-
pickCurrent();
|
|
246
|
-
};
|
|
247
|
-
/** Shows one disabled, unpickable line, like "Loading…". */
|
|
248
|
-
const showOnly = (text) => {
|
|
249
|
-
options = [];
|
|
250
|
-
const element = document.createElement("option");
|
|
251
|
-
element.textContent = text;
|
|
252
|
-
select.replaceChildren(element);
|
|
253
|
-
select.disabled = true;
|
|
254
|
-
};
|
|
255
|
-
const load = async (row) => {
|
|
256
|
-
if (typeof config.options !== "function") return "loaded";
|
|
257
|
-
controller?.abort();
|
|
258
|
-
const own = new AbortController();
|
|
259
|
-
controller = own;
|
|
260
|
-
try {
|
|
261
|
-
const result = config.options(row, { signal: own.signal });
|
|
262
|
-
if (!(result instanceof Promise)) {
|
|
263
|
-
fill(result);
|
|
264
|
-
return "loaded";
|
|
265
|
-
}
|
|
266
|
-
showOnly("Loading…");
|
|
267
|
-
const list = await result;
|
|
268
|
-
if (own.signal.aborted) return "cancelled";
|
|
269
|
-
fill(list);
|
|
270
|
-
return "loaded";
|
|
271
|
-
} catch {
|
|
272
|
-
if (own.signal.aborted) return "cancelled";
|
|
273
|
-
showOnly("");
|
|
274
|
-
return "failed";
|
|
275
|
-
}
|
|
276
|
-
};
|
|
277
|
-
const getValue = () => current;
|
|
278
|
-
const setValue = (value) => {
|
|
279
|
-
current = value;
|
|
280
|
-
if (!select.disabled) pickCurrent();
|
|
281
|
-
};
|
|
282
|
-
if (typeof config.options !== "function") fill(config.options);
|
|
283
|
-
select.addEventListener("change", () => {
|
|
284
|
-
current = options[select.selectedIndex]?.value ?? null;
|
|
285
|
-
context.onChange(current);
|
|
286
|
-
});
|
|
287
|
-
return {
|
|
288
|
-
element: select,
|
|
289
|
-
getValue,
|
|
290
|
-
setValue,
|
|
291
|
-
load,
|
|
292
|
-
focus: () => select.focus(),
|
|
293
|
-
destroy: () => {
|
|
294
|
-
controller?.abort();
|
|
295
|
-
select.remove();
|
|
296
|
-
}
|
|
297
|
-
};
|
|
298
|
-
}
|
|
299
|
-
// src/editors/text.ts
|
|
300
|
-
/**
|
|
301
|
-
* Creates a live text editor.
|
|
302
|
-
*
|
|
303
|
-
* Internal: Tadabut calls it when a row starts being edited.
|
|
304
|
-
*
|
|
305
|
-
* @param options The editor config from the column.
|
|
306
|
-
* @param context The starting value, the classes and the change callback.
|
|
307
|
-
*/
|
|
308
|
-
function createTextEditor(config, context) {
|
|
309
|
-
const input = document.createElement("input");
|
|
310
|
-
input.type = "text";
|
|
311
|
-
addClasses(input, ["tdb-input", "tdb-input-text"], context.className);
|
|
312
|
-
if (config.minLength !== void 0) input.minLength = config.minLength;
|
|
313
|
-
if (config.maxLength !== void 0) input.maxLength = config.maxLength;
|
|
314
|
-
if (config.pattern !== void 0) input.pattern = config.pattern;
|
|
315
|
-
input.value = context.value ?? "";
|
|
316
|
-
input.addEventListener("input", () => context.onChange(input.value));
|
|
317
|
-
return {
|
|
318
|
-
element: input,
|
|
319
|
-
getValue: () => input.value,
|
|
320
|
-
setValue: (value) => {
|
|
321
|
-
input.value = value ?? "";
|
|
322
|
-
},
|
|
323
|
-
focus: () => input.focus(),
|
|
324
|
-
destroy: () => input.remove()
|
|
325
|
-
};
|
|
326
|
-
}
|
|
327
|
-
// src/editors/create.ts
|
|
328
|
-
/**
|
|
329
|
-
* Creates the live editor that matches a column's editor config.
|
|
330
|
-
*
|
|
331
|
-
* Internal: Tadabut calls it for each editable cell when a row starts being
|
|
332
|
-
* edited.
|
|
333
|
-
*
|
|
334
|
-
* The exact column type is not known here, so values are `unknown`.
|
|
335
|
-
* That is safe: `Column<Row>` already checked that the config fits the
|
|
336
|
-
* column, before the table was created.
|
|
337
|
-
*
|
|
338
|
-
* @param config The editor config from the column.
|
|
339
|
-
* @param context The starting value and the change callback. Its
|
|
340
|
-
* `className` is filled in from `config`, so every editor, built-in or
|
|
341
|
-
* custom, gets the same full context.
|
|
342
|
-
*/
|
|
343
|
-
function createEditor(config, context) {
|
|
344
|
-
const fullContext = {
|
|
345
|
-
...context,
|
|
346
|
-
className: config.className
|
|
347
|
-
};
|
|
348
|
-
switch (config.type) {
|
|
349
|
-
case "text": return loosen(createTextEditor(config, tighten(fullContext)));
|
|
350
|
-
case "number": return loosen(createNumberEditor(config, tighten(fullContext)));
|
|
351
|
-
case "date": return loosen(createDateEditor(config, tighten(fullContext)));
|
|
352
|
-
case "checkbox": return loosen(createCheckboxEditor(config, tighten(fullContext)));
|
|
353
|
-
case "select": return createSelectEditor(config, fullContext);
|
|
354
|
-
case "custom": return config.create(fullContext);
|
|
355
|
-
default: throw new Error(`Unknown editor: ${JSON.stringify(config)}`);
|
|
356
|
-
}
|
|
357
|
-
}
|
|
358
|
-
/** Gives a built-in editor the context type it expects. */
|
|
359
|
-
function tighten(context) {
|
|
360
|
-
return context;
|
|
361
|
-
}
|
|
362
|
-
/** Turns a typed editor back into the loose type `createEditor` returns. */
|
|
363
|
-
function loosen(editor) {
|
|
364
|
-
return editor;
|
|
365
|
-
}
|
|
366
|
-
// src/validation/validate.ts
|
|
367
|
-
/**
|
|
368
|
-
* Error messages. Plain English for now. They will become an option when
|
|
369
|
-
* translations are added.
|
|
370
|
-
*
|
|
371
|
-
* Internal: exported for the edit session, which shows `loadFailed`.
|
|
372
|
-
*/
|
|
373
|
-
const messages = {
|
|
374
|
-
required: "This field is required.",
|
|
375
|
-
loadFailed: "Could not load the options.",
|
|
376
|
-
tooShort: (min) => `Must be at least ${min} characters.`,
|
|
377
|
-
tooLong: (max) => `Must be at most ${max} characters.`,
|
|
378
|
-
patternMismatch: "Wrong format.",
|
|
379
|
-
tooSmall: (min) => `Must be ${min} or more.`,
|
|
380
|
-
tooLarge: (max) => `Must be ${max} or less.`,
|
|
381
|
-
tooEarly: (min) => `Must be on or after ${min}.`,
|
|
382
|
-
tooLate: (max) => `Must be on or before ${max}.`
|
|
383
|
-
};
|
|
384
|
-
/**
|
|
385
|
-
* Checks a value against a column's rules.
|
|
386
|
-
*
|
|
387
|
-
* Internal: Tadabut calls it before saving a row.
|
|
388
|
-
*
|
|
389
|
-
* The checks run in this order, and the first error wins:
|
|
390
|
-
* 1. Empty value (`null`, `undefined` or `''`): an error if `required`,
|
|
391
|
-
* otherwise no error and nothing else is checked. An empty optional field
|
|
392
|
-
* never fails a rule like `min`.
|
|
393
|
-
* 2. The editor's own rules (`minLength`, `min`, and so on).
|
|
394
|
-
* 3. The user's `validate`.
|
|
395
|
-
*
|
|
396
|
-
* @param config The editor config, with its validation rules.
|
|
397
|
-
* @param value The value the editor gave.
|
|
398
|
-
* @param row The row being edited.
|
|
399
|
-
* @returns The error message, or `undefined` if the value is fine.
|
|
400
|
-
*/
|
|
401
|
-
function validateValue(config, value, row) {
|
|
402
|
-
if (value === null || value === void 0 || value === "") return config.required ? messages.required : void 0;
|
|
403
|
-
return builtInError(config, value) ?? config.validate?.(value, row);
|
|
404
|
-
}
|
|
405
|
-
/** Checks the rules that come with each built-in editor. */
|
|
406
|
-
function builtInError(config, value) {
|
|
407
|
-
switch (config.type) {
|
|
408
|
-
case "text": return typeof value === "string" ? textError(config, value) : void 0;
|
|
409
|
-
case "number": return typeof value === "number" ? rangeError(config, value) : void 0;
|
|
410
|
-
case "date": return typeof value === "string" ? dateError(config, value) : void 0;
|
|
411
|
-
case "checkbox":
|
|
412
|
-
case "select":
|
|
413
|
-
case "custom": return;
|
|
414
|
-
default: throw new Error(`Unknown editor: ${JSON.stringify(config)}`);
|
|
415
|
-
}
|
|
416
|
-
}
|
|
417
|
-
function textError(config, value) {
|
|
418
|
-
if (config.minLength !== void 0 && value.length < config.minLength) return messages.tooShort(config.minLength);
|
|
419
|
-
if (config.maxLength !== void 0 && value.length > config.maxLength) return messages.tooLong(config.maxLength);
|
|
420
|
-
if (config.pattern !== void 0 && !matchesWhole(config.pattern, value)) return messages.patternMismatch;
|
|
421
|
-
}
|
|
422
|
-
function rangeError(config, value) {
|
|
423
|
-
if (config.min !== void 0 && value < config.min) return messages.tooSmall(config.min);
|
|
424
|
-
if (config.max !== void 0 && value > config.max) return messages.tooLarge(config.max);
|
|
425
|
-
}
|
|
426
|
-
function dateError(config, value) {
|
|
427
|
-
if (config.min !== void 0 && value < config.min) return messages.tooEarly(config.min);
|
|
428
|
-
if (config.max !== void 0 && value > config.max) return messages.tooLate(config.max);
|
|
429
|
-
}
|
|
430
|
-
/**
|
|
431
|
-
* Checks that `pattern` matches the whole value, like the HTML `pattern`
|
|
432
|
-
* attribute: same `v` flag, and an invalid pattern is ignored.
|
|
433
|
-
*
|
|
434
|
-
* The `(?:…)` keeps `^` and `$` around the whole pattern: without it,
|
|
435
|
-
* `a|b` would become `^a|b$`, which means something else.
|
|
436
|
-
*/
|
|
437
|
-
function matchesWhole(pattern, value) {
|
|
438
|
-
let regex;
|
|
439
|
-
try {
|
|
440
|
-
regex = new RegExp(`^(?:${pattern})$`, "v");
|
|
441
|
-
} catch {
|
|
442
|
-
return true;
|
|
443
|
-
}
|
|
444
|
-
return regex.test(value);
|
|
445
|
-
}
|
|
446
|
-
// src/editing/session.ts
|
|
447
|
-
/** Counter for unique error element ids, like `tdb-error-3`. */
|
|
448
|
-
let errorCount = 0;
|
|
449
|
-
/**
|
|
450
|
-
* Everything about one row while it is being edited.
|
|
451
|
-
*
|
|
452
|
-
* Internal. It knows nothing about DataTables: `Tadabut` finds the cells and
|
|
453
|
-
* passes them in, and puts the cells' text back after `destroy`.
|
|
454
|
-
*
|
|
455
|
-
* @typeParam Row - The shape of one row.
|
|
456
|
-
*/
|
|
457
|
-
var EditSession = class {
|
|
458
|
-
/** A copy of the row as it was when editing started. */
|
|
459
|
-
originalData;
|
|
460
|
-
/** The edited fields, by row property. */
|
|
461
|
-
fields = /* @__PURE__ */ new Map();
|
|
462
|
-
/** The error messages being shown, by row property. */
|
|
463
|
-
errors = /* @__PURE__ */ new Map();
|
|
464
|
-
/**
|
|
465
|
-
* For each field, the selects that depend on it: when it changes, they
|
|
466
|
-
* are cleared and their options load again.
|
|
467
|
-
*/
|
|
468
|
-
dependents = /* @__PURE__ */ new Map();
|
|
469
|
-
/** The fields whose last options load failed. */
|
|
470
|
-
loadErrors = /* @__PURE__ */ new Set();
|
|
471
|
-
/**
|
|
472
|
-
* `true` while `onSave` runs. Set by `Tadabut`, which ignores a second
|
|
473
|
-
* save, or a cancel, during that time.
|
|
474
|
-
*/
|
|
475
|
-
saving = false;
|
|
476
|
-
/**
|
|
477
|
-
* Replaces each cell's content with an editor showing the row's value,
|
|
478
|
-
* then loads the options of every select whose `options` is a function.
|
|
479
|
-
*
|
|
480
|
-
* @param row The row to edit.
|
|
481
|
-
* @param cells The editable columns and their cells. Columns without an
|
|
482
|
-
* `editor` are skipped.
|
|
483
|
-
*/
|
|
484
|
-
constructor(row, cells) {
|
|
485
|
-
this.originalData = { ...row };
|
|
486
|
-
for (const { column, cell } of cells) {
|
|
487
|
-
if (column.editor === void 0) continue;
|
|
488
|
-
const config = column.editor;
|
|
489
|
-
if (config.type === "select" && config.dependsOn !== void 0) {
|
|
490
|
-
const list = this.dependents.get(config.dependsOn) ?? [];
|
|
491
|
-
list.push(column.prop);
|
|
492
|
-
this.dependents.set(config.dependsOn, list);
|
|
493
|
-
}
|
|
494
|
-
const editor = createEditor(config, {
|
|
495
|
-
value: this.originalData[column.prop],
|
|
496
|
-
onChange: () => this.changed(column.prop)
|
|
497
|
-
});
|
|
498
|
-
cell.replaceChildren(editor.element);
|
|
499
|
-
this.fields.set(column.prop, {
|
|
500
|
-
editor,
|
|
501
|
-
config
|
|
502
|
-
});
|
|
503
|
-
}
|
|
504
|
-
for (const prop of this.fields.keys()) this.loadField(prop);
|
|
505
|
-
}
|
|
506
|
-
/**
|
|
507
|
-
* Returns the row as the user has edited it so far: a copy of the
|
|
508
|
-
* original, with each edited field replaced by its editor's value.
|
|
509
|
-
* Fields that aren't edited are kept as they were.
|
|
510
|
-
*
|
|
511
|
-
* The result can hold `null` where `Row` says it can't, like an empty
|
|
512
|
-
* number editor on an `age: number` column. Validate before using it.
|
|
513
|
-
*/
|
|
514
|
-
getRow() {
|
|
515
|
-
const row = { ...this.originalData };
|
|
516
|
-
for (const [prop, { editor }] of this.fields) row[prop] = editor.getValue();
|
|
517
|
-
return row;
|
|
518
|
-
}
|
|
519
|
-
/**
|
|
520
|
-
* Checks every field against its rules.
|
|
521
|
-
*
|
|
522
|
-
* Each field is checked with the whole edited row, so a rule can compare
|
|
523
|
-
* it with other new values.
|
|
524
|
-
*
|
|
525
|
-
* @returns The error message of each invalid field, by row property.
|
|
526
|
-
* Empty when everything is valid.
|
|
527
|
-
*/
|
|
528
|
-
validate() {
|
|
529
|
-
const row = this.getRow();
|
|
530
|
-
const errors = /* @__PURE__ */ new Map();
|
|
531
|
-
for (const [prop, { editor, config }] of this.fields) {
|
|
532
|
-
const message = this.loadErrors.has(prop) ? messages.loadFailed : validateValue(config, editor.getValue(), row);
|
|
533
|
-
if (message !== void 0) errors.set(prop, message);
|
|
534
|
-
}
|
|
535
|
-
return errors;
|
|
536
|
-
}
|
|
537
|
-
/**
|
|
538
|
-
* Shows each error message under its field's editor.
|
|
539
|
-
*
|
|
540
|
-
* Errors already shown are removed first.
|
|
541
|
-
*
|
|
542
|
-
* @param errors The error message of each invalid field, by row property,
|
|
543
|
-
* as returned by `validate`.
|
|
544
|
-
*/
|
|
545
|
-
showErrors(errors) {
|
|
546
|
-
this.clearErrors();
|
|
547
|
-
for (const [prop, message] of errors) this.showError(prop, message);
|
|
548
|
-
}
|
|
549
|
-
/**
|
|
550
|
-
* Removes every error message, and the ARIA attributes that point at them.
|
|
551
|
-
*/
|
|
552
|
-
clearErrors() {
|
|
553
|
-
for (const prop of [...this.errors.keys()]) this.hideError(prop);
|
|
554
|
-
}
|
|
555
|
-
/**
|
|
556
|
-
* Puts the cursor in a field's editor. Does nothing if the field isn't
|
|
557
|
-
* being edited.
|
|
558
|
-
*
|
|
559
|
-
* @param prop The row property of the field.
|
|
560
|
-
*/
|
|
561
|
-
focus(prop) {
|
|
562
|
-
this.fields.get(prop)?.editor.focus();
|
|
563
|
-
}
|
|
564
|
-
/**
|
|
565
|
-
* Destroys every editor and error message. Safe to call more than once.
|
|
566
|
-
*
|
|
567
|
-
* The editable cells are left empty: the caller must put their content
|
|
568
|
-
* back.
|
|
569
|
-
*/
|
|
570
|
-
destroy() {
|
|
571
|
-
this.clearErrors();
|
|
572
|
-
for (const { editor } of this.fields.values()) editor.destroy();
|
|
573
|
-
this.fields.clear();
|
|
574
|
-
this.dependents.clear();
|
|
575
|
-
this.loadErrors.clear();
|
|
576
|
-
}
|
|
577
|
-
/**
|
|
578
|
-
* Shows one error message under a field's editor, replacing the one that
|
|
579
|
-
* was there.
|
|
580
|
-
*
|
|
581
|
-
* The editor's element gets `aria-invalid` and points at the message with
|
|
582
|
-
* `aria-describedby`, so screen readers read the error with the field.
|
|
583
|
-
*/
|
|
584
|
-
showError(prop, message) {
|
|
585
|
-
const field = this.fields.get(prop);
|
|
586
|
-
if (field === void 0) return;
|
|
587
|
-
this.hideError(prop);
|
|
588
|
-
errorCount += 1;
|
|
589
|
-
const error = document.createElement("div");
|
|
590
|
-
error.id = `tdb-error-${errorCount}`;
|
|
591
|
-
error.className = "tdb-error";
|
|
592
|
-
error.textContent = message;
|
|
593
|
-
field.editor.element.after(error);
|
|
594
|
-
field.editor.element.setAttribute("aria-invalid", "true");
|
|
595
|
-
field.editor.element.setAttribute("aria-describedby", error.id);
|
|
596
|
-
this.errors.set(prop, error);
|
|
597
|
-
}
|
|
598
|
-
/** Removes one field's error message and its ARIA attributes, if shown. */
|
|
599
|
-
hideError(prop) {
|
|
600
|
-
const error = this.errors.get(prop);
|
|
601
|
-
if (error === void 0) return;
|
|
602
|
-
error.remove();
|
|
603
|
-
this.errors.delete(prop);
|
|
604
|
-
const element = this.fields.get(prop)?.editor.element;
|
|
605
|
-
element?.removeAttribute("aria-invalid");
|
|
606
|
-
element?.removeAttribute("aria-describedby");
|
|
607
|
-
}
|
|
608
|
-
/**
|
|
609
|
-
* Runs when the user changes a field: every select below it in the chain
|
|
610
|
-
* is cleared, then loads its options again.
|
|
611
|
-
*
|
|
612
|
-
* Example, with Country → Region → City: changing the country clears the
|
|
613
|
-
* region and the city, then both reload.
|
|
614
|
-
*/
|
|
615
|
-
changed(prop) {
|
|
616
|
-
const cleared = /* @__PURE__ */ new Set([prop]);
|
|
617
|
-
this.clearBelow(prop, cleared);
|
|
618
|
-
cleared.delete(prop);
|
|
619
|
-
for (const field of cleared) this.loadField(field);
|
|
620
|
-
}
|
|
621
|
-
/**
|
|
622
|
-
* Clears the fields that depend on `prop`, then the ones that depend on
|
|
623
|
-
* those, and so on. `setValue` never calls `onChange`, so the chain has to
|
|
624
|
-
* be walked here.
|
|
625
|
-
*
|
|
626
|
-
* @param cleared The fields cleared so far. A field already in it is
|
|
627
|
-
* skipped, so a loop (A depends on B, B depends on A) can't run forever.
|
|
628
|
-
*/
|
|
629
|
-
clearBelow(prop, cleared) {
|
|
630
|
-
for (const dependent of this.dependents.get(prop) ?? []) {
|
|
631
|
-
if (cleared.has(dependent)) continue;
|
|
632
|
-
cleared.add(dependent);
|
|
633
|
-
this.fields.get(dependent)?.editor.setValue(null);
|
|
634
|
-
this.clearBelow(dependent, cleared);
|
|
635
|
-
}
|
|
636
|
-
}
|
|
637
|
-
/**
|
|
638
|
-
* Loads a field's options with the current row, and shows or hides
|
|
639
|
-
* "Could not load the options." under it.
|
|
640
|
-
*
|
|
641
|
-
* Fields that aren't selects with an options function finish at once.
|
|
642
|
-
*/
|
|
643
|
-
async loadField(prop) {
|
|
644
|
-
const field = this.fields.get(prop);
|
|
645
|
-
if (field?.editor.load === void 0) return;
|
|
646
|
-
const result = await field.editor.load(this.getRow());
|
|
647
|
-
if (result === "cancelled") return;
|
|
648
|
-
if (result === "failed") {
|
|
649
|
-
this.loadErrors.add(prop);
|
|
650
|
-
this.showError(prop, messages.loadFailed);
|
|
651
|
-
return;
|
|
652
|
-
}
|
|
653
|
-
if (this.loadErrors.delete(prop)) this.hideError(prop);
|
|
654
|
-
}
|
|
655
|
-
};
|
|
656
|
-
// src/table/tadabut.ts
|
|
657
|
-
/**
|
|
658
|
-
* A DataTables table, created and owned by Tadabut.
|
|
659
|
-
*
|
|
660
|
-
* @typeParam Row - The shape of one row. Usually worked out from `data`,
|
|
661
|
-
* so there is no need to write it.
|
|
662
|
-
*
|
|
663
|
-
* @example
|
|
664
|
-
* const table = new Tadabut(container, {
|
|
665
|
-
* data: [{ id: 1, name: 'Amina' }],
|
|
666
|
-
* rowId: 'id',
|
|
667
|
-
* columns: [
|
|
668
|
-
* { title: 'ID', prop: 'id' },
|
|
669
|
-
* { title: 'Name', prop: 'name' },
|
|
670
|
-
* ],
|
|
671
|
-
* });
|
|
672
|
-
*/
|
|
673
|
-
var Tadabut = class {
|
|
674
|
-
dataTable;
|
|
675
|
-
tableEl;
|
|
676
|
-
rowId;
|
|
677
|
-
columns;
|
|
678
|
-
/** The `editing` option, with its defaults filled in. */
|
|
679
|
-
editing;
|
|
680
|
-
/** The user's extra classes. `{}` when there are none. */
|
|
681
|
-
classNames;
|
|
682
|
-
onSave;
|
|
683
|
-
/** Whether the table has the actions column, with the Edit buttons. */
|
|
684
|
-
hasActions;
|
|
685
|
-
/** Whether at least one column has an editor. */
|
|
686
|
-
hasEditor;
|
|
687
|
-
/** The rows being edited, by row id. */
|
|
688
|
-
sessions = /* @__PURE__ */ new Map();
|
|
689
|
-
/**
|
|
690
|
-
* Creates a table inside `container`.
|
|
691
|
-
*
|
|
692
|
-
* @param container An empty element. Tadabut adds the table to it, and
|
|
693
|
-
* `destroy()` leaves it empty again.
|
|
694
|
-
* @param options The data and columns to show.
|
|
695
|
-
* @throws If a column has an `editor` but `rowId` is missing. Editing
|
|
696
|
-
* needs `rowId` to find rows.
|
|
697
|
-
*/
|
|
698
|
-
constructor(container, options) {
|
|
699
|
-
this.hasEditor = options.columns.some((column) => column.editor !== void 0);
|
|
700
|
-
if (this.hasEditor && options.rowId === void 0) throw new Error("Tadabut: rowId is required when a column has an editor.");
|
|
701
|
-
this.rowId = options.rowId;
|
|
702
|
-
this.columns = options.columns;
|
|
703
|
-
this.editing = {
|
|
704
|
-
mode: options.editing?.mode ?? "single",
|
|
705
|
-
button: options.editing?.button ?? true
|
|
706
|
-
};
|
|
707
|
-
this.hasActions = this.hasEditor && this.editing.button;
|
|
708
|
-
this.onSave = options.onSave;
|
|
709
|
-
this.classNames = options.classNames ?? {};
|
|
710
|
-
this.tableEl = document.createElement("table");
|
|
711
|
-
addClasses(this.tableEl, [], this.classNames.table);
|
|
712
|
-
container.appendChild(this.tableEl);
|
|
713
|
-
this.dataTable = new DataTable(this.tableEl, {
|
|
714
|
-
data: [...options.data],
|
|
715
|
-
columns: toDataTables(options.columns, this.hasActions, this.classNames)
|
|
716
|
-
});
|
|
717
|
-
if (this.hasActions) this.tableEl.addEventListener("click", this.onClick);
|
|
718
|
-
if (this.hasEditor) this.tableEl.addEventListener("keydown", this.onKeyDown);
|
|
719
|
-
}
|
|
720
|
-
/**
|
|
721
|
-
* Replaces all rows without rebuilding the table.
|
|
722
|
-
*
|
|
723
|
-
* Sorting, search and the current page are kept. If the current page no
|
|
724
|
-
* longer exists (fewer rows than before), DataTables shows the last one.
|
|
725
|
-
*
|
|
726
|
-
* Any row being edited is cancelled first. Unsaved changes are lost.
|
|
727
|
-
*
|
|
728
|
-
* @param rows The new rows. The array is copied, never changed.
|
|
729
|
-
*/
|
|
730
|
-
setData(rows) {
|
|
731
|
-
this.destroySessions();
|
|
732
|
-
this.dataTable.clear().rows.add([...rows]).draw(false);
|
|
733
|
-
}
|
|
734
|
-
/**
|
|
735
|
-
* Starts editing a row: its editable cells are replaced by editors showing
|
|
736
|
-
* the row's values.
|
|
737
|
-
*
|
|
738
|
-
* In `'single'` mode (the default), only one row can be open at a time.
|
|
739
|
-
* In `'multiple'` mode, any number can.
|
|
740
|
-
*
|
|
741
|
-
* @param id The row's id: the value of its `rowId` field.
|
|
742
|
-
* @returns `true` if editing started. `false` if the row doesn't exist,
|
|
743
|
-
* it's already open, another row is open in `'single'` mode, no column
|
|
744
|
-
* is editable, or the row is on a page that hasn't been shown yet.
|
|
745
|
-
*/
|
|
746
|
-
edit(id) {
|
|
747
|
-
if (!this.hasEditor || this.sessions.has(id) || this.editing.mode === "single" && this.sessions.size > 0) return false;
|
|
748
|
-
const row = this.findRow(id);
|
|
749
|
-
if (row === void 0 || row.node() === null) return false;
|
|
750
|
-
const rowIndex = row.index();
|
|
751
|
-
const cells = [];
|
|
752
|
-
this.columns.forEach((column, columnIndex) => {
|
|
753
|
-
if (column.editor !== void 0) {
|
|
754
|
-
const cell = this.dataTable.cell(rowIndex, columnIndex).node();
|
|
755
|
-
cells.push({
|
|
756
|
-
column,
|
|
757
|
-
cell
|
|
758
|
-
});
|
|
759
|
-
}
|
|
760
|
-
});
|
|
761
|
-
this.sessions.set(id, new EditSession(row.data(), cells));
|
|
762
|
-
row.node()?.classList.add("tdb-row-editing");
|
|
763
|
-
if (this.hasActions) this.dataTable.cell(rowIndex, this.columns.length).node().replaceChildren(actionButton("save", "Save", splitClasses(this.classNames.button, this.classNames.save)), actionButton("cancel", "Cancel", splitClasses(this.classNames.button, this.classNames.cancel)));
|
|
764
|
-
return true;
|
|
765
|
-
}
|
|
766
|
-
/**
|
|
767
|
-
* Stops editing a row and puts its original values back. Unsaved changes
|
|
768
|
-
* are lost. Does nothing if the row isn't being edited.
|
|
769
|
-
*
|
|
770
|
-
* @param id The row's id: the value of its `rowId` field.
|
|
771
|
-
*/
|
|
772
|
-
cancel(id) {
|
|
773
|
-
const session = this.sessions.get(id);
|
|
774
|
-
if (session === void 0) return;
|
|
775
|
-
if (session.saving) return;
|
|
776
|
-
session.destroy();
|
|
777
|
-
this.sessions.delete(id);
|
|
778
|
-
this.clearSaveError(id);
|
|
779
|
-
this.findRow(id)?.node()?.classList.remove("tdb-row-editing");
|
|
780
|
-
this.findRow(id)?.invalidate("data").draw(false);
|
|
781
|
-
}
|
|
782
|
-
/**
|
|
783
|
-
* Tells whether a row is being edited.
|
|
784
|
-
*
|
|
785
|
-
* @param id The row's id: the value of its `rowId` field.
|
|
786
|
-
*/
|
|
787
|
-
isEditing(id) {
|
|
788
|
-
return this.sessions.has(id);
|
|
789
|
-
}
|
|
790
|
-
/**
|
|
791
|
-
* Validates and saves the row being edited.
|
|
792
|
-
*
|
|
793
|
-
* 1. Every field is validated. If one fails, its error shows under its
|
|
794
|
-
* input, the cursor goes to the first wrong field, and `onSave` isn't
|
|
795
|
-
* called.
|
|
796
|
-
* 2. The row is locked while `onSave` runs.
|
|
797
|
-
* 3. If `onSave` works, the row closes and shows the new values: the row
|
|
798
|
-
* `onSave` returned, or else the edited row.
|
|
799
|
-
* 4. If `onSave` throws, the row stays open with what the user typed, and
|
|
800
|
-
* the error's message shows under the row.
|
|
801
|
-
*
|
|
802
|
-
* The table's data only changes when saving works.
|
|
803
|
-
*
|
|
804
|
-
* @param id The row's id: the value of its `rowId` field.
|
|
805
|
-
* @returns `true` if the row was saved and closed. `false` if a field is
|
|
806
|
-
* invalid, `onSave` failed, the row isn't being edited, it's already
|
|
807
|
-
* saving, or editing was stopped (by `setData` or `destroy`) while
|
|
808
|
-
* `onSave` was running.
|
|
809
|
-
*/
|
|
810
|
-
async save(id) {
|
|
811
|
-
const session = this.sessions.get(id);
|
|
812
|
-
if (session === void 0 || session.saving) return false;
|
|
813
|
-
session.clearErrors();
|
|
814
|
-
this.clearSaveError(id);
|
|
815
|
-
const errors = session.validate();
|
|
816
|
-
const [firstInvalid] = errors.keys();
|
|
817
|
-
if (firstInvalid !== void 0) {
|
|
818
|
-
session.showErrors(errors);
|
|
819
|
-
session.focus(firstInvalid);
|
|
820
|
-
return false;
|
|
821
|
-
}
|
|
822
|
-
const row = session.getRow();
|
|
823
|
-
this.setSaving(id, true);
|
|
824
|
-
let saved = row;
|
|
825
|
-
try {
|
|
826
|
-
const result = await this.onSave?.(row, { previous: session.originalData });
|
|
827
|
-
if (result !== void 0) saved = result;
|
|
828
|
-
} catch (error) {
|
|
829
|
-
if (this.sessions.get(id) !== session) return false;
|
|
830
|
-
this.setSaving(id, false);
|
|
831
|
-
this.showSaveError(id, error instanceof Error ? error.message : "Could not save.");
|
|
832
|
-
return false;
|
|
833
|
-
}
|
|
834
|
-
if (this.sessions.get(id) !== session) return false;
|
|
835
|
-
session.destroy();
|
|
836
|
-
this.sessions.delete(id);
|
|
837
|
-
this.findRow(id)?.data({ ...saved }).draw(false);
|
|
838
|
-
this.findRow(id)?.node()?.classList.remove("tdb-row-editing");
|
|
839
|
-
this.setSaving(id, false);
|
|
840
|
-
return true;
|
|
841
|
-
}
|
|
842
|
-
/**
|
|
843
|
-
* Removes the table and everything Tadabut added to the container.
|
|
844
|
-
* Any row being edited is cancelled first.
|
|
845
|
-
*/
|
|
846
|
-
destroy() {
|
|
847
|
-
this.tableEl.removeEventListener("click", this.onClick);
|
|
848
|
-
this.tableEl.removeEventListener("keydown", this.onKeyDown);
|
|
849
|
-
this.destroySessions();
|
|
850
|
-
this.dataTable.destroy();
|
|
851
|
-
this.tableEl.remove();
|
|
852
|
-
}
|
|
853
|
-
/**
|
|
854
|
-
* Finds a row by its id. `undefined` if there is no `rowId` option, or no
|
|
855
|
-
* row with that id.
|
|
856
|
-
*/
|
|
857
|
-
findRow(id) {
|
|
858
|
-
const rowId = this.rowId;
|
|
859
|
-
if (rowId === void 0) return;
|
|
860
|
-
const row = this.dataTable.row((_index, data) => data[rowId] === id);
|
|
861
|
-
return row.any() ? row : void 0;
|
|
862
|
-
}
|
|
863
|
-
/**
|
|
864
|
-
* Destroys every open editor, without putting the cells' content back.
|
|
865
|
-
* Used right before the rows or the whole table go away.
|
|
866
|
-
*/
|
|
867
|
-
destroySessions() {
|
|
868
|
-
for (const session of this.sessions.values()) session.destroy();
|
|
869
|
-
this.sessions.clear();
|
|
870
|
-
}
|
|
871
|
-
/**
|
|
872
|
-
* Locks or unlocks a row while it's being saved: sets the session's
|
|
873
|
-
* `saving` flag, and `inert` and the `tdb-row-saving` class on the row.
|
|
874
|
-
*
|
|
875
|
-
* Works even when the session is already gone, so a row can always be
|
|
876
|
-
* unlocked after saving.
|
|
877
|
-
*/
|
|
878
|
-
setSaving(id, saving) {
|
|
879
|
-
const session = this.sessions.get(id);
|
|
880
|
-
if (session !== void 0) session.saving = saving;
|
|
881
|
-
const tr = this.findRow(id)?.node();
|
|
882
|
-
if (tr) {
|
|
883
|
-
tr.inert = saving;
|
|
884
|
-
tr.classList.toggle("tdb-row-saving", saving);
|
|
885
|
-
}
|
|
886
|
-
}
|
|
887
|
-
/**
|
|
888
|
-
* Shows a save error on its own line under the row. The line (a `<tr>`)
|
|
889
|
-
* gets `tdb-save-error`, and the message inside it gets
|
|
890
|
-
* `tdb-save-error-message`.
|
|
891
|
-
*
|
|
892
|
-
* The message needs its own class: frameworks like Bootstrap set a color
|
|
893
|
-
* on every table cell, which hides a color set on the `<tr>`.
|
|
894
|
-
*
|
|
895
|
-
* The message is set as text, never as HTML: it often comes from a server,
|
|
896
|
-
* and HTML in it could run code in the page.
|
|
897
|
-
*/
|
|
898
|
-
showSaveError(id, message) {
|
|
899
|
-
const error = document.createElement("div");
|
|
900
|
-
error.className = "tdb-save-error-message";
|
|
901
|
-
error.textContent = message;
|
|
902
|
-
this.findRow(id)?.child(error, "tdb-save-error").show();
|
|
903
|
-
}
|
|
904
|
-
/** Removes the save error under a row, if there is one. */
|
|
905
|
-
clearSaveError(id) {
|
|
906
|
-
this.findRow(id)?.child.remove();
|
|
907
|
-
}
|
|
908
|
-
/**
|
|
909
|
-
* Finds the id of the row an element is in.
|
|
910
|
-
*
|
|
911
|
-
* `undefined` if the element isn't in a row DataTables knows, like the
|
|
912
|
-
* save error line under a row, or a table inside a custom editor.
|
|
913
|
-
*/
|
|
914
|
-
rowIdOf(element) {
|
|
915
|
-
const tr = element.closest("tr");
|
|
916
|
-
if (tr === null || this.rowId === void 0) return;
|
|
917
|
-
const row = this.dataTable.row(tr);
|
|
918
|
-
return row.any() ? row.data()[this.rowId] : void 0;
|
|
919
|
-
}
|
|
920
|
-
/**
|
|
921
|
-
* Handles clicks on the Edit, Save and Cancel buttons.
|
|
922
|
-
*
|
|
923
|
-
* One listener on the whole table, not one per button: DataTables adds and
|
|
924
|
-
* removes rows all the time (paging, `setData`), and new buttons would need
|
|
925
|
-
* new listeners.
|
|
926
|
-
*
|
|
927
|
-
* An arrow function, so `this` is still the Tadabut when the browser calls
|
|
928
|
-
* it, and the same function can be removed in `destroy`.
|
|
929
|
-
*/
|
|
930
|
-
onClick = (event) => {
|
|
931
|
-
if (!(event.target instanceof Element)) return;
|
|
932
|
-
const button = event.target.closest("[data-tdb-action]");
|
|
933
|
-
const id = button ? this.rowIdOf(button) : void 0;
|
|
934
|
-
if (!button || id === void 0) return;
|
|
935
|
-
switch (button.dataset.tdbAction) {
|
|
936
|
-
case "edit":
|
|
937
|
-
this.edit(id);
|
|
938
|
-
break;
|
|
939
|
-
case "save":
|
|
940
|
-
this.save(id);
|
|
941
|
-
break;
|
|
942
|
-
case "cancel": this.cancel(id);
|
|
943
|
-
}
|
|
944
|
-
};
|
|
945
|
-
/**
|
|
946
|
-
* Enter saves the row being edited, Escape cancels it.
|
|
947
|
-
*
|
|
948
|
-
* An arrow function, for the same reasons as `onClick`.
|
|
949
|
-
*/
|
|
950
|
-
onKeyDown = (event) => {
|
|
951
|
-
if (event.defaultPrevented) return;
|
|
952
|
-
if (!(event.target instanceof Element) || event.target.closest("button")) return;
|
|
953
|
-
const id = this.rowIdOf(event.target);
|
|
954
|
-
if (id === void 0 || !this.sessions.has(id)) return;
|
|
955
|
-
switch (event.key) {
|
|
956
|
-
case "Enter":
|
|
957
|
-
event.preventDefault();
|
|
958
|
-
this.save(id);
|
|
959
|
-
break;
|
|
960
|
-
case "Escape":
|
|
961
|
-
event.preventDefault();
|
|
962
|
-
this.cancel(id);
|
|
963
|
-
}
|
|
964
|
-
};
|
|
965
|
-
};
|
|
966
|
-
/**
|
|
967
|
-
* Makes one of the buttons in the actions column.
|
|
968
|
-
*
|
|
969
|
-
* Built with `createElement`, not an HTML string, so a label can never be
|
|
970
|
-
* read as HTML, even once labels can be translated.
|
|
971
|
-
*
|
|
972
|
-
* @param extra The user's classes from `classNames`, already split.
|
|
973
|
-
*/
|
|
974
|
-
function actionButton(action, label, extra) {
|
|
975
|
-
const button = document.createElement("button");
|
|
976
|
-
button.type = "button";
|
|
977
|
-
button.classList.add("tdb-button", `tdb-button-${action}`, ...extra);
|
|
978
|
-
button.dataset.tdbAction = action;
|
|
979
|
-
button.textContent = label;
|
|
980
|
-
return button;
|
|
981
|
-
}
|
|
982
|
-
export { Tadabut as t };
|