pict-section-form 1.3.11 → 1.3.13

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pict-section-form",
3
- "version": "1.3.11",
3
+ "version": "1.3.13",
4
4
  "description": "Pict dynamic forms",
5
5
  "main": "source/Pict-Section-Form.js",
6
6
  "directories": {
@@ -204,9 +204,20 @@ class CustomInputHandler extends libPictSectionInputExtension
204
204
  const tmpState = this.currentChartLegendState[pRawHTMLID] = this.currentChartLegendState[pRawHTMLID] || { Hidden: false, Collapsed: {} };
205
205
  const tmpPosition = pLegendConfiguration.Position || 'right';
206
206
 
207
- // Wrap the canvas once: shell > (canvas area | legend).
208
- let tmpShell = tmpContainer.parentElement && tmpContainer.parentElement.classList.contains('pict-chart-shell')
209
- ? tmpContainer.parentElement : null;
207
+ // Wrap the canvas ONCE: shell > (canvas area | legend).
208
+ //
209
+ // After the first wrap the container's parent is the canvas AREA, not the
210
+ // shell, so testing the immediate parent for 'pict-chart-shell' never matches
211
+ // again and every re-render nests another shell inside the last -- 14 shells
212
+ // and 14 legends for 3 charts after a handful of toggles. Detect the wrapped
213
+ // shape by its own marker and climb to the shell.
214
+ let tmpShell = null;
215
+ const tmpExistingArea = tmpContainer.parentElement;
216
+ if (tmpExistingArea && tmpExistingArea.classList.contains('pict-chart-canvas-area')
217
+ && tmpExistingArea.parentElement && tmpExistingArea.parentElement.classList.contains('pict-chart-shell'))
218
+ {
219
+ tmpShell = tmpExistingArea.parentElement;
220
+ }
210
221
  if (!tmpShell)
211
222
  {
212
223
  tmpShell = document.createElement('div');
@@ -28,6 +28,16 @@ class CustomInputHandler extends libPictSectionInputExtension
28
28
 
29
29
  roundValue(pInput, pValue)
30
30
  {
31
+ // A value that is not a number must render BLANK, never a formatted stand-in.
32
+ // Formatting one produced "NaN" in a tabular cell (from a null return) and
33
+ // "0.00" from undefined -- both of which read as real measurements. A
34
+ // running average is legitimately empty until its window fills, so this is
35
+ // the common case, not an edge case.
36
+ if ((pValue === null) || (typeof (pValue) === 'undefined') || (String(pValue).trim() === ''))
37
+ {
38
+ return '';
39
+ }
40
+
31
41
  let tmpValue = pValue;
32
42
 
33
43
  if ('DecimalPrecision' in pInput.PictForm)
@@ -273,6 +273,53 @@ suite('Pict-Provider-Input-Chart', () =>
273
273
  });
274
274
  });
275
275
 
276
+ suite('legend re-entrancy', () =>
277
+ {
278
+ // renderChartLegend runs again on every data refresh and on every toggle.
279
+ // It must reuse the wrapper it already built. It did not: after the first
280
+ // wrap the container's parent is the canvas AREA rather than the shell, so
281
+ // the guard never matched and each call nested another shell — 14 shells and
282
+ // 14 legends for 3 charts in the browser.
283
+ const buildChart = () =>
284
+ {
285
+ const tmpCanvas = document.createElement('canvas');
286
+ const tmpContainer = document.createElement('div');
287
+ tmpContainer.appendChild(tmpCanvas);
288
+ document.body.appendChild(tmpContainer);
289
+ return {
290
+ canvas: tmpCanvas,
291
+ data: { datasets: [ { label: 'Reading' }, { label: 'Limit', pictLegendGroup: 'Limits' } ] },
292
+ getDatasetMeta: () => ({ hidden: false }),
293
+ update: () => {},
294
+ };
295
+ };
296
+
297
+ test('re-rendering reuses the shell instead of nesting another', () =>
298
+ {
299
+ const tmpProvider = new libChartInput(new libPict(), {});
300
+ const tmpChart = buildChart();
301
+ for (let i = 0; i < 5; i++)
302
+ {
303
+ tmpProvider.renderChartLegend('ReentrantChart', tmpChart, { Position: 'right' });
304
+ }
305
+ Expect(document.querySelectorAll('.pict-chart-shell').length, 'shells').to.equal(1);
306
+ Expect(document.querySelectorAll('.pict-chart-legend').length, 'legend panels').to.equal(1);
307
+ Expect(document.querySelectorAll('.pict-chart-shell .pict-chart-shell').length, 'nested shells').to.equal(0);
308
+ // Two datasets in two groups → two items, not ten.
309
+ Expect(document.querySelectorAll('.pict-chart-legend-item').length, 'items').to.equal(2);
310
+ Expect(tmpChart.canvas.closest('.pict-chart-canvas-area'), 'canvas still inside the area').to.be.ok;
311
+ });
312
+
313
+ test('the canvas is never detached from the document by a re-render', () =>
314
+ {
315
+ const tmpProvider = new libChartInput(new libPict(), {});
316
+ const tmpChart = buildChart();
317
+ tmpProvider.renderChartLegend('AttachedChart', tmpChart, { Position: 'right' });
318
+ tmpProvider.renderChartLegend('AttachedChart', tmpChart, { Position: 'right' });
319
+ Expect(document.body.contains(tmpChart.canvas)).to.equal(true);
320
+ });
321
+ });
322
+
276
323
  suite('template', () =>
277
324
  {
278
325
  test('the chart canvas container carries the ChartHeight min-height', () =>
@@ -0,0 +1,93 @@
1
+ /*
2
+ Unit tests for source/providers/inputs/Pict-Provider-Input-PreciseNumber.js
3
+
4
+ The case that mattered: a blank value in a formatted numeric column. A running
5
+ average is legitimately empty until its trailing window fills, so a dashboard
6
+ table has blanks in the same column as real numbers. Formatting those produced
7
+ "NaN" in the rendered cell (roundValue returned null) and "0.00" from
8
+ undefined — both of which read as measurements that were never taken.
9
+ */
10
+
11
+ const libBrowserEnv = require('browser-env');
12
+ libBrowserEnv();
13
+
14
+ const Chai = require('chai');
15
+ const Expect = Chai.expect;
16
+
17
+ const libPict = require('pict');
18
+ const libPreciseNumber = require('../source/providers/inputs/Pict-Provider-Input-PreciseNumber.js');
19
+
20
+ /**
21
+ * @param {object} [pPictForm] - The input's PictForm block.
22
+ * @return {{ provider: object, input: object }}
23
+ */
24
+ function build(pPictForm)
25
+ {
26
+ const tmpPict = new libPict();
27
+ const tmpProvider = new libPreciseNumber(tmpPict, {});
28
+ return { provider: tmpProvider, input: { Hash: 'Measure', PictForm: Object.assign({ DecimalPrecision: 2 }, pPictForm || {}) } };
29
+ }
30
+
31
+ suite('Pict-Provider-Input-PreciseNumber', () =>
32
+ {
33
+ suite('blank values', () =>
34
+ {
35
+ test('an empty string renders blank, not NaN', () =>
36
+ {
37
+ const { provider, input } = build();
38
+ Expect(provider.roundValue(input, '')).to.equal('');
39
+ });
40
+
41
+ test('null renders blank', () =>
42
+ {
43
+ const { provider, input } = build();
44
+ Expect(provider.roundValue(input, null)).to.equal('');
45
+ });
46
+
47
+ test('undefined renders blank, not 0.00', () =>
48
+ {
49
+ const { provider, input } = build();
50
+ Expect(provider.roundValue(input, undefined)).to.equal('');
51
+ });
52
+
53
+ test('whitespace renders blank', () =>
54
+ {
55
+ const { provider, input } = build();
56
+ Expect(provider.roundValue(input, ' ')).to.equal('');
57
+ });
58
+
59
+ test('a blank is blank even with a prefix/postfix configured — no bare "%" in an empty cell', () =>
60
+ {
61
+ const { provider, input } = build({ DigitsPostfix: '%', DigitsPrefix: '$' });
62
+ Expect(provider.roundValue(input, '')).to.equal('');
63
+ });
64
+ });
65
+
66
+ suite('real values still format', () =>
67
+ {
68
+ test('zero-fills to the configured precision', () =>
69
+ {
70
+ const { provider, input } = build({ DecimalPrecision: 2 });
71
+ Expect(provider.roundValue(input, '5.5')).to.equal('5.50');
72
+ });
73
+
74
+ test('a value at full solver precision is rounded down to the column width', () =>
75
+ {
76
+ const { provider, input } = build({ DecimalPrecision: 5 });
77
+ Expect(provider.roundValue(input, '2.47770392749244712991')).to.equal('2.47770');
78
+ });
79
+
80
+ test('a real zero still renders as a number', () =>
81
+ {
82
+ const { provider, input } = build({ DecimalPrecision: 2 });
83
+ Expect(provider.roundValue(input, 0)).to.equal('0.00');
84
+ Expect(provider.roundValue(input, '0')).to.equal('0.00');
85
+ });
86
+
87
+ test('postfix is still applied to a real value', () =>
88
+ {
89
+ const { provider, input } = build({ DecimalPrecision: 1, DigitsPostfix: '%' });
90
+ Expect(provider.roundValue(input, '2.91')).to.equal('2.9%');
91
+ });
92
+ });
93
+ });