@sincpro/web-ui 2.5.0 → 2.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.
Files changed (52) hide show
  1. package/README.md +97 -7
  2. package/dist/components/activity/Activity.d.ts +8 -1
  3. package/dist/components/activity/Activity.js +9 -4
  4. package/dist/components/activity/Messages.d.ts +4 -0
  5. package/dist/components/attachments/Attachments.d.ts +1 -28
  6. package/dist/components/attachments/Attachments.js +5 -24
  7. package/dist/components/attachments/FileList.d.ts +28 -0
  8. package/dist/components/attachments/FileList.js +91 -0
  9. package/dist/components/attachments/FileThumb.d.ts +13 -0
  10. package/dist/components/attachments/FileThumb.js +34 -0
  11. package/dist/components/attachments/download.d.ts +9 -0
  12. package/dist/components/attachments/download.js +36 -0
  13. package/dist/components/attachments/files.d.ts +28 -0
  14. package/dist/components/attachments/index.d.ts +4 -2
  15. package/dist/components/attachments/index.js +4 -1
  16. package/dist/components/calendar/RecordCalendar.d.ts +2 -1
  17. package/dist/components/calendar/RecordCalendar.js +4 -1
  18. package/dist/components/calendar/calendar.css +37 -0
  19. package/dist/components/chat/ChatThread.d.ts +8 -1
  20. package/dist/components/chat/ChatThread.js +9 -4
  21. package/dist/components/chat/model.d.ts +4 -0
  22. package/dist/components/gantt/RecordGantt.d.ts +9 -1
  23. package/dist/components/gantt/RecordGantt.js +16 -3
  24. package/dist/components/gantt/gantt.css +52 -0
  25. package/dist/components/search/CriteriaBuilder.d.ts +11 -2
  26. package/dist/components/search/CriteriaBuilder.js +25 -6
  27. package/dist/components/search/FilterBuilder.d.ts +63 -5
  28. package/dist/components/search/FilterBuilder.js +346 -50
  29. package/dist/components/search/RuleValue.d.ts +17 -0
  30. package/dist/components/search/RuleValue.js +121 -0
  31. package/dist/components/search/SearchBar.d.ts +25 -2
  32. package/dist/components/search/SearchBar.js +360 -39
  33. package/dist/components/search/context.d.ts +45 -0
  34. package/dist/components/search/context.js +25 -0
  35. package/dist/components/search/criteria.d.ts +20 -0
  36. package/dist/components/search/index.d.ts +7 -4
  37. package/dist/components/search/index.js +9 -3
  38. package/dist/components/search/model.d.ts +16 -1
  39. package/dist/components/search/model.js +5 -0
  40. package/dist/components/search/query.d.ts +88 -4
  41. package/dist/components/search/query.js +262 -62
  42. package/dist/components/search/records.d.ts +68 -0
  43. package/dist/components/search/records.js +115 -0
  44. package/dist/foundations/copy.d.ts +27 -0
  45. package/dist/foundations/copy.es.js +15 -0
  46. package/dist/foundations/copy.js +15 -0
  47. package/dist/primitives/button/Button.d.ts +5 -1
  48. package/dist/primitives/button/Button.js +9 -2
  49. package/dist/primitives/value/Markdown.d.ts +1 -0
  50. package/dist/primitives/value/Markdown.js +1 -0
  51. package/dist/tokens.css +6 -85
  52. package/package.json +1 -1
@@ -1,5 +1,12 @@
1
- import { fieldLabel, isFilterable, isNumericType, } from "./model.js";
1
+ import { filterCriteria, } from "./criteria.js";
2
+ import { choiceLabel, fieldLabel, isFilterable, isNumericType, } from "./model.js";
3
+ import { recordsFilter, recordTarget, sameKey, } from "./records.js";
2
4
  import { currentCopy, fillCopy } from "../../foundations/copy.js";
5
+ import { matches as contains } from "../../foundations/match.js";
6
+ /** How a value of a field reads: an enum's word, or the value as it is. */
7
+ export function valueLabel(field, value) {
8
+ return field.choiceLabels?.[value] ?? value;
9
+ }
3
10
  /**
4
11
  * The words that, after the hash sign, open a GROUP instead of naming an
5
12
  * attribute.
@@ -128,42 +135,54 @@ export function queryFields(model, locale) {
128
135
  the same question with two labels —«proyecto» and «nombre del proyecto»— and the
129
136
  one that is understood is the relation's. */
130
137
  const naming = new Set(Object.values(model.fields)
131
- .filter((one) => one.kind === "relation")
138
+ .filter((one) => one.kind === "relation" && one.many !== true)
132
139
  .map((one) => one.identifiedBy)
133
140
  .filter((one) => one !== null && one !== undefined));
134
141
  const out = [];
135
142
  for (const [name, definition] of Object.entries(model.fields)) {
136
143
  const kind = kindOf(definition);
137
144
  if (kind === "relation") {
138
- const through = definition.identifiedBy;
139
- if (through === null || through === undefined)
140
- continue;
141
- const identifying = model.fields[through];
142
- if (identifying === undefined || !isFilterable(identifying))
145
+ /* A to-one relation is compared by a column of this record —its foreign key, or a
146
+ column that names it—. A to-many one has none here (the column that points back lives
147
+ on the other side): it enters with no operators, and is asked only by its records —
148
+ `suggest` drops it when nobody can search them. */
149
+ const target = recordTarget(model, name);
150
+ if (target === undefined)
143
151
  continue;
144
152
  out.push({
145
- field: through,
153
+ field: target.column ?? name,
146
154
  label: fieldLabel(model, name, locale),
147
155
  kind: "relation",
148
- ops: identifying.ops,
149
- through: definition.relation ?? name,
156
+ ops: target.ops,
157
+ through: target.relation,
158
+ relation: target,
150
159
  });
151
160
  continue;
152
161
  }
153
162
  if (!isFilterable(definition) || naming.has(name))
154
163
  continue;
164
+ const choices = definition.choices?.map((one) => String(one));
155
165
  out.push({
156
166
  field: name,
157
167
  label: fieldLabel(model, name, locale),
158
168
  kind,
159
169
  ops: definition.ops,
160
- ...(definition.choices === undefined
170
+ ...(choices === undefined
161
171
  ? {}
162
- : { choices: definition.choices.map((one) => String(one)) }),
172
+ : {
173
+ choices,
174
+ choiceLabels: Object.fromEntries(choices.map((one) => [one, choiceLabel(model, name, one, locale)])),
175
+ }),
163
176
  });
164
177
  }
165
178
  return out;
166
179
  }
180
+ /** One field of the bar by its name in the model — a relation by the relational field
181
+ * («customer_id»), anything else by itself. It is what an application uses to build, by code,
182
+ * the same chips the bar builds: `valuesCriteria(queryField(model, "customer_id", locale)!, …)`. */
183
+ export function queryField(model, name, locale) {
184
+ return queryFields(model, locale).find((one) => one.relation?.name === name || (one.relation === undefined && one.field === name));
185
+ }
167
186
  /**
168
187
  * Whether a field can answer what was written.
169
188
  *
@@ -194,18 +213,22 @@ export function answers(field, typed) {
194
213
  }
195
214
  if (typed.shape === "empty")
196
215
  return true;
216
+ /* A relation answers a text only through a column that NAMES it —one that takes `like`—.
217
+ Through its foreign key, «cliente es andres» compares a name against a number: a question
218
+ that gives zero. Its records, chosen by name, are the way to ask it (`searchRecords`). */
219
+ const named = field.kind === "relation" && field.ops.includes("like");
197
220
  switch (typed.shape) {
198
221
  case "number":
199
222
  return field.kind === "number" || field.kind === "choice";
200
223
  case "date":
201
224
  return field.kind === "date";
202
225
  case "glob":
203
- return field.kind === "text" || field.kind === "relation";
226
+ return field.kind === "text" || named;
204
227
  default:
205
228
  /* A text is answered by what has letters inside, and also an enum: its
206
229
  values are words. A number does not —«test» is not a number— and a date
207
230
  neither. */
208
- return field.kind === "text" || field.kind === "relation" || field.kind === "choice";
231
+ return field.kind === "text" || named || field.kind === "choice";
209
232
  }
210
233
  }
211
234
  /**
@@ -257,16 +280,27 @@ function matches(field, text) {
257
280
  return true;
258
281
  return (field.label.toLowerCase().includes(needle) || field.field.toLowerCase().includes(needle));
259
282
  }
260
- /**
261
- * What is offered right now.
262
- *
263
- * It is a single list and not three screens: in the first step the fields
264
- * named like what was typed coexist —«nombr» → «nombre»— with the whole questions
265
- * that what was typed already allows building —«test» en el nombre—. Whoever writes a field's
266
- * name wants to continue that way; whoever writes a value wants to ask it
267
- * right away. The two intentions are written the same, so both are offered, with the
268
- * fields first: it is the step that has not decided anything yet.
269
- */
283
+ /** Two words are the same one, whatever their case and accents: «pagada» is «Pagada». */
284
+ function same(a, b) {
285
+ return a.trim() !== "" && contains(a, b) && contains(b, a);
286
+ }
287
+ /** Which field a value chip belongs to: a relation by the relational field, anything else
288
+ * by itself. Two chips with the same key are the same question with more values. */
289
+ export function fieldKey(field) {
290
+ return field.relation?.name ?? field.field;
291
+ }
292
+ /** The values of an enum that contain what was typed — by their word or their value — as
293
+ * options: what an unfolded enum shows, and what the builder offers. */
294
+ export function choiceValues(field, text) {
295
+ return (field.choices ?? [])
296
+ .map((value) => ({ id: value, name: valueLabel(field, value) }))
297
+ .filter((one) => text.trim() === "" || contains(one.name, text) || contains(one.id, text));
298
+ }
299
+ /** Whether the field can be asked anything from the bar: it has operators, or its records
300
+ * can be searched. A to-many relation has neither without `searchRecords`. */
301
+ function askable(field, records) {
302
+ return field.ops.length > 0 || (records && field.relation !== undefined);
303
+ }
270
304
  /**
271
305
  * Whether the MODEL reads what was typed as a question of its own: an operator written in
272
306
  * front («> 100»), the name of a field («estado»), a value of an enum («pagada»), or a number or
@@ -276,19 +310,99 @@ function matches(field, text) {
276
310
  function readByModel(typed, byName, fields) {
277
311
  if (typed.op !== undefined || byName.length > 0)
278
312
  return true;
279
- const value = typed.value.toLowerCase();
280
- if (fields.some((field) => field.choices?.some((one) => one.toLowerCase() === value)))
313
+ if (fields.some((field) => field.choices?.some((one) => same(one, typed.value) || same(valueLabel(field, one), typed.value))))
281
314
  return true;
282
315
  return ((typed.shape === "number" || typed.shape === "date") &&
283
316
  fields.some((field) => ORDERED.has(field.kind) && answers(field, typed)));
284
317
  }
285
- export function suggest(draft, fields,
286
- /** The set has a literal search (`SearchBar literal`): offer it for free text. */
287
- options = {}) {
318
+ /**
319
+ * The first step's question about ONE field, for what was typed — or none.
320
+ *
321
+ * an enum its value when one is exactly what was typed («pagada» → Estado:
322
+ * Pagada); else a row that unfolds the values that contain it; none
323
+ * when no value does — «estado es xyz» gives zero
324
+ * a relation by the column that names it («contiene»), which ALSO unfolds its
325
+ * records when they can be searched
326
+ * anything else the condition, with the operator the shape asks for
327
+ */
328
+ function questionAbout(field, typed, records) {
329
+ if (field.choices !== undefined && (typed.op === undefined || typed.op === "=")) {
330
+ const values = choiceValues(field, typed.value);
331
+ const exact = values.find((one) => same(one.name, typed.value) || same(String(one.id), typed.value));
332
+ if (exact !== undefined) {
333
+ return {
334
+ kind: "value",
335
+ id: `v:${fieldKey(field)}:${exact.id}`,
336
+ label: field.label,
337
+ hint: exact.name,
338
+ field,
339
+ option: exact,
340
+ };
341
+ }
342
+ if (values.length === 0)
343
+ return null;
344
+ return {
345
+ kind: "unfold",
346
+ id: `u:${fieldKey(field)}`,
347
+ label: field.label,
348
+ hint: typed.value,
349
+ field,
350
+ text: typed.value,
351
+ };
352
+ }
353
+ const op = defaultOp(field, typed);
354
+ return {
355
+ kind: "condition",
356
+ id: `c:${field.field}`,
357
+ label: field.label,
358
+ hint: `${opLabel(op)} ${valueFor(op, typed)}`,
359
+ field,
360
+ op,
361
+ value: valueFor(op, typed),
362
+ ...(records && field.relation !== undefined && op === "like"
363
+ ? { unfolds: true }
364
+ : {}),
365
+ };
366
+ }
367
+ /**
368
+ * What is offered right now.
369
+ *
370
+ * It is a single list and not three screens: in the first step the fields
371
+ * named like what was typed coexist —«nombr» → «nombre»— with the whole questions
372
+ * that what was typed already allows building —«test» en el nombre—. Whoever writes a field's
373
+ * name wants to continue that way; whoever writes a value wants to ask it
374
+ * right away. The two intentions are written the same, so both are offered, with the
375
+ * fields first: it is the step that has not decided anything yet.
376
+ *
377
+ * With `records` (the bar has `searchRecords`) a relation is ALSO asked by its records: its
378
+ * row unfolds them, and a relation that cannot compare a text —its foreign key, a many2many—
379
+ * is offered only that way.
380
+ */
381
+ export function suggest(draft, all, options = {}) {
382
+ const records = options.records === true;
383
+ const fields = all.filter((field) => askable(field, records));
288
384
  const typed = readTyped(draft.text);
289
385
  /* Step 3 — the field and the operator are already there: the value is missing. */
290
386
  if (draft.field !== undefined && draft.op !== undefined) {
291
387
  const field = draft.field;
388
+ /* A relation chosen by its name, with its records searchable: what is typed searches
389
+ them —the bar unfolds them below—, and the text itself is a question only through a
390
+ column that names it. */
391
+ if (records && field.relation !== undefined) {
392
+ if (typed.shape === "empty" || !field.ops.includes("like"))
393
+ return [];
394
+ return [
395
+ {
396
+ kind: "condition",
397
+ id: "v:typed",
398
+ label: valueFor("like", typed),
399
+ hint: `${field.label} ${opLabel("like")}`,
400
+ field,
401
+ op: "like",
402
+ value: valueFor("like", typed),
403
+ },
404
+ ];
405
+ }
292
406
  /* Writing a pattern CHANGES the comparison: «igual a ventas_*» searches for a
293
407
  name that ends in an asterisk, which does not exist and nobody meant to ask for.
294
408
  It only overrides equality —whoever chose «mayor que» and wrote a `*` is
@@ -296,19 +410,28 @@ options = {}) {
296
410
  const op = typed.shape === "glob" && EQUALITY.has(draft.op) && field.ops.includes("like")
297
411
  ? "like"
298
412
  : draft.op;
299
- const values = field.choices === undefined
300
- ? []
301
- : field.choices.filter((one) => one.toLowerCase().includes(typed.value.toLowerCase()));
302
- const ready = values.map((one) => ({
303
- kind: "condition",
304
- id: `v:${one}`,
305
- label: one,
306
- hint: `${field.label} ${opLabel(op)}`,
307
- field,
308
- op,
309
- value: one,
310
- }));
311
- if (typed.shape !== "empty" && !values.includes(typed.value)) {
413
+ /* An enum's values, by their word. With «es» each one is a VALUE —choosing two makes one
414
+ chip, «Estado: Pagada o Vencida»—; with any other comparison, a question of its own. */
415
+ const values = choiceValues(field, typed.value);
416
+ const ready = values.map((one) => op === "="
417
+ ? {
418
+ kind: "value",
419
+ id: `v:${fieldKey(field)}:${one.id}`,
420
+ label: one.name,
421
+ hint: `${field.label} ${opLabel(op)}`,
422
+ field,
423
+ option: one,
424
+ }
425
+ : {
426
+ kind: "condition",
427
+ id: `v:${one.id}`,
428
+ label: one.name,
429
+ hint: `${field.label} ${opLabel(op)}`,
430
+ field,
431
+ op,
432
+ value: String(one.id),
433
+ });
434
+ if (typed.shape !== "empty" && !values.some((one) => same(one.name, typed.value))) {
312
435
  ready.unshift({
313
436
  kind: "condition",
314
437
  id: "v:typed",
@@ -327,7 +450,7 @@ options = {}) {
327
450
  if (draft.field !== undefined) {
328
451
  const field = draft.field;
329
452
  const first = defaultOp(field, typed);
330
- const ops = [first, ...field.ops.filter((one) => one !== first)].filter((op) => !withTwoValues(op));
453
+ const ops = [first, ...field.ops.filter((one) => one !== first)].filter((op) => !withTwoValues(op) && field.ops.includes(op));
331
454
  return ops.map((op) =>
332
455
  /* The one that takes NO value is a whole question as soon as it is chosen: sending it
333
456
  to the value step left whoever chose «está vacío» typing something that
@@ -375,7 +498,7 @@ options = {}) {
375
498
  .filter((field) => matches(field, typed.value))
376
499
  .map((field) => ({
377
500
  kind: "field",
378
- id: `f:${field.field}`,
501
+ id: `f:${fieldKey(field)}`,
379
502
  label: field.label,
380
503
  ...(field.through === undefined ? {} : { hint: field.through }),
381
504
  field,
@@ -389,25 +512,11 @@ options = {}) {
389
512
  .filter((field) => matches(field, draft.text))
390
513
  .map((field) => ({
391
514
  kind: "field",
392
- id: `f:${field.field}`,
515
+ id: `f:${fieldKey(field)}`,
393
516
  label: field.label,
394
517
  ...(field.through === undefined ? {} : { hint: field.through }),
395
518
  field,
396
519
  }));
397
- const ready = fields
398
- .filter((field) => answers(field, typed))
399
- .map((field) => {
400
- const op = defaultOp(field, typed);
401
- return {
402
- kind: "condition",
403
- id: `c:${field.field}`,
404
- label: field.label,
405
- hint: `${opLabel(op)} ${valueFor(op, typed)}`,
406
- field,
407
- op,
408
- value: valueFor(op, typed),
409
- };
410
- });
411
520
  /* Without a VALUE there is no question to build yet —a question without a value
412
521
  is not a question— so fields are offered. But if the operator was already written,
413
522
  that alone narrows: `>` keeps those that have an order and drops the texts
@@ -417,20 +526,46 @@ options = {}) {
417
526
  .filter((field) => answers(field, typed))
418
527
  .map((field) => ({
419
528
  kind: "field",
420
- id: `f:${field.field}`,
529
+ id: `f:${fieldKey(field)}`,
421
530
  label: field.label,
422
531
  ...(field.through === undefined ? {} : { hint: field.through }),
423
532
  field,
424
533
  }));
425
534
  }
535
+ const asked = fields
536
+ .filter((field) => answers(field, typed))
537
+ .map((field) => questionAbout(field, typed, records))
538
+ .filter((one) => one !== null);
539
+ /* An enum's value written whole —«pagada»— is the likeliest reading of all: it goes before
540
+ «número contiene pagada», which every text field would answer. */
541
+ const ready = [
542
+ ...asked.filter((one) => one.kind === "value"),
543
+ ...asked.filter((one) => one.kind !== "value"),
544
+ ];
545
+ /* A relation that cannot compare a text —its foreign key, a many2many— is asked by its
546
+ records: the row unfolds the ones that match what was typed. Only for a text: a number
547
+ or an operator written in front is a question about a column, not a name. */
548
+ const byRecords = records && typed.op === undefined && (typed.shape === "text" || typed.shape === "glob")
549
+ ? fields
550
+ .filter((field) => field.relation !== undefined && !answers(field, typed))
551
+ .map((field) => ({
552
+ kind: "unfold",
553
+ id: `u:${fieldKey(field)}`,
554
+ label: field.label,
555
+ hint: typed.value,
556
+ field,
557
+ text: typed.value,
558
+ }))
559
+ : [];
426
560
  /* A field named like what was typed is not offered twice: if «nombre»
427
561
  appears above to continue by it, below it is no longer needed as a
428
562
  question already made — unless what was typed is also a reasonable value, which is
429
563
  precisely the case of «test». */
430
- const named = new Set(byName.map((one) => (one.kind === "field" ? one.field.field : "")));
564
+ const named = new Set(byName.map((one) => (one.kind === "field" ? fieldKey(one.field) : "")));
431
565
  const byModel = [
432
566
  ...byName,
433
- ...ready.filter((one) => one.kind !== "condition" || !named.has(one.field.field)),
567
+ ...[...ready, ...byRecords].filter((one) => (one.kind !== "condition" && one.kind !== "unfold") ||
568
+ !named.has(fieldKey(one.field))),
434
569
  ];
435
570
  if (options.literal !== true)
436
571
  return byModel;
@@ -466,3 +601,68 @@ export function sayCondition(label, op, value) {
466
601
  export function describeCondition(field, op, value) {
467
602
  return sayCondition(field.label, op, value);
468
603
  }
604
+ /* ---------------------------------------------------------------- *
605
+ * Chips made by code — the same ones the bar makes
606
+ *
607
+ * A list that opens on «mis facturas», a chooser that opens on the customer of the form, a
608
+ * link that carries «Estado: Vencida»: the application puts the SAME instances the bar puts
609
+ * when somebody types, so they read, open in the builder and are removed the same. The bar
610
+ * is controlled (`criteria`), so a default is the first value of that state.
611
+ * ---------------------------------------------------------------- */
612
+ /** The question about one field as a chip — what the bar puts when a condition is chosen:
613
+ * «nombre contiene test». */
614
+ export function conditionCriteria(field, op, value) {
615
+ return filterCriteria(
616
+ /* The one that takes no value travels with `true`, which is what the engine expects of
617
+ «está vacío»: sending it the empty string would be asking for the field that is "". */
618
+ { field: field.field, operator: op, value: withoutValue(op) ? true : value }, describeCondition(field, op, value));
619
+ }
620
+ /** What asks for these values of a field: records of a relation by their key (see
621
+ * `recordsFilter`), values of an enum or of any column with `=` / `in`. */
622
+ export function valuesSelection(field, values) {
623
+ const keys = values.map((one) => one.id);
624
+ if (field.relation !== undefined)
625
+ return recordsFilter(field.relation, keys);
626
+ if (keys.length === 1)
627
+ return { field: field.field, operator: "=", value: keys[0] };
628
+ if (field.ops.includes("in"))
629
+ return { field: field.field, operator: "in", value: keys };
630
+ return { any: keys.map((key) => ({ field: field.field, operator: "=", value: key })) };
631
+ }
632
+ /** How a chip of values reads: «Cliente: Andrés Pérez o Andrés Gutiérrez». Past three, the
633
+ * rest is counted — the builder shows them whole. */
634
+ export function sayValues(field, values) {
635
+ const copy = currentCopy();
636
+ const names = values.slice(0, 3).map((one) => one.name);
637
+ const said = values.length > 3
638
+ ? `${names.join(", ")} ${fillCopy(copy.search.andMore, { count: values.length - 3 })}`
639
+ : names.join(` ${copy.search.or} `);
640
+ return fillCopy(copy.search.valuesChip, { field: field.label, values: said });
641
+ }
642
+ /** The chip that asks for these values of a field. Its id says which field, so a second value
643
+ * chosen lands in the SAME chip (`withValue`). */
644
+ export function valuesCriteria(field, values) {
645
+ return {
646
+ id: `v:${fieldKey(field)}`,
647
+ kind: "filter",
648
+ label: sayValues(field, values),
649
+ selection: valuesSelection(field, values),
650
+ values: [...values],
651
+ };
652
+ }
653
+ /**
654
+ * One more value of a field, where the bar puts things: into the chip that already asks about
655
+ * that field —«Cliente: Andrés Pérez» becomes «… o Andrés Gutiérrez»— or as a new chip at the
656
+ * end. Choosing a value that is already there changes nothing.
657
+ */
658
+ export function withValue(all, field, value) {
659
+ const id = `v:${fieldKey(field)}`;
660
+ const at = all.findIndex((one) => one.id === id && one.kind === "filter");
661
+ if (at < 0)
662
+ return [...all, valuesCriteria(field, [value])];
663
+ const held = all[at];
664
+ const before = held?.kind === "filter" ? (held.values ?? []) : [];
665
+ if (before.some((one) => sameKey(one.id, value.id)))
666
+ return [...all];
667
+ return all.map((one, index) => index === at ? valuesCriteria(field, [...before, value]) : one);
668
+ }
@@ -0,0 +1,68 @@
1
+ import type { BuiltFilter, RecordKey, RecordOption } from "./criteria.js";
2
+ import { type ModelDefinition } from "./model.js";
3
+ export type { RecordKey, RecordOption };
4
+ /** What a records search is asked. */
5
+ export interface RecordSearch {
6
+ /** The aggregate on the other side — WHICH records: «Partner», «Tag». */
7
+ relation: string;
8
+ /** The relational field, from the root record: `["customer_id"]`, `["lines", "product_id"]`. */
9
+ path: readonly string[];
10
+ /** What was typed. Empty: the first ones, in their own order. */
11
+ text: string;
12
+ /** Instead of a text: name THESE records. A filter read back carries keys; the screen needs
13
+ * their names. */
14
+ keys?: readonly RecordKey[];
15
+ /** The question was replaced by a newer one (more was typed): stop. */
16
+ signal: AbortSignal;
17
+ }
18
+ /** The application's search over the records of a relation. A few are enough — a dropdown is
19
+ * not a page; the rest are reached by typing more. */
20
+ export type SearchRecords = (search: RecordSearch) => Promise<readonly RecordOption[]>;
21
+ /** How a relation is compared once records of it are chosen. */
22
+ export interface RecordTarget {
23
+ /** The relational field: «customer_id», «tags». */
24
+ name: string;
25
+ /** The aggregate on the other side. */
26
+ relation: string;
27
+ /** It reaches many records: one2many, many2many. */
28
+ many: boolean;
29
+ /** The key of the records on the other side, compared through the relation. */
30
+ key: string;
31
+ /** A column of this record that identifies the one on the other side (a many2one's foreign
32
+ * key or name): compared directly, without crossing. */
33
+ column?: string;
34
+ /** The operators that column accepts. */
35
+ ops: readonly string[];
36
+ }
37
+ /**
38
+ * How a relation of the model is compared once records of it are chosen — or `undefined`
39
+ * when the field is not a relation.
40
+ *
41
+ * A to-one relation is compared by a column of this record (its foreign key, or a column that
42
+ * names it) when the model has it; a to-many one —or a to-one without that column— crossing
43
+ * it, by the key of the records on the other side.
44
+ */
45
+ export declare function recordTarget(model: ModelDefinition, name: string): RecordTarget | undefined;
46
+ /**
47
+ * The filter that asks for these records.
48
+ *
49
+ * in Cliente (many2one, column customer_id), keys [7]
50
+ * out {field: "customer_id", operator: "=", value: 7}
51
+ *
52
+ * in Cliente, keys [7, 9]
53
+ * out {field: "customer_id", operator: "in", value: [7, 9]}
54
+ *
55
+ * in Etiquetas (many2many), keys [3, 4]
56
+ * out {through: "tags", selection: {field: "id", operator: "in", value: [3, 4]}}
57
+ *
58
+ * `negate` asks for the rest: «no es ninguno de», «ninguna etiqueta entre».
59
+ */
60
+ export declare function recordsFilter(target: RecordTarget, keys: readonly RecordKey[], negate?: boolean): BuiltFilter;
61
+ /** The keys a filter asks for, when it is exactly what `recordsFilter` writes for this target;
62
+ * `null` otherwise. It is how a filter read back is shown as names again. */
63
+ export declare function recordsOf(target: RecordTarget, filter: unknown): {
64
+ keys: RecordKey[];
65
+ negate: boolean;
66
+ } | null;
67
+ /** The same key, whatever its type: `7` and `"7"` are one record on the screen. */
68
+ export declare function sameKey(a: RecordKey, b: RecordKey): boolean;
@@ -0,0 +1,115 @@
1
+ import { isFilterable } from "./model.js";
2
+ /**
3
+ * How a relation of the model is compared once records of it are chosen — or `undefined`
4
+ * when the field is not a relation.
5
+ *
6
+ * A to-one relation is compared by a column of this record (its foreign key, or a column that
7
+ * names it) when the model has it; a to-many one —or a to-one without that column— crossing
8
+ * it, by the key of the records on the other side.
9
+ */
10
+ export function recordTarget(model, name) {
11
+ const definition = model.fields[name];
12
+ if (definition === undefined)
13
+ return undefined;
14
+ const relational = definition.kind === "relation" ||
15
+ (definition.relation !== undefined && definition.relation !== null);
16
+ if (!relational)
17
+ return undefined;
18
+ const many = definition.many === true;
19
+ const column = many ? undefined : (definition.identifiedBy ?? undefined);
20
+ const identifying = column === undefined ? undefined : model.fields[column];
21
+ const local = column !== undefined && identifying !== undefined && isFilterable(identifying);
22
+ return {
23
+ name,
24
+ relation: definition.relation ?? name,
25
+ many,
26
+ key: definition.relationKey ?? "id",
27
+ ...(local ? { column } : {}),
28
+ ops: local ? identifying.ops : [],
29
+ };
30
+ }
31
+ /** The same key compared to each one: what is left when a column does not take `in`. */
32
+ function eachOf(field, keys) {
33
+ const each = keys.map((key) => ({ field, operator: "=", value: key }));
34
+ return each.length === 1 ? each[0] : { any: each };
35
+ }
36
+ /**
37
+ * The filter that asks for these records.
38
+ *
39
+ * in Cliente (many2one, column customer_id), keys [7]
40
+ * out {field: "customer_id", operator: "=", value: 7}
41
+ *
42
+ * in Cliente, keys [7, 9]
43
+ * out {field: "customer_id", operator: "in", value: [7, 9]}
44
+ *
45
+ * in Etiquetas (many2many), keys [3, 4]
46
+ * out {through: "tags", selection: {field: "id", operator: "in", value: [3, 4]}}
47
+ *
48
+ * `negate` asks for the rest: «no es ninguno de», «ninguna etiqueta entre».
49
+ */
50
+ export function recordsFilter(target, keys, negate = false) {
51
+ const one = keys.length === 1;
52
+ if (!target.many && target.column !== undefined) {
53
+ const op = negate ? (one ? "!=" : "not in") : one ? "=" : "in";
54
+ if (target.ops.includes(op)) {
55
+ return { field: target.column, operator: op, value: one ? keys[0] : [...keys] };
56
+ }
57
+ const asked = eachOf(target.column, keys);
58
+ return negate ? { negate: asked } : asked;
59
+ }
60
+ const inner = one
61
+ ? { field: target.key, operator: "=", value: keys[0] }
62
+ : { field: target.key, operator: "in", value: [...keys] };
63
+ const crossed = { through: target.name, selection: inner };
64
+ return negate ? { negate: crossed } : crossed;
65
+ }
66
+ /** The keys a filter asks for, when it is exactly what `recordsFilter` writes for this target;
67
+ * `null` otherwise. It is how a filter read back is shown as names again. */
68
+ export function recordsOf(target, filter) {
69
+ if (typeof filter !== "object" || filter === null)
70
+ return null;
71
+ const node = filter;
72
+ if ("negate" in node) {
73
+ const inside = recordsOf(target, node.negate);
74
+ return inside === null || inside.negate ? null : { keys: inside.keys, negate: true };
75
+ }
76
+ const keysOf = (value, op) => {
77
+ if (op === "=" && (typeof value === "string" || typeof value === "number"))
78
+ return [value];
79
+ if (op === "in" && Array.isArray(value))
80
+ return value;
81
+ return null;
82
+ };
83
+ if (!target.many && target.column !== undefined) {
84
+ if (node.field === target.column) {
85
+ if (node.operator === "!=" || node.operator === "not in") {
86
+ const keys = keysOf(node.value, node.operator === "!=" ? "=" : "in");
87
+ return keys === null ? null : { keys, negate: true };
88
+ }
89
+ const keys = keysOf(node.value, node.operator);
90
+ return keys === null ? null : { keys, negate: false };
91
+ }
92
+ if (Array.isArray(node.any)) {
93
+ const keys = [];
94
+ for (const each of node.any) {
95
+ const one = recordsOf(target, each);
96
+ if (one === null || one.negate || one.keys.length !== 1)
97
+ return null;
98
+ keys.push(one.keys[0]);
99
+ }
100
+ return keys.length === 0 ? null : { keys, negate: false };
101
+ }
102
+ return null;
103
+ }
104
+ if (node.through !== target.name || typeof node.selection !== "object")
105
+ return null;
106
+ const inner = node.selection;
107
+ if (inner === null || inner.field !== target.key)
108
+ return null;
109
+ const keys = keysOf(inner.value, inner.operator);
110
+ return keys === null ? null : { keys, negate: false };
111
+ }
112
+ /** The same key, whatever its type: `7` and `"7"` are one record on the screen. */
113
+ export function sameKey(a, b) {
114
+ return String(a) === String(b);
115
+ }