package fmlib_js

  1. Overview
  2. Docs

Source file dom.ml

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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
open Js_of_ocaml
open Base

type js_string = Js.js_string Js.t



class type style =
object
    method setProperty:    js_string -> js_string -> unit Js.meth
    method removeProperty: js_string -> unit Js.meth
end




class type node =
object
    method parentNode:  node Js.t Js.Opt.t Js.readonly_prop
    method firstChild:  node Js.t Js.Opt.t Js.readonly_prop
    method nextSibling: node Js.t Js.Opt.t Js.readonly_prop

    method appendChild:  node Js.t -> unit Js.meth
    method removeChild:  node Js.t -> unit Js.meth
    method replaceChild: node Js.t -> node Js.t -> unit Js.meth
    method nodeValue:    js_string Js.prop
end







class type element =
object
    method scrollWidth:     int Js.readonly_prop
    method scrollHeight:    int Js.readonly_prop
    method clientWidth:     int Js.readonly_prop
    method clientHeight:    int Js.readonly_prop
    method scrollLeft:      int Js.prop
    method scrollTop:       int Js.prop

    method style:           style Js.t Js.readonly_prop
    method setAttribute:    js_string -> js_string -> unit Js.meth
    method removeAttribute: js_string -> unit Js.meth

    method focus: unit -> unit Js.meth
    method blur:  unit -> unit Js.meth
end






class type document =
object
    method title:          js_string Js.prop
    method body:           element Js.t Js.readonly_prop
    method getElementById: js_string -> element Js.t Js.Opt.t Js.meth
    method createTextNode: js_string -> node Js.t Js.meth
    method createElement:  js_string -> element Js.t Js.meth
    method createElementNS:  js_string -> js_string -> element Js.t Js.meth
    method createDocumentFragment:  unit -> node Js.t Js.meth
end





class type history =
object
    method go: int -> unit Js.meth
    method pushState:    Base.Value.t -> js_string -> js_string -> unit Js.meth
    method replaceState: Base.Value.t -> js_string -> js_string -> unit Js.meth
end




class type location =
object
    method href:        js_string Js.readonly_prop
    method protocol:    js_string Js.readonly_prop
    method host:        js_string Js.readonly_prop
    method port:        js_string Js.readonly_prop
    method pathname:    js_string Js.readonly_prop
    method search:      js_string Js.readonly_prop
    method hash:        js_string Js.readonly_prop

    method assign: js_string -> unit Js.meth
    method reload: unit -> unit Js.meth
end





class type window =
object
    method history:  history Js.t Js.readonly_prop
    method location: location Js.t Js.readonly_prop
    method document: document Js.t Js.readonly_prop
    method requestAnimationFrame: (float -> unit) -> unit Js.meth
end






module Style =
struct
    type t = style Js.t

    let set (name: string) (value: string) (s: t): unit =
        s##setProperty (Js.string name) (Js.string value)

    let remove (name: string) (s: t): unit =
        s##removeProperty (Js.string name)
end




module Node =
struct
    type t = node Js.t

    let event_target (node: t): Event_target.t =
        Obj.magic node

    let parent (node: t): t option =
        let open Js in
        Opt.to_option node##.parentNode

    let first (node: t): t option =
        let open Js in
        Opt.to_option node##.firstChild

    let next (node: t): t option =
        let open Js in
        Opt.to_option node##.nextSibling

    let append (child: t) (node: t): unit =
        assert (not (node == Obj.magic Value.null));
        node##appendChild child

    let remove (child: t) (node: t): unit =
        node##removeChild child

    let replace (new_child: t) (old_child: t) (node: t): unit =
        node##replaceChild new_child old_child

    let rec remove_children (parent: t): unit =
        match first parent with
        | None ->
            ()
        | Some child ->
            remove child parent;
            remove_children parent (* tail recursion, compiled to javascript
                                      loop. *)

    let node_value (node: t): string =
        Js.to_string node##.nodeValue

    let set_node_value (value: string) (node: t): unit =
        node##.nodeValue := (Js.string value)
end







module Element =
struct
    type t = element Js.t

    let node (element: t): Node.t =
        Js.Unsafe.coerce element

    let scroll_width (element: t): int =
        element##.scrollWidth

    let scroll_height (element: t): int =
        element##.scrollHeight

    let client_width (element: t): int =
        element##.clientWidth

    let client_height (element: t): int =
        element##.clientHeight

    let scroll_left (element: t): int =
        element##.scrollLeft

    let scroll_top (element: t): int =
        element##.scrollTop


    let set_scroll_left (v: int) (element: t): unit =
        element##.scrollLeft := v

    let set_scroll_top (v: int) (element: t): unit =
        element##.scrollTop := v




    let style (element: t): Style.t =
        element##.style

    let set_attribute (name: string) (value: string) (element: t): unit =
        element##setAttribute (Js.string name) (Js.string value)

    let remove_attribute (name: string) (element: t): unit =
        element##removeAttribute (Js.string name)


    let set_property (name: string) (value: Value.t) (element: t): unit =
        Js.Unsafe.set element (Js.string name) value

    let delete_property (name: string) (element: t): unit =
        Js.Unsafe.delete element (Js.string name)


    let focus (element: t): unit =
        element##focus ()

    let blur (element: t): unit =
        element##blur ()
end







module Document =
struct
    type t = document Js.t


    let title (doc: t): string =
        Js.to_string doc##.title


    let set_title (title: string) (doc: t): unit =
        doc##.title := Js.string title


    let body (doc: t): Element.t =
        assert (doc##.body != Js.Unsafe.js_expr "null");
        doc##.body

    let find (name: string) (doc: t): Element.t option =
        Js.Opt.to_option (doc##getElementById (Js.string name))

    let create_element (tag: string) (doc: t): Element.t =
        doc##createElement (Js.string tag)

    let create_text_node (text: string) (doc: t): Node.t =
        doc##createTextNode (Js.string text)

    let create_element_ns (namespace: string) (tag: string) (doc: t): Element.t =
        doc##createElementNS (Js.string namespace) (Js.string tag)

    let create_document_fragment (doc: t): Node.t =
        doc##createDocumentFragment ()
end



module History =
struct
    type t = history Js.t


    let go (i: int) (history: t): unit =
        history##go i


    let push_state
            (state: Value.t)
            (title: string)
            (url: string)
            (history: t)
        : unit =
        let open Js in
        history##pushState state (string title) (string url)


    let replace_state
            (state: Value.t)
            (title: string)
            (url: string)
            (history: t)
        : unit =
        let open Js in
        history##replaceState state (string title) (string url)
end





module Location =
struct
    type t = location Js.t

    let href (location: t): string =
        Js.to_string location##.href

    let protocol (location: t): string =
        Js.to_string location##.protocol

    let host (location: t): string =
        Js.to_string location##.host

    let port (location: t): string =
        Js.to_string location##.port

    let pathname (location: t): string =
        Js.to_string location##.pathname

    let search (location: t): string =
        Js.to_string location##.search

    let hash (location: t): string =
        Js.to_string location##.hash

    let assign (url: string) (location: t): unit =
        location##assign (Js.string url)


    let reload (location: t): unit =
        location##reload ()
end




module Window =
struct
    type t = window Js.t


    let get (): t =
        Js.Unsafe.global

    let event_target (w: t): Event_target.t =
        Obj.magic w


    let document (w: t): Document.t =
        w##.document


    let history (w: t): History.t =
        w##.history


    let location (w: t): Location.t =
        w##.location


    let on_next_animation (callback: float -> unit) (w: t): unit =
        w##requestAnimationFrame callback
end
OCaml

Innovation. Community. Security.