tadabut 0.5.0 → 0.6.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 +162 -29
- package/dist/{index-KmSXRJlI.d.ts → index-C_77p8Jv.d.ts} +166 -13
- package/dist/index.d.ts +8 -2
- package/dist/index.js +1 -1
- package/dist/react.d.ts +23 -8
- package/dist/react.js +55 -10
- package/dist/{src-CBK3xu9-.js → src-BxdYoViF.js} +418 -37
- package/package.json +5 -5
|
@@ -1,4 +1,55 @@
|
|
|
1
1
|
import DataTable from "datatables.net";
|
|
2
|
+
// src/editors/date-format.ts
|
|
3
|
+
/** ISO, what `<input type="date">` and most servers use. */
|
|
4
|
+
const defaultDateFormat = "yyyy-mm-dd";
|
|
5
|
+
/**
|
|
6
|
+
* Reads a date written in `format`, and gives it as `'yyyy-mm-dd'`. A time
|
|
7
|
+
* after it is dropped. `null` if it isn't a real date, like `31/02/2024`.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* parseDate('05/01/2024', 'dd/mm/yyyy'); // '2024-01-05'
|
|
11
|
+
*/
|
|
12
|
+
function parseDate(value, format) {
|
|
13
|
+
const { separator, order } = partsOf(format);
|
|
14
|
+
const [date = ""] = value.trim().split(/[T ]/);
|
|
15
|
+
const pieces = date.split(separator);
|
|
16
|
+
if (pieces.length !== 3) return null;
|
|
17
|
+
const yyyy = pieces[order.indexOf("yyyy")] ?? "";
|
|
18
|
+
const mm = pieces[order.indexOf("mm")] ?? "";
|
|
19
|
+
const dd = pieces[order.indexOf("dd")] ?? "";
|
|
20
|
+
if (!/^\d{4}$/.test(yyyy) || !/^\d{1,2}$/.test(mm) || !/^\d{1,2}$/.test(dd)) return null;
|
|
21
|
+
const year = Number(yyyy);
|
|
22
|
+
const month = Number(mm);
|
|
23
|
+
const day = Number(dd);
|
|
24
|
+
const check = new Date(Date.UTC(year, month - 1, day));
|
|
25
|
+
if (check.getUTCFullYear() !== year || check.getUTCMonth() !== month - 1 || check.getUTCDate() !== day) return null;
|
|
26
|
+
return `${yyyy}-${mm.padStart(2, "0")}-${dd.padStart(2, "0")}`;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Writes a `'yyyy-mm-dd'` date in `format`. Only give it dates `parseDate`
|
|
30
|
+
* returned: it checks nothing.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* formatDate('2024-01-05', 'dd.mm.yyyy'); // '05.01.2024'
|
|
34
|
+
*/
|
|
35
|
+
function formatDate(iso, format) {
|
|
36
|
+
const [yyyy = "", mm = "", dd = ""] = iso.split("-");
|
|
37
|
+
const { separator, order } = partsOf(format);
|
|
38
|
+
const pieces = {
|
|
39
|
+
yyyy,
|
|
40
|
+
mm,
|
|
41
|
+
dd
|
|
42
|
+
};
|
|
43
|
+
return order.map((part) => pieces[part]).join(separator);
|
|
44
|
+
}
|
|
45
|
+
/** The separator and the order of the parts. */
|
|
46
|
+
function partsOf(format) {
|
|
47
|
+
const separator = format.match(/[^a-z]/)?.[0] ?? "-";
|
|
48
|
+
return {
|
|
49
|
+
separator,
|
|
50
|
+
order: format.split(separator)
|
|
51
|
+
};
|
|
52
|
+
}
|
|
2
53
|
// src/editors/money-format.ts
|
|
3
54
|
/** `12 500.00`. */
|
|
4
55
|
const defaultMoneyFormat = {
|
|
@@ -219,6 +270,18 @@ function rowErrorMessage(message) {
|
|
|
219
270
|
error.textContent = message;
|
|
220
271
|
return error;
|
|
221
272
|
}
|
|
273
|
+
/** The header's "check all" checkbox, as HTML for the column title. */
|
|
274
|
+
function selectAllHtml(texts, classNames) {
|
|
275
|
+
return checkboxHtml("tdb-select-all", texts.selectAll, classNames);
|
|
276
|
+
}
|
|
277
|
+
/** A row's checkbox, as HTML for `defaultContent`. */
|
|
278
|
+
function selectRowHtml(texts, classNames) {
|
|
279
|
+
return checkboxHtml("tdb-select-row", texts.selectRow, classNames);
|
|
280
|
+
}
|
|
281
|
+
/** A selection checkbox as HTML, so the classes and the label are escaped. */
|
|
282
|
+
function checkboxHtml(kind, label, classNames) {
|
|
283
|
+
return `<input type="checkbox" class="${escapeHtml([kind, ...splitClasses(classNames.select)].join(" "))}" aria-label="${escapeHtml(label)}">`;
|
|
284
|
+
}
|
|
222
285
|
// src/columns/to-datatables.ts
|
|
223
286
|
/**
|
|
224
287
|
* The column with the Edit and Delete buttons. While a row is open, Save and
|
|
@@ -235,12 +298,28 @@ function actionsColumn(actions, classNames, texts) {
|
|
|
235
298
|
};
|
|
236
299
|
}
|
|
237
300
|
/**
|
|
238
|
-
* The
|
|
239
|
-
*
|
|
301
|
+
* The checkbox column. The checkboxes are the same HTML on every row; which
|
|
302
|
+
* ones are ticked is set after each draw.
|
|
303
|
+
*/
|
|
304
|
+
function selectColumn(classNames, texts) {
|
|
305
|
+
return {
|
|
306
|
+
title: selectAllHtml(texts, classNames),
|
|
307
|
+
data: null,
|
|
308
|
+
className: "tdb-select",
|
|
309
|
+
orderable: false,
|
|
310
|
+
searchable: false,
|
|
311
|
+
defaultContent: selectRowHtml(texts, classNames)
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
/**
|
|
315
|
+
* The checkbox column goes first with `selection`, and the actions column
|
|
316
|
+
* last. Tadabut shifts your column indexes to match. No `actions` means no
|
|
317
|
+
* actions column.
|
|
240
318
|
*/
|
|
241
|
-
function toDataTables(columns, actions, classNames, texts) {
|
|
319
|
+
function toDataTables(columns, actions, classNames, texts, selection) {
|
|
242
320
|
const converted = columns.map((column) => {
|
|
243
321
|
const label = labelOf(column);
|
|
322
|
+
const sortValue = sortValueOf(column);
|
|
244
323
|
return {
|
|
245
324
|
...defined({
|
|
246
325
|
width: column.width,
|
|
@@ -256,27 +335,48 @@ function toDataTables(columns, actions, classNames, texts) {
|
|
|
256
335
|
switch (type) {
|
|
257
336
|
case "display": return escapeHtml(label(value, row));
|
|
258
337
|
case "filter": return `${label(value, row)} ${asText(value)}`;
|
|
259
|
-
default: return value;
|
|
338
|
+
default: return sortValue(value);
|
|
260
339
|
}
|
|
261
340
|
}
|
|
262
341
|
};
|
|
263
342
|
});
|
|
264
|
-
|
|
343
|
+
const first = selection ? [selectColumn(classNames, texts)] : [];
|
|
344
|
+
const last = actions.length > 0 ? [actionsColumn(actions, classNames, texts)] : [];
|
|
345
|
+
return [
|
|
346
|
+
...first,
|
|
347
|
+
...converted,
|
|
348
|
+
...last
|
|
349
|
+
];
|
|
265
350
|
}
|
|
266
|
-
/**
|
|
351
|
+
/**
|
|
352
|
+
* `render`, then a money or date format, then `labelProp`, then a fixed
|
|
353
|
+
* list's label, then the value. A date that isn't valid shows as it is.
|
|
354
|
+
*/
|
|
267
355
|
function labelOf(column) {
|
|
268
356
|
const { render, editor } = column;
|
|
269
357
|
if (render !== void 0) return render;
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
358
|
+
switch (editor?.type) {
|
|
359
|
+
case "money": {
|
|
360
|
+
const format = moneyFormat(editor);
|
|
361
|
+
return (value) => typeof value === "number" ? formatMoney(value, format) : asText(value);
|
|
362
|
+
}
|
|
363
|
+
case "date": {
|
|
364
|
+
const format = editor.format ?? "yyyy-mm-dd";
|
|
365
|
+
const display = editor.display ?? format;
|
|
366
|
+
return (value) => {
|
|
367
|
+
if (typeof value !== "string") return asText(value);
|
|
368
|
+
const iso = parseDate(value, format);
|
|
369
|
+
if (iso === null) return asText(value);
|
|
370
|
+
return formatDate(iso, display);
|
|
371
|
+
};
|
|
372
|
+
}
|
|
373
|
+
case "select": {
|
|
374
|
+
const labelProp = editor.labelProp;
|
|
375
|
+
if (labelProp !== void 0) return (_value, row) => asText(row[labelProp]);
|
|
376
|
+
if (typeof editor.options !== "function") {
|
|
377
|
+
const options = editor.options;
|
|
378
|
+
return (value) => options.find((option) => option.value === value)?.label ?? asText(value);
|
|
379
|
+
}
|
|
280
380
|
}
|
|
281
381
|
}
|
|
282
382
|
return asText;
|
|
@@ -290,6 +390,19 @@ function defined(values) {
|
|
|
290
390
|
const kept = Object.entries(values).filter(([, value]) => value !== void 0);
|
|
291
391
|
return Object.fromEntries(kept);
|
|
292
392
|
}
|
|
393
|
+
/**
|
|
394
|
+
* What DataTables sorts by. Dates become `'yyyy-mm-dd'`, so `'05/01/2024'`
|
|
395
|
+
* sorts as January; invalid and empty dates give `''`, which sorts first.
|
|
396
|
+
* Anything else stays as it is: numbers stay numbers.
|
|
397
|
+
*/
|
|
398
|
+
function sortValueOf(column) {
|
|
399
|
+
const editor = column.editor;
|
|
400
|
+
if (editor?.type === "date") {
|
|
401
|
+
const format = editor.format ?? "yyyy-mm-dd";
|
|
402
|
+
return (value) => typeof value === "string" ? parseDate(value, format) ?? "" : "";
|
|
403
|
+
}
|
|
404
|
+
return (value) => value;
|
|
405
|
+
}
|
|
293
406
|
// src/texts/en.ts
|
|
294
407
|
/** English, the default. */
|
|
295
408
|
const en = {
|
|
@@ -308,6 +421,8 @@ const en = {
|
|
|
308
421
|
loadFailed: "Could not load the options.",
|
|
309
422
|
required: "This field is required.",
|
|
310
423
|
patternMismatch: "Wrong format.",
|
|
424
|
+
selectRow: "Select row",
|
|
425
|
+
selectAll: "Select all",
|
|
311
426
|
tooShort: (min) => `Must be at least ${min} characters.`,
|
|
312
427
|
tooLong: (max) => `Must be at most ${max} characters.`,
|
|
313
428
|
tooSmall: (min) => `Must be ${min} or more.`,
|
|
@@ -348,18 +463,22 @@ function createCheckboxEditor(config, context) {
|
|
|
348
463
|
};
|
|
349
464
|
}
|
|
350
465
|
// src/editors/date.ts
|
|
351
|
-
/**
|
|
466
|
+
/**
|
|
467
|
+
* Builds the date input. The input itself always works in `'yyyy-mm-dd'`,
|
|
468
|
+
* whatever the computer's language, so values are converted going in and out.
|
|
469
|
+
*/
|
|
352
470
|
function createDateEditor(config, context) {
|
|
471
|
+
const format = config.format ?? "yyyy-mm-dd";
|
|
353
472
|
const input = document.createElement("input");
|
|
354
473
|
input.type = "date";
|
|
355
474
|
addClasses(input, ["tdb-input", "tdb-input-date"], context.className);
|
|
356
|
-
if (config.min !== void 0) input.min = config.min;
|
|
357
|
-
if (config.max !== void 0) input.max = config.max;
|
|
358
|
-
/** Empty gives `null`. */
|
|
359
|
-
const getValue = () => input.value === "" ? null : input.value;
|
|
360
|
-
/** `null` shows as empty. */
|
|
475
|
+
if (config.min !== void 0) input.min = parseDate(config.min, format) ?? "";
|
|
476
|
+
if (config.max !== void 0) input.max = parseDate(config.max, format) ?? "";
|
|
477
|
+
/** Empty gives `null`. Otherwise the date in `format`. */
|
|
478
|
+
const getValue = () => input.value === "" ? null : formatDate(input.value, format);
|
|
479
|
+
/** `null`, or a value that isn't a date in `format`, shows as empty. */
|
|
361
480
|
const setValue = (value) => {
|
|
362
|
-
input.value = value ?? "";
|
|
481
|
+
input.value = value === null ? "" : parseDate(value, format) ?? "";
|
|
363
482
|
};
|
|
364
483
|
setValue(context.value ?? null);
|
|
365
484
|
input.addEventListener("input", () => context.onChange(getValue()));
|
|
@@ -739,14 +858,17 @@ var EditSession = class {
|
|
|
739
858
|
*
|
|
740
859
|
* @throws If a column has an `editor` and there's no `rowId`.
|
|
741
860
|
* @throws If `onDelete` is set and there's no `rowId`.
|
|
861
|
+
* @throws If `selection` is on and there's no `rowId`.
|
|
742
862
|
* @throws If a hidden column (`visible: false`) has an `editor`.
|
|
743
863
|
* @throws If a select's `labelProp` is its own column.
|
|
744
864
|
* @throws If a money editor's `separator` and `point` are the same.
|
|
865
|
+
* @throws If a date editor's `min` or `max` isn't a real date in its `format`.
|
|
745
866
|
*/
|
|
746
867
|
function checkOptions(options) {
|
|
747
868
|
if (options.rowId === void 0) {
|
|
748
869
|
if (options.columns.some((column) => column.editor !== void 0)) throw new Error("Tadabut: rowId is required when a column has an editor.");
|
|
749
870
|
if (options.onDelete !== void 0) throw new Error("Tadabut: rowId is required with onDelete.");
|
|
871
|
+
if (options.selection) throw new Error("Tadabut: rowId is required with selection.");
|
|
750
872
|
}
|
|
751
873
|
const hiddenEditable = options.columns.find((column) => column.editor !== void 0 && column.visible === false);
|
|
752
874
|
if (hiddenEditable !== void 0) throw new Error(`Tadabut: the "${hiddenEditable.prop}" column can't have both an editor and visible: false.`);
|
|
@@ -757,6 +879,11 @@ function checkOptions(options) {
|
|
|
757
879
|
const { separator, point } = moneyFormat(column.editor);
|
|
758
880
|
if (separator === point) throw new Error(`Tadabut: the "${column.prop}" column's money separator and point can't both be "${point}".`);
|
|
759
881
|
}
|
|
882
|
+
for (const column of options.columns) {
|
|
883
|
+
if (column.editor?.type !== "date") continue;
|
|
884
|
+
const format = column.editor.format ?? "yyyy-mm-dd";
|
|
885
|
+
for (const limit of [column.editor.min, column.editor.max]) if (limit !== void 0 && parseDate(limit, format) === null) throw new Error(`Tadabut: the "${column.prop}" column's date "${limit}" isn't a real date written as ${format}.`);
|
|
886
|
+
}
|
|
760
887
|
}
|
|
761
888
|
// src/table/new-row.ts
|
|
762
889
|
/**
|
|
@@ -779,6 +906,7 @@ var NewRow = class {
|
|
|
779
906
|
this.body.classList.add("tdb-new-rows");
|
|
780
907
|
this.tr = document.createElement("tr");
|
|
781
908
|
this.tr.classList.add("tdb-row-new", "tdb-row-editing");
|
|
909
|
+
for (let index = 0; index < options.leadingCells; index++) this.tr.append(document.createElement("td"));
|
|
782
910
|
const cells = [];
|
|
783
911
|
options.columns.forEach((column, index) => {
|
|
784
912
|
if (!options.isVisible(index)) return;
|
|
@@ -878,9 +1006,9 @@ function footerFor(cellCount) {
|
|
|
878
1006
|
tfoot.append(tr);
|
|
879
1007
|
return tfoot;
|
|
880
1008
|
}
|
|
881
|
-
/** The DataTables filter for each `
|
|
882
|
-
function rowsFor(
|
|
883
|
-
switch (
|
|
1009
|
+
/** The DataTables filter for each `RowScope`. */
|
|
1010
|
+
function rowsFor(scope) {
|
|
1011
|
+
switch (scope) {
|
|
884
1012
|
case "searched": return { search: "applied" };
|
|
885
1013
|
case "all": return { search: "none" };
|
|
886
1014
|
case "page": return { page: "current" };
|
|
@@ -909,6 +1037,132 @@ function compute(total, values) {
|
|
|
909
1037
|
case "max": return Math.max(...values);
|
|
910
1038
|
}
|
|
911
1039
|
}
|
|
1040
|
+
// src/table/selection.ts
|
|
1041
|
+
/**
|
|
1042
|
+
* Which rows are ticked. The ids are the truth; the checkboxes on the page
|
|
1043
|
+
* are redrawn from them after every draw, since DataTables rebuilds rows.
|
|
1044
|
+
*/
|
|
1045
|
+
var Selection = class {
|
|
1046
|
+
/** The selected row ids. */
|
|
1047
|
+
ids = /* @__PURE__ */ new Set();
|
|
1048
|
+
/** What the selection needs from the table. */
|
|
1049
|
+
parts;
|
|
1050
|
+
/** Starts listening to the checkboxes. Nothing is ticked yet. */
|
|
1051
|
+
constructor(parts) {
|
|
1052
|
+
this.parts = parts;
|
|
1053
|
+
this.parts.dataTable.table().node().addEventListener("change", this.onChange);
|
|
1054
|
+
}
|
|
1055
|
+
/**
|
|
1056
|
+
* Ticks the checkboxes on the page to match the selection, and sets the
|
|
1057
|
+
* header's checkbox to all, none or some.
|
|
1058
|
+
*/
|
|
1059
|
+
sync() {
|
|
1060
|
+
const rows = this.parts.dataTable.rows({ page: "current" });
|
|
1061
|
+
const data = rows.data().toArray();
|
|
1062
|
+
rows.nodes().toArray().forEach((node, index) => {
|
|
1063
|
+
const row = data[index];
|
|
1064
|
+
if (row === void 0) return;
|
|
1065
|
+
const selected = this.ids.has(this.idFor(row));
|
|
1066
|
+
node.classList.toggle("tdb-row-selected", selected);
|
|
1067
|
+
const checkbox = node.querySelector(".tdb-select-row");
|
|
1068
|
+
if (checkbox !== null) checkbox.checked = selected;
|
|
1069
|
+
});
|
|
1070
|
+
const all = this.parts.dataTable.table().header().querySelector(".tdb-select-all");
|
|
1071
|
+
if (all === null) return;
|
|
1072
|
+
const scope = this.scopeIds();
|
|
1073
|
+
const count = scope.filter((id) => this.ids.has(id)).length;
|
|
1074
|
+
all.checked = count > 0 && count === scope.length;
|
|
1075
|
+
all.indeterminate = count > 0 && count < scope.length;
|
|
1076
|
+
}
|
|
1077
|
+
/**
|
|
1078
|
+
* The selected rows, in the order they were added to the table. Rows the
|
|
1079
|
+
* search hides, or on other pages, are in it too.
|
|
1080
|
+
*/
|
|
1081
|
+
selected() {
|
|
1082
|
+
return this.parts.dataTable.rows().data().toArray().filter((row) => this.ids.has(this.idFor(row)));
|
|
1083
|
+
}
|
|
1084
|
+
/** Whether the row with this id is selected. */
|
|
1085
|
+
has(id) {
|
|
1086
|
+
return this.ids.has(id);
|
|
1087
|
+
}
|
|
1088
|
+
/**
|
|
1089
|
+
* Selects or unselects rows. Ids of rows that aren't in the table are
|
|
1090
|
+
* skipped. `onSelect` is only called if something changed.
|
|
1091
|
+
*/
|
|
1092
|
+
set(ids, selected) {
|
|
1093
|
+
const before = this.ids.size;
|
|
1094
|
+
if (selected) {
|
|
1095
|
+
const existing = this.allIds();
|
|
1096
|
+
for (const id of ids) if (existing.has(id)) this.ids.add(id);
|
|
1097
|
+
} else for (const id of ids) this.ids.delete(id);
|
|
1098
|
+
if (this.ids.size !== before) {
|
|
1099
|
+
this.sync();
|
|
1100
|
+
this.changed();
|
|
1101
|
+
}
|
|
1102
|
+
}
|
|
1103
|
+
/** Unselects every row. */
|
|
1104
|
+
clear() {
|
|
1105
|
+
if (this.ids.size === 0) return;
|
|
1106
|
+
this.ids.clear();
|
|
1107
|
+
this.sync();
|
|
1108
|
+
this.changed();
|
|
1109
|
+
}
|
|
1110
|
+
/** Forgets the ids of rows that left the table, after `setData` or a delete. */
|
|
1111
|
+
prune() {
|
|
1112
|
+
const existing = this.allIds();
|
|
1113
|
+
const before = this.ids.size;
|
|
1114
|
+
for (const id of this.ids) if (!existing.has(id)) this.ids.delete(id);
|
|
1115
|
+
if (this.ids.size !== before) {
|
|
1116
|
+
this.sync();
|
|
1117
|
+
this.changed();
|
|
1118
|
+
}
|
|
1119
|
+
}
|
|
1120
|
+
/** Stops listening to the checkboxes. */
|
|
1121
|
+
destroy() {
|
|
1122
|
+
this.parts.dataTable.table().node().removeEventListener("change", this.onChange);
|
|
1123
|
+
}
|
|
1124
|
+
/** Tells the user the selection changed. */
|
|
1125
|
+
changed() {
|
|
1126
|
+
this.parts.onSelect?.(this.selected());
|
|
1127
|
+
}
|
|
1128
|
+
/** The id of a row. */
|
|
1129
|
+
idFor(row) {
|
|
1130
|
+
return row[this.parts.rowId];
|
|
1131
|
+
}
|
|
1132
|
+
/** The ids of every row in the table. */
|
|
1133
|
+
allIds() {
|
|
1134
|
+
return new Set(this.parts.dataTable.rows().data().toArray().map((row) => this.idFor(row)));
|
|
1135
|
+
}
|
|
1136
|
+
/** The id of the row an element is in, or `undefined` outside DataTables' rows. */
|
|
1137
|
+
idOf(element) {
|
|
1138
|
+
const tr = element.closest("tr");
|
|
1139
|
+
if (tr === null) return;
|
|
1140
|
+
const row = this.parts.dataTable.row(tr);
|
|
1141
|
+
return row.any() ? this.idFor(row.data()) : void 0;
|
|
1142
|
+
}
|
|
1143
|
+
/** The ids of the rows in `scope`. */
|
|
1144
|
+
scopeIds() {
|
|
1145
|
+
return this.parts.dataTable.rows(rowsFor(this.parts.scope)).data().toArray().map((row) => this.idFor(row));
|
|
1146
|
+
}
|
|
1147
|
+
/**
|
|
1148
|
+
* A row's checkbox or the header's was clicked. An arrow function, so
|
|
1149
|
+
* `destroy` can remove it.
|
|
1150
|
+
*/
|
|
1151
|
+
onChange = (event) => {
|
|
1152
|
+
const target = event.target;
|
|
1153
|
+
if (!(target instanceof HTMLInputElement)) return;
|
|
1154
|
+
if (target.classList.contains("tdb-select-row")) {
|
|
1155
|
+
const id = this.idOf(target);
|
|
1156
|
+
if (id === void 0) return;
|
|
1157
|
+
if (target.checked) this.ids.add(id);
|
|
1158
|
+
else this.ids.delete(id);
|
|
1159
|
+
} else if (target.classList.contains("tdb-select-all")) for (const id of this.scopeIds()) if (target.checked) this.ids.add(id);
|
|
1160
|
+
else this.ids.delete(id);
|
|
1161
|
+
else return;
|
|
1162
|
+
this.sync();
|
|
1163
|
+
this.changed();
|
|
1164
|
+
};
|
|
1165
|
+
};
|
|
912
1166
|
// src/table/tadabut.ts
|
|
913
1167
|
/**
|
|
914
1168
|
* A DataTables table with row editing.
|
|
@@ -962,14 +1216,32 @@ var Tadabut = class {
|
|
|
962
1216
|
hasTotals;
|
|
963
1217
|
/** Which rows the totals count, with its default filled in. */
|
|
964
1218
|
totalRows;
|
|
1219
|
+
/** How many columns Tadabut adds before yours: 1 with `selection`. */
|
|
1220
|
+
offset;
|
|
1221
|
+
/** The ticked rows. Only with `selection`. */
|
|
1222
|
+
selection;
|
|
1223
|
+
/** Asked before a row opens. */
|
|
1224
|
+
onEditStart;
|
|
1225
|
+
/** The user's after-save callback. */
|
|
1226
|
+
onSaved;
|
|
1227
|
+
/** The user's after-add callback. */
|
|
1228
|
+
onAdded;
|
|
1229
|
+
/** The user's after-delete callback. */
|
|
1230
|
+
onDeleted;
|
|
1231
|
+
/** The user's row click callback. */
|
|
1232
|
+
onRowClick;
|
|
1233
|
+
/** The user's row double click callback. */
|
|
1234
|
+
onRowDoubleClick;
|
|
965
1235
|
/**
|
|
966
1236
|
* Builds the table inside `container`, which should be empty.
|
|
967
1237
|
*
|
|
968
1238
|
* @throws If a column has an `editor` and there's no `rowId`.
|
|
969
1239
|
* @throws If `onDelete` is set and there's no `rowId`.
|
|
1240
|
+
* @throws If `selection` is on and there's no `rowId`.
|
|
970
1241
|
* @throws If a hidden column (`visible: false`) has an `editor`.
|
|
971
1242
|
* @throws If a select's `labelProp` is its own column.
|
|
972
1243
|
* @throws If a money editor's `separator` and `point` are the same.
|
|
1244
|
+
* @throws If a date editor's `min` or `max` isn't a real date in its `format`.
|
|
973
1245
|
*/
|
|
974
1246
|
constructor(container, options) {
|
|
975
1247
|
checkOptions(options);
|
|
@@ -978,7 +1250,8 @@ var Tadabut = class {
|
|
|
978
1250
|
this.columns = options.columns;
|
|
979
1251
|
this.editing = {
|
|
980
1252
|
mode: options.editing?.mode ?? "single",
|
|
981
|
-
button: options.editing?.button ?? true
|
|
1253
|
+
button: options.editing?.button ?? true,
|
|
1254
|
+
doubleClick: options.editing?.doubleClick ?? false
|
|
982
1255
|
};
|
|
983
1256
|
const actions = [];
|
|
984
1257
|
if (this.hasEditor && this.editing.button) actions.push("edit");
|
|
@@ -987,6 +1260,12 @@ var Tadabut = class {
|
|
|
987
1260
|
this.onSave = options.onSave;
|
|
988
1261
|
this.onAdd = options.onAdd;
|
|
989
1262
|
this.onDelete = options.onDelete;
|
|
1263
|
+
this.onEditStart = options.onEditStart;
|
|
1264
|
+
this.onSaved = options.onSaved;
|
|
1265
|
+
this.onAdded = options.onAdded;
|
|
1266
|
+
this.onDeleted = options.onDeleted;
|
|
1267
|
+
this.onRowClick = options.onRowClick;
|
|
1268
|
+
this.onRowDoubleClick = options.onRowDoubleClick;
|
|
990
1269
|
this.confirmDelete = options.confirmDelete;
|
|
991
1270
|
this.classNames = options.classNames ?? {};
|
|
992
1271
|
this.texts = {
|
|
@@ -995,21 +1274,35 @@ var Tadabut = class {
|
|
|
995
1274
|
};
|
|
996
1275
|
this.hasTotals = options.columns.some((column) => column.total !== void 0);
|
|
997
1276
|
this.totalRows = options.totalRows ?? "searched";
|
|
1277
|
+
this.offset = options.selection ? 1 : 0;
|
|
998
1278
|
this.tableEl = document.createElement("table");
|
|
999
1279
|
addClasses(this.tableEl, [], this.classNames.table);
|
|
1000
1280
|
container.appendChild(this.tableEl);
|
|
1001
|
-
if (this.hasTotals) this.tableEl.append(footerFor(this.columns.length + (this.hasActions ? 1 : 0)));
|
|
1281
|
+
if (this.hasTotals) this.tableEl.append(footerFor(this.offset + this.columns.length + (this.hasActions ? 1 : 0)));
|
|
1002
1282
|
this.dataTable = new DataTable(this.tableEl, {
|
|
1003
1283
|
...options.dataTables,
|
|
1284
|
+
...this.offset > 0 && { order: shiftOrder(options.dataTables?.order, this.offset) },
|
|
1004
1285
|
data: [...options.data],
|
|
1005
|
-
columns: toDataTables(options.columns, actions, this.classNames, this.texts)
|
|
1286
|
+
columns: toDataTables(options.columns, actions, this.classNames, this.texts, this.offset > 0)
|
|
1006
1287
|
});
|
|
1288
|
+
if (this.offset > 0) {
|
|
1289
|
+
this.selection = new Selection({
|
|
1290
|
+
dataTable: this.dataTable,
|
|
1291
|
+
rowId: this.rowId,
|
|
1292
|
+
scope: typeof options.selection === "object" ? options.selection.checkAll ?? "searched" : "searched",
|
|
1293
|
+
onSelect: options.onSelect
|
|
1294
|
+
});
|
|
1295
|
+
this.dataTable.on("draw", () => this.selection?.sync());
|
|
1296
|
+
this.selection.sync();
|
|
1297
|
+
}
|
|
1007
1298
|
if (this.hasTotals) {
|
|
1008
1299
|
this.dataTable.on("draw", this.updateTotals);
|
|
1009
1300
|
this.updateTotals();
|
|
1010
1301
|
}
|
|
1011
1302
|
if (this.hasActions) this.tableEl.addEventListener("click", this.onClick);
|
|
1012
1303
|
if (this.hasEditor) this.tableEl.addEventListener("keydown", this.onKeyDown);
|
|
1304
|
+
this.tableEl.addEventListener("click", this.onRowEvent);
|
|
1305
|
+
this.tableEl.addEventListener("dblclick", this.onRowEvent);
|
|
1013
1306
|
}
|
|
1014
1307
|
/**
|
|
1015
1308
|
* Replaces all rows. Sorting, search and the page are kept.
|
|
@@ -1021,6 +1314,7 @@ var Tadabut = class {
|
|
|
1021
1314
|
this.destroySessions();
|
|
1022
1315
|
this.deleting.clear();
|
|
1023
1316
|
this.dataTable.clear().rows.add([...rows]).draw(false);
|
|
1317
|
+
this.selection?.prune();
|
|
1024
1318
|
}
|
|
1025
1319
|
/**
|
|
1026
1320
|
* Opens a row for editing.
|
|
@@ -1028,17 +1322,19 @@ var Tadabut = class {
|
|
|
1028
1322
|
* @param id The value of the row's `rowId` field.
|
|
1029
1323
|
* @returns `false` if the row doesn't exist, is already open, another row
|
|
1030
1324
|
* or the new row is open in `'single'` mode, nothing is editable, the row
|
|
1031
|
-
* is being deleted,
|
|
1325
|
+
* is being deleted, it's on a page that hasn't been displayed yet, or
|
|
1326
|
+
* `onEditStart` returned `false`.
|
|
1032
1327
|
*/
|
|
1033
1328
|
edit(id) {
|
|
1034
1329
|
if (!this.hasEditor || this.sessions.has(id) || this.editing.mode === "single" && (this.sessions.size > 0 || this.newRow !== void 0) || this.deleting.has(id)) return false;
|
|
1035
1330
|
const row = this.findRow(id);
|
|
1036
1331
|
if (row === void 0 || row.node() === null) return false;
|
|
1332
|
+
if (this.onEditStart?.(row.data()) === false) return false;
|
|
1037
1333
|
const rowIndex = row.index();
|
|
1038
1334
|
const cells = [];
|
|
1039
1335
|
this.columns.forEach((column, columnIndex) => {
|
|
1040
1336
|
if (column.editor !== void 0) {
|
|
1041
|
-
const cell = this.dataTable.cell(rowIndex, columnIndex).node();
|
|
1337
|
+
const cell = this.dataTable.cell(rowIndex, this.dtIndex(columnIndex)).node();
|
|
1042
1338
|
cells.push({
|
|
1043
1339
|
column,
|
|
1044
1340
|
cell
|
|
@@ -1064,10 +1360,11 @@ var Tadabut = class {
|
|
|
1064
1360
|
if (!this.hasEditor || this.newRow !== void 0 || this.editing.mode === "single" && this.sessions.size > 0) return false;
|
|
1065
1361
|
const newRow = new NewRow(defaults, {
|
|
1066
1362
|
columns: this.columns,
|
|
1067
|
-
isVisible: (index) => this.dataTable.column(index).visible(),
|
|
1363
|
+
isVisible: (index) => this.dataTable.column(this.dtIndex(index)).visible(),
|
|
1068
1364
|
withButtons: this.hasActions,
|
|
1069
1365
|
texts: this.texts,
|
|
1070
|
-
classNames: this.classNames
|
|
1366
|
+
classNames: this.classNames,
|
|
1367
|
+
leadingCells: this.offset
|
|
1071
1368
|
});
|
|
1072
1369
|
this.tableEl.insertBefore(newRow.body, this.dataTable.table().body());
|
|
1073
1370
|
newRow.focus();
|
|
@@ -1079,6 +1376,35 @@ var Tadabut = class {
|
|
|
1079
1376
|
return this.newRow !== void 0;
|
|
1080
1377
|
}
|
|
1081
1378
|
/**
|
|
1379
|
+
* The selected rows, in the order they were added. Rows the search hides,
|
|
1380
|
+
* or on other pages, are in it too. Always `[]` without `selection`.
|
|
1381
|
+
*/
|
|
1382
|
+
getSelected() {
|
|
1383
|
+
return this.selection?.selected() ?? [];
|
|
1384
|
+
}
|
|
1385
|
+
/** Whether a row is selected. Always `false` without `selection`. */
|
|
1386
|
+
isSelected(id) {
|
|
1387
|
+
return this.selection?.has(id) ?? false;
|
|
1388
|
+
}
|
|
1389
|
+
/**
|
|
1390
|
+
* Selects rows by id. Ids not in the table are skipped. Does nothing
|
|
1391
|
+
* without `selection`.
|
|
1392
|
+
*
|
|
1393
|
+
* @example
|
|
1394
|
+
* table.select([1, 2]);
|
|
1395
|
+
*/
|
|
1396
|
+
select(ids) {
|
|
1397
|
+
this.selection?.set(ids, true);
|
|
1398
|
+
}
|
|
1399
|
+
/** Unselects rows by id. Does nothing without `selection`. */
|
|
1400
|
+
deselect(ids) {
|
|
1401
|
+
this.selection?.set(ids, false);
|
|
1402
|
+
}
|
|
1403
|
+
/** Unselects every row. Does nothing without `selection`. */
|
|
1404
|
+
clearSelection() {
|
|
1405
|
+
this.selection?.clear();
|
|
1406
|
+
}
|
|
1407
|
+
/**
|
|
1082
1408
|
* Closes the new row and throws it away. Does nothing if there's none, or
|
|
1083
1409
|
* while it's saving.
|
|
1084
1410
|
*/
|
|
@@ -1131,6 +1457,7 @@ var Tadabut = class {
|
|
|
1131
1457
|
}).indexes().toArray().indexOf(added.index());
|
|
1132
1458
|
const length = this.dataTable.page.len();
|
|
1133
1459
|
if (position >= 0 && length > 0) this.dataTable.page(Math.floor(position / length)).draw(false);
|
|
1460
|
+
this.onAdded?.(added.data());
|
|
1134
1461
|
return true;
|
|
1135
1462
|
}
|
|
1136
1463
|
/**
|
|
@@ -1190,6 +1517,7 @@ var Tadabut = class {
|
|
|
1190
1517
|
this.findRow(id)?.data({ ...saved }).draw(false);
|
|
1191
1518
|
this.findRow(id)?.node()?.classList.remove("tdb-row-editing");
|
|
1192
1519
|
this.setSaving(id, false);
|
|
1520
|
+
this.onSaved?.(saved, { previous: session.originalData });
|
|
1193
1521
|
return true;
|
|
1194
1522
|
}
|
|
1195
1523
|
/**
|
|
@@ -1206,11 +1534,12 @@ var Tadabut = class {
|
|
|
1206
1534
|
async delete(id) {
|
|
1207
1535
|
const row = this.findRow(id);
|
|
1208
1536
|
if (row === void 0 || this.sessions.has(id) || this.deleting.has(id)) return false;
|
|
1537
|
+
const data = row.data();
|
|
1209
1538
|
this.clearRowError(id);
|
|
1210
1539
|
this.deleting.add(id);
|
|
1211
1540
|
this.setSaving(id, true);
|
|
1212
1541
|
try {
|
|
1213
|
-
await this.onDelete?.(
|
|
1542
|
+
await this.onDelete?.(data);
|
|
1214
1543
|
} catch (error) {
|
|
1215
1544
|
if (!this.deleting.has(id)) return false;
|
|
1216
1545
|
this.deleting.delete(id);
|
|
@@ -1222,14 +1551,19 @@ var Tadabut = class {
|
|
|
1222
1551
|
if (!this.deleting.has(id)) return false;
|
|
1223
1552
|
this.deleting.delete(id);
|
|
1224
1553
|
this.findRow(id)?.remove().draw(false);
|
|
1554
|
+
this.selection?.prune();
|
|
1555
|
+
this.onDeleted?.(data);
|
|
1225
1556
|
return true;
|
|
1226
1557
|
}
|
|
1227
1558
|
/** Removes the table. Open rows are closed without saving. */
|
|
1228
1559
|
destroy() {
|
|
1229
1560
|
this.tableEl.removeEventListener("click", this.onClick);
|
|
1230
1561
|
this.tableEl.removeEventListener("keydown", this.onKeyDown);
|
|
1562
|
+
this.tableEl.removeEventListener("click", this.onRowEvent);
|
|
1563
|
+
this.tableEl.removeEventListener("dblclick", this.onRowEvent);
|
|
1231
1564
|
this.destroySessions();
|
|
1232
1565
|
this.deleting.clear();
|
|
1566
|
+
this.selection?.destroy();
|
|
1233
1567
|
this.dataTable.destroy();
|
|
1234
1568
|
this.tableEl.remove();
|
|
1235
1569
|
}
|
|
@@ -1270,7 +1604,7 @@ var Tadabut = class {
|
|
|
1270
1604
|
}
|
|
1271
1605
|
/** The last cell of a row, the one with the buttons. */
|
|
1272
1606
|
actionsCell(row) {
|
|
1273
|
-
return this.dataTable.cell(row.index(), this.columns.length).node();
|
|
1607
|
+
return this.dataTable.cell(row.index(), this.dtIndex(this.columns.length)).node();
|
|
1274
1608
|
}
|
|
1275
1609
|
/**
|
|
1276
1610
|
* What the Delete button does: asks inline, asks with the user's function,
|
|
@@ -1379,12 +1713,57 @@ var Tadabut = class {
|
|
|
1379
1713
|
updateTotals = () => {
|
|
1380
1714
|
const rows = this.dataTable.rows(rowsFor(this.totalRows)).data().toArray();
|
|
1381
1715
|
this.columns.forEach((column, index) => {
|
|
1382
|
-
const cell = this.dataTable.column(index).footer();
|
|
1716
|
+
const cell = this.dataTable.column(this.dtIndex(index)).footer();
|
|
1383
1717
|
if (column.total !== void 0) cell.textContent = totalText(column, rows);
|
|
1384
1718
|
else if (index === 0) cell.textContent = this.texts.total;
|
|
1385
1719
|
});
|
|
1386
1720
|
};
|
|
1721
|
+
/** Your column's index as DataTables counts it. */
|
|
1722
|
+
dtIndex(index) {
|
|
1723
|
+
return index + this.offset;
|
|
1724
|
+
}
|
|
1725
|
+
/**
|
|
1726
|
+
* The row a click landed on, or `undefined` if it doesn't count: buttons,
|
|
1727
|
+
* inputs, links, the checkbox and buttons cells, the new row, a row being
|
|
1728
|
+
* edited, the header, the footer and error lines.
|
|
1729
|
+
*/
|
|
1730
|
+
clickedRow(event) {
|
|
1731
|
+
const target = event.target;
|
|
1732
|
+
if (!(target instanceof Element)) return;
|
|
1733
|
+
if (target.closest("button, input, select, textarea, label, a") !== null) return;
|
|
1734
|
+
if (target.closest("td.tdb-select, td.tdb-actions") !== null) return;
|
|
1735
|
+
if (this.isInNewRow(target)) return;
|
|
1736
|
+
const tr = target.closest("tr");
|
|
1737
|
+
if (tr === null || tr.classList.contains("tdb-row-editing")) return;
|
|
1738
|
+
const row = this.dataTable.row(tr);
|
|
1739
|
+
return row.any() && row.node() === tr ? row : void 0;
|
|
1740
|
+
}
|
|
1741
|
+
/**
|
|
1742
|
+
* Row clicks and double clicks. A double click also opens the row with
|
|
1743
|
+
* `editing.doubleClick`. An arrow function, so `destroy` can remove it.
|
|
1744
|
+
*/
|
|
1745
|
+
onRowEvent = (event) => {
|
|
1746
|
+
const row = this.clickedRow(event);
|
|
1747
|
+
if (row === void 0) return;
|
|
1748
|
+
if (event.type === "click") {
|
|
1749
|
+
this.onRowClick?.(row.data(), event);
|
|
1750
|
+
return;
|
|
1751
|
+
}
|
|
1752
|
+
if (this.editing.doubleClick && this.rowId !== void 0) this.edit(row.data()[this.rowId]);
|
|
1753
|
+
this.onRowDoubleClick?.(row.data(), event);
|
|
1754
|
+
};
|
|
1387
1755
|
};
|
|
1756
|
+
/**
|
|
1757
|
+
* DataTables counts the checkbox column too, so `[[1, 'asc']]` from you means
|
|
1758
|
+
* its column 2. Moves each `[index, direction]` along; the default sorts by
|
|
1759
|
+
* your first column, as without selection. Other shapes of `order` are rare
|
|
1760
|
+
* and left as they are.
|
|
1761
|
+
*/
|
|
1762
|
+
function shiftOrder(order, offset) {
|
|
1763
|
+
if (order === void 0) return [[offset, "asc"]];
|
|
1764
|
+
if (!Array.isArray(order)) return order;
|
|
1765
|
+
return order.map((entry) => Array.isArray(entry) && typeof entry[0] === "number" ? [entry[0] + offset, entry[1]] : entry);
|
|
1766
|
+
}
|
|
1388
1767
|
// src/texts/fr.ts
|
|
1389
1768
|
/** French. */
|
|
1390
1769
|
const fr = {
|
|
@@ -1403,6 +1782,8 @@ const fr = {
|
|
|
1403
1782
|
loadFailed: "Impossible de charger les options.",
|
|
1404
1783
|
required: "Ce champ est obligatoire.",
|
|
1405
1784
|
patternMismatch: "Format incorrect.",
|
|
1785
|
+
selectRow: "Sélectionner la ligne",
|
|
1786
|
+
selectAll: "Tout sélectionner",
|
|
1406
1787
|
tooShort: (min) => `Au moins ${min} caractères.`,
|
|
1407
1788
|
tooLong: (max) => `Au plus ${max} caractères.`,
|
|
1408
1789
|
tooSmall: (min) => `Doit être supérieur ou égal à ${min}.`,
|
|
@@ -1410,4 +1791,4 @@ const fr = {
|
|
|
1410
1791
|
tooEarly: (min) => `Doit être le ${min} ou après.`,
|
|
1411
1792
|
tooLate: (max) => `Doit être le ${max} ou avant.`
|
|
1412
1793
|
};
|
|
1413
|
-
export { formatMoney as a, defaultMoneyFormat as i, Tadabut as n, en as r, fr as t };
|
|
1794
|
+
export { formatMoney as a, parseDate as c, defaultMoneyFormat as i, Tadabut as n, defaultDateFormat as o, en as r, formatDate as s, fr as t };
|