blob: 68deeeea9a357d6a3c8f30640df9713d9348ba2d (
plain)
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
|
window.onload = function() {
// Update ingredients amounts
let inputs = []
document.querySelectorAll('.number').forEach(function (number) {
// Install input
const value = parseInt(number.innerHTML)
number.innerHTML = `<input value="${value}">`
// Push to inputs
const element = number.children[0]
inputs.push({ element, value })
element.addEventListener('input', function() {
// Parse modified input value
const n = parseFloat(element.value.replace(',', '.')) || 0
if (!isNaN(n)) {
// Find current factor
const currentInput = inputs.find(function (input) {
return input.element === element
})
const factor = n / currentInput.value
// Apply factor to other inputs
inputs.forEach(function (input) {
if (input.element !== currentInput.element) {
input.element.value = formatNumber(factor * input.value)
}
})
}
})
})
// Set up done marks for steps
document.querySelectorAll('ol > li').forEach(function (item) {
item.addEventListener('click', function() {
item.className = item.className ? '' : 'completed'
})
})
}
function formatNumber(n) {
const xs = n.toString().split('.')
if (xs.length == 2) {
return `${xs[0]}.${xs[1].slice(0, 1)}`
} else {
return n
}
}
|