-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
111 lines (89 loc) · 3.02 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
function Store(initialState = {}, callback) {
this.state = initialState;
this.mergeState = function mergeState(partialState) {
Object.assign(this.state, partialState);
callback(this);
};
}
function withinRange(value, range) {
if (value >= range.max) return range.max
if (value <= range.min) return range.min
return value;
}
/* SelectorComponent implements the Adapter Pattern
* against the DOM element(s) that make up a Quantity Selector.
*
* The purpose of this adapter (or component) is to provide a
* contract for interacting with a part of the system that we
* don't own and is awkward to test and control. The contract
* is the external methods and event handlers that it exposes:
* id(), range(), value(), onChange, onIncrement, onDecrement.
*
* These contracts can then be implemented by anyone, including
* test stubs that allow us to produce the behavior we want.
* */
function SelectorComponent(el) {
this.el = el;
const qty = this.el.querySelector('.qty');
const inc = this.el.querySelector('.inc');
const dec = this.el.querySelector('.dec');
qty.addEventListener('change', () => {
if (this.onChange) this.onChange();
});
inc.addEventListener('click', () => {
if (this.onIncrement) this.onIncrement();
})
dec.addEventListener('click', () => {
if (this.onDecrement) this.onDecrement();
});
this.id = function() {
return el.dataset.id;
}
this.range = function() {
const qty = this.el.querySelector('.qty');
return { min: qty.min, max: qty.max };
}
this.value = function() {
const qty = this.el.querySelector('.qty');
return parseInt(qty.value, 10);
}
this.setQuantity = function(newQuantity) {
const qty = this.el.querySelector('.qty');
qty.value = newQuantity;
}
}
function syncAllSelectors(store, selectorComponents) {
selectorComponents.forEach((selectorComponent) => {
/* Need to know the id of the quantity selector */
const id = selectorComponent.id();
/* Need to set the quantity value it should be at */
selectorComponent.setQuantity(store.state[id]);
});
}
function init(selectorComponents, store) {
selectorComponents.forEach((selectorComponent) => {
const id = selectorComponent.id();
/* Need to know the quantity selector range */
const range = selectorComponent.range();
/* Need to know when the quantity selector is changed */
selectorComponent.onChange = () => {
store.mergeState({ [id]: withinRange(selectorComponent.value(), range) });
}
/* Need to know when the quantity selector is incremented */
selectorComponent.onIncrement = () => {
store.mergeState({ [id]: withinRange(selectorComponent.value()+ 1, range) });
};
/* Need to know when the quantity selector is decremented */
selectorComponent.onDecrement = () => {
store.mergeState({ [id]: withinRange(selectorComponent.value()- 1, range) });
};
});
syncAllSelectors(store, selectorComponents);
}
module.exports = {
Store,
withinRange,
SelectorComponent,
syncAllSelectors,
init
}