From: Anton Khorev Date: Sun, 6 Apr 2025 10:28:11 +0000 (+0300) Subject: Move directions step numbering out of engine-specific code X-Git-Tag: live~69^2 X-Git-Url: https://git.openstreetmap.org./rails.git/commitdiff_plain/ccfadb1441b44d12295e9e0ad7bdf78067b1c2c0?ds=sidebyside Move directions step numbering out of engine-specific code --- diff --git a/app/assets/javascripts/index/directions.js b/app/assets/javascripts/index/directions.js index 57def249a..8b3047b68 100644 --- a/app/assets/javascripts/index/directions.js +++ b/app/assets/javascripts/index/directions.js @@ -184,7 +184,7 @@ OSM.Directions = function (map) { ); // Add each row - route.steps.forEach(function (step) { + for (const [i, step] of route.steps.entries()) { const [ll, direction, instruction, dist, lineseg] = step; const row = $(""); @@ -193,7 +193,7 @@ OSM.Directions = function (map) { } else { row.append(""); } - row.append("" + instruction); + row.append(`${i + 1}. ${instruction}`); row.append("" + formatStepDistance(dist)); row.on("click", function () { @@ -212,7 +212,7 @@ OSM.Directions = function (map) { }); turnByTurnTable.append(row); - }); + } const blob = new Blob([JSON.stringify(polyline.toGeoJSON())], { type: "application/json" }); URL.revokeObjectURL(downloadURL); diff --git a/app/assets/javascripts/index/directions/fossgis_osrm.js b/app/assets/javascripts/index/directions/fossgis_osrm.js index b2586e68e..b5faca510 100644 --- a/app/assets/javascripts/index/directions/fossgis_osrm.js +++ b/app/assets/javascripts/index/directions/fossgis_osrm.js @@ -89,7 +89,7 @@ } const steps = route.legs.flatMap( - leg => leg.steps.map(function (step, idx) { + leg => leg.steps.map(function (step) { const maneuver_id = getManeuverId(step.maneuver); const instrPrefix = "javascripts.directions.instructions."; @@ -97,7 +97,7 @@ const step_geometry = L.PolylineUtil.decode(step.geometry, { precision: 5 }).map(L.latLng); - let instText = "" + (idx + 1) + ". "; + let instText; const destinations = "" + step.destinations + ""; let namedRoad = true; let name; @@ -114,16 +114,16 @@ } if (step.maneuver.type.match(/^exit (rotary|roundabout)$/)) { - instText += OSM.i18n.t(template, { name: name }); + instText = OSM.i18n.t(template, { name: name }); } else if (step.maneuver.type.match(/^(rotary|roundabout)$/)) { if (step.maneuver.exit) { if (step.maneuver.exit <= 10) { - instText += OSM.i18n.t(template + "_with_exit_ordinal", { exit: OSM.i18n.t(instrPrefix + "exit_counts." + numToWord(step.maneuver.exit)), name: name }); + instText = OSM.i18n.t(template + "_with_exit_ordinal", { exit: OSM.i18n.t(instrPrefix + "exit_counts." + numToWord(step.maneuver.exit)), name: name }); } else { - instText += OSM.i18n.t(template + "_with_exit", { exit: step.maneuver.exit, name: name }); + instText = OSM.i18n.t(template + "_with_exit", { exit: step.maneuver.exit, name: name }); } } else { - instText += OSM.i18n.t(template + "_without_exit", { name: name }); + instText = OSM.i18n.t(template + "_without_exit", { name: name }); } } else if (step.maneuver.type.match(/^(on ramp|off ramp)$/)) { const params = {}; @@ -133,9 +133,9 @@ if (Object.keys(params).length > 0) { template = template + "_with_" + Object.keys(params).join("_"); } - instText += OSM.i18n.t(template, params); + instText = OSM.i18n.t(template, params); } else { - instText += OSM.i18n.t(template + "_without_exit", { name: name }); + instText = OSM.i18n.t(template + "_without_exit", { name: name }); } return [[step.maneuver.location[1], step.maneuver.location[0]], ICON_MAP[maneuver_id], instText, step.distance, step_geometry]; }) diff --git a/app/assets/javascripts/index/directions/fossgis_valhalla.js b/app/assets/javascripts/index/directions/fossgis_valhalla.js index d5e781e4a..48b3ad64a 100644 --- a/app/assets/javascripts/index/directions/fossgis_valhalla.js +++ b/app/assets/javascripts/index/directions/fossgis_valhalla.js @@ -53,15 +53,14 @@ precision: 6 }); - const legSteps = leg.maneuvers.map(function (manoeuvre, idx) { - const num = `${idx + 1}. `; + const legSteps = leg.maneuvers.map(function (manoeuvre) { const lineseg = legLine .slice(manoeuvre.begin_shape_index, manoeuvre.end_shape_index + 1) .map(([lat, lng]) => ({ lat, lng })); return [ lineseg[0], INSTR_MAP[manoeuvre.type], - num + manoeuvre.instruction, + manoeuvre.instruction, manoeuvre.length * 1000, lineseg ]; diff --git a/app/assets/javascripts/index/directions/graphhopper.js b/app/assets/javascripts/index/directions/graphhopper.js index b11c49472..20de38e37 100644 --- a/app/assets/javascripts/index/directions/graphhopper.js +++ b/app/assets/javascripts/index/directions/graphhopper.js @@ -21,15 +21,14 @@ function _processDirections(path) { const line = L.PolylineUtil.decode(path.points); - const steps = path.instructions.map(function (instr, i) { - const num = `${i + 1}. `; + const steps = path.instructions.map(function (instr) { const lineseg = line .slice(instr.interval[0], instr.interval[1] + 1) .map(([lat, lng]) => ({ lat, lng })); return [ lineseg[0], GH_INSTR_MAP[instr.sign], - num + instr.text, + instr.text, instr.distance, lineseg ]; // TODO does graphhopper map instructions onto line indices?