pict-section-form 1.3.4 → 1.3.6

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.
package/CONTRIBUTING.md CHANGED
@@ -47,4 +47,4 @@ When in doubt, match what the surrounding code does.
47
47
 
48
48
  ## Repository Structure
49
49
 
50
- Each module is its own git repository. The [retold](https://github.com/stevenvelozo/retold) repository tracks module organization but does not contain module source code. Direct your pull request to the specific module repository where your change belongs.
50
+ Each module is its own git repository. The [retold](https://github.com/fable-retold/retold) repository tracks module organization but does not contain module source code. Direct your pull request to the specific module repository where your change belongs.
package/README.md CHANGED
@@ -271,7 +271,7 @@ of data in your application.
271
271
 
272
272
  ## Documentation
273
273
 
274
- The complete documentation - an interactive guide with live examples - is hosted at **[stevenvelozo.github.io/pict-section-form](https://fable-retold.github.io/pict-section-form/)**:
274
+ The complete documentation - an interactive guide with live examples - is hosted at **[fable-retold.github.io/pict-section-form](https://fable-retold.github.io/pict-section-form/)**:
275
275
 
276
276
  | Document | Description |
277
277
  |----------|-------------|
@@ -365,4 +365,4 @@ MIT
365
365
 
366
366
  ## Contributing
367
367
 
368
- Pull requests are welcome. For details on our code of conduct, contribution process, and testing requirements, see the [Retold Contributing Guide](https://github.com/stevenvelozo/retold/blob/main/docs/contributing.md).
368
+ Pull requests are welcome. For details on our code of conduct, contribution process, and testing requirements, see the [Retold Contributing Guide](https://github.com/fable-retold/retold/blob/main/docs/contributing.md).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pict-section-form",
3
- "version": "1.3.4",
3
+ "version": "1.3.6",
4
4
  "description": "Pict dynamic forms",
5
5
  "main": "source/Pict-Section-Form.js",
6
6
  "directories": {
@@ -330,7 +330,7 @@ class DynamicTabularData extends libPictProvider
330
330
  // Render (source + dependent views) BEFORE solving so dependent DynamicColumns tables
331
331
  // don't blank until the next edit, and the solve's DOM side effects survive. Matches
332
332
  // the add/delete handlers' order.
333
- this._repaintAfterRowReorder(tmpGroup);
333
+ this._repaintAfterRowReorder(pView, tmpGroup);
334
334
  }
335
335
  }
336
336
  }
@@ -375,7 +375,7 @@ class DynamicTabularData extends libPictProvider
375
375
  // (e.g. SetGroupVisibility hiding a validation message) act on the freshly rebuilt
376
376
  // DOM and survive, and so dependent DynamicColumns tables don't blank until the next
377
377
  // edit. Matches the add/delete handlers' order.
378
- this._repaintAfterRowReorder(tmpGroup);
378
+ this._repaintAfterRowReorder(pView, tmpGroup);
379
379
  }
380
380
  }
381
381
  }
@@ -425,7 +425,7 @@ class DynamicTabularData extends libPictProvider
425
425
  // (e.g. SetGroupVisibility hiding a validation message) act on the freshly rebuilt
426
426
  // DOM and survive, and so dependent DynamicColumns tables don't blank until the next
427
427
  // edit. Matches the add/delete handlers' order.
428
- this._repaintAfterRowReorder(tmpGroup);
428
+ this._repaintAfterRowReorder(pView, tmpGroup);
429
429
  }
430
430
  }
431
431
  }
@@ -843,43 +843,34 @@ class DynamicTabularData extends libPictProvider
843
843
  *
844
844
  * @param {Object} pGroup - The reordered group (its RecordSetAddress drives dependents).
845
845
  */
846
- _repaintAfterRowReorder(pGroup)
846
+ _repaintAfterRowReorder(pView, pGroup)
847
847
  {
848
- // Render every view that renders this record set as a table (the source table itself
849
- // plus any sibling views bound to the same RecordSetAddress).
850
- let tmpViewsToRender = this.pict.views.PictFormMetacontroller.filterViews(
851
- (pViewToTestForGroup) =>
852
- {
853
- if (!pViewToTestForGroup.isPictSectionForm)
854
- {
855
- return false;
856
- }
857
- let tmpGroupsToTest = pViewToTestForGroup.getGroups();
858
- for (let i = 0; i < tmpGroupsToTest.length; i++)
859
- {
860
- if (tmpGroupsToTest[i].RecordSetAddress == pGroup.RecordSetAddress)
861
- {
862
- return true;
863
- }
864
- }
865
- return false;
866
- });
867
- // Re-rendering an entity-selector table clobbers its selected values in the
868
- // model, so preserve them across the render.
869
- this._renderViewsPreservingRecordSet(pGroup.RecordSetAddress, tmpViewsToRender);
870
-
871
- // Rebuild any OTHER views whose DynamicColumns are sourced from this record set
872
- // HERE, in the render phase -- BEFORE the marshal below -- so the column DOM is
873
- // correct (and re-labeled to the new order) when the marshal fills it. Without
874
- // this the dependent table is only rebuilt mid-marshal (onDataMarshalToForm),
875
- // which renders AFTER the cells were filled and blanks them until the next edit.
848
+ // A reorder (move up/down/set-index) preserves the row COUNT -- the source table's DOM rows
849
+ // already exist; only which row holds which value changes. A full source-view render() would
850
+ // tear down and re-create every cell widget, and each entity-selector select2 re-init costs
851
+ // roughly O(document size), so on a large form that is SECONDS per move. Skip the source
852
+ // render and let the marshal below push the reordered model values into the existing DOM rows
853
+ // in place. (Add/delete change the row count and still render via their own paths.) Skipping
854
+ // the render also removes the need for the snapshot/restore in _renderViewsPreservingRecordSet:
855
+ // with no destructive render, the entity values are never clobbered.
856
+
857
+ // Dependent DynamicColumns views still need their generated column labels re-resolved for the
858
+ // new order (cheap -- those tables carry no entity widgets).
876
859
  this._rebuildDependentDynamicColumnViews(pGroup.RecordSetAddress);
877
860
 
878
- // Run the solver
861
+ // Run the solver, then marshal the model back out to the existing form DOM.
879
862
  this.pict.providers.DynamicSolver.solveViews();
880
-
881
- // We've re-rendered but we don't know what needs to be marshaled based on the solve that ran above so marshal everything
882
863
  this.pict.views.PictFormMetacontroller.marshalFormSections();
864
+
865
+ // marshalFormSections updates simple cells in place, but a stateful widget (an entity
866
+ // selector's select2) will not switch to a reordered value whose <option> isn't the one it
867
+ // currently shows. Ask each input provider to repaint its display for the new row order:
868
+ // entity selectors force their select2 to the row's current value, everything else no-ops.
869
+ // Cheap (in-place value set, no widget teardown) versus the full re-render this replaced.
870
+ if (pView && (typeof pView.runInputProviderFunctions === 'function'))
871
+ {
872
+ pView.runInputProviderFunctions('onTabularRowReorderRepaint', null, null, this.fable.getUUID());
873
+ }
883
874
  }
884
875
  }
885
876
 
@@ -156,6 +156,45 @@ class PictInputExtensionProvider extends libPictProvider
156
156
  return true;
157
157
  }
158
158
 
159
+ /**
160
+ * Repaint this input's rendered display from the model after a tabular row REORDER
161
+ * (move up/down/set-index), which preserves the row count but changes which row holds
162
+ * which value. A plain marshal updates simple inputs in place, but stateful widgets
163
+ * (e.g. an entity-selector's select2) will not switch to a value whose option isn't the
164
+ * one currently shown; those providers override this to force their display to the row's
165
+ * current value. Default is a no-op.
166
+ *
167
+ * @param {Object} pView - The view object.
168
+ * @param {Object} pGroup - The group definition object.
169
+ * @param {number} pRow - The row index.
170
+ * @param {Object} pInput - The input object.
171
+ * @param {any} pValue - The value to display.
172
+ * @param {string} pHTMLSelector - The HTML selector.
173
+ * @param {string} pTransactionGUID - The transaction GUID for the event dispatch.
174
+ * @returns {boolean} - Returns true.
175
+ */
176
+ onTabularRowReorderRepaint(pView, pGroup, pRow, pInput, pValue, pHTMLSelector, pTransactionGUID)
177
+ {
178
+ return true;
179
+ }
180
+
181
+ /**
182
+ * Tabular sibling of onTabularRowReorderRepaint. Default is a no-op.
183
+ *
184
+ * @param {Object} pView - The view object.
185
+ * @param {Object} pGroup - The group definition object.
186
+ * @param {Object} pInput - The input object.
187
+ * @param {any} pValue - The value to display.
188
+ * @param {string} pHTMLSelector - The HTML selector.
189
+ * @param {number} pRowIndex - The row index of the tabular data.
190
+ * @param {string} pTransactionGUID - The transaction GUID for the event dispatch.
191
+ * @returns {boolean} - Returns true.
192
+ */
193
+ onTabularRowReorderRepaintTabular(pView, pGroup, pInput, pValue, pHTMLSelector, pRowIndex, pTransactionGUID)
194
+ {
195
+ return true;
196
+ }
197
+
159
198
  /**
160
199
  * Handles data requests for the Pict-Provider-InputExtension.
161
200
  *
@@ -1,5 +1,15 @@
1
1
  const libPictSectionGroupLayout = require('../Pict-Provider-DynamicLayout.js');
2
2
 
3
+ // The resolved --theme-color-* tokens carried onto a column-chooser popover when it is portaled to
4
+ // <body>: off its scoped container it would otherwise fall back to the stylesheet's hex defaults.
5
+ // Mirrors pict-section-picker's PictView-Picker `_THEME_TOKENS`.
6
+ const _COLCHOOSER_THEME_TOKENS =
7
+ [
8
+ '--theme-color-text-primary', '--theme-color-text-secondary', '--theme-color-text-muted',
9
+ '--theme-color-border-default', '--theme-color-border-light', '--theme-color-background-panel',
10
+ '--theme-color-background-tertiary',
11
+ ];
12
+
3
13
  class TabularLayout extends libPictSectionGroupLayout
4
14
  {
5
15
  constructor(pFable, pOptions, pServiceHash)
@@ -39,19 +49,24 @@ class TabularLayout extends libPictSectionGroupLayout
39
49
  + ' .pict-tabular-sort-control.pict-tabular-sort-asc, .pict-tabular-sort-control.pict-tabular-sort-desc { opacity: 1; }',
40
50
  500);
41
51
 
42
- // CSS for the optional ColumnChooser feature: a right-aligned trigger button
43
- // above the table and a fixed-position popover of checkbox rows. Fixed +
44
- // JS-positioned (like the recordset's column chooser) so no ancestor
45
- // overflow can clip it; the transparent backdrop catches outside clicks.
46
- this.pict.CSSMap.addCSS('Pict-Layout-Tabular-ColumnChooser-CSS', /*css*/`
47
- .pict-tabular-colchooser-bar { display: flex; justify-content: flex-end; margin: 0 0 0.35rem; }
52
+ // CSS for the optional ColumnChooser feature: a right-aligned trigger button above the
53
+ // table plus a checkbox popover. Anchored the same listener-free way as pict-section-picker:
54
+ // the popover is position:absolute under the (position:relative) bar so it rides page scroll
55
+ // for free, and is portaled to <body> in DOCUMENT coords only when a clipping ancestor would
56
+ // otherwise cut it -- no scroll/resize handlers. The transparent backdrop catches outside
57
+ // clicks. Dark-mode blocks (below, with DARK var() fallbacks) let the widget theme correctly
58
+ // even where the host has not defined the --theme-color-* tokens; a host that HAS defined
59
+ // them still wins, and the token values are carried across when the popover is portaled.
60
+ let tmpColumnChooserBaseCSS = /*css*/`
61
+ .pict-tabular-colchooser-bar { position: relative; display: flex; justify-content: flex-end; margin: 0 0 0.35rem; }
48
62
  .pict-tabular-colchooser-trigger { display: inline-flex; align-items: center; gap: 0.35rem; cursor: pointer; font: inherit; font-size: 0.88rem;
49
63
  padding: 0.3rem 0.65rem; border: 1px solid var(--theme-color-border-default, #d7dce3); border-radius: 7px;
50
64
  background: var(--theme-color-background-panel, #fff); color: var(--theme-color-text-secondary, #45505f); }
51
65
  .pict-tabular-colchooser-trigger:hover { background: var(--theme-color-background-tertiary, #eceef2); color: var(--theme-color-text-primary, #1f2733); }
52
66
  .pict-tabular-colchooser-count { font-size: 0.8em; color: var(--theme-color-text-muted, #6b7686); }
53
- .pict-tabular-colchooser-pop { position: fixed; z-index: 30; min-width: 220px; max-width: 320px; display: none; }
67
+ .pict-tabular-colchooser-pop { position: absolute; top: calc(100% + 6px); right: 0; z-index: 30; min-width: 220px; max-width: 320px; display: none; }
54
68
  .pict-tabular-colchooser-pop.open { display: block; }
69
+ .pict-tabular-colchooser-pop.pict-tabular-colchooser-pop-portal { right: auto; }
55
70
  .pict-tabular-colchooser-backdrop { position: fixed; inset: 0; z-index: 0; }
56
71
  .pict-tabular-colchooser-panel { position: relative; z-index: 1; display: flex; flex-direction: column; max-height: min(70vh, 420px);
57
72
  background: var(--theme-color-background-panel, #fff); border: 1px solid var(--theme-color-border-default, #d7dce3);
@@ -64,7 +79,29 @@ class TabularLayout extends libPictSectionGroupLayout
64
79
  .pict-tabular-colchooser-footer { flex: 0 0 auto; display: flex; justify-content: flex-end; padding: 0.45rem 0.7rem; border-top: 1px solid var(--theme-color-border-light, #e8ebf0); }
65
80
  .pict-tabular-colchooser-reset { font: inherit; font-size: 0.84rem; cursor: pointer; border: none; background: transparent; color: var(--theme-color-text-muted, #6b7686); padding: 0.15rem 0.3rem; border-radius: 5px; }
66
81
  .pict-tabular-colchooser-reset:hover { color: var(--theme-color-text-primary, #1f2733); background: var(--theme-color-background-tertiary, #eceef2); }
67
- `,
82
+ `;
83
+ // Dark-mode overrides: re-declare only the color-bearing rules with DARK var() fallbacks under
84
+ // each of the app's dark signals. Duplicated per selector (explicit [data-theme=dark] and
85
+ // .theme-dark, plus the system preference guarded by html:not([data-theme=light]) so an
86
+ // explicit light choice still wins) exactly as headlight-styles' bulma-*.scss does. Higher
87
+ // specificity than the base rules, so dark wins whenever its selector matches; the [data-theme]
88
+ // signal lives on <html>, so these still apply after a popover is portaled to <body>.
89
+ let fColumnChooserDarkRules = (pScope) => /*css*/`
90
+ ${pScope} .pict-tabular-colchooser-trigger { border-color: var(--theme-color-border-default, #3a4250); background: var(--theme-color-background-panel, #242b36); color: var(--theme-color-text-secondary, #c3cad4); }
91
+ ${pScope} .pict-tabular-colchooser-trigger:hover { background: var(--theme-color-background-tertiary, #2c3542); color: var(--theme-color-text-primary, #e6e9ee); }
92
+ ${pScope} .pict-tabular-colchooser-count { color: var(--theme-color-text-muted, #8b95a4); }
93
+ ${pScope} .pict-tabular-colchooser-panel { background: var(--theme-color-background-panel, #242b36); border-color: var(--theme-color-border-default, #3a4250); }
94
+ ${pScope} .pict-tabular-colchooser-row { color: var(--theme-color-text-primary, #e6e9ee); }
95
+ ${pScope} .pict-tabular-colchooser-row:hover { background: var(--theme-color-background-tertiary, #2c3542); }
96
+ ${pScope} .pict-tabular-colchooser-footer { border-top-color: var(--theme-color-border-light, #333b47); }
97
+ ${pScope} .pict-tabular-colchooser-reset { color: var(--theme-color-text-muted, #8b95a4); }
98
+ ${pScope} .pict-tabular-colchooser-reset:hover { color: var(--theme-color-text-primary, #e6e9ee); background: var(--theme-color-background-tertiary, #2c3542); }
99
+ `;
100
+ this.pict.CSSMap.addCSS('Pict-Layout-Tabular-ColumnChooser-CSS',
101
+ tmpColumnChooserBaseCSS
102
+ + fColumnChooserDarkRules('[data-theme="dark"]')
103
+ + fColumnChooserDarkRules('.theme-dark')
104
+ + `@media (prefers-color-scheme: dark) {` + fColumnChooserDarkRules('html:not([data-theme="light"])') + `}`,
68
105
  500);
69
106
  }
70
107
  }
@@ -1098,52 +1135,268 @@ class TabularLayout extends libPictSectionGroupLayout
1098
1135
  tmpPopoverElements[0].classList.toggle('open', !!pOpen);
1099
1136
  if (pOpen)
1100
1137
  {
1101
- this._positionTabularColumnChooserPopover(pView, pGroup, tmpPopoverElements[0]);
1138
+ this._applyTabularColumnChooserAnchor(pView, pGroup, tmpPopoverElements[0]);
1139
+ }
1140
+ else
1141
+ {
1142
+ // Bring a portaled popover home on close so a closed chooser never strands a node on <body>.
1143
+ this._restoreTabularColumnChooserPopover(pView, pGroup, tmpPopoverElements[0]);
1102
1144
  }
1103
1145
  }
1104
1146
 
1105
1147
  /**
1106
- * Position the (fixed) chooser popover against its trigger button, flipping
1107
- * above when the room below is genuinely cramped — same approach as the
1108
- * recordset's column chooser, so no ancestor overflow can clip it.
1148
+ * Anchor the (open) chooser popover to its trigger with the same listener-free strategy as
1149
+ * pict-section-picker: keep it in the DOM and CSS-absolute under the (position:relative) bar --
1150
+ * which rides page scroll for free -- UNLESS a clipping ancestor would cut it, in which case
1151
+ * portal it to <body> and place it in document coords. No scroll/resize handlers: an absolute
1152
+ * (not fixed) panel tracks the document as it scrolls, so one placement holds.
1109
1153
  *
1110
1154
  * @param {Object} pView
1111
1155
  * @param {Object} pGroup
1112
1156
  * @param {HTMLElement} pPopover
1113
1157
  */
1114
- _positionTabularColumnChooserPopover(pView, pGroup, pPopover)
1158
+ _applyTabularColumnChooserAnchor(pView, pGroup, pPopover)
1159
+ {
1160
+ if (!pPopover || (typeof document === 'undefined') || (typeof window === 'undefined'))
1161
+ {
1162
+ return;
1163
+ }
1164
+ let tmpTrigger = document.getElementById(this._getTabularColumnChooserElementId(pView, pGroup, 'TRIGGER'));
1165
+ if (!tmpTrigger)
1166
+ {
1167
+ return;
1168
+ }
1169
+ // Judge from the trigger (it never moves); a portaled panel has no clipping ancestors, so
1170
+ // testing the panel itself would flip-flop.
1171
+ if (this._tabularColumnChooserShouldPortal(tmpTrigger, pPopover))
1172
+ {
1173
+ this._portalTabularColumnChooserPopover(pPopover, tmpTrigger);
1174
+ }
1175
+ else
1176
+ {
1177
+ this._restoreTabularColumnChooserPopover(pView, pGroup, pPopover);
1178
+ }
1179
+ }
1180
+
1181
+ /**
1182
+ * Move the popover to <body> and place it against the trigger in document coords (viewport rect +
1183
+ * scroll), flipping above when the room below is genuinely cramped. Carries the resolved
1184
+ * --theme-color-* tokens across so the panel stays themed off its scoped container.
1185
+ *
1186
+ * @param {HTMLElement} pPopover
1187
+ * @param {HTMLElement} pTrigger
1188
+ */
1189
+ _portalTabularColumnChooserPopover(pPopover, pTrigger)
1115
1190
  {
1116
- let tmpTriggerElements = this.pict.ContentAssignment.getElement(`#${this._getTabularColumnChooserElementId(pView, pGroup, 'TRIGGER')}`);
1117
- if (!tmpTriggerElements.length || (typeof window === 'undefined'))
1191
+ if (!pPopover || !pTrigger || (typeof document === 'undefined') || (typeof window === 'undefined'))
1118
1192
  {
1119
1193
  return;
1120
1194
  }
1195
+ // Adopt this popover onto <body>, sweeping any stale same-id copy a prior re-render left behind.
1196
+ this._sweepPortaledColchooserOrphans(pPopover.getAttribute('id'), pPopover);
1197
+ if (pPopover.parentElement !== document.body)
1198
+ {
1199
+ document.body.appendChild(pPopover);
1200
+ }
1201
+ pPopover.classList.add('pict-tabular-colchooser-pop-portal');
1202
+ // Carry the resolved --theme-color-* tokens across: off its scoped container the panel would
1203
+ // otherwise fall back to the stylesheet hex defaults. Skip empty reads so a token the host never
1204
+ // defined keeps its own var(--token, #hex) fallback (matches pict-section-picker's _portalPop).
1205
+ if (typeof window.getComputedStyle === 'function')
1206
+ {
1207
+ let tmpComputed = window.getComputedStyle(pTrigger);
1208
+ _COLCHOOSER_THEME_TOKENS.forEach((pToken) =>
1209
+ {
1210
+ let tmpValue = tmpComputed.getPropertyValue(pToken);
1211
+ if (tmpValue && tmpValue.trim() !== '')
1212
+ {
1213
+ pPopover.style.setProperty(pToken, tmpValue.trim());
1214
+ }
1215
+ });
1216
+ }
1121
1217
  let tmpPanel = pPopover.querySelector('.pict-tabular-colchooser-panel');
1122
- let tmpRect = tmpTriggerElements[0].getBoundingClientRect();
1218
+ let tmpRect = pTrigger.getBoundingClientRect();
1123
1219
  let tmpGap = 6;
1124
1220
  let tmpMargin = 8;
1221
+ let tmpWidth = pPopover.offsetWidth || 240;
1125
1222
  let tmpViewportHeight = window.innerHeight;
1126
1223
  let tmpViewportWidth = window.innerWidth;
1127
- let tmpWidth = pPopover.offsetWidth || 240;
1128
- // Right-align the popover to the (right-aligned) trigger, clamped into the viewport.
1129
- pPopover.style.left = `${Math.round(Math.max(tmpMargin, Math.min(tmpRect.right - tmpWidth, tmpViewportWidth - tmpWidth - tmpMargin)))}px`;
1130
- pPopover.style.right = 'auto';
1131
1224
  let tmpSpaceBelow = tmpViewportHeight - tmpRect.bottom - tmpGap - tmpMargin;
1132
1225
  let tmpSpaceAbove = tmpRect.top - tmpGap - tmpMargin;
1133
- if (tmpSpaceBelow >= 220 || tmpSpaceBelow >= tmpSpaceAbove)
1226
+ let tmpBelow = (tmpSpaceBelow >= 220) || (tmpSpaceBelow >= tmpSpaceAbove);
1227
+ // Document coords (viewport rect + scroll): absolute against <body>, so the panel rides page
1228
+ // scroll with no listener. Right-aligned to the trigger, clamped into the viewport.
1229
+ pPopover.style.right = 'auto';
1230
+ pPopover.style.bottom = 'auto';
1231
+ pPopover.style.left = `${Math.round(Math.max(tmpMargin, Math.min(tmpRect.right - tmpWidth, tmpViewportWidth - tmpWidth - tmpMargin)) + window.scrollX)}px`;
1232
+ if (tmpBelow)
1134
1233
  {
1135
- pPopover.style.top = `${Math.round(tmpRect.bottom + tmpGap)}px`;
1136
- pPopover.style.bottom = 'auto';
1234
+ pPopover.style.top = `${Math.round(tmpRect.bottom + tmpGap + window.scrollY)}px`;
1235
+ pPopover.style.transform = '';
1137
1236
  if (tmpPanel) { tmpPanel.style.maxHeight = `${Math.max(160, Math.min(tmpSpaceBelow, 420))}px`; }
1138
1237
  }
1139
1238
  else
1140
1239
  {
1141
- pPopover.style.top = 'auto';
1142
- pPopover.style.bottom = `${Math.round(tmpViewportHeight - tmpRect.top + tmpGap)}px`;
1240
+ pPopover.style.top = `${Math.round(tmpRect.top - tmpGap + window.scrollY)}px`;
1241
+ pPopover.style.transform = 'translateY(-100%)';
1143
1242
  if (tmpPanel) { tmpPanel.style.maxHeight = `${Math.max(160, Math.min(tmpSpaceAbove, 420))}px`; }
1144
1243
  }
1145
1244
  }
1146
1245
 
1246
+ /**
1247
+ * Re-home a portaled popover into its bar and drop everything the portal wrote. Doubles as the
1248
+ * orphan sweeper: if a re-render already baked a fresh in-bar popover, the portaled one is stale
1249
+ * and gets discarded rather than duplicated.
1250
+ *
1251
+ * @param {Object} pView
1252
+ * @param {Object} pGroup
1253
+ * @param {HTMLElement} pPopover
1254
+ */
1255
+ _restoreTabularColumnChooserPopover(pView, pGroup, pPopover)
1256
+ {
1257
+ if (!pPopover || (typeof document === 'undefined'))
1258
+ {
1259
+ return;
1260
+ }
1261
+ // Discard any OTHER portaled copy of this popover a prior re-render may have left on <body>.
1262
+ this._sweepPortaledColchooserOrphans(pPopover.getAttribute('id'), pPopover);
1263
+ if (pPopover.parentElement === document.body)
1264
+ {
1265
+ let tmpTrigger = document.getElementById(this._getTabularColumnChooserElementId(pView, pGroup, 'TRIGGER'));
1266
+ let tmpBar = tmpTrigger ? tmpTrigger.parentElement : null;
1267
+ let tmpFreshInBar = tmpBar ? tmpBar.querySelector('.pict-tabular-colchooser-pop') : null;
1268
+ if (tmpBar && (!tmpFreshInBar || tmpFreshInBar === pPopover))
1269
+ {
1270
+ // Re-home the popover next to its trigger, inside the bar.
1271
+ tmpBar.appendChild(pPopover);
1272
+ }
1273
+ else
1274
+ {
1275
+ // A re-render already baked a fresh in-bar popover; this portaled one is a leftover.
1276
+ pPopover.remove();
1277
+ return;
1278
+ }
1279
+ }
1280
+ pPopover.classList.remove('pict-tabular-colchooser-pop-portal');
1281
+ // Clear the inline offsets/transform, or they'd override the stylesheet and strand the panel.
1282
+ pPopover.style.top = '';
1283
+ pPopover.style.left = '';
1284
+ pPopover.style.right = '';
1285
+ pPopover.style.bottom = '';
1286
+ pPopover.style.transform = '';
1287
+ // Drop the copied tokens so back in the bar it inherits them live again.
1288
+ _COLCHOOSER_THEME_TOKENS.forEach((pToken) => pPopover.style.removeProperty(pToken));
1289
+ let tmpPanel = pPopover.querySelector('.pict-tabular-colchooser-panel');
1290
+ if (tmpPanel) { tmpPanel.style.maxHeight = ''; }
1291
+ }
1292
+
1293
+ /**
1294
+ * Remove any portaled copies of a chooser popover (matched by element id) that are direct children
1295
+ * of <body>, except the one to keep. Iterates body children so no attribute-selector escaping of
1296
+ * the id is needed, and is scoped by id so it never touches another table's portaled popover.
1297
+ *
1298
+ * @param {string} pElementId
1299
+ * @param {HTMLElement} pKeep
1300
+ */
1301
+ _sweepPortaledColchooserOrphans(pElementId, pKeep)
1302
+ {
1303
+ if (!pElementId || (typeof document === 'undefined') || !document.body)
1304
+ {
1305
+ return;
1306
+ }
1307
+ Array.prototype.slice.call(document.body.children).forEach((pNode) =>
1308
+ {
1309
+ if (pNode !== pKeep && pNode.id === pElementId
1310
+ && pNode.classList && pNode.classList.contains('pict-tabular-colchooser-pop'))
1311
+ {
1312
+ pNode.remove();
1313
+ }
1314
+ });
1315
+ }
1316
+
1317
+ /**
1318
+ * Portal when ANY clipping ancestor of the trigger would actually cut the popover -- an overflow
1319
+ * ancestor alone isn't enough, but a roomy inner clipper doesn't excuse a tighter one further out.
1320
+ *
1321
+ * @param {HTMLElement} pTrigger
1322
+ * @param {HTMLElement} pPopover
1323
+ * @returns {boolean}
1324
+ */
1325
+ _tabularColumnChooserShouldPortal(pTrigger, pPopover)
1326
+ {
1327
+ let tmpWidth = (pPopover && pPopover.offsetWidth) ? pPopover.offsetWidth : 240;
1328
+ let tmpClipper = this._tabularColumnChooserClippingAncestor(pTrigger);
1329
+ while (tmpClipper)
1330
+ {
1331
+ if (this._tabularColumnChooserClipBites(pTrigger, tmpClipper, tmpWidth))
1332
+ {
1333
+ return true;
1334
+ }
1335
+ tmpClipper = this._tabularColumnChooserClippingAncestor(tmpClipper);
1336
+ }
1337
+ return false;
1338
+ }
1339
+
1340
+ /**
1341
+ * Nearest ancestor (up to, not including, <body>) with an overflow clip on any axis, or null.
1342
+ * Stops at <body> because the document's own scroll is what the absolute anchoring rides on.
1343
+ *
1344
+ * @param {HTMLElement} pElement
1345
+ * @returns {HTMLElement|null}
1346
+ */
1347
+ _tabularColumnChooserClippingAncestor(pElement)
1348
+ {
1349
+ if ((typeof window === 'undefined') || (typeof window.getComputedStyle !== 'function'))
1350
+ {
1351
+ return null;
1352
+ }
1353
+ let tmpNode = pElement ? pElement.parentElement : null;
1354
+ while (tmpNode && tmpNode.nodeType === 1 && tmpNode !== document.body)
1355
+ {
1356
+ let tmpStyle = window.getComputedStyle(tmpNode);
1357
+ if (tmpStyle && [ tmpStyle.overflow, tmpStyle.overflowX, tmpStyle.overflowY ].some((pValue) => pValue && pValue !== 'visible'))
1358
+ {
1359
+ return tmpNode;
1360
+ }
1361
+ tmpNode = tmpNode.parentElement;
1362
+ }
1363
+ return null;
1364
+ }
1365
+
1366
+ /**
1367
+ * Would the clipper cut the popover if it opened in place? The CSS path opens downward, right-
1368
+ * aligned to the trigger; project that worst-case box (trigger bottom + gap, up to the max height)
1369
+ * and test whether any edge falls outside the clipper. 1px slack avoids a needless portal on a
1370
+ * flush edge.
1371
+ *
1372
+ * @param {HTMLElement} pTrigger
1373
+ * @param {HTMLElement} pClipper
1374
+ * @param {number} pWidth
1375
+ * @returns {boolean}
1376
+ */
1377
+ _tabularColumnChooserClipBites(pTrigger, pClipper, pWidth)
1378
+ {
1379
+ if (!pTrigger || !pClipper || (typeof pTrigger.getBoundingClientRect !== 'function')
1380
+ || (typeof pClipper.getBoundingClientRect !== 'function'))
1381
+ {
1382
+ return true;
1383
+ }
1384
+ let tmpTriggerRect = pTrigger.getBoundingClientRect();
1385
+ let tmpClipRect = pClipper.getBoundingClientRect();
1386
+ let tmpGap = 6;
1387
+ let tmpIdeal = 420;
1388
+ let tmpSlack = 1;
1389
+ let tmpWidthResolved = Math.max(220, pWidth || 220);
1390
+ let tmpPanelTop = tmpTriggerRect.bottom + tmpGap;
1391
+ let tmpPanelBottom = tmpPanelTop + tmpIdeal;
1392
+ let tmpPanelRight = tmpTriggerRect.right;
1393
+ let tmpPanelLeft = tmpTriggerRect.right - tmpWidthResolved;
1394
+ return (tmpPanelBottom > tmpClipRect.bottom + tmpSlack)
1395
+ || (tmpPanelTop < tmpClipRect.top - tmpSlack)
1396
+ || (tmpPanelLeft < tmpClipRect.left - tmpSlack)
1397
+ || (tmpPanelRight > tmpClipRect.right + tmpSlack);
1398
+ }
1399
+
1147
1400
  /**
1148
1401
  * Rebuild + re-render a tabular view and re-marshal the form data into it.
1149
1402
  * Same tail as sortTabularColumn: the rebuild re-bakes the table template
@@ -1764,4 +1764,55 @@ suite('PictSectionForm Tabular Features', () =>
1764
1764
  }, fDone);
1765
1765
  });
1766
1766
  });
1767
+
1768
+ suite('Row reorder performance (move skips the full source re-render)', () =>
1769
+ {
1770
+ test('moveDynamicTableRowUp reorders in place without full-rendering the source view, and repaints inputs via the hook', (fDone) =>
1771
+ {
1772
+ let App = makeApplication({
1773
+ Hash: 'Students',
1774
+ Layout: 'Tabular',
1775
+ RecordSetAddress: 'Students',
1776
+ RecordManifest: 'StudentEditor'
1777
+ });
1778
+ bootstrap(App, (_Pict) =>
1779
+ {
1780
+ let tmpView = _Pict.views['PictSectionForm-Class'];
1781
+ let tmpDTD = _Pict.providers.DynamicTabularData;
1782
+
1783
+ // A move must NOT go through the expensive full source-view render -- that path tears
1784
+ // down and re-initializes every cell widget (each entity-selector select2 re-init is
1785
+ // what made BatchSheetCombined take seconds per move)...
1786
+ let tmpFullRenderCalls = 0;
1787
+ let tmpOriginalRender = tmpDTD._renderViewsPreservingRecordSet;
1788
+ tmpDTD._renderViewsPreservingRecordSet = function (...pArgs)
1789
+ {
1790
+ tmpFullRenderCalls++;
1791
+ return tmpOriginalRender.apply(this, pArgs);
1792
+ };
1793
+ // ...and it MUST ask the input providers to repaint their display for the new order.
1794
+ let tmpRepaintHookCalls = 0;
1795
+ let tmpOriginalRun = tmpView.runInputProviderFunctions;
1796
+ tmpView.runInputProviderFunctions = function (pFunctionName)
1797
+ {
1798
+ if (pFunctionName === 'onTabularRowReorderRepaint') { tmpRepaintHookCalls++; }
1799
+ return tmpOriginalRun.apply(this, arguments);
1800
+ };
1801
+
1802
+ let tmpBefore = _Pict.AppData.Students.map((pRow) => pRow.StudentName);
1803
+ tmpDTD.moveDynamicTableRowUp(tmpView, 0, 1); // 'Bob' (index 1) moves up to index 0
1804
+
1805
+ let tmpAfter = _Pict.AppData.Students.map((pRow) => pRow.StudentName);
1806
+
1807
+ tmpDTD._renderViewsPreservingRecordSet = tmpOriginalRender;
1808
+ tmpView.runInputProviderFunctions = tmpOriginalRun;
1809
+
1810
+ Expect(tmpBefore[0]).to.equal('Alice', 'sanity: row 0 started as Alice');
1811
+ Expect(tmpAfter[0]).to.equal('Bob', 'moved row is now first in the model');
1812
+ Expect(tmpAfter[1]).to.equal('Alice', 'displaced row shifted down');
1813
+ Expect(tmpFullRenderCalls).to.equal(0, 'a reorder must not full-render the source view (the slow select2 teardown)');
1814
+ Expect(tmpRepaintHookCalls).to.equal(1, 'a reorder repaints inputs in place via the onTabularRowReorderRepaint hook');
1815
+ }, fDone);
1816
+ });
1817
+ });
1767
1818
  });