use gadts for layer, clean code
This commit is contained in:
parent
f4ab3e17ad
commit
fbfb403734
17 changed files with 152 additions and 163 deletions
92
src/layer.ml
92
src/layer.ml
|
|
@ -1,23 +1,89 @@
|
|||
type t = Jv.t
|
||||
type _ t =
|
||||
| Basic : Jv.t -> [> `Basic ] t
|
||||
| Geojson : Jv.t -> [> `Geojson ] t
|
||||
| Marker : Jv.t -> [> `Marker ] t
|
||||
| Tile : Jv.t -> [> `Tile ] t
|
||||
|
||||
let add_to map layer = ignore @@ Jv.call layer "addTo" [| Map.to_jv_t map |]
|
||||
(** Basic layers *)
|
||||
|
||||
let remove layer = ignore @@ Jv.call layer "remove" [||]
|
||||
let add_to : type kind. Map.t -> kind t -> unit =
|
||||
fun map -> function
|
||||
| Basic l | Geojson l | Marker l | Tile l ->
|
||||
let (_ : Jv.t) = Jv.call l "addTo" [| Map.to_jv map |] in
|
||||
()
|
||||
|
||||
let remove_from map layer =
|
||||
ignore @@ Jv.call layer "removeFrom" [| Map.to_jv_t map |]
|
||||
let remove : type kind. kind t -> unit = function
|
||||
| Basic l | Geojson l | Marker l | Tile l ->
|
||||
let (_ : Jv.t) = Jv.call l "remove" [||] in
|
||||
()
|
||||
|
||||
let bind_popup el layer =
|
||||
ignore @@ Jv.call layer "bindPopup" [| Brr.El.to_jv el |]
|
||||
let remove_from : type kind. Map.t -> kind t -> unit =
|
||||
fun map -> function
|
||||
| Basic l | Geojson l | Marker l | Tile l ->
|
||||
let (_ : Jv.t) = Jv.call l "removeFrom" [| Map.to_jv map |] in
|
||||
()
|
||||
|
||||
let unbind_popup layer = ignore @@ Jv.call layer "unbindPopup" [||]
|
||||
let bind_popup : type kind. Brr.El.t -> kind t -> unit =
|
||||
fun el -> function
|
||||
| Basic l | Geojson l | Marker l | Tile l ->
|
||||
let (_ : Jv.t) = Jv.call l "bindPopup" [| Brr.El.to_jv el |] in
|
||||
()
|
||||
|
||||
let open_popup layer = ignore @@ Jv.call layer "openPopup" [||]
|
||||
let unbind_popup : type kind. kind t -> unit = function
|
||||
| Basic l | Geojson l | Marker l | Tile l ->
|
||||
let (_ : Jv.t) = Jv.call l "unbindPopup" [||] in
|
||||
()
|
||||
|
||||
let close_popup layer = ignore @@ Jv.call layer "closePopup" [||]
|
||||
let open_popup : type kind. kind t -> unit = function
|
||||
| Basic l | Geojson l | Marker l | Tile l ->
|
||||
let (_ : Jv.t) = Jv.call l "openPopup" [||] in
|
||||
()
|
||||
|
||||
let get_popup layer = Popup.of_jv_t @@ Jv.call layer "getPopup" [||]
|
||||
let close_popup : type kind. kind t -> unit = function
|
||||
| Basic l | Geojson l | Marker l | Tile l ->
|
||||
let (_ : Jv.t) = Jv.call l "closePopup" [||] in
|
||||
()
|
||||
|
||||
let of_jv_t = Fun.id
|
||||
let get_popup : type kind. kind t -> Popup.t = function
|
||||
| Basic l | Geojson l | Marker l | Tile l ->
|
||||
Jv.call l "getPopup" [||] |> Popup.of_jv
|
||||
|
||||
let to_jv_t = Fun.id
|
||||
let to_jv : type kind. kind t -> Jv.t = function
|
||||
| Basic l | Geojson l | Marker l | Tile l -> l
|
||||
|
||||
(** Geojson layers *)
|
||||
|
||||
let create_geojson : ?options:Jv.t -> Jv.t -> [ `Geojson ] t =
|
||||
fun ?(options = Jv.null) geojson ->
|
||||
let jv_t = Jv.call Global.leaflet "geoJSON" [| geojson; options |] in
|
||||
Geojson jv_t
|
||||
|
||||
(** Marker layers *)
|
||||
|
||||
let create_marker : Latlng.t -> [ `Marker ] t =
|
||||
fun latlng ->
|
||||
let jv_t = Jv.call Global.leaflet "marker" [| Latlng.to_jv latlng |] in
|
||||
Marker jv_t
|
||||
|
||||
(** Tile layers *)
|
||||
|
||||
let create_tile_osm : string option -> [ `Tile ] t =
|
||||
fun url ->
|
||||
(* see https://wiki.openstreetmap.org/wiki/Tile_servers *)
|
||||
let url =
|
||||
Option.value url
|
||||
~default:"https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
|
||||
in
|
||||
let jv_t =
|
||||
Jv.call Global.leaflet "tileLayer"
|
||||
[| Jv.of_string url
|
||||
; Jv.obj
|
||||
[| ( "attribution"
|
||||
, Jv.of_string
|
||||
"© <a \
|
||||
href=\"https://www.openstreetmap.org/copyright\">OpenStreetMap</a> \
|
||||
contributors" )
|
||||
|]
|
||||
|]
|
||||
in
|
||||
Tile jv_t
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue