@inseefr/lunatic 3.7.7 → 3.8.0-rc.ucq-options-variable.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/esm/type.source.d.ts +18 -3
- package/esm/use-lunatic/commons/compile-controls.js +2 -16
- package/esm/use-lunatic/commons/compile-controls.js.map +1 -1
- package/esm/use-lunatic/commons/component.d.ts +33 -0
- package/esm/use-lunatic/commons/component.js +37 -0
- package/esm/use-lunatic/commons/component.js.map +1 -1
- package/esm/use-lunatic/commons/fill-components/fill-component-expressions.d.ts +1 -1
- package/esm/use-lunatic/commons/fill-components/fill-component-expressions.js.map +1 -1
- package/esm/use-lunatic/commons/fill-components/fill-components.js +10 -2
- package/esm/use-lunatic/commons/fill-components/fill-components.js.map +1 -1
- package/esm/use-lunatic/commons/variables/behaviours/cleaning-behaviour.js.map +1 -1
- package/esm/use-lunatic/commons/variables/global-variables.d.ts +5 -0
- package/esm/use-lunatic/commons/variables/global-variables.js +7 -0
- package/esm/use-lunatic/commons/variables/global-variables.js.map +1 -0
- package/esm/use-lunatic/commons/variables/lunatic-variables-store.d.ts +21 -9
- package/esm/use-lunatic/commons/variables/lunatic-variables-store.js +67 -49
- package/esm/use-lunatic/commons/variables/lunatic-variables-store.js.map +1 -1
- package/esm/use-lunatic/commons/variables/lunatic-variables-store.spec.js +137 -6
- package/esm/use-lunatic/commons/variables/lunatic-variables-store.spec.js.map +1 -1
- package/esm/use-lunatic/commons/variables/models.d.ts +1 -0
- package/esm/use-lunatic/commons/variables/models.js +2 -0
- package/esm/use-lunatic/commons/variables/models.js.map +1 -0
- package/esm/use-lunatic/commons/variables/pairwise-variables.d.ts +29 -0
- package/esm/use-lunatic/commons/variables/pairwise-variables.js +196 -0
- package/esm/use-lunatic/commons/variables/pairwise-variables.js.map +1 -0
- package/esm/use-lunatic/props/getComponentTypeProps.d.ts +1 -1
- package/esm/use-lunatic/props/propOptions.d.ts +9 -1
- package/esm/use-lunatic/props/propOptions.js +56 -1
- package/esm/use-lunatic/props/propOptions.js.map +1 -1
- package/esm/use-lunatic/props/propOptions.spec.js +220 -56
- package/esm/use-lunatic/props/propOptions.spec.js.map +1 -1
- package/esm/use-lunatic/reducer/reducerInitializer.js +5 -1
- package/esm/use-lunatic/reducer/reducerInitializer.js.map +1 -1
- package/package.json +26 -1
- package/src/stories/checkbox/checkbox.stories.tsx +13 -0
- package/src/stories/checkbox/sourceOneDynamicOptions.json +496 -0
- package/src/stories/dropdown/dropdown.stories.tsx +12 -0
- package/src/stories/dropdown/sourceDynamicOptions.json +496 -0
- package/src/stories/pairwise/pairwise.stories.tsx +7 -0
- package/src/stories/pairwise/sourceGlobalVariables.json +337 -0
- package/src/stories/radio/radio.stories.tsx +13 -0
- package/src/stories/radio/sourceDynamicOptions.json +496 -0
- package/src/type.source.ts +18 -3
- package/src/use-lunatic/commons/compile-controls.ts +10 -42
- package/src/use-lunatic/commons/component.ts +79 -0
- package/src/use-lunatic/commons/fill-components/fill-component-expressions.ts +2 -1
- package/src/use-lunatic/commons/fill-components/fill-components.ts +9 -10
- package/src/use-lunatic/commons/variables/behaviours/cleaning-behaviour.ts +2 -4
- package/src/use-lunatic/commons/variables/global-variables.ts +9 -0
- package/src/use-lunatic/commons/variables/lunatic-variables-store.spec.ts +149 -6
- package/src/use-lunatic/commons/variables/lunatic-variables-store.ts +113 -50
- package/src/use-lunatic/commons/variables/models.ts +1 -0
- package/src/use-lunatic/commons/variables/pairwise-variables.ts +251 -0
- package/src/use-lunatic/props/propOptions.spec.ts +217 -147
- package/src/use-lunatic/props/propOptions.ts +97 -8
- package/src/use-lunatic/reducer/reducerInitializer.tsx +5 -7
- package/tsconfig.build.tsbuildinfo +1 -1
- package/type.source.d.ts +18 -3
- package/use-lunatic/commons/compile-controls.js +4 -18
- package/use-lunatic/commons/compile-controls.js.map +1 -1
- package/use-lunatic/commons/component.d.ts +33 -0
- package/use-lunatic/commons/component.js +42 -0
- package/use-lunatic/commons/component.js.map +1 -1
- package/use-lunatic/commons/fill-components/fill-component-expressions.d.ts +1 -1
- package/use-lunatic/commons/fill-components/fill-component-expressions.js.map +1 -1
- package/use-lunatic/commons/fill-components/fill-components.js +9 -1
- package/use-lunatic/commons/fill-components/fill-components.js.map +1 -1
- package/use-lunatic/commons/variables/behaviours/cleaning-behaviour.js.map +1 -1
- package/use-lunatic/commons/variables/global-variables.d.ts +5 -0
- package/use-lunatic/commons/variables/global-variables.js +11 -0
- package/use-lunatic/commons/variables/global-variables.js.map +1 -0
- package/use-lunatic/commons/variables/lunatic-variables-store.d.ts +21 -9
- package/use-lunatic/commons/variables/lunatic-variables-store.js +72 -50
- package/use-lunatic/commons/variables/lunatic-variables-store.js.map +1 -1
- package/use-lunatic/commons/variables/lunatic-variables-store.spec.js +137 -6
- package/use-lunatic/commons/variables/lunatic-variables-store.spec.js.map +1 -1
- package/use-lunatic/commons/variables/models.d.ts +1 -0
- package/use-lunatic/commons/variables/models.js +3 -0
- package/use-lunatic/commons/variables/models.js.map +1 -0
- package/use-lunatic/commons/variables/pairwise-variables.d.ts +29 -0
- package/use-lunatic/commons/variables/pairwise-variables.js +199 -0
- package/use-lunatic/commons/variables/pairwise-variables.js.map +1 -0
- package/use-lunatic/props/getComponentTypeProps.d.ts +1 -1
- package/use-lunatic/props/propOptions.d.ts +9 -1
- package/use-lunatic/props/propOptions.js +57 -2
- package/use-lunatic/props/propOptions.js.map +1 -1
- package/use-lunatic/props/propOptions.spec.js +217 -55
- package/use-lunatic/props/propOptions.spec.js.map +1 -1
- package/use-lunatic/reducer/reducerInitializer.js +5 -1
- package/use-lunatic/reducer/reducerInitializer.js.map +1 -1
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Global variables that are created when a pairwise component is present.
|
|
3
|
+
|
|
4
|
+
They can be used when creating the survey, so that filters can be made without
|
|
5
|
+
needing to make complex formula.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { ComponentDefinitionBaseWithResponse } from '../../../type.source';
|
|
9
|
+
import { LunaticSource } from '../../type';
|
|
10
|
+
import { getPairwiseComponent } from '../component';
|
|
11
|
+
import { LunaticVariable } from './lunatic-variables-store';
|
|
12
|
+
import { IterationLevel } from './models';
|
|
13
|
+
|
|
14
|
+
/** Name of the first parent defined in the pairwise component. */
|
|
15
|
+
const GLOBAL_PARENT1_NAME = 'GLOBAL_PARENT1_PRENOM';
|
|
16
|
+
/**
|
|
17
|
+
* Gender of the first parent defined in the pairwise component:
|
|
18
|
+
* `"1"` = man, `"2"` = woman.
|
|
19
|
+
*/
|
|
20
|
+
const GLOBAL_PARENT1_GENDER = 'GLOBAL_PARENT1_SEXE';
|
|
21
|
+
/** Name of the second parent defined in the pairwise component. */
|
|
22
|
+
const GLOBAL_PARENT2_NAME = 'GLOBAL_PARENT2_PRENOM';
|
|
23
|
+
/**
|
|
24
|
+
* Gender of the second parent defined in the pairwise component:
|
|
25
|
+
* `"1"` = man, `"2"` = woman.
|
|
26
|
+
*/
|
|
27
|
+
const GLOBAL_PARENT2_GENDER = 'GLOBAL_PARENT2_SEXE';
|
|
28
|
+
/** Name of the partner defined in the pairwise component. */
|
|
29
|
+
const GLOBAL_PARTNER_NAME = 'GLOBAL_CONJOINT_PRENOM';
|
|
30
|
+
/**
|
|
31
|
+
* Names of the children defined in the pairwise component: the format is a
|
|
32
|
+
* string with ";" separating them.
|
|
33
|
+
*/
|
|
34
|
+
const GLOBAL_CHILDREN_NAMES = 'GLOBAL_ENFANTS_PRENOMS';
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Pairwise links are defined by fixed values which represent a specific
|
|
38
|
+
* relationship type.
|
|
39
|
+
*/
|
|
40
|
+
enum LinkType {
|
|
41
|
+
Partner = '1',
|
|
42
|
+
Parent = '2',
|
|
43
|
+
Child = '3',
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Key used by the global dependencies map to store specific dependency. */
|
|
47
|
+
export enum PairwiseGlobalDependency {
|
|
48
|
+
Links,
|
|
49
|
+
Name,
|
|
50
|
+
Gender,
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Manually setup the available pairwise global variables thanks to known
|
|
55
|
+
* business rules.
|
|
56
|
+
*
|
|
57
|
+
* The variables information (necessary for the setup) are only returned if
|
|
58
|
+
* there is a pairwise component in the provided source.
|
|
59
|
+
*/
|
|
60
|
+
export function computePairwiseGlobalVariables(source: LunaticSource): {
|
|
61
|
+
name: string;
|
|
62
|
+
dependencies: string[];
|
|
63
|
+
globalDependencies: Map<PairwiseGlobalDependency, string>;
|
|
64
|
+
shapeFrom?: string;
|
|
65
|
+
}[] {
|
|
66
|
+
const component = getPairwiseComponent(source.components);
|
|
67
|
+
|
|
68
|
+
// There is not pairwise component, no need to initialize the variables.
|
|
69
|
+
if (!component) return [];
|
|
70
|
+
|
|
71
|
+
const variables = [];
|
|
72
|
+
|
|
73
|
+
const pairwiseVariable = (
|
|
74
|
+
component.components[0] as ComponentDefinitionBaseWithResponse
|
|
75
|
+
).response.name;
|
|
76
|
+
const nameSourceVariable = component.sourceVariables?.name;
|
|
77
|
+
const genderSourceVariable = component.sourceVariables?.gender;
|
|
78
|
+
|
|
79
|
+
// Setup variables associated to the name.
|
|
80
|
+
if (nameSourceVariable) {
|
|
81
|
+
const globalDependencies = new Map();
|
|
82
|
+
globalDependencies.set(PairwiseGlobalDependency.Links, pairwiseVariable);
|
|
83
|
+
globalDependencies.set(PairwiseGlobalDependency.Name, nameSourceVariable);
|
|
84
|
+
const dependencies = [pairwiseVariable, nameSourceVariable];
|
|
85
|
+
variables.push(
|
|
86
|
+
{
|
|
87
|
+
name: GLOBAL_PARENT1_NAME,
|
|
88
|
+
dependencies,
|
|
89
|
+
globalDependencies,
|
|
90
|
+
shapeFrom: nameSourceVariable,
|
|
91
|
+
},
|
|
92
|
+
{
|
|
93
|
+
name: GLOBAL_PARENT2_NAME,
|
|
94
|
+
dependencies,
|
|
95
|
+
globalDependencies,
|
|
96
|
+
shapeFrom: nameSourceVariable,
|
|
97
|
+
},
|
|
98
|
+
{
|
|
99
|
+
name: GLOBAL_PARTNER_NAME,
|
|
100
|
+
dependencies,
|
|
101
|
+
globalDependencies,
|
|
102
|
+
shapeFrom: nameSourceVariable,
|
|
103
|
+
},
|
|
104
|
+
{
|
|
105
|
+
name: GLOBAL_CHILDREN_NAMES,
|
|
106
|
+
dependencies,
|
|
107
|
+
globalDependencies,
|
|
108
|
+
shapeFrom: nameSourceVariable,
|
|
109
|
+
}
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// Setup variables associated to the gender.
|
|
114
|
+
if (genderSourceVariable) {
|
|
115
|
+
const globalDependencies = new Map();
|
|
116
|
+
globalDependencies.set(PairwiseGlobalDependency.Links, pairwiseVariable);
|
|
117
|
+
globalDependencies.set(
|
|
118
|
+
PairwiseGlobalDependency.Gender,
|
|
119
|
+
genderSourceVariable
|
|
120
|
+
);
|
|
121
|
+
const dependencies = [pairwiseVariable, genderSourceVariable];
|
|
122
|
+
variables.push(
|
|
123
|
+
{
|
|
124
|
+
name: GLOBAL_PARENT1_GENDER,
|
|
125
|
+
dependencies,
|
|
126
|
+
globalDependencies,
|
|
127
|
+
shapeFrom: nameSourceVariable,
|
|
128
|
+
},
|
|
129
|
+
{
|
|
130
|
+
name: GLOBAL_PARENT2_GENDER,
|
|
131
|
+
dependencies,
|
|
132
|
+
globalDependencies,
|
|
133
|
+
shapeFrom: nameSourceVariable,
|
|
134
|
+
}
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return variables;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Compute the value of the pairwise global variable.
|
|
143
|
+
*
|
|
144
|
+
* It the variable name does not exist or if the value does not exist, it will
|
|
145
|
+
* return undefined.
|
|
146
|
+
*/
|
|
147
|
+
export function computePairwiseGlobalVariableValue(
|
|
148
|
+
variableName: string,
|
|
149
|
+
iteration: IterationLevel,
|
|
150
|
+
globalDependencies: Map<PairwiseGlobalDependency, string>,
|
|
151
|
+
dictionary: Map<string, LunaticVariable>
|
|
152
|
+
): string | undefined {
|
|
153
|
+
// Get the values of the global dependencies we need
|
|
154
|
+
// (i.e. they exist in the map)
|
|
155
|
+
const linksVar = globalDependencies.get(PairwiseGlobalDependency.Links);
|
|
156
|
+
const links = linksVar ? dictionary?.get(linksVar)?.getValue() : [[]];
|
|
157
|
+
|
|
158
|
+
const namesVar = globalDependencies.get(PairwiseGlobalDependency.Name);
|
|
159
|
+
const names = namesVar ? dictionary?.get(namesVar)?.getValue() : [];
|
|
160
|
+
|
|
161
|
+
const gendersVar = globalDependencies.get(PairwiseGlobalDependency.Gender);
|
|
162
|
+
const genders = gendersVar ? dictionary?.get(gendersVar)?.getValue() : [];
|
|
163
|
+
|
|
164
|
+
return computeVariableValue(variableName, iteration, {
|
|
165
|
+
genders: genders as string[],
|
|
166
|
+
links: links as string[][],
|
|
167
|
+
names: names as string[],
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** Compute the value of the pairwise global variable. */
|
|
172
|
+
function computeVariableValue(
|
|
173
|
+
variableName: string,
|
|
174
|
+
iteration: IterationLevel,
|
|
175
|
+
{
|
|
176
|
+
genders = [],
|
|
177
|
+
links = [[]],
|
|
178
|
+
names = [],
|
|
179
|
+
}: { genders?: string[]; links: string[][]; names?: string[] }
|
|
180
|
+
): string | undefined {
|
|
181
|
+
const idx = Array.isArray(iteration) ? iteration[0] : iteration;
|
|
182
|
+
const currentLinks = links[idx];
|
|
183
|
+
|
|
184
|
+
switch (variableName) {
|
|
185
|
+
case GLOBAL_PARENT1_NAME:
|
|
186
|
+
return computeParent1NameValue(currentLinks, names);
|
|
187
|
+
case GLOBAL_PARENT1_GENDER:
|
|
188
|
+
return computeParent1GenderValue(currentLinks, genders);
|
|
189
|
+
case GLOBAL_PARENT2_NAME:
|
|
190
|
+
return computeParent2NameValue(currentLinks, names);
|
|
191
|
+
case GLOBAL_PARENT2_GENDER:
|
|
192
|
+
return computeParent2GenderValue(currentLinks, genders);
|
|
193
|
+
case GLOBAL_PARTNER_NAME:
|
|
194
|
+
return computePartnerNameValue(currentLinks, names);
|
|
195
|
+
case GLOBAL_CHILDREN_NAMES:
|
|
196
|
+
return computeChildrenNamesValue(currentLinks, names);
|
|
197
|
+
default:
|
|
198
|
+
return undefined;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/** Get the index of the first parent found in pairwise links. */
|
|
203
|
+
function getFirstParentIndex(links: string[]): number {
|
|
204
|
+
return links.indexOf(LinkType.Parent);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** Get the index of the second parent found in pairwise links. */
|
|
208
|
+
function getSecondParentIndex(links: string[]): number {
|
|
209
|
+
const firstParentIdx = links.indexOf(LinkType.Parent);
|
|
210
|
+
return links.indexOf(LinkType.Parent, firstParentIdx + 1);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/** Get the index of the first partner found in pairwise links. */
|
|
214
|
+
function getPartnerIndex(links: string[]): number {
|
|
215
|
+
return links.indexOf(LinkType.Partner);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function computeParent1NameValue(links: string[], names: string[]) {
|
|
219
|
+
const idx = getFirstParentIndex(links);
|
|
220
|
+
return idx === -1 ? undefined : names[idx];
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function computeParent1GenderValue(links: string[], genders: string[]) {
|
|
224
|
+
const idx = getFirstParentIndex(links);
|
|
225
|
+
return idx === -1 ? undefined : genders[idx];
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
function computeParent2NameValue(links: string[], names: string[]) {
|
|
229
|
+
const idx = getSecondParentIndex(links);
|
|
230
|
+
return idx === -1 ? undefined : names[idx];
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
function computeParent2GenderValue(links: string[], genders: string[]) {
|
|
234
|
+
const idx = getSecondParentIndex(links);
|
|
235
|
+
return idx === -1 ? undefined : genders[idx];
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
function computePartnerNameValue(links: string[], names: string[]) {
|
|
239
|
+
const idx = getPartnerIndex(links);
|
|
240
|
+
return idx === -1 ? undefined : names[idx];
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function computeChildrenNamesValue(links: string[], names: string[]) {
|
|
244
|
+
const res = [];
|
|
245
|
+
for (let i = 0; i < links.length; i++) {
|
|
246
|
+
if (links[i] === LinkType.Child) {
|
|
247
|
+
res.push(names[i]);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
return res.length > 0 ? res.join(';') : undefined;
|
|
251
|
+
}
|