package ezjs_odometer

  1. Overview
  2. Docs

Source file odometer.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
open Js_of_ocaml

class type  configuration = object
  method auto : bool Js.prop
  method el : Dom_html.divElement Js.t Js.prop
  method value : int Js.prop
  method format : Js.js_string Js.t Js.prop
  method theme : Js.js_string Js.t Js.prop
  method animation : Js.js_string Js.t
end

class type odometer = object
  method update : int -> unit Js.meth
end

(* [auto]: Don't automatically initialize everything with class
   'odometer' duration: Change how long the javascript expects the CSS
   animation to take
   [theme]: Specify the theme (if you have more than one theme css file
   on the page)
   [animation]: Count is a simpler animation method which just
   increments the value, use it when you're looking for something more
   subtle.
   format:
     (,ddd) - 12,345,678
     (,ddd).dd - 12,345,678.09
     (.ddd),dd - 12.345.678,09
     ( ddd),dd - 12 345 678,09
     d - 12345678 *)
type configuraton = {
  auto : bool option ;
  element : Dom_html.divElement Js.t ;
  value : int ;
  format : string option ;
  theme : string option ;
  animation : string option ;
 }

let from_config config =
  let configuration : configuration Js.t =  Js.Unsafe.obj [||] in
  configuration##.el := config.element  ;
  configuration##.value := config.value ;
  begin match config.format with
    | None -> ()
    | Some format -> configuration##.format := Js.string format
  end  ;
  begin match config.auto with
    | None -> ()
    | Some auto -> configuration##.auto := auto
  end ;
  begin match config.theme with
    | None -> ()
    | Some theme -> configuration##.theme := Js.string theme
  end


(* With specific configuration *)
let odometer_with_config config =
  let configuration = from_config config in
  let odometer_ctsr = Js.Unsafe.global##._Odometer in
  (Js.Unsafe.new_obj odometer_ctsr
     [| Js.Unsafe.inject configuration |] : odometer Js.t)

(* With default config *)
let odometer el =
  let configuration : configuration Js.t =  Js.Unsafe.obj [||] in
  configuration##.el := el ;
  configuration##.value := 0 ;
  configuration##.format := Js.string "d" ;
  let odometer_ctsr = Js.Unsafe.global##._Odometer in
  (Js.Unsafe.new_obj odometer_ctsr
      [| Js.Unsafe.inject configuration |] : odometer Js.t)

let update od value =
   od##(update value)