Commit c17ca235 authored by Stephane Glondu's avatar Stephane Glondu

Simplify Tool_js_common

parent c286a8bd
...@@ -162,6 +162,10 @@ module List = struct ...@@ -162,6 +162,10 @@ module List = struct
end end
module Option = struct module Option = struct
let iter f = function
| None -> ()
| Some x -> f x
let get x default_value = match x with let get x default_value = match x with
| None -> default_value | None -> default_value
| Some x -> x | Some x -> x
......
...@@ -55,6 +55,7 @@ module List : sig ...@@ -55,6 +55,7 @@ module List : sig
end end
module Option : sig module Option : sig
val iter : ('a -> unit) -> 'a option -> unit
val get : 'a option -> 'a -> 'a val get : 'a option -> 'a -> 'a
val map : ('a -> 'b) -> 'a option -> 'b option val map : ('a -> 'b) -> 'a option -> 'b option
end end
......
...@@ -20,31 +20,23 @@ ...@@ -20,31 +20,23 @@
(**************************************************************************) (**************************************************************************)
open Js_of_ocaml open Js_of_ocaml
open Common
let document = Dom_html.window##.document let document = Dom_html.document
let alert s : unit = let alert s =
let open Js.Unsafe in Dom_html.window##alert (Js.string s)
fun_call (variable "alert") [| s |> Js.string |> inject |]
let prompt s = let prompt s =
let open Js.Unsafe in let x = Dom_html.window##prompt (Js.string s) (Js.string "") in
Js.Opt.map Js.Opt.to_option (Js.Opt.map x Js.to_string)
(fun_call (variable "prompt") [| s |> Js.string |> inject |])
Js.to_string |> Js.Opt.to_option
let with_element x f = let with_element x f =
Js.Opt.iter (document##getElementById (Js.string x)) f Option.iter f (Dom_html.getElementById_opt x)
let get_textarea_opt id = let get_textarea_opt id =
let res = ref None in Option.map (fun x -> Js.to_string x##.value)
with_element id (Dom_html.getElementById_coerce id Dom_html.CoerceTo.textarea)
(fun e ->
Js.Opt.iter
(Dom_html.CoerceTo.textarea e)
(fun x -> res := Some (Js.to_string (x##.value)))
);
!res
let get_textarea id = let get_textarea id =
match get_textarea_opt id with match get_textarea_opt id with
...@@ -52,22 +44,12 @@ let get_textarea id = ...@@ -52,22 +44,12 @@ let get_textarea id =
| None -> Printf.ksprintf failwith "<textarea> %s is missing" id | None -> Printf.ksprintf failwith "<textarea> %s is missing" id
let set_textarea id z = let set_textarea id z =
with_element id Option.iter (fun x -> x##.value := Js.string z)
(fun e -> (Dom_html.getElementById_coerce id Dom_html.CoerceTo.textarea)
Js.Opt.iter
(Dom_html.CoerceTo.textarea e)
(fun x -> x##.value := Js.string z)
)
let get_input_opt id = let get_input_opt id =
let res = ref None in Option.map (fun x -> Js.to_string x##.value)
with_element id (Dom_html.getElementById_coerce id Dom_html.CoerceTo.input)
(fun e ->
Js.Opt.iter
(Dom_html.CoerceTo.input e)
(fun x -> res := Some (Js.to_string (x##.value)))
);
!res
let get_input id = let get_input id =
match get_input_opt id with match get_input_opt id with
...@@ -79,11 +61,11 @@ let set_element_display id x = ...@@ -79,11 +61,11 @@ let set_element_display id x =
let set_download id mime fn x = let set_download id mime fn x =
let x = (Js.string ("data:" ^ mime ^ ","))##concat (Js.encodeURI (Js.string x)) in let x = (Js.string ("data:" ^ mime ^ ","))##concat (Js.encodeURI (Js.string x)) in
with_element id match Dom_html.getElementById_coerce id Dom_html.CoerceTo.a with
(fun e -> | None -> ()
e##setAttribute (Js.string "download") (Js.string fn); | Some e ->
Js.Opt.iter (Dom_html.CoerceTo.a e) (fun e -> e##.href := x) e##setAttribute (Js.string "download") (Js.string fn);
) e##.href := x
let get_content x = let get_content x =
let r = ref x in let r = ref x in
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment