geochan/src/js/babillard.ml

124 lines
3.8 KiB
OCaml
Raw Normal View History

2022-04-18 03:11:31 +02:00
open Brr
open Utils
open Map
2022-02-22 06:07:29 +01:00
module Visibility = struct
2022-04-18 03:11:31 +02:00
let new_thread_div = find_by_id "new-thread"
2022-02-23 14:30:06 +01:00
2022-04-18 03:21:56 +02:00
let thread_comment = find_by_id "thread-comment"
2022-04-18 03:11:31 +02:00
let thread_preview_div = find_by_id "thread-preview"
2022-02-23 14:30:06 +01:00
2022-04-18 03:11:31 +02:00
let return_button = find_by_id "return-button"
2022-02-23 14:30:06 +01:00
(* new-thread-button is new-thread-button-redirect if not logged in *)
2022-04-18 03:11:31 +02:00
let new_thread_button = find_by_id_opt "new-thread-button"
2022-02-23 14:30:06 +01:00
let is_in_new_thread_mode = ref false
let set_visible el =
log "set_visible@\n";
2022-04-18 03:11:31 +02:00
El.set_class (Jstr.of_string "off") false el
2022-02-23 14:30:06 +01:00
let set_invisible el =
log "set_invisible@\n";
2022-04-18 03:11:31 +02:00
El.set_class (Jstr.of_string "off") true el
2022-02-23 14:30:06 +01:00
let to_new_thread_mode _event =
2022-02-22 06:07:29 +01:00
log "change_page_mode@\n";
2022-02-23 14:30:06 +01:00
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;
2022-04-18 03:21:56 +02:00
El.set_has_focus true thread_comment;
2022-04-08 10:39:20 +02:00
Leaflet.Map.close_popup ~popup:None map
2022-02-23 14:30:06 +01:00
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;
2022-04-08 10:39:20 +02:00
Leaflet.Map.close_popup ~popup:None map
2022-02-22 06:07:29 +01:00
let () =
log "add events on return/new thread button@\n";
2022-04-18 03:11:31 +02:00
let (_ : Ev.listener) =
Ev.listen Ev.click to_babillard_mode (El.as_target return_button)
in
2022-02-23 14:30:06 +01:00
Option.iter
(fun button ->
2022-04-18 03:11:31 +02:00
let (_ : Ev.listener) =
Ev.listen Ev.click to_new_thread_mode (El.as_target button)
in
() )
2022-02-23 14:30:06 +01:00
new_thread_button
2022-02-22 06:07:29 +01:00
end
module Marker = struct
2022-04-18 03:11:31 +02:00
let thread_preview_div = find_by_id "thread-preview"
2022-02-23 14:30:06 +01:00
2022-02-22 06:07:29 +01:00
let marker_on_click thread_preview _e =
log "marker_on_click@\n";
2022-04-18 03:11:31 +02:00
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 () )
2022-02-22 06:07:29 +01:00
let on_each_feature feature layer =
log "on_each_feature@\n";
let feature_properties = Jv.get feature "properties" in
2022-04-18 03:11:31 +02:00
let thread_preview = Jv.get feature_properties "content" |> Jv.to_jstr in
2022-04-08 10:39:20 +02:00
Leaflet.Layer.on Leaflet.Event.Click (marker_on_click thread_preview) layer
2022-02-22 06:07:29 +01:00
let handle_geojson geojson =
log "handle_geojson@\n";
2022-04-08 10:39:20 +02:00
let layer =
Leaflet.Layer.create_geojson geojson [ On_each_feature on_each_feature ]
in
let _marker_layer = Leaflet.Layer.add_to map layer in
2022-02-22 06:07:29 +01:00
()
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
2022-04-18 03:11:31 +02:00
(* todo: fetch with Brr *)
let window = Jv.get Jv.global "window" in
2022-02-22 06:07:29 +01:00
let fetchfutur = Jv.call window "fetch" [| link |] in
ignore @@ Jv.call fetchfutur "then" [| Jv.repr markers_handle_response |]
end
2022-04-18 03:11:31 +02:00
let lat_input = find_by_id "lat-input"
2022-02-23 14:30:06 +01:00
2022-04-18 03:11:31 +02:00
let lng_input = find_by_id "lng-input"
2022-02-23 14:30:06 +01:00
2022-04-18 03:11:31 +02:00
let button = find_by_id "submit-new-thread-button"
2022-02-23 14:30:06 +01:00
2022-02-22 06:07:29 +01:00
(* set input lat/lng when clicked*)
let on_click_set_latlng e =
log "on_click_set_latlng@\n";
2022-02-23 14:30:06 +01:00
if !Visibility.is_in_new_thread_mode then (
2022-04-08 10:39:20 +02:00
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;
2022-04-18 03:11:31 +02:00
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 )
2022-02-22 06:07:29 +01:00
(*add on_click callback to map*)
2022-04-08 10:39:20 +02:00
let () = Leaflet.Map.on Leaflet.Event.Click on_click_set_latlng map