This commit is contained in:
Swrup 2022-04-18 03:11:31 +02:00
parent cd03f75612
commit f73e220176
20 changed files with 166 additions and 178 deletions

View file

@ -1,121 +0,0 @@
open Js_map
include Js_post_form
module Visibility = struct
let new_thread_div = Jv.get Jv.global "new-thread"
let thread_preview_div = Jv.get Jv.global "thread-preview"
let return_button = Jv.get Jv.global "return-button"
(* new-thread-button is new-thread-button-redirect if not logged in *)
let new_thread_button = Jv.find Jv.global "new-thread-button"
let is_in_new_thread_mode = ref false
let set_visible el =
log "set_visible@\n";
let class_list = Jv.get el "classList" in
ignore
@@ Jv.call class_list "replace" [| Jv.of_string "off"; Jv.of_string "on" |]
let set_invisible el =
log "set_invisible@\n";
let class_list = Jv.get el "classList" in
ignore
@@ Jv.call class_list "replace" [| Jv.of_string "on"; Jv.of_string "off" |]
let to_new_thread_mode _event =
log "change_page_mode@\n";
is_in_new_thread_mode := true;
set_visible new_thread_div;
set_visible return_button;
set_invisible thread_preview_div;
Option.iter set_invisible new_thread_button;
Leaflet.Map.close_popup ~popup:None map
let to_babillard_mode _event =
log "change_page_mode@\n";
is_in_new_thread_mode := false;
set_invisible new_thread_div;
set_invisible return_button;
set_visible thread_preview_div;
Option.iter set_visible new_thread_button;
Leaflet.Map.close_popup ~popup:None map
let () =
log "add events on return/new thread button@\n";
ignore
@@ Jv.call return_button "addEventListener"
[| Jv.of_string "click"; Jv.repr to_babillard_mode |];
Option.iter
(fun button ->
ignore
@@ Jv.call button "addEventListener"
[| Jv.of_string "click"; Jv.repr to_new_thread_mode |] )
new_thread_button
end
module Marker = struct
let window = Jv.get Jv.global "window"
let thread_preview_div = Jv.get Jv.global "thread-preview"
let marker_on_click thread_preview _e =
log "marker_on_click@\n";
if not !Visibility.is_in_new_thread_mode then
ignore @@ Jv.set thread_preview_div "innerHTML" thread_preview;
Js_pretty_post.make_pretty ()
let on_each_feature feature layer =
log "on_each_feature@\n";
let feature_properties = Jv.get feature "properties" in
let thread_preview = Jv.get feature_properties "content" in
Leaflet.Layer.on Leaflet.Event.Click (marker_on_click thread_preview) layer
let handle_geojson geojson =
log "handle_geojson@\n";
let layer =
Leaflet.Layer.create_geojson geojson [ On_each_feature on_each_feature ]
in
let _marker_layer = Leaflet.Layer.add_to map layer in
()
let markers_handle_response response =
log "markers_handle_response@\n";
let geo_json_list_futur = Jv.call response "json" [||] in
ignore @@ Jv.call geo_json_list_futur "then" [| Jv.repr handle_geojson |]
let () =
log "fetch thread geojson@\n";
let link = Jv.of_string "/markers" in
let fetchfutur = Jv.call window "fetch" [| link |] in
ignore @@ Jv.call fetchfutur "then" [| Jv.repr markers_handle_response |]
end
let lat_input = Jv.get Jv.global "lat-input"
let lng_input = Jv.get Jv.global "lng-input"
let button = Jv.get Jv.global "submit-new-thread-button"
(* set input lat/lng when clicked*)
let on_click_set_latlng e =
log "on_click_set_latlng@\n";
if !Visibility.is_in_new_thread_mode then (
let latlng = Leaflet.Event.latlng e in
let popup =
Leaflet.Popup.create ~content:(Some "create thread here")
~latlng:(Some latlng) []
in
Leaflet.Map.open_popup popup map;
let lat = Leaflet.Latlng.lat latlng |> Jv.of_float in
let lng = Leaflet.Latlng.lng latlng |> Jv.of_float in
ignore @@ Jv.call lat_input "setAttribute" [| Jv.of_string "value"; lat |];
ignore @@ Jv.call lng_input "setAttribute" [| Jv.of_string "value"; lng |];
ignore @@ Jv.call button "removeAttribute" [| Jv.of_string "disabled" |] )
(*add on_click callback to map*)
let () = Leaflet.Map.on Leaflet.Event.Click on_click_set_latlng map