Source

xJs / lib / base.ml

Full commit
open Js

module U = Unsafe

type int_t      = int t
type float_t    = float t
type string_t   = js_string t
type 'a array_t = 'a js_array t

let (!$) = Js.string

external int : int -> int t = "%identity"

let alert s = ignore (U.fun_call (U.variable "alert") [| U.inject (string s) |])
let alertf fmt  = Printf.ksprintf alert fmt

let load_script url =
  let document = Dom_html.window##document in
  let script = document##createElement (!$ "script") in
  script##setAttribute (!$ "type", !$ "text/javascript");
  script##setAttribute (!$ "src", !$ url);
  Dom.appendChild document##body script

let set_onload f = 
  Dom_html.window##onload <- Dom.handler (fun _ -> f (); _false)

let getElementById id =
  Opt.to_option (Dom_html.window##document##getElementById (!$ id))

open Spotlib_js.Spot

module OptionFields = struct
  module Make(A : sig
    type key
    type value
  end) = struct
    open A
    type kvs = (key * value) list
    type options = kvs -> kvs
    let empty = id
    let (++) = ( ** )
    let make k f v0 kvs = (k, f v0) :: kvs
    let run opts = opts []
  end
end

module StringOptionFields = 
  OptionFields.Make(struct type key = string type value = string end)