Show recent maps; name maps

This commit is contained in:
Christian Lawson-Perfect 2025-02-11 05:17:18 +00:00
parent 6b02b73b9b
commit e6a51f162d
6 changed files with 268 additions and 50 deletions

View file

@ -51,6 +51,8 @@ type alias Model =
{ markers : List Marker
, emoji : List Emoji
, map_id : String
, map_name : String
, map_ids : List ({name: String, id: String})
, current_position : LatLon
, selection : Selection
, new_marker : Marker
@ -62,6 +64,8 @@ init_model =
{ markers = []
, emoji = []
, map_id = ""
, map_name = ""
, map_ids = []
, current_position = {lat = 55.04, lon = -1.46}
, selection = NoSelection
, new_marker = blank_marker
@ -79,6 +83,8 @@ type Msg
| EditMarker Int Marker
| RemoveMarker Int
| ClickCurrentPosition
| SetMapName String
| Save
type alias Emoji =
{ emoji : String
@ -94,19 +100,30 @@ type alias Flags =
{ emoji : List Emoji
, markers : List Marker
, map_id : String
, map_ids : List ({name: String, id: String})
, map_name : String
}
decode_flags =
JD.map3 Flags
JD.map5 Flags
(JD.field "emoji" (JD.list decode_emoji))
(JD.field "markers" (JD.list decode_marker))
(JD.at ["data", "markers"] (JD.list decode_marker))
(JD.field "map_id" JD.string)
(JD.field "map_ids"
(JD.list
(JD.map2 (\name id -> { name = name, id = id})
(JD.field "name" JD.string)
(JD.field "id" JD.string)
)
)
)
(JD.at ["data", "name"] JD.string)
init : (JD.Value) -> (Model, Cmd msg)
init vflags =
(case JD.decodeValue decode_flags vflags of
Err _ -> init_model
Ok flags -> { init_model | emoji = flags.emoji, markers = flags.markers, map_id = flags.map_id }
Ok flags -> { init_model | emoji = flags.emoji, markers = flags.markers, map_id = flags.map_id, map_ids = flags.map_ids, map_name = flags.map_name }
) |> nocmd
nocmd m = (m, Cmd.none)
@ -117,7 +134,11 @@ save model =
, send_value
<| JE.object
[ ("type", JE.string "save")
, ("markers", JE.list identity <| List.indexedMap (\i m -> encode_marker (String.fromInt i) m) model.markers)
, ("data", JE.object
[ ("markers", JE.list identity <| List.indexedMap (\i m -> encode_marker (String.fromInt i) m) model.markers)
, ("name", JE.string model.map_name)
]
)
]
)
@ -157,6 +178,10 @@ update msg model =
ClickCurrentPosition -> { model | centre = CurrentPositionCentre, selection = NoSelection } |> nocmd
SetMapName name -> { model | map_name = name } |> nocmd
Save -> model |> save
add_marker model = case model.selection of
SelectedPosition pos ->
let
@ -383,6 +408,21 @@ view model =
, HA.class "marker-detail"
]
[ H.h1 [] [ H.text "Closest markers" ]
, H.p
[]
[ H.text <| "🗺 "
, H.form
[ HE.onSubmit Save
, HA.id "name-form"
]
[ H.input
[ HA.type_ "text"
, HE.onInput SetMapName
, HA.value model.map_name
]
[]
]
]
, H.ul
[]
(List.map (\(i,m) ->
@ -397,6 +437,18 @@ view model =
[ HA.href <| "?map=" ++ model.map_id ]
[ H.text "🔗 Link to this map" ]
]
, H.h3
[]
[ H.text "Recently-used maps"]
, H.ul
[]
(List.map
(\d -> H.li [] [H.a [HA.href <| "?map="++d.id] [H.text d.name, H.text " ", H.small [] [H.text d.id]]])
model.map_ids
)
, H.p
[]
[ H.a [HA.href "?"] [H.text "New map"]]
]
closest_markers =