Skip to content

Commit 10a82ef

Browse files
committed
Dispatch event when a variant is resolved on a given option
1 parent b2ca096 commit 10a82ef

File tree

3 files changed

+53
-8
lines changed

3 files changed

+53
-8
lines changed

src/Resources/public/js/events.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,19 @@ export class OptionSelectedEvent extends Event {
1313
this.option = option;
1414
}
1515
}
16+
17+
export class VariantResolvedEvent extends Event {
18+
static name = 'ssrn:variant-resolved';
19+
20+
/**
21+
* @param {Option} option
22+
*/
23+
constructor(option) {
24+
super(VariantResolvedEvent.name, {
25+
bubbles: true,
26+
cancelable: true
27+
});
28+
29+
this.option = option;
30+
}
31+
}

src/Resources/public/js/option.js

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import {OptionSelectedEvent} from "./events.js";
1+
import {OptionSelectedEvent, VariantResolvedEvent} from "./events.js";
2+
3+
/**
4+
* @typedef {import('./types.js').Product} Product
5+
*/
26

37
export default class Option {
48
/**
@@ -32,27 +36,38 @@ export default class Option {
3236
return this.#element.querySelector('.ssrn-option-label').textContent;
3337
}
3438

39+
/**
40+
* @param {string} label
41+
*/
42+
setLabel(label) {
43+
this.#element.querySelector('.ssrn-option-label').textContent = label;
44+
}
45+
3546
getValue() {
3647
return this.#element.dataset.value;
3748
}
3849

50+
isSelected() {
51+
return this.#element.classList.contains('selected');
52+
}
53+
3954
/**
4055
* @param {boolean} selected
4156
*/
4257
setSelected(selected = true) {
4358
this.#element.classList.toggle('selected', selected);
4459
}
4560

46-
isSelected() {
47-
return this.#element.classList.contains('selected');
61+
isInStock() {
62+
return !this.#element.classList.contains('out-of-stock');
4863
}
4964

5065
setInStock(inStock = true) {
5166
this.#element.classList.toggle('out-of-stock', !inStock);
5267
}
5368

54-
isInStock() {
55-
return !this.#element.classList.contains('out-of-stock');
69+
isAvailable() {
70+
return !this.#element.classList.contains('unavailable');
5671
}
5772

5873
setAvailable(available = true) {
@@ -63,7 +78,21 @@ export default class Option {
6378
}
6479
}
6580

66-
isAvailable() {
67-
return !this.#element.classList.contains('unavailable');
81+
getVariant() {
82+
if('variant' in this.#element.dataset) {
83+
return this.#element.dataset.variant;
84+
}
85+
86+
return null;
87+
}
88+
89+
/**
90+
* @param {Product} variant
91+
*/
92+
setVariant(variant) {
93+
this.setInStock(variant.inStock)
94+
this.#element.dataset.variant = variant.code;
95+
96+
this.#element.dispatchEvent(new VariantResolvedEvent(this));
6897
}
6998
}

src/Resources/public/js/select-manager.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export default class SelectManager {
8787
const concreteOptionCombination = optionCombination.concat(option.getValue());
8888
const variant = this.#repository.getVariantFromOptionCombination(concreteOptionCombination);
8989
if(null !== variant) {
90-
option.setInStock(variant.inStock);
90+
option.setVariant(variant);
9191
}
9292
option.setAvailable(this.#repository.hasOptionCombination(concreteOptionCombination));
9393
});

0 commit comments

Comments
 (0)