blob: 184d26da2b61cafb43ef30d43bcbfc6850c44c53 (
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 => ({ name: 'li', node: itemNode }))
const h1 = document.querySelector<HTMLElement>('.g-Recipe__Content h1')
if (h1 !== null) {
itemEntries.push({ name: '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) {
const factor = parsed.number / input.number
inputs.map(input2 => {
if (input.node !== input2.node) {
input2.node.value = number.prettyPrint(input2.number * factor)
}
})
}
}
}
})
interface InputTag {
name: string;
node: HTMLElement;
}
interface InputResult {
number: number,
node: HTMLInputElement
}
function setupInputs(tags: InputTag[]): InputResult[] {
const res: InputResult[] = []
tags.forEach(tag => {
const parsed = number.parseInsideText(tag.node.innerText)
if (parsed !== undefined) {
const numberNode = number.node(tag.name, parsed)
dom.replace(tag.node, numberNode.node)
res.push({
number: parsed.number,
node: numberNode.numberInput
})
}
})
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()
}
})
|