blob: a2174f2e4661407cae56d10f3be55b9534c589e1 (
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
|
open Webapi.Dom
(* Set up inputs for the ingredients *)
type ingredient = { quantity : float; element : Dom.element }
let ingredients : ingredient Js.Array.t =
document
|> Document.querySelectorAll ".g-Recipe__Content ul > li"
|> NodeList.toArray
|> ArrayUtils.flatMap (fun node ->
Belt.Option.map (Element.ofNode node) (fun e -> ("li", e)))
|> Js.Array.concat
( match Document.querySelector ".g-Recipe__Content h1" document with
| Some element -> [| ("h1", element) |]
| _ -> [||] )
|> ArrayUtils.flatMap (fun (tag, element) ->
Belt.Option.map
(Number.parseInsideText (Element.innerHTML element))
(fun parsed ->
let created = Number.createElement tag parsed in
let () = DomUtils.replace element created.element in
{ quantity = parsed.number; element = created.numberInput }))
(* Update ingredients amounts *)
let () =
ingredients
|> Js.Array.forEach (fun ingredient ->
Element.addEventListener "input"
(fun e ->
Belt.Option.forEach
(DomUtils.value (Event.target e))
(fun numberStr ->
Belt.Option.forEach (Number.parse numberStr) (fun parsed ->
let factor = parsed.number /. ingredient.quantity in
ingredients
|> Js.Array.forEach (fun otherIngredient ->
if ingredient.element != otherIngredient.element
then
DomUtils.setValue otherIngredient.element
(Number.prettyPrint
(factor *. otherIngredient.quantity))
else ()))))
ingredient.element)
(* Set up done marks for steps *)
let () =
document
|> Document.querySelectorAll ".g-Recipe__Content ol > li"
|> NodeList.toArray
|> Js.Array.forEach (fun node ->
match Element.ofNode node with
| Some element ->
Element.addEventListener "click"
(fun e ->
let () =
DomUtils.toggleClassName element "g-Recipe__Completed"
in
Event.stopPropagation e)
element
| _ -> ())
|