pict-section-form 1.3.14 → 1.3.16

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 (80) hide show
  1. package/docs/Injection_Conformance.md +137 -0
  2. package/docs/Providers.md +12 -7
  3. package/docs/_sidebar.md +3 -0
  4. package/docs/input_providers/016-facet-filter.md +106 -0
  5. package/docs/input_providers/README.md +1 -0
  6. package/example_applications/README.md +6 -0
  7. package/example_applications/facet_filter/.quackage.json +9 -0
  8. package/example_applications/facet_filter/Facet-Filter-Application.js +210 -0
  9. package/example_applications/facet_filter/Facet-Filter-Server.js +233 -0
  10. package/example_applications/facet_filter/Facet-Filter-Smoke-Runner.js +101 -0
  11. package/example_applications/facet_filter/FacetFilterData.json +44 -0
  12. package/example_applications/facet_filter/README-FacetFilter.md +70 -0
  13. package/example_applications/facet_filter/html/Facet-Filter-Smoke.js +128 -0
  14. package/example_applications/facet_filter/html/index.html +74 -0
  15. package/example_applications/facet_filter/package.json +27 -0
  16. package/example_applications/nested_distill_sandbox/Sandbox-Driver-Headlight.js +17 -0
  17. package/example_applications/nested_distill_sandbox/Sandbox-Driver.js +369 -0
  18. package/example_applications/nested_distill_sandbox/Sandbox-Runner.js +372 -0
  19. package/example_applications/nested_distill_sandbox/approaches/A1-tabular-trigger-group.js +67 -0
  20. package/example_applications/nested_distill_sandbox/approaches/A3-crossmap-selectoptionsrefresh.js +61 -0
  21. package/example_applications/nested_distill_sandbox/approaches/A4-graph-solvers-flat-list.js +53 -0
  22. package/example_applications/nested_distill_sandbox/approaches/A5-facet-filter.js +75 -0
  23. package/example_applications/nested_distill_sandbox/approaches/A5b-facet-filter-symmetric.js +15 -0
  24. package/example_applications/nested_distill_sandbox/approaches/A5h-facet-filter-headlight-picker.js +15 -0
  25. package/example_applications/nested_distill_sandbox/approaches/R0-original-jmf-manifest-headlight.js +27 -0
  26. package/example_applications/nested_distill_sandbox/approaches/R1-real-jmf-manifest-headlight.js +136 -0
  27. package/example_applications/nested_distill_sandbox/approaches/_Common.js +344 -0
  28. package/example_applications/nested_distill_sandbox/approaches/_Scenarios.js +262 -0
  29. package/package.json +6 -5
  30. package/source/providers/Pict-Provider-DynamicFormSolverBehaviors.js +73 -5
  31. package/source/providers/Pict-Provider-DynamicSolver.js +22 -2
  32. package/source/providers/Pict-Provider-ListDistilling.js +266 -1
  33. package/source/providers/Pict-Provider-MetaLists.js +16 -2
  34. package/source/providers/inputs/Pict-Provider-Input-FacetFilter.js +232 -0
  35. package/source/providers/inputs/Pict-Provider-Input-Select.js +41 -1
  36. package/source/views/Pict-View-Form-Metacontroller.js +690 -93
  37. package/test/Pict-Provider-DynamicFormSolverBehaviors-OrdinalScope_tests.js +105 -0
  38. package/test/Pict-Provider-ListDistilling-Facet_tests.js +375 -0
  39. package/test/Pict-View-Form-Metacontroller-DistinctManifest_tests.js +410 -0
  40. package/test/PictSectionForm-Injection-Conformance_tests.js +762 -0
  41. package/test/browser/Injection-Conformance-Differential-Scenario.js +346 -0
  42. package/test/browser/Injection-Conformance-EdgeCase-Scenario.js +168 -0
  43. package/test/browser/Injection-Conformance-Harness-Driver.js +538 -0
  44. package/test/browser/Injection-Conformance-Harness-Runner.js +487 -0
  45. package/test/browser/Injection-Conformance-Reconstitution-Scenario.js +191 -0
  46. package/test/browser/Injection-Conformance-Standalone-Scenario.js +114 -0
  47. package/test/fixtures/injection/Injection-Fixture-Cases.js +204 -0
  48. package/test/fixtures/injection/Injection-Fixture-Manifest.json +381 -0
  49. package/types/source/providers/Pict-Provider-DynamicFormSolverBehaviors.d.ts +110 -1
  50. package/types/source/providers/Pict-Provider-DynamicFormSolverBehaviors.d.ts.map +1 -1
  51. package/types/source/providers/Pict-Provider-DynamicSolver.d.ts.map +1 -1
  52. package/types/source/providers/Pict-Provider-DynamicTabularData.d.ts +17 -1
  53. package/types/source/providers/Pict-Provider-DynamicTabularData.d.ts.map +1 -1
  54. package/types/source/providers/Pict-Provider-InputExtension.d.ts +31 -0
  55. package/types/source/providers/Pict-Provider-InputExtension.d.ts.map +1 -1
  56. package/types/source/providers/Pict-Provider-ListDistilling.d.ts +90 -1
  57. package/types/source/providers/Pict-Provider-ListDistilling.d.ts.map +1 -1
  58. package/types/source/providers/Pict-Provider-MetaLists.d.ts.map +1 -1
  59. package/types/source/providers/inputs/Pict-Provider-Input-Chart-CSS.d.ts +16 -0
  60. package/types/source/providers/inputs/Pict-Provider-Input-Chart-CSS.d.ts.map +1 -0
  61. package/types/source/providers/inputs/Pict-Provider-Input-Chart.d.ts +47 -0
  62. package/types/source/providers/inputs/Pict-Provider-Input-Chart.d.ts.map +1 -1
  63. package/types/source/providers/inputs/Pict-Provider-Input-Diagram.d.ts +86 -0
  64. package/types/source/providers/inputs/Pict-Provider-Input-Diagram.d.ts.map +1 -1
  65. package/types/source/providers/inputs/Pict-Provider-Input-FacetFilter.d.ts +103 -0
  66. package/types/source/providers/inputs/Pict-Provider-Input-FacetFilter.d.ts.map +1 -0
  67. package/types/source/providers/inputs/Pict-Provider-Input-ObjectEditor.d.ts +128 -0
  68. package/types/source/providers/inputs/Pict-Provider-Input-ObjectEditor.d.ts.map +1 -0
  69. package/types/source/providers/inputs/Pict-Provider-Input-PreciseNumber.d.ts.map +1 -1
  70. package/types/source/providers/inputs/Pict-Provider-Input-Select.d.ts +12 -0
  71. package/types/source/providers/inputs/Pict-Provider-Input-Select.d.ts.map +1 -1
  72. package/types/source/providers/inputs/Pict-Provider-Input-TabGroupSelector.d.ts +10 -1
  73. package/types/source/providers/inputs/Pict-Provider-Input-TabGroupSelector.d.ts.map +1 -1
  74. package/types/source/providers/inputs/Pict-Provider-Input-TabSectionSelector.d.ts +10 -1
  75. package/types/source/providers/inputs/Pict-Provider-Input-TabSectionSelector.d.ts.map +1 -1
  76. package/types/source/providers/layouts/Pict-Layout-Tabular.d.ts +76 -4
  77. package/types/source/providers/layouts/Pict-Layout-Tabular.d.ts.map +1 -1
  78. package/types/source/views/Pict-View-DynamicForm.d.ts.map +1 -1
  79. package/types/source/views/Pict-View-Form-Metacontroller.d.ts +63 -2
  80. package/types/source/views/Pict-View-Form-Metacontroller.d.ts.map +1 -1
@@ -81,9 +81,10 @@ class PictListDistilling extends libPictProvider
81
81
  * @param {Object} pView - The pict view the list is within
82
82
  * @param {*} pInput - The input within the dynamic forms view
83
83
  * @param {*} pList - The raw list of options to filter
84
+ * @param {number} [pRowIndex] - The tabular row the list is for; Facet rules read sibling values from this row
84
85
  * @returns
85
86
  */
86
- filterList(pView, pInput, pList)
87
+ filterList(pView, pInput, pList, pRowIndex)
87
88
  {
88
89
  // The list is expected to be an array of objects with "id" and "text" properties. This is because of Select2.
89
90
  if (typeof(pInput) != 'object')
@@ -107,6 +108,9 @@ class PictListDistilling extends libPictProvider
107
108
  return pList;
108
109
  }
109
110
 
111
+ // Facet rules resolve to a set of allowed values once per list build rather than once per option.
112
+ const tmpFacetAllowedValues = tmpFilterRules.map((pRule) => (pRule && (pRule.FilterType === 'Facet')) ? this.getFacetOfferedValues(pView, pInput, pRule, pRowIndex) : null);
113
+
110
114
  // Make a copy of the incoming list of options
111
115
  let tmpFilteredList = pList.slice(0);
112
116
  // We walk backwards so we can remove items from the end without breaking this loop.
@@ -295,6 +299,13 @@ class PictListDistilling extends libPictProvider
295
299
 
296
300
  break;
297
301
 
302
+ case 'Facet':
303
+ if (tmpFacetAllowedValues[i] && !tmpFacetAllowedValues[i].has(String(tmpComparisonValue)))
304
+ {
305
+ tmpEntryAllowed = false;
306
+ }
307
+ break;
308
+
298
309
  default:
299
310
  // See if there is a provider for it
300
311
  let tmpProvider = this.pict.providers[tmpFilterRule.FilterType];
@@ -318,6 +329,260 @@ class PictListDistilling extends libPictProvider
318
329
 
319
330
  return tmpFilteredList;
320
331
  }
332
+
333
+ /**
334
+ * @param {Record<string, any>} pInput
335
+ * @return {Record<string, any>|null} the input's Facet rule, if it has one
336
+ */
337
+ getFacetRule(pInput)
338
+ {
339
+ const tmpRules = pInput && pInput.PictForm && pInput.PictForm.ListFilterRules;
340
+ if (!Array.isArray(tmpRules))
341
+ {
342
+ return null;
343
+ }
344
+ return tmpRules.find((pRule) => pRule && (pRule.FilterType === 'Facet')) || null;
345
+ }
346
+
347
+ /**
348
+ * @param {Object} pView
349
+ * @param {Record<string, any>} pRule
350
+ * @return {Array<any>} the join list a Facet rule distills, as an array
351
+ */
352
+ getFacetJoinList(pView, pRule)
353
+ {
354
+ let tmpJoinList;
355
+ if (pRule.JoinListAddressGlobal)
356
+ {
357
+ const tmpAddressSpace = { Fable: this.pict, Pict: this.pict, AppData: this.pict.AppData, Bundle: this.pict.Bundle, View: pView };
358
+ tmpJoinList = this.pict.manifest.getValueAtAddress(tmpAddressSpace, pRule.JoinListAddress);
359
+ }
360
+ else
361
+ {
362
+ tmpJoinList = pView.getValueByHash(pRule.JoinListAddress);
363
+ }
364
+ if (Array.isArray(tmpJoinList))
365
+ {
366
+ return tmpJoinList;
367
+ }
368
+ if (tmpJoinList && (typeof(tmpJoinList) === 'object'))
369
+ {
370
+ return Object.values(tmpJoinList);
371
+ }
372
+ return [];
373
+ }
374
+
375
+ /**
376
+ * Every input that shares a Facet rule's join list with pInput, in the same tabular row (or the same
377
+ * view, outside a table), with the value it currently holds. pInput itself is included.
378
+ *
379
+ * @param {Object} pView
380
+ * @param {Record<string, any>} pInput
381
+ * @param {number} [pRowIndex]
382
+ * @return {Array<{ Input: Record<string, any>, Rule: Record<string, any>, Value: any }>}
383
+ */
384
+ getFacetSiblings(pView, pInput, pRowIndex)
385
+ {
386
+ const tmpRule = this.getFacetRule(pInput);
387
+ if (!tmpRule)
388
+ {
389
+ return [];
390
+ }
391
+ const tmpSiblings = [];
392
+ const fAdd = (pSiblingInput, fGetValue) =>
393
+ {
394
+ const tmpSiblingRule = this.getFacetRule(pSiblingInput);
395
+ if (tmpSiblingRule && (tmpSiblingRule.JoinListAddress === tmpRule.JoinListAddress))
396
+ {
397
+ tmpSiblings.push({ Input: pSiblingInput, Rule: tmpSiblingRule, Value: fGetValue(pSiblingInput) });
398
+ }
399
+ };
400
+ const tmpIsTabular = (typeof(pRowIndex) === 'number') || ((typeof(pRowIndex) === 'string') && (pRowIndex !== ''));
401
+ if (tmpIsTabular)
402
+ {
403
+ const tmpGroupIndex = pInput.PictForm.GroupIndex;
404
+ const tmpGroup = pView.getGroup(tmpGroupIndex);
405
+ const tmpRecord = pView.getTabularRecordData(tmpGroupIndex, pRowIndex) || {};
406
+ const tmpAddresses = (tmpGroup && tmpGroup.supportingManifest) ? tmpGroup.supportingManifest.elementAddresses : [];
407
+ for (let i = 0; i < tmpAddresses.length; i++)
408
+ {
409
+ const tmpSiblingInput = pView.getTabularRecordInput(tmpGroupIndex, i);
410
+ if (tmpSiblingInput)
411
+ {
412
+ fAdd(tmpSiblingInput, (pSibling) => tmpGroup.supportingManifest.getValueByHash(tmpRecord, pSibling.DataAddress || tmpAddresses[i]));
413
+ }
414
+ }
415
+ return tmpSiblings;
416
+ }
417
+ const tmpDescriptors = (pView.sectionManifest && pView.sectionManifest.elementDescriptors) ? pView.sectionManifest.elementDescriptors : {};
418
+ for (const tmpAddress of Object.keys(tmpDescriptors))
419
+ {
420
+ const tmpSiblingInput = tmpDescriptors[tmpAddress];
421
+ if (tmpSiblingInput && tmpSiblingInput.PictForm && !tmpSiblingInput.IsTabular)
422
+ {
423
+ fAdd(tmpSiblingInput, (pSibling) => pView.getValueByHash(pSibling.Hash));
424
+ }
425
+ }
426
+ return tmpSiblings;
427
+ }
428
+
429
+ /**
430
+ * @param {any} pValue
431
+ * @return {boolean}
432
+ */
433
+ isEmptyFacetValue(pValue)
434
+ {
435
+ return (pValue === undefined) || (pValue === null) || (pValue === '');
436
+ }
437
+
438
+ /**
439
+ * The values a Facet rule's dropdown offers: the allowed values (narrowed per the rule's NarrowBy), plus
440
+ * the input's own current value unless KeepCurrentValue is false, so a filter never hides what the row
441
+ * already stores (a saved choice that has since fallen out of scope still shows).
442
+ *
443
+ * @param {Object} pView
444
+ * @param {Record<string, any>} pInput
445
+ * @param {Record<string, any>} pRule - the input's Facet rule
446
+ * @param {number} [pRowIndex]
447
+ * @return {Set<string>}
448
+ */
449
+ getFacetOfferedValues(pView, pInput, pRule, pRowIndex)
450
+ {
451
+ const tmpSiblings = this.getFacetSiblings(pView, pInput, pRowIndex);
452
+ const tmpOffered = this.getFacetAllowedValues(pView, pInput, pRule, pRowIndex, tmpSiblings, pRule.NarrowBy);
453
+ const tmpSelf = tmpSiblings.find((pSibling) => pSibling.Input === pInput);
454
+ if ((pRule.KeepCurrentValue !== false) && tmpSelf && !this.isEmptyFacetValue(tmpSelf.Value))
455
+ {
456
+ tmpOffered.add(String(tmpSelf.Value));
457
+ }
458
+ return tmpOffered;
459
+ }
460
+
461
+ /**
462
+ * @param {any} pJoinValue - the value on the join record
463
+ * @param {string} pOperator - EQ (default), NE, LT, LE, GT or GE
464
+ * @param {string} pValue - the constraint value
465
+ * @return {boolean}
466
+ */
467
+ compareFacetValue(pJoinValue, pOperator, pValue)
468
+ {
469
+ if (!pOperator || (pOperator === 'EQ'))
470
+ {
471
+ return String(pJoinValue) === pValue;
472
+ }
473
+ if (pOperator === 'NE')
474
+ {
475
+ return String(pJoinValue) !== pValue;
476
+ }
477
+ if (this.isEmptyFacetValue(pJoinValue))
478
+ {
479
+ return false;
480
+ }
481
+ // Numbers compare as numbers; anything else compares as text, and a bare YYYY-MM-DD compares against
482
+ // only the date part of an ISO timestamp so a record dated that day is on the day, not after it.
483
+ const tmpNumeric = (pJoinValue !== '') && (pValue !== '') && !isNaN(Number(pJoinValue)) && !isNaN(Number(pValue));
484
+ let tmpLeft = tmpNumeric ? Number(pJoinValue) : String(pJoinValue);
485
+ if (!tmpNumeric && /^\d{4}-\d{2}-\d{2}$/.test(pValue) && /^\d{4}-\d{2}-\d{2}T/.test(tmpLeft))
486
+ {
487
+ tmpLeft = tmpLeft.substring(0, 10);
488
+ }
489
+ const tmpRight = tmpNumeric ? Number(pValue) : pValue;
490
+ switch (pOperator)
491
+ {
492
+ case 'LT':
493
+ return tmpLeft < tmpRight;
494
+ case 'LE':
495
+ return tmpLeft <= tmpRight;
496
+ case 'GT':
497
+ return tmpLeft > tmpRight;
498
+ case 'GE':
499
+ return tmpLeft >= tmpRight;
500
+ default:
501
+ this.log.warn(`Facet constraint operator [${pOperator}] is not supported; the constraint is ignored.`);
502
+ return true;
503
+ }
504
+ }
505
+
506
+ /**
507
+ * The values a Facet rule's input may hold, given what its siblings hold: the distinct
508
+ * JoinListValueAddress values of the join records that agree with every constraining non-empty sibling
509
+ * value and every explicit facet. Empty siblings do not constrain.
510
+ *
511
+ * pNarrowBy picks the constraining siblings: 'All' (default) is every sibling; 'Preceding' is only the
512
+ * siblings before this input (column order in a table), so a list offers every value its upstream
513
+ * choices allow and picking one never needs a downstream value cleared first.
514
+ *
515
+ * @param {Object} pView
516
+ * @param {Record<string, any>} pInput
517
+ * @param {Record<string, any>} pRule - the input's Facet rule
518
+ * @param {number} [pRowIndex]
519
+ * @param {Array<{ Input: Record<string, any>, Rule: Record<string, any>, Value: any }>} [pSiblings] - precomputed siblings (values may be overridden by the caller)
520
+ * @param {string} [pNarrowBy] - 'All' or 'Preceding'
521
+ * @return {Set<string>}
522
+ */
523
+ getFacetAllowedValues(pView, pInput, pRule, pRowIndex, pSiblings, pNarrowBy)
524
+ {
525
+ const tmpAllowed = new Set();
526
+ for (const tmpJoinRecord of this.getFacetMatchingRecords(pView, pInput, pRule, pRowIndex, pSiblings, pNarrowBy))
527
+ {
528
+ const tmpCandidate = this.pict.manifest.getValueAtAddress(tmpJoinRecord, pRule.JoinListValueAddress);
529
+ if (!this.isEmptyFacetValue(tmpCandidate))
530
+ {
531
+ tmpAllowed.add(String(tmpCandidate));
532
+ }
533
+ }
534
+ return tmpAllowed;
535
+ }
536
+
537
+ /**
538
+ * The join records that agree with every constraining non-empty sibling value and every explicit facet;
539
+ * see getFacetAllowedValues for pNarrowBy. pInput's own value does not constrain.
540
+ *
541
+ * @param {Object} pView
542
+ * @param {Record<string, any>} pInput
543
+ * @param {Record<string, any>} pRule - the input's Facet rule
544
+ * @param {number} [pRowIndex]
545
+ * @param {Array<{ Input: Record<string, any>, Rule: Record<string, any>, Value: any }>} [pSiblings]
546
+ * @param {string} [pNarrowBy] - 'All' or 'Preceding'
547
+ * @return {Array<Record<string, any>>}
548
+ */
549
+ getFacetMatchingRecords(pView, pInput, pRule, pRowIndex, pSiblings, pNarrowBy)
550
+ {
551
+ const tmpConstraints = [];
552
+ const tmpSiblings = pSiblings || this.getFacetSiblings(pView, pInput, pRowIndex);
553
+ const tmpOwnIndex = tmpSiblings.findIndex((pSibling) => pSibling.Input === pInput);
554
+ for (let i = 0; i < tmpSiblings.length; i++)
555
+ {
556
+ const tmpSibling = tmpSiblings[i];
557
+ if ((pNarrowBy === 'Preceding') && (tmpOwnIndex >= 0) && (i > tmpOwnIndex))
558
+ {
559
+ continue;
560
+ }
561
+ if ((tmpSibling.Input !== pInput) && !this.isEmptyFacetValue(tmpSibling.Value))
562
+ {
563
+ tmpConstraints.push({ Address: tmpSibling.Rule.JoinListValueAddress, Value: String(tmpSibling.Value) });
564
+ }
565
+ }
566
+ for (const tmpFacet of (Array.isArray(pRule.Facets) ? pRule.Facets : []))
567
+ {
568
+ const tmpValue = ('ValueAddress' in tmpFacet) ? pView.getValueByHash(tmpFacet.ValueAddress) : tmpFacet.Value;
569
+ if (!this.isEmptyFacetValue(tmpValue))
570
+ {
571
+ tmpConstraints.push({ Address: tmpFacet.JoinListValueAddress, Value: String(tmpValue), Operator: tmpFacet.Operator });
572
+ }
573
+ }
574
+ return this.getFacetJoinList(pView, pRule).filter((pJoinRecord) =>
575
+ {
576
+ for (const tmpConstraint of tmpConstraints)
577
+ {
578
+ if (!this.compareFacetValue(this.pict.manifest.getValueAtAddress(pJoinRecord, tmpConstraint.Address), tmpConstraint.Operator, tmpConstraint.Value))
579
+ {
580
+ return false;
581
+ }
582
+ }
583
+ return true;
584
+ });
585
+ }
321
586
  }
322
587
 
323
588
  module.exports = PictListDistilling;
@@ -191,8 +191,17 @@ class PictMetalist extends libPictProvider
191
191
  return false;
192
192
  }
193
193
 
194
- // Now try to fetch the list data
195
- let tmpListData = this.pict.providers.DataBroker.getValueByHash(pListObject.ListSourceAddress);
194
+ // Now try to fetch the list data; a global source reads application state rather than the form data.
195
+ let tmpListData;
196
+ if (pListObject.ListSourceAddressGlobal)
197
+ {
198
+ const tmpAddressSpace = { Fable: this.pict, Pict: this.pict, AppData: this.pict.AppData, Bundle: this.pict.Bundle };
199
+ tmpListData = this.pict.manifest.getValueAtAddress(tmpAddressSpace, pListObject.ListSourceAddress);
200
+ }
201
+ else
202
+ {
203
+ tmpListData = this.pict.providers.DataBroker.getValueByHash(pListObject.ListSourceAddress);
204
+ }
196
205
  if (!tmpListData)
197
206
  {
198
207
  //this.log.warn(`Dynamic MetaList Provider [${this.UUID}]::[${this.Hash}] failed to fetch the list data for PickList [${pListObject.Hash}]. Skipping.`);
@@ -215,6 +224,11 @@ class PictMetalist extends libPictProvider
215
224
 
216
225
  if (!tmpTextString || !tmpIDString)
217
226
  {
227
+ // A list over an optional field (SkipEmpty) expects records without it.
228
+ if (pListObject.SkipEmpty)
229
+ {
230
+ continue;
231
+ }
218
232
  this.log.error(`Dynamic MetaList Provider [${this.UUID}]::[${this.Hash}] failed to generate the Text or ID for PickList [${pListObject.Hash}]. Skipping.`);
219
233
  // this.log.error(`Dynamic MetaList Provider [${this.UUID}]::[${this.Hash}] failed to generate the Text or ID for PickList [${pListObject.Hash}]. Skipping.`, {"Text": tmpTextString, "ID": tmpIDString, "Entry": tmpListEntry});
220
234
  continue;
@@ -0,0 +1,232 @@
1
+ const libPictSectionInputExtension = require('../Pict-Provider-InputExtension.js');
2
+
3
+ /**
4
+ * FacetFilter input provider.
5
+ *
6
+ * Keeps a set of inputs that choose from one shared join list (their `Facet` ListFilterRules name the same
7
+ * `JoinListAddress`) mutually consistent when one of them changes. In a tabular group the set is the
8
+ * facet inputs in the changed row only; outside a table it is the facet inputs in the same view.
9
+ *
10
+ * On a change it runs, before the form solves:
11
+ * 1. ClearInvalid (default true): walking the siblings in column order, a sibling whose value no longer
12
+ * appears in any join record agreeing with the changed value (and the siblings already kept) is cleared.
13
+ * 2. AutoFillSingle (default true): an empty sibling left with exactly one allowed value takes it, repeated
14
+ * until nothing changes. The changed input itself is never filled, so clearing it sticks.
15
+ * 3. CarryFields ({ "<row address>": "<join record address>" }): on a sibling that holds a value, the join
16
+ * record its facet set now identifies (exactly one agreeing record) is copied into the row; with no value,
17
+ * or no single record, those row fields are cleared.
18
+ *
19
+ * It only acts on a user change, never on load or marshal, so a saved value -- carried fields included -- is
20
+ * never rewritten by opening a document or by the join list changing later. Narrowing the offered options
21
+ * is the `Facet` rule's job in ListDistilling.filterList.
22
+ *
23
+ * ListFilterRules: [ { FilterType: "Facet", JoinListAddress: "AppData.Graph", JoinListAddressGlobal: true, JoinListValueAddress: "Colour" } ],
24
+ * Providers: [ "Pict-Input-FacetFilter" ]
25
+ *
26
+ * @class
27
+ * @extends libPictSectionInputExtension
28
+ * @memberof providers.inputs
29
+ */
30
+ class FacetFilterInputHandler extends libPictSectionInputExtension
31
+ {
32
+ /**
33
+ * @param {import('fable')} pFable - The Fable instance.
34
+ * @param {Record<string, any>} [pOptions] - The options for the provider.
35
+ * @param {string} [pServiceHash] - The service hash for the provider.
36
+ */
37
+ constructor(pFable, pOptions, pServiceHash)
38
+ {
39
+ super(pFable, pOptions, pServiceHash);
40
+
41
+ /** @type {import('pict')} */
42
+ this.pict;
43
+ /** @type {any} */
44
+ this.log;
45
+
46
+ // Writing a sibling runs that sibling's change providers (this one included); only the user's change reconciles.
47
+ this.reconciling = false;
48
+ }
49
+
50
+ /**
51
+ * @param {Object} pView
52
+ * @param {Record<string, any>} pInput
53
+ * @param {number|undefined} pRowIndex - undefined outside a table
54
+ * @param {any} pValue
55
+ */
56
+ setFacetValue(pView, pInput, pRowIndex, pValue)
57
+ {
58
+ if ((typeof(pRowIndex) === 'number') || ((typeof(pRowIndex) === 'string') && (pRowIndex !== '')))
59
+ {
60
+ pView.setDataTabularByHash(pInput.PictForm.GroupIndex, pInput.Hash, pRowIndex, pValue);
61
+ return;
62
+ }
63
+ pView.setDataByInput(pInput, pValue);
64
+ }
65
+
66
+ /**
67
+ * Reconciles the changed input's facet siblings; see the class description.
68
+ *
69
+ * @param {Object} pView
70
+ * @param {Record<string, any>} pInput - the input the user changed
71
+ * @param {number} [pRowIndex]
72
+ * @return {Array<string>} hashes of the siblings whose value was changed
73
+ */
74
+ reconcileFacets(pView, pInput, pRowIndex)
75
+ {
76
+ if (this.reconciling)
77
+ {
78
+ return [];
79
+ }
80
+ this.reconciling = true;
81
+ try
82
+ {
83
+ return this.reconcileFacetSiblings(pView, pInput, pRowIndex);
84
+ }
85
+ finally
86
+ {
87
+ this.reconciling = false;
88
+ }
89
+ }
90
+
91
+ /**
92
+ * @param {Object} pView
93
+ * @param {Record<string, any>} pInput - the input the user changed
94
+ * @param {number} [pRowIndex]
95
+ * @return {Array<string>} hashes of the siblings whose value was changed
96
+ */
97
+ reconcileFacetSiblings(pView, pInput, pRowIndex)
98
+ {
99
+ const tmpDistilling = this.pict.providers.ListDistilling;
100
+ const tmpSiblings = tmpDistilling.getFacetSiblings(pView, pInput, pRowIndex);
101
+ const tmpChanged = [];
102
+ if (tmpSiblings.length < 2)
103
+ {
104
+ return tmpChanged;
105
+ }
106
+
107
+ const tmpKept = tmpSiblings.map((pSibling) => (pSibling.Input === pInput));
108
+ for (let i = 0; i < tmpSiblings.length; i++)
109
+ {
110
+ const tmpSibling = tmpSiblings[i];
111
+ if (tmpKept[i] || tmpDistilling.isEmptyFacetValue(tmpSibling.Value))
112
+ {
113
+ tmpKept[i] = true;
114
+ continue;
115
+ }
116
+ const tmpConstraining = tmpSiblings.map((pOther, pIndex) => ({ Input: pOther.Input, Rule: pOther.Rule, Value: tmpKept[pIndex] ? pOther.Value : '' }));
117
+ const tmpAllowed = tmpDistilling.getFacetAllowedValues(pView, tmpSibling.Input, tmpSibling.Rule, pRowIndex, tmpConstraining);
118
+ if (tmpAllowed.has(String(tmpSibling.Value)) || (tmpSibling.Rule.ClearInvalid === false))
119
+ {
120
+ tmpKept[i] = true;
121
+ continue;
122
+ }
123
+ tmpSibling.Value = '';
124
+ this.setFacetValue(pView, tmpSibling.Input, pRowIndex, '');
125
+ tmpChanged.push(tmpSibling.Input.Hash);
126
+ tmpKept[i] = true;
127
+ }
128
+
129
+ for (let tmpPass = 0; tmpPass < tmpSiblings.length; tmpPass++)
130
+ {
131
+ let tmpFilled = false;
132
+ for (const tmpSibling of tmpSiblings)
133
+ {
134
+ if ((tmpSibling.Input === pInput) || !tmpDistilling.isEmptyFacetValue(tmpSibling.Value) || (tmpSibling.Rule.AutoFillSingle === false))
135
+ {
136
+ continue;
137
+ }
138
+ const tmpAllowed = tmpDistilling.getFacetAllowedValues(pView, tmpSibling.Input, tmpSibling.Rule, pRowIndex, tmpSiblings);
139
+ if (tmpAllowed.size === 1)
140
+ {
141
+ tmpSibling.Value = tmpAllowed.values().next().value;
142
+ this.setFacetValue(pView, tmpSibling.Input, pRowIndex, tmpSibling.Value);
143
+ tmpChanged.push(tmpSibling.Input.Hash);
144
+ tmpFilled = true;
145
+ }
146
+ }
147
+ if (!tmpFilled)
148
+ {
149
+ break;
150
+ }
151
+ }
152
+
153
+ for (const tmpSibling of tmpSiblings)
154
+ {
155
+ if (tmpSibling.Rule.CarryFields && (typeof(tmpSibling.Rule.CarryFields) === 'object'))
156
+ {
157
+ this.carryFields(pView, tmpSibling, pRowIndex, tmpSiblings);
158
+ }
159
+ }
160
+ return tmpChanged;
161
+ }
162
+
163
+ /**
164
+ * Copies a sibling's CarryFields from the one join record its facet set identifies, or clears them.
165
+ *
166
+ * @param {Object} pView
167
+ * @param {{ Input: Record<string, any>, Rule: Record<string, any>, Value: any }} pSibling
168
+ * @param {number|undefined} pRowIndex
169
+ * @param {Array<{ Input: Record<string, any>, Rule: Record<string, any>, Value: any }>} pSiblings
170
+ */
171
+ carryFields(pView, pSibling, pRowIndex, pSiblings)
172
+ {
173
+ const tmpDistilling = this.pict.providers.ListDistilling;
174
+ let tmpRecord = null;
175
+ if (!tmpDistilling.isEmptyFacetValue(pSibling.Value))
176
+ {
177
+ const tmpMatches = tmpDistilling.getFacetMatchingRecords(pView, pSibling.Input, pSibling.Rule, pRowIndex, pSiblings)
178
+ .filter((pJoinRecord) => String(this.pict.manifest.getValueAtAddress(pJoinRecord, pSibling.Rule.JoinListValueAddress)) === String(pSibling.Value));
179
+ tmpRecord = (tmpMatches.length === 1) ? tmpMatches[0] : null;
180
+ }
181
+ const tmpIsTabular = (typeof(pRowIndex) === 'number') || ((typeof(pRowIndex) === 'string') && (pRowIndex !== ''));
182
+ const tmpTarget = tmpIsTabular ? pView.getTabularRecordData(pSibling.Input.PictForm.GroupIndex, pRowIndex) : pView.getMarshalDestinationObject();
183
+ if (!tmpTarget)
184
+ {
185
+ return;
186
+ }
187
+ for (const tmpRowAddress of Object.keys(pSibling.Rule.CarryFields))
188
+ {
189
+ const tmpValue = tmpRecord ? this.pict.manifest.getValueAtAddress(tmpRecord, pSibling.Rule.CarryFields[tmpRowAddress]) : '';
190
+ this.pict.manifest.setValueAtAddress(tmpTarget, tmpRowAddress, (tmpValue === undefined) ? '' : tmpValue);
191
+ }
192
+ }
193
+
194
+ /**
195
+ * @param {Object} pView - The view object.
196
+ * @param {Object} pInput - The input object.
197
+ * @param {any} pValue - The new value.
198
+ * @param {string} pHTMLSelector - The HTML selector.
199
+ * @param {string} pTransactionGUID - The transaction GUID, if any.
200
+ * @returns {any}
201
+ */
202
+ onDataChange(pView, pInput, pValue, pHTMLSelector, pTransactionGUID)
203
+ {
204
+ this.reconcileFacets(pView, pInput);
205
+ return super.onDataChange(pView, pInput, pValue, pHTMLSelector, pTransactionGUID);
206
+ }
207
+
208
+ /**
209
+ * @param {Object} pView - The view object.
210
+ * @param {Object} pInput - The input object.
211
+ * @param {any} pValue - The new value.
212
+ * @param {string} pHTMLSelector - The HTML selector.
213
+ * @param {number} pRowIndex - The index of the row.
214
+ * @param {string} pTransactionGUID - The transaction GUID, if any.
215
+ * @returns {any}
216
+ */
217
+ onDataChangeTabular(pView, pInput, pValue, pHTMLSelector, pRowIndex, pTransactionGUID)
218
+ {
219
+ this.reconcileFacets(pView, pInput, pRowIndex);
220
+ return super.onDataChangeTabular(pView, pInput, pValue, pHTMLSelector, pRowIndex, pTransactionGUID);
221
+ }
222
+ }
223
+
224
+ module.exports = FacetFilterInputHandler;
225
+
226
+ module.exports.default_configuration = (
227
+ {
228
+ "ProviderIdentifier": "Pict-Input-FacetFilter",
229
+ "AutoInitialize": true,
230
+ "AutoInitializeOrdinal": 0,
231
+ "AutoSolveWithApp": false
232
+ });
@@ -61,6 +61,29 @@ class CustomInputHandler extends libPictSectionInputExtension
61
61
  return `#SELECT-TABULAR-DROPDOWN-${pInputHTMLID}-${pRowIndex}`;
62
62
  }
63
63
 
64
+ /**
65
+ * Records the option ids on the element and reports whether they differ from what it last rendered,
66
+ * so a refresh on every marshal does not rebuild (and blur) a dropdown whose options are unchanged.
67
+ *
68
+ * @param {HTMLElement} pSelectElement
69
+ * @param {Array<{ id: string, text: string }>} pListData
70
+ * @return {boolean}
71
+ */
72
+ optionsChanged(pSelectElement, pListData)
73
+ {
74
+ if (!pSelectElement || !pSelectElement.setAttribute)
75
+ {
76
+ return true;
77
+ }
78
+ const tmpSignature = Array.isArray(pListData) ? pListData.map((pEntry) => `${pEntry.id}\u0001${pEntry.text}`).join('\u0002') : '';
79
+ if (pSelectElement.getAttribute('data-pict-options') === tmpSignature)
80
+ {
81
+ return false;
82
+ }
83
+ pSelectElement.setAttribute('data-pict-options', tmpSignature);
84
+ return true;
85
+ }
86
+
64
87
  /**
65
88
  * Refreshes the select list for a dynamic input.
66
89
  * @param {Object} pView - The view object.
@@ -91,6 +114,10 @@ class CustomInputHandler extends libPictSectionInputExtension
91
114
  tmpListData = this.pict.providers.ListDistilling.filterList(pView, pInput, tmpListData);
92
115
 
93
116
  tmpInputSelectElement = tmpInputSelectElement[0];
117
+ if (!this.optionsChanged(tmpInputSelectElement, tmpListData))
118
+ {
119
+ return;
120
+ }
94
121
  // HAX
95
122
  tmpInputSelectElement.innerHTML = '';
96
123
 
@@ -128,7 +155,7 @@ class CustomInputHandler extends libPictSectionInputExtension
128
155
  tmpListData = this.pict.providers.DynamicMetaLists.getList(pInput.PictForm.SelectOptionsPickList);
129
156
  }
130
157
 
131
- tmpListData = this.pict.providers.ListDistilling.filterList(pView, pInput, tmpListData);
158
+ tmpListData = this.pict.providers.ListDistilling.filterList(pView, pInput, tmpListData, pRowIndex);
132
159
 
133
160
  // TODO: Determine later if this should ever be an array.
134
161
  if (!Array.isArray(tmpInputSelectElement) || tmpInputSelectElement.length < 1)
@@ -138,6 +165,11 @@ class CustomInputHandler extends libPictSectionInputExtension
138
165
 
139
166
  tmpInputSelectElement = tmpInputSelectElement[0];
140
167
 
168
+ if (!this.optionsChanged(tmpInputSelectElement, tmpListData))
169
+ {
170
+ return;
171
+ }
172
+
141
173
  // HAX
142
174
  tmpInputSelectElement.innerHTML = '';
143
175
 
@@ -234,6 +266,10 @@ class CustomInputHandler extends libPictSectionInputExtension
234
266
  */
235
267
  onDataMarshalToForm(pView, pGroup, pRow, pInput, pValue, pHTMLSelector, pTransactionGUID)
236
268
  {
269
+ if (this.pict.providers.ListDistilling.getFacetRule(pInput))
270
+ {
271
+ this.refreshSelectList(pView, pGroup, pRow, pInput, pValue, pHTMLSelector);
272
+ }
237
273
  /** @type {Array<HTMLElement>|HTMLElement} */
238
274
  const tmpInputSelectElements = this.pict.ContentAssignment.getElement(this.getSelectInputHTMLID(pInput.Macro.RawHTMLID));
239
275
  if (!tmpInputSelectElements || tmpInputSelectElements.length < 1)
@@ -281,6 +317,10 @@ class CustomInputHandler extends libPictSectionInputExtension
281
317
  */
282
318
  onDataMarshalToFormTabular(pView, pGroup, pInput, pValue, pHTMLSelector, pRowIndex, pTransactionGUID)
283
319
  {
320
+ if (this.pict.providers.ListDistilling.getFacetRule(pInput))
321
+ {
322
+ this.refreshSelectListTabular(pView, pGroup, null, pInput, pValue, pHTMLSelector, pRowIndex);
323
+ }
284
324
  /** @type {Array<HTMLElement>|HTMLElement} */
285
325
  const tmpInputSelectElements = this.pict.ContentAssignment.getElement(this.getTabularSelectDropdownID(pInput.Macro.RawHTMLID, pRowIndex));
286
326
  if (!tmpInputSelectElements || tmpInputSelectElements.length < 1)