@configura/web-api 1.5.0 → 1.6.0-iotest.1

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