@inseefr/lunatic 2.7.5 → 2.7.7

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/README.fr.md +1 -1
  2. package/README.md +6 -1
  3. package/lib/components/loop/block-for-loop.js +2 -0
  4. package/lib/components/loop/constant.js +10 -0
  5. package/lib/components/loop/roster-for-loop/roster-for-loop.js +42 -20
  6. package/lib/components/lunatic-components.js +45 -7
  7. package/lib/components/table/lunatic-table.js +5 -1
  8. package/lib/src/components/datepicker/lunatic-datepicker.d.ts +1 -1
  9. package/lib/src/components/loop/constant.d.ts +4 -0
  10. package/lib/src/components/loop/roster-for-loop/roster-for-loop.d.ts +1 -0
  11. package/lib/src/components/lunatic-components.d.ts +12 -5
  12. package/lib/src/components/type.d.ts +4 -1
  13. package/lib/src/use-lunatic/commons/compile-controls.d.ts +4 -3
  14. package/lib/src/use-lunatic/commons/fill-components/fill-component-required.d.ts +75 -70
  15. package/lib/src/use-lunatic/commons/fill-components/fill-from-state.d.ts +55 -50
  16. package/lib/src/use-lunatic/commons/fill-components/fill-iterations.d.ts +31 -26
  17. package/lib/src/use-lunatic/commons/fill-components/fill-specific-expression.d.ts +680 -73
  18. package/lib/src/use-lunatic/commons/use-components-from-state.d.ts +1 -1
  19. package/lib/src/use-lunatic/commons/variables/lunatic-variables-store.d.ts +4 -1
  20. package/lib/src/use-lunatic/reducer/commons/is-loop-component.d.ts +2 -2
  21. package/lib/src/use-lunatic/reducer/controls/check-base-control.d.ts +2 -0
  22. package/lib/src/use-lunatic/reducer/{resolve-component-controls/resolve-roundabout-control.d.ts → controls/check-roundabout-control.d.ts} +2 -2
  23. package/lib/src/use-lunatic/type-source.d.ts +16 -10
  24. package/lib/src/use-lunatic/use-lunatic.d.ts +11 -5
  25. package/lib/stories/behaviour/controls/controls.stories.js +14 -7
  26. package/lib/stories/behaviour/controls/loop.json +130 -0
  27. package/lib/stories/behaviour/controls/{V2_ControlesNonNum_horsBoucle_PasPageFin.json → simple.json} +2 -2
  28. package/lib/stories/date-picker/source.json +22 -67
  29. package/lib/stories/declaration/source.json +14 -28
  30. package/lib/stories/dropdown/source.json +11 -28
  31. package/lib/stories/filter-description/source.json +3 -2
  32. package/lib/stories/input/input.stories.js +1 -8
  33. package/lib/stories/input/source.json +1 -63
  34. package/lib/stories/input-number/input-number.stories.js +2 -22
  35. package/lib/stories/input-number/source.json +4 -6
  36. package/lib/stories/loop/{block-for-loop.stories.js → loop.stories.js} +10 -8
  37. package/lib/stories/loop/roster-for-loop.stories.js +8 -2
  38. package/lib/stories/loop/source-bloc.json +10 -39
  39. package/lib/stories/loop/source-paginated.json +65 -78
  40. package/lib/stories/overview/overview.stories.js +1 -1
  41. package/lib/stories/pairwise/pairwise-links.stories.js +4 -4
  42. package/lib/stories/pairwise/{links.json → source.json} +2 -6
  43. package/lib/stories/{loop/not-paginated-loop.stories.js → question-context/question-context.stories.js} +5 -9
  44. package/lib/stories/question-context/source.json +13 -0
  45. package/lib/stories/question-explication/source.json +1 -1
  46. package/lib/stories/{loop/paginated-loop.stories.js → question-information/question-information.stories.js} +4 -4
  47. package/lib/stories/question-information/source.json +14 -0
  48. package/lib/stories/roundabout/source.json +21 -31
  49. package/lib/stories/sequence/sequence.stories.js +7 -1
  50. package/lib/stories/sequence/source.json +17 -0
  51. package/lib/stories/suggester/simple.json +1 -1
  52. package/lib/stories/table/source-colspan.json +314 -0
  53. package/lib/stories/table/table.stories.js +7 -1
  54. package/lib/stories/textarea/source.json +6 -22
  55. package/lib/use-lunatic/commons/compile-controls.js +135 -34
  56. package/lib/use-lunatic/commons/fill-components/fill-component-value.js +10 -0
  57. package/lib/use-lunatic/commons/fill-components/fill-specific-expression.js +4 -1
  58. package/lib/use-lunatic/commons/variables/lunatic-variables-store.js +39 -15
  59. package/lib/use-lunatic/commons/variables/lunatic-variables-store.spec.js +16 -6
  60. package/lib/use-lunatic/hooks/use-page-has-response.js +17 -0
  61. package/lib/use-lunatic/reducer/{resolve-component-controls/resolve-simple-control.js → controls/check-base-control.js} +5 -7
  62. package/lib/use-lunatic/reducer/{resolve-component-controls/resolve-roundabout-control.js → controls/check-roundabout-control.js} +2 -3
  63. package/lib/use-lunatic/reducer/overview/overview-on-init.js +6 -4
  64. package/lib/use-lunatic/type-source.js +1 -0
  65. package/package.json +1 -1
  66. package/lib/src/use-lunatic/commons/get-errors-without-empty-value.d.ts +0 -6
  67. package/lib/src/use-lunatic/reducer/resolve-component-controls/index.d.ts +0 -1
  68. package/lib/src/use-lunatic/reducer/resolve-component-controls/resolve-component-control.d.ts +0 -6
  69. package/lib/src/use-lunatic/reducer/resolve-component-controls/resolve-simple-control.d.ts +0 -3
  70. package/lib/stories/Introduction.stories.mdx +0 -112
  71. package/lib/stories/behaviour/controls/V2_Controles_BouclesLiees_PasPageFin.json +0 -533
  72. package/lib/stories/input/source-with-question.json +0 -80
  73. package/lib/stories/input-number/source-euros.json +0 -37
  74. package/lib/stories/input-number/source-thansand.json +0 -34
  75. package/lib/stories/loop/source-not-paginated.json +0 -138
  76. package/lib/use-lunatic/commons/get-errors-without-empty-value.js +0 -24
  77. package/lib/use-lunatic/reducer/resolve-component-controls/index.js +0 -12
  78. package/lib/use-lunatic/reducer/resolve-component-controls/resolve-component-control.js +0 -38
  79. /package/lib/stories/behaviour/controls/{V2_ControlesNum_horsBoucle_PasPageFin.json → simple-numeric.json} +0 -0
  80. /package/lib/stories/pairwise/{links-componentset.json → source-componentset.json} +0 -0
@@ -1,71 +1,172 @@
1
1
  "use strict";
2
2
 
3
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
3
4
  Object.defineProperty(exports, "__esModule", {
4
5
  value: true
5
6
  });
6
7
  exports.compileControls = compileControls;
7
8
  var _commons = require("../reducer/commons");
8
- var _resolveComponentControls = require("../reducer/resolve-component-controls");
9
9
  var _replaceComponentSequence = require("../replace-component-sequence");
10
10
  var _typeSource = require("../type-source");
11
11
  var _fillComponentExpressions = _interopRequireDefault(require("./fill-components/fill-component-expressions"));
12
12
  var _getComponentsFromState = _interopRequireDefault(require("./get-components-from-state"));
13
- var _getErrorsWithoutEmptyValue = _interopRequireDefault(require("./get-errors-without-empty-value"));
13
+ var _checkRoundaboutControl = require("../reducer/controls/check-roundabout-control");
14
+ var _checkBaseControl = require("../reducer/controls/check-base-control");
14
15
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
15
- function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
16
16
  function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
17
17
  function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
18
18
  function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
19
19
  function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
20
20
  function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
21
+ function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
22
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
23
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
21
24
  /**
22
- * Check if components of current page have errors
25
+ * Check if components of the current page have errors, and return a map of error (indexed by component ID)
23
26
  */
24
- function validateComponents(state, components) {
25
- var pager = state.pager;
26
- return components.reduce(function (errors, component) {
27
- var controls = component.controls,
28
- id = component.id;
29
- if (Array.isArray(controls)) {
30
- var componentErrors = (0, _resolveComponentControls.resolveComponentControls)(state, controls);
31
- var shallowIteration = pager.shallowIteration;
32
- var idC = shallowIteration !== undefined ? "".concat(id, "-").concat(shallowIteration) : id;
33
- return (0, _getErrorsWithoutEmptyValue["default"])(_objectSpread(_objectSpread({}, errors), {}, _defineProperty({}, idC, componentErrors)));
27
+ function checkComponents(state, components) {
28
+ var errors = {};
29
+ var _iterator = _createForOfIteratorHelper(components),
30
+ _step;
31
+ try {
32
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
33
+ var component = _step.value;
34
+ var controls = component.controls,
35
+ id = component.id;
36
+ // The component has global level controls
37
+ if (Array.isArray(controls)) {
38
+ var componentErrors = checkControls(controls.filter(function (c) {
39
+ return c.type !== _typeSource.ControlTypeEnum.row;
40
+ }), state.executeExpression, state.pager);
41
+ if (componentErrors.length > 0) {
42
+ errors[id] = componentErrors;
43
+ }
44
+ }
45
+
46
+ // For loop, inspect children
47
+ if ((0, _commons.isLoopComponent)(component)) {
48
+ var _component$controls;
49
+ var rowControls = (_component$controls = component.controls) === null || _component$controls === void 0 ? void 0 : _component$controls.filter(function (c) {
50
+ return c.type === _typeSource.ControlTypeEnum.row;
51
+ });
52
+ if (rowControls !== null && rowControls !== void 0 && rowControls.length) {
53
+ errors = checkComponentInLoop(state, _objectSpread(_objectSpread({}, component), {}, {
54
+ controls: rowControls
55
+ }), errors);
56
+ }
57
+ var _iterator2 = _createForOfIteratorHelper(component.components),
58
+ _step2;
59
+ try {
60
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
61
+ var child = _step2.value;
62
+ errors = checkComponentInLoop(state, child, errors);
63
+ }
64
+ } catch (err) {
65
+ _iterator2.e(err);
66
+ } finally {
67
+ _iterator2.f();
68
+ }
69
+ }
70
+ }
71
+ } catch (err) {
72
+ _iterator.e(err);
73
+ } finally {
74
+ _iterator.f();
75
+ }
76
+ return errors;
77
+ }
78
+ function checkControls(controls, executeExpression, pager) {
79
+ return controls.map(function (control) {
80
+ switch (control.type) {
81
+ case _typeSource.ControlTypeEnum.roundabout:
82
+ return (0, _checkRoundaboutControl.checkRoundaboutControl)(control, executeExpression);
83
+ default:
84
+ return (0, _checkBaseControl.checkBaseControl)(control, executeExpression, pager);
34
85
  }
86
+ }).filter(function (error) {
87
+ return error !== undefined;
88
+ });
89
+ }
35
90
 
36
- //Thanks to init which split basic Loops, we only go into unPaginatedLoops
37
- if ((0, _commons.isLoopComponent)(component)) {
38
- var _components = component.components;
39
- var recurs = validateComponents(state, _components);
40
- return (0, _getErrorsWithoutEmptyValue["default"])(_objectSpread(_objectSpread({}, errors), recurs));
91
+ /**
92
+ * Figure out the number of iterations of a component
93
+ */
94
+ function computeIterations(component, executeExpression) {
95
+ if ('iterations' in component) {
96
+ return executeExpression(component.iterations.value);
97
+ }
98
+ if ('response' in component) {
99
+ var value = executeExpression(component.response.name);
100
+ if (Array.isArray(value)) {
101
+ return value.length;
41
102
  }
42
- // Keep previous errors to allow multiple controls in same page (multiple question/component in the same page)
103
+ }
104
+ // Look at the first component
105
+ if ('components' in component) {
106
+ return computeIterations(component.components[0], executeExpression);
107
+ }
108
+ return 0;
109
+ }
110
+
111
+ /**
112
+ * Check controls on a component for each iteration
113
+ * Errors are returned using a map of id suffixed with the iteration index (ex: {prenom-1: [], prenom-3: []})
114
+ */
115
+ function checkComponentInLoop(state, component, errors) {
116
+ // The component has no controls, skip it
117
+ if (!Array.isArray(component.controls)) {
43
118
  return errors;
44
- }, {});
119
+ }
120
+
121
+ // Execute control for each iteration
122
+ var iterations = computeIterations(component, state.executeExpression);
123
+ for (var i = 0; i < iterations; i++) {
124
+ // Create a pager representing the iteration we want to check
125
+ var iterationPager = _objectSpread(_objectSpread({}, state.pager), {}, {
126
+ iteration: i,
127
+ nbIterations: iterations
128
+ });
129
+ // The component is filtered on this iteration, skip it
130
+ if (component.conditionFilter && !state.executeExpression(component.conditionFilter.value, iterationPager)) {
131
+ continue;
132
+ }
133
+ var componentErrors = checkControls(component.controls, state.executeExpression, iterationPager);
134
+ if (componentErrors.length > 0) {
135
+ errors["".concat(component.id, "-").concat(i)] = componentErrors;
136
+ }
137
+ }
138
+ return errors;
45
139
  }
46
- function isCriticalErrors(errors) {
47
- if (errors) {
48
- return Object.values(errors).flat().reduce(function (status, _ref) {
49
- var criticality = _ref.criticality,
50
- typeOfControl = _ref.typeOfControl;
51
- return status || typeOfControl === _typeSource.TypeOfControl.FORMAT || criticality.startsWith(_typeSource.Criticality.ERROR);
52
- }, false);
140
+
141
+ /**
142
+ * Check if there is a critical error (type: "Error" and criticality: "Format")
143
+ */
144
+ function hasCriticalError(errors) {
145
+ if (!errors) {
146
+ return false;
53
147
  }
54
- return false;
148
+ // Look for at least one critical error in the list
149
+ var criticalError = Object.values(errors).flat().find(function (error) {
150
+ return error.criticality.startsWith(_typeSource.Criticality.ERROR) || error.typeOfControl === _typeSource.TypeOfControl.FORMAT;
151
+ });
152
+ return criticalError !== undefined;
55
153
  }
154
+
155
+ /**
156
+ * Check controls for currently visible components and output errors
157
+ */
56
158
  function compileControls(state) {
57
159
  var components = (0, _replaceComponentSequence.replaceComponentSequence)((0, _getComponentsFromState["default"])(state));
58
160
  var componentFiltered = components.map(function (component) {
59
161
  return (0, _fillComponentExpressions["default"])(component, state);
60
- }).filter(function (_ref2) {
61
- var conditionFilter = _ref2.conditionFilter;
162
+ }).filter(function (_ref) {
163
+ var conditionFilter = _ref.conditionFilter;
62
164
  return conditionFilter !== null && conditionFilter !== void 0 ? conditionFilter : true;
63
165
  });
64
- var errors = validateComponents(state, componentFiltered);
166
+ var errors = checkComponents(state, componentFiltered);
65
167
  var currentErrors = Object.keys(errors).length > 0 ? errors : undefined;
66
- var isCritical = isCriticalErrors(currentErrors);
67
168
  return {
68
169
  currentErrors: currentErrors,
69
- isCritical: isCritical
170
+ isCritical: hasCriticalError(currentErrors)
70
171
  };
71
172
  }
@@ -32,5 +32,15 @@ function getValueForComponent(component, state) {
32
32
  if ((0, _component.hasResponse)(component)) {
33
33
  return state.variables.get(component.response.name, iteration);
34
34
  }
35
+ // For loop, value will be a map of child component values
36
+ if ('components' in component) {
37
+ return Object.fromEntries(component.components.map(function (c) {
38
+ return 'response' in c ? c.response.name : null;
39
+ }).filter(function (name) {
40
+ return name !== null;
41
+ }).map(function (name) {
42
+ return [name, state.variables.get(name)];
43
+ }));
44
+ }
35
45
  return null;
36
46
  }
@@ -114,7 +114,10 @@ function fillTable(component, state) {
114
114
  if ((0, _component.hasComponentType)(component)) {
115
115
  return (0, _fillComponents.fillComponent)(component, state);
116
116
  }
117
- return state.executeExpression((0, _vtl.getVTLCompatibleValue)(component.label));
117
+ // We can have a non typed component with extra attributes (colspan, rowspan)
118
+ return _objectSpread(_objectSpread({}, component), {}, {
119
+ label: state.executeExpression((0, _vtl.getVTLCompatibleValue)(component.label))
120
+ });
118
121
  });
119
122
  })
120
123
  });
@@ -84,13 +84,15 @@ var LunaticVariablesStore = exports.LunaticVariablesStore = /*#__PURE__*/functio
84
84
  value: function setCalculated(name, expression) {
85
85
  var _ref = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {},
86
86
  dependencies = _ref.dependencies,
87
- iterationDepth = _ref.iterationDepth;
87
+ iterationDepth = _ref.iterationDepth,
88
+ shapeFrom = _ref.shapeFrom;
88
89
  if (this.dictionary.has(name)) {
89
90
  return this.dictionary.get(name);
90
91
  }
91
92
  var variable = new LunaticVariable({
92
93
  expression: expression,
93
94
  dictionary: this.dictionary,
95
+ shapeFrom: shapeFrom,
94
96
  dependencies: dependencies,
95
97
  iterationDepth: iterationDepth,
96
98
  name: name
@@ -160,7 +162,8 @@ var LunaticVariablesStore = exports.LunaticVariablesStore = /*#__PURE__*/functio
160
162
  case 'CALCULATED':
161
163
  store.setCalculated(variable.name, variable.expression.value, {
162
164
  dependencies: variable.bindingDependencies,
163
- iterationDepth: getIterationDepth(variable.name)
165
+ iterationDepth: getIterationDepth(variable.name),
166
+ shapeFrom: variable.shapeFrom
164
167
  });
165
168
  break;
166
169
  case 'COLLECTED':
@@ -216,6 +219,8 @@ var LunaticVariable = /*#__PURE__*/function () {
216
219
  this.dictionary = void 0;
217
220
  // Specific iteration depth to get value from dependencies (used for yAxis for instance)
218
221
  this.iterationDepth = void 0;
222
+ // For calculated variable, shape is copied from another variable
223
+ this.shapeFrom = void 0;
219
224
  // Keep a record of variable name (optional, used for debug)
220
225
  this.name = void 0;
221
226
  if (args.expression && !args.dictionary) {
@@ -225,15 +230,34 @@ var LunaticVariable = /*#__PURE__*/function () {
225
230
  this.dictionary = args.dictionary;
226
231
  this.dependencies = args.dependencies;
227
232
  this.iterationDepth = args.iterationDepth;
233
+ this.shapeFrom = args.shapeFrom;
228
234
  this.name = (_args$name = args.name) !== null && _args$name !== void 0 ? _args$name : args.expression;
229
235
  }
230
236
  _createClass(LunaticVariable, [{
231
237
  key: "getValue",
232
238
  value: function getValue(iteration) {
239
+ var _this$dictionary,
240
+ _this = this,
241
+ _iteration;
233
242
  // The variable is not calculated
234
243
  if (!this.expression) {
235
244
  return this.getSavedValue(iteration);
236
245
  }
246
+ var shapeFromValue = this.shapeFrom ? (_this$dictionary = this.dictionary) === null || _this$dictionary === void 0 || (_this$dictionary = _this$dictionary.get(this.shapeFrom)) === null || _this$dictionary === void 0 ? void 0 : _this$dictionary.getValue() : null;
247
+ // If we want the root value of a calculated array, loop using the shapeFrom value
248
+ if (!iteration && Array.isArray(shapeFromValue)) {
249
+ return shapeFromValue.map(function (_, k) {
250
+ return _this.getValue([k]);
251
+ });
252
+ }
253
+
254
+ // For calculated variable, ignore iteration if shapeFrom exists and is not an array
255
+ if (
256
+ // We have a calculated variable (not a simple expression)
257
+ this.name !== this.expression && !Array.isArray(shapeFromValue)) {
258
+ iteration = undefined;
259
+ }
260
+
237
261
  // Calculate bindings first to refresh "updatedAt" on calculated dependencies
238
262
  var bindings = this.getDependenciesValues(iteration);
239
263
  if (!this.isOutdated(iteration)) {
@@ -248,7 +272,7 @@ var LunaticVariable = /*#__PURE__*/function () {
248
272
  } catch (e) {
249
273
  throw new Error("Cannot interpret expression \"".concat(this.expression, "\" with bindings ").concat(JSON.stringify(bindings), ", error : ").concat(e.toString()));
250
274
  }
251
- this.calculatedAt.set(iteration === null || iteration === void 0 ? void 0 : iteration.join('.'), performance.now());
275
+ this.calculatedAt.set((_iteration = iteration) === null || _iteration === void 0 ? void 0 : _iteration.join('.'), performance.now());
252
276
  this.calculatedAt.set(undefined, performance.now());
253
277
  return this.getSavedValue(iteration);
254
278
  }
@@ -282,13 +306,13 @@ var LunaticVariable = /*#__PURE__*/function () {
282
306
  }, {
283
307
  key: "setValueForArray",
284
308
  value: function setValueForArray(value) {
285
- var _this = this;
309
+ var _this2 = this;
286
310
  var savedValue = this.getSavedValue();
287
311
  var oldSize = Array.isArray(savedValue) ? savedValue.length : -1;
288
312
  var newSize = value.length;
289
313
  // Update every item of the array and look if we changed one item
290
314
  var oneValueChanged = (0, _array.times)(Math.max(oldSize, newSize), function (k) {
291
- return _this.setValue(value[k], [k]);
315
+ return _this2.setValue(value[k], [k]);
292
316
  }).find(function (v) {
293
317
  return v;
294
318
  }) !== undefined;
@@ -334,21 +358,21 @@ var LunaticVariable = /*#__PURE__*/function () {
334
358
  }, {
335
359
  key: "getDependenciesValues",
336
360
  value: function getDependenciesValues(iteration) {
337
- var _this2 = this;
361
+ var _this3 = this;
338
362
  try {
339
363
  return Object.fromEntries(this.getDependencies().map(function (dep) {
340
- var _this2$dictionary, _this2$dictionary$get;
364
+ var _this3$dictionary, _this3$dictionary$get;
341
365
  if (dep === iterationVariableName && iteration) {
342
- return [dep, iteration[0]];
366
+ return [dep, iteration[0] + 1];
343
367
  }
344
- var dependencyIteration = (0, _number.isNumber)(_this2.iterationDepth) && Array.isArray(iteration) ? [iteration[_this2.iterationDepth]] : iteration;
368
+ var dependencyIteration = (0, _number.isNumber)(_this3.iterationDepth) && Array.isArray(iteration) ? [iteration[_this3.iterationDepth]] : iteration;
345
369
 
346
370
  // The variable is not registered in the variable dictionary
347
371
  // Happens when calculating unquoted VTL expression
348
- if (!_this2.dictionary || !((_this2$dictionary = _this2.dictionary) !== null && _this2$dictionary !== void 0 && _this2$dictionary.has(dep))) {
349
- throw new Error("Unknown variable \"".concat(dep, "\" in expression ").concat(_this2.expression));
372
+ if (!_this3.dictionary || !((_this3$dictionary = _this3.dictionary) !== null && _this3$dictionary !== void 0 && _this3$dictionary.has(dep))) {
373
+ throw new Error("Unknown variable \"".concat(dep, "\" in expression ").concat(_this3.expression));
350
374
  }
351
- return [dep, (_this2$dictionary$get = _this2.dictionary.get(dep)) === null || _this2$dictionary$get === void 0 ? void 0 : _this2$dictionary$get.getValue(dependencyIteration)];
375
+ return [dep, (_this3$dictionary$get = _this3.dictionary.get(dep)) === null || _this3$dictionary$get === void 0 ? void 0 : _this3$dictionary$get.getValue(dependencyIteration)];
352
376
  }));
353
377
  } catch (e) {
354
378
  if (e instanceof RangeError) {
@@ -360,11 +384,11 @@ var LunaticVariable = /*#__PURE__*/function () {
360
384
  }, {
361
385
  key: "isOutdated",
362
386
  value: function isOutdated(iteration) {
363
- var _this3 = this,
387
+ var _this4 = this,
364
388
  _this$calculatedAt$ge;
365
389
  var dependenciesUpdatedAt = Math.max.apply(Math, [0].concat(_toConsumableArray(this.getDependencies().map(function (dep) {
366
- var _this3$dictionary$get, _this3$dictionary;
367
- return (_this3$dictionary$get = (_this3$dictionary = _this3.dictionary) === null || _this3$dictionary === void 0 || (_this3$dictionary = _this3$dictionary.get(dep)) === null || _this3$dictionary === void 0 ? void 0 : _this3$dictionary.updatedAt.get(iteration === null || iteration === void 0 ? void 0 : iteration.join('.'))) !== null && _this3$dictionary$get !== void 0 ? _this3$dictionary$get : 0;
390
+ var _this4$dictionary$get, _this4$dictionary;
391
+ return (_this4$dictionary$get = (_this4$dictionary = _this4.dictionary) === null || _this4$dictionary === void 0 || (_this4$dictionary = _this4$dictionary.get(dep)) === null || _this4$dictionary === void 0 ? void 0 : _this4$dictionary.updatedAt.get(iteration === null || iteration === void 0 ? void 0 : iteration.join('.'))) !== null && _this4$dictionary$get !== void 0 ? _this4$dictionary$get : 0;
368
392
  }))));
369
393
  return dependenciesUpdatedAt > ((_this$calculatedAt$ge = this.calculatedAt.get(iteration === null || iteration === void 0 ? void 0 : iteration.join('.'))) !== null && _this$calculatedAt$ge !== void 0 ? _this$calculatedAt$ge : -1);
370
394
  }
@@ -1,10 +1,10 @@
1
1
  "use strict";
2
2
 
3
3
  var _vitest = require("vitest");
4
- var _lunaticVariablesStore = require("./lunatic-variables-store");
5
- var _resizingBehaviour = require("./behaviours/resizing-behaviour");
6
4
  var _cleaningBehaviour = require("./behaviours/cleaning-behaviour");
7
5
  var _missingBehaviour = require("./behaviours/missing-behaviour");
6
+ var _resizingBehaviour = require("./behaviours/resizing-behaviour");
7
+ var _lunaticVariablesStore = require("./lunatic-variables-store");
8
8
  (0, _vitest.describe)('lunatic-variables-store', function () {
9
9
  var variables;
10
10
  (0, _vitest.beforeEach)(function () {
@@ -152,7 +152,8 @@ var _missingBehaviour = require("./behaviours/missing-behaviour");
152
152
  variables.set('FIRSTNAME', ['John', 'Jane']);
153
153
  variables.set('LASTNAME', ['Doe', 'Dae']);
154
154
  variables.setCalculated('FULLNAME', 'FIRSTNAME || " " || LASTNAME', {
155
- dependencies: ['FIRSTNAME', 'LASTNAME']
155
+ dependencies: ['FIRSTNAME', 'LASTNAME'],
156
+ shapeFrom: 'FIRSTNAME'
156
157
  });
157
158
  (0, _vitest.expect)(variables.get('FULLNAME', [0])).toEqual('John Doe');
158
159
  (0, _vitest.expect)(variables.get('FULLNAME', [1])).toEqual('Jane Dae');
@@ -185,9 +186,18 @@ var _missingBehaviour = require("./behaviours/missing-behaviour");
185
186
  });
186
187
  (0, _vitest.it)('should handle global iteration variable', function () {
187
188
  variables.set('FIRSTNAME', ['John', 'Jane']);
188
- variables.setCalculated('FULLNAME', 'FIRSTNAME || " " || cast(GLOBAL_ITERATION_INDEX, string)');
189
- (0, _vitest.expect)(variables.get('FULLNAME', [0])).toEqual('John 0');
190
- (0, _vitest.expect)(variables.get('FULLNAME', [1])).toEqual('Jane 1');
189
+ variables.setCalculated('FULLNAME', 'FIRSTNAME || " " || cast(GLOBAL_ITERATION_INDEX, string)', {
190
+ shapeFrom: 'FIRSTNAME'
191
+ });
192
+ (0, _vitest.expect)(variables.get('FULLNAME', [0])).toEqual('John 1');
193
+ (0, _vitest.expect)(variables.get('FULLNAME', [1])).toEqual('Jane 2');
194
+ });
195
+ (0, _vitest.it)('should handle shapeFrom correctly', function () {
196
+ variables.set('FIRSTNAME', ['John', 'Jane']);
197
+ variables.setCalculated('FULLNAME', 'FIRSTNAME || " " || cast(GLOBAL_ITERATION_INDEX, string)', {
198
+ shapeFrom: 'FIRSTNAME'
199
+ });
200
+ (0, _vitest.expect)(variables.get('FULLNAME')).toEqual(['John 1', 'Jane 2']);
191
201
  });
192
202
  });
193
203
  (0, _vitest.describe)('resizing', function () {
@@ -5,7 +5,12 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.usePageHasResponse = usePageHasResponse;
7
7
  var _react = require("react");
8
+ var _isObject = require("../../utils/is-object");
8
9
  function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
10
+ function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
11
+ function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
12
+ function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
13
+ function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
9
14
  function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
10
15
  function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
11
16
  function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
@@ -26,6 +31,18 @@ function usePageHasResponse(components, executeExpression) {
26
31
  if (['PairwiseLinks', 'Roundabout'].includes(component.componentType)) {
27
32
  return true;
28
33
  }
34
+ // For Table, we have to extract components from its body and apply isSubComponentsEmpty function
35
+ if (component.componentType === 'Table') {
36
+ // Body is array for array (row), each "cell" could be an Label or Component, so we filter array.
37
+ var childrenComponent = component.body.reduce(function (_, row) {
38
+ var componentsInRow = row.filter(function (cell) {
39
+ return (0, _isObject.isObject)(cell) && 'componentType' in cell;
40
+ });
41
+ return [].concat(_toConsumableArray(_), _toConsumableArray(componentsInRow));
42
+ }, []);
43
+ return !isSubComponentsEmpty(childrenComponent, executeExpression);
44
+ }
45
+
29
46
  // We found a value in one of the root component
30
47
  if ('value' in component && !isEmpty(component.value)) {
31
48
  return true;
@@ -3,13 +3,11 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.resolveSimpleControl = resolveSimpleControl;
7
- function resolveSimpleControl(state, control) {
8
- var _state$pager, _control$control$valu, _control$control;
9
- var executeExpression = state.executeExpression;
10
- var _ref = (_state$pager = state.pager) !== null && _state$pager !== void 0 ? _state$pager : {},
11
- iteration = _ref.iteration,
12
- linksIterations = _ref.linksIterations;
6
+ exports.checkBaseControl = checkBaseControl;
7
+ function checkBaseControl(control, executeExpression, pager) {
8
+ var _control$control$valu, _control$control;
9
+ var iteration = pager.iteration,
10
+ linksIterations = pager.linksIterations;
13
11
  var criticality = control.criticality,
14
12
  errorMessage = control.errorMessage,
15
13
  id = control.id,
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.resolveRoundaboutControl = resolveRoundaboutControl;
6
+ exports.checkRoundaboutControl = checkRoundaboutControl;
7
7
  /**
8
8
  * Pour le Roundabout, le controle doit être validé pour chaque itération
9
9
  * composants l'unité enquêtée.
@@ -11,9 +11,8 @@ exports.resolveRoundaboutControl = resolveRoundaboutControl;
11
11
  * Le rondpoint ne peut pas être placé dans une boucle car l'itération est effacée.
12
12
  * TODO intégrer cela dans le cadre de boucle de bloucle.
13
13
  */
14
- function resolveRoundaboutControl(state, control) {
14
+ function checkRoundaboutControl(control, executeExpression) {
15
15
  var _control$control$valu, _control$control;
16
- var executeExpression = state.executeExpression;
17
16
  var criticality = control.criticality,
18
17
  errorMessage = control.errorMessage,
19
18
  id = control.id,
@@ -25,10 +25,12 @@ var isSequence = function isSequence(sourceComponent) {
25
25
  return sourceComponent.components.length > 0 && 'hierarchy' in sourceComponent.components[0];
26
26
  };
27
27
  var isSequenceSamePage = function isSequenceSamePage(sourceComponent, page) {
28
- return sourceComponent.components[0].hierarchy.sequence.page === page;
28
+ var _sourceComponent$comp;
29
+ return ((_sourceComponent$comp = sourceComponent.components[0].hierarchy) === null || _sourceComponent$comp === void 0 ? void 0 : _sourceComponent$comp.sequence.page) === page;
29
30
  };
30
31
  var isSubsequenceSamePage = function isSubsequenceSamePage(sourceComponent, page) {
31
- return sourceComponent.components[0].hierarchy.subSequence !== undefined && sourceComponent.components[0].hierarchy.subSequence.page === page;
32
+ var _sourceComponent$comp2, _sourceComponent$comp3;
33
+ return ((_sourceComponent$comp2 = sourceComponent.components[0].hierarchy) === null || _sourceComponent$comp2 === void 0 ? void 0 : _sourceComponent$comp2.subSequence) !== undefined && ((_sourceComponent$comp3 = sourceComponent.components[0].hierarchy) === null || _sourceComponent$comp3 === void 0 ? void 0 : _sourceComponent$comp3.subSequence.page) === page;
32
34
  };
33
35
  var isPageReached = function isPageReached(page, lastReachedPage) {
34
36
  return parseInt(page, 10) <= parseInt(lastReachedPage, 10);
@@ -64,7 +66,7 @@ function getOverview(state) {
64
66
  var component = _step.value;
65
67
  var hierarchy = component.hierarchy,
66
68
  conditionFilter = component.conditionFilter;
67
- if (hierarchy.subSequence) {
69
+ if (hierarchy && hierarchy.subSequence) {
68
70
  var _executeExpression, _pager$lastReachedPag;
69
71
  overviewEntries.subSequences.push(_objectSpread(_objectSpread({}, hierarchy.subSequence), {}, {
70
72
  type: 'subSequence',
@@ -75,7 +77,7 @@ function getOverview(state) {
75
77
  evaluatedLabel: executeExpression(hierarchy.subSequence.label),
76
78
  children: []
77
79
  }));
78
- } else {
80
+ } else if (hierarchy) {
79
81
  var _pager$lastReachedPag2, _executeExpression2;
80
82
  overviewEntries.sequences.push(_objectSpread(_objectSpread({}, hierarchy.sequence), {}, {
81
83
  type: 'sequence',
@@ -20,6 +20,7 @@ var TypeOfControl = exports.TypeOfControl = /*#__PURE__*/function (TypeOfControl
20
20
  }({});
21
21
  var ControlTypeEnum = exports.ControlTypeEnum = /*#__PURE__*/function (ControlTypeEnum) {
22
22
  ControlTypeEnum["roundabout"] = "roundabout";
23
+ ControlTypeEnum["row"] = "ROW";
23
24
  ControlTypeEnum["simple"] = "simple";
24
25
  return ControlTypeEnum;
25
26
  }({});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inseefr/lunatic",
3
- "version": "2.7.5",
3
+ "version": "2.7.7",
4
4
  "workersVersion": "0.3.0",
5
5
  "description": "Library of questionnaire components",
6
6
  "repository": {
@@ -1,6 +0,0 @@
1
- import type { LunaticError } from '../type';
2
- /**
3
- * Keep errors with at least one error inside
4
- */
5
- declare function getErrorsWithoutEmptyValue(errors?: Record<string, LunaticError[]>): Record<string, LunaticError[]>;
6
- export default getErrorsWithoutEmptyValue;
@@ -1 +0,0 @@
1
- export { resolveComponentControls } from './resolve-component-control';
@@ -1,6 +0,0 @@
1
- import { type StateForControls } from '../../commons/compile-controls';
2
- import type { LunaticControl, LunaticError } from '../../type';
3
- /**
4
- * Convert controls into errors
5
- */
6
- export declare function resolveComponentControls(state: StateForControls, controls: LunaticControl[]): LunaticError[];
@@ -1,3 +0,0 @@
1
- import { type StateForControls } from '../../commons/compile-controls';
2
- import type { LunaticControl, LunaticError } from '../../type';
3
- export declare function resolveSimpleControl(state: StateForControls, control: LunaticControl): LunaticError | undefined;