diff options
author | Sébastien Dailly <sebastien@dailly.me> | 2022-02-07 15:38:37 +0100 |
---|---|---|
committer | Sébastien Dailly <sebastien@dailly.me> | 2022-02-07 16:01:12 +0100 |
commit | 77544bdfad2af41514ec1435f706fee87ea2969e (patch) | |
tree | 4de23870e08711da25ff92e9670370fc0a74e459 /lib/elements | |
parent | ad526111f0dd619ae9e0e98ef2253146b58a068f (diff) |
Added viz.js code
Diffstat (limited to 'lib/elements')
-rwxr-xr-x | lib/elements/dune | 9 | ||||
-rwxr-xr-x | lib/elements/input.ml | 23 | ||||
-rwxr-xr-x | lib/elements/prop.ml | 20 | ||||
-rwxr-xr-x | lib/elements/timer.ml | 38 | ||||
-rwxr-xr-x | lib/elements/timer.mli | 11 | ||||
-rwxr-xr-x | lib/elements/transfert.ml | 22 |
6 files changed, 123 insertions, 0 deletions
diff --git a/lib/elements/dune b/lib/elements/dune new file mode 100755 index 0000000..97d0753 --- /dev/null +++ b/lib/elements/dune @@ -0,0 +1,9 @@ +(library + (name elements) + (libraries + brr + brr.note + js_of_ocaml + ) + (preprocess (pps ppx_hash js_of_ocaml-ppx)) +) diff --git a/lib/elements/input.ml b/lib/elements/input.ml new file mode 100755 index 0000000..6ae9aa8 --- /dev/null +++ b/lib/elements/input.ml @@ -0,0 +1,23 @@ +open Brr +open Brr_note +open Note + +(** Create a slider element, and a signal with the value *) +let slider + : at:Brr.At.t list -> Brr.El.t * float S.t + + = fun ~at -> + let slider = + El.input ~at () in + + let init_value = (Jstr.to_float (El.prop El.Prop.value slider)) in + + let event = + Evr.on_el + Ev.input (fun _ -> + let raw_value = El.prop El.Prop.value slider in + Jstr.to_float raw_value) + slider + |> S.hold init_value + in + slider, event diff --git a/lib/elements/prop.ml b/lib/elements/prop.ml new file mode 100755 index 0000000..054864c --- /dev/null +++ b/lib/elements/prop.ml @@ -0,0 +1,20 @@ +open Brr + +include El.Prop + +let offsetWidth + : int t + = El.Prop.int (Jstr.v "offsetWidth") + +let offsetHeight + : int t + = El.Prop.int (Jstr.v "offsetHeight") + +let outerHTML + : Jstr.t t + = El.Prop.jstr (Jstr.v "outerHTML") + + +let value + : Jstr.t t + = El.Prop.jstr (Jstr.v "value") diff --git a/lib/elements/timer.ml b/lib/elements/timer.ml new file mode 100755 index 0000000..28516fc --- /dev/null +++ b/lib/elements/timer.ml @@ -0,0 +1,38 @@ +open Brr_note_kit + +type t = + { mutable id : Brr.G.timer_id + ; send : float Note.E.send + ; mutable counter : Time.counter + } + +let create + : unit -> (t * Brr_note_kit.Time.span Note.E.t) + = fun () -> + let event, send = Note.E.create () + and counter = (Time.counter ()) in + {id = (-1); send; counter}, event + +let stop + : t -> unit + = fun {id; _} -> + Brr.G.stop_timer id + +let start + : t -> float -> unit + = fun t d -> + let {id; send; _} = t in + t.counter <- Time.counter (); + + Brr.G.stop_timer id; + let timer_id = Brr.G.set_interval + ~ms:(int_of_float @@ d *. 1000.) + (fun () -> + + let span = Time.counter_value t.counter in + send span) in + t.id <- timer_id + + +let delay : t -> float + = fun t -> Time.counter_value t.counter diff --git a/lib/elements/timer.mli b/lib/elements/timer.mli new file mode 100755 index 0000000..0509ad0 --- /dev/null +++ b/lib/elements/timer.mli @@ -0,0 +1,11 @@ +open Brr_note_kit + +type t + +val create : unit -> t * Time.span Note.E.t + +val start: t -> float -> unit + +val stop: t -> unit + +val delay : t -> float diff --git a/lib/elements/transfert.ml b/lib/elements/transfert.ml new file mode 100755 index 0000000..ddeecd0 --- /dev/null +++ b/lib/elements/transfert.ml @@ -0,0 +1,22 @@ +open Js_of_ocaml +open Brr + +let send + : mime_type:Jstr.t -> filename:Jstr.t -> Jstr.t -> unit + = fun ~mime_type ~filename content -> + let btoa = Jv.get Jv.global "btoa" in + let base64data = Jv.apply btoa + [| Jv.of_jstr content |] in + + let mime = (Jv.Id.(of_jv @@ to_jv mime_type)) + and base64 = (Jv.Id.(of_jv @@ to_jv base64data)) + in + + let data = (Js.string "data:")##concat_3 mime (Js.string ";base64,") base64 in + + (* Create the link to download the the element, and simulate a click on it *) + let a = El.a + ~at:At.[ href Jv.Id.(of_jv @@ to_jv data) + ; v (Jstr.v "download") filename ] + [] in + El.click a |