tool_js_common.ml 3.28 KB
Newer Older
Stephane Glondu's avatar
Stephane Glondu committed
1 2 3
(**************************************************************************)
(*                                BELENIOS                                *)
(*                                                                        *)
Stephane Glondu's avatar
Stephane Glondu committed
4
(*  Copyright © 2012-2018 Inria                                           *)
Stephane Glondu's avatar
Stephane Glondu committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
(*                                                                        *)
(*  This program is free software: you can redistribute it and/or modify  *)
(*  it under the terms of the GNU Affero General Public License as        *)
(*  published by the Free Software Foundation, either version 3 of the    *)
(*  License, or (at your option) any later version, with the additional   *)
(*  exemption that compiling, linking, and/or using OpenSSL is allowed.   *)
(*                                                                        *)
(*  This program is distributed in the hope that it will be useful, but   *)
(*  WITHOUT ANY WARRANTY; without even the implied warranty of            *)
(*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *)
(*  Affero General Public License for more details.                       *)
(*                                                                        *)
(*  You should have received a copy of the GNU Affero General Public      *)
(*  License along with this program.  If not, see                         *)
(*  <http://www.gnu.org/licenses/>.                                       *)
(**************************************************************************)

22
open Js_of_ocaml
Stephane Glondu's avatar
Stephane Glondu committed
23
open Common
24

Stephane Glondu's avatar
Stephane Glondu committed
25
let document = Dom_html.document
Stephane Glondu's avatar
Stephane Glondu committed
26

Stephane Glondu's avatar
Stephane Glondu committed
27 28
let alert s =
  Dom_html.window##alert (Js.string s)
Stephane Glondu's avatar
Stephane Glondu committed
29

Stephane Glondu's avatar
Stephane Glondu committed
30
let prompt s =
Stephane Glondu's avatar
Stephane Glondu committed
31 32
  let x = Dom_html.window##prompt (Js.string s) (Js.string "") in
  Js.Opt.to_option (Js.Opt.map x Js.to_string)
Stephane Glondu's avatar
Stephane Glondu committed
33

34
let with_element x f =
Stephane Glondu's avatar
Stephane Glondu committed
35
  Option.iter f (Dom_html.getElementById_opt x)
36

37
let get_textarea_opt id =
Stephane Glondu's avatar
Stephane Glondu committed
38 39
  Option.map (fun x -> Js.to_string x##.value)
    (Dom_html.getElementById_coerce id Dom_html.CoerceTo.textarea)
40 41 42

let get_textarea id =
  match get_textarea_opt id with
Stephane Glondu's avatar
Stephane Glondu committed
43
  | Some x -> x
44
  | None -> Printf.ksprintf failwith "<textarea> %s is missing" id
Stephane Glondu's avatar
Stephane Glondu committed
45 46

let set_textarea id z =
Stephane Glondu's avatar
Stephane Glondu committed
47 48
  Option.iter (fun x -> x##.value := Js.string z)
    (Dom_html.getElementById_coerce id Dom_html.CoerceTo.textarea)
49

50
let get_input_opt id =
Stephane Glondu's avatar
Stephane Glondu committed
51 52
  Option.map (fun x -> Js.to_string x##.value)
    (Dom_html.getElementById_coerce id Dom_html.CoerceTo.input)
53 54 55

let get_input id =
  match get_input_opt id with
56
  | Some x -> x
57
  | None -> Printf.ksprintf failwith "<input> %s is missing" id
58

59
let set_element_display id x =
60
  with_element id (fun e -> e##.style##.display := Js.string x)
61 62 63

let set_download id mime fn x =
  let x = (Js.string ("data:" ^ mime ^ ","))##concat (Js.encodeURI (Js.string x)) in
Stephane Glondu's avatar
Stephane Glondu committed
64 65 66 67 68
  match Dom_html.getElementById_coerce id Dom_html.CoerceTo.a with
  | None -> ()
  | Some e ->
     e##setAttribute (Js.string "download") (Js.string fn);
     e##.href := x
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87

let get_content x =
  let r = ref x in
  with_element x (fun x ->
    Js.Opt.iter (x##.textContent) (fun x -> r := Js.to_string x)
  ); !r

let set_content id x =
  with_element id (fun e ->
    let t = document##createTextNode (Js.string x) in
    Dom.appendChild e t
  )

let run_handler handler () =
  (try handler ()
   with e ->
     let msg = "Unexpected error: " ^ Printexc.to_string e in
     alert msg
  ); Js._false