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

120
src/js/babillard.ml Normal file
View file

@ -0,0 +1,120 @@
open Brr
open Utils
open Map
module Visibility = struct
let new_thread_div = find_by_id "new-thread"
let thread_preview_div = find_by_id "thread-preview"
let return_button = find_by_id "return-button"
(* new-thread-button is new-thread-button-redirect if not logged in *)
let new_thread_button = find_by_id_opt "new-thread-button"
let is_in_new_thread_mode = ref false
let set_visible el =
log "set_visible@\n";
El.set_class (Jstr.of_string "off") false el
let set_invisible el =
log "set_invisible@\n";
El.set_class (Jstr.of_string "off") true el
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";
let (_ : Ev.listener) =
Ev.listen Ev.click to_babillard_mode (El.as_target return_button)
in
Option.iter
(fun button ->
let (_ : Ev.listener) =
Ev.listen Ev.click to_new_thread_mode (El.as_target button)
in
() )
new_thread_button
end
module Marker = struct
let thread_preview_div = find_by_id "thread-preview"
let marker_on_click thread_preview _e =
log "marker_on_click@\n";
if not !Visibility.is_in_new_thread_mode then (
let inner_html = El.Prop.jstr (Jstr.of_string "innerHTML") in
El.set_prop inner_html thread_preview thread_preview_div;
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" |> Jv.to_jstr 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
(* todo: fetch with Brr *)
let window = Jv.get Jv.global "window" in
let fetchfutur = Jv.call window "fetch" [| link |] in
ignore @@ Jv.call fetchfutur "then" [| Jv.repr markers_handle_response |]
end
let lat_input = find_by_id "lat-input"
let lng_input = find_by_id "lng-input"
let button = find_by_id "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 |> Jstr.of_float in
let lng = Leaflet.Latlng.lng latlng |> Jstr.of_float in
let value_jstr = Jstr.of_string "value" in
El.set_at value_jstr (Some lat) lat_input;
El.set_at value_jstr (Some lng) lng_input;
El.set_at (Jstr.of_string "disabled") None button )
(*add on_click callback to map*)
let () = Leaflet.Map.on Leaflet.Event.Click on_click_set_latlng map