tadabut 0.6.0 → 0.7.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.
@@ -50,6 +50,64 @@ function partsOf(format) {
50
50
  order: format.split(separator)
51
51
  };
52
52
  }
53
+ // src/editors/time-format.ts
54
+ /**
55
+ * Reads `'h:mm'`, `'hh:mm'` or `'hh:mm:ss'`, and gives `'hh:mm:ss'`. Anything
56
+ * after is ignored, like `'.000Z'`. `null` if it isn't a real time.
57
+ *
58
+ * @example
59
+ * parseTime('9:05'); // '09:05:00'
60
+ */
61
+ function parseTime(value) {
62
+ const match = value.trim().match(/^(\d{1,2}):(\d{2})(?::(\d{2}))?/);
63
+ if (match === null) return null;
64
+ const [, hh = "", mm = "", ss = "00"] = match;
65
+ if (Number(hh) > 23 || Number(mm) > 59 || Number(ss) > 59) return null;
66
+ return `${hh.padStart(2, "0")}:${mm}:${ss}`;
67
+ }
68
+ /**
69
+ * Writes a time `parseTime` returned, with or without the seconds.
70
+ *
71
+ * @example
72
+ * formatTime('09:05:00', false); // '09:05'
73
+ */
74
+ function formatTime(time, seconds) {
75
+ return seconds ? time : time.slice(0, 5);
76
+ }
77
+ // src/editors/datetime-format.ts
78
+ /**
79
+ * Reads a date in `format`, then a time after a `T` or a space, and gives
80
+ * `'yyyy-mm-ddThh:mm:ss'`. No time means midnight. A time zone is ignored,
81
+ * not converted. `null` if the date or the time isn't real.
82
+ *
83
+ * @example
84
+ * parseDateTime('05/01/2024 14:30', 'dd/mm/yyyy'); // '2024-01-05T14:30:00'
85
+ */
86
+ function parseDateTime(value, format) {
87
+ const text = value.trim();
88
+ const at = text.search(/[T ]/);
89
+ const date = parseDate(at === -1 ? text : text.slice(0, at), format);
90
+ const time = parseTime(at === -1 ? "00:00" : text.slice(at + 1));
91
+ return date === null || time === null ? null : `${date}T${time}`;
92
+ }
93
+ /**
94
+ * Writes a date-time `parseDateTime` returned. `separator` goes between the
95
+ * date and the time.
96
+ *
97
+ * @example
98
+ * formatDateTime('2024-01-05T14:30:00', 'dd/mm/yyyy', false, ' '); // '05/01/2024 14:30'
99
+ */
100
+ function formatDateTime(value, format, seconds, separator) {
101
+ const [date = "", time = ""] = value.split("T");
102
+ return `${formatDate(date, format)}${separator}${formatTime(time, seconds)}`;
103
+ }
104
+ /**
105
+ * What goes between the date and the time in the rows: `T` for ISO, as
106
+ * servers send it, a space otherwise.
107
+ */
108
+ function dataSeparator(format) {
109
+ return format === "yyyy-mm-dd" ? "T" : " ";
110
+ }
53
111
  // src/editors/money-format.ts
54
112
  /** `12 500.00`. */
55
113
  const defaultMoneyFormat = {
@@ -164,6 +222,7 @@ function createMoneyEditor(config, context) {
164
222
  input.type = "text";
165
223
  input.inputMode = "decimal";
166
224
  input.autocomplete = "off";
225
+ input.disabled = context.readOnly ?? false;
167
226
  addClasses(input, ["tdb-input", "tdb-input-money"], context.className);
168
227
  /** Read from the text every time, so there's no copy to get out of sync. */
169
228
  const getValue = () => parseMoney(input.value, format);
@@ -213,7 +272,8 @@ const buttonActions = /* @__PURE__ */ new Set([
213
272
  "save",
214
273
  "cancel",
215
274
  "confirm",
216
- "keep"
275
+ "keep",
276
+ "detail"
217
277
  ]);
218
278
  /** Whether a `data-tdb-action` is one of ours. */
219
279
  function isButtonAction(value) {
@@ -282,6 +342,10 @@ function selectRowHtml(texts, classNames) {
282
342
  function checkboxHtml(kind, label, classNames) {
283
343
  return `<input type="checkbox" class="${escapeHtml([kind, ...splitClasses(classNames.select)].join(" "))}" aria-label="${escapeHtml(label)}">`;
284
344
  }
345
+ /** The ▸ button that opens a row's detail, as HTML for `defaultContent`. Starts closed. */
346
+ function detailToggleHtml(texts, classNames) {
347
+ return `<button type="button" class="${escapeHtml(["tdb-detail-toggle", ...splitClasses(classNames.detail)].join(" "))}" data-tdb-action="detail" aria-expanded="false" aria-label="${escapeHtml(texts.showDetail)}">▸</button>`;
348
+ }
285
349
  // src/columns/to-datatables.ts
286
350
  /**
287
351
  * The column with the Edit and Delete buttons. While a row is open, Save and
@@ -311,12 +375,23 @@ function selectColumn(classNames, texts) {
311
375
  defaultContent: selectRowHtml(texts, classNames)
312
376
  };
313
377
  }
378
+ /** The column with the ▸ button. Which ones are open is set after each draw. */
379
+ function detailColumn(classNames, texts) {
380
+ return {
381
+ title: "",
382
+ data: null,
383
+ className: "tdb-detail",
384
+ orderable: false,
385
+ searchable: false,
386
+ defaultContent: detailToggleHtml(texts, classNames)
387
+ };
388
+ }
314
389
  /**
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.
390
+ * The checkbox column goes first with `selection`, then the ▸ column with
391
+ * `detail`, and the actions column last. Tadabut shifts your column indexes
392
+ * to match. No `actions` means no actions column.
318
393
  */
319
- function toDataTables(columns, actions, classNames, texts, selection) {
394
+ function toDataTables(columns, actions, classNames, texts, leading) {
320
395
  const converted = columns.map((column) => {
321
396
  const label = labelOf(column);
322
397
  const sortValue = sortValueOf(column);
@@ -340,7 +415,7 @@ function toDataTables(columns, actions, classNames, texts, selection) {
340
415
  }
341
416
  };
342
417
  });
343
- const first = selection ? [selectColumn(classNames, texts)] : [];
418
+ const first = [...leading.selection ? [selectColumn(classNames, texts)] : [], ...leading.detail ? [detailColumn(classNames, texts)] : []];
344
419
  const last = actions.length > 0 ? [actionsColumn(actions, classNames, texts)] : [];
345
420
  return [
346
421
  ...first,
@@ -349,8 +424,9 @@ function toDataTables(columns, actions, classNames, texts, selection) {
349
424
  ];
350
425
  }
351
426
  /**
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.
427
+ * `render`, then a money, date or time format, then `labelProp`, then a fixed
428
+ * list's label, then the value. A date or time that isn't valid shows as it
429
+ * is.
354
430
  */
355
431
  function labelOf(column) {
356
432
  const { render, editor } = column;
@@ -370,6 +446,24 @@ function labelOf(column) {
370
446
  return formatDate(iso, display);
371
447
  };
372
448
  }
449
+ case "time": {
450
+ const seconds = editor.seconds ?? false;
451
+ return (value) => {
452
+ if (typeof value !== "string") return asText(value);
453
+ const time = parseTime(value);
454
+ return time === null ? asText(value) : formatTime(time, seconds);
455
+ };
456
+ }
457
+ case "datetime": {
458
+ const format = editor.format ?? "yyyy-mm-dd";
459
+ const display = editor.display ?? format;
460
+ const seconds = editor.seconds ?? false;
461
+ return (value) => {
462
+ if (typeof value !== "string") return asText(value);
463
+ const dateTime = parseDateTime(value, format);
464
+ return dateTime === null ? asText(value) : formatDateTime(dateTime, display, seconds, " ");
465
+ };
466
+ }
373
467
  case "select": {
374
468
  const labelProp = editor.labelProp;
375
469
  if (labelProp !== void 0) return (_value, row) => asText(row[labelProp]);
@@ -392,8 +486,9 @@ function defined(values) {
392
486
  }
393
487
  /**
394
488
  * 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.
489
+ * sorts as January, times `'hh:mm:ss'`, and both `'yyyy-mm-ddThh:mm:ss'`.
490
+ * Invalid and empty ones give `''`, which sorts first. Anything else stays as
491
+ * it is: numbers stay numbers.
397
492
  */
398
493
  function sortValueOf(column) {
399
494
  const editor = column.editor;
@@ -401,6 +496,11 @@ function sortValueOf(column) {
401
496
  const format = editor.format ?? "yyyy-mm-dd";
402
497
  return (value) => typeof value === "string" ? parseDate(value, format) ?? "" : "";
403
498
  }
499
+ if (editor?.type === "time") return (value) => typeof value === "string" ? parseTime(value) ?? "" : "";
500
+ if (editor?.type === "datetime") {
501
+ const format = editor.format ?? "yyyy-mm-dd";
502
+ return (value) => typeof value === "string" ? parseDateTime(value, format) ?? "" : "";
503
+ }
404
504
  return (value) => value;
405
505
  }
406
506
  // src/texts/en.ts
@@ -415,20 +515,26 @@ const en = {
415
515
  keep: "No",
416
516
  deleteFailed: "Could not delete.",
417
517
  total: "Total",
518
+ selectRow: "Select row",
519
+ selectAll: "Select all",
520
+ showDetail: "Show details",
521
+ hideDetail: "Hide details",
522
+ detailLoading: "Loading…",
523
+ detailFailed: "Couldn't load the details.",
418
524
  loading: "Loading…",
419
525
  saveFailed: "Could not save.",
420
526
  addFailed: "Could not add.",
421
527
  loadFailed: "Could not load the options.",
422
528
  required: "This field is required.",
423
529
  patternMismatch: "Wrong format.",
424
- selectRow: "Select row",
425
- selectAll: "Select all",
426
530
  tooShort: (min) => `Must be at least ${min} characters.`,
427
531
  tooLong: (max) => `Must be at most ${max} characters.`,
428
532
  tooSmall: (min) => `Must be ${min} or more.`,
429
533
  tooLarge: (max) => `Must be ${max} or less.`,
430
534
  tooEarly: (min) => `Must be on or after ${min}.`,
431
- tooLate: (max) => `Must be on or before ${max}.`
535
+ tooLate: (max) => `Must be on or before ${max}.`,
536
+ timeTooEarly: (min) => `Must be at or after ${min}.`,
537
+ timeTooLate: (max) => `Must be at or before ${max}.`
432
538
  };
433
539
  // src/editors/checkbox.ts
434
540
  /** Builds the checkbox. It keeps the row's value as it is until the user clicks it. */
@@ -440,6 +546,7 @@ function createCheckboxEditor(config, context) {
440
546
  let current = context.value;
441
547
  const input = document.createElement("input");
442
548
  input.type = "checkbox";
549
+ input.disabled = context.readOnly ?? false;
443
550
  addClasses(input, ["tdb-input", "tdb-input-checkbox"], context.className);
444
551
  /**
445
552
  * Only the `checked` value ticks the box. Anything else, `null` included,
@@ -471,6 +578,7 @@ function createDateEditor(config, context) {
471
578
  const format = config.format ?? "yyyy-mm-dd";
472
579
  const input = document.createElement("input");
473
580
  input.type = "date";
581
+ input.disabled = context.readOnly ?? false;
474
582
  addClasses(input, ["tdb-input", "tdb-input-date"], context.className);
475
583
  if (config.min !== void 0) input.min = parseDate(config.min, format) ?? "";
476
584
  if (config.max !== void 0) input.max = parseDate(config.max, format) ?? "";
@@ -490,11 +598,50 @@ function createDateEditor(config, context) {
490
598
  destroy: () => input.remove()
491
599
  };
492
600
  }
601
+ // src/editors/datetime.ts
602
+ /**
603
+ * Builds the date-and-time input. The input itself always works in
604
+ * `'yyyy-mm-ddThh:mm'`, so values are converted going in and out.
605
+ */
606
+ function createDateTimeEditor(config, context) {
607
+ const format = config.format ?? "yyyy-mm-dd";
608
+ const seconds = config.seconds ?? false;
609
+ const separator = dataSeparator(format);
610
+ const input = document.createElement("input");
611
+ input.type = "datetime-local";
612
+ input.disabled = context.readOnly ?? false;
613
+ addClasses(input, ["tdb-input", "tdb-input-datetime"], context.className);
614
+ if (seconds) input.step = "1";
615
+ /** What the input wants: `'yyyy-mm-ddThh:mm'`, with `:ss` when seconds show. */
616
+ const forInput = (value) => seconds ? value : value.slice(0, 16);
617
+ if (config.min !== void 0) input.min = forInput(parseDateTime(config.min, format) ?? "");
618
+ if (config.max !== void 0) input.max = forInput(parseDateTime(config.max, format) ?? "");
619
+ /** Empty gives `null`. Otherwise written like the data. */
620
+ const getValue = () => {
621
+ const value = input.value === "" ? null : parseDateTime(input.value, defaultDateFormat);
622
+ return value === null ? null : formatDateTime(value, format, seconds, separator);
623
+ };
624
+ /** `null`, or a value that isn't a date and time in `format`, shows as empty. */
625
+ const setValue = (value) => {
626
+ const inside = value === null ? null : parseDateTime(value, format);
627
+ input.value = inside === null ? "" : forInput(inside);
628
+ };
629
+ setValue(context.value ?? null);
630
+ input.addEventListener("input", () => context.onChange(getValue()));
631
+ return {
632
+ element: input,
633
+ getValue,
634
+ setValue,
635
+ focus: () => input.focus(),
636
+ destroy: () => input.remove()
637
+ };
638
+ }
493
639
  // src/editors/number.ts
494
640
  /** Builds the number input. */
495
641
  function createNumberEditor(config, context) {
496
642
  const input = document.createElement("input");
497
643
  input.type = "number";
644
+ input.disabled = context.readOnly ?? false;
498
645
  addClasses(input, ["tdb-input", "tdb-input-number"], context.className);
499
646
  if (config.min !== void 0) input.min = String(config.min);
500
647
  if (config.max !== void 0) input.max = String(config.max);
@@ -523,6 +670,8 @@ function createNumberEditor(config, context) {
523
670
  */
524
671
  function createSelectEditor(config, context, texts = en) {
525
672
  const select = document.createElement("select");
673
+ const readOnly = context.readOnly ?? false;
674
+ select.disabled = readOnly;
526
675
  addClasses(select, ["tdb-input", "tdb-input-select"], context.className);
527
676
  /** The options on screen. Empty while a loader runs. */
528
677
  let options = [];
@@ -530,12 +679,14 @@ function createSelectEditor(config, context, texts = en) {
530
679
  let current = context.value;
531
680
  /** The running load, so a newer one or `destroy` can abort it. */
532
681
  let controller;
682
+ /** Whether "Loading…" or an empty line shows instead of the options. */
683
+ let waiting = false;
533
684
  /** Selects `current`. A value that isn't in the list becomes `null`. */
534
685
  const pickCurrent = () => {
535
686
  select.selectedIndex = options.findIndex((option) => option.value === current);
536
687
  if (select.selectedIndex === -1) current = null;
537
688
  };
538
- /** Shows `list`, enables the select and picks `current`. */
689
+ /** Shows `list`, unlocks the select unless it's read-only, and picks `current`. */
539
690
  const fill = (list) => {
540
691
  options = list;
541
692
  select.replaceChildren(...list.map((option) => {
@@ -543,7 +694,8 @@ function createSelectEditor(config, context, texts = en) {
543
694
  element.textContent = option.label;
544
695
  return element;
545
696
  }));
546
- select.disabled = false;
697
+ waiting = false;
698
+ select.disabled = readOnly;
547
699
  pickCurrent();
548
700
  };
549
701
  /** Shows a single disabled line, like "Loading…". */
@@ -552,6 +704,7 @@ function createSelectEditor(config, context, texts = en) {
552
704
  const element = document.createElement("option");
553
705
  element.textContent = text;
554
706
  select.replaceChildren(element);
707
+ waiting = true;
555
708
  select.disabled = true;
556
709
  };
557
710
  /** Runs the `options` function and shows its result. */
@@ -584,7 +737,7 @@ function createSelectEditor(config, context, texts = en) {
584
737
  /** While loading, the value is only remembered. */
585
738
  const setValue = (value) => {
586
739
  current = value;
587
- if (!select.disabled) pickCurrent();
740
+ if (!waiting) pickCurrent();
588
741
  };
589
742
  if (typeof config.options !== "function") fill(config.options);
590
743
  select.addEventListener("change", () => {
@@ -595,6 +748,7 @@ function createSelectEditor(config, context, texts = en) {
595
748
  element: select,
596
749
  getValue,
597
750
  getLabel,
751
+ getOptions: () => options,
598
752
  setValue,
599
753
  load,
600
754
  focus: () => select.focus(),
@@ -609,6 +763,7 @@ function createSelectEditor(config, context, texts = en) {
609
763
  function createTextEditor(config, context) {
610
764
  const input = document.createElement("input");
611
765
  input.type = "text";
766
+ input.disabled = context.readOnly ?? false;
612
767
  addClasses(input, ["tdb-input", "tdb-input-text"], context.className);
613
768
  if (config.minLength !== void 0) input.minLength = config.minLength;
614
769
  if (config.maxLength !== void 0) input.maxLength = config.maxLength;
@@ -625,6 +780,37 @@ function createTextEditor(config, context) {
625
780
  destroy: () => input.remove()
626
781
  };
627
782
  }
783
+ // src/editors/time.ts
784
+ /** Builds the time input. */
785
+ function createTimeEditor(config, context) {
786
+ const seconds = config.seconds ?? false;
787
+ const input = document.createElement("input");
788
+ input.type = "time";
789
+ input.disabled = context.readOnly ?? false;
790
+ addClasses(input, ["tdb-input", "tdb-input-time"], context.className);
791
+ if (seconds) input.step = "1";
792
+ if (config.min !== void 0) input.min = formatTime(parseTime(config.min) ?? "", seconds);
793
+ if (config.max !== void 0) input.max = formatTime(parseTime(config.max) ?? "", seconds);
794
+ /**
795
+ * Empty gives `null`. Read through `parseTime`, since some browsers drop
796
+ * `:00` seconds.
797
+ */
798
+ const getValue = () => input.value === "" ? null : formatTime(parseTime(input.value) ?? "", seconds);
799
+ /** `null`, or a value that isn't a time, shows as empty. */
800
+ const setValue = (value) => {
801
+ const time = value === null ? null : parseTime(value);
802
+ input.value = time === null ? "" : formatTime(time, seconds);
803
+ };
804
+ setValue(context.value ?? null);
805
+ input.addEventListener("input", () => context.onChange(getValue()));
806
+ return {
807
+ element: input,
808
+ getValue,
809
+ setValue,
810
+ focus: () => input.focus(),
811
+ destroy: () => input.remove()
812
+ };
813
+ }
628
814
  // src/editors/create.ts
629
815
  /**
630
816
  * `Column<Row>` already checked that each config fits its column, so the
@@ -633,7 +819,8 @@ function createTextEditor(config, context) {
633
819
  function createEditor(config, context, texts = en) {
634
820
  const fullContext = {
635
821
  ...context,
636
- className: config.className
822
+ className: config.className,
823
+ readOnly: config.readOnly
637
824
  };
638
825
  switch (config.type) {
639
826
  case "text": return loosen(createTextEditor(config, tighten(fullContext)));
@@ -643,6 +830,8 @@ function createEditor(config, context, texts = en) {
643
830
  case "select": return createSelectEditor(config, fullContext, texts);
644
831
  case "custom": return config.create(fullContext);
645
832
  case "money": return loosen(createMoneyEditor(config, tighten(fullContext)));
833
+ case "time": return loosen(createTimeEditor(config, tighten(fullContext)));
834
+ case "datetime": return loosen(createDateTimeEditor(config, tighten(fullContext)));
646
835
  default: throw new Error(`Unknown editor: ${JSON.stringify(config)}`);
647
836
  }
648
837
  }
@@ -667,6 +856,8 @@ function builtInError(config, value, texts) {
667
856
  case "number":
668
857
  case "money": return typeof value === "number" ? rangeError(config, value, texts) : void 0;
669
858
  case "date": return typeof value === "string" ? dateError(config, value, texts) : void 0;
859
+ case "time": return typeof value === "string" ? timeError(config, value, texts) : void 0;
860
+ case "datetime": return typeof value === "string" ? dateTimeError(config, value, texts) : void 0;
670
861
  case "checkbox":
671
862
  case "select":
672
863
  case "custom": return;
@@ -684,10 +875,48 @@ function rangeError(config, value, texts) {
684
875
  if (config.min !== void 0 && value < config.min) return texts.tooSmall(config.min);
685
876
  if (config.max !== void 0 && value > config.max) return texts.tooLarge(config.max);
686
877
  }
687
- /** `min` and `max` for dates. */
878
+ /**
879
+ * `min` and `max` for dates. Compared as `'yyyy-mm-dd'`, since `'15/03/1999'`
880
+ * comes after `'01/01/2000'` as text. The message shows the limit as the
881
+ * cells show dates.
882
+ */
688
883
  function dateError(config, value, texts) {
689
- if (config.min !== void 0 && value < config.min) return texts.tooEarly(config.min);
690
- if (config.max !== void 0 && value > config.max) return texts.tooLate(config.max);
884
+ const format = config.format ?? "yyyy-mm-dd";
885
+ const display = config.display ?? format;
886
+ const date = parseDate(value, format);
887
+ if (date === null) return;
888
+ const min = config.min === void 0 ? null : parseDate(config.min, format);
889
+ const max = config.max === void 0 ? null : parseDate(config.max, format);
890
+ if (min !== null && date < min) return texts.tooEarly(formatDate(min, display));
891
+ if (max !== null && date > max) return texts.tooLate(formatDate(max, display));
892
+ }
893
+ /**
894
+ * `min` and `max` for times. Compared as `'hh:mm:ss'`, since `'9:05'` comes
895
+ * after `'14:30'` as text.
896
+ */
897
+ function timeError(config, value, texts) {
898
+ const seconds = config.seconds ?? false;
899
+ const time = parseTime(value);
900
+ if (time === null) return;
901
+ const min = config.min === void 0 ? null : parseTime(config.min);
902
+ const max = config.max === void 0 ? null : parseTime(config.max);
903
+ if (min !== null && time < min) return texts.timeTooEarly(formatTime(min, seconds));
904
+ if (max !== null && time > max) return texts.timeTooLate(formatTime(max, seconds));
905
+ }
906
+ /**
907
+ * `min` and `max` for dates with a time. Compared as `'yyyy-mm-ddThh:mm:ss'`.
908
+ * The message shows the limit as the cells show it.
909
+ */
910
+ function dateTimeError(config, value, texts) {
911
+ const format = config.format ?? "yyyy-mm-dd";
912
+ const display = config.display ?? format;
913
+ const seconds = config.seconds ?? false;
914
+ const dateTime = parseDateTime(value, format);
915
+ if (dateTime === null) return;
916
+ const min = config.min === void 0 ? null : parseDateTime(config.min, format);
917
+ const max = config.max === void 0 ? null : parseDateTime(config.max, format);
918
+ if (min !== null && dateTime < min) return texts.tooEarly(formatDateTime(min, display, seconds, " "));
919
+ if (max !== null && dateTime > max) return texts.tooLate(formatDateTime(max, display, seconds, " "));
691
920
  }
692
921
  /**
693
922
  * Same rules as the HTML `pattern` attribute: whole value, `v` flag, and an
@@ -724,10 +953,13 @@ var EditSession = class {
724
953
  saving = false;
725
954
  /** Error messages and the "Loading…" text. */
726
955
  texts;
956
+ /** Told each time a select's options load, so the table can remember them. */
957
+ onOptions;
727
958
  /** Puts an editor in each cell and starts loading select options. */
728
- constructor(row, cells, texts = en) {
959
+ constructor(row, cells, texts = en, onOptions) {
729
960
  this.originalData = { ...row };
730
961
  this.texts = texts;
962
+ this.onOptions = onOptions;
731
963
  for (const { column, cell } of cells) {
732
964
  if (column.editor === void 0) continue;
733
965
  const config = column.editor;
@@ -755,6 +987,7 @@ var EditSession = class {
755
987
  getRow() {
756
988
  const row = { ...this.originalData };
757
989
  for (const [prop, { editor, config }] of this.fields) {
990
+ if (config.readOnly) continue;
758
991
  row[prop] = editor.getValue();
759
992
  if (config.type === "select" && config.labelProp !== void 0) {
760
993
  const label = editor.getLabel?.();
@@ -768,6 +1001,7 @@ var EditSession = class {
768
1001
  const row = this.getRow();
769
1002
  const errors = /* @__PURE__ */ new Map();
770
1003
  for (const [prop, { editor, config }] of this.fields) {
1004
+ if (config.readOnly) continue;
771
1005
  const message = this.loadErrors.has(prop) ? this.texts.loadFailed : validateValue(config, editor.getValue(), row, this.texts);
772
1006
  if (message !== void 0) errors.set(prop, message);
773
1007
  }
@@ -850,6 +1084,7 @@ var EditSession = class {
850
1084
  return;
851
1085
  }
852
1086
  if (this.loadErrors.delete(prop)) this.hideError(prop);
1087
+ this.onOptions?.(prop, field.editor.getOptions?.() ?? []);
853
1088
  }
854
1089
  };
855
1090
  // src/table/check-options.ts
@@ -859,16 +1094,20 @@ var EditSession = class {
859
1094
  * @throws If a column has an `editor` and there's no `rowId`.
860
1095
  * @throws If `onDelete` is set and there's no `rowId`.
861
1096
  * @throws If `selection` is on and there's no `rowId`.
1097
+ * @throws If `detail` is set and there's no `rowId`.
862
1098
  * @throws If a hidden column (`visible: false`) has an `editor`.
863
1099
  * @throws If a select's `labelProp` is its own column.
864
1100
  * @throws If a money editor's `separator` and `point` are the same.
865
1101
  * @throws If a date editor's `min` or `max` isn't a real date in its `format`.
1102
+ * @throws If a time editor's `min` or `max` isn't a real time.
1103
+ * @throws If a datetime editor's `min` or `max` isn't a real date and time in its `format`.
866
1104
  */
867
1105
  function checkOptions(options) {
868
1106
  if (options.rowId === void 0) {
869
1107
  if (options.columns.some((column) => column.editor !== void 0)) throw new Error("Tadabut: rowId is required when a column has an editor.");
870
1108
  if (options.onDelete !== void 0) throw new Error("Tadabut: rowId is required with onDelete.");
871
1109
  if (options.selection) throw new Error("Tadabut: rowId is required with selection.");
1110
+ if (options.detail !== void 0) throw new Error("Tadabut: rowId is required with detail.");
872
1111
  }
873
1112
  const hiddenEditable = options.columns.find((column) => column.editor !== void 0 && column.visible === false);
874
1113
  if (hiddenEditable !== void 0) throw new Error(`Tadabut: the "${hiddenEditable.prop}" column can't have both an editor and visible: false.`);
@@ -884,6 +1123,15 @@ function checkOptions(options) {
884
1123
  const format = column.editor.format ?? "yyyy-mm-dd";
885
1124
  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
1125
  }
1126
+ for (const column of options.columns) {
1127
+ if (column.editor?.type !== "time") continue;
1128
+ for (const limit of [column.editor.min, column.editor.max]) if (limit !== void 0 && parseTime(limit) === null) throw new Error(`Tadabut: the "${column.prop}" column's time "${limit}" isn't a real time.`);
1129
+ }
1130
+ for (const column of options.columns) {
1131
+ if (column.editor?.type !== "datetime") continue;
1132
+ const format = column.editor.format ?? "yyyy-mm-dd";
1133
+ for (const limit of [column.editor.min, column.editor.max]) if (limit !== void 0 && parseDateTime(limit, format) === null) throw new Error(`Tadabut: the "${column.prop}" column's date and time "${limit}" isn't real, or isn't written as ${format} hh:mm.`);
1134
+ }
887
1135
  }
888
1136
  // src/table/new-row.ts
889
1137
  /**
@@ -898,7 +1146,7 @@ var NewRow = class {
898
1146
  tr;
899
1147
  /** Its inputs. */
900
1148
  session;
901
- /** The first column with an editor, for the focus. */
1149
+ /** The first field that isn't read-only, for the focus. A locked input can't take it. */
902
1150
  firstProp;
903
1151
  /** Builds the row. Nothing is shown until `body` is put in the table. */
904
1152
  constructor(defaults, options) {
@@ -928,8 +1176,8 @@ var NewRow = class {
928
1176
  this.body.append(this.tr);
929
1177
  const start = { ...defaults };
930
1178
  for (const { column } of cells) start[column.prop] ??= null;
931
- this.session = new EditSession(start, cells, options.texts);
932
- this.firstProp = cells[0]?.column.prop;
1179
+ this.session = new EditSession(start, cells, options.texts, options.onOptions);
1180
+ this.firstProp = cells.find(({ column }) => !column.editor?.readOnly)?.column.prop;
933
1181
  }
934
1182
  /** Whether `onAdd` is running. */
935
1183
  get saving() {
@@ -993,6 +1241,160 @@ var NewRow = class {
993
1241
  this.body.remove();
994
1242
  }
995
1243
  };
1244
+ // src/table/row-lines.ts
1245
+ /**
1246
+ * The lines under each row: its detail and its error. DataTables only allows
1247
+ * one set of lines per row, so both live here and are put back together each
1248
+ * time one changes.
1249
+ */
1250
+ var RowLines = class {
1251
+ /** The open details, by row id. */
1252
+ details = /* @__PURE__ */ new Map();
1253
+ /** The error lines, by row id. */
1254
+ errors = /* @__PURE__ */ new Map();
1255
+ /** What the lines need from the table. */
1256
+ parts;
1257
+ /** Nothing is shown until a detail opens or an error comes. */
1258
+ constructor(parts) {
1259
+ this.parts = parts;
1260
+ }
1261
+ /** Whether a row's detail is open. */
1262
+ isOpen(id) {
1263
+ return this.details.has(id);
1264
+ }
1265
+ /**
1266
+ * Opens a row's detail. "Loading…" shows until `detail` returns. Does
1267
+ * nothing without `detail`, if it's open already, or if the row isn't in
1268
+ * the table.
1269
+ */
1270
+ open(id) {
1271
+ const detail = this.parts.detail;
1272
+ const row = this.findRow(id);
1273
+ if (detail === void 0 || row === void 0 || this.isOpen(id)) return;
1274
+ const cell = document.createElement("div");
1275
+ cell.textContent = this.parts.texts.detailLoading;
1276
+ const controller = new AbortController();
1277
+ this.details.set(id, {
1278
+ line: this.line("tdb-row-detail", cell),
1279
+ controller
1280
+ });
1281
+ this.render(id);
1282
+ this.load(detail, row.data(), cell, controller);
1283
+ }
1284
+ /** Closes a row's detail, and stops its load if it's still running. */
1285
+ close(id) {
1286
+ const open = this.details.get(id);
1287
+ if (open === void 0) return;
1288
+ open.controller.abort();
1289
+ this.details.delete(id);
1290
+ this.render(id);
1291
+ }
1292
+ /** Opens a closed detail, closes an open one. */
1293
+ toggle(id) {
1294
+ if (this.isOpen(id)) this.close(id);
1295
+ else this.open(id);
1296
+ }
1297
+ /** Runs an open detail's `detail` again, after a save. Leaves a closed one closed. */
1298
+ reload(id) {
1299
+ if (this.isOpen(id)) {
1300
+ this.close(id);
1301
+ this.open(id);
1302
+ }
1303
+ }
1304
+ /** Shows an error under a row, under its detail if open. Replaces the previous one. */
1305
+ showError(id, message) {
1306
+ this.errors.set(id, this.line("tdb-row-error", rowErrorMessage(message)));
1307
+ this.render(id);
1308
+ }
1309
+ /** Removes a row's error, if any. */
1310
+ clearError(id) {
1311
+ if (this.errors.delete(id)) this.render(id);
1312
+ }
1313
+ /**
1314
+ * Sets the ▸ buttons and the `tdb-detail-open` class on the rows shown.
1315
+ * Runs after each draw, since redrawing a row brings back a closed ▸.
1316
+ */
1317
+ sync() {
1318
+ const rowId = this.parts.rowId;
1319
+ if (rowId === void 0) return;
1320
+ const { texts } = this.parts;
1321
+ const rows = this.parts.dataTable.rows({ page: "current" });
1322
+ const data = rows.data().toArray();
1323
+ rows.nodes().toArray().forEach((tr, index) => {
1324
+ const row = data[index];
1325
+ if (row === void 0) return;
1326
+ const open = this.details.has(row[rowId]);
1327
+ tr.classList.toggle("tdb-detail-open", open);
1328
+ const button = tr.querySelector(".tdb-detail-toggle");
1329
+ if (button !== null) {
1330
+ button.setAttribute("aria-expanded", String(open));
1331
+ button.setAttribute("aria-label", open ? texts.hideDetail : texts.showDetail);
1332
+ button.textContent = open ? "▾" : "▸";
1333
+ }
1334
+ });
1335
+ }
1336
+ /**
1337
+ * Forgets the details and errors of rows that left the table, after
1338
+ * `setData` or a delete. Their lines left with them.
1339
+ */
1340
+ prune() {
1341
+ const rowId = this.parts.rowId;
1342
+ if (rowId === void 0) return;
1343
+ const existing = new Set(this.parts.dataTable.rows().data().toArray().map((row) => row[rowId]));
1344
+ for (const [id, open] of this.details) if (!existing.has(id)) {
1345
+ open.controller.abort();
1346
+ this.details.delete(id);
1347
+ }
1348
+ for (const id of this.errors.keys()) if (!existing.has(id)) this.errors.delete(id);
1349
+ }
1350
+ /** Stops every load still running, and forgets everything. */
1351
+ destroy() {
1352
+ for (const open of this.details.values()) open.controller.abort();
1353
+ this.details.clear();
1354
+ this.errors.clear();
1355
+ }
1356
+ /** The row with this id, or `undefined`. */
1357
+ findRow(id) {
1358
+ const rowId = this.parts.rowId;
1359
+ if (rowId === void 0) return;
1360
+ const row = this.parts.dataTable.row((_index, data) => data[rowId] === id);
1361
+ return row.any() ? row : void 0;
1362
+ }
1363
+ /** Waits for `detail`, then puts what it gave, or its error, in the line. */
1364
+ async load(detail, row, cell, controller) {
1365
+ try {
1366
+ const content = await detail(row, { signal: controller.signal });
1367
+ if (controller.signal.aborted) return;
1368
+ if (typeof content === "string") cell.textContent = content;
1369
+ else cell.replaceChildren(content);
1370
+ } catch (error) {
1371
+ if (controller.signal.aborted) return;
1372
+ cell.replaceChildren(rowErrorMessage(error instanceof Error ? error.message : this.parts.texts.detailFailed));
1373
+ }
1374
+ }
1375
+ /** A line under a row: one cell as wide as the table. */
1376
+ line(className, content) {
1377
+ const tr = document.createElement("tr");
1378
+ tr.className = className;
1379
+ const td = document.createElement("td");
1380
+ td.colSpan = this.parts.dataTable.columns(":visible").count();
1381
+ td.append(content);
1382
+ tr.append(td);
1383
+ return tr;
1384
+ }
1385
+ /**
1386
+ * Puts the row's lines back under it: its detail, then its error. With
1387
+ * neither, the lines are removed.
1388
+ */
1389
+ render(id) {
1390
+ const row = this.findRow(id);
1391
+ if (row === void 0) return;
1392
+ const lines = [this.details.get(id)?.line, this.errors.get(id)].filter((line) => line !== void 0);
1393
+ if (lines.length === 0) row.child.remove();
1394
+ else row.child(lines).show();
1395
+ this.sync();
1396
+ }
1397
+ };
996
1398
  // src/table/totals.ts
997
1399
  /**
998
1400
  * An empty footer, one cell per column. It must be in the table before
@@ -1014,17 +1416,33 @@ function rowsFor(scope) {
1014
1416
  case "page": return { page: "current" };
1015
1417
  }
1016
1418
  }
1017
- /** The text under a column: `count` and functions as they are, numbers formatted like the cells. */
1018
- function totalText(column, rows) {
1019
- const { prop, total, render, editor } = column;
1020
- if (total === void 0) return "";
1419
+ /**
1420
+ * The total of a column, before it's written: a number for built-in totals,
1421
+ * rounded like the footer, or your function's text. `undefined` when there's
1422
+ * nothing to show.
1423
+ */
1424
+ function totalValue(column, rows) {
1425
+ const { prop, total, editor } = column;
1426
+ if (total === void 0) return;
1021
1427
  if (typeof total === "function") return total(rows);
1022
- if (total === "count") return String(rows.length);
1428
+ if (total === "count") return rows.length;
1023
1429
  const result = compute(total, rows.map((row) => row[prop]).filter((value) => typeof value === "number" && !Number.isNaN(value)));
1024
- if (result === void 0) return "";
1025
- if (editor?.type === "money" && render === void 0) return formatMoney(result, moneyFormat(editor));
1026
- const rounded = Math.round(result * 100) / 100;
1027
- return render === void 0 ? String(rounded) : render(rounded, {});
1430
+ if (result === void 0) return;
1431
+ return roundTo(result, editor?.type === "money" ? moneyFormat(editor).decimals : 2);
1432
+ }
1433
+ /** The text under a column: `count` and functions as they are, numbers formatted like the cells. */
1434
+ function totalText(column, rows) {
1435
+ const { total, render, editor } = column;
1436
+ const value = totalValue(column, rows);
1437
+ if (value === void 0) return "";
1438
+ if (typeof value === "string" || total === "count") return String(value);
1439
+ if (editor?.type === "money" && render === void 0) return formatMoney(value, moneyFormat(editor));
1440
+ return render === void 0 ? String(value) : render(value, {});
1441
+ }
1442
+ /** Rounds to this many decimals. */
1443
+ function roundTo(value, decimals) {
1444
+ const factor = 10 ** decimals;
1445
+ return Math.round(value * factor) / factor;
1028
1446
  }
1029
1447
  /** `undefined` when there's nothing to average, or no smallest or biggest. */
1030
1448
  function compute(total, values) {
@@ -1216,7 +1634,7 @@ var Tadabut = class {
1216
1634
  hasTotals;
1217
1635
  /** Which rows the totals count, with its default filled in. */
1218
1636
  totalRows;
1219
- /** How many columns Tadabut adds before yours: 1 with `selection`. */
1637
+ /** How many columns Tadabut adds before yours: the checkboxes and the ▸ button. */
1220
1638
  offset;
1221
1639
  /** The ticked rows. Only with `selection`. */
1222
1640
  selection;
@@ -1232,16 +1650,23 @@ var Tadabut = class {
1232
1650
  onRowClick;
1233
1651
  /** The user's row double click callback. */
1234
1652
  onRowDoubleClick;
1653
+ /** The lines under each row: details and errors. */
1654
+ rowLines;
1655
+ /** The last options each select loaded, by field. */
1656
+ loadedOptions = /* @__PURE__ */ new Map();
1235
1657
  /**
1236
1658
  * Builds the table inside `container`, which should be empty.
1237
1659
  *
1238
1660
  * @throws If a column has an `editor` and there's no `rowId`.
1239
1661
  * @throws If `onDelete` is set and there's no `rowId`.
1240
1662
  * @throws If `selection` is on and there's no `rowId`.
1663
+ * @throws If `detail` is set and there's no `rowId`.
1241
1664
  * @throws If a hidden column (`visible: false`) has an `editor`.
1242
1665
  * @throws If a select's `labelProp` is its own column.
1243
1666
  * @throws If a money editor's `separator` and `point` are the same.
1244
1667
  * @throws If a date editor's `min` or `max` isn't a real date in its `format`.
1668
+ * @throws If a time editor's `min` or `max` isn't a real time.
1669
+ * @throws If a datetime editor's `min` or `max` isn't a real date and time in its `format`.
1245
1670
  */
1246
1671
  constructor(container, options) {
1247
1672
  checkOptions(options);
@@ -1274,7 +1699,7 @@ var Tadabut = class {
1274
1699
  };
1275
1700
  this.hasTotals = options.columns.some((column) => column.total !== void 0);
1276
1701
  this.totalRows = options.totalRows ?? "searched";
1277
- this.offset = options.selection ? 1 : 0;
1702
+ this.offset = (options.selection ? 1 : 0) + (options.detail !== void 0 ? 1 : 0);
1278
1703
  this.tableEl = document.createElement("table");
1279
1704
  addClasses(this.tableEl, [], this.classNames.table);
1280
1705
  container.appendChild(this.tableEl);
@@ -1283,9 +1708,19 @@ var Tadabut = class {
1283
1708
  ...options.dataTables,
1284
1709
  ...this.offset > 0 && { order: shiftOrder(options.dataTables?.order, this.offset) },
1285
1710
  data: [...options.data],
1286
- columns: toDataTables(options.columns, actions, this.classNames, this.texts, this.offset > 0)
1711
+ columns: toDataTables(options.columns, actions, this.classNames, this.texts, {
1712
+ selection: Boolean(options.selection),
1713
+ detail: options.detail !== void 0
1714
+ })
1287
1715
  });
1288
- if (this.offset > 0) {
1716
+ this.rowLines = new RowLines({
1717
+ dataTable: this.dataTable,
1718
+ rowId: this.rowId,
1719
+ detail: options.detail,
1720
+ texts: this.texts
1721
+ });
1722
+ if (options.detail !== void 0) this.dataTable.on("draw", () => this.rowLines.sync());
1723
+ if (options.selection) {
1289
1724
  this.selection = new Selection({
1290
1725
  dataTable: this.dataTable,
1291
1726
  rowId: this.rowId,
@@ -1299,7 +1734,7 @@ var Tadabut = class {
1299
1734
  this.dataTable.on("draw", this.updateTotals);
1300
1735
  this.updateTotals();
1301
1736
  }
1302
- if (this.hasActions) this.tableEl.addEventListener("click", this.onClick);
1737
+ if (this.hasActions || options.detail !== void 0) this.tableEl.addEventListener("click", this.onClick);
1303
1738
  if (this.hasEditor) this.tableEl.addEventListener("keydown", this.onKeyDown);
1304
1739
  this.tableEl.addEventListener("click", this.onRowEvent);
1305
1740
  this.tableEl.addEventListener("dblclick", this.onRowEvent);
@@ -1308,15 +1743,135 @@ var Tadabut = class {
1308
1743
  * Replaces all rows. Sorting, search and the page are kept.
1309
1744
  *
1310
1745
  * Open rows, and the new row, are closed first, and their unsaved changes
1311
- * are lost.
1746
+ * are lost. Open details close too.
1312
1747
  */
1313
1748
  setData(rows) {
1314
1749
  this.destroySessions();
1315
1750
  this.deleting.clear();
1751
+ this.rowLines.destroy();
1316
1752
  this.dataTable.clear().rows.add([...rows]).draw(false);
1317
1753
  this.selection?.prune();
1318
1754
  }
1319
1755
  /**
1756
+ * Every row, in the order shown. `scope` works like `totalRows`: `'all'`
1757
+ * (default), `'searched'` or `'page'`. They're copies, so changing them
1758
+ * changes nothing: use `updateRow` for that.
1759
+ *
1760
+ * @example
1761
+ * table.getData('searched'); // the rows that match the search
1762
+ */
1763
+ getData(scope = "all") {
1764
+ return this.dataTable.rows(rowsFor(scope)).data().toArray().map((row) => ({ ...row }));
1765
+ }
1766
+ /**
1767
+ * The row with this id, as a copy, or `undefined`. It's the saved row:
1768
+ * while a row is edited, what's typed isn't in it until Save.
1769
+ */
1770
+ getRow(id) {
1771
+ const row = this.findRow(id);
1772
+ return row === void 0 ? void 0 : { ...row.data() };
1773
+ }
1774
+ /**
1775
+ * The DataTables API, for anything Tadabut doesn't do itself.
1776
+ *
1777
+ * Careful: rows added or removed through it skip Tadabut, so the
1778
+ * selection, details and open rows won't know. Use `insertRow`,
1779
+ * `updateRow`, `removeRow` and `setData` for that.
1780
+ *
1781
+ * @example
1782
+ * table.getApi().order([2, 'desc']).draw();
1783
+ */
1784
+ getApi() {
1785
+ return this.dataTable;
1786
+ }
1787
+ /**
1788
+ * Puts one row or many in the table, without `onAdd` or the new-row form,
1789
+ * and `onAdded` doesn't fire. They go where the sorting puts them, and the
1790
+ * table stays on the page it's on. Either every row goes in, or none.
1791
+ *
1792
+ * @throws If there's no `rowId`, a row has no `rowId` value, or its id is
1793
+ * already in the table (or twice in `rows`).
1794
+ *
1795
+ * @example
1796
+ * table.insertRow(rowFromServer);
1797
+ */
1798
+ insertRow(rows) {
1799
+ const rowId = this.rowId;
1800
+ if (rowId === void 0) throw new Error("Tadabut: rowId is required with insertRow.");
1801
+ const list = Array.isArray(rows) ? rows : [rows];
1802
+ const seen = /* @__PURE__ */ new Set();
1803
+ for (const row of list) {
1804
+ const id = row[rowId];
1805
+ if (typeof id !== "string" && typeof id !== "number") throw new Error(`Tadabut: a row given to insertRow has no "${rowId}".`);
1806
+ if (seen.has(id) || this.findRow(id) !== void 0) throw new Error(`Tadabut: insertRow got the id ${id}, which is already in the table.`);
1807
+ seen.add(id);
1808
+ }
1809
+ this.dataTable.rows.add(list.map((row) => ({ ...row }))).draw(false);
1810
+ }
1811
+ /**
1812
+ * Replaces the row with the same id, without `onSave`, and `onSaved` doesn't
1813
+ * fire. An open detail reloads.
1814
+ *
1815
+ * @returns `false` if no row has this id, or it's being edited or deleted.
1816
+ *
1817
+ * @example
1818
+ * table.updateRow({ ...table.getRow(1), name: 'Nadia' });
1819
+ */
1820
+ updateRow(row) {
1821
+ if (this.rowId === void 0) return false;
1822
+ const id = row[this.rowId];
1823
+ const found = this.findRow(id);
1824
+ if (found === void 0 || this.sessions.has(id) || this.deleting.has(id)) return false;
1825
+ found.data({ ...row }).draw(false);
1826
+ this.rowLines.reload(id);
1827
+ return true;
1828
+ }
1829
+ /**
1830
+ * Takes a row out of the table, without `onDelete` and without asking, and
1831
+ * `onDeleted` doesn't fire. Its detail closes, and it leaves the selection.
1832
+ *
1833
+ * @returns `false` if no row has this id, or it's being edited or deleted.
1834
+ */
1835
+ removeRow(id) {
1836
+ const found = this.findRow(id);
1837
+ if (found === void 0 || this.sessions.has(id) || this.deleting.has(id)) return false;
1838
+ found.remove().draw(false);
1839
+ this.rowLines.prune();
1840
+ this.selection?.prune();
1841
+ return true;
1842
+ }
1843
+ /**
1844
+ * The footer's totals, by field: numbers for built-in totals (not
1845
+ * formatted, rounded like the footer), or your function's text. They count
1846
+ * the same rows as the footer. If two columns show the same field with a
1847
+ * total each, the last one wins.
1848
+ *
1849
+ * @example
1850
+ * table.getTotals().salary; // 176651.5
1851
+ */
1852
+ getTotals() {
1853
+ const rows = this.dataTable.rows(rowsFor(this.totalRows)).data().toArray();
1854
+ const totals = {};
1855
+ for (const column of this.columns) {
1856
+ const value = totalValue(column, rows);
1857
+ if (value !== void 0) totals[column.prop] = value;
1858
+ }
1859
+ return totals;
1860
+ }
1861
+ /**
1862
+ * A select's options. With a fixed list, that list. With an `options`
1863
+ * function, the list it last loaded in an open row, or `undefined` before
1864
+ * any row was opened. `undefined` for a column that isn't a select.
1865
+ *
1866
+ * @example
1867
+ * table.getOptions('countryId'); // [{ value: 1, label: 'Morocco' }, …]
1868
+ */
1869
+ getOptions(prop) {
1870
+ const editor = this.columns.find((column) => column.prop === prop)?.editor;
1871
+ if (editor?.type !== "select") return;
1872
+ return typeof editor.options === "function" ? this.loadedOptions.get(prop) : editor.options;
1873
+ }
1874
+ /**
1320
1875
  * Opens a row for editing.
1321
1876
  *
1322
1877
  * @param id The value of the row's `rowId` field.
@@ -1341,7 +1896,7 @@ var Tadabut = class {
1341
1896
  });
1342
1897
  }
1343
1898
  });
1344
- this.sessions.set(id, new EditSession(row.data(), cells, this.texts));
1899
+ this.sessions.set(id, new EditSession(row.data(), cells, this.texts, this.rememberOptions));
1345
1900
  row.node()?.classList.add("tdb-row-editing");
1346
1901
  if (this.hasActions) this.actionsCell(row).replaceChildren(...saveCancelButtons(this.texts, this.classNames));
1347
1902
  return true;
@@ -1364,7 +1919,8 @@ var Tadabut = class {
1364
1919
  withButtons: this.hasActions,
1365
1920
  texts: this.texts,
1366
1921
  classNames: this.classNames,
1367
- leadingCells: this.offset
1922
+ leadingCells: this.offset,
1923
+ onOptions: this.rememberOptions
1368
1924
  });
1369
1925
  this.tableEl.insertBefore(newRow.body, this.dataTable.table().body());
1370
1926
  newRow.focus();
@@ -1405,6 +1961,29 @@ var Tadabut = class {
1405
1961
  this.selection?.clear();
1406
1962
  }
1407
1963
  /**
1964
+ * Opens a row's detail under it. "Loading…" shows until `detail` returns.
1965
+ * Does nothing without `detail`, if it's open already, or if the row
1966
+ * isn't in the table.
1967
+ *
1968
+ * @example
1969
+ * onRowClick: (row) => table.toggleDetail(row.id)
1970
+ */
1971
+ openDetail(id) {
1972
+ this.rowLines.open(id);
1973
+ }
1974
+ /** Closes a row's detail, and stops its load if it's still running. Does nothing without `detail`. */
1975
+ closeDetail(id) {
1976
+ this.rowLines.close(id);
1977
+ }
1978
+ /** Opens a closed detail, closes an open one, like the ▸ button. Does nothing without `detail`. */
1979
+ toggleDetail(id) {
1980
+ this.rowLines.toggle(id);
1981
+ }
1982
+ /** Whether a row's detail is open, or loading. Always `false` without `detail`. */
1983
+ isDetailOpen(id) {
1984
+ return this.rowLines.isOpen(id);
1985
+ }
1986
+ /**
1408
1987
  * Closes the new row and throws it away. Does nothing if there's none, or
1409
1988
  * while it's saving.
1410
1989
  */
@@ -1470,7 +2049,7 @@ var Tadabut = class {
1470
2049
  if (session.saving) return;
1471
2050
  session.destroy();
1472
2051
  this.sessions.delete(id);
1473
- this.clearRowError(id);
2052
+ this.rowLines.clearError(id);
1474
2053
  this.findRow(id)?.node()?.classList.remove("tdb-row-editing");
1475
2054
  this.findRow(id)?.invalidate("data").draw(false);
1476
2055
  }
@@ -1491,7 +2070,7 @@ var Tadabut = class {
1491
2070
  const session = this.sessions.get(id);
1492
2071
  if (session === void 0 || session.saving) return false;
1493
2072
  session.clearErrors();
1494
- this.clearRowError(id);
2073
+ this.rowLines.clearError(id);
1495
2074
  const errors = session.validate();
1496
2075
  const [firstInvalid] = errors.keys();
1497
2076
  if (firstInvalid !== void 0) {
@@ -1508,7 +2087,7 @@ var Tadabut = class {
1508
2087
  } catch (error) {
1509
2088
  if (this.sessions.get(id) !== session) return false;
1510
2089
  this.setSaving(id, false);
1511
- this.showRowError(id, error instanceof Error ? error.message : this.texts.saveFailed);
2090
+ this.rowLines.showError(id, error instanceof Error ? error.message : this.texts.saveFailed);
1512
2091
  return false;
1513
2092
  }
1514
2093
  if (this.sessions.get(id) !== session) return false;
@@ -1517,6 +2096,7 @@ var Tadabut = class {
1517
2096
  this.findRow(id)?.data({ ...saved }).draw(false);
1518
2097
  this.findRow(id)?.node()?.classList.remove("tdb-row-editing");
1519
2098
  this.setSaving(id, false);
2099
+ this.rowLines.reload(id);
1520
2100
  this.onSaved?.(saved, { previous: session.originalData });
1521
2101
  return true;
1522
2102
  }
@@ -1535,7 +2115,7 @@ var Tadabut = class {
1535
2115
  const row = this.findRow(id);
1536
2116
  if (row === void 0 || this.sessions.has(id) || this.deleting.has(id)) return false;
1537
2117
  const data = row.data();
1538
- this.clearRowError(id);
2118
+ this.rowLines.clearError(id);
1539
2119
  this.deleting.add(id);
1540
2120
  this.setSaving(id, true);
1541
2121
  try {
@@ -1545,12 +2125,13 @@ var Tadabut = class {
1545
2125
  this.deleting.delete(id);
1546
2126
  this.setSaving(id, false);
1547
2127
  this.keepRow(id);
1548
- this.showRowError(id, error instanceof Error ? error.message : this.texts.deleteFailed);
2128
+ this.rowLines.showError(id, error instanceof Error ? error.message : this.texts.deleteFailed);
1549
2129
  return false;
1550
2130
  }
1551
2131
  if (!this.deleting.has(id)) return false;
1552
2132
  this.deleting.delete(id);
1553
2133
  this.findRow(id)?.remove().draw(false);
2134
+ this.rowLines.prune();
1554
2135
  this.selection?.prune();
1555
2136
  this.onDeleted?.(data);
1556
2137
  return true;
@@ -1564,6 +2145,7 @@ var Tadabut = class {
1564
2145
  this.destroySessions();
1565
2146
  this.deleting.clear();
1566
2147
  this.selection?.destroy();
2148
+ this.rowLines.destroy();
1567
2149
  this.dataTable.destroy();
1568
2150
  this.tableEl.remove();
1569
2151
  }
@@ -1594,14 +2176,6 @@ var Tadabut = class {
1594
2176
  tr.classList.toggle("tdb-row-saving", saving);
1595
2177
  }
1596
2178
  }
1597
- /** Shows a save or delete error in a child row under the row. */
1598
- showRowError(id, message) {
1599
- this.findRow(id)?.child(rowErrorMessage(message), "tdb-row-error").show();
1600
- }
1601
- /** Removes the save or delete error under a row, if any. */
1602
- clearRowError(id) {
1603
- this.findRow(id)?.child.remove();
1604
- }
1605
2179
  /** The last cell of a row, the one with the buttons. */
1606
2180
  actionsCell(row) {
1607
2181
  return this.dataTable.cell(row.index(), this.dtIndex(this.columns.length)).node();
@@ -1634,14 +2208,15 @@ var Tadabut = class {
1634
2208
  return this.newRow?.contains(element) ?? false;
1635
2209
  }
1636
2210
  /**
1637
- * `undefined` for rows DataTables doesn't know, like the error row or
1638
- * a table inside a custom editor.
2211
+ * `undefined` for rows DataTables doesn't know, like a table inside a custom
2212
+ * editor, and for the lines under a row (detail and error), which DataTables
2213
+ * maps back to their row.
1639
2214
  */
1640
2215
  rowIdOf(element) {
1641
2216
  const tr = element.closest("tr");
1642
2217
  if (tr === null || this.rowId === void 0) return;
1643
2218
  const row = this.dataTable.row(tr);
1644
- return row.any() ? row.data()[this.rowId] : void 0;
2219
+ return row.any() && row.node() === tr ? row.data()[this.rowId] : void 0;
1645
2220
  }
1646
2221
  /**
1647
2222
  * Handles the action buttons, the new row's too. One listener on the
@@ -1677,7 +2252,10 @@ var Tadabut = class {
1677
2252
  case "keep":
1678
2253
  this.keepRow(id);
1679
2254
  break;
1680
- case "confirm": this.delete(id);
2255
+ case "confirm":
2256
+ this.delete(id);
2257
+ break;
2258
+ case "detail": this.toggleDetail(id);
1681
2259
  }
1682
2260
  };
1683
2261
  /** Enter saves, Escape cancels, in edited rows and the new row. */
@@ -1718,20 +2296,24 @@ var Tadabut = class {
1718
2296
  else if (index === 0) cell.textContent = this.texts.total;
1719
2297
  });
1720
2298
  };
2299
+ /** Keeps the options a select loaded, for `getOptions`. An arrow function, so it can be passed as is. */
2300
+ rememberOptions = (prop, options) => {
2301
+ this.loadedOptions.set(prop, options);
2302
+ };
1721
2303
  /** Your column's index as DataTables counts it. */
1722
2304
  dtIndex(index) {
1723
2305
  return index + this.offset;
1724
2306
  }
1725
2307
  /**
1726
2308
  * 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.
2309
+ * inputs, links, the checkbox, ▸ and buttons cells, the new row, a row
2310
+ * being edited, the header, the footer, and the lines under a row.
1729
2311
  */
1730
2312
  clickedRow(event) {
1731
2313
  const target = event.target;
1732
2314
  if (!(target instanceof Element)) return;
1733
2315
  if (target.closest("button, input, select, textarea, label, a") !== null) return;
1734
- if (target.closest("td.tdb-select, td.tdb-actions") !== null) return;
2316
+ if (target.closest("td.tdb-select, td.tdb-detail, td.tdb-actions") !== null) return;
1735
2317
  if (this.isInNewRow(target)) return;
1736
2318
  const tr = target.closest("tr");
1737
2319
  if (tr === null || tr.classList.contains("tdb-row-editing")) return;
@@ -1776,19 +2358,25 @@ const fr = {
1776
2358
  keep: "Non",
1777
2359
  deleteFailed: "Impossible de supprimer.",
1778
2360
  total: "Total",
2361
+ selectRow: "Sélectionner la ligne",
2362
+ selectAll: "Tout sélectionner",
2363
+ showDetail: "Afficher les détails",
2364
+ hideDetail: "Masquer les détails",
2365
+ detailLoading: "Chargement…",
2366
+ detailFailed: "Impossible de charger les détails.",
1779
2367
  loading: "Chargement…",
1780
2368
  saveFailed: "Impossible d'enregistrer.",
1781
2369
  addFailed: "Impossible d'ajouter.",
1782
2370
  loadFailed: "Impossible de charger les options.",
1783
2371
  required: "Ce champ est obligatoire.",
1784
2372
  patternMismatch: "Format incorrect.",
1785
- selectRow: "Sélectionner la ligne",
1786
- selectAll: "Tout sélectionner",
1787
2373
  tooShort: (min) => `Au moins ${min} caractères.`,
1788
2374
  tooLong: (max) => `Au plus ${max} caractères.`,
1789
2375
  tooSmall: (min) => `Doit être supérieur ou égal à ${min}.`,
1790
2376
  tooLarge: (max) => `Doit être inférieur ou égal à ${max}.`,
1791
2377
  tooEarly: (min) => `Doit être le ${min} ou après.`,
1792
- tooLate: (max) => `Doit être le ${max} ou avant.`
2378
+ tooLate: (max) => `Doit être le ${max} ou avant.`,
2379
+ timeTooEarly: (min) => `Doit être à ${min} ou après.`,
2380
+ timeTooLate: (max) => `Doit être à ${max} ou avant.`
1793
2381
  };
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 };
2382
+ export { formatMoney as a, formatTime as c, formatDate as d, parseDate as f, defaultMoneyFormat as i, parseTime as l, Tadabut as n, formatDateTime as o, en as r, parseDateTime as s, fr as t, defaultDateFormat as u };