Skip to content

Commit

Permalink
Write directions sidebar header using .erb template
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonKhorev committed Jun 14, 2024
1 parent 84aa7f4 commit 78d3b47
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 20 deletions.
52 changes: 32 additions & 20 deletions app/assets/javascripts/index/directions.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,24 @@ OSM.Directions = function (map) {
select.val(index);
}

function setSidebarOverlaidForEndpoints() {
map.setSidebarOverlaid(!endpoints[0].latlng || !endpoints[1].latlng);
}

function getRoute(fitRoute, reportErrors) {
if ($("#directions_content").length) {
getRouteWithLoadedSidebar(fitRoute, reportErrors);
} else {
OSM.loadSidebarContent("/directions", function () {
getRouteWithLoadedSidebar(fitRoute, reportErrors);
});
setSidebarOverlaidForEndpoints();
}
}

function getRouteWithLoadedSidebar(fitRoute, reportErrors) {
setSidebarOverlaidForEndpoints();

// Cancel any route that is already in progress
if (awaitingRoute) awaitingRoute.abort();

Expand Down Expand Up @@ -226,8 +243,7 @@ OSM.Directions = function (map) {
// copy loading item to sidebar and display it. we copy it, rather than
// just using it in-place and replacing it in case it has to be used
// again.
$("#sidebar_content").html($(".directions_form .loader_copy").html());
map.setSidebarOverlaid(false);
$("#directions_content").html($(".directions_form .loader_copy").html());

awaitingRoute = chosenEngine.getRoute([o, d], function (err, route) {
awaitingRoute = null;
Expand All @@ -236,7 +252,7 @@ OSM.Directions = function (map) {
map.removeLayer(polyline);

if (reportErrors) {
$("#sidebar_content").html("<div class=\"alert alert-danger\">" + I18n.t("javascripts.directions.errors.no_route") + "</div>");
$("#directions_content").html("<div class=\"alert alert-danger\">" + I18n.t("javascripts.directions.errors.no_route") + "</div>");
}

return;
Expand All @@ -262,16 +278,10 @@ OSM.Directions = function (map) {

var turnByTurnTable = $("<table class='table table-hover table-sm mb-3'>")
.append($("<tbody>"));
var directionsCloseButton = $("<button type='button' class='btn-close'>")
.attr("aria-label", I18n.t("javascripts.close"));

$("#sidebar_content")
$("#directions_content")
.empty()
.append(
$("<div class='d-flex'>").append(
$("<h2 class='flex-grow-1 text-break'>")
.text(I18n.t("javascripts.directions.directions")),
$("<div>").append(directionsCloseButton)),
distanceText,
turnByTurnTable
);
Expand Down Expand Up @@ -319,19 +329,20 @@ OSM.Directions = function (map) {
turnByTurnTable.append(row);
});

$("#sidebar_content").append("<p class=\"text-center\">" +
$("#directions_content").append("<p class=\"text-center\">" +
I18n.t("javascripts.directions.instructions.courtesy", { link: chosenEngine.creditline }) +
"</p>");

directionsCloseButton.on("click", function () {
map.removeLayer(polyline);
$("#sidebar_content").html("");
map.setSidebarOverlaid(true);
// TODO: collapse width of sidebar back to previous
});
});
}

function hideRoute(e) {
e.stopPropagation();
map.removeLayer(polyline);
$("#directions_content").html("");
map.setSidebarOverlaid(true);
// TODO: collapse width of sidebar back to previous
}

var chosenEngineIndex = findEngine("fossgis_osrm_car");
if (Cookies.get("_osm_directions_engine")) {
chosenEngineIndex = findEngine(Cookies.get("_osm_directions_engine"));
Expand Down Expand Up @@ -366,6 +377,8 @@ OSM.Directions = function (map) {
$(".search_form").hide();
$(".directions_form").show();

$("#sidebar_content").on("click", ".btn-close", hideRoute);

$("#map").on("dragend dragover", function (e) {
e.preventDefault();
});
Expand Down Expand Up @@ -398,8 +411,6 @@ OSM.Directions = function (map) {
endpoints[0].setValue(params.from || "", from);
endpoints[1].setValue(params.to || "", to);

map.setSidebarOverlaid(!from || !to);

getRoute(true, true);
};

Expand All @@ -410,6 +421,7 @@ OSM.Directions = function (map) {
page.unload = function () {
$(".search_form").show();
$(".directions_form").hide();
$("#sidebar_content").off("click", ".btn-close", hideRoute);
$("#map").off("dragend dragover drop");

map
Expand Down
4 changes: 4 additions & 0 deletions app/views/directions/search.html.erb
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
<% content_for(:content_class) { "overlay-sidebar" } %>

<%= render "sidebar_header", :title => t("javascripts.directions.directions") %>

<div id="directions_content"></div>

0 comments on commit 78d3b47

Please sign in to comment.