From 3944ac7340e65595b4236b18acd80b3717e609c4 Mon Sep 17 00:00:00 2001 From: Swrup Date: Mon, 7 Feb 2022 21:04:51 +0100 Subject: [PATCH] store map's view to web storage --- src/js_babillard.ml | 70 +++++++++++++++++++++++++++++++++++------- src/js_newthread.ml | 75 ++++++++++++++++++++++++++++++++++++--------- 2 files changed, 120 insertions(+), 25 deletions(-) diff --git a/src/js_babillard.ml b/src/js_babillard.ml index 296224c..de7a33f 100644 --- a/src/js_babillard.ml +++ b/src/js_babillard.ml @@ -1,5 +1,6 @@ let log = Format.printf +(*TODO fix duplicate module *) module Leaflet = struct (* get the leaflet object *) let leaflet = @@ -17,17 +18,6 @@ module Leaflet = struct let _container = El.div ~at:At.[ id (Jstr.v "map") ] [] in Jv.call leaflet "map" [| Jv.of_string "map" |] - (* create map's pos *) - let lat_lng = - log "making latlng@."; - Jv.call leaflet "latLng" [| Jv.of_float 51.505; Jv.of_float (-0.09) |] - - (* set map's pos *) - let () = - log "setting view@."; - let _m : Jv.t = Jv.call map "setView" [| lat_lng; Jv.of_int 13 |] in - () - (* create map tile layer *) let tile_layer = log "creating tile layer@."; @@ -46,6 +36,64 @@ module Leaflet = struct log "adding tile layer@."; let _map : Jv.t = Jv.call tile_layer "addTo" [| map |] in () + + let storage = Brr_io.Storage.local Brr.G.window + + (* set map's view *) + (* try to set map's view to last position viewed by using web storage *) + let () = + log "setting view@."; + let lat = Brr_io.Storage.get_item storage (Jstr.of_string "lat") in + let lng = Brr_io.Storage.get_item storage (Jstr.of_string "lng") in + let zoom = Brr_io.Storage.get_item storage (Jstr.of_string "zoom") in + match (lat, lng, zoom) with + | Some lat, Some lng, Some zoom -> + let latlng = + Jv.call leaflet "latLng" [| Jv.of_jstr lat; Jv.of_jstr lng |] + in + ignore @@ Jv.call map "setView" [| latlng; Jv.of_jstr zoom |] + | _ -> + let latlng = + Jv.call leaflet "latLng" [| Jv.of_float 51.505; Jv.of_float (-0.09) |] + in + ignore @@ Jv.call map "setView" [| latlng; Jv.of_int 13 |] + + let on_moveend _event = + log "on zoomend event@."; + let latlng = Jv.call map "getCenter" [||] in + let lat = Jv.get latlng "lat" in + let lng = Jv.get latlng "lng" in + match + Brr_io.Storage.set_item storage (Jstr.of_string "lat") (Jv.to_jstr lat) + with + | (exception Jv.Error _) + | Error _ -> + failwith "can't set latlng storage" + | Ok () -> ( + match + Brr_io.Storage.set_item storage (Jstr.of_string "lng") (Jv.to_jstr lng) + with + | (exception Jv.Error _) + | Error _ -> + failwith "can't set latlng storage" + | Ok () -> () ) + + let on_zoomend _event = + log "on zoomend event@."; + let zoom = Jv.call map "getZoom" [||] in + match + Brr_io.Storage.set_item storage (Jstr.of_string "zoom") (Jv.to_jstr zoom) + with + | (exception Jv.Error _) + | Error _ -> + failwith "can't set latlng storage" + | Ok () -> () + + let () = + log "add on (move/zoom)end event@."; + ignore @@ Jv.call map "on" [| Jv.of_string "moveend"; Jv.repr on_moveend |]; + ignore @@ Jv.call map "on" [| Jv.of_string "zoomend"; Jv.repr on_zoomend |]; + () end module Marker = struct diff --git a/src/js_newthread.ml b/src/js_newthread.ml index 9dcdd30..71590c8 100644 --- a/src/js_newthread.ml +++ b/src/js_newthread.ml @@ -1,5 +1,6 @@ let log = Format.printf +(*TODO fix duplicate module *) module Leaflet = struct (* get the leaflet object *) let leaflet = @@ -7,9 +8,8 @@ module Leaflet = struct | Some l -> l | None -> failwith "can't load leaflet" - (* get popup object, with no close button*) - let popup = - Jv.call leaflet "popup" [| Jv.obj [| ("closeButton", Jv.false') |] |] + (* get popup object *) + let popup = Jv.call leaflet "popup" [||] (* create a map *) let map = @@ -18,17 +18,6 @@ module Leaflet = struct let _container = El.div ~at:At.[ id (Jstr.v "map") ] [] in Jv.call leaflet "map" [| Jv.of_string "map" |] - (* create map's pos *) - let lat_lng = - log "making latlng@."; - Jv.call leaflet "latLng" [| Jv.of_float 51.505; Jv.of_float (-0.09) |] - - (* set map's pos *) - let () = - log "setting view@."; - let _m : Jv.t = Jv.call map "setView" [| lat_lng; Jv.of_int 13 |] in - () - (* create map tile layer *) let tile_layer = log "creating tile layer@."; @@ -47,6 +36,64 @@ module Leaflet = struct log "adding tile layer@."; let _map : Jv.t = Jv.call tile_layer "addTo" [| map |] in () + + let storage = Brr_io.Storage.local Brr.G.window + + (* set map's view *) + (* try to set map's view to last position viewed by using web storage *) + let () = + log "setting view@."; + let lat = Brr_io.Storage.get_item storage (Jstr.of_string "lat") in + let lng = Brr_io.Storage.get_item storage (Jstr.of_string "lng") in + let zoom = Brr_io.Storage.get_item storage (Jstr.of_string "zoom") in + match (lat, lng, zoom) with + | Some lat, Some lng, Some zoom -> + let latlng = + Jv.call leaflet "latLng" [| Jv.of_jstr lat; Jv.of_jstr lng |] + in + ignore @@ Jv.call map "setView" [| latlng; Jv.of_jstr zoom |] + | _ -> + let latlng = + Jv.call leaflet "latLng" [| Jv.of_float 51.505; Jv.of_float (-0.09) |] + in + ignore @@ Jv.call map "setView" [| latlng; Jv.of_int 13 |] + + let on_moveend _event = + log "on zoomend event@."; + let latlng = Jv.call map "getCenter" [||] in + let lat = Jv.get latlng "lat" in + let lng = Jv.get latlng "lng" in + match + Brr_io.Storage.set_item storage (Jstr.of_string "lat") (Jv.to_jstr lat) + with + | (exception Jv.Error _) + | Error _ -> + failwith "can't set latlng storage" + | Ok () -> ( + match + Brr_io.Storage.set_item storage (Jstr.of_string "lng") (Jv.to_jstr lng) + with + | (exception Jv.Error _) + | Error _ -> + failwith "can't set latlng storage" + | Ok () -> () ) + + let on_zoomend _event = + log "on zoomend event@."; + let zoom = Jv.call map "getZoom" [||] in + match + Brr_io.Storage.set_item storage (Jstr.of_string "zoom") (Jv.to_jstr zoom) + with + | (exception Jv.Error _) + | Error _ -> + failwith "can't set latlng storage" + | Ok () -> () + + let () = + log "add on (move/zoom)end event@."; + ignore @@ Jv.call map "on" [| Jv.of_string "moveend"; Jv.repr on_moveend |]; + ignore @@ Jv.call map "on" [| Jv.of_string "zoomend"; Jv.repr on_zoomend |]; + () end module Marker = struct