package leaflet

  1. Overview
  2. Docs

Source file popup.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
(* BSD-2-Clause License *)

type t = Jv.t

let set_latlng latlng popup =
  let (_ : Jv.t) = Jv.call popup "setLatLng" [| Latlng.to_jv latlng |] in
  ()

let set_content content popup =
  let (_ : Jv.t) = Jv.call popup "setContent" [| Jv.of_string content |] in
  ()

let set_content_to_el el popup =
  let (_ : Jv.t) = Jv.call popup "setContent" [| Brr.El.to_jv el |] in
  ()

let of_jv = Fun.id

let to_jv = Fun.id

type opt =
  | Pane of string
  | Offset of Point.t
  | Max_width of int
  | Min_width of int
  | Max_height of int
  | Auto_pan of bool
  | Auto_pan_padding_top_left of Point.t
  | Auto_pan_padding_bottom_right of Point.t
  | Auto_pan_padding of Point.t
  | Keep_in_view of bool
  | Close_button of bool
  | Auto_close of bool
  | Close_on_escape_key of bool
  | Close_on_click of bool
  | Class_name of string

let opt_to_string = function
  | Pane _ -> "pane"
  | Offset _ -> "offset"
  | Max_width _ -> "maxWidth"
  | Min_width _ -> "minWidth"
  | Max_height _ -> "maxHeight"
  | Auto_pan _ -> "autoPan"
  | Auto_pan_padding_top_left _ -> "autoPanPaddingTopLeft"
  | Auto_pan_padding_bottom_right _ -> "autoPanPaddingBottomRight"
  | Auto_pan_padding _ -> "autoPanPadding"
  | Keep_in_view _ -> "keepInView"
  | Close_button _ -> "closeButton"
  | Auto_close _ -> "autoClose"
  | Close_on_escape_key _ -> "closeOnEscapeKey"
  | Close_on_click _ -> "closeOnClick"
  | Class_name _ -> "className"

let opt_to_jv = function
  | Offset p
  | Auto_pan_padding_top_left p
  | Auto_pan_padding_bottom_right p
  | Auto_pan_padding p ->
    Point.to_jv p
  | Max_width i | Min_width i | Max_height i -> Jv.of_int i
  | Auto_pan b
  | Keep_in_view b
  | Close_button b
  | Auto_close b
  | Close_on_escape_key b
  | Close_on_click b ->
    Jv.of_bool b
  | Pane s | Class_name s -> Jv.of_string s

let create ~content ~latlng options =
  let l = Array.map (fun o -> (opt_to_string o, opt_to_jv o)) options in
  let popup = Jv.call Global.leaflet "popup" [| Jv.obj l |] in
  let popup =
    match latlng with
    | None -> popup
    | Some latlng ->
      set_latlng latlng popup;
      popup
  in
  match content with
  | None -> popup
  | Some content ->
    set_content content popup;
    popup

let create_from_el el ~latlng options =
  let l = Array.map (fun o -> (opt_to_string o, opt_to_jv o)) options in
  let popup = Jv.call Global.leaflet "popup" [| Jv.obj l |] in
  let popup =
    match latlng with
    | None -> popup
    | Some latlng ->
      set_latlng latlng popup;
      popup
  in
  set_content_to_el el popup;
  popup