@configura/web-api 2.0.0-alpha.9 → 2.0.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.
Files changed (100) hide show
  1. package/.eslintrc.json +5 -18
  2. package/LICENSE +201 -201
  3. package/README.md +1 -1
  4. package/dist/CatalogueAPI.d.ts +622 -551
  5. package/dist/CatalogueAPI.js +329 -273
  6. package/dist/CfgMeasure.d.ts +32 -32
  7. package/dist/CfgMeasure.js +30 -30
  8. package/dist/CfgProduct.d.ts +344 -268
  9. package/dist/CfgProduct.js +991 -780
  10. package/dist/CfgReferencePathHelper.d.ts +26 -26
  11. package/dist/CfgReferencePathHelper.js +26 -26
  12. package/dist/ConfigurationConverter.d.ts +17 -4
  13. package/dist/ConfigurationConverter.js +175 -72
  14. package/dist/index.d.ts +23 -23
  15. package/dist/index.js +23 -23
  16. package/dist/io/CfgHistoryManager.d.ts +83 -51
  17. package/dist/io/CfgHistoryManager.js +144 -82
  18. package/dist/io/CfgHistoryToProdConfConnector.d.ts +21 -20
  19. package/dist/io/CfgHistoryToProdConfConnector.js +50 -56
  20. package/dist/io/CfgIOManager.d.ts +53 -48
  21. package/dist/io/CfgIOManager.js +134 -115
  22. package/dist/io/CfgIOProdConfConnector.d.ts +64 -55
  23. package/dist/io/CfgIOProdConfConnector.js +149 -143
  24. package/dist/io/CfgIOWarningSupplier.d.ts +4 -0
  25. package/dist/io/CfgIOWarningSupplier.js +1 -0
  26. package/dist/io/CfgObservableStateManager.d.ts +25 -25
  27. package/dist/io/CfgObservableStateManager.js +69 -69
  28. package/dist/io/CfgObservableStateToProdConfConnector.d.ts +14 -15
  29. package/dist/io/CfgObservableStateToProdConfConnector.js +16 -17
  30. package/dist/io/CfgWindowEventManager.d.ts +21 -21
  31. package/dist/io/CfgWindowEventManager.js +38 -38
  32. package/dist/io/CfgWindowMessageManager.d.ts +40 -40
  33. package/dist/io/CfgWindowMessageManager.js +91 -87
  34. package/dist/io/CfgWindowMessageToProdConfConnector.d.ts +16 -17
  35. package/dist/io/CfgWindowMessageToProdConfConnector.js +18 -19
  36. package/dist/io/index.d.ts +8 -8
  37. package/dist/io/index.js +8 -8
  38. package/dist/material/CfgMaterialMapping.d.ts +7 -7
  39. package/dist/material/CfgMaterialMapping.js +181 -181
  40. package/dist/material/CfgMtrlApplication.d.ts +18 -18
  41. package/dist/material/CfgMtrlApplication.js +43 -43
  42. package/dist/material/CfgMtrlApplicationSource.d.ts +7 -7
  43. package/dist/material/CfgMtrlApplicationSource.js +8 -8
  44. package/dist/material/CfgMtrlSource.d.ts +19 -19
  45. package/dist/material/CfgMtrlSource.js +40 -40
  46. package/dist/material/CfgMtrlSourceWithMetaData.d.ts +7 -7
  47. package/dist/material/CfgMtrlSourceWithMetaData.js +1 -1
  48. package/dist/productConfiguration/CfgFeature.d.ts +192 -187
  49. package/dist/productConfiguration/CfgFeature.js +663 -645
  50. package/dist/productConfiguration/CfgOption.d.ts +157 -151
  51. package/dist/productConfiguration/CfgOption.js +444 -416
  52. package/dist/productConfiguration/CfgProductConfiguration.d.ts +128 -117
  53. package/dist/productConfiguration/CfgProductConfiguration.js +331 -307
  54. package/dist/productConfiguration/filters.d.ts +15 -15
  55. package/dist/productConfiguration/filters.js +70 -70
  56. package/dist/productConfiguration/productParamsGenerator.d.ts +15 -15
  57. package/dist/productConfiguration/productParamsGenerator.js +51 -51
  58. package/dist/productConfiguration/utilitiesProductConfiguration.d.ts +17 -17
  59. package/dist/productConfiguration/utilitiesProductConfiguration.js +87 -80
  60. package/dist/productLoader.d.ts +33 -33
  61. package/dist/productLoader.js +49 -49
  62. package/dist/syncGroups/SyncGroupsApplyMode.d.ts +20 -20
  63. package/dist/syncGroups/SyncGroupsApplyMode.js +21 -21
  64. package/dist/syncGroups/SyncGroupsHandler.d.ts +47 -40
  65. package/dist/syncGroups/SyncGroupsHandler.js +370 -359
  66. package/dist/syncGroups/SyncGroupsPathHelper.d.ts +26 -26
  67. package/dist/syncGroups/SyncGroupsPathHelper.js +90 -90
  68. package/dist/syncGroups/SyncGroupsState.d.ts +39 -35
  69. package/dist/syncGroups/SyncGroupsState.js +167 -125
  70. package/dist/syncGroups/SyncGroupsTransaction.d.ts +154 -154
  71. package/dist/syncGroups/SyncGroupsTransaction.js +589 -576
  72. package/dist/tasks/TaskHandler.d.ts +77 -77
  73. package/dist/tasks/TaskHandler.js +276 -275
  74. package/dist/tasks/formats.d.ts +4 -4
  75. package/dist/tasks/formats.js +7 -7
  76. package/dist/tests/testData/collectorForTest.d.ts +73 -73
  77. package/dist/tests/testData/collectorForTest.js +194 -194
  78. package/dist/tests/testData/dummyProductForTest.d.ts +4 -4
  79. package/dist/tests/testData/dummyProductForTest.js +32 -32
  80. package/dist/tests/testData/testDataAdditionalProductInAdditionalProductInProductForTest.d.ts +11 -11
  81. package/dist/tests/testData/testDataAdditionalProductInAdditionalProductInProductForTest.js +282 -277
  82. package/dist/tests/testData/testDataCachedGetProduct.d.ts +5 -5
  83. package/dist/tests/testData/testDataCachedGetProduct.js +187 -185
  84. package/dist/tests/testData/testDataCachedPostValidate.d.ts +7 -7
  85. package/dist/tests/testData/testDataCachedPostValidate.js +185 -183
  86. package/dist/tests/testData/testDataNoAdditionalProductNoPropagateForTest.d.ts +3 -3
  87. package/dist/tests/testData/testDataNoAdditionalProductNoPropagateForTest.js +1099 -1099
  88. package/dist/tests/testData/testDataOptions.d.ts +12 -12
  89. package/dist/tests/testData/testDataOptions.js +60 -60
  90. package/dist/tests/testData/testDataProductAggregatedPrice.d.ts +6 -6
  91. package/dist/tests/testData/testDataProductAggregatedPrice.js +189 -187
  92. package/dist/tests/testData/testDataUpcharge.d.ts +8 -8
  93. package/dist/tests/testData/testDataUpcharge.js +121 -119
  94. package/dist/utilitiesCatalogueData.d.ts +47 -42
  95. package/dist/utilitiesCatalogueData.js +180 -173
  96. package/dist/utilitiesCataloguePermission.d.ts +38 -38
  97. package/dist/utilitiesCataloguePermission.js +79 -79
  98. package/dist/utilitiesNumericValues.d.ts +24 -24
  99. package/dist/utilitiesNumericValues.js +114 -109
  100. package/package.json +3 -3
@@ -1,307 +1,331 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
- return new (P || (P = Promise))(function (resolve, reject) {
4
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
- step((generator = generator.apply(thisArg, _arguments || [])).next());
8
- });
9
- };
10
- import { compareArrays, count, isEqualLength, isLengthUnit, Observable, toError, toLengthUnit, } from "@configura/web-utilities";
11
- import { CfgProduct } from "../CfgProduct.js";
12
- import { convertDtoConfFeaturesToSelOptions } from "../ConfigurationConverter.js";
13
- import { CfgFeature } from "./CfgFeature.js";
14
- import { ProductConfigurationBubbleMode } from "./CfgOption.js";
15
- import { syncCfgFeatures } from "./utilitiesProductConfiguration.js";
16
- /**
17
- * This class is meant to only be used through CfgProductConfiguration. It should never be
18
- * instantiated on its own. Normally the internal state of this class should never be directly
19
- * modified. CfgProductConfiguration is the class that should be used and interacted with.
20
- */
21
- export class _CfgProductConfigurationInternal {
22
- constructor(allRawFeatures, // Flat packed. All the features that can appear anyplace in the selection tree.
23
- parentProduct, rootProduct) {
24
- this.allRawFeatures = allRawFeatures;
25
- this.parentProduct = parentProduct;
26
- this.rootProduct = rootProduct;
27
- this.key = "~";
28
- this._rootFeatureRefs = [];
29
- this._features = [];
30
- this.changeObservable = new Observable();
31
- this._notifyAllOfChange = (bubbleMode, committed) => __awaiter(this, void 0, void 0, function* () {
32
- if (bubbleMode === ProductConfigurationBubbleMode.Stop) {
33
- return;
34
- }
35
- const freshRef = CfgProductConfiguration._makeNewRefFrom(this);
36
- const parent = this.parentProduct;
37
- this.changeObservable.notifyAll({
38
- freshRef,
39
- committed,
40
- });
41
- if (parent === undefined) {
42
- return;
43
- }
44
- yield parent._configurationHasChanged(freshRef, bubbleMode === ProductConfigurationBubbleMode.OneLevel
45
- ? ProductConfigurationBubbleMode.Stop
46
- : bubbleMode, committed);
47
- });
48
- /** Called by child to tell its parent that it has changed. */
49
- this._childHasChanged = (freshRef, bubbleMode, committed) => __awaiter(this, void 0, void 0, function* () {
50
- const features = this._features;
51
- const i = features.findIndex((a) => a.isBackedBySame(freshRef));
52
- if (i === -1) {
53
- throw Error("Child feature not found");
54
- }
55
- features[i] = freshRef;
56
- yield this._notifyAllOfChange(bubbleMode, committed);
57
- });
58
- this.getDtoConf = (includeExtendedData) => this._features.map((f) => f._internal.getDtoConf(includeExtendedData));
59
- /**
60
- * When used internally the notifications are taken care off by the caller, but if set from
61
- * outside we want notifications to bubble all the way to the root.
62
- */
63
- this.setApiSelection = (selectedOptions, bubbleToRoot) => __awaiter(this, void 0, void 0, function* () {
64
- const featuresLength = this._features.length;
65
- const selectedOptionsLength = selectedOptions.length;
66
- if (featuresLength !== selectedOptionsLength) {
67
- throw new Error(`Wrong selection count. Features on this configuration: ${featuresLength}. Passed feature count: ${selectedOptionsLength}.`);
68
- }
69
- const change = (yield Promise.all(this._features.map((f, i) => f._internal.setApiSelection(selectedOptions[i].next)))).some((b) => b);
70
- if (change) {
71
- yield this._notifyAllOfChange(bubbleToRoot
72
- ? ProductConfigurationBubbleMode.ToRoot
73
- : ProductConfigurationBubbleMode.OneLevel, true);
74
- }
75
- return change;
76
- });
77
- this.structureCompare = (other, strictOrder, descriptionMatch) => compareArrays(this.features, other.features, (l, r) => l._internal.structureCompare(r._internal, strictOrder, descriptionMatch), strictOrder);
78
- /**
79
- * When used internally the notifications are taken care off by the caller, but if set from
80
- * outside we want notifications to bubble all the way to the root.
81
- */
82
- this.tryMatchSelection = (other, descriptionMatch = false, // Match on case insensitive description, not code
83
- validate) => __awaiter(this, void 0, void 0, function* () {
84
- const thisFeatures = this.features;
85
- const otherFeatures = other.features;
86
- const change = (yield Promise.all(otherFeatures.map((otherF) => (() => __awaiter(this, void 0, void 0, function* () {
87
- if (1 <
88
- count(otherFeatures, (item) => item._internal.keyMatch(otherF._internal, descriptionMatch))) {
89
- console.warn("tryMatchSelection will ignore items with same key");
90
- return false;
91
- }
92
- const toSetFeatures = thisFeatures.filter((f) => f._internal.keyMatch(otherF._internal, descriptionMatch));
93
- if (1 < toSetFeatures.length) {
94
- console.warn("tryMatchSelection will ignore items with same key");
95
- return false;
96
- }
97
- if (toSetFeatures.length === 0) {
98
- return false;
99
- }
100
- return yield toSetFeatures[0]._internal.tryMatchSelection(otherF._internal, descriptionMatch);
101
- }))()))).some((b) => b);
102
- if (change) {
103
- yield this._notifyAllOfChange(validate
104
- ? ProductConfigurationBubbleMode.Validate
105
- : ProductConfigurationBubbleMode.OneLevel, true);
106
- }
107
- return change;
108
- });
109
- /** Only selected features. */
110
- this._getFeaturesWithCode = (code) => this._features.reduce((agg, feature) => {
111
- agg.push(...feature._internal._getFeaturesWithCode(code));
112
- return agg;
113
- }, []);
114
- this.populateFeatures = (rootFeatureRefs) => {
115
- this._rootFeatureRefs = rootFeatureRefs;
116
- this._features = syncCfgFeatures(rootFeatureRefs, this._features, this.allRawFeatures, this, this, this.parentProduct, this.rootProduct);
117
- };
118
- this.setStretchReferenceLength = (measureParamCode, referenceLength, unit) => __awaiter(this, void 0, void 0, function* () {
119
- if (measureParamCode === "") {
120
- return false;
121
- }
122
- if (referenceLength === undefined) {
123
- // We assume that referenceLength === undefined shall be ignored.
124
- // One can add a measureParam to a Feature in Catalogue Creator
125
- // without actually having any numeric values on the Options.
126
- // Then this will happen. A previous version of the code assumed
127
- // undefined meant reset. However, this does not seem consistent
128
- // with CET.
129
- return false;
130
- }
131
- const stretchReferenceLengthsByMeasureParamCode = this.stretchReferenceLengthsByMeasureParamCode;
132
- const stretchReferenceLength = stretchReferenceLengthsByMeasureParamCode.get(measureParamCode);
133
- const referenceLengthWithUnit = {
134
- length: referenceLength,
135
- unit,
136
- };
137
- if (stretchReferenceLength === undefined) {
138
- stretchReferenceLengthsByMeasureParamCode.set(measureParamCode, {
139
- def: undefined,
140
- current: referenceLengthWithUnit,
141
- });
142
- }
143
- else {
144
- if (isEqualLength(stretchReferenceLength.current, referenceLengthWithUnit)) {
145
- return false;
146
- }
147
- stretchReferenceLength.current = referenceLengthWithUnit;
148
- }
149
- yield this._notifyAllOfChange(ProductConfigurationBubbleMode.ToRoot, false);
150
- return true;
151
- });
152
- /* eslint-disable */
153
- // Useful debug tool:
154
- if (false) {
155
- const dbgName = `conf${parentProduct.refKey || parentProduct.key}`;
156
- window[dbgName] = this;
157
- window.conf = this;
158
- console.log(`Use "window['${dbgName}']" or window.conf to access conf.`);
159
- }
160
- /* eslint-enable */
161
- /**
162
- * Although the measurement-datas are also passed in validate-calls they are only used at
163
- * initial product creation. The data is assumed to always be the same for a product, so
164
- * after a validate call the product configuration values are what controls how stretched
165
- * things are.
166
- */
167
- const stretchReferenceLengthsByMeasureParamCode = new Map();
168
- for (const measurementDefinition of parentProduct.measureDefinitions) {
169
- const { measureParamCode, numericValue } = measurementDefinition;
170
- if (numericValue === undefined) {
171
- continue;
172
- }
173
- let unit;
174
- const rawUnit = numericValue.unit;
175
- if (rawUnit === undefined) {
176
- unit = parentProduct.unit;
177
- }
178
- else if (rawUnit === "") {
179
- unit = parentProduct.unit;
180
- console.warn(`MeasureDefinition unit is empty string for code ${measureParamCode}.`);
181
- }
182
- else if (isLengthUnit(rawUnit)) {
183
- unit = toLengthUnit(rawUnit);
184
- }
185
- else {
186
- console.info(`MeasureDefinition unit is non length: "${rawUnit}" for code ${measureParamCode}. Ignored for stretch.`);
187
- continue;
188
- }
189
- const referenceLength = { length: numericValue.value, unit };
190
- stretchReferenceLengthsByMeasureParamCode.set(measureParamCode, {
191
- def: referenceLength,
192
- current: referenceLength,
193
- });
194
- }
195
- this.stretchReferenceLengthsByMeasureParamCode = stretchReferenceLengthsByMeasureParamCode;
196
- }
197
- static _makeUninitialized(rootFeatureRefs, allRawFeatures, // Flat packed. All the features that can appear anyplace in the selection tree.
198
- parentProduct, rootProduct) {
199
- const configuration = new this(allRawFeatures, parentProduct, rootProduct);
200
- configuration.populateFeatures(rootFeatureRefs);
201
- return configuration;
202
- }
203
- get rootFeatureRefs() {
204
- return this._rootFeatureRefs;
205
- }
206
- // The root features at the root of the selection tree.
207
- get features() {
208
- return this._features;
209
- }
210
- _freshRefDescendants() {
211
- const features = this._features;
212
- for (let i = 0; i < features.length; i++) {
213
- const featureInternal = features[i]._internal;
214
- featureInternal._freshRefDescendants();
215
- features[i] = CfgFeature._makeNewRefFrom(featureInternal);
216
- }
217
- }
218
- }
219
- export class CfgProductConfiguration {
220
- /**
221
- * Private constructor and make-method because make new ref requires the constructor to take an
222
- * internal and we don't want those who instantiate CfgProductConfiguration to have to be aware
223
- * of the internal.
224
- */
225
- constructor(_internal) {
226
- this._internal = _internal;
227
- this.isBackedBySame = (other) => this._internal === other._internal;
228
- this.structureCompare = (other, strictOrder = true, descriptionMatch = false) => this._internal.structureCompare(other._internal, strictOrder, descriptionMatch);
229
- /**
230
- * This method will try to match the selection from another product configuration
231
- * This method does not propagate its selections.
232
- * This method will cause validation calls.
233
- */
234
- this.tryMatchSelection = (other, descriptionMatch = false // Match on case insensitive description, not code
235
- ) => __awaiter(this, void 0, void 0, function* () { return yield this._internal.tryMatchSelection(other._internal, descriptionMatch, true); });
236
- this.getApiSelection = () => convertDtoConfFeaturesToSelOptions(this._internal.getDtoConf(false), true);
237
- /**
238
- * This method does not propagate its selections.
239
- * This method will not cause validation calls. Data is assumed to already be validated.
240
- */
241
- this.setApiSelection = (selectedOptions) => __awaiter(this, void 0, void 0, function* () { return yield this._internal.setApiSelection(selectedOptions, true); });
242
- /**
243
- * Set how stretched a certain measure should be measureParamCode is the measure to be
244
- * stretched referenceLength is a value relative to the initial length of the measure. If the
245
- * stretch drawn up in Model Lab was from the start let's say 2.4 units from start point to end
246
- * point a reference value of 4.8 would stretch the start point to end point to double length.
247
- * This however does not stop things from extending outside start point to end point. So the
248
- * actual perceived length after stretching might have little to do with the referenceLength.
249
- * Undefined as referenceLength will clear, that is, make the stretched thing revert to its
250
- * default state.
251
- */
252
- this.setStretchReferenceLength = (measureParamCode, referenceLength, unit) => this._internal.setStretchReferenceLength(measureParamCode, referenceLength, unit);
253
- this.listenForChange = (l) => this._internal.changeObservable.listen(l);
254
- this.stopListenForChange = (l) => this._internal.changeObservable.stopListen(l);
255
- this.stopAllListenForChange = () => this._internal.changeObservable.stopAllListen();
256
- }
257
- /**
258
- * This method is semi-async. It will immediately give you a reference to the created
259
- * CfgProductConfiguration, but is not properly initialized until the initDone-callback
260
- * has been called.
261
- */
262
- static make(initSuccess, initFail, rootFeatureRefs, allRawFeatures, // Flat packed. All the features that can appear anyplace in the selection tree.
263
- apiSelection, parentProduct, rootProduct) {
264
- const internal = _CfgProductConfigurationInternal._makeUninitialized(rootFeatureRefs, allRawFeatures, parentProduct, rootProduct);
265
- const external = new this(internal);
266
- // Note the async-callback at the end of the following line. The call is async.
267
- internal
268
- .setApiSelection(apiSelection, false)
269
- .then(() => initSuccess(external))
270
- .catch((e) => initFail(toError(e)));
271
- return external;
272
- }
273
- /**
274
- * Makes an object wrapping the passed object. This is not a clone method, it is a method to
275
- * make a new outer reference. Like a shallow copy.
276
- *
277
- * We use this to help frameworks that are built around using equals to detect change.
278
- */
279
- static _makeNewRefFrom(internal) {
280
- return new this(internal);
281
- }
282
- get parentProduct() {
283
- return CfgProduct._makeNewRefFrom(this._internal.parentProduct);
284
- }
285
- get rootProduct() {
286
- return CfgProduct._makeNewRefFrom(this._internal.rootProduct);
287
- }
288
- get key() {
289
- return this._internal.key;
290
- }
291
- /**
292
- * Every (unprocessed) feature which might be used in this product. This is constant for a
293
- * product load. What features are actually used is controlled by rootFeatureRefs and what
294
- * options are selected.
295
- */
296
- get allRawFeatures() {
297
- return this._internal.allRawFeatures;
298
- }
299
- /** What features are used in the root of this. This can change with new validate calls. */
300
- get rootFeatureRefs() {
301
- return this._internal.rootFeatureRefs;
302
- }
303
- /** The root features at the root of the selection tree. */
304
- get features() {
305
- return this._internal.features;
306
- }
307
- }
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { compareArrays, count, isEqualLength, isLengthUnit, Observable, toError, toLengthUnit, } from "@configura/web-utilities";
11
+ import { CfgProduct } from "../CfgProduct.js";
12
+ import { convertDtoFeatureConfsToSelOptions } from "../ConfigurationConverter.js";
13
+ import { ProductConfigurationBubbleMode } from "./CfgOption.js";
14
+ import { syncCfgFeatures } from "./utilitiesProductConfiguration.js";
15
+ /**
16
+ * This class is meant to only be used through CfgProductConfiguration. It should never be
17
+ * instantiated on its own. Normally the internal state of this class should never be directly
18
+ * modified. CfgProductConfiguration is the class that should be used and interacted with.
19
+ */
20
+ export class _CfgProductConfigurationInternal {
21
+ constructor(rawFeatures, parentProduct, rootProduct, _initialRootFeatureRefs) {
22
+ this.parentProduct = parentProduct;
23
+ this.rootProduct = rootProduct;
24
+ this._initialRootFeatureRefs = _initialRootFeatureRefs;
25
+ this.key = "~";
26
+ this._rootFeatureRefs = [];
27
+ this.accumulatedRawFeatures = []; // Flat packed. May be extended in validate calls.
28
+ this._features = [];
29
+ this.changeObservable = new Observable();
30
+ this._notifyAllOfChange = (bubbleMode, committed) => __awaiter(this, void 0, void 0, function* () {
31
+ if (bubbleMode === ProductConfigurationBubbleMode.Stop) {
32
+ return;
33
+ }
34
+ const freshRef = CfgProductConfiguration._makeNewRefFrom(this);
35
+ const parent = this.parentProduct;
36
+ this.changeObservable.notifyAll({
37
+ freshRef,
38
+ committed,
39
+ });
40
+ if (parent === undefined) {
41
+ return;
42
+ }
43
+ yield parent._configurationHasChanged(freshRef, bubbleMode === ProductConfigurationBubbleMode.OneLevel
44
+ ? ProductConfigurationBubbleMode.Stop
45
+ : bubbleMode, committed);
46
+ });
47
+ /** Called by child to tell its parent that it has changed. */
48
+ this._childHasChanged = (freshRef, bubbleMode, committed) => __awaiter(this, void 0, void 0, function* () {
49
+ const features = this._features;
50
+ const i = features.findIndex((a) => a.isBackedBySame(freshRef));
51
+ if (i === -1) {
52
+ throw Error("Child feature not found");
53
+ }
54
+ features[i] = freshRef;
55
+ yield this._notifyAllOfChange(bubbleMode, committed);
56
+ });
57
+ this.getDtoConf = (includeExtendedData) => this._features.map((f) => f._internal.getDtoConf(includeExtendedData));
58
+ this.getApiSelection = () => convertDtoFeatureConfsToSelOptions(this.getDtoConf(false), true);
59
+ /**
60
+ * When used internally the notifications are taken care off by the caller, but if set from
61
+ * outside we want notifications to bubble all the way to the root.
62
+ * This method will not cause validation calls. Data is assumed to already be validated.
63
+ */
64
+ this.setApiSelection = (selectedOptions, bubbleToRoot) => __awaiter(this, void 0, void 0, function* () {
65
+ const featuresLength = this._features.length;
66
+ const selectedOptionsLength = selectedOptions.length;
67
+ if (featuresLength !== selectedOptionsLength) {
68
+ throw new Error(`Wrong selection count. Features on this configuration: ${featuresLength}. Passed feature count: ${selectedOptionsLength}.`);
69
+ }
70
+ const change = (yield Promise.all(this._features.map((f, i) => f._internal.setApiSelection(selectedOptions[i].next)))).some((b) => b);
71
+ if (change) {
72
+ yield this._notifyAllOfChange(bubbleToRoot
73
+ ? ProductConfigurationBubbleMode.ToRoot
74
+ : ProductConfigurationBubbleMode.OneLevel, true);
75
+ }
76
+ return change;
77
+ });
78
+ this.structureCompare = (other, strictOrder, descriptionMatch) => compareArrays(this.features, other.features, (l, r) => l._internal.structureCompare(r._internal, strictOrder, descriptionMatch), strictOrder);
79
+ /**
80
+ * When used internally the notifications are taken care off by the caller, but if set from
81
+ * outside we want notifications to bubble all the way to the root.
82
+ */
83
+ this.tryMatchSelection = (other, descriptionMatch = false, // Match on case insensitive description, not code
84
+ validate) => __awaiter(this, void 0, void 0, function* () {
85
+ const thisFeatures = this.features;
86
+ const otherFeatures = other.features;
87
+ const change = (yield Promise.all(otherFeatures.map((otherF) => (() => __awaiter(this, void 0, void 0, function* () {
88
+ if (1 <
89
+ count(otherFeatures, (item) => item._internal.keyMatch(otherF._internal, descriptionMatch))) {
90
+ console.warn("tryMatchSelection will ignore items with same key");
91
+ return false;
92
+ }
93
+ const toSetFeatures = thisFeatures.filter((f) => f._internal.keyMatch(otherF._internal, descriptionMatch));
94
+ if (1 < toSetFeatures.length) {
95
+ console.warn("tryMatchSelection will ignore items with same key");
96
+ return false;
97
+ }
98
+ if (toSetFeatures.length === 0) {
99
+ return false;
100
+ }
101
+ return yield toSetFeatures[0]._internal.tryMatchSelection(otherF._internal, descriptionMatch);
102
+ }))()))).some((b) => b);
103
+ if (change) {
104
+ yield this._notifyAllOfChange(validate
105
+ ? ProductConfigurationBubbleMode.Validate
106
+ : ProductConfigurationBubbleMode.OneLevel, true);
107
+ }
108
+ return change;
109
+ });
110
+ /** Only selected features. */
111
+ this._getFeaturesWithCode = (code) => this._features.reduce((agg, feature) => {
112
+ agg.push(...feature._internal._getFeaturesWithCode(code));
113
+ return agg;
114
+ }, []);
115
+ /**
116
+ * Extends the list of loaded potentially used features. Will warn for but ignore duplicates.
117
+ * Returns true if a change happened.
118
+ */
119
+ this.addRawFeatures = (rawFeatures, warnForDuplicates) => {
120
+ let change = false;
121
+ const accumulatedRawFeatures = this.accumulatedRawFeatures;
122
+ for (const rawFeature of rawFeatures) {
123
+ const code = rawFeature.code;
124
+ if (accumulatedRawFeatures.find((f) => code === f.code)) {
125
+ if (warnForDuplicates) {
126
+ console.warn(`Feature ${code} was already loaded. Will ignore it.`);
127
+ }
128
+ continue;
129
+ }
130
+ accumulatedRawFeatures.push(rawFeature);
131
+ change = true;
132
+ }
133
+ return change;
134
+ };
135
+ this._hasRootFeaturesChanged = false;
136
+ /**
137
+ * Populates _features based on the passed @param rootFeatureRefs .
138
+ * @return true if a change happened.
139
+ */
140
+ this.populateFeatures = (rootFeatureRefs) => {
141
+ if (compareArrays(this._rootFeatureRefs, rootFeatureRefs, (l, r) => l.code === r.code, true)) {
142
+ return false;
143
+ }
144
+ this._rootFeatureRefs = rootFeatureRefs;
145
+ this._hasRootFeaturesChanged = !compareArrays(this._initialRootFeatureRefs, rootFeatureRefs, (l, r) => l.code === r.code);
146
+ this._features = syncCfgFeatures(rootFeatureRefs, this._features, this.accumulatedRawFeatures, this, this, this.parentProduct, this.rootProduct);
147
+ return true;
148
+ };
149
+ this.setStretchReferenceLength = (measureParamCode, referenceLength, unit) => __awaiter(this, void 0, void 0, function* () {
150
+ if (measureParamCode === "") {
151
+ return false;
152
+ }
153
+ if (referenceLength === undefined) {
154
+ // We assume that referenceLength === undefined shall be ignored.
155
+ // One can add a measureParam to a Feature in Catalogue Creator
156
+ // without actually having any numeric values on the Options.
157
+ // Then this will happen. A previous version of the code assumed
158
+ // undefined meant reset. However, this does not seem consistent
159
+ // with CET.
160
+ return false;
161
+ }
162
+ const stretchReferenceLengthsByMeasureParamCode = this.stretchReferenceLengthsByMeasureParamCode;
163
+ const stretchReferenceLength = stretchReferenceLengthsByMeasureParamCode.get(measureParamCode);
164
+ const referenceLengthWithUnit = {
165
+ length: referenceLength,
166
+ unit,
167
+ };
168
+ if (stretchReferenceLength === undefined) {
169
+ stretchReferenceLengthsByMeasureParamCode.set(measureParamCode, {
170
+ def: undefined,
171
+ current: referenceLengthWithUnit,
172
+ });
173
+ }
174
+ else {
175
+ if (isEqualLength(stretchReferenceLength.current, referenceLengthWithUnit)) {
176
+ return false;
177
+ }
178
+ stretchReferenceLength.current = referenceLengthWithUnit;
179
+ }
180
+ yield this._notifyAllOfChange(ProductConfigurationBubbleMode.ToRoot, false);
181
+ return true;
182
+ });
183
+ /* eslint-disable */
184
+ // Useful debug tool:
185
+ if (false) {
186
+ const dbgName = `conf${parentProduct.refKey || parentProduct.key}`;
187
+ window[dbgName] = this;
188
+ window.conf = this;
189
+ console.log(`Use "window['${dbgName}']" or window.conf to access conf.`);
190
+ }
191
+ /* eslint-enable */
192
+ this.addRawFeatures(rawFeatures, true);
193
+ this.populateFeatures(_initialRootFeatureRefs);
194
+ /**
195
+ * Although the measurement-datas are also passed in validate-calls they are only used at
196
+ * initial product creation. The data is assumed to always be the same for a product, so
197
+ * after a validate call the product configuration values are what controls how stretched
198
+ * things are.
199
+ */
200
+ const stretchReferenceLengthsByMeasureParamCode = new Map();
201
+ for (const measurementDefinition of parentProduct.measureDefinitions) {
202
+ const { measureParamCode, numericValue } = measurementDefinition;
203
+ if (numericValue === undefined) {
204
+ continue;
205
+ }
206
+ let unit;
207
+ const rawUnit = numericValue.unit;
208
+ if (rawUnit === undefined) {
209
+ unit = parentProduct.unit;
210
+ }
211
+ else if (rawUnit === "") {
212
+ unit = parentProduct.unit;
213
+ console.warn(`MeasureDefinition unit is empty string for code ${measureParamCode}.`);
214
+ }
215
+ else if (isLengthUnit(rawUnit)) {
216
+ unit = toLengthUnit(rawUnit);
217
+ }
218
+ else {
219
+ console.info(`MeasureDefinition unit is non length: "${rawUnit}" for code ${measureParamCode}. Ignored for stretch.`);
220
+ continue;
221
+ }
222
+ const referenceLength = { length: numericValue.value, unit };
223
+ stretchReferenceLengthsByMeasureParamCode.set(measureParamCode, {
224
+ def: referenceLength,
225
+ current: referenceLength,
226
+ });
227
+ }
228
+ this.stretchReferenceLengthsByMeasureParamCode = stretchReferenceLengthsByMeasureParamCode;
229
+ }
230
+ static _makeUninitialized(rootFeatureRefs, rawFeatures, // Flat packed. All the features that can currently appear anyplace in the selection tree.
231
+ parentProduct, rootProduct) {
232
+ return new this(rawFeatures, parentProduct, rootProduct, rootFeatureRefs);
233
+ }
234
+ get rootFeatureRefs() {
235
+ return this._rootFeatureRefs;
236
+ }
237
+ // The root features at the root of the selection tree.
238
+ get features() {
239
+ return this._features;
240
+ }
241
+ /**
242
+ * True if what root Features are used is not the same as at initial load.
243
+ * This means that functional selection has happened.
244
+ */
245
+ get hasRootFeaturesChanged() {
246
+ return this._hasRootFeaturesChanged;
247
+ }
248
+ }
249
+ export class CfgProductConfiguration {
250
+ /**
251
+ * Private constructor and make-method because make new ref requires the constructor to take an
252
+ * internal and we don't want those who instantiate CfgProductConfiguration to have to be aware
253
+ * of the internal.
254
+ */
255
+ constructor(_internal) {
256
+ this._internal = _internal;
257
+ this.isBackedBySame = (other) => this._internal === other._internal;
258
+ this.structureCompare = (other, strictOrder = true, descriptionMatch = false) => this._internal.structureCompare(other._internal, strictOrder, descriptionMatch);
259
+ /**
260
+ * This method will try to match the selection from another product configuration
261
+ * This method does not propagate its selections.
262
+ * This method will cause validation calls.
263
+ */
264
+ this.tryMatchSelection = (other, descriptionMatch = false // Match on case insensitive description, not code
265
+ ) => __awaiter(this, void 0, void 0, function* () { return yield this._internal.tryMatchSelection(other._internal, descriptionMatch, true); });
266
+ /**
267
+ * Set how stretched a certain measure should be measureParamCode is the measure to be
268
+ * stretched referenceLength is a value relative to the initial length of the measure. If the
269
+ * stretch drawn up in Model Lab was from the start let's say 2.4 units from start point to end
270
+ * point a reference value of 4.8 would stretch the start point to end point to double length.
271
+ * This however does not stop things from extending outside start point to end point. So the
272
+ * actual perceived length after stretching might have little to do with the referenceLength.
273
+ * Undefined as referenceLength will clear, that is, make the stretched thing revert to its
274
+ * default state.
275
+ */
276
+ this.setStretchReferenceLength = (measureParamCode, referenceLength, unit) => this._internal.setStretchReferenceLength(measureParamCode, referenceLength, unit);
277
+ this.listenForChange = (l) => this._internal.changeObservable.listen(l);
278
+ this.stopListenForChange = (l) => this._internal.changeObservable.stopListen(l);
279
+ this.stopAllListenForChange = () => this._internal.changeObservable.stopAllListen();
280
+ }
281
+ /**
282
+ * This method is semi-async. It will immediately give you a reference to the created
283
+ * CfgProductConfiguration, but is not properly initialized until the initDone-callback
284
+ * has been called.
285
+ */
286
+ static make(initSuccess, initFail, rootFeatureRefs, rawFeatures, // Flat packed. All the features that can currently appear anyplace in the selection tree.
287
+ apiSelection, parentProduct, rootProduct) {
288
+ const internal = _CfgProductConfigurationInternal._makeUninitialized(rootFeatureRefs, rawFeatures, parentProduct, rootProduct);
289
+ const external = new this(internal);
290
+ // Note the async-callback at the end of the following line. The call is async.
291
+ internal
292
+ .setApiSelection(apiSelection, false)
293
+ .then(() => initSuccess(external))
294
+ .catch((e) => initFail(toError(e)));
295
+ return external;
296
+ }
297
+ /**
298
+ * Makes an object wrapping the passed object. This is not a clone method, it is a method to
299
+ * make a new outer reference. Like a shallow copy.
300
+ *
301
+ * We use this to help frameworks that are built around using equals to detect change.
302
+ */
303
+ static _makeNewRefFrom(internal) {
304
+ return new this(internal);
305
+ }
306
+ get parentProduct() {
307
+ return CfgProduct._makeNewRefFrom(this._internal.parentProduct);
308
+ }
309
+ get rootProduct() {
310
+ return CfgProduct._makeNewRefFrom(this._internal.rootProduct);
311
+ }
312
+ get key() {
313
+ return this._internal.key;
314
+ }
315
+ /**
316
+ * Every (unprocessed) feature that is currently loaded for this product. This can be extended
317
+ * by validation calls. What features are actually used is controlled by rootFeatureRefs and what
318
+ * options are selected.
319
+ */
320
+ get rawFeatures() {
321
+ return this._internal.accumulatedRawFeatures;
322
+ }
323
+ /** What features are used in the root of this. This can change with new validate calls. */
324
+ get rootFeatureRefs() {
325
+ return this._internal.rootFeatureRefs;
326
+ }
327
+ /** The root features at the root of the selection tree. */
328
+ get features() {
329
+ return this._internal.features;
330
+ }
331
+ }