derive-dashboards 0.3.0 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (88) hide show
  1. package/README.md +6 -3
  2. package/dist/cli/check.mjs +36 -1
  3. package/dist/cli/doctor.mjs +2 -2
  4. package/dist/core/document/display-entries.mjs +48 -0
  5. package/dist/core/document/display.d.mts +15 -3
  6. package/dist/core/document/display.mjs +35 -11
  7. package/dist/core/engine/filter.mjs +4 -2
  8. package/dist/core/engine/version.d.mts +1 -1
  9. package/dist/core/engine/version.mjs +1 -1
  10. package/dist/core/error/codes.d.mts +2 -0
  11. package/dist/core/error/codes.mjs +2 -0
  12. package/dist/core/index.d.mts +4 -3
  13. package/dist/core/index.mjs +8 -7
  14. package/dist/core/model/catalog/check.mjs +11 -5
  15. package/dist/core/model/member.mjs +6 -1
  16. package/dist/core/model/validate/members.mjs +31 -1
  17. package/dist/core/oracle/visibility.mjs +3 -1
  18. package/dist/core/query/drill/line.mjs +2 -2
  19. package/dist/core/query/to-wire.d.mts +7 -0
  20. package/dist/core/query/to-wire.mjs +18 -7
  21. package/dist/core/result/additive.d.mts +9 -0
  22. package/dist/core/result/additive.mjs +59 -0
  23. package/dist/core/result/columns.mjs +8 -2
  24. package/dist/core/result/types.d.mts +12 -0
  25. package/dist/core/schema/answer-parts.mjs +2 -1
  26. package/dist/core/schema/answers.d.mts +6 -0
  27. package/dist/core/types/api.d.mts +1 -1
  28. package/dist/core/types/api.mjs +1 -1
  29. package/dist/handler.mjs +10 -2
  30. package/dist/index.mjs +1 -1
  31. package/dist/llms.txt +1 -1
  32. package/dist/react/History.mjs +1 -1
  33. package/dist/react/Tile.mjs +18 -10
  34. package/dist/react/builder/FilterEditor.mjs +2 -7
  35. package/dist/react/builder/Preview.mjs +1 -1
  36. package/dist/react/columns.mjs +15 -0
  37. package/dist/react/context.d.mts +7 -0
  38. package/dist/react/context.mjs +3 -0
  39. package/dist/react/digits.mjs +46 -0
  40. package/dist/react/drill/Bars.mjs +2 -1
  41. package/dist/react/drill/Controls.mjs +2 -1
  42. package/dist/react/drill/Header.mjs +2 -1
  43. package/dist/react/drill/Items.mjs +6 -1
  44. package/dist/react/drill/Notes.mjs +2 -1
  45. package/dist/react/drill/item-parts.mjs +2 -1
  46. package/dist/react/english.mjs +4 -0
  47. package/dist/react/export/ExportButton.mjs +23 -0
  48. package/dist/react/export/csv.d.mts +9 -0
  49. package/dist/react/export/csv.mjs +42 -0
  50. package/dist/react/export/download.d.mts +5 -0
  51. package/dist/react/export/download.mjs +61 -0
  52. package/dist/react/format-time.d.mts +15 -0
  53. package/dist/react/format-time.mjs +71 -0
  54. package/dist/react/format.d.mts +6 -10
  55. package/dist/react/format.mjs +55 -87
  56. package/dist/react/index.d.mts +10 -4
  57. package/dist/react/index.mjs +10 -4
  58. package/dist/react/messages.d.mts +8 -0
  59. package/dist/react/renderers/entries.mjs +15 -0
  60. package/dist/react/renderers/exact.d.mts +6 -0
  61. package/dist/react/renderers/exact.mjs +68 -0
  62. package/dist/react/renderers/index.d.mts +5 -5
  63. package/dist/react/renderers/index.mjs +72 -83
  64. package/dist/react/renderers/list.mjs +5 -17
  65. package/dist/react/renderers/table-cells.mjs +130 -0
  66. package/dist/react/renderers/table-fit.mjs +52 -0
  67. package/dist/react/renderers/table-head.mjs +47 -0
  68. package/dist/react/renderers/table-measure.mjs +49 -0
  69. package/dist/react/renderers/table-row.mjs +61 -0
  70. package/dist/react/renderers/table-scale.mjs +70 -0
  71. package/dist/react/renderers/table-sort.mjs +99 -0
  72. package/dist/react/renderers/table.d.mts +3 -0
  73. package/dist/react/renderers/table.mjs +121 -0
  74. package/dist/react/series-points.d.mts +18 -0
  75. package/dist/react/series-points.mjs +19 -0
  76. package/dist/react/series.d.mts +7 -10
  77. package/dist/react/series.mjs +7 -10
  78. package/dist/react/theme-unlayered.css +3144 -0
  79. package/dist/react/theme-unlayered.css.d.ts +1 -0
  80. package/dist/react/theme.css +379 -8
  81. package/dist/recharts/axes.mjs +13 -10
  82. package/dist/recharts/axis.mjs +25 -1
  83. package/dist/recharts/bar.d.mts +2 -1
  84. package/dist/recharts/bar.mjs +29 -6
  85. package/dist/recharts/parts.mjs +1 -1
  86. package/dist/recharts/table.mjs +10 -8
  87. package/dist/skills/derive-dashboards/SKILL.md +13 -1
  88. package/package.json +42 -4
@@ -1,23 +1,75 @@
1
+ import { isValue } from "../columns.mjs";
1
2
  import { formatCell } from "../format.mjs";
3
+ import { answerIds } from "./entries.mjs";
4
+ import { exactDigits, unitsOf } from "./exact.mjs";
2
5
  import { ListTile } from "./list.mjs";
3
- import { isMark, roleOf } from "derive-dashboards/core";
6
+ import { TableTile } from "./table.mjs";
7
+ import { displayOf, isMark, roleOf } from "derive-dashboards/core";
4
8
  import { jsx, jsxs } from "react/jsx-runtime";
5
9
  // src/renderers/index.tsx
6
- const isMeasure = (column) => roleOf(column) === "measure" || roleOf(column) === "calc";
7
10
  const isAll = (cell) => isMark(cell) && cell.mark === "all";
8
- /** A calc is what the query was built to show (a share, a rate); else its first value. */
9
- const shownIndex = (columns) => {
10
- const calc = columns.findIndex((c) => roleOf(c) === "calc" && c.signed !== true);
11
- return calc === -1 ? columns.findIndex((c) => isMeasure(c)) : calc;
12
- };
13
11
  /**
14
- * The first calc or value column, large, from the total row when there is one, else the first row;
15
- * a change calc (a signed column) sits under it.
12
+ * The column a number tile shows large: the entry its widget names, else a calc that is not a
13
+ * change (what the query was built to show: a share, a rate), else its first value. The column
14
+ * shown as the second figure is passed over, so a widget that names its query's one calc as
15
+ * the second figure keeps its measure large.
16
+ */
17
+ function shownIndex(columns, named, second) {
18
+ const asked = columns.findIndex((c) => c.id === named && isValue(c));
19
+ if (asked !== -1) return asked;
20
+ const free = (c, at) => at !== second && c.signed !== true;
21
+ const calc = columns.findIndex((c, at) => roleOf(c) === "calc" && free(c, at));
22
+ if (calc !== -1) return calc;
23
+ const value = columns.findIndex((c, at) => isValue(c) && free(c, at));
24
+ return value === -1 ? columns.findIndex((c) => isValue(c)) : value;
25
+ }
26
+ /**
27
+ * Which columns a number tile draws: the one shown large, the change under it (a signed calc),
28
+ * and the second figure its widget names. `display` names entries as the widget's query does,
29
+ * or as the answer does. A change is written once: it is never also the second figure, and it
30
+ * is not repeated under itself when the widget shows it large.
31
+ */
32
+ function figuresOf({ answer, widget }) {
33
+ const display = displayOf("number", widget.display);
34
+ const answerId = answerIds(widget);
35
+ const at = (entry) => entry === void 0 ? -1 : answer.columns.findIndex((c) => c.id === answerId(entry) && isValue(c));
36
+ const signed = answer.columns.findIndex((c) => c.signed === true);
37
+ const asked = at(display.secondary);
38
+ const second = answer.columns[asked]?.signed === true ? -1 : asked;
39
+ const named = display.value === void 0 ? void 0 : answerId(display.value);
40
+ const index = shownIndex(answer.columns, named, second);
41
+ return {
42
+ index,
43
+ second,
44
+ change: signed === index ? -1 : signed
45
+ };
46
+ }
47
+ /**
48
+ * A second, smaller figure under the number, beside its column's label ("30% Save rate"): the
49
+ * entry the widget names, when the answer has it and it is not the number itself.
50
+ */
51
+ function Secondary(props) {
52
+ const column = props.answer.columns[props.at];
53
+ if (column === void 0 || props.at === props.shown) return null;
54
+ return /* @__PURE__ */ jsxs("span", {
55
+ className: "dd-number-secondary",
56
+ children: [
57
+ /* @__PURE__ */ jsx("span", {
58
+ className: "dd-number-secondary-value",
59
+ children: formatCell(props.row?.[props.at] ?? null, column, props)
60
+ }),
61
+ " ",
62
+ column.label
63
+ ]
64
+ });
65
+ }
66
+ /**
67
+ * One value, large, from the total row when there is one, else the first row; a change calc (a
68
+ * signed column) sits under it, then a second figure when the widget names one.
16
69
  */
17
70
  function NumberTile(props) {
18
71
  const { answer } = props;
19
- const index = shownIndex(answer.columns);
20
- const change = answer.columns.findIndex((c) => c.signed === true);
72
+ const { index, second, change } = figuresOf(props);
21
73
  const row = answer.rows.find((cells) => cells.some((cell) => isAll(cell))) ?? answer.rows[0];
22
74
  const cell = row?.[index] ?? null;
23
75
  const entry = answer.columns[index]?.id;
@@ -38,6 +90,12 @@ function NumberTile(props) {
38
90
  /* @__PURE__ */ jsx(Before, {
39
91
  ...props,
40
92
  index
93
+ }),
94
+ /* @__PURE__ */ jsx(Secondary, {
95
+ ...props,
96
+ row,
97
+ at: second,
98
+ shown: index
41
99
  })
42
100
  ]
43
101
  });
@@ -47,18 +105,6 @@ const numberOf = (cell) => {
47
105
  if (typeof cell === "number") return cell;
48
106
  if (cell !== null && typeof cell === "object" && "v" in cell) return Number(cell.v);
49
107
  };
50
- const PLAIN_DECIMAL = /^-?\d+(\.\d+)?$/u;
51
- /** A cell's exact digits ("12.50"), when it has them: exact cells, and whole JS numbers. */
52
- const digitsOf = (cell) => {
53
- if (typeof cell === "number") return Number.isSafeInteger(cell) ? String(cell) : void 0;
54
- if (cell !== null && typeof cell === "object" && "v" in cell && typeof cell.v === "string") return PLAIN_DECIMAL.test(cell.v) ? cell.v : void 0;
55
- };
56
- /** A decimal as an integer of `scale` fraction digits: "12.5" at scale 2 is 1250n. */
57
- const scaled = (digits, scale) => {
58
- const negative = digits.startsWith("-");
59
- const [whole = "0", fraction = ""] = digits.replace("-", "").split(".");
60
- return BigInt(`${negative ? "-" : ""}${whole}${fraction.padEnd(scale, "0")}`);
61
- };
62
108
  /** now − was in floats, for values that are not exact digits. */
63
109
  function floatDifference(now, was) {
64
110
  const [x, y] = [numberOf(now), numberOf(was)];
@@ -71,7 +117,7 @@ function floatDifference(now, was) {
71
117
  /** a − b of two decimal strings, exactly, as a cell of `type`. */
72
118
  function exactDifference(a, b, type) {
73
119
  const scale = Math.max(a.split(".")[1]?.length ?? 0, b.split(".")[1]?.length ?? 0);
74
- const d = scaled(a, scale) - scaled(b, scale);
120
+ const d = unitsOf(a, scale) - unitsOf(b, scale);
75
121
  const abs = (d < 0n ? -d : d).toString().padStart(scale + 1, "0");
76
122
  const v = scale === 0 ? abs : `${abs.slice(0, -scale)}.${abs.slice(-scale)}`;
77
123
  return {
@@ -87,7 +133,7 @@ function exactDifference(a, b, type) {
87
133
  * floats; its sign and its size as a cell to format. Undefined when either is not a number.
88
134
  */
89
135
  function difference(now, was) {
90
- const [a, b] = [digitsOf(now), digitsOf(was)];
136
+ const [a, b] = [exactDigits(now), exactDigits(was)];
91
137
  if (a === void 0 || b === void 0) return floatDifference(now, was);
92
138
  return exactDifference(a, b, typeof now === "object" && now !== null && "t" in now ? now.t : "decimal");
93
139
  }
@@ -122,66 +168,9 @@ function Before(props) {
122
168
  children: text
123
169
  });
124
170
  }
125
- /** Cells as table cells: a run of equal marks is one cell spanning its columns. */
126
- function spans(cells) {
127
- const out = [];
128
- for (const [at, cell] of cells.entries()) {
129
- const last = out.at(-1);
130
- if (last !== void 0 && isMark(cell) && isMark(last.cell) && last.cell.mark === cell.mark) last.span += 1;
131
- else out.push({
132
- cell,
133
- at,
134
- span: 1
135
- });
136
- }
137
- return out;
138
- }
139
- /** One table row. A row that folds dimensions is a summary, labelled where it folds them. */
140
- function Row({ cells, columns, format, drill }) {
141
- const summary = cells.some((cell) => isMark(cell));
142
- const whole = drill?.props(cells);
143
- const cellMarks = (column) => whole === void 0 && column !== void 0 && isMeasure(column) ? drill?.props(cells, column.id) : void 0;
144
- return /* @__PURE__ */ jsx("tr", {
145
- className: summary ? "dd-table-summary" : void 0,
146
- ...whole,
147
- children: spans(cells).map(({ cell, at, span }) => {
148
- const column = columns[at];
149
- const numeric = column !== void 0 && isMeasure(column);
150
- if (isMark(cell)) return /* @__PURE__ */ jsx("th", {
151
- scope: "row",
152
- colSpan: span,
153
- children: formatCell(cell, column, format)
154
- }, column?.id ?? at);
155
- return /* @__PURE__ */ jsx("td", {
156
- "data-numeric": numeric,
157
- ...cellMarks(column),
158
- children: cell === null && column !== void 0 && !numeric ? format.messages.drillNone(column.label) : formatCell(cell, column, format)
159
- }, column?.id ?? at);
160
- })
161
- });
162
- }
163
- /** Every row and column: the groups, then Other and the total under them. */
164
- function TableTile(props) {
165
- const { answer } = props;
166
- const { columns } = answer;
167
- const keys = columns.filter((c) => !isMeasure(c)).length;
168
- return /* @__PURE__ */ jsxs("table", {
169
- className: "dd-table",
170
- children: [/* @__PURE__ */ jsx("thead", { children: /* @__PURE__ */ jsx("tr", { children: columns.map((c) => /* @__PURE__ */ jsx("th", {
171
- scope: "col",
172
- "data-numeric": isMeasure(c),
173
- children: c.label
174
- }, c.id)) }) }), /* @__PURE__ */ jsx("tbody", { children: answer.rows.map((cells) => /* @__PURE__ */ jsx(Row, {
175
- cells,
176
- columns,
177
- format: props,
178
- drill: props.drill
179
- }, JSON.stringify(cells.slice(0, keys)))) })]
180
- });
181
- }
182
171
  const BUILTIN_RENDERERS = {
183
172
  number: NumberTile,
184
173
  table: TableTile,
185
174
  list: ListTile
186
175
  };
187
- export { BUILTIN_RENDERERS, NumberTile, TableTile };
176
+ export { BUILTIN_RENDERERS, NumberTile, TableTile, exactDigits };
@@ -1,10 +1,11 @@
1
+ import { isValue } from "../columns.mjs";
1
2
  import { formatCell } from "../format.mjs";
2
3
  import { RowBody } from "../RowBody.mjs";
3
- import { displayOf, isMark, roleOf } from "derive-dashboards/core";
4
+ import { answerIds } from "./entries.mjs";
5
+ import { displayOf, isMark } from "derive-dashboards/core";
4
6
  import { jsx } from "react/jsx-runtime";
5
7
  // src/renderers/list.tsx
6
8
  const initials = (text) => text.split(/[\s._@-]+/u).filter(Boolean).slice(0, 2).map((word) => word[0]?.toUpperCase()).join("");
7
- const isValue = (c) => roleOf(c) === "measure" || roleOf(c) === "calc";
8
9
  /** With no display, the first group is the title, the others the subtitle, the last value the number. */
9
10
  function defaults(columns) {
10
11
  const groups = columns.filter((c) => !isValue(c)).map((c) => c.id);
@@ -15,23 +16,10 @@ function defaults(columns) {
15
16
  meta
16
17
  };
17
18
  }
18
- /**
19
- * The answer names a query's entries by place (m1.. measures, d1.. groups, c1.. calcs), as the
20
- * server runs it; `display` names them as the widget's query does. This maps one to the other.
21
- */
22
- function answerIds(widget) {
23
- const query = widget.queries.main;
24
- if (query === void 0) return /* @__PURE__ */ new Map();
25
- return new Map([
26
- ...query.measures.map((entry, i) => [entry.id, `m${String(i + 1)}`]),
27
- ...(query.dimensions ?? []).map((entry, i) => [entry.id, `d${String(i + 1)}`]),
28
- ...(query.calc ?? []).map((entry, i) => [entry.id, `c${String(i + 1)}`])
29
- ]);
30
- }
31
19
  function readDisplay(widget, columns) {
32
20
  const display = displayOf("list", widget.display);
33
- const ids = answerIds(widget);
34
- const id = (entry) => entry === void 0 ? void 0 : ids.get(entry) ?? entry;
21
+ const answerId = answerIds(widget);
22
+ const id = (entry) => entry === void 0 ? void 0 : answerId(entry);
35
23
  const fallback = defaults(columns);
36
24
  return {
37
25
  title: id(display.title) ?? fallback.title,
@@ -0,0 +1,130 @@
1
+ import { isNumeric, isValue } from "../columns.mjs";
2
+ import { formatCell, plotValue } from "../format.mjs";
3
+ import { depthOf, reachOf } from "./table-scale.mjs";
4
+ import { isMark } from "derive-dashboards/core";
5
+ import { jsx, jsxs } from "react/jsx-runtime";
6
+ // src/renderers/table-cells.tsx
7
+ /** The tone of a value's bar: the rest in grey, a value below zero in the warning colour, else the first series' colour. */
8
+ function toneOf(cell, other) {
9
+ if (other) return "other";
10
+ return (plotValue(cell) ?? 0) < 0 ? "negative" : "series";
11
+ }
12
+ /** A value with its bar: the bar in a track every row shares the length of, then the number in room every row shares. */
13
+ function Bar({ text, cell, at }) {
14
+ const reach = reachOf(cell, at.scale);
15
+ const vars = {
16
+ "--dd-fill": `${String(Math.round(reach * 1e3) / 10)}%`,
17
+ "--dd-chars": String(at.chars)
18
+ };
19
+ return /* @__PURE__ */ jsxs("span", {
20
+ className: "dd-table-bar",
21
+ "data-tone": toneOf(cell, at.other),
22
+ style: vars,
23
+ children: [/* @__PURE__ */ jsx("span", { className: "dd-table-bar-track" }), /* @__PURE__ */ jsx("span", {
24
+ className: "dd-table-bar-value",
25
+ children: text
26
+ })]
27
+ });
28
+ }
29
+ /** How deep a cell is shaded, as the style that says so; none for a column that is not shaded, or a cell with no value. */
30
+ function shadeOf(cell, how) {
31
+ const depth = how?.heat === void 0 ? void 0 : depthOf(cell, how.heat);
32
+ return depth === void 0 ? void 0 : { "--dd-heat": String(Math.round(depth * 100) / 100) };
33
+ }
34
+ /**
35
+ * What a cell carries only where the table is cards. There a table's own layout is gone, and
36
+ * with it, in some browsers, what a screen reader is told of it: so the cell says it is a cell,
37
+ * and its column's name is written before it for the eye alone, the header still naming it
38
+ * for a screen reader.
39
+ */
40
+ const inCard = (at) => at.cards ? { role: "cell" } : {};
41
+ /** The column's name before a card's cell. */
42
+ function Label({ column, at }) {
43
+ if (!at.cards) return null;
44
+ return /* @__PURE__ */ jsx("span", {
45
+ className: "dd-table-label",
46
+ "aria-hidden": "true",
47
+ children: column.label
48
+ });
49
+ }
50
+ /** What a value cell holds: its bar and number, or its number alone (in a card, as an element of its own, to sit opposite its name). */
51
+ function valueBody(text, cell, at, how) {
52
+ if (how?.bar === true) return /* @__PURE__ */ jsx(Bar, {
53
+ text,
54
+ cell,
55
+ at: {
56
+ ...how,
57
+ ...at
58
+ }
59
+ });
60
+ return at.cards ? /* @__PURE__ */ jsx("span", {
61
+ className: "dd-table-value",
62
+ children: text
63
+ }) : text;
64
+ }
65
+ /** A cell of a column of values: its number, with its bar or its shade when the column draws one. */
66
+ function ValueCell(props) {
67
+ const { cell, column, at, marks } = props;
68
+ const how = at.total || cell === null ? void 0 : at.measured.get(column.id);
69
+ const text = how?.texts.get(at.place) ?? formatCell(cell, column, at.format);
70
+ const shade = shadeOf(cell, how);
71
+ return /* @__PURE__ */ jsxs("td", {
72
+ "data-numeric": "true",
73
+ "data-empty": cell === null ? "true" : void 0,
74
+ "data-heat": shade === void 0 ? void 0 : "true",
75
+ style: shade,
76
+ ...inCard(at),
77
+ ...marks,
78
+ children: [/* @__PURE__ */ jsx(Label, {
79
+ column,
80
+ at
81
+ }), valueBody(text, cell, at, how)]
82
+ });
83
+ }
84
+ /** A cell of words: yes or no as a badge, anything else on one line, cut where it has no room. */
85
+ function WordCell({ cell, column, at }) {
86
+ if (typeof cell === "boolean") return /* @__PURE__ */ jsxs("td", {
87
+ ...inCard(at),
88
+ children: [/* @__PURE__ */ jsx(Label, {
89
+ column,
90
+ at
91
+ }), /* @__PURE__ */ jsx("span", {
92
+ className: "dd-badge",
93
+ "data-tone": cell ? "accent" : "neutral",
94
+ children: formatCell(cell, column, at.format)
95
+ })]
96
+ });
97
+ const text = cell === null ? at.format.messages.drillNone(column.label) : formatCell(cell, column, at.format);
98
+ return /* @__PURE__ */ jsxs("td", {
99
+ "data-empty": cell === null ? "true" : void 0,
100
+ ...inCard(at),
101
+ children: [/* @__PURE__ */ jsx(Label, {
102
+ column,
103
+ at
104
+ }), /* @__PURE__ */ jsx("span", {
105
+ className: "dd-table-text",
106
+ children: text
107
+ })]
108
+ });
109
+ }
110
+ /** One cell: a mark that folds dimensions as the row's heading, a value, or words. */
111
+ function Cell$1(props) {
112
+ const { cell, column, at } = props;
113
+ if (isMark(cell) || column === void 0) return /* @__PURE__ */ jsx("th", {
114
+ scope: "row",
115
+ colSpan: props.span,
116
+ role: at.cards ? "rowheader" : void 0,
117
+ children: formatCell(cell, column, at.format)
118
+ });
119
+ return isValue(column) || isNumeric(column) && cell !== null ? /* @__PURE__ */ jsx(ValueCell, {
120
+ cell,
121
+ column,
122
+ at,
123
+ marks: props.marks
124
+ }) : /* @__PURE__ */ jsx(WordCell, {
125
+ cell,
126
+ column,
127
+ at
128
+ });
129
+ }
130
+ export { Cell$1 as Cell };
@@ -0,0 +1,52 @@
1
+ import { useCallback, useEffect, useLayoutEffect, useRef, useState } from "react";
2
+ // src/renderers/table-fit.ts
3
+ /** A server has no layout to measure: there the effect waits for the browser. */
4
+ const useLayout = typeof document === "undefined" ? useEffect : useLayoutEffect;
5
+ /** How a table is drawn, from whether its columns are wider than its box and how wide the box is. */
6
+ const fitOf = (over, width) => {
7
+ if (!over) return "columns";
8
+ return width < 480 ? "cards" : "scroll";
9
+ };
10
+ /**
11
+ * How a table fits the box around it, measured after every draw and whenever the box changes
12
+ * size: the table's ref, and how it is drawn. A new `of` (another answer) is measured afresh,
13
+ * as columns first.
14
+ */
15
+ function useFit(of) {
16
+ const table = useRef(null);
17
+ const need = useRef(0);
18
+ const [fit, setFit] = useState("columns");
19
+ const [seen, setSeen] = useState(of);
20
+ if (seen !== of) {
21
+ setSeen(of);
22
+ setFit("columns");
23
+ }
24
+ const measure = useCallback(() => {
25
+ const box = table.current?.parentElement;
26
+ if (table.current === null || box === null || box === void 0) return;
27
+ if (table.current.dataset["layout"] !== "cards") need.current = box.scrollWidth;
28
+ setFit(fitOf(need.current > box.clientWidth, box.clientWidth));
29
+ }, []);
30
+ useLayout(measure);
31
+ useLayout(() => {
32
+ const box = table.current?.parentElement;
33
+ if (box === null || box === void 0 || typeof ResizeObserver === "undefined") return;
34
+ const observer = new ResizeObserver(measure);
35
+ observer.observe(box);
36
+ return () => {
37
+ observer.disconnect();
38
+ };
39
+ }, [measure]);
40
+ return [table, fit];
41
+ }
42
+ /**
43
+ * Gives a cut text its whole self as its title, when the pointer comes over it; a text that
44
+ * is all in sight gets none. Read from the element, since only the browser knows what was cut.
45
+ */
46
+ function nameCut(event) {
47
+ const { target } = event;
48
+ if (!(target instanceof HTMLElement) || !target.classList.contains("dd-table-text")) return;
49
+ if (target.scrollWidth > target.clientWidth) target.title = target.textContent;
50
+ else target.removeAttribute("title");
51
+ }
52
+ export { fitOf, nameCut, useFit };
@@ -0,0 +1,47 @@
1
+ import { isNumeric } from "../columns.mjs";
2
+ import { nextSort } from "./table-sort.mjs";
3
+ import { jsx, jsxs } from "react/jsx-runtime";
4
+ // src/renderers/table-head.tsx
5
+ /** A column's name past this many characters breaks over lines; a shorter one keeps to one. */
6
+ const LONG_NAME = 16;
7
+ /** A column's name as a button that sorts by it, with an arrow for the eye: `aria-sort` says the way to a screen reader. */
8
+ function SortButton({ column, sort, onSort, messages }) {
9
+ return /* @__PURE__ */ jsxs("button", {
10
+ type: "button",
11
+ className: "dd-table-sort",
12
+ title: messages.sortBy(column.label),
13
+ onClick: () => {
14
+ onSort?.(nextSort(sort, column));
15
+ },
16
+ children: [column.label, /* @__PURE__ */ jsx("span", {
17
+ className: "dd-table-sort-mark",
18
+ "aria-hidden": "true"
19
+ })]
20
+ });
21
+ }
22
+ /** The header row: each column's name, as a button that sorts by it when the rows can be sorted. */
23
+ function Head(props) {
24
+ const { columns, sort, cards } = props;
25
+ const sorts = props.onSort !== void 0 && !cards;
26
+ return /* @__PURE__ */ jsx("thead", {
27
+ role: cards ? "rowgroup" : void 0,
28
+ children: /* @__PURE__ */ jsx("tr", {
29
+ role: cards ? "row" : void 0,
30
+ children: columns.map((column) => {
31
+ const way = sorts && sort?.id === column.id ? sort.way : void 0;
32
+ return /* @__PURE__ */ jsx("th", {
33
+ scope: "col",
34
+ role: cards ? "columnheader" : void 0,
35
+ "data-numeric": isNumeric(column),
36
+ "data-long": column.label.length > LONG_NAME ? "true" : void 0,
37
+ "aria-sort": way,
38
+ children: sorts ? /* @__PURE__ */ jsx(SortButton, {
39
+ ...props,
40
+ column
41
+ }) : column.label
42
+ }, column.id);
43
+ })
44
+ })
45
+ });
46
+ }
47
+ export { Head };
@@ -0,0 +1,49 @@
1
+ import { isValue } from "../columns.mjs";
2
+ import { formatCell } from "../format.mjs";
3
+ import { answerIds } from "./entries.mjs";
4
+ import { isSummary, measurable, rangeOf, scaleOf } from "./table-scale.mjs";
5
+ import { displayOf } from "derive-dashboards/core";
6
+ // src/renderers/table-measure.ts
7
+ /** One column's measure: its scale over every row, and the values of the rows drawn as written, with the room the longest takes. */
8
+ function measure(asked, at, column, draws) {
9
+ const { rows } = asked.answer;
10
+ const places = asked.drawn ?? rows.map((_, place) => place);
11
+ const texts = new Map(places.map((place) => [place, formatCell(rows[place]?.[at] ?? null, column, asked)]));
12
+ const scale = scaleOf(rows, at, column);
13
+ return {
14
+ scale,
15
+ chars: Math.max(0, ...[...texts.values()].map((text) => text.length)),
16
+ texts,
17
+ bar: draws.bar && measurable(scale),
18
+ heat: draws.shaded ? rangeOf(rows, at) : void 0
19
+ };
20
+ }
21
+ /**
22
+ * The value columns a widget draws beyond their numbers, by column id. `display.bars` and
23
+ * `display.heat` name entries as the widget's query does, or as the answer does.
24
+ */
25
+ function measuredOf(asked) {
26
+ const display = displayOf("table", asked.widget.display);
27
+ const answerId = answerIds(asked.widget);
28
+ const bars = new Set((display.bars ?? []).map((entry) => answerId(entry)));
29
+ const heat = new Set((display.heat ?? []).map((entry) => answerId(entry)));
30
+ return new Map(asked.answer.columns.flatMap((column, at) => {
31
+ const draws = {
32
+ bar: bars.has(column.id),
33
+ shaded: heat.has(column.id)
34
+ };
35
+ if (!isValue(column) || !(draws.bar || draws.shaded)) return [];
36
+ return [[column.id, measure(asked, at, column, draws)]];
37
+ }));
38
+ }
39
+ /**
40
+ * The rows drawn, of the rows in a viewer's order: all of a table of `limit` rows or fewer,
41
+ * else the first `limit` of them, with the rows that fold dimensions (Other, a total) still
42
+ * under them.
43
+ */
44
+ function drawnOf(order, rows, limit) {
45
+ if (order.length <= limit) return order;
46
+ const folds = order.slice(limit).filter((place) => isSummary(rows[place] ?? []));
47
+ return [...order.slice(0, limit), ...folds];
48
+ }
49
+ export { drawnOf, measuredOf };
@@ -0,0 +1,61 @@
1
+ import { isValue } from "../columns.mjs";
2
+ import { Cell as Cell$1 } from "./table-cells.mjs";
3
+ import { isSummary, isTotal } from "./table-scale.mjs";
4
+ import { memo } from "react";
5
+ import { isMark } from "derive-dashboards/core";
6
+ import { jsx } from "react/jsx-runtime";
7
+ // src/renderers/table-row.tsx
8
+ /** Cells as table cells: a run of equal marks is one cell spanning its columns. */
9
+ function spans(cells) {
10
+ const out = [];
11
+ for (const [at, cell] of cells.entries()) {
12
+ const last = out.at(-1);
13
+ if (last !== void 0 && isMark(cell) && isMark(last.cell) && last.cell.mark === cell.mark) last.span += 1;
14
+ else out.push({
15
+ cell,
16
+ at,
17
+ span: 1
18
+ });
19
+ }
20
+ return out;
21
+ }
22
+ /** Which fold a row is, for its look: a total, the rest as Other, or neither. */
23
+ const markOf = (cells) => {
24
+ if (isTotal(cells)) return "all";
25
+ return isSummary(cells) ? "other" : void 0;
26
+ };
27
+ /** One table row. A row that folds dimensions is a summary, labelled where it folds them. */
28
+ function RowOf({ cells, columns, ruled, drill, ...at }) {
29
+ const mark = markOf(cells);
30
+ const whole = drill?.props(cells);
31
+ const cellMarks = (column) => whole === void 0 && column !== void 0 && isValue(column) ? drill?.props(cells, column.id) : void 0;
32
+ const cellAt = {
33
+ ...at,
34
+ total: mark === "all",
35
+ other: mark === "other"
36
+ };
37
+ return /* @__PURE__ */ jsx("tr", {
38
+ className: mark === void 0 ? void 0 : "dd-table-summary",
39
+ "data-mark": mark,
40
+ "data-rule": ruled ? "full" : void 0,
41
+ role: at.cards ? "row" : void 0,
42
+ ...whole,
43
+ children: spans(cells).map(({ cell, at: place, span }) => {
44
+ const column = columns[place];
45
+ return /* @__PURE__ */ jsx(Cell$1, {
46
+ cell,
47
+ column,
48
+ span,
49
+ at: cellAt,
50
+ marks: cellMarks(column)
51
+ }, column?.id ?? place);
52
+ })
53
+ });
54
+ }
55
+ /**
56
+ * A row is drawn again only when something of its own changes. A sort moves rows and changes
57
+ * none, so it draws none of them again: each takes what it is drawn with as it is, not in an
58
+ * object made anew at every draw.
59
+ */
60
+ const Row = memo(RowOf);
61
+ export { Row };
@@ -0,0 +1,70 @@
1
+ import { plotValue } from "../format.mjs";
2
+ import { exactDigits } from "./exact.mjs";
3
+ import { isMark } from "derive-dashboards/core";
4
+ // src/renderers/table-scale.ts
5
+ /** Whether a row folds dimensions: a total, or the rest as Other. */
6
+ const isSummary = (cells) => cells.some((cell) => isMark(cell));
7
+ /** Whether a row is a total: the sum of the rows above it, and not one of them. */
8
+ const isTotal = (cells) => cells.some((cell) => isMark(cell) && cell.mark === "all");
9
+ /** The whole a percent is a part of: 100 in percent points, 1 as a fraction. */
10
+ const wholeOf = (column) => column.fraction === true ? 1 : 100;
11
+ /** A cell's size as a float: nothing for a cell with no number, infinite for an exact amount past what a float holds. */
12
+ const sizeOf = (cell) => {
13
+ const digits = exactDigits(cell);
14
+ return Math.abs(digits === void 0 ? numberOf(cell) ?? 0 : Number(digits));
15
+ };
16
+ /** A cell's number, when it has one: a cell that is not a number has none, as an empty cell has none. */
17
+ const numberOf = (cell) => {
18
+ const value = plotValue(cell);
19
+ return value === null || Number.isNaN(value) ? null : value;
20
+ };
21
+ /**
22
+ * The size a column's values are measured against: the largest size among the rows that are
23
+ * not a total. A percent no row takes past the whole is measured against the whole, so a rate
24
+ * reads as how far it is to all of it, and two tables of rates compare. Past the whole (a
25
+ * change of 200%), a percent is measured as any other value is. Infinite when a value is past
26
+ * what a float holds: such a column is not measured (`measurable`).
27
+ */
28
+ function scaleOf(rows, at, column) {
29
+ const largest = rows.filter((cells) => !isTotal(cells)).map((cells) => sizeOf(cells[at] ?? null)).reduce((most, size) => Math.max(most, size), 0);
30
+ const whole = wholeOf(column);
31
+ return column.format === "percent" && largest <= whole ? whole : largest;
32
+ }
33
+ /** Whether a column can be measured for bars: a float holds its largest size. */
34
+ const measurable = (scale) => Number.isFinite(scale);
35
+ /**
36
+ * How far along its column's scale a value reaches, from 0 to 1: nothing for a cell with no
37
+ * value, or a column with nothing to measure against.
38
+ */
39
+ function reachOf(cell, scale) {
40
+ const value = numberOf(cell);
41
+ if (value === null || !(scale > 0) || !measurable(scale)) return 0;
42
+ return Math.min(1, Math.abs(value) / scale);
43
+ }
44
+ /** The least and the most of a column's values among the rows that are not a total; none when it has no value. */
45
+ function rangeOf(rows, at) {
46
+ const values = rows.filter((cells) => !isTotal(cells)).flatMap((cells) => numberOf(cells[at] ?? null) ?? []);
47
+ const [first] = values;
48
+ if (first === void 0) return void 0;
49
+ return values.reduce((range, value) => ({
50
+ least: Math.min(range.least, value),
51
+ most: Math.max(range.most, value)
52
+ }), {
53
+ least: first,
54
+ most: first
55
+ });
56
+ }
57
+ /**
58
+ * How deep a cell is shaded, from 0 to 1: where its value stands between its column's least
59
+ * and most, so the differences inside a column show whatever its size. A column of one value
60
+ * is shaded in full. Nothing for a cell with no value.
61
+ */
62
+ function depthOf(cell, range) {
63
+ const value = numberOf(cell);
64
+ if (value === null || range === void 0) return void 0;
65
+ const at = (span, from) => span > 0 ? Math.min(1, Math.max(0, from / span)) : 1;
66
+ const span = range.most - range.least;
67
+ if (Number.isFinite(span)) return at(span, value - range.least);
68
+ return at(range.most / 2 - range.least / 2, value / 2 - range.least / 2);
69
+ }
70
+ export { depthOf, isSummary, isTotal, measurable, rangeOf, reachOf, scaleOf };