From 23ea827e40b5203930b80fb60f611fef10822cd4 Mon Sep 17 00:00:00 2001 From: Swrup Date: Sun, 26 Jun 2022 04:08:53 +0200 Subject: [PATCH] better Layer.bind_popup and add Map.open/close_popup --- src/layer.ml | 12 +++--------- src/layer.mli | 2 +- src/map.ml | 6 ++++++ src/map.mli | 6 ++++++ src/popup.ml | 10 ++-------- src/popup.mli | 9 +++------ 6 files changed, 21 insertions(+), 24 deletions(-) diff --git a/src/layer.ml b/src/layer.ml index b18c108..85a9ce8 100644 --- a/src/layer.ml +++ b/src/layer.ml @@ -31,16 +31,10 @@ let remove_from : type kind. Map.t -> kind t -> unit = let (_ : Jv.t) = Jv.call l "removeFrom" [| Map.to_jv map |] in () -let bind_popup : type kind. Brr.El.t -> Popup.opt list -> kind t -> unit = - fun el options -> function +let bind_popup : type kind. Popup.t -> kind t -> unit = + fun popup -> function | Basic layer | Geojson layer | Marker layer | Tile layer -> - let l = - Array.of_list - @@ List.map (fun o -> (Popup.opt_to_string o, Popup.opt_to_jv o)) options - in - let (_ : Jv.t) = - Jv.call layer "bindPopup" [| Brr.El.to_jv el; Jv.obj l |] - in + let (_ : Jv.t) = Jv.call layer "bindPopup" [| Popup.to_jv popup |] in () let unbind_popup : type kind. kind t -> unit = function diff --git a/src/layer.mli b/src/layer.mli index 9c7a7cd..e819d2e 100644 --- a/src/layer.mli +++ b/src/layer.mli @@ -24,7 +24,7 @@ val remove : _ t -> unit val remove_from : Map.t -> _ t -> unit (** [bind_popup popup layer] binds [popup] to [layer] *) -val bind_popup : Brr.El.t -> Popup.opt list -> _ t -> unit +val bind_popup : Popup.t -> _ t -> unit (** [unbind_popup layer] unbinds the popup bound to [layer] *) val unbind_popup : _ t -> unit diff --git a/src/map.ml b/src/map.ml index 2f2db23..235510c 100644 --- a/src/map.ml +++ b/src/map.ml @@ -46,3 +46,9 @@ let get_zoom map = Jv.call map "getZoom" [||] |> Jv.to_int let wrap_latlng latlng map = Latlng.of_jv @@ Jv.call map "wrapLatLng" [| Latlng.to_jv latlng |] + +let open_popup popup map = + ignore @@ Jv.call map "openPopup" [| Popup.to_jv popup |] + +let close_popup popup map = + ignore @@ Jv.call map "closePopup" [| Popup.to_jv popup |] diff --git a/src/map.mli b/src/map.mli index c1a7770..db91af9 100644 --- a/src/map.mli +++ b/src/map.mli @@ -47,3 +47,9 @@ val of_jv : Jv.t -> t (** [to_jv o] is [o] as {!Jv.t} *) val to_jv : t -> Jv.t + +(* [open_popup popup map] opens [popup] on [map] *) +val open_popup : Popup.t -> t -> unit + +(* [close_popup popup map] closes [popup] on [map] *) +val close_popup : Popup.t -> t -> unit diff --git a/src/popup.ml b/src/popup.ml index f7a1c69..b2a5072 100644 --- a/src/popup.ml +++ b/src/popup.ml @@ -12,16 +12,10 @@ let set_content content = let (_ : Jv.t) = Jv.call popup "setContent" [| Jv.of_string content |] in () -let open_on map = - let (_ : Jv.t) = Jv.call popup "openOn" [| Map.to_jv map |] in - () - -let close map = - let (_ : Jv.t) = Jv.call (Map.to_jv map) "closePopup" [||] in - () - let of_jv = Fun.id +let to_jv = Fun.id + type opt = | Pane of string | Offset of Point.t diff --git a/src/popup.mli b/src/popup.mli index da7f27a..6391cee 100644 --- a/src/popup.mli +++ b/src/popup.mli @@ -26,15 +26,12 @@ val set_latlng : Latlng.t -> unit (** [set_content s] changes the popup content to [s]*) val set_content : string -> unit -(** [open_on map] * Adds the popup to [map] and closes the previous one. *) -val open_on : Map.t -> unit - -(** [close map] * closes the popup of [map]*) -val close : Map.t -> unit - (** [of_jv jv] is [jv] as {!t} *) val of_jv : Jv.t -> t +(** [to_jv o] is [o] as {!Jv.t} *) +val to_jv : t -> Jv.t + (** [opt_to_string opt] is [opt] as {!string} *) val opt_to_string : opt -> string