diff options
author | Joris | 2020-07-26 18:16:59 +0200 |
---|---|---|
committer | Joris | 2020-07-26 18:16:59 +0200 |
commit | 4ee0dfae75fda3a8b6347d55c728b50ce5c210d9 (patch) | |
tree | 5f73adaf57354e0070acaa9a6b60dc49c0c48526 /src/State.ml | |
parent | 447f43995ae8d83c82d98d9d8968e90d6c4518e7 (diff) |
Allow to customize icons
Diffstat (limited to 'src/State.ml')
-rw-r--r-- | src/State.ml | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/src/State.ml b/src/State.ml new file mode 100644 index 0000000..cc20b16 --- /dev/null +++ b/src/State.ml @@ -0,0 +1,61 @@ +type marker_state = + { pos : Leaflet.lat_lng + ; name : string + ; color : string + ; icon : string + } + +let remove state pos = + Js.Array.filter (fun m -> m.pos != pos) state + +let update state previousPos marker = + Js.Array.concat [| marker |] (remove state previousPos) + +let last_added state = + if Js.Array.length state > 0 then + Some state.(0) + else + None + +(* Serialization *) + +let sep = "|" + +let marker_to_string marker = + [| String.format_float 6 marker.pos.lat + ; String.format_float 6 marker.pos.lng + ; marker.name + ; marker.color + ; marker.icon + |] + |> Js.Array.joinWith sep + +let to_string state = + state + |> Js.Array.map marker_to_string + |> Js.Array.joinWith sep + |> String.encode + +let from_string str = + let (_, _, res) = Js.Array.reduce + (fun (acc_str, acc_marker, acc_state) c -> + let length = Js.Array.length acc_marker in + if c != sep then + (acc_str ^ c, acc_marker, acc_state) + else if c == sep && length < 4 then + ("", Js.Array.concat [| acc_str |] acc_marker, acc_state) + else + let marker = + { pos = + { lat = Js.Float.fromString acc_marker.(0) + ; lng = Js.Float.fromString acc_marker.(1) + } + ; name = acc_marker.(2) + ; color = acc_marker.(3) + ; icon = acc_str + } + in ("", [| |], Js.Array.concat acc_state [| marker |]) + ) + ("", [| |], [| |]) + (Js.Array.from (Js.String.castToArrayLike ((String.decode str) ^ sep))) + in res |