@panneau/protocol 5.0.0-alpha.39 → 5.0.0-alpha.41

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 (40) hide show
  1. package/dist/conformance/index.d.ts.map +1 -1
  2. package/dist/conformance/index.js +29 -7
  3. package/dist/conformance/index.js.map +1 -1
  4. package/dist/index.js +4 -4
  5. package/dist/lint.d.ts.map +1 -1
  6. package/dist/lint.js +176 -20
  7. package/dist/lint.js.map +1 -1
  8. package/dist/mock/index.js +2 -2
  9. package/dist/mock/index.js.map +1 -1
  10. package/dist/openapi.d.ts.map +1 -1
  11. package/dist/openapi.js +3 -1
  12. package/dist/openapi.js.map +1 -1
  13. package/dist/periods.d.ts +12 -0
  14. package/dist/periods.d.ts.map +1 -1
  15. package/dist/periods.js +38 -1
  16. package/dist/periods.js.map +1 -1
  17. package/dist/runtime/index.d.ts +2 -2
  18. package/dist/runtime/index.d.ts.map +1 -1
  19. package/dist/runtime/index.js +3 -3
  20. package/dist/schemas/definition.d.ts +930 -2
  21. package/dist/schemas/definition.d.ts.map +1 -1
  22. package/dist/schemas/index.d.ts +4158 -4
  23. package/dist/schemas/index.d.ts.map +1 -1
  24. package/dist/schemas/index.js +14 -3
  25. package/dist/schemas/index.js.map +1 -1
  26. package/dist/schemas/resource.d.ts +464 -0
  27. package/dist/schemas/resource.d.ts.map +1 -1
  28. package/dist/schemas/theme.d.ts +2 -2
  29. package/dist/schemas/widget.d.ts +4210 -0
  30. package/dist/schemas/widget.d.ts.map +1 -1
  31. package/dist/schemas/widget.js +184 -17
  32. package/dist/schemas/widget.js.map +1 -1
  33. package/dist/widgets.d.ts +158 -7
  34. package/dist/widgets.d.ts.map +1 -1
  35. package/dist/widgets.js +469 -40
  36. package/dist/widgets.js.map +1 -1
  37. package/package.json +1 -1
  38. package/schema/boot.schema.json +459 -0
  39. package/schema/definition.schema.json +459 -0
  40. package/schema/openapi.json +976 -5
package/dist/widgets.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { isCurrencyCode } from "./currencies.js";
1
2
  import { isRecord } from "./values.js";
2
3
  import { fromSearchParams } from "./query.js";
3
4
  //#region src/widgets.ts
@@ -9,7 +10,9 @@ var BUILT_IN_WIDGET_TYPES = [
9
10
  "stat",
10
11
  "series",
11
12
  "list",
12
- "breakdown"
13
+ "breakdown",
14
+ "metric",
15
+ "table"
13
16
  ];
14
17
  /** The periods of the points of a `series` widget. */
15
18
  var WIDGET_SERIES_PERIODS = [
@@ -21,15 +24,115 @@ var WIDGET_SERIES_PERIODS = [
21
24
  var WIDGET_LIST_MAX_ITEMS = 5;
22
25
  /** The most items of a `breakdown` widget: the rest is its `other`. */
23
26
  var WIDGET_BREAKDOWN_MAX_ITEMS = 10;
27
+ /** The parts of a `metric` that clients draw as bars: the rest goes in "Other" (decision 0089). */
28
+ var WIDGET_METRIC_MAX_PARTS = 5;
29
+ /** The most lines of a `series` (decision 0089): 5 values of a field, and the rest. */
30
+ var WIDGET_SERIES_MAX_SERIES = 6;
31
+ /**
32
+ * The most lines a `series` on resources reads (decision 0089): its `sources`, or the values of
33
+ * its `split`, past which the rest is one line, "Other".
34
+ */
35
+ var WIDGET_SOURCES_MAX = 5;
36
+ /** The most rows of a `table` widget, and of its `rows` on resources (decision 0089). */
37
+ var WIDGET_TABLE_MAX_ROWS = 8;
38
+ /** The most columns of numbers of a `table` widget, after the column of the labels. */
39
+ var WIDGET_TABLE_MAX_COLUMNS = 6;
40
+ /** How the columns of a `table` align their numbers: `end` by default. */
41
+ var WIDGET_TABLE_ALIGNS = ["start", "end"];
42
+ /** Whether a rise or a fall of a `metric` or a `table` is good news (`settings.goodWhen`). */
43
+ var WIDGET_GOOD_WHEN = ["up", "down"];
44
+ /** The options of `Intl.NumberFormat` that a widget's `format` takes (decision 0089). */
45
+ var WIDGET_FORMAT_STYLES = [
46
+ "decimal",
47
+ "percent",
48
+ "currency",
49
+ "unit"
50
+ ];
51
+ var WIDGET_FORMAT_CURRENCY_DISPLAYS = [
52
+ "symbol",
53
+ "narrowSymbol",
54
+ "code",
55
+ "name"
56
+ ];
57
+ var WIDGET_FORMAT_UNIT_DISPLAYS = [
58
+ "short",
59
+ "narrow",
60
+ "long"
61
+ ];
62
+ var WIDGET_FORMAT_NOTATIONS = ["standard", "compact"];
63
+ var WIDGET_FORMAT_SIGN_DISPLAYS = [
64
+ "auto",
65
+ "always",
66
+ "exceptZero",
67
+ "never"
68
+ ];
69
+ /**
70
+ * The units of `Intl.NumberFormat` (the sanctioned units of ECMA-402): a widget's `format` takes
71
+ * one, or two joined by `-per-` (`kilometer-per-hour`).
72
+ */
73
+ var WIDGET_FORMAT_UNITS = [
74
+ "acre",
75
+ "bit",
76
+ "byte",
77
+ "celsius",
78
+ "centimeter",
79
+ "day",
80
+ "degree",
81
+ "fahrenheit",
82
+ "fluid-ounce",
83
+ "foot",
84
+ "gallon",
85
+ "gigabit",
86
+ "gigabyte",
87
+ "gram",
88
+ "hectare",
89
+ "hour",
90
+ "inch",
91
+ "kilobit",
92
+ "kilobyte",
93
+ "kilogram",
94
+ "kilometer",
95
+ "liter",
96
+ "megabit",
97
+ "megabyte",
98
+ "meter",
99
+ "microsecond",
100
+ "mile",
101
+ "mile-scandinavian",
102
+ "milliliter",
103
+ "millimeter",
104
+ "millisecond",
105
+ "minute",
106
+ "month",
107
+ "nanosecond",
108
+ "ounce",
109
+ "percent",
110
+ "petabyte",
111
+ "pound",
112
+ "second",
113
+ "stone",
114
+ "terabit",
115
+ "terabyte",
116
+ "week",
117
+ "yard",
118
+ "year"
119
+ ];
120
+ /** Whether a string is a unit of `Intl.NumberFormat`, or two joined by `-per-`. */
121
+ function isWidgetUnit(value) {
122
+ if (typeof value !== "string") return false;
123
+ const parts = value.split("-per-");
124
+ return parts.length <= 2 && parts.every((part) => WIDGET_FORMAT_UNITS.includes(part));
125
+ }
24
126
  /**
25
127
  * The built-in types that can read a resource (`settings.resource`): `stat` and `list` read its
26
- * list, `series` and `breakdown` its aggregates, and a `stat` with a `sum:` measure too.
128
+ * list, `series`, `breakdown` and `metric` its aggregates, and a `stat` with a `sum:` measure too.
27
129
  */
28
130
  var RESOURCE_WIDGET_TYPES = [
29
131
  "stat",
30
132
  "series",
31
133
  "list",
32
- "breakdown"
134
+ "breakdown",
135
+ "metric"
33
136
  ];
34
137
  /** The built-in types that read the aggregates of a resource, grouped by `settings.group`. */
35
138
  var GROUPED_WIDGET_TYPES = ["series", "breakdown"];
@@ -48,7 +151,7 @@ function aggregateOf(widget) {
48
151
  const group = widget.settings?.group;
49
152
  const grouped = GROUPED_WIDGET_TYPES.includes(widget.type);
50
153
  const sums = typeof measure === "string" && measure.startsWith("sum:");
51
- if (!grouped && !(widget.type === "stat" && sums)) return null;
154
+ if (!grouped && !(widget.type === "stat" && sums) && widget.type !== "metric") return null;
52
155
  return {
53
156
  measure: typeof measure === "string" && measure !== "" ? measure : "count",
54
157
  group: grouped && typeof group === "string" && group !== "" ? group : null
@@ -61,14 +164,7 @@ function aggregateOf(widget) {
61
164
  function getWidgetResource(widget) {
62
165
  const resource = widget.settings?.resource;
63
166
  if (!RESOURCE_WIDGET_TYPES.includes(widget.type) || typeof resource !== "string" || resource === "") return null;
64
- const params = new URLSearchParams();
65
- const query = widget.settings?.query;
66
- if (isRecord(query)) Object.entries(query).forEach(([key, value]) => {
67
- if (WIDGET_RESERVED_QUERY_PARAMS.includes(key)) return;
68
- (Array.isArray(value) ? value : [value]).forEach((entry) => {
69
- if (typeof entry === "string" || typeof entry === "number" || typeof entry === "boolean") params.append(key, String(entry));
70
- });
71
- });
167
+ const params = paramsOf(widget.settings?.query);
72
168
  return {
73
169
  resource,
74
170
  params,
@@ -76,6 +172,164 @@ function getWidgetResource(widget) {
76
172
  aggregate: aggregateOf(widget)
77
173
  };
78
174
  }
175
+ /** The query of a widget or of a source, as query parameters, without the client's own. */
176
+ function paramsOf(query, into = new URLSearchParams()) {
177
+ if (isRecord(query)) Object.entries(query).forEach(([key, value]) => {
178
+ if (WIDGET_RESERVED_QUERY_PARAMS.includes(key)) return;
179
+ into.delete(key);
180
+ (Array.isArray(value) ? value : [value]).forEach((entry) => {
181
+ if (typeof entry === "string" || typeof entry === "number" || typeof entry === "boolean") into.append(key, String(entry));
182
+ });
183
+ });
184
+ return into;
185
+ }
186
+ var stringOr = (value, fallback) => typeof value === "string" && value !== "" ? value : fallback;
187
+ /**
188
+ * The sources of a widget on several resources (decision 0089): the `sources` of a `series`, at
189
+ * most `WIDGET_SOURCES_MAX`, or the `rows` of a `table`, at most `WIDGET_TABLE_MAX_ROWS`. Each one
190
+ * takes the `group` (`points` for a row) and the `period` of the widget when it has none. Empty for
191
+ * other widgets.
192
+ */
193
+ function getWidgetSources(widget) {
194
+ const settings = widget.settings ?? {};
195
+ const isSeries = widget.type === "series" && Array.isArray(settings.sources);
196
+ const isTable = widget.type === "table" && Array.isArray(settings.rows);
197
+ if (!isSeries && !isTable) return [];
198
+ const entries = isSeries ? settings.sources : settings.rows;
199
+ const fallbackGroup = stringOr(isSeries ? settings.group : settings.points, null);
200
+ const fallbackPeriod = settings.period === false ? false : stringOr(settings.period, null);
201
+ return entries.slice(0, isSeries ? 5 : 8).flatMap((entry) => {
202
+ if (!isRecord(entry) || typeof entry.resource !== "string" || entry.resource === "") return [];
203
+ const params = paramsOf(entry.query);
204
+ const alert = isRecord(entry.alert) ? entry.alert : null;
205
+ const alertParams = alert !== null ? paramsOf(alert.query, new URLSearchParams(params)) : null;
206
+ return [{
207
+ resource: entry.resource,
208
+ label: stringOr(entry.label, entry.resource),
209
+ params,
210
+ query: fromSearchParams(params),
211
+ measure: stringOr(entry.measure, "count"),
212
+ group: stringOr(isSeries ? entry.group : entry.points, fallbackGroup),
213
+ period: entry.period === false ? false : stringOr(entry.period, fallbackPeriod),
214
+ help: stringOr(entry.help, null),
215
+ tone: stringOr(entry.tone, null),
216
+ alert: alert !== null && alertParams !== null ? {
217
+ params: alertParams,
218
+ query: fromSearchParams(alertParams),
219
+ label: stringOr(alert.label, ""),
220
+ tone: stringOr(alert.tone, "danger")
221
+ } : null
222
+ }];
223
+ });
224
+ }
225
+ /**
226
+ * Whether a built-in widget reads resources rather than data of its own at
227
+ * `GET /dashboard/widgets/{widget}`: `settings.resource`, or the `sources` of a `series` or the
228
+ * `rows` of a `table` (decision 0089).
229
+ */
230
+ function readsResources(widget) {
231
+ const settings = widget.settings ?? {};
232
+ return getWidgetResource(widget) !== null || widget.type === "series" && Array.isArray(settings.sources) || widget.type === "table" && Array.isArray(settings.rows);
233
+ }
234
+ /**
235
+ * Whether a widget combines several reads of the aggregates of resources into one card (decision
236
+ * 0089): a `metric` on a resource, a `series` with `split` or `sources`, a `table` with `rows`.
237
+ */
238
+ function isCombinedWidget(widget) {
239
+ const settings = widget.settings ?? {};
240
+ if (widget.type === "metric") return getWidgetResource(widget) !== null;
241
+ if (widget.type === "series") return Array.isArray(settings.sources) || getWidgetResource(widget) !== null && stringOr(settings.split, null) !== null;
242
+ return widget.type === "table" && Array.isArray(settings.rows);
243
+ }
244
+ /**
245
+ * The reads of the aggregates that a combined widget makes, whatever the period (decision 0089).
246
+ * A client adds, with a period, the `value` of each source over the previous period, and for a
247
+ * `split`, a line per value of its `parts`, filtered by it.
248
+ *
249
+ * - `metric`: its `value`, its `points` with `settings.points`, its `parts` with `settings.by`.
250
+ * - `series` with `split`: its total line (`points`), and the `parts` of the field it splits by.
251
+ * - `series` with `sources`: a line per source (`points`), and the count of its alert.
252
+ * - `table` with `rows`: per row, its `value`, its `points`, and the count of its alert.
253
+ */
254
+ function getWidgetAggregates(widget) {
255
+ if (!isCombinedWidget(widget)) return [];
256
+ const settings = widget.settings ?? {};
257
+ const own = getWidgetResource(widget);
258
+ const ownPeriod = settings.period === false ? false : stringOr(settings.period, null);
259
+ if (own !== null && own.aggregate !== null) {
260
+ const base = {
261
+ source: null,
262
+ resource: own.resource,
263
+ query: own.query,
264
+ measure: own.aggregate.measure,
265
+ period: ownPeriod
266
+ };
267
+ if (widget.type === "metric") {
268
+ const points = stringOr(settings.points, null);
269
+ const by = stringOr(settings.by, null);
270
+ return [
271
+ {
272
+ ...base,
273
+ role: "value",
274
+ group: null
275
+ },
276
+ ...points !== null ? [{
277
+ ...base,
278
+ role: "points",
279
+ group: points
280
+ }] : [],
281
+ ...by !== null ? [{
282
+ ...base,
283
+ role: "parts",
284
+ group: by
285
+ }] : []
286
+ ];
287
+ }
288
+ return [{
289
+ ...base,
290
+ role: "points",
291
+ group: own.aggregate.group
292
+ }, {
293
+ ...base,
294
+ role: "parts",
295
+ group: stringOr(settings.split, null)
296
+ }];
297
+ }
298
+ return getWidgetSources(widget).flatMap((source, index) => {
299
+ const base = {
300
+ source: index,
301
+ resource: source.resource,
302
+ query: source.query,
303
+ measure: source.measure,
304
+ period: source.period
305
+ };
306
+ const alert = source.alert !== null ? [{
307
+ ...base,
308
+ role: "alert",
309
+ query: source.alert.query,
310
+ measure: "count",
311
+ group: null
312
+ }] : [];
313
+ if (widget.type === "series") return [{
314
+ ...base,
315
+ role: "points",
316
+ group: source.group
317
+ }, ...alert];
318
+ return [
319
+ {
320
+ ...base,
321
+ role: "value",
322
+ group: null
323
+ },
324
+ ...source.group !== null ? [{
325
+ ...base,
326
+ role: "points",
327
+ group: source.group
328
+ }] : [],
329
+ ...alert
330
+ ];
331
+ });
332
+ }
79
333
  /**
80
334
  * Whether a widget of the summary of a resource follows its list (decision 0084), and gets its
81
335
  * current filters and search: a widget on that resource, or with data of its own, unless its
@@ -83,8 +337,8 @@ function getWidgetResource(widget) {
83
337
  */
84
338
  function followsList(widget, resource) {
85
339
  if (widget.settings?.followsList === false) return false;
86
- const read = getWidgetResource(widget);
87
- return read === null || read.resource === resource;
340
+ if (!readsResources(widget)) return true;
341
+ return getWidgetResource(widget)?.resource === resource || getWidgetSources(widget).some((source) => source.resource === resource);
88
342
  }
89
343
  /**
90
344
  * Whether a widget follows the period of its overview (decision 0084): a built-in widget on a
@@ -95,7 +349,7 @@ function followsList(widget, resource) {
95
349
  function followsPeriod(widget, summaryOf) {
96
350
  const period = widget.settings?.period;
97
351
  if (period === false) return false;
98
- return getWidgetResource(widget) === null || typeof period === "string" && period !== "" || summaryOf !== void 0 && followsList(widget, summaryOf);
352
+ return !readsResources(widget) || typeof period === "string" && period !== "" || getWidgetSources(widget).some((source) => typeof source.period === "string") || summaryOf !== void 0 && followsList(widget, summaryOf);
99
353
  }
100
354
  var DATE = /^\d{4}-\d{2}-\d{2}$/;
101
355
  var TONE = /^[a-z0-9-]+$/;
@@ -107,6 +361,73 @@ function optional(data, key, check) {
107
361
  var isString = (value) => typeof value === "string";
108
362
  var isTone = (value) => isString(value) && TONE.test(value);
109
363
  var isKey = (value) => isString(value) || isNumber(value) || typeof value === "boolean";
364
+ var ENUM_FORMAT_KEYS = {
365
+ style: WIDGET_FORMAT_STYLES,
366
+ currencyDisplay: WIDGET_FORMAT_CURRENCY_DISPLAYS,
367
+ unitDisplay: WIDGET_FORMAT_UNIT_DISPLAYS,
368
+ notation: WIDGET_FORMAT_NOTATIONS,
369
+ signDisplay: WIDGET_FORMAT_SIGN_DISPLAYS
370
+ };
371
+ var isDigits = (value) => typeof value === "number" && Number.isInteger(value) && value >= 0 && value <= 20;
372
+ /** The keys of a format that are wrong: an option out of its values, a style without its unit. */
373
+ function formatErrors(value) {
374
+ if (!isRecord(value)) return [""];
375
+ const wrong = Object.entries(ENUM_FORMAT_KEYS).filter(([key, values]) => !optional(value, key, (entry) => values.includes(entry))).map(([key]) => key);
376
+ if (!optional(value, "currency", isCurrencyCode)) wrong.push("currency");
377
+ if (!optional(value, "unit", isWidgetUnit)) wrong.push("unit");
378
+ ["minimumFractionDigits", "maximumFractionDigits"].forEach((key) => {
379
+ if (!optional(value, key, isDigits)) wrong.push(key);
380
+ });
381
+ if (value.style === "currency" && !isCurrencyCode(value.currency)) wrong.push("currency");
382
+ if (value.style === "unit" && !isWidgetUnit(value.unit)) wrong.push("unit");
383
+ if (isDigits(value.minimumFractionDigits) && isDigits(value.maximumFractionDigits) && value.minimumFractionDigits > value.maximumFractionDigits) wrong.push("minimumFractionDigits");
384
+ return [...new Set(wrong)];
385
+ }
386
+ /**
387
+ * The format of the numbers of a widget (decision 0089): the options of `Intl.NumberFormat` among
388
+ * those a widget takes, with valid values, or `undefined` without any. A style without its
389
+ * `currency` or its `unit` is left out, as an unknown key. The `format` of the data, then the
390
+ * `format` of the settings over it, key by key: `readWidgetFormat(data.format, settings.format)`.
391
+ */
392
+ function readWidgetFormat(...formats) {
393
+ const merged = {};
394
+ formats.forEach((format) => {
395
+ if (isRecord(format)) Object.assign(merged, format);
396
+ });
397
+ const wrong = new Set(formatErrors(merged));
398
+ const format = {};
399
+ [
400
+ ...Object.keys(ENUM_FORMAT_KEYS),
401
+ "currency",
402
+ "unit",
403
+ "minimumFractionDigits",
404
+ "maximumFractionDigits"
405
+ ].forEach((key) => {
406
+ if (merged[key] !== void 0 && merged[key] !== null && !wrong.has(key)) format[key] = merged[key];
407
+ });
408
+ if (format.style === "currency" && format.currency === void 0 || format.style === "unit" && format.unit === void 0) delete format.style;
409
+ if (wrong.has("minimumFractionDigits")) delete format.maximumFractionDigits;
410
+ return Object.keys(format).length > 0 ? format : void 0;
411
+ }
412
+ function checkFormat(data, path = "format") {
413
+ return data[path] === void 0 || data[path] === null ? [] : formatErrors(data[path]).map((key) => key === "" ? path : `${path}.${key}`);
414
+ }
415
+ /** The paths of the wrong points of a line: a date that doesn't go up, a value that isn't a number. */
416
+ function checkPoints(points, path) {
417
+ if (!Array.isArray(points)) return [path];
418
+ const wrong = [];
419
+ let previous = "";
420
+ points.forEach((point, index) => {
421
+ if (!isRecord(point)) {
422
+ wrong.push(`${path}.${index}`);
423
+ return;
424
+ }
425
+ if (!isString(point.date) || !DATE.test(point.date) || point.date <= previous) wrong.push(`${path}.${index}.date`);
426
+ else previous = point.date;
427
+ if (!isNumber(point.value)) wrong.push(`${path}.${index}.value`);
428
+ });
429
+ return wrong;
430
+ }
110
431
  function checkStat(data) {
111
432
  const wrong = [];
112
433
  if (!isNumber(data.value)) wrong.push("value");
@@ -114,26 +435,42 @@ function checkStat(data) {
114
435
  if (!optional(data, "tone", isTone)) wrong.push("tone");
115
436
  if (!optional(data, "url", isString)) wrong.push("url");
116
437
  if (!optional(data, "target", (value) => isNumber(value) && value > 0)) wrong.push("target");
117
- return wrong;
438
+ return [...wrong, ...checkFormat(data)];
439
+ }
440
+ function checkAlert(alert, path) {
441
+ if (alert === void 0 || alert === null) return [];
442
+ if (!isRecord(alert)) return [path];
443
+ return [
444
+ ...!isNumber(alert.value) ? [`${path}.value`] : [],
445
+ ...!isString(alert.label) ? [`${path}.label`] : [],
446
+ ...!optional(alert, "tone", isTone) ? [`${path}.tone`] : []
447
+ ];
118
448
  }
119
449
  function checkSeries(data) {
120
450
  const wrong = [];
121
451
  if (!WIDGET_SERIES_PERIODS.includes(data.period)) wrong.push("period");
122
- if (!Array.isArray(data.points)) wrong.push("points");
123
- else {
124
- let previous = "";
125
- data.points.forEach((point, index) => {
126
- if (!isRecord(point)) {
127
- wrong.push(`points.${index}`);
452
+ wrong.push(...checkPoints(data.points, "points"));
453
+ if (data.series !== void 0) {
454
+ if (!Array.isArray(data.series) || data.series.length > 6) wrong.push("series");
455
+ else data.series.forEach((series, index) => {
456
+ const path = `series.${index}`;
457
+ if (!isRecord(series)) {
458
+ wrong.push(path);
128
459
  return;
129
460
  }
130
- if (!isString(point.date) || !DATE.test(point.date) || point.date <= previous) wrong.push(`points.${index}.date`);
131
- else previous = point.date;
132
- if (!isNumber(point.value)) wrong.push(`points.${index}.value`);
461
+ if (!isString(series.label)) wrong.push(`${path}.label`);
462
+ wrong.push(...checkPoints(series.points, `${path}.points`));
463
+ if (!optional(series, "total", isNumber)) wrong.push(`${path}.total`);
464
+ if (!optional(series, "tone", isTone)) wrong.push(`${path}.tone`);
465
+ ["url", "help"].forEach((key) => {
466
+ if (!optional(series, key, isString)) wrong.push(`${path}.${key}`);
467
+ });
468
+ if (!optional(series, "key", isKey)) wrong.push(`${path}.key`);
469
+ wrong.push(...checkAlert(series.alert, `${path}.alert`));
133
470
  });
134
471
  }
135
472
  if (!optional(data, "total", isNumber)) wrong.push("total");
136
- return wrong;
473
+ return [...wrong, ...checkFormat(data)];
137
474
  }
138
475
  function checkList(data) {
139
476
  const wrong = [];
@@ -161,29 +498,83 @@ function checkList(data) {
161
498
  if (!optional(data, "url", isString)) wrong.push("url");
162
499
  return wrong;
163
500
  }
164
- function checkBreakdown(data) {
501
+ /** The parts of a `breakdown` (`items`) or of a `metric` (`parts`). */
502
+ function checkParts(parts, path) {
503
+ if (!Array.isArray(parts) || parts.length > 10) return [path];
165
504
  const wrong = [];
166
- if (!Array.isArray(data.items) || data.items.length > 10) wrong.push("items");
167
- else data.items.forEach((item, index) => {
505
+ parts.forEach((item, index) => {
168
506
  if (!isRecord(item)) {
169
- wrong.push(`items.${index}`);
507
+ wrong.push(`${path}.${index}`);
170
508
  return;
171
509
  }
172
- if (!isString(item.label)) wrong.push(`items.${index}.label`);
173
- if (!isNumber(item.value)) wrong.push(`items.${index}.value`);
174
- if (!optional(item, "tone", isTone)) wrong.push(`items.${index}.tone`);
175
- if (!optional(item, "url", isString)) wrong.push(`items.${index}.url`);
176
- if (!optional(item, "key", isKey)) wrong.push(`items.${index}.key`);
510
+ if (!isString(item.label)) wrong.push(`${path}.${index}.label`);
511
+ if (!isNumber(item.value)) wrong.push(`${path}.${index}.value`);
512
+ if (!optional(item, "tone", isTone)) wrong.push(`${path}.${index}.tone`);
513
+ if (!optional(item, "url", isString)) wrong.push(`${path}.${index}.url`);
514
+ if (!optional(item, "key", isKey)) wrong.push(`${path}.${index}.key`);
177
515
  });
516
+ return wrong;
517
+ }
518
+ function checkBreakdown(data) {
519
+ const wrong = checkParts(data.items, "items");
178
520
  if (!optional(data, "total", isNumber)) wrong.push("total");
179
521
  if (!optional(data, "other", isNumber)) wrong.push("other");
522
+ return [...wrong, ...checkFormat(data)];
523
+ }
524
+ function checkMetric(data) {
525
+ const wrong = checkStat(data);
526
+ if (!optional(data, "previous", isNumber)) wrong.push("previous");
527
+ if (data.points !== void 0 && data.points !== null) wrong.push(...checkPoints(data.points, "points"));
528
+ if (!optional(data, "period", (value) => WIDGET_SERIES_PERIODS.includes(value))) wrong.push("period");
529
+ if (data.parts !== void 0 && data.parts !== null) wrong.push(...checkParts(data.parts, "parts"));
530
+ if (!optional(data, "other", isNumber)) wrong.push("other");
180
531
  return wrong;
181
532
  }
533
+ var isCell = (cell) => cell === null || isNumber(cell) || isRecord(cell) && (cell.value === null || isNumber(cell.value)) && optional(cell, "tone", isTone) && optional(cell, "help", isString) && checkFormat(cell).length === 0;
534
+ function checkTable(data) {
535
+ const wrong = [];
536
+ if (!Array.isArray(data.columns) || data.columns.length === 0 || data.columns.length > 6) wrong.push("columns");
537
+ else {
538
+ const keys = /* @__PURE__ */ new Set();
539
+ data.columns.forEach((column, index) => {
540
+ const path = `columns.${index}`;
541
+ if (!isRecord(column)) {
542
+ wrong.push(path);
543
+ return;
544
+ }
545
+ if (!isString(column.key) || column.key === "" || keys.has(column.key)) wrong.push(`${path}.key`);
546
+ else keys.add(column.key);
547
+ if (!isString(column.label)) wrong.push(`${path}.label`);
548
+ if (!optional(column, "align", (value) => WIDGET_TABLE_ALIGNS.includes(value))) wrong.push(`${path}.align`);
549
+ wrong.push(...checkFormat(column).map((key) => `${path}.${key}`));
550
+ });
551
+ }
552
+ if (!Array.isArray(data.rows) || data.rows.length > 8) wrong.push("rows");
553
+ else data.rows.forEach((row, index) => {
554
+ const path = `rows.${index}`;
555
+ if (!isRecord(row)) {
556
+ wrong.push(path);
557
+ return;
558
+ }
559
+ if (!isString(row.label)) wrong.push(`${path}.label`);
560
+ if (!optional(row, "url", isString)) wrong.push(`${path}.url`);
561
+ if (!isRecord(row.cells)) wrong.push(`${path}.cells`);
562
+ else Object.entries(row.cells).forEach(([key, cell]) => {
563
+ if (!isCell(cell)) wrong.push(`${path}.cells.${key}`);
564
+ });
565
+ if (row.points !== void 0 && row.points !== null) wrong.push(...checkPoints(row.points, `${path}.points`));
566
+ });
567
+ if (!optional(data, "period", (value) => WIDGET_SERIES_PERIODS.includes(value))) wrong.push("period");
568
+ ["empty", "url"].forEach((key) => {
569
+ if (!optional(data, key, isString)) wrong.push(key);
570
+ });
571
+ return [...wrong, ...checkFormat(data)];
572
+ }
182
573
  /**
183
- * Checks the data of a widget against the shape of its type (decisions 0064 and 0084), without the
184
- * schemas: the paths of the wrong keys (`value`, `points.2.date`), empty when it is right. The data
185
- * of a widget of a plugin only has to be an object. The dates of a series must go up, one point per
186
- * period.
574
+ * Checks the data of a widget against the shape of its type (decisions 0064, 0084 and 0089),
575
+ * without the schemas: the paths of the wrong keys (`value`, `points.2.date`), empty when it is
576
+ * right. The data of a widget of a plugin only has to be an object. The dates of a series must go
577
+ * up, one point per period.
187
578
  */
188
579
  function checkWidgetData(type, data) {
189
580
  if (!isRecord(data)) return [""];
@@ -192,10 +583,48 @@ function checkWidgetData(type, data) {
192
583
  case "series": return checkSeries(data);
193
584
  case "list": return checkList(data);
194
585
  case "breakdown": return checkBreakdown(data);
586
+ case "metric": return checkMetric(data);
587
+ case "table": return checkTable(data);
195
588
  default: return [];
196
589
  }
197
590
  }
591
+ var allZero = (points) => !Array.isArray(points) || points.every((point) => !isRecord(point) || point.value === 0);
592
+ /**
593
+ * Whether the data of a built-in widget has nothing to show (decision 0089), for
594
+ * `settings.hideEmpty`: a `stat` at 0, a `metric` at 0 that was at 0 or has no previous value, a
595
+ * `series` whose points are all 0, a `breakdown` whose parts and rest are all 0, a `list` without
596
+ * items, a `table` without rows. Data of plugins is never empty.
597
+ */
598
+ function isWidgetDataEmpty(type, data) {
599
+ if (!isRecord(data)) return false;
600
+ switch (type) {
601
+ case "stat": return data.value === 0;
602
+ case "metric": return data.value === 0 && (data.previous ?? 0) === 0;
603
+ case "series": return Array.isArray(data.series) ? data.series.every((series) => !isRecord(series) || allZero(series.points)) : allZero(data.points);
604
+ case "breakdown": return allZero(data.items) && (data.other ?? 0) === 0;
605
+ case "list": return Array.isArray(data.items) && data.items.length === 0;
606
+ case "table": return Array.isArray(data.rows) && data.rows.length === 0;
607
+ default: return false;
608
+ }
609
+ }
610
+ /** The change of a number from its value over the previous period (decision 0089). */
611
+ function getWidgetChange(value, previous) {
612
+ const difference = value - previous;
613
+ return {
614
+ difference,
615
+ ratio: previous !== 0 ? difference / Math.abs(previous) : null,
616
+ direction: difference > 0 ? "up" : difference < 0 ? "down" : "none"
617
+ };
618
+ }
619
+ /**
620
+ * The tone of a change (decision 0089): `success` when it goes the way `goodWhen` says is good,
621
+ * `danger` the other way, and none without `goodWhen` or without a change.
622
+ */
623
+ function getChangeTone(direction, goodWhen) {
624
+ if (direction === "none" || goodWhen !== "up" && goodWhen !== "down") return null;
625
+ return direction === goodWhen ? "success" : "danger";
626
+ }
198
627
  //#endregion
199
- export { BUILT_IN_WIDGET_TYPES, GROUPED_WIDGET_TYPES, RESOURCE_WIDGET_TYPES, WIDGET_BREAKDOWN_MAX_ITEMS, WIDGET_LIST_MAX_ITEMS, WIDGET_RESERVED_QUERY_PARAMS, WIDGET_SERIES_PERIODS, checkWidgetData, followsList, followsPeriod, getWidgetResource, isBuiltInWidgetType };
628
+ export { BUILT_IN_WIDGET_TYPES, GROUPED_WIDGET_TYPES, RESOURCE_WIDGET_TYPES, WIDGET_BREAKDOWN_MAX_ITEMS, WIDGET_FORMAT_CURRENCY_DISPLAYS, WIDGET_FORMAT_NOTATIONS, WIDGET_FORMAT_SIGN_DISPLAYS, WIDGET_FORMAT_STYLES, WIDGET_FORMAT_UNITS, WIDGET_FORMAT_UNIT_DISPLAYS, WIDGET_GOOD_WHEN, WIDGET_LIST_MAX_ITEMS, WIDGET_METRIC_MAX_PARTS, WIDGET_RESERVED_QUERY_PARAMS, WIDGET_SERIES_MAX_SERIES, WIDGET_SERIES_PERIODS, WIDGET_SOURCES_MAX, WIDGET_TABLE_ALIGNS, WIDGET_TABLE_MAX_COLUMNS, WIDGET_TABLE_MAX_ROWS, checkWidgetData, followsList, followsPeriod, getChangeTone, getWidgetAggregates, getWidgetChange, getWidgetResource, getWidgetSources, isBuiltInWidgetType, isCombinedWidget, isWidgetDataEmpty, isWidgetUnit, readWidgetFormat, readsResources };
200
629
 
201
630
  //# sourceMappingURL=widgets.js.map