aboutsummaryrefslogtreecommitdiff
path: root/src/domUtils.ml
blob: 282ac129d86aba757970c193893d7d6e3f491d3b (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
open Webapi.Dom

let toggleClassName (element : Dom.element) (className : string) : unit =
  Element.setClassName element
    (if Element.className element == className then "" else className)

type child = TextChild of string | ElemChild of Dom.element

let h (tag : string) (attributes : (string * string) Js.Array.t)
    (children : child Js.Array.t) : Dom.element =
  let element = Document.createElement tag document in
  let () =
    attributes
    |> Js.Array.forEach (fun a -> Element.setAttribute (fst a) (snd a) element)
  in
  let () =
    children
    |> Js.Array.forEach (fun c ->
           match c with
           | TextChild t ->
               Element.appendChild (Document.createTextNode t document) element
           | ElemChild e -> Element.appendChild e element)
  in
  element

external replace_child : Dom.node -> Dom.element -> Dom.element -> unit
  = "replaceChild"
  [@@bs.send]

let replace (element : Dom.element) (replacement : Dom.element) : unit =
  match Element.parentNode element with
  | Some parent -> replace_child parent replacement element
  | _ -> ()

external value : Dom.eventTarget -> string option = "value" [@@bs.get]

external setValue : Dom.element -> string -> unit = "value" [@@bs.set]