blob: ad83591825614657f4b73ac2cc6245f169fc1c84 (
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
import * as number from './number'
import * as dom from './dom'
/* Set up inputs for the ingredients */
const itemEntries =
dom.nodeListToArray(document.querySelectorAll('.g-Recipe__Content ul > li'))
.map(itemNode => ({ tag: 'li', node: itemNode }))
const h1 = document.querySelector<HTMLElement>('.g-Recipe__Content h1')
if (h1 !== null) {
itemEntries.push({ tag: 'h1', node: h1 })
}
const inputs = setupInputs(itemEntries)
inputs.map(input => {
input.node.oninput = e => {
if (e.target !==null) {
const parsed = number.parse((e.target as HTMLInputElement).value)
if (parsed !== undefined && parsed.before === '' && parsed.after === '') {
const factor = parsed.number / input.number
inputs.map(input2 => {
if (input.node !== input2.node) {
input2.node.value = number.prettyPrint(input2.number * factor)
}
})
}
}
}
})
interface InputEntry {
tag: string;
node: HTMLElement;
}
interface InputResult {
number: number,
node: HTMLInputElement
}
function setupInputs(xs: InputEntry[]): InputResult[] {
const res: InputResult[] = []
xs.forEach(x => {
const parsed = number.parse(x.node.innerText)
if (parsed !== undefined) {
const numberNode = number.node(x.tag, parsed)
dom.replace(x.node, numberNode.node)
res.push({
number: parsed.number,
node: numberNode.number
})
}
})
return res
}
/* Set up done marks for steps */
dom.nodeListToArray(document.querySelectorAll('.g-Recipe__Content ol > li')).forEach(todo => {
todo.onclick = e => {
dom.toggleClassName(todo, 'g-Recipe__Completed')
e.stopPropagation()
}
})
|