Skip to content

Commit 613b77f

Browse files
authored
[Line Diagram][Map]: Show a tooltip with vehicle status when clicking on a vehicle marker on the map (#3569)
* Added popups to vehicle markers. Reused vehicle_label from upcoming departures to obtain the info. * Cleaned up code thanks to Josh's refactoring * Added CSS class support to vehicle_label * Add CSS class override support for crowding message as well * Adjusted pattern to match more specifically * Simplified call to vehicle_name and to_vehicle_marker (we didn't need the whole assigns). Updated the map CSS so that only the default markers have a default fill, not every single SVG in the map. This keeps the trip planner look on that page, but lets us use our other icons on the map without them being blue by default. * Added white_bg? param to vehilce_label and the relevant children, so that <.crowding_icon> can have a white bg where necessary. * Fix warning
1 parent 37d638d commit 613b77f

4 files changed

Lines changed: 72 additions & 14 deletions

File tree

‎assets/css/map.css‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@
33
height: 100%;
44
}
55

6-
.mbta-map svg {
7-
fill: var(--colors-cobalt-30);
8-
}
9-
106
.mbta-map-pin {
117
width: 4rem;
128
height: 4rem;
9+
fill: var(--colors-cobalt-30);
10+
1311
}
1412

1513
.mbta-map-point {

‎lib/dotcom/schedule_finder/trip_details.ex‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,27 @@ defmodule Dotcom.ScheduleFinder.TripDetails do
253253
}
254254
end
255255

256+
defp vehicle_info(
257+
%Vehicle{
258+
crowding: crowding,
259+
status: status,
260+
stop_id: stop_id,
261+
stop_sequence: stop_sequence,
262+
trip_id: vehicle_trip_id
263+
},
264+
[]
265+
) do
266+
stop = @stops_repo.get(stop_id)
267+
268+
%VehicleInfo{
269+
crowding: crowding,
270+
status: status,
271+
stop_id: stop.parent_id || stop.id,
272+
stop_name: stop.name,
273+
stop_sequence: stop_sequence
274+
}
275+
end
276+
256277
defp vehicle_info(_, _),
257278
do: %VehicleInfo{status: :finishing_another_trip}
258279

‎lib/dotcom_web/live/line_diagram_live.ex‎

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ defmodule DotcomWeb.LineDiagramLive do
129129
|> assign_new(:vehicle_icons, fn ->
130130
route_id
131131
|> @vehicles_repo.route(direction_id: direction_id)
132-
|> Map.new(&{&1.id, to_vehicle_marker(&1)})
132+
|> Map.new(&{&1.id, to_vehicle_marker(&1, route)})
133133
end)
134134
|> assign(:route_id, route_id)
135135
|> assign(:route, route)
@@ -190,22 +190,40 @@ defmodule DotcomWeb.LineDiagramLive do
190190

191191
def assign_vehicle_icons(socket, "reset", vehicles) do
192192
vehicles
193-
|> Map.new(&{&1.id, to_vehicle_marker(&1)})
193+
|> Map.new(&{&1.id, to_vehicle_marker(&1, socket.assigns.route)})
194194
|> then(&assign(socket, :vehicle_icons, &1))
195195
end
196196

197197
# add or update
198198
def assign_vehicle_icons(socket, _, vehicles) do
199-
updated_vehicles = Map.new(vehicles, &{&1.id, to_vehicle_marker(&1)})
199+
updated_vehicles =
200+
Map.new(vehicles, &{&1.id, to_vehicle_marker(&1, socket.assigns.route)})
201+
200202
update(socket, :vehicle_icons, &Map.merge(&1, updated_vehicles))
201203
end
202204

203-
defp to_vehicle_marker(vehicle) do
205+
defp to_vehicle_marker(vehicle, route) do
206+
%{stops: _, vehicle_info: vehicle_info} =
207+
Dotcom.ScheduleFinder.TripDetails.trip_details(%{
208+
predicted_schedules: [],
209+
trip_vehicle: vehicle
210+
})
211+
212+
popup =
213+
DotcomWeb.Live.UpcomingDeparturesLive.vehicle_label(%{
214+
route: route,
215+
vehicle_info: vehicle_info,
216+
classes: "text-white mr-2",
217+
crowding_classes: "text-white",
218+
white_bg?: true
219+
})
220+
204221
%{
205222
coordinates: [vehicle.longitude, vehicle.latitude],
206223
type: "icon-svg",
207224
name: "icon-vehicle-bordered-expanded",
208-
class: "size-6",
225+
class: "size-6 cursor-pointer",
226+
popup: popup,
209227
rotation: vehicle.bearing || "0"
210228
}
211229
end

‎lib/dotcom_web/live/upcoming_departures_live.ex‎

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -499,10 +499,13 @@ defmodule DotcomWeb.Live.UpcomingDeparturesLive do
499499

500500
attr :vehicle_info, Dotcom.ScheduleFinder.TripDetails.VehicleInfo, required: true
501501
attr :route, Routes.Route, required: true
502+
attr :classes, :string, default: ""
503+
attr :crowding_classes, :string, default: ""
504+
attr :white_bg?, :boolean, default: false
502505

503-
defp vehicle_label(assigns) do
506+
def vehicle_label(assigns) do
504507
~H"""
505-
<div class="font-normal text-charcoal-30 text-sm">
508+
<div class={"font-normal text-charcoal-30 #{@classes} text-sm"}>
506509
<span :if={@vehicle_info.status != :in_transit} class="sr-only">
507510
{Routes.Route.vehicle_name(@route)}
508511
</span>
@@ -514,7 +517,9 @@ defmodule DotcomWeb.Live.UpcomingDeparturesLive do
514517
/>
515518
<.vehicle_crowding
516519
crowding={crowding(@vehicle_info)}
520+
classes={@crowding_classes}
517521
show_label?
522+
white_bg?={@white_bg?}
518523
/>
519524
"""
520525
end
@@ -531,26 +536,42 @@ defmodule DotcomWeb.Live.UpcomingDeparturesLive do
531536

532537
attr :crowding, :atom
533538
attr :show_label?, :boolean, default: false
539+
attr :classes, :string, default: ""
540+
attr :white_bg?, :boolean, default: false
534541

535542
defp vehicle_crowding(%{show_label?: true} = assigns) do
536543
~H"""
537544
<div :if={@crowding} class="flex gap-xs text-sm flex-nowrap items-center">
538-
<.crowding_icon class="size-4" crowding={@crowding} aria-hidden />
539-
<div class="font-normal text-charcoal-30">{crowding_message(@crowding)}</div>
545+
<.crowding_icon class="size-4" crowding={@crowding} aria-hidden white_bg?={@white_bg?} />
546+
<div class={"font-normal text-charcoal-30 #{@classes}"}>{crowding_message(@crowding)}</div>
540547
</div>
541548
"""
542549
end
543550

544551
defp vehicle_crowding(assigns) do
545552
~H"""
546-
<.crowding_icon :if={@crowding} crowding={@crowding} aria-label={crowding_message(@crowding)} />
553+
<.crowding_icon
554+
:if={@crowding}
555+
crowding={@crowding}
556+
aria-label={crowding_message(@crowding)}
557+
white_bg?={@white_bg?}
558+
/>
547559
"""
548560
end
549561

550562
attr :class, :string, default: ""
551563
attr :crowding, :atom
564+
attr :white_bg?, :boolean, default: false
552565
attr :rest, :global
553566

567+
defp crowding_icon(%{white_bg?: true} = assigns) do
568+
~H"""
569+
<div class="bg-white p-1 pb-0 rounded-sm">
570+
<.crowding_icon class={@class} crowding={@crowding} {@rest} />
571+
</div>
572+
"""
573+
end
574+
554575
defp crowding_icon(assigns) do
555576
~H"""
556577
<.icon

0 commit comments

Comments
 (0)