-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebgl_plot_dom_helper.ml
46 lines (37 loc) · 1.53 KB
/
webgl_plot_dom_helper.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
(* This file is released under the terms of an MIT-like license. *)
(* See the attached LICENSE file. *)
(* Copyright 2016 by LexiFi. *)
open Js_browser
let error s =
print_endline ("Error: "^s);
failwith s
let document = Window.document window
let removeAll element =
while Element.has_child_nodes element do
Element.remove_child element (Element.last_child element)
done
let element_of_id id =
match Document.get_element_by_id document id with
| Some element -> element
| None -> error (Printf.sprintf "Element of id '%s' not found" id)
let hide element =
Element.set_attribute element "style" "display: none"
let show element =
Element.remove_attribute element "style"
let configure_element ?text ?class_name ?style ?(attributes = []) element =
(match text with
| Some text -> Element.set_text_content element text
| _ -> ());
(match style with
| Some style -> Element.set_attribute element "style" style
| _ -> ());
(match class_name with
| Some class_name -> Element.set_class_name element class_name
| _ -> ());
List.iter (fun (name, value) -> Element.set_attribute element name value) attributes
let create ?text ?class_name ?style ?attributes ?parent name children =
let element = Document.create_element document name in
configure_element ?text ?class_name ?style ?attributes element;
List.iter (Element.append_child element) children;
(match parent with Some p -> Element.append_child p element | _ -> ());
element