This repository has been archived by the owner on Nov 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 73
/
dom_example.re
98 lines (81 loc) · 2.55 KB
/
dom_example.re
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
95
96
97
98
open Webapi.Dom;
/* Adapted from https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Examples#Example_7:_Displaying_Event_Object_Properties */
/* Option.map */
let map = (f) =>
fun
| Some(v) => Some(f(v))
| None => None;
let andThen = (f: 'a => option('b)) =>
fun
| Some(v) => f(v)
| None => None;
let unwrapUnsafely =
fun
| Some(v) => v
| None => raise(Invalid_argument("Passed `None` to unwrapUnsafely"));
/*
* These SHOULD type check
*/
document |> Document.createElement("div")
|> Element.className;
document |> Document.createElement("div")
|> Element.nextElementSibling
|> map(Element.innerText); /* innerText is a function that accepts a Node */
/* Before subtyping:
document |> Document.createElement("div")
|> Element.asNode
|> Node.parentElement /* inherited from Node, returns DomRe.element */
|> map(Element.innerText); /* inherited from Node */
*/
/* After subtyping: */
document |> Document.createElement("div")
|> Element.parentElement /* inherited from Node, returns DomRe.element */
|> map(Element.innerText); /* inherited from Node */
let el =
document |> Document.createElement("div")
|> Element.asHtmlElement
|> unwrapUnsafely;
/*
document |> Document.asHtmlDocument
|> andThen(HtmlDocument.body)
|> map(Element.appendChild(el));
*/
/* Before subtyping:
document |> Document.asHtmlDocument
|> andThen(HtmlDocument.body)
|> map(Element.appendChild (el |> HtmlElement.asNode));
*/
/* After subtyping: */
document |> Document.asHtmlDocument
|> andThen(HtmlDocument.body)
|> map(Element.appendChild(el));
/*
/*
* These MAY fail type check
*/
document |> Document.createElement("div")
|> Element.nextElementSibling
|> map(Node.innerText);
/*
* These SHOULD NOT type check
*/
document |> Document.createElement("div")
|> Element.asNode
|> Element.parentElement; /* inherited from Node, returns DomRe.element */
*/
/*
/* ideal, but requires piped setters */
switch (document |> body) {
| Some body =>
document |> createElement("div")
|> setInnerText("</>")
|> setClassName("reason_tools_button")
|> setOnClick(swap)
|> (body |> appendChild);
| None =>
...
}
*/
/* non-standard event-specific listener API - log screen coordinates of mouse cursor when moved */
document |> Document.createElement("div")
|> Element.addMouseMoveEventListener(e => (MouseEvent.screenX(e), MouseEvent.screenY(e)) |> Js.log);