@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,416 +1,444 @@
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, Observable, } from "@configura/web-utilities";
11
- import { CfgProduct } from "../CfgProduct.js";
12
- import { CfgMtrlApplication } from "../material/CfgMtrlApplication.js";
13
- import { CfgMtrlApplicationSource } from "../material/CfgMtrlApplicationSource.js";
14
- import { recursivelyGetPriceCodeValue } from "../utilitiesCatalogueData.js";
15
- import { NumericValuesSelection } from "../utilitiesNumericValues.js";
16
- import { CfgFeature, SelectionType } from "./CfgFeature.js";
17
- import { getMtrlPreview, syncCfgFeatures } from "./utilitiesProductConfiguration.js";
18
- export var ProductConfigurationBubbleMode;
19
- (function (ProductConfigurationBubbleMode) {
20
- /**
21
- * If this is select it will turns on all ancestors all the way up.
22
- */
23
- ProductConfigurationBubbleMode["BubbleSelected"] = "BubbleSelected";
24
- /**
25
- * Bubble to the closest CfgProduct, let it revalidate, then that will continue the bubble
26
- * after validate.
27
- */
28
- ProductConfigurationBubbleMode["Validate"] = "Validate";
29
- /**
30
- * Bubble to the closest CfgProduct, let it revalidate, then that will continue the bubble
31
- * after validate. If this is select it will turn on all ancestors all the way up.
32
- * So with this mode it is possible to select an option where its parents are not selected.
33
- */
34
- ProductConfigurationBubbleMode["ValidateAndBubbleSelected"] = "ValidateAndBubbleSelected";
35
- /**
36
- * Like ValidateAndBubbleSelected, but SyncGroups are applied after ValidateAndBubbleSelected
37
- * has been done
38
- */
39
- ProductConfigurationBubbleMode["ValidateAndBubbleSelectedAndApplySyncGroups"] = "ValidateAndBubbleSelectedAndApplySyncGroups";
40
- /**
41
- * Stop bubbling
42
- * This mode supports internal functionality and is not expected to be used by integrators.
43
- */
44
- ProductConfigurationBubbleMode["Stop"] = "Stop";
45
- /**
46
- * Bubble to the next level up the tree. In features-options the next level is considered
47
- * the next feature, so option levels are skipped over. The node we call from notifies its
48
- * parent, and the parent switches out the reference to the node. Then bubbling stops.
49
- * This mode supports internal functionality and is not expected to be used by integrators.
50
- */
51
- ProductConfigurationBubbleMode["OneLevel"] = "OneLevel";
52
- /**
53
- * Bubble to the closest CfgProduct without doing any validation
54
- * This mode supports internal functionality and is not expected to be used by integrators.
55
- */
56
- ProductConfigurationBubbleMode["ToParentProduct"] = "ToParentProduct";
57
- /**
58
- * Bubble to the root CfgProduct
59
- * This mode supports internal functionality and is not expected to be used by integrators.
60
- */
61
- ProductConfigurationBubbleMode["ToRoot"] = "ToRoot";
62
- })(ProductConfigurationBubbleMode || (ProductConfigurationBubbleMode = {}));
63
- /** Overlap in one child also count */
64
- function doesChildrenShareOptionsCode(features) {
65
- const optionCodeSet = new Set();
66
- for (const feature of features) {
67
- for (const option of feature.options) {
68
- const code = option.code;
69
- if (optionCodeSet.has(code)) {
70
- return true;
71
- }
72
- optionCodeSet.add(code);
73
- }
74
- }
75
- return false;
76
- }
77
- /**
78
- * This class is only meant to be used through CfgOption. It should never be instantiated on its
79
- * own. Normally the internal state of this class should never be directly modified. CfgOption is
80
- * the class that should be used and interacted with.
81
- */
82
- export class _CfgOptionInternal {
83
- constructor(rawOption, allRawFeatures, siblingHasDuplicateDescription, parent, parentConfiguration, parentProduct, rootProduct) {
84
- this.rawOption = rawOption;
85
- this.allRawFeatures = allRawFeatures;
86
- this.parent = parent;
87
- this.parentConfiguration = parentConfiguration;
88
- this.parentProduct = parentProduct;
89
- this.rootProduct = rootProduct;
90
- this.changeObservable = new Observable();
91
- /** Called by child to tell its parent that it has changed. */
92
- this._childHasChanged = (freshRef, bubbleMode, committed) => __awaiter(this, void 0, void 0, function* () {
93
- const features = this._features;
94
- if (features === undefined) {
95
- throw Error("Child says it changed, but no children has actually been generated");
96
- }
97
- const i = features.findIndex((a) => a.isBackedBySame(freshRef));
98
- if (i === -1) {
99
- throw Error("Child feature not found");
100
- }
101
- features[i] = freshRef;
102
- // In CfgOption we let stop bubble slip through. This is because CfgOption is sort of
103
- // a semi level. Like Feature + Option together constitutes one level.
104
- if (bubbleMode === ProductConfigurationBubbleMode.ValidateAndBubbleSelected ||
105
- bubbleMode === ProductConfigurationBubbleMode.BubbleSelected) {
106
- // selectOption takes care of the bubble
107
- yield this.parent.selectOption(this, true, bubbleMode);
108
- }
109
- else {
110
- yield this.parent._childHasChanged(this, bubbleMode, committed);
111
- }
112
- });
113
- this.getDtoConf = (includeExtendedData) => {
114
- const { features, isUseNumericValue, code, selected, numericValue } = this;
115
- if (!selected) {
116
- throw new Error("Currently only useable on selected options. Selected in the result is for future use.");
117
- }
118
- const result = {
119
- code,
120
- selected, // For future use
121
- };
122
- if (isUseNumericValue) {
123
- if (numericValue === undefined) {
124
- throw new Error("numeric Sku feature with option without numeric value");
125
- }
126
- else {
127
- result.numericValue = { value: numericValue, unit: this.unit };
128
- }
129
- }
130
- if (0 < features.length) {
131
- result.features = features.map((f) => f._internal.getDtoConf(includeExtendedData));
132
- }
133
- return result;
134
- };
135
- this.setApiSelection = (apiOptionSelection) => __awaiter(this, void 0, void 0, function* () {
136
- let features;
137
- if (apiOptionSelection === undefined) {
138
- features = this._features || []; // All already generated children
139
- }
140
- else {
141
- features = this.features; // This will generate all children
142
- }
143
- return (yield Promise.all(features.map((f) => f._internal.setApiSelection(apiOptionSelection ? apiOptionSelection.next : undefined)))).some((b) => b);
144
- });
145
- this.structureCompare = (other, strictOrder = true, descriptionMatch = false) => this.keyMatch(other, descriptionMatch) &&
146
- compareArrays(this.features, other.features, (l, r) => l._internal.structureCompare(r._internal, strictOrder, descriptionMatch), strictOrder);
147
- this.tryMatchSelection = (other, descriptionMatch = false) => __awaiter(this, void 0, void 0, function* () {
148
- return (yield Promise.all(other.features.map((otherF) => (() => __awaiter(this, void 0, void 0, function* () {
149
- if (1 <
150
- count(other.features, (item) => item._internal.keyMatch(otherF._internal, descriptionMatch))) {
151
- console.warn("tryMatchSelection will ignore items with same key");
152
- return false;
153
- }
154
- const toSetFeatures = this.features.filter((f) => f._internal.keyMatch(otherF._internal, descriptionMatch));
155
- if (1 < toSetFeatures.length) {
156
- console.warn("tryMatchSelection will ignore items with same key");
157
- return false;
158
- }
159
- if (toSetFeatures.length === 0) {
160
- return false;
161
- }
162
- return yield toSetFeatures[0]._internal.tryMatchSelection(otherF._internal, descriptionMatch);
163
- }))()))).some((b) => b);
164
- });
165
- this.keyMatch = (other, descriptionMatch = false) => descriptionMatch
166
- ? this.description.toLowerCase() === other.description.toLowerCase()
167
- : this.code === other.code;
168
- this._getFeaturesWithCode = (code) => this.features.reduce((agg, feature) => {
169
- agg.push(...feature._internal._getFeaturesWithCode(code));
170
- return agg;
171
- }, []);
172
- // Description based key helps when switching between products with similar feature-options
173
- // tree and trying to retain made selection.
174
- this.key =
175
- this.description +
176
- (this.description === "" || siblingHasDuplicateDescription ? this.code : "");
177
- const rawRanges = rawOption.codeRanges;
178
- if (rawRanges === undefined || rawRanges.length === 0) {
179
- if (this.isUseNumericValue) {
180
- throw new Error("Options in numeric sku Features must have at lease one code range");
181
- }
182
- }
183
- else {
184
- this.allowedNumericValues = new NumericValuesSelection(rawRanges);
185
- this._numericValue = this.allowedNumericValues.first;
186
- }
187
- }
188
- isAllowedNumericValue(val) {
189
- const allowedNumericValues = this.allowedNumericValues;
190
- if (allowedNumericValues === undefined) {
191
- return false;
192
- }
193
- if (this.isUseNumericValue) {
194
- return allowedNumericValues.includesValue(val);
195
- }
196
- // There is some limited support for numeric values on non numeric features.
197
- // The first value is always used in these cases.
198
- return allowedNumericValues.first === val;
199
- }
200
- get code() {
201
- return this.rawOption.code;
202
- }
203
- get isUseNumericValue() {
204
- return this.parent.isUseNumericValue;
205
- }
206
- get numericValue() {
207
- return this._numericValue;
208
- }
209
- setNumericValue(val, doSelectOption) {
210
- return __awaiter(this, void 0, void 0, function* () {
211
- if (!this.isAllowedNumericValue(val)) {
212
- throw new Error(`The value ${val} is not allowed. This could be because: 1. There are no allowed ranges defined 2. The Feature is numeric selection and the value does not fit in the allowed values 3. The Feature is not numeric selection and the value is not the first in the allowed ranges.`);
213
- }
214
- let change = false;
215
- if (this.isUseNumericValue) {
216
- if (this._numericValue !== val) {
217
- this._numericValue = val;
218
- change = true;
219
- }
220
- // It could be that even though our value did not change some sibling value did, and
221
- // this could make it needed to bubble later. Maybe. A bit uncertain about why I did
222
- // not put this in the if-statement above //Linus
223
- if (yield this.parent.pushStretch()) {
224
- change = true;
225
- }
226
- }
227
- if (doSelectOption) {
228
- if (yield this.parent.selectOption(this, true, ProductConfigurationBubbleMode.Validate)) {
229
- change = true;
230
- }
231
- }
232
- else {
233
- if (change) {
234
- yield this.parent._childHasChanged(this, ProductConfigurationBubbleMode.Stop, false);
235
- }
236
- }
237
- return change;
238
- });
239
- }
240
- get unit() {
241
- return this.parent.unit;
242
- }
243
- get description() {
244
- return this.rawOption.description;
245
- }
246
- get selected() {
247
- return this.parent.isSelected(this);
248
- }
249
- get selectedChangeInProgress() {
250
- const syncGroupHandler = this.rootProduct.syncGroupHandler;
251
- if (syncGroupHandler === undefined) {
252
- return false;
253
- }
254
- const inProgressOption = syncGroupHandler.pending;
255
- if (inProgressOption === this) {
256
- return true;
257
- }
258
- if (!(this.selected && this.parent.selectionType === SelectionType.SelectOne)) {
259
- return false;
260
- }
261
- return this.parent.options.some((o) => o._internal === inProgressOption);
262
- }
263
- get ancestorsSelected() {
264
- return this.selected && this.parent.ancestorsSelected;
265
- }
266
- get mtrlApplications() {
267
- if (this._mtrlApplications === undefined) {
268
- this._mtrlApplications = (this.rawOption.mtrlApplications || []).map((m) => CfgMtrlApplication.fromMtrlLikeApplication(CfgMtrlApplicationSource.Option, m));
269
- }
270
- return this._mtrlApplications;
271
- }
272
- get thumbnail() {
273
- return this.rawOption.material || getMtrlPreview(this.mtrlApplications);
274
- }
275
- get upcharge() {
276
- let upcharge = this.rawOption.upcharge || 0;
277
- const priceCodes = this.rawOption.priceCodes || [];
278
- const prices = this.rootProduct.prices;
279
- upcharge += recursivelyGetPriceCodeValue(priceCodes, prices) || 0;
280
- return upcharge;
281
- }
282
- get priceChangeAtSelectChange() {
283
- if (!this.parent.hasUpcharge) {
284
- return undefined;
285
- }
286
- const upcharge = this.upcharge || 0;
287
- const isSelected = this.parent.isSelected(this);
288
- if (this.parent.selectionType === SelectionType.SelectMany) {
289
- return isSelected ? -upcharge : upcharge;
290
- }
291
- if (this.parent.selectionType === SelectionType.SelectOne) {
292
- if (isSelected) {
293
- return 0;
294
- }
295
- const selectedOptions = this.parent.selectedOptions;
296
- if (1 !== selectedOptions.length) {
297
- console.warn("More or less than one selected in select one");
298
- return upcharge;
299
- }
300
- return upcharge - (selectedOptions[0].upcharge || 0);
301
- }
302
- return undefined;
303
- }
304
- get features() {
305
- if (this._features === undefined) {
306
- const allRefs = this.rawOption.featureRefs || [];
307
- const features = syncCfgFeatures(allRefs, [], this.allRawFeatures, this, this.parentConfiguration, this.parentProduct, this.rootProduct);
308
- if (doesChildrenShareOptionsCode(features)) {
309
- throw new Error("Stage does not yet properly support Options that has multiple sub-features with overlapping option codes.");
310
- }
311
- this._features = features;
312
- }
313
- return this._features;
314
- }
315
- _freshRefDescendants() {
316
- const features = this._features || [];
317
- for (let i = 0; i < features.length; i++) {
318
- const featureInternal = features[i]._internal;
319
- featureInternal._freshRefDescendants();
320
- features[i] = CfgFeature._makeNewRefFrom(featureInternal);
321
- }
322
- }
323
- }
324
- export class CfgOption {
325
- /**
326
- * Private constructor and make-method because make new ref requires the constructor to
327
- * take an internal and we don't want those who instantiate CfgOption to have to be aware
328
- * of the internal.
329
- */
330
- constructor(_internal) {
331
- this._internal = _internal;
332
- this.isBackedBySame = (other) => this._internal === other._internal;
333
- this.setNumericValue = (val, doSelectOption) => __awaiter(this, void 0, void 0, function* () { return yield this._internal.setNumericValue(val, doSelectOption); });
334
- this.isAllowedNumericValue = (val) => this._internal.isAllowedNumericValue(val);
335
- /**
336
- * Selects this Option.
337
- * Only Options belonging to Features that are "select many" can be deselected.
338
- * Calling this will cause a validation call to the server.
339
- */
340
- this.setSelected = (on) => __awaiter(this, void 0, void 0, function* () {
341
- return yield this._internal.parent.selectOption(this._internal, on, ProductConfigurationBubbleMode.ValidateAndBubbleSelectedAndApplySyncGroups);
342
- });
343
- this.listenForChange = (l) => this._internal.changeObservable.listen(l);
344
- this.stopListenForChange = (l) => this._internal.changeObservable.stopListen(l);
345
- }
346
- static make(rawOption, allRawFeatures, siblingHasDuplicateDescription, parent, parentConfiguration, parentProduct, rootProduct) {
347
- return new this(new _CfgOptionInternal(rawOption, allRawFeatures, siblingHasDuplicateDescription, parent, parentConfiguration, parentProduct, rootProduct));
348
- }
349
- /**
350
- * Makes an object wrapping the passed object. This is not a clone method,
351
- * it is a method to make a new outer reference. Like a shallow copy.
352
- * We use this to help frameworks that are build around using equals to detect change.
353
- */
354
- static _makeNewRefFrom(internal) {
355
- return new this(internal);
356
- }
357
- get parentProduct() {
358
- return CfgProduct._makeNewRefFrom(this._internal.parentProduct);
359
- }
360
- get rootProduct() {
361
- return CfgProduct._makeNewRefFrom(this._internal.rootProduct);
362
- }
363
- get rawOption() {
364
- return this._internal.rawOption;
365
- }
366
- get key() {
367
- return this._internal.key;
368
- }
369
- get code() {
370
- return this._internal.code;
371
- }
372
- get isUseNumericValue() {
373
- return this._internal.isUseNumericValue;
374
- }
375
- get numericValue() {
376
- return this._internal.numericValue;
377
- }
378
- get allowedNumericValues() {
379
- return this._internal.allowedNumericValues;
380
- }
381
- get unit() {
382
- return this._internal.unit;
383
- }
384
- get description() {
385
- return this._internal.description;
386
- }
387
- get selected() {
388
- return this._internal.selected;
389
- }
390
- /**
391
- * Selection state is in progress to be changed. This can be used in GUI
392
- * to display the state as transitioning, or as already changed.
393
- * If selectedChangeInProgress and:
394
- * selected is true, it means that this is about to get unselected
395
- * selected is false, it means that this is about to get selected
396
- */
397
- get selectedChangeInProgress() {
398
- return this._internal.selectedChangeInProgress;
399
- }
400
- /** Are all ancestors up to the CfgProductConfiguration selected? Includes self. */
401
- get ancestorsSelected() {
402
- return this._internal.ancestorsSelected;
403
- }
404
- get thumbnail() {
405
- return this._internal.thumbnail;
406
- }
407
- get upcharge() {
408
- return this._internal.upcharge;
409
- }
410
- get priceChangeAtSelectChange() {
411
- return this._internal.priceChangeAtSelectChange;
412
- }
413
- get features() {
414
- return this._internal.features;
415
- }
416
- }
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, Observable, } from "@configura/web-utilities";
11
+ import { CfgProduct } from "../CfgProduct.js";
12
+ import { CfgMtrlApplication } from "../material/CfgMtrlApplication.js";
13
+ import { CfgMtrlApplicationSource } from "../material/CfgMtrlApplicationSource.js";
14
+ import { recursivelyGetPriceCodeValue } from "../utilitiesCatalogueData.js";
15
+ import { NumericValuesSelection } from "../utilitiesNumericValues.js";
16
+ import { CfgFeature, SelectionType } from "./CfgFeature.js";
17
+ import { getMtrlPreview, syncCfgFeatures } from "./utilitiesProductConfiguration.js";
18
+ export var ProductConfigurationBubbleMode;
19
+ (function (ProductConfigurationBubbleMode) {
20
+ /**
21
+ * If this is select it will turns on all ancestors all the way up.
22
+ */
23
+ ProductConfigurationBubbleMode["BubbleSelected"] = "BubbleSelected";
24
+ /**
25
+ * Bubble to the closest CfgProduct, let it revalidate, then that will continue the bubble
26
+ * after validate.
27
+ */
28
+ ProductConfigurationBubbleMode["Validate"] = "Validate";
29
+ /**
30
+ * Bubble to the closest CfgProduct, let it revalidate, then that will continue the bubble
31
+ * after validate. If this is select it will turn on all ancestors all the way up.
32
+ * So with this mode it is possible to select an option where its parents are not selected.
33
+ */
34
+ ProductConfigurationBubbleMode["ValidateAndBubbleSelected"] = "ValidateAndBubbleSelected";
35
+ /**
36
+ * Like ValidateAndBubbleSelected, but SyncGroups are applied after ValidateAndBubbleSelected
37
+ * has been done
38
+ */
39
+ ProductConfigurationBubbleMode["ValidateAndBubbleSelectedAndApplySyncGroups"] = "ValidateAndBubbleSelectedAndApplySyncGroups";
40
+ /**
41
+ * Stop bubbling
42
+ * This mode supports internal functionality and is not expected to be used by integrators.
43
+ */
44
+ ProductConfigurationBubbleMode["Stop"] = "Stop";
45
+ /**
46
+ * Bubble to the next level up the tree. In features-options the next level is considered
47
+ * the next feature, so option levels are skipped over. The node we call from notifies its
48
+ * parent, and the parent switches out the reference to the node. Then bubbling stops.
49
+ * This mode supports internal functionality and is not expected to be used by integrators.
50
+ */
51
+ ProductConfigurationBubbleMode["OneLevel"] = "OneLevel";
52
+ /**
53
+ * Bubble to the closest CfgProduct without doing any validation
54
+ * This mode supports internal functionality and is not expected to be used by integrators.
55
+ */
56
+ ProductConfigurationBubbleMode["ToParentProduct"] = "ToParentProduct";
57
+ /**
58
+ * Bubble to the root CfgProduct
59
+ * This mode supports internal functionality and is not expected to be used by integrators.
60
+ */
61
+ ProductConfigurationBubbleMode["ToRoot"] = "ToRoot";
62
+ })(ProductConfigurationBubbleMode || (ProductConfigurationBubbleMode = {}));
63
+ /** Overlap in one child also count */
64
+ function doesChildrenShareOptionsCode(features) {
65
+ const optionCodeSet = new Set();
66
+ for (const feature of features) {
67
+ for (const option of feature.options) {
68
+ const code = option.code;
69
+ if (optionCodeSet.has(code)) {
70
+ return true;
71
+ }
72
+ optionCodeSet.add(code);
73
+ }
74
+ }
75
+ return false;
76
+ }
77
+ /**
78
+ * This class is only meant to be used through CfgOption. It should never be instantiated on its
79
+ * own. Normally the internal state of this class should never be directly modified. CfgOption is
80
+ * the class that should be used and interacted with.
81
+ */
82
+ export class _CfgOptionInternal {
83
+ constructor(rawOption, rawFeatures, siblingHasDuplicateDescription, parent, parentConfiguration, parentProduct, rootProduct) {
84
+ this.rawOption = rawOption;
85
+ this.rawFeatures = rawFeatures;
86
+ this.parent = parent;
87
+ this.parentConfiguration = parentConfiguration;
88
+ this.parentProduct = parentProduct;
89
+ this.rootProduct = rootProduct;
90
+ this.changeObservable = new Observable();
91
+ /** Called by child to tell its parent that it has changed. */
92
+ this._childHasChanged = (freshRef, bubbleMode, committed) => __awaiter(this, void 0, void 0, function* () {
93
+ const features = this._features;
94
+ if (features === undefined) {
95
+ throw Error("Child says it changed, but no children has actually been generated");
96
+ }
97
+ const i = features.findIndex((a) => a.isBackedBySame(freshRef));
98
+ if (i === -1) {
99
+ throw Error("Child feature not found");
100
+ }
101
+ features[i] = freshRef;
102
+ // In CfgOption we let stop bubble slip through. This is because CfgOption is sort of
103
+ // a semi level. Like Feature + Option together constitutes one level.
104
+ if (bubbleMode === ProductConfigurationBubbleMode.ValidateAndBubbleSelected ||
105
+ bubbleMode === ProductConfigurationBubbleMode.BubbleSelected) {
106
+ // selectOption takes care of the bubble
107
+ yield this.parent.selectOption(this, true, bubbleMode);
108
+ }
109
+ else {
110
+ yield this.parent._childHasChanged(this, bubbleMode, committed);
111
+ }
112
+ });
113
+ this.getDtoConf = (includeExtendedData) => {
114
+ const { features, isUseNumericValue, code, selected, numericValue } = this;
115
+ if (!selected) {
116
+ throw new Error("Currently only useable on selected options. Selected in the result is for future use.");
117
+ }
118
+ const result = {
119
+ code,
120
+ selected, // For future use
121
+ };
122
+ if (isUseNumericValue) {
123
+ if (numericValue === undefined) {
124
+ throw new Error("numeric Sku feature with option without numeric value");
125
+ }
126
+ else {
127
+ result.numericValue = { value: numericValue, unit: this.unit };
128
+ }
129
+ }
130
+ if (0 < features.length) {
131
+ result.features = features.map((f) => f._internal.getDtoConf(includeExtendedData));
132
+ }
133
+ return result;
134
+ };
135
+ // DtoOptionConf is the newer more easily readable format for configuration. As
136
+ // the API:s are still using the older format (and will for the forseeable future) we need
137
+ // to support both formats. The new format can be converted to the old, but not the other
138
+ // way around. For that reason the get-method above uses the new format, and the set-method
139
+ // below the old format. As these functions are meant to only be used internally this should't
140
+ // cause too much confusion.
141
+ this.setApiSelection = (apiOptionSelection) => __awaiter(this, void 0, void 0, function* () {
142
+ let change = false;
143
+ const upcharge = this._calculateUpcharge();
144
+ if (this._upcharge !== upcharge) {
145
+ change = true;
146
+ this._upcharge = upcharge;
147
+ }
148
+ let features;
149
+ if (apiOptionSelection === undefined) {
150
+ features = this._features || []; // All already generated children
151
+ }
152
+ else {
153
+ features = this.features; // This will generate all children
154
+ }
155
+ if ((yield Promise.all(features.map((f) => f._internal.setApiSelection(apiOptionSelection ? apiOptionSelection.next : undefined)))).some((b) => b)) {
156
+ change = true;
157
+ }
158
+ return change;
159
+ });
160
+ this.structureCompare = (other, strictOrder = true, descriptionMatch = false) => this.keyMatch(other, descriptionMatch) &&
161
+ compareArrays(this.features, other.features, (l, r) => l._internal.structureCompare(r._internal, strictOrder, descriptionMatch), strictOrder);
162
+ this.tryMatchSelection = (other, descriptionMatch = false) => __awaiter(this, void 0, void 0, function* () {
163
+ return (yield Promise.all(other.features.map((otherF) => (() => __awaiter(this, void 0, void 0, function* () {
164
+ if (1 <
165
+ count(other.features, (item) => item._internal.keyMatch(otherF._internal, descriptionMatch))) {
166
+ console.warn("tryMatchSelection will ignore items with same key");
167
+ return false;
168
+ }
169
+ const toSetFeatures = this.features.filter((f) => f._internal.keyMatch(otherF._internal, descriptionMatch));
170
+ if (1 < toSetFeatures.length) {
171
+ console.warn("tryMatchSelection will ignore items with same key");
172
+ return false;
173
+ }
174
+ if (toSetFeatures.length === 0) {
175
+ return false;
176
+ }
177
+ return yield toSetFeatures[0]._internal.tryMatchSelection(otherF._internal, descriptionMatch);
178
+ }))()))).some((b) => b);
179
+ });
180
+ this.keyMatch = (other, descriptionMatch = false) => descriptionMatch
181
+ ? this.description.toLowerCase() === other.description.toLowerCase()
182
+ : this.code === other.code;
183
+ this._getFeaturesWithCode = (code) => this.features.reduce((agg, feature) => {
184
+ agg.push(...feature._internal._getFeaturesWithCode(code));
185
+ return agg;
186
+ }, []);
187
+ // Description based key helps when switching between products with similar feature-options
188
+ // tree and trying to retain made selection.
189
+ this.key =
190
+ this.description +
191
+ (this.description === "" || siblingHasDuplicateDescription ? this.code : "");
192
+ this._upcharge = this._calculateUpcharge();
193
+ const rawRanges = rawOption.codeRanges;
194
+ if (rawRanges === undefined || rawRanges.length === 0) {
195
+ if (this.isUseNumericValue) {
196
+ throw new Error("Options in numeric sku Features must have at lease one code range");
197
+ }
198
+ }
199
+ else {
200
+ this.allowedNumericValues = new NumericValuesSelection(rawRanges);
201
+ this._numericValue = this.allowedNumericValues.first;
202
+ }
203
+ }
204
+ isAllowedNumericValue(val) {
205
+ const allowedNumericValues = this.allowedNumericValues;
206
+ if (allowedNumericValues === undefined) {
207
+ return false;
208
+ }
209
+ if (this.isUseNumericValue) {
210
+ return allowedNumericValues.includesValue(val);
211
+ }
212
+ // There is some limited support for numeric values on non numeric features.
213
+ // The first value is always used in these cases.
214
+ return allowedNumericValues.first === val;
215
+ }
216
+ get code() {
217
+ return this.rawOption.code;
218
+ }
219
+ get notes() {
220
+ var _a;
221
+ return this.parentProduct.getNotes((_a = this.rawOption.noteRefs) !== null && _a !== void 0 ? _a : []);
222
+ }
223
+ get miscFiles() {
224
+ var _a;
225
+ return (_a = this.rawOption.miscFiles) !== null && _a !== void 0 ? _a : [];
226
+ }
227
+ get isUseNumericValue() {
228
+ return this.parent.isUseNumericValue;
229
+ }
230
+ get numericValue() {
231
+ return this._numericValue;
232
+ }
233
+ setNumericValue(val, doSelectOption) {
234
+ return __awaiter(this, void 0, void 0, function* () {
235
+ if (!this.isAllowedNumericValue(val)) {
236
+ throw new Error(`The value ${val} is not allowed. This could be because: 1. There are no allowed ranges defined 2. The Feature is numeric selection and the value does not fit in the allowed values 3. The Feature is not numeric selection and the value is not the first in the allowed ranges.`);
237
+ }
238
+ let change = false;
239
+ if (this.isUseNumericValue) {
240
+ if (this._numericValue !== val) {
241
+ this._numericValue = val;
242
+ change = true;
243
+ }
244
+ // It could be that even though our value did not change some sibling value did, and
245
+ // this could make it needed to bubble later. Maybe. A bit uncertain about why I did
246
+ // not put this in the if-statement above //Linus
247
+ if (yield this.parent.pushStretch()) {
248
+ change = true;
249
+ }
250
+ }
251
+ if (doSelectOption) {
252
+ if (yield this.parent.selectOption(this, true, ProductConfigurationBubbleMode.Validate)) {
253
+ change = true;
254
+ }
255
+ }
256
+ else {
257
+ if (change) {
258
+ yield this.parent._childHasChanged(this, ProductConfigurationBubbleMode.Stop, false);
259
+ }
260
+ }
261
+ return change;
262
+ });
263
+ }
264
+ get unit() {
265
+ return this.parent.unit;
266
+ }
267
+ get description() {
268
+ return this.rawOption.description;
269
+ }
270
+ get selected() {
271
+ return this.parent.isSelected(this);
272
+ }
273
+ get selectedChangeInProgress() {
274
+ const syncGroupHandler = this.rootProduct.syncGroupHandler;
275
+ if (syncGroupHandler === undefined) {
276
+ return false;
277
+ }
278
+ const inProgressOption = syncGroupHandler.pending;
279
+ if (inProgressOption === this) {
280
+ return true;
281
+ }
282
+ if (!(this.selected && this.parent.selectionType === SelectionType.SelectOne)) {
283
+ return false;
284
+ }
285
+ return this.parent.options.some((o) => o._internal === inProgressOption);
286
+ }
287
+ get ancestorsSelected() {
288
+ return this.selected && this.parent.ancestorsSelected;
289
+ }
290
+ get mtrlApplications() {
291
+ if (this._mtrlApplications === undefined) {
292
+ this._mtrlApplications = (this.rawOption.mtrlApplications || []).map((m) => CfgMtrlApplication.fromMtrlLikeApplication(CfgMtrlApplicationSource.Option, m));
293
+ }
294
+ return this._mtrlApplications;
295
+ }
296
+ get thumbnail() {
297
+ return this.rawOption.material || getMtrlPreview(this.mtrlApplications);
298
+ }
299
+ _calculateUpcharge() {
300
+ let upcharge = this.rawOption.upcharge || 0;
301
+ const priceCodes = this.rawOption.priceCodes || [];
302
+ const prices = this.rootProduct.prices;
303
+ upcharge += recursivelyGetPriceCodeValue(priceCodes, prices) || 0;
304
+ return upcharge;
305
+ }
306
+ get upcharge() {
307
+ return this._upcharge;
308
+ }
309
+ get priceChangeAtSelectChange() {
310
+ if (!this.parent.hasUpcharge) {
311
+ return 0;
312
+ }
313
+ const upcharge = this._upcharge || 0;
314
+ const isSelected = this.parent.isSelected(this);
315
+ if (this.parent.selectionType === SelectionType.SelectMany) {
316
+ return isSelected ? -upcharge : upcharge;
317
+ }
318
+ if (this.parent.selectionType === SelectionType.SelectOne) {
319
+ if (isSelected) {
320
+ return 0;
321
+ }
322
+ const selectedOptions = this.parent.selectedOptions;
323
+ if (1 !== selectedOptions.length) {
324
+ console.warn("More or less than one selected in select one");
325
+ return upcharge;
326
+ }
327
+ return upcharge - (selectedOptions[0].upcharge || 0);
328
+ }
329
+ return 0;
330
+ }
331
+ get features() {
332
+ if (this._features === undefined) {
333
+ const allRefs = this.rawOption.featureRefs || [];
334
+ const features = syncCfgFeatures(allRefs, [], this.rawFeatures, this, this.parentConfiguration, this.parentProduct, this.rootProduct);
335
+ if (doesChildrenShareOptionsCode(features)) {
336
+ throw new Error("Stage does not yet properly support Options that has multiple sub-features with overlapping option codes.");
337
+ }
338
+ this._features = features;
339
+ }
340
+ return this._features;
341
+ }
342
+ }
343
+ export class CfgOption {
344
+ /**
345
+ * Private constructor and make-method because make new ref requires the constructor to
346
+ * take an internal and we don't want those who instantiate CfgOption to have to be aware
347
+ * of the internal.
348
+ */
349
+ constructor(_internal) {
350
+ this._internal = _internal;
351
+ this.isBackedBySame = (other) => this._internal === other._internal;
352
+ this.setNumericValue = (val, doSelectOption) => __awaiter(this, void 0, void 0, function* () { return yield this._internal.setNumericValue(val, doSelectOption); });
353
+ this.isAllowedNumericValue = (val) => this._internal.isAllowedNumericValue(val);
354
+ /**
355
+ * Selects this Option.
356
+ * Only Options belonging to Features that are "select many" can be deselected.
357
+ * Calling this will cause a validation call to the server.
358
+ */
359
+ this.setSelected = (on) => __awaiter(this, void 0, void 0, function* () {
360
+ return yield this._internal.parent.selectOption(this._internal, on, ProductConfigurationBubbleMode.ValidateAndBubbleSelectedAndApplySyncGroups);
361
+ });
362
+ this.listenForChange = (l) => this._internal.changeObservable.listen(l);
363
+ this.stopListenForChange = (l) => this._internal.changeObservable.stopListen(l);
364
+ }
365
+ static make(rawOption, rawFeatures, siblingHasDuplicateDescription, parent, parentConfiguration, parentProduct, rootProduct) {
366
+ return new this(new _CfgOptionInternal(rawOption, rawFeatures, siblingHasDuplicateDescription, parent, parentConfiguration, parentProduct, rootProduct));
367
+ }
368
+ /**
369
+ * Makes an object wrapping the passed object. This is not a clone method,
370
+ * it is a method to make a new outer reference. Like a shallow copy.
371
+ * We use this to help frameworks that are build around using equals to detect change.
372
+ */
373
+ static _makeNewRefFrom(internal) {
374
+ return new this(internal);
375
+ }
376
+ get parentProduct() {
377
+ return CfgProduct._makeNewRefFrom(this._internal.parentProduct);
378
+ }
379
+ get parent() {
380
+ return CfgFeature._makeNewRefFrom(this._internal.parent);
381
+ }
382
+ get rootProduct() {
383
+ return CfgProduct._makeNewRefFrom(this._internal.rootProduct);
384
+ }
385
+ get rawOption() {
386
+ return this._internal.rawOption;
387
+ }
388
+ get key() {
389
+ return this._internal.key;
390
+ }
391
+ get code() {
392
+ return this._internal.code;
393
+ }
394
+ get notes() {
395
+ return this._internal.notes;
396
+ }
397
+ get miscFiles() {
398
+ return this._internal.miscFiles;
399
+ }
400
+ get isUseNumericValue() {
401
+ return this._internal.isUseNumericValue;
402
+ }
403
+ get numericValue() {
404
+ return this._internal.numericValue;
405
+ }
406
+ get allowedNumericValues() {
407
+ return this._internal.allowedNumericValues;
408
+ }
409
+ get unit() {
410
+ return this._internal.unit;
411
+ }
412
+ get description() {
413
+ return this._internal.description;
414
+ }
415
+ get selected() {
416
+ return this._internal.selected;
417
+ }
418
+ /**
419
+ * Selection state is in progress to be changed. This can be used in GUI
420
+ * to display the state as transitioning, or as already changed.
421
+ * If selectedChangeInProgress and:
422
+ * selected is true, it means that this is about to get unselected
423
+ * selected is false, it means that this is about to get selected
424
+ */
425
+ get selectedChangeInProgress() {
426
+ return this._internal.selectedChangeInProgress;
427
+ }
428
+ /** Are all ancestors up to the CfgProductConfiguration selected? Includes self. */
429
+ get ancestorsSelected() {
430
+ return this._internal.ancestorsSelected;
431
+ }
432
+ get thumbnail() {
433
+ return this._internal.thumbnail;
434
+ }
435
+ get upcharge() {
436
+ return this._internal.upcharge;
437
+ }
438
+ get priceChangeAtSelectChange() {
439
+ return this._internal.priceChangeAtSelectChange;
440
+ }
441
+ get features() {
442
+ return this._internal.features;
443
+ }
444
+ }