blob: f8e20f93fb314a0bc3e393c18f70b010d7d14090 (
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
(* Model *)
type config = {
prepare : int;
tabatas : int;
cycles : int;
work : int;
rest : int;
}
(* State *)
let config = ref { prepare = 10; tabatas = 4; cycles = 8; work = 20; rest = 10 }
let onStart : (unit -> unit) ref = ref (fun () -> ())
(* Elements *)
let formElt = Document.querySelectorUnsafe "#g-Form"
let prepareElt = Document.querySelectorUnsafe "#g-Form__Prepare"
let tabatasElt = Document.querySelectorUnsafe "#g-Form__Tabatas"
let cyclesElt = Document.querySelectorUnsafe "#g-Form__Cycles"
let workElt = Document.querySelectorUnsafe "#g-Form__Work"
let restElt = Document.querySelectorUnsafe "#g-Form__Rest"
let durationElt = Document.querySelectorUnsafe "#g-Form__DurationValue"
(* Duration *)
let getDuration () =
let { prepare; tabatas; cycles; work; rest } = !config in
tabatas * (prepare + (cycles * (work + rest)))
let writeDuration () =
let duration = getDuration () in
Element.setInnerText durationElt (Duration.prettyPrint duration)
(* Write to DOM *)
let writeToDom () =
let () = Element.setValue prepareElt (Js.Int.toString !config.prepare) in
let () = Element.setValue tabatasElt (Js.Int.toString !config.tabatas) in
let () = Element.setValue cyclesElt (Js.Int.toString !config.cycles) in
let () = Element.setValue workElt (Js.Int.toString !config.work) in
let () = Element.setValue restElt (Js.Int.toString !config.rest) in
writeDuration ()
(* Update from DOM *)
let listenTo inputElt update =
Element.addEventListener inputElt "input" (fun e ->
match
EventTarget.value (Event.target e) |> Option.flatMap Belt.Int.fromString
with
| Some n ->
let () = config := update !config n in
writeDuration ()
| None -> ())
let listenToChanges () =
let () = listenTo prepareElt (fun config n -> { config with prepare = n }) in
let () = listenTo tabatasElt (fun config n -> { config with tabatas = n }) in
let () = listenTo cyclesElt (fun config n -> { config with cycles = n }) in
let () = listenTo workElt (fun config n -> { config with work = n }) in
listenTo restElt (fun config n -> { config with rest = n })
(* Setup *)
let setup onTimerStart =
let () = onStart := onTimerStart in
let () = writeToDom () in
listenToChanges ()
(* Start *)
let startTimer () =
let () = Element.setStyle formElt "display: none" in
!onStart ()
(* Hide / show *)
let show () = Element.setStyle formElt "display: flex"
let hide () = Element.setStyle formElt "display: none"
let () =
Element.addEventListener formElt "submit" (fun e ->
let () = Event.preventDefault e in
!onStart ())
|