tadabut 0.3.0 → 0.5.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 +336 -52
- package/dist/{index-DYUdG69c.d.ts → index-KmSXRJlI.d.ts} +252 -29
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/react.d.ts +10 -6
- package/dist/react.js +46 -7
- package/dist/src-CBK3xu9-.js +1413 -0
- package/package.json +3 -3
- package/dist/src-cI0CYV46.js +0 -811
package/dist/src-cI0CYV46.js
DELETED
|
@@ -1,811 +0,0 @@
|
|
|
1
|
-
import DataTable from "datatables.net";
|
|
2
|
-
// src/editors/classes.ts
|
|
3
|
-
/**
|
|
4
|
-
* `'btn btn-sm '` → `['btn', 'btn-sm']`. Empty pieces are dropped because
|
|
5
|
-
* `classList.add('')` throws.
|
|
6
|
-
*/
|
|
7
|
-
function splitClasses(...lists) {
|
|
8
|
-
return lists.flatMap((list) => list?.split(/\s+/).filter(Boolean) ?? []);
|
|
9
|
-
}
|
|
10
|
-
/** Adds Tadabut's own classes, then the user's. */
|
|
11
|
-
function addClasses(element, ownClasses, className) {
|
|
12
|
-
element.classList.add(...ownClasses, ...splitClasses(className));
|
|
13
|
-
}
|
|
14
|
-
// src/columns/escape.ts
|
|
15
|
-
/** Characters that mean something in HTML. */
|
|
16
|
-
const entities = {
|
|
17
|
-
"&": "&",
|
|
18
|
-
"<": "<",
|
|
19
|
-
">": ">",
|
|
20
|
-
"\"": """,
|
|
21
|
-
"'": "'"
|
|
22
|
-
};
|
|
23
|
-
/** DataTables inserts rendered cells as HTML, so labels go through this. */
|
|
24
|
-
function escapeHtml(text) {
|
|
25
|
-
return text.replace(/[&<>"']/g, (char) => entities[char] ?? char);
|
|
26
|
-
}
|
|
27
|
-
// src/columns/to-datatables.ts
|
|
28
|
-
/** The Edit button column. Save and Cancel replace the button while a row is open. */
|
|
29
|
-
function actionsColumn(classNames, label) {
|
|
30
|
-
return {
|
|
31
|
-
title: "",
|
|
32
|
-
data: null,
|
|
33
|
-
className: "tdb-actions",
|
|
34
|
-
orderable: false,
|
|
35
|
-
searchable: false,
|
|
36
|
-
defaultContent: `<button type="button" class="${escapeHtml([
|
|
37
|
-
"tdb-button",
|
|
38
|
-
"tdb-button-edit",
|
|
39
|
-
...splitClasses(classNames.button, classNames.edit)
|
|
40
|
-
].join(" "))}" data-tdb-action="edit">${escapeHtml(label)}</button>`
|
|
41
|
-
};
|
|
42
|
-
}
|
|
43
|
-
/**
|
|
44
|
-
* The actions column is always appended last, because the rest of Tadabut
|
|
45
|
-
* finds cells by the user's column index.
|
|
46
|
-
*/
|
|
47
|
-
function toDataTables(columns, withActions, classNames, texts) {
|
|
48
|
-
const converted = columns.map((column) => {
|
|
49
|
-
const label = labelOf(column);
|
|
50
|
-
return {
|
|
51
|
-
...defined({
|
|
52
|
-
width: column.width,
|
|
53
|
-
visible: column.visible,
|
|
54
|
-
orderable: column.orderable,
|
|
55
|
-
searchable: column.searchable,
|
|
56
|
-
className: column.className
|
|
57
|
-
}),
|
|
58
|
-
title: column.title,
|
|
59
|
-
data: column.prop,
|
|
60
|
-
render: (value, type, row) => {
|
|
61
|
-
switch (type) {
|
|
62
|
-
case "display": return escapeHtml(label(value, row));
|
|
63
|
-
case "filter": return label(value, row);
|
|
64
|
-
default: return value;
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
};
|
|
68
|
-
});
|
|
69
|
-
return withActions ? [...converted, actionsColumn(classNames, texts.edit)] : converted;
|
|
70
|
-
}
|
|
71
|
-
/** `render`, then `labelProp`, then a fixed list's label, then the value. */
|
|
72
|
-
function labelOf(column) {
|
|
73
|
-
const { render, editor } = column;
|
|
74
|
-
if (render !== void 0) return render;
|
|
75
|
-
if (editor?.type === "select") {
|
|
76
|
-
const labelProp = editor.labelProp;
|
|
77
|
-
if (labelProp !== void 0) return (_value, row) => asText(row[labelProp]);
|
|
78
|
-
if (typeof editor.options !== "function") {
|
|
79
|
-
const options = editor.options;
|
|
80
|
-
return (value) => options.find((option) => option.value === value)?.label ?? asText(value);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
return asText;
|
|
84
|
-
}
|
|
85
|
-
/** `null` and `undefined` become an empty cell. */
|
|
86
|
-
function asText(value) {
|
|
87
|
-
return value === null || value === void 0 ? "" : String(value);
|
|
88
|
-
}
|
|
89
|
-
/** Drops the keys whose value is `undefined`. */
|
|
90
|
-
function defined(values) {
|
|
91
|
-
const kept = Object.entries(values).filter(([, value]) => value !== void 0);
|
|
92
|
-
return Object.fromEntries(kept);
|
|
93
|
-
}
|
|
94
|
-
// src/texts/en.ts
|
|
95
|
-
/** English, the default. */
|
|
96
|
-
const en = {
|
|
97
|
-
edit: "Edit",
|
|
98
|
-
save: "Save",
|
|
99
|
-
cancel: "Cancel",
|
|
100
|
-
loading: "Loading…",
|
|
101
|
-
saveFailed: "Could not save.",
|
|
102
|
-
loadFailed: "Could not load the options.",
|
|
103
|
-
required: "This field is required.",
|
|
104
|
-
patternMismatch: "Wrong format.",
|
|
105
|
-
tooShort: (min) => `Must be at least ${min} characters.`,
|
|
106
|
-
tooLong: (max) => `Must be at most ${max} characters.`,
|
|
107
|
-
tooSmall: (min) => `Must be ${min} or more.`,
|
|
108
|
-
tooLarge: (max) => `Must be ${max} or less.`,
|
|
109
|
-
tooEarly: (min) => `Must be on or after ${min}.`,
|
|
110
|
-
tooLate: (max) => `Must be on or before ${max}.`
|
|
111
|
-
};
|
|
112
|
-
// src/editors/checkbox.ts
|
|
113
|
-
/**
|
|
114
|
-
* Builds the checkbox. `_config` is unused for now, but keeps the same
|
|
115
|
-
* shape as the other editors.
|
|
116
|
-
*/
|
|
117
|
-
function createCheckboxEditor(_config, context) {
|
|
118
|
-
const input = document.createElement("input");
|
|
119
|
-
input.type = "checkbox";
|
|
120
|
-
addClasses(input, ["tdb-input", "tdb-input-checkbox"], context.className);
|
|
121
|
-
/** `null` shows as unchecked. */
|
|
122
|
-
const setValue = (value) => {
|
|
123
|
-
input.checked = value === true;
|
|
124
|
-
};
|
|
125
|
-
setValue(context.value);
|
|
126
|
-
input.addEventListener("change", () => context.onChange(input.checked));
|
|
127
|
-
return {
|
|
128
|
-
element: input,
|
|
129
|
-
getValue: () => input.checked,
|
|
130
|
-
setValue,
|
|
131
|
-
focus: () => input.focus(),
|
|
132
|
-
destroy: () => input.remove()
|
|
133
|
-
};
|
|
134
|
-
}
|
|
135
|
-
// src/editors/date.ts
|
|
136
|
-
/** Builds the date input. */
|
|
137
|
-
function createDateEditor(config, context) {
|
|
138
|
-
const input = document.createElement("input");
|
|
139
|
-
input.type = "date";
|
|
140
|
-
addClasses(input, ["tdb-input", "tdb-input-date"], context.className);
|
|
141
|
-
if (config.min !== void 0) input.min = config.min;
|
|
142
|
-
if (config.max !== void 0) input.max = config.max;
|
|
143
|
-
/** Empty gives `null`. */
|
|
144
|
-
const getValue = () => input.value === "" ? null : input.value;
|
|
145
|
-
/** `null` shows as empty. */
|
|
146
|
-
const setValue = (value) => {
|
|
147
|
-
input.value = value ?? "";
|
|
148
|
-
};
|
|
149
|
-
setValue(context.value ?? null);
|
|
150
|
-
input.addEventListener("input", () => context.onChange(getValue()));
|
|
151
|
-
return {
|
|
152
|
-
element: input,
|
|
153
|
-
getValue,
|
|
154
|
-
setValue,
|
|
155
|
-
focus: () => input.focus(),
|
|
156
|
-
destroy: () => input.remove()
|
|
157
|
-
};
|
|
158
|
-
}
|
|
159
|
-
// src/editors/number.ts
|
|
160
|
-
/** Builds the number input. */
|
|
161
|
-
function createNumberEditor(config, context) {
|
|
162
|
-
const input = document.createElement("input");
|
|
163
|
-
input.type = "number";
|
|
164
|
-
addClasses(input, ["tdb-input", "tdb-input-number"], context.className);
|
|
165
|
-
if (config.min !== void 0) input.min = String(config.min);
|
|
166
|
-
if (config.max !== void 0) input.max = String(config.max);
|
|
167
|
-
if (config.step !== void 0) input.step = String(config.step);
|
|
168
|
-
/** Empty gives `null`, not `0`. */
|
|
169
|
-
const getValue = () => input.value === "" ? null : input.valueAsNumber;
|
|
170
|
-
/** `null` shows as empty. */
|
|
171
|
-
const setValue = (value) => {
|
|
172
|
-
input.value = value === null ? "" : String(value);
|
|
173
|
-
};
|
|
174
|
-
setValue(context.value ?? null);
|
|
175
|
-
input.addEventListener("input", () => context.onChange(getValue()));
|
|
176
|
-
return {
|
|
177
|
-
element: input,
|
|
178
|
-
getValue,
|
|
179
|
-
setValue,
|
|
180
|
-
focus: () => input.focus(),
|
|
181
|
-
destroy: () => input.remove()
|
|
182
|
-
};
|
|
183
|
-
}
|
|
184
|
-
// src/editors/select.ts
|
|
185
|
-
/**
|
|
186
|
-
* `<option>` values are always strings, so the picked value is looked up by
|
|
187
|
-
* index to keep its real type. Until a loader's options arrive, the starting
|
|
188
|
-
* value is kept in `current`.
|
|
189
|
-
*/
|
|
190
|
-
function createSelectEditor(config, context, texts = en) {
|
|
191
|
-
const select = document.createElement("select");
|
|
192
|
-
addClasses(select, ["tdb-input", "tdb-input-select"], context.className);
|
|
193
|
-
/** The options on screen. Empty while a loader runs. */
|
|
194
|
-
let options = [];
|
|
195
|
-
/** The value, kept even while there are no options to pick it from. */
|
|
196
|
-
let current = context.value;
|
|
197
|
-
/** The running load, so a newer one or `destroy` can abort it. */
|
|
198
|
-
let controller;
|
|
199
|
-
/** Selects `current`. A value that isn't in the list becomes `null`. */
|
|
200
|
-
const pickCurrent = () => {
|
|
201
|
-
select.selectedIndex = options.findIndex((option) => option.value === current);
|
|
202
|
-
if (select.selectedIndex === -1) current = null;
|
|
203
|
-
};
|
|
204
|
-
/** Shows `list`, enables the select and picks `current`. */
|
|
205
|
-
const fill = (list) => {
|
|
206
|
-
options = list;
|
|
207
|
-
select.replaceChildren(...list.map((option) => {
|
|
208
|
-
const element = document.createElement("option");
|
|
209
|
-
element.textContent = option.label;
|
|
210
|
-
return element;
|
|
211
|
-
}));
|
|
212
|
-
select.disabled = false;
|
|
213
|
-
pickCurrent();
|
|
214
|
-
};
|
|
215
|
-
/** Shows a single disabled line, like "Loading…". */
|
|
216
|
-
const showOnly = (text) => {
|
|
217
|
-
options = [];
|
|
218
|
-
const element = document.createElement("option");
|
|
219
|
-
element.textContent = text;
|
|
220
|
-
select.replaceChildren(element);
|
|
221
|
-
select.disabled = true;
|
|
222
|
-
};
|
|
223
|
-
/** Runs the `options` function and shows its result. */
|
|
224
|
-
const load = async (row) => {
|
|
225
|
-
if (typeof config.options !== "function") return "loaded";
|
|
226
|
-
controller?.abort();
|
|
227
|
-
const own = new AbortController();
|
|
228
|
-
controller = own;
|
|
229
|
-
try {
|
|
230
|
-
const result = config.options(row, { signal: own.signal });
|
|
231
|
-
if (!(result instanceof Promise)) {
|
|
232
|
-
fill(result);
|
|
233
|
-
return "loaded";
|
|
234
|
-
}
|
|
235
|
-
showOnly(texts.loading);
|
|
236
|
-
const list = await result;
|
|
237
|
-
if (own.signal.aborted) return "cancelled";
|
|
238
|
-
fill(list);
|
|
239
|
-
return "loaded";
|
|
240
|
-
} catch {
|
|
241
|
-
if (own.signal.aborted) return "cancelled";
|
|
242
|
-
showOnly("");
|
|
243
|
-
return "failed";
|
|
244
|
-
}
|
|
245
|
-
};
|
|
246
|
-
/** The value, even while options are loading. */
|
|
247
|
-
const getValue = () => current;
|
|
248
|
-
/** `undefined` while options aren't loaded. */
|
|
249
|
-
const getLabel = () => current === null ? null : options.find((option) => option.value === current)?.label;
|
|
250
|
-
/** While loading, the value is only remembered. */
|
|
251
|
-
const setValue = (value) => {
|
|
252
|
-
current = value;
|
|
253
|
-
if (!select.disabled) pickCurrent();
|
|
254
|
-
};
|
|
255
|
-
if (typeof config.options !== "function") fill(config.options);
|
|
256
|
-
select.addEventListener("change", () => {
|
|
257
|
-
current = options[select.selectedIndex]?.value ?? null;
|
|
258
|
-
context.onChange(current);
|
|
259
|
-
});
|
|
260
|
-
return {
|
|
261
|
-
element: select,
|
|
262
|
-
getValue,
|
|
263
|
-
getLabel,
|
|
264
|
-
setValue,
|
|
265
|
-
load,
|
|
266
|
-
focus: () => select.focus(),
|
|
267
|
-
destroy: () => {
|
|
268
|
-
controller?.abort();
|
|
269
|
-
select.remove();
|
|
270
|
-
}
|
|
271
|
-
};
|
|
272
|
-
}
|
|
273
|
-
// src/editors/text.ts
|
|
274
|
-
/** Builds the text input. `null` shows as empty. */
|
|
275
|
-
function createTextEditor(config, context) {
|
|
276
|
-
const input = document.createElement("input");
|
|
277
|
-
input.type = "text";
|
|
278
|
-
addClasses(input, ["tdb-input", "tdb-input-text"], context.className);
|
|
279
|
-
if (config.minLength !== void 0) input.minLength = config.minLength;
|
|
280
|
-
if (config.maxLength !== void 0) input.maxLength = config.maxLength;
|
|
281
|
-
if (config.pattern !== void 0) input.pattern = config.pattern;
|
|
282
|
-
input.value = context.value ?? "";
|
|
283
|
-
input.addEventListener("input", () => context.onChange(input.value));
|
|
284
|
-
return {
|
|
285
|
-
element: input,
|
|
286
|
-
getValue: () => input.value,
|
|
287
|
-
setValue: (value) => {
|
|
288
|
-
input.value = value ?? "";
|
|
289
|
-
},
|
|
290
|
-
focus: () => input.focus(),
|
|
291
|
-
destroy: () => input.remove()
|
|
292
|
-
};
|
|
293
|
-
}
|
|
294
|
-
// src/editors/create.ts
|
|
295
|
-
/**
|
|
296
|
-
* `Column<Row>` already checked that each config fits its column, so the
|
|
297
|
-
* casts below are safe.
|
|
298
|
-
*/
|
|
299
|
-
function createEditor(config, context, texts = en) {
|
|
300
|
-
const fullContext = {
|
|
301
|
-
...context,
|
|
302
|
-
className: config.className
|
|
303
|
-
};
|
|
304
|
-
switch (config.type) {
|
|
305
|
-
case "text": return loosen(createTextEditor(config, tighten(fullContext)));
|
|
306
|
-
case "number": return loosen(createNumberEditor(config, tighten(fullContext)));
|
|
307
|
-
case "date": return loosen(createDateEditor(config, tighten(fullContext)));
|
|
308
|
-
case "checkbox": return loosen(createCheckboxEditor(config, tighten(fullContext)));
|
|
309
|
-
case "select": return createSelectEditor(config, fullContext, texts);
|
|
310
|
-
case "custom": return config.create(fullContext);
|
|
311
|
-
default: throw new Error(`Unknown editor: ${JSON.stringify(config)}`);
|
|
312
|
-
}
|
|
313
|
-
}
|
|
314
|
-
/** Gives a built-in editor the context type it expects. */
|
|
315
|
-
function tighten(context) {
|
|
316
|
-
return context;
|
|
317
|
-
}
|
|
318
|
-
/** Turns a typed editor back into the loose type. */
|
|
319
|
-
function loosen(editor) {
|
|
320
|
-
return editor;
|
|
321
|
-
}
|
|
322
|
-
// src/validation/validate.ts
|
|
323
|
-
/** Returns the first error message, or `undefined`. */
|
|
324
|
-
function validateValue(config, value, row, texts = en) {
|
|
325
|
-
if (value === null || value === void 0 || value === "") return config.required ? texts.required : void 0;
|
|
326
|
-
return builtInError(config, value, texts) ?? config.validate?.(value, row);
|
|
327
|
-
}
|
|
328
|
-
/** The rules each built-in editor brings. */
|
|
329
|
-
function builtInError(config, value, texts) {
|
|
330
|
-
switch (config.type) {
|
|
331
|
-
case "text": return typeof value === "string" ? textError(config, value, texts) : void 0;
|
|
332
|
-
case "number": return typeof value === "number" ? rangeError(config, value, texts) : void 0;
|
|
333
|
-
case "date": return typeof value === "string" ? dateError(config, value, texts) : void 0;
|
|
334
|
-
case "checkbox":
|
|
335
|
-
case "select":
|
|
336
|
-
case "custom": return;
|
|
337
|
-
default: throw new Error(`Unknown editor: ${JSON.stringify(config)}`);
|
|
338
|
-
}
|
|
339
|
-
}
|
|
340
|
-
/** `minLength`, `maxLength` and `pattern`. */
|
|
341
|
-
function textError(config, value, texts) {
|
|
342
|
-
if (config.minLength !== void 0 && value.length < config.minLength) return texts.tooShort(config.minLength);
|
|
343
|
-
if (config.maxLength !== void 0 && value.length > config.maxLength) return texts.tooLong(config.maxLength);
|
|
344
|
-
if (config.pattern !== void 0 && !matchesWhole(config.pattern, value)) return texts.patternMismatch;
|
|
345
|
-
}
|
|
346
|
-
/** `min` and `max` for numbers. */
|
|
347
|
-
function rangeError(config, value, texts) {
|
|
348
|
-
if (config.min !== void 0 && value < config.min) return texts.tooSmall(config.min);
|
|
349
|
-
if (config.max !== void 0 && value > config.max) return texts.tooLarge(config.max);
|
|
350
|
-
}
|
|
351
|
-
/** `min` and `max` for dates. */
|
|
352
|
-
function dateError(config, value, texts) {
|
|
353
|
-
if (config.min !== void 0 && value < config.min) return texts.tooEarly(config.min);
|
|
354
|
-
if (config.max !== void 0 && value > config.max) return texts.tooLate(config.max);
|
|
355
|
-
}
|
|
356
|
-
/**
|
|
357
|
-
* Same rules as the HTML `pattern` attribute: whole value, `v` flag, and an
|
|
358
|
-
* invalid pattern is ignored. The group stops `a|b` from becoming `^a|b$`.
|
|
359
|
-
*/
|
|
360
|
-
function matchesWhole(pattern, value) {
|
|
361
|
-
let regex;
|
|
362
|
-
try {
|
|
363
|
-
regex = new RegExp(`^(?:${pattern})$`, "v");
|
|
364
|
-
} catch {
|
|
365
|
-
return true;
|
|
366
|
-
}
|
|
367
|
-
return regex.test(value);
|
|
368
|
-
}
|
|
369
|
-
// src/editing/session.ts
|
|
370
|
-
/** For unique error ids (`aria-describedby` needs one). */
|
|
371
|
-
let errorCount = 0;
|
|
372
|
-
/**
|
|
373
|
-
* One open row. Doesn't know about DataTables: `Tadabut` hands it the cells
|
|
374
|
-
* and restores them afterwards.
|
|
375
|
-
*/
|
|
376
|
-
var EditSession = class {
|
|
377
|
-
/** A copy of the row from before editing. `onSave` gets it as `previous`. */
|
|
378
|
-
originalData;
|
|
379
|
-
/** Open fields, by row field. */
|
|
380
|
-
fields = /* @__PURE__ */ new Map();
|
|
381
|
-
/** Error messages on screen, by row field. */
|
|
382
|
-
errors = /* @__PURE__ */ new Map();
|
|
383
|
-
/** For each field, the selects that depend on it. */
|
|
384
|
-
dependents = /* @__PURE__ */ new Map();
|
|
385
|
-
/** Selects whose last options load failed. */
|
|
386
|
-
loadErrors = /* @__PURE__ */ new Set();
|
|
387
|
-
/** Set by `Tadabut` while `onSave` runs. */
|
|
388
|
-
saving = false;
|
|
389
|
-
/** Error messages and the "Loading…" text. */
|
|
390
|
-
texts;
|
|
391
|
-
/** Puts an editor in each cell and starts loading select options. */
|
|
392
|
-
constructor(row, cells, texts = en) {
|
|
393
|
-
this.originalData = { ...row };
|
|
394
|
-
this.texts = texts;
|
|
395
|
-
for (const { column, cell } of cells) {
|
|
396
|
-
if (column.editor === void 0) continue;
|
|
397
|
-
const config = column.editor;
|
|
398
|
-
if (config.type === "select" && config.dependsOn !== void 0) {
|
|
399
|
-
const list = this.dependents.get(config.dependsOn) ?? [];
|
|
400
|
-
list.push(column.prop);
|
|
401
|
-
this.dependents.set(config.dependsOn, list);
|
|
402
|
-
}
|
|
403
|
-
const editor = createEditor(config, {
|
|
404
|
-
value: this.originalData[column.prop],
|
|
405
|
-
onChange: () => this.changed(column.prop)
|
|
406
|
-
}, texts);
|
|
407
|
-
cell.replaceChildren(editor.element);
|
|
408
|
-
this.fields.set(column.prop, {
|
|
409
|
-
editor,
|
|
410
|
-
config
|
|
411
|
-
});
|
|
412
|
-
}
|
|
413
|
-
for (const prop of this.fields.keys()) this.loadField(prop);
|
|
414
|
-
}
|
|
415
|
-
/**
|
|
416
|
-
* The row with the current editor values. It can hold `null` where `Row`
|
|
417
|
-
* doesn't allow it (an empty number input), so validate before trusting it.
|
|
418
|
-
*/
|
|
419
|
-
getRow() {
|
|
420
|
-
const row = { ...this.originalData };
|
|
421
|
-
for (const [prop, { editor, config }] of this.fields) {
|
|
422
|
-
row[prop] = editor.getValue();
|
|
423
|
-
if (config.type === "select" && config.labelProp !== void 0) {
|
|
424
|
-
const label = editor.getLabel?.();
|
|
425
|
-
if (label !== void 0) row[config.labelProp] = label;
|
|
426
|
-
}
|
|
427
|
-
}
|
|
428
|
-
return row;
|
|
429
|
-
}
|
|
430
|
-
/** Every rule gets the whole edited row, so fields can be compared. */
|
|
431
|
-
validate() {
|
|
432
|
-
const row = this.getRow();
|
|
433
|
-
const errors = /* @__PURE__ */ new Map();
|
|
434
|
-
for (const [prop, { editor, config }] of this.fields) {
|
|
435
|
-
const message = this.loadErrors.has(prop) ? this.texts.loadFailed : validateValue(config, editor.getValue(), row, this.texts);
|
|
436
|
-
if (message !== void 0) errors.set(prop, message);
|
|
437
|
-
}
|
|
438
|
-
return errors;
|
|
439
|
-
}
|
|
440
|
-
/** Replaces whatever errors are currently shown. */
|
|
441
|
-
showErrors(errors) {
|
|
442
|
-
this.clearErrors();
|
|
443
|
-
for (const [prop, message] of errors) this.showError(prop, message);
|
|
444
|
-
}
|
|
445
|
-
/** Removes every error message and its ARIA attributes. */
|
|
446
|
-
clearErrors() {
|
|
447
|
-
for (const prop of [...this.errors.keys()]) this.hideError(prop);
|
|
448
|
-
}
|
|
449
|
-
/** Moves the cursor to a field. */
|
|
450
|
-
focus(prop) {
|
|
451
|
-
this.fields.get(prop)?.editor.focus();
|
|
452
|
-
}
|
|
453
|
-
/** Leaves the cells empty; the caller redraws them. Safe to call twice. */
|
|
454
|
-
destroy() {
|
|
455
|
-
this.clearErrors();
|
|
456
|
-
for (const { editor } of this.fields.values()) editor.destroy();
|
|
457
|
-
this.fields.clear();
|
|
458
|
-
this.dependents.clear();
|
|
459
|
-
this.loadErrors.clear();
|
|
460
|
-
}
|
|
461
|
-
/** Shows one message under a field, linked with `aria-describedby`. */
|
|
462
|
-
showError(prop, message) {
|
|
463
|
-
const field = this.fields.get(prop);
|
|
464
|
-
if (field === void 0) return;
|
|
465
|
-
this.hideError(prop);
|
|
466
|
-
errorCount += 1;
|
|
467
|
-
const error = document.createElement("div");
|
|
468
|
-
error.id = `tdb-error-${errorCount}`;
|
|
469
|
-
error.className = "tdb-error";
|
|
470
|
-
error.textContent = message;
|
|
471
|
-
field.editor.element.after(error);
|
|
472
|
-
field.editor.element.setAttribute("aria-invalid", "true");
|
|
473
|
-
field.editor.element.setAttribute("aria-describedby", error.id);
|
|
474
|
-
this.errors.set(prop, error);
|
|
475
|
-
}
|
|
476
|
-
/** Removes one field's message, if there is one. */
|
|
477
|
-
hideError(prop) {
|
|
478
|
-
const error = this.errors.get(prop);
|
|
479
|
-
if (error === void 0) return;
|
|
480
|
-
error.remove();
|
|
481
|
-
this.errors.delete(prop);
|
|
482
|
-
const element = this.fields.get(prop)?.editor.element;
|
|
483
|
-
element?.removeAttribute("aria-invalid");
|
|
484
|
-
element?.removeAttribute("aria-describedby");
|
|
485
|
-
}
|
|
486
|
-
/** Country changed: clear region and city, then reload both. */
|
|
487
|
-
changed(prop) {
|
|
488
|
-
const cleared = /* @__PURE__ */ new Set([prop]);
|
|
489
|
-
this.clearBelow(prop, cleared);
|
|
490
|
-
cleared.delete(prop);
|
|
491
|
-
for (const field of cleared) this.loadField(field);
|
|
492
|
-
}
|
|
493
|
-
/**
|
|
494
|
-
* Walks the chain by hand, since `setValue` doesn't fire `onChange`.
|
|
495
|
-
* `cleared` also stops dependency loops.
|
|
496
|
-
*/
|
|
497
|
-
clearBelow(prop, cleared) {
|
|
498
|
-
for (const dependent of this.dependents.get(prop) ?? []) {
|
|
499
|
-
if (cleared.has(dependent)) continue;
|
|
500
|
-
cleared.add(dependent);
|
|
501
|
-
this.fields.get(dependent)?.editor.setValue(null);
|
|
502
|
-
this.clearBelow(dependent, cleared);
|
|
503
|
-
}
|
|
504
|
-
}
|
|
505
|
-
/** Loads one select's options and shows or hides the load error. */
|
|
506
|
-
async loadField(prop) {
|
|
507
|
-
const field = this.fields.get(prop);
|
|
508
|
-
if (field?.editor.load === void 0) return;
|
|
509
|
-
const result = await field.editor.load(this.getRow());
|
|
510
|
-
if (result === "cancelled") return;
|
|
511
|
-
if (result === "failed") {
|
|
512
|
-
this.loadErrors.add(prop);
|
|
513
|
-
this.showError(prop, this.texts.loadFailed);
|
|
514
|
-
return;
|
|
515
|
-
}
|
|
516
|
-
if (this.loadErrors.delete(prop)) this.hideError(prop);
|
|
517
|
-
}
|
|
518
|
-
};
|
|
519
|
-
// src/table/tadabut.ts
|
|
520
|
-
/**
|
|
521
|
-
* A DataTables table with row editing.
|
|
522
|
-
*
|
|
523
|
-
* @typeParam Row - Usually inferred from `data`.
|
|
524
|
-
*
|
|
525
|
-
* @example
|
|
526
|
-
* const table = new Tadabut(container, {
|
|
527
|
-
* data: [{ id: 1, name: 'Amina' }],
|
|
528
|
-
* rowId: 'id',
|
|
529
|
-
* columns: [
|
|
530
|
-
* { title: 'ID', prop: 'id' },
|
|
531
|
-
* { title: 'Name', prop: 'name' },
|
|
532
|
-
* ],
|
|
533
|
-
* });
|
|
534
|
-
*/
|
|
535
|
-
var Tadabut = class {
|
|
536
|
-
/** The DataTables instance. */
|
|
537
|
-
dataTable;
|
|
538
|
-
/** The `<table>` Tadabut created. */
|
|
539
|
-
tableEl;
|
|
540
|
-
/** The field that identifies a row. */
|
|
541
|
-
rowId;
|
|
542
|
-
/** The user's columns, without the actions column. */
|
|
543
|
-
columns;
|
|
544
|
-
/** `editing` with its defaults filled in. */
|
|
545
|
-
editing;
|
|
546
|
-
/** The user's classes, or `{}`. */
|
|
547
|
-
classNames;
|
|
548
|
-
/** The user's texts over English. */
|
|
549
|
-
texts;
|
|
550
|
-
/** The user's save callback. */
|
|
551
|
-
onSave;
|
|
552
|
-
/** Whether the Edit/Save/Cancel column exists. */
|
|
553
|
-
hasActions;
|
|
554
|
-
/** Whether any column has an editor. */
|
|
555
|
-
hasEditor;
|
|
556
|
-
/** Open rows, by row id. */
|
|
557
|
-
sessions = /* @__PURE__ */ new Map();
|
|
558
|
-
/**
|
|
559
|
-
* Builds the table inside `container`, which should be empty.
|
|
560
|
-
*
|
|
561
|
-
* @throws If a column has an `editor` and there's no `rowId`.
|
|
562
|
-
* @throws If a hidden column (`visible: false`) has an `editor`.
|
|
563
|
-
* @throws If a select's `labelProp` is its own column.
|
|
564
|
-
*/
|
|
565
|
-
constructor(container, options) {
|
|
566
|
-
this.hasEditor = options.columns.some((column) => column.editor !== void 0);
|
|
567
|
-
if (this.hasEditor && options.rowId === void 0) throw new Error("Tadabut: rowId is required when a column has an editor.");
|
|
568
|
-
const hiddenEditable = options.columns.find((column) => column.editor !== void 0 && column.visible === false);
|
|
569
|
-
if (hiddenEditable !== void 0) throw new Error(`Tadabut: the "${hiddenEditable.prop}" column can't have both an editor and visible: false.`);
|
|
570
|
-
const selfLabeled = options.columns.find((column) => column.editor?.type === "select" && column.editor.labelProp === column.prop);
|
|
571
|
-
if (selfLabeled !== void 0) throw new Error(`Tadabut: the "${selfLabeled.prop}" column's labelProp can't be "${selfLabeled.prop}" itself.`);
|
|
572
|
-
this.rowId = options.rowId;
|
|
573
|
-
this.columns = options.columns;
|
|
574
|
-
this.editing = {
|
|
575
|
-
mode: options.editing?.mode ?? "single",
|
|
576
|
-
button: options.editing?.button ?? true
|
|
577
|
-
};
|
|
578
|
-
this.hasActions = this.hasEditor && this.editing.button;
|
|
579
|
-
this.onSave = options.onSave;
|
|
580
|
-
this.classNames = options.classNames ?? {};
|
|
581
|
-
this.texts = {
|
|
582
|
-
...en,
|
|
583
|
-
...options.texts
|
|
584
|
-
};
|
|
585
|
-
this.tableEl = document.createElement("table");
|
|
586
|
-
addClasses(this.tableEl, [], this.classNames.table);
|
|
587
|
-
container.appendChild(this.tableEl);
|
|
588
|
-
this.dataTable = new DataTable(this.tableEl, {
|
|
589
|
-
...options.dataTables,
|
|
590
|
-
data: [...options.data],
|
|
591
|
-
columns: toDataTables(options.columns, this.hasActions, this.classNames, this.texts)
|
|
592
|
-
});
|
|
593
|
-
if (this.hasActions) this.tableEl.addEventListener("click", this.onClick);
|
|
594
|
-
if (this.hasEditor) this.tableEl.addEventListener("keydown", this.onKeyDown);
|
|
595
|
-
}
|
|
596
|
-
/**
|
|
597
|
-
* Replaces all rows. Sorting, search and the page are kept.
|
|
598
|
-
*
|
|
599
|
-
* Open rows are closed first, and their unsaved changes are lost.
|
|
600
|
-
*/
|
|
601
|
-
setData(rows) {
|
|
602
|
-
this.destroySessions();
|
|
603
|
-
this.dataTable.clear().rows.add([...rows]).draw(false);
|
|
604
|
-
}
|
|
605
|
-
/**
|
|
606
|
-
* Opens a row for editing.
|
|
607
|
-
*
|
|
608
|
-
* @param id The value of the row's `rowId` field.
|
|
609
|
-
* @returns `false` if the row doesn't exist, is already open, another row
|
|
610
|
-
* is open in `'single'` mode, nothing is editable, or the row is on a
|
|
611
|
-
* page that hasn't been displayed yet.
|
|
612
|
-
*/
|
|
613
|
-
edit(id) {
|
|
614
|
-
if (!this.hasEditor || this.sessions.has(id) || this.editing.mode === "single" && this.sessions.size > 0) return false;
|
|
615
|
-
const row = this.findRow(id);
|
|
616
|
-
if (row === void 0 || row.node() === null) return false;
|
|
617
|
-
const rowIndex = row.index();
|
|
618
|
-
const cells = [];
|
|
619
|
-
this.columns.forEach((column, columnIndex) => {
|
|
620
|
-
if (column.editor !== void 0) {
|
|
621
|
-
const cell = this.dataTable.cell(rowIndex, columnIndex).node();
|
|
622
|
-
cells.push({
|
|
623
|
-
column,
|
|
624
|
-
cell
|
|
625
|
-
});
|
|
626
|
-
}
|
|
627
|
-
});
|
|
628
|
-
this.sessions.set(id, new EditSession(row.data(), cells, this.texts));
|
|
629
|
-
row.node()?.classList.add("tdb-row-editing");
|
|
630
|
-
if (this.hasActions) this.dataTable.cell(rowIndex, this.columns.length).node().replaceChildren(actionButton("save", this.texts.save, splitClasses(this.classNames.button, this.classNames.save)), actionButton("cancel", this.texts.cancel, splitClasses(this.classNames.button, this.classNames.cancel)));
|
|
631
|
-
return true;
|
|
632
|
-
}
|
|
633
|
-
/**
|
|
634
|
-
* Closes a row and throws away its changes. Does nothing if the row isn't
|
|
635
|
-
* open, or while it's saving.
|
|
636
|
-
*/
|
|
637
|
-
cancel(id) {
|
|
638
|
-
const session = this.sessions.get(id);
|
|
639
|
-
if (session === void 0) return;
|
|
640
|
-
if (session.saving) return;
|
|
641
|
-
session.destroy();
|
|
642
|
-
this.sessions.delete(id);
|
|
643
|
-
this.clearSaveError(id);
|
|
644
|
-
this.findRow(id)?.node()?.classList.remove("tdb-row-editing");
|
|
645
|
-
this.findRow(id)?.invalidate("data").draw(false);
|
|
646
|
-
}
|
|
647
|
-
/** Whether a row is open for editing. */
|
|
648
|
-
isEditing(id) {
|
|
649
|
-
return this.sessions.has(id);
|
|
650
|
-
}
|
|
651
|
-
/**
|
|
652
|
-
* Validates the open row, then calls `onSave`.
|
|
653
|
-
*
|
|
654
|
-
* Invalid fields show their errors and `onSave` isn't called. While
|
|
655
|
-
* `onSave` runs the row is locked. If it throws, the row stays open and
|
|
656
|
-
* the error message is shown under it. The table only changes on success.
|
|
657
|
-
*
|
|
658
|
-
* @returns `true` once the row is saved and closed.
|
|
659
|
-
*/
|
|
660
|
-
async save(id) {
|
|
661
|
-
const session = this.sessions.get(id);
|
|
662
|
-
if (session === void 0 || session.saving) return false;
|
|
663
|
-
session.clearErrors();
|
|
664
|
-
this.clearSaveError(id);
|
|
665
|
-
const errors = session.validate();
|
|
666
|
-
const [firstInvalid] = errors.keys();
|
|
667
|
-
if (firstInvalid !== void 0) {
|
|
668
|
-
session.showErrors(errors);
|
|
669
|
-
session.focus(firstInvalid);
|
|
670
|
-
return false;
|
|
671
|
-
}
|
|
672
|
-
const row = session.getRow();
|
|
673
|
-
this.setSaving(id, true);
|
|
674
|
-
let saved = row;
|
|
675
|
-
try {
|
|
676
|
-
const result = await this.onSave?.(row, { previous: session.originalData });
|
|
677
|
-
if (result !== void 0) saved = result;
|
|
678
|
-
} catch (error) {
|
|
679
|
-
if (this.sessions.get(id) !== session) return false;
|
|
680
|
-
this.setSaving(id, false);
|
|
681
|
-
this.showSaveError(id, error instanceof Error ? error.message : this.texts.saveFailed);
|
|
682
|
-
return false;
|
|
683
|
-
}
|
|
684
|
-
if (this.sessions.get(id) !== session) return false;
|
|
685
|
-
session.destroy();
|
|
686
|
-
this.sessions.delete(id);
|
|
687
|
-
this.findRow(id)?.data({ ...saved }).draw(false);
|
|
688
|
-
this.findRow(id)?.node()?.classList.remove("tdb-row-editing");
|
|
689
|
-
this.setSaving(id, false);
|
|
690
|
-
return true;
|
|
691
|
-
}
|
|
692
|
-
/** Removes the table. Open rows are closed without saving. */
|
|
693
|
-
destroy() {
|
|
694
|
-
this.tableEl.removeEventListener("click", this.onClick);
|
|
695
|
-
this.tableEl.removeEventListener("keydown", this.onKeyDown);
|
|
696
|
-
this.destroySessions();
|
|
697
|
-
this.dataTable.destroy();
|
|
698
|
-
this.tableEl.remove();
|
|
699
|
-
}
|
|
700
|
-
/** The row with this id, or `undefined`. */
|
|
701
|
-
findRow(id) {
|
|
702
|
-
const rowId = this.rowId;
|
|
703
|
-
if (rowId === void 0) return;
|
|
704
|
-
const row = this.dataTable.row((_index, data) => data[rowId] === id);
|
|
705
|
-
return row.any() ? row : void 0;
|
|
706
|
-
}
|
|
707
|
-
/** Only for when the rows are about to go away: cells aren't restored. */
|
|
708
|
-
destroySessions() {
|
|
709
|
-
for (const session of this.sessions.values()) session.destroy();
|
|
710
|
-
this.sessions.clear();
|
|
711
|
-
}
|
|
712
|
-
/** Also works after the session is gone, so a row can always be unlocked. */
|
|
713
|
-
setSaving(id, saving) {
|
|
714
|
-
const session = this.sessions.get(id);
|
|
715
|
-
if (session !== void 0) session.saving = saving;
|
|
716
|
-
const tr = this.findRow(id)?.node();
|
|
717
|
-
if (tr) {
|
|
718
|
-
tr.inert = saving;
|
|
719
|
-
tr.classList.toggle("tdb-row-saving", saving);
|
|
720
|
-
}
|
|
721
|
-
}
|
|
722
|
-
/**
|
|
723
|
-
* Shows the error in a child row under the row. The message gets its own
|
|
724
|
-
* class because Bootstrap colors every cell, which hides a color on the
|
|
725
|
-
* `<tr>`. Set as text: it often comes from a server.
|
|
726
|
-
*/
|
|
727
|
-
showSaveError(id, message) {
|
|
728
|
-
const error = document.createElement("div");
|
|
729
|
-
error.className = "tdb-save-error-message";
|
|
730
|
-
error.textContent = message;
|
|
731
|
-
this.findRow(id)?.child(error, "tdb-save-error").show();
|
|
732
|
-
}
|
|
733
|
-
/** Removes the error row under a row, if any. */
|
|
734
|
-
clearSaveError(id) {
|
|
735
|
-
this.findRow(id)?.child.remove();
|
|
736
|
-
}
|
|
737
|
-
/**
|
|
738
|
-
* `undefined` for rows DataTables doesn't know, like the save error row or
|
|
739
|
-
* a table inside a custom editor.
|
|
740
|
-
*/
|
|
741
|
-
rowIdOf(element) {
|
|
742
|
-
const tr = element.closest("tr");
|
|
743
|
-
if (tr === null || this.rowId === void 0) return;
|
|
744
|
-
const row = this.dataTable.row(tr);
|
|
745
|
-
return row.any() ? row.data()[this.rowId] : void 0;
|
|
746
|
-
}
|
|
747
|
-
/**
|
|
748
|
-
* Handles Edit, Save and Cancel. One listener on the table, because
|
|
749
|
-
* DataTables keeps replacing rows. An arrow function, so `this` stays
|
|
750
|
-
* bound and `destroy` can remove it.
|
|
751
|
-
*/
|
|
752
|
-
onClick = (event) => {
|
|
753
|
-
if (!(event.target instanceof Element)) return;
|
|
754
|
-
const button = event.target.closest("[data-tdb-action]");
|
|
755
|
-
const id = button ? this.rowIdOf(button) : void 0;
|
|
756
|
-
if (!button || id === void 0) return;
|
|
757
|
-
switch (button.dataset.tdbAction) {
|
|
758
|
-
case "edit":
|
|
759
|
-
this.edit(id);
|
|
760
|
-
break;
|
|
761
|
-
case "save":
|
|
762
|
-
this.save(id);
|
|
763
|
-
break;
|
|
764
|
-
case "cancel": this.cancel(id);
|
|
765
|
-
}
|
|
766
|
-
};
|
|
767
|
-
/** Enter saves, Escape cancels. */
|
|
768
|
-
onKeyDown = (event) => {
|
|
769
|
-
if (event.defaultPrevented) return;
|
|
770
|
-
if (!(event.target instanceof Element) || event.target.closest("button")) return;
|
|
771
|
-
const id = this.rowIdOf(event.target);
|
|
772
|
-
if (id === void 0 || !this.sessions.has(id)) return;
|
|
773
|
-
switch (event.key) {
|
|
774
|
-
case "Enter":
|
|
775
|
-
event.preventDefault();
|
|
776
|
-
this.save(id);
|
|
777
|
-
break;
|
|
778
|
-
case "Escape":
|
|
779
|
-
event.preventDefault();
|
|
780
|
-
this.cancel(id);
|
|
781
|
-
}
|
|
782
|
-
};
|
|
783
|
-
};
|
|
784
|
-
/** Builds a Save or Cancel button. */
|
|
785
|
-
function actionButton(action, label, extra) {
|
|
786
|
-
const button = document.createElement("button");
|
|
787
|
-
button.type = "button";
|
|
788
|
-
button.classList.add("tdb-button", `tdb-button-${action}`, ...extra);
|
|
789
|
-
button.dataset.tdbAction = action;
|
|
790
|
-
button.textContent = label;
|
|
791
|
-
return button;
|
|
792
|
-
}
|
|
793
|
-
// src/texts/fr.ts
|
|
794
|
-
/** French. */
|
|
795
|
-
const fr = {
|
|
796
|
-
edit: "Modifier",
|
|
797
|
-
save: "Enregistrer",
|
|
798
|
-
cancel: "Annuler",
|
|
799
|
-
loading: "Chargement…",
|
|
800
|
-
saveFailed: "Impossible d'enregistrer.",
|
|
801
|
-
loadFailed: "Impossible de charger les options.",
|
|
802
|
-
required: "Ce champ est obligatoire.",
|
|
803
|
-
patternMismatch: "Format incorrect.",
|
|
804
|
-
tooShort: (min) => `Au moins ${min} caractères.`,
|
|
805
|
-
tooLong: (max) => `Au plus ${max} caractères.`,
|
|
806
|
-
tooSmall: (min) => `Doit être supérieur ou égal à ${min}.`,
|
|
807
|
-
tooLarge: (max) => `Doit être inférieur ou égal à ${max}.`,
|
|
808
|
-
tooEarly: (min) => `Doit être le ${min} ou après.`,
|
|
809
|
-
tooLate: (max) => `Doit être le ${max} ou avant.`
|
|
810
|
-
};
|
|
811
|
-
export { Tadabut as n, en as r, fr as t };
|