2022-01-09 11:28:18 +01:00
|
|
|
let log = Format.printf
|
|
|
|
|
|
|
|
|
|
module Leaflet = struct
|
|
|
|
|
(* get the leaflet object *)
|
|
|
|
|
let leaflet =
|
|
|
|
|
match Jv.(find global "L") with
|
|
|
|
|
| Some l -> l
|
|
|
|
|
| None -> failwith "can't load leaflet"
|
|
|
|
|
|
|
|
|
|
(* get popup object *)
|
|
|
|
|
let popup = Jv.call leaflet "popup" [||]
|
|
|
|
|
|
|
|
|
|
(* create a map *)
|
|
|
|
|
let map =
|
2022-02-19 00:59:01 +01:00
|
|
|
log "creating map@\n";
|
2022-01-09 11:28:18 +01:00
|
|
|
let open Brr in
|
|
|
|
|
let _container = El.div ~at:At.[ id (Jstr.v "map") ] [] in
|
|
|
|
|
Jv.call leaflet "map" [| Jv.of_string "map" |]
|
|
|
|
|
|
|
|
|
|
(* create map tile layer *)
|
|
|
|
|
let tile_layer =
|
2022-02-19 00:59:01 +01:00
|
|
|
log "creating tile layer@\n";
|
2022-01-09 11:28:18 +01:00
|
|
|
Jv.call leaflet "tileLayer"
|
|
|
|
|
[| Jv.of_string "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
|
|
|
|
|
; Jv.obj
|
|
|
|
|
[| ( "attribution"
|
|
|
|
|
, Jv.of_string
|
|
|
|
|
{|© <a href="https://.www.openstreetmap.org/copyright">OpenStreetMap</a> contributors|}
|
|
|
|
|
)
|
|
|
|
|
|]
|
|
|
|
|
|]
|
|
|
|
|
|
|
|
|
|
(* add tile layer *)
|
|
|
|
|
let () =
|
2022-02-19 00:59:01 +01:00
|
|
|
log "adding tile layer@\n";
|
2022-01-09 11:28:18 +01:00
|
|
|
let _map : Jv.t = Jv.call tile_layer "addTo" [| map |] in
|
|
|
|
|
()
|
2022-02-07 21:04:51 +01:00
|
|
|
|
|
|
|
|
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 () =
|
2022-02-19 00:59:01 +01:00
|
|
|
log "setting view@\n";
|
2022-02-07 21:04:51 +01:00
|
|
|
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 =
|
2022-02-19 00:59:01 +01:00
|
|
|
log "on moveend event@\n";
|
2022-02-07 21:04:51 +01:00
|
|
|
let latlng = Jv.call map "getCenter" [||] in
|
2022-02-19 00:21:31 +01:00
|
|
|
(*we need to wrap coordinates so we don't drift into a parralel universe and lose track of markers :^) *)
|
|
|
|
|
let wrapped_latlng = Jv.call map "wrapLatLng" [| latlng |] in
|
|
|
|
|
let lat = Jv.get wrapped_latlng "lat" in
|
|
|
|
|
let lng = Jv.get wrapped_latlng "lng" in
|
2022-02-07 21:04:51 +01:00
|
|
|
match
|
|
|
|
|
Brr_io.Storage.set_item storage (Jstr.of_string "lat") (Jv.to_jstr lat)
|
|
|
|
|
with
|
2022-02-18 02:40:04 +01:00
|
|
|
| (exception Jv.Error _) | Error _ -> failwith "can't set latlng storage"
|
2022-02-07 21:04:51 +01:00
|
|
|
| Ok () -> (
|
|
|
|
|
match
|
|
|
|
|
Brr_io.Storage.set_item storage (Jstr.of_string "lng") (Jv.to_jstr lng)
|
|
|
|
|
with
|
2022-02-18 02:40:04 +01:00
|
|
|
| (exception Jv.Error _) | Error _ -> failwith "can't set latlng storage"
|
2022-02-19 00:21:31 +01:00
|
|
|
| Ok () ->
|
|
|
|
|
let is_wrapped =
|
|
|
|
|
not @@ Jv.to_bool @@ Jv.call latlng "equals" [| wrapped_latlng |]
|
|
|
|
|
in
|
|
|
|
|
if is_wrapped then (
|
2022-02-19 00:59:01 +01:00
|
|
|
log "setView to wrapped coordinate@\n";
|
2022-02-19 00:21:31 +01:00
|
|
|
(* warning: calling setView in on_moveend can cause recursion *)
|
|
|
|
|
ignore @@ Jv.call map "setView" [| wrapped_latlng |] ) )
|
2022-02-07 21:04:51 +01:00
|
|
|
|
|
|
|
|
let on_zoomend _event =
|
2022-02-19 00:59:01 +01:00
|
|
|
log "on zoomend event@\n";
|
2022-02-07 21:04:51 +01:00
|
|
|
let zoom = Jv.call map "getZoom" [||] in
|
|
|
|
|
match
|
|
|
|
|
Brr_io.Storage.set_item storage (Jstr.of_string "zoom") (Jv.to_jstr zoom)
|
|
|
|
|
with
|
2022-02-18 02:40:04 +01:00
|
|
|
| (exception Jv.Error _) | Error _ -> failwith "can't set latlng storage"
|
2022-02-07 21:04:51 +01:00
|
|
|
| Ok () -> ()
|
|
|
|
|
|
|
|
|
|
let () =
|
2022-02-19 00:59:01 +01:00
|
|
|
log "add on (move/zoom)end event@\n";
|
2022-02-07 21:04:51 +01:00
|
|
|
ignore @@ Jv.call map "on" [| Jv.of_string "moveend"; Jv.repr on_moveend |];
|
2022-02-18 01:37:25 +01:00
|
|
|
ignore @@ Jv.call map "on" [| Jv.of_string "zoomend"; Jv.repr on_zoomend |]
|
2022-01-09 11:28:18 +01:00
|
|
|
end
|
|
|
|
|
|
2022-02-08 22:39:13 +01:00
|
|
|
module Geolocalize = struct
|
|
|
|
|
let update_location geo =
|
2022-02-19 00:59:01 +01:00
|
|
|
log "update_location@\n";
|
2022-02-08 22:39:13 +01:00
|
|
|
match geo with
|
|
|
|
|
| Error _ -> failwith "error in geolocation"
|
|
|
|
|
| Ok geo ->
|
|
|
|
|
let lat = Brr_io.Geolocation.Pos.latitude geo in
|
|
|
|
|
let lng = Brr_io.Geolocation.Pos.longitude geo in
|
|
|
|
|
let latlng =
|
|
|
|
|
Jv.call Leaflet.leaflet "latLng" [| Jv.of_float lat; Jv.of_float lng |]
|
|
|
|
|
in
|
2022-02-18 01:37:25 +01:00
|
|
|
ignore @@ Jv.call Leaflet.map "setView" [| latlng; Jv.of_int 13 |]
|
2022-02-08 22:39:13 +01:00
|
|
|
|
2022-02-18 18:31:55 +01:00
|
|
|
let geolocalize _ =
|
2022-02-19 00:59:01 +01:00
|
|
|
log "geolocalize@\n";
|
2022-02-08 22:39:13 +01:00
|
|
|
let l = Brr_io.Geolocation.of_navigator Brr.G.navigator in
|
2022-02-18 01:37:25 +01:00
|
|
|
ignore @@ Fut.await (Brr_io.Geolocation.get l) update_location
|
2022-02-08 22:39:13 +01:00
|
|
|
|
2022-02-18 18:31:55 +01:00
|
|
|
let () =
|
|
|
|
|
let button = Jv.get Jv.global "geolocalize" in
|
|
|
|
|
ignore
|
|
|
|
|
@@ Jv.call button "addEventListener"
|
|
|
|
|
[| Jv.of_string "click"; Jv.repr geolocalize |]
|
2022-02-08 22:39:13 +01:00
|
|
|
end
|
|
|
|
|
|
2022-01-09 11:28:18 +01:00
|
|
|
module Marker = struct
|
2022-02-14 17:09:29 +01:00
|
|
|
(*todo do this in js_babillard*)
|
2022-01-09 11:28:18 +01:00
|
|
|
let marker_on_click thread_preview thread_id _e =
|
2022-02-19 00:59:01 +01:00
|
|
|
log "marker_on_click@\n";
|
2022-01-09 11:28:18 +01:00
|
|
|
let thread_id = Jv.to_string thread_id in
|
|
|
|
|
let thread_preview_div = Jv.get Jv.global "thread_preview_div" in
|
|
|
|
|
ignore @@ Jv.set thread_preview_div "innerHTML" thread_preview;
|
|
|
|
|
let thread_link = Jv.get Jv.global "thread_link" in
|
2022-02-18 19:02:17 +01:00
|
|
|
let link = Format.sprintf "/%s" thread_id in
|
2022-01-09 11:28:18 +01:00
|
|
|
ignore @@ Jv.set thread_link "href" (Jv.of_string link);
|
|
|
|
|
ignore @@ Jv.set thread_link "innerText" (Jv.of_string "[View Thread]");
|
2022-02-14 17:09:29 +01:00
|
|
|
let _ = Js_pretty_post.make_pretty () in
|
2022-01-09 11:28:18 +01:00
|
|
|
()
|
|
|
|
|
|
|
|
|
|
let on_each_feature feature layer =
|
2022-02-19 00:59:01 +01:00
|
|
|
log "on_each_feature@\n";
|
2022-01-09 11:28:18 +01:00
|
|
|
let feature_properties = Jv.get feature "properties" in
|
|
|
|
|
let thread_preview = Jv.get feature_properties "content" in
|
|
|
|
|
let thread_id = Jv.get feature_properties "thread_id" in
|
|
|
|
|
ignore
|
|
|
|
|
@@ Jv.call layer "on"
|
|
|
|
|
[| Jv.of_string "click"
|
|
|
|
|
; Jv.repr (marker_on_click thread_preview thread_id)
|
2022-02-18 01:37:25 +01:00
|
|
|
|]
|
2022-01-09 11:28:18 +01:00
|
|
|
|
|
|
|
|
let handle_geojson geojson =
|
2022-02-19 00:59:01 +01:00
|
|
|
log "handle_geojson@\n";
|
|
|
|
|
log "feed geojson to leaflet@\n";
|
2022-02-14 17:09:29 +01:00
|
|
|
(* make markers unresponsive on newthread page*)
|
|
|
|
|
match Jv.find Jv.global "newthread" with
|
|
|
|
|
| None ->
|
|
|
|
|
let dict = Jv.obj [| ("onEachFeature", Jv.repr on_each_feature) |] in
|
|
|
|
|
let layer = Jv.call Leaflet.leaflet "geoJSON" [| geojson; dict |] in
|
|
|
|
|
let _marker_layer = Jv.call layer "addTo" [| Leaflet.map |] in
|
|
|
|
|
()
|
|
|
|
|
| Some _ ->
|
|
|
|
|
let layer = Jv.call Leaflet.leaflet "geoJSON" [| geojson |] in
|
|
|
|
|
let _marker_layer = Jv.call layer "addTo" [| Leaflet.map |] in
|
|
|
|
|
()
|
2022-01-09 11:28:18 +01:00
|
|
|
|
|
|
|
|
let markers_handle_response response =
|
2022-02-19 00:59:01 +01:00
|
|
|
log "markers_handle_response@\n";
|
2022-01-09 11:28:18 +01:00
|
|
|
let geo_json_list_futur = Jv.call response "json" [||] in
|
2022-02-18 01:37:25 +01:00
|
|
|
ignore @@ Jv.call geo_json_list_futur "then" [| Jv.repr handle_geojson |]
|
2022-01-09 11:28:18 +01:00
|
|
|
|
|
|
|
|
let () =
|
2022-02-19 00:59:01 +01:00
|
|
|
log "fetch thread geojson@\n";
|
2022-01-09 11:28:18 +01:00
|
|
|
let window = Jv.get Jv.global "window" in
|
2022-02-18 19:02:17 +01:00
|
|
|
let link = Jv.of_string "/markers" in
|
2022-01-14 13:23:45 +01:00
|
|
|
let fetchfutur = Jv.call window "fetch" [| link |] in
|
2022-02-18 01:37:25 +01:00
|
|
|
ignore @@ Jv.call fetchfutur "then" [| Jv.repr markers_handle_response |]
|
2022-01-09 11:28:18 +01:00
|
|
|
end
|