@configura/web-api 2.0.0 → 2.1.0-alpha.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 -5
  2. package/LICENSE +201 -201
  3. package/README.md +1 -1
  4. package/dist/CatalogueAPI.d.ts +622 -622
  5. package/dist/CatalogueAPI.js +329 -329
  6. package/dist/CfgMeasure.d.ts +32 -32
  7. package/dist/CfgMeasure.js +30 -30
  8. package/dist/CfgProduct.d.ts +344 -344
  9. package/dist/CfgProduct.js +990 -991
  10. package/dist/CfgReferencePathHelper.d.ts +26 -26
  11. package/dist/CfgReferencePathHelper.js +26 -26
  12. package/dist/index.d.ts +23 -23
  13. package/dist/index.js +23 -23
  14. package/dist/io/CfgHistoryManager.d.ts +83 -83
  15. package/dist/io/CfgHistoryManager.js +144 -144
  16. package/dist/io/CfgHistoryToProdConfConnector.d.ts +21 -21
  17. package/dist/io/CfgHistoryToProdConfConnector.js +50 -50
  18. package/dist/io/CfgIOManager.d.ts +53 -53
  19. package/dist/io/CfgIOManager.js +134 -134
  20. package/dist/io/CfgIOProdConfConnector.d.ts +54 -64
  21. package/dist/io/CfgIOProdConfConnector.js +137 -149
  22. package/dist/io/CfgIOWarningSupplier.d.ts +3 -3
  23. package/dist/io/CfgIOWarningSupplier.js +1 -1
  24. package/dist/io/CfgObservableStateManager.d.ts +25 -25
  25. package/dist/io/CfgObservableStateManager.js +69 -69
  26. package/dist/io/CfgObservableStateToProdConfConnector.d.ts +15 -14
  27. package/dist/io/CfgObservableStateToProdConfConnector.js +17 -16
  28. package/dist/io/CfgWindowEventManager.d.ts +21 -21
  29. package/dist/io/CfgWindowEventManager.js +38 -38
  30. package/dist/io/CfgWindowMessageManager.d.ts +40 -40
  31. package/dist/io/CfgWindowMessageManager.js +91 -91
  32. package/dist/io/CfgWindowMessageToProdConfConnector.d.ts +17 -16
  33. package/dist/io/CfgWindowMessageToProdConfConnector.js +19 -18
  34. package/dist/io/index.d.ts +8 -8
  35. package/dist/io/index.js +8 -8
  36. package/dist/material/CfgMaterialMapping.d.ts +7 -7
  37. package/dist/material/CfgMaterialMapping.js +181 -181
  38. package/dist/material/CfgMtrlApplication.d.ts +18 -18
  39. package/dist/material/CfgMtrlApplication.js +43 -43
  40. package/dist/material/CfgMtrlApplicationSource.d.ts +7 -7
  41. package/dist/material/CfgMtrlApplicationSource.js +8 -8
  42. package/dist/material/CfgMtrlSource.d.ts +19 -19
  43. package/dist/material/CfgMtrlSource.js +40 -40
  44. package/dist/material/CfgMtrlSourceWithMetaData.d.ts +7 -7
  45. package/dist/material/CfgMtrlSourceWithMetaData.js +1 -1
  46. package/dist/productConfiguration/CfgFeature.d.ts +192 -192
  47. package/dist/productConfiguration/CfgFeature.js +664 -663
  48. package/dist/productConfiguration/CfgOption.d.ts +157 -157
  49. package/dist/productConfiguration/CfgOption.js +444 -444
  50. package/dist/productConfiguration/CfgProductConfiguration.d.ts +128 -128
  51. package/dist/productConfiguration/CfgProductConfiguration.js +331 -331
  52. package/dist/productConfiguration/filters.d.ts +17 -15
  53. package/dist/productConfiguration/filters.js +141 -70
  54. package/dist/productConfiguration/productParamsGenerator.d.ts +15 -15
  55. package/dist/productConfiguration/productParamsGenerator.js +51 -51
  56. package/dist/productConfiguration/utilitiesProductConfiguration.d.ts +17 -17
  57. package/dist/productConfiguration/utilitiesProductConfiguration.js +87 -87
  58. package/dist/productLoader.d.ts +33 -33
  59. package/dist/productLoader.js +49 -49
  60. package/dist/syncGroups/SyncGroupsApplyMode.d.ts +20 -20
  61. package/dist/syncGroups/SyncGroupsApplyMode.js +21 -21
  62. package/dist/syncGroups/SyncGroupsHandler.d.ts +47 -47
  63. package/dist/syncGroups/SyncGroupsHandler.js +370 -370
  64. package/dist/syncGroups/SyncGroupsPathHelper.d.ts +26 -26
  65. package/dist/syncGroups/SyncGroupsPathHelper.js +90 -90
  66. package/dist/syncGroups/SyncGroupsState.d.ts +39 -39
  67. package/dist/syncGroups/SyncGroupsState.js +167 -167
  68. package/dist/syncGroups/SyncGroupsTransaction.d.ts +154 -154
  69. package/dist/syncGroups/SyncGroupsTransaction.js +589 -589
  70. package/dist/tasks/TaskHandler.d.ts +77 -77
  71. package/dist/tasks/TaskHandler.js +276 -276
  72. package/dist/tasks/formats.d.ts +4 -4
  73. package/dist/tasks/formats.js +7 -7
  74. package/dist/tests/testData/collectorForTest.d.ts +73 -73
  75. package/dist/tests/testData/collectorForTest.js +194 -194
  76. package/dist/tests/testData/dummyProductForTest.d.ts +4 -4
  77. package/dist/tests/testData/dummyProductForTest.js +32 -32
  78. package/dist/tests/testData/testDataAdditionalProductInAdditionalProductInProductForTest.d.ts +11 -11
  79. package/dist/tests/testData/testDataAdditionalProductInAdditionalProductInProductForTest.js +282 -282
  80. package/dist/tests/testData/testDataCachedGetProduct.d.ts +5 -5
  81. package/dist/tests/testData/testDataCachedGetProduct.js +187 -187
  82. package/dist/tests/testData/testDataCachedPostValidate.d.ts +7 -7
  83. package/dist/tests/testData/testDataCachedPostValidate.js +185 -185
  84. package/dist/tests/testData/testDataNoAdditionalProductNoPropagateForTest.d.ts +3 -3
  85. package/dist/tests/testData/testDataNoAdditionalProductNoPropagateForTest.js +1099 -1099
  86. package/dist/tests/testData/testDataOptions.d.ts +12 -12
  87. package/dist/tests/testData/testDataOptions.js +60 -60
  88. package/dist/tests/testData/testDataProductAggregatedPrice.d.ts +6 -6
  89. package/dist/tests/testData/testDataProductAggregatedPrice.js +189 -189
  90. package/dist/tests/testData/testDataUpcharge.d.ts +8 -8
  91. package/dist/tests/testData/testDataUpcharge.js +121 -121
  92. package/dist/utilitiesCatalogueData.d.ts +47 -47
  93. package/dist/utilitiesCatalogueData.js +180 -180
  94. package/dist/utilitiesCataloguePermission.d.ts +38 -38
  95. package/dist/utilitiesCataloguePermission.js +79 -79
  96. package/dist/{ConfigurationConverter.d.ts → utilitiesConfiguration.d.ts} +29 -18
  97. package/dist/{ConfigurationConverter.js → utilitiesConfiguration.js} +200 -175
  98. package/dist/utilitiesNumericValues.d.ts +24 -24
  99. package/dist/utilitiesNumericValues.js +114 -114
  100. package/package.json +3 -3
@@ -1,331 +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 { 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
- }
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 "../utilitiesConfiguration.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
+ }