blob: 59391d2b1fb5736a4989b48839ef94c102cce394 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
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
(* Colors *)
let colors =
Js.Array.reduce
(fun colors marker ->
if Js.Array.indexOf marker.color colors == -1 then
Js.Array.concat [| marker.color |] colors
else
colors)
[| |]
|