2 // Doesn't yet support hints
 
   5   function FOSSGISOSRMEngine(modeId, vehicleType) {
 
   8     function getInstructionText(step) {
 
   9       const INSTRUCTION_TEMPLATE = {
 
  10         "continue": "continue",
 
  11         "merge right": "merge_right",
 
  12         "merge left": "merge_left",
 
  13         "off ramp right": "offramp_right",
 
  14         "off ramp left": "offramp_left",
 
  15         "on ramp right": "onramp_right",
 
  16         "on ramp left": "onramp_left",
 
  17         "fork right": "fork_right",
 
  18         "fork left": "fork_left",
 
  19         "end of road right": "endofroad_right",
 
  20         "end of road left": "endofroad_left",
 
  21         "turn straight": "continue",
 
  22         "turn slight right": "slight_right",
 
  23         "turn right": "turn_right",
 
  24         "turn sharp right": "sharp_right",
 
  25         "turn uturn": "uturn",
 
  26         "turn sharp left": "sharp_left",
 
  27         "turn left": "turn_left",
 
  28         "turn slight left": "slight_left",
 
  29         "roundabout": "roundabout",
 
  30         "rotary": "roundabout",
 
  31         "exit roundabout": "exit_roundabout",
 
  32         "exit rotary": "exit_roundabout",
 
  35         "arrive": "destination"
 
  37       function numToWord(num) {
 
  38         return ["first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth"][num - 1];
 
  41       const instrPrefix = "javascripts.directions.instructions.";
 
  42       let template = instrPrefix + INSTRUCTION_TEMPLATE[step.maneuverId];
 
  44       const destinations = "<b>" + step.destinations + "</b>";
 
  48       if (step.name && step.ref) {
 
  49         name = "<b>" + step.name + " (" + step.ref + ")</b>";
 
  50       } else if (step.name) {
 
  51         name = "<b>" + step.name + "</b>";
 
  52       } else if (step.ref) {
 
  53         name = "<b>" + step.ref + "</b>";
 
  55         name = OSM.i18n.t(instrPrefix + "unnamed");
 
  59       if (step.maneuver.type.match(/^exit (rotary|roundabout)$/)) {
 
  60         return OSM.i18n.t(template, { name: name });
 
  62       if (step.maneuver.type.match(/^(rotary|roundabout)$/)) {
 
  63         if (!step.maneuver.exit) {
 
  64           return OSM.i18n.t(template + "_without_exit", { name: name });
 
  66         if (step.maneuver.exit > 10) {
 
  67           return OSM.i18n.t(template + "_with_exit", { exit: step.maneuver.exit, name: name });
 
  69         return OSM.i18n.t(template + "_with_exit_ordinal", { exit: OSM.i18n.t(instrPrefix + "exit_counts." + numToWord(step.maneuver.exit)), name: name });
 
  71       if (!step.maneuver.type.match(/^(on ramp|off ramp)$/)) {
 
  72         return OSM.i18n.t(template + "_without_exit", { name: name });
 
  75       if (step.exits && step.maneuver.type.match(/^(off ramp)$/)) params.exit = step.exits;
 
  76       if (step.destinations) params.directions = destinations;
 
  77       if (namedRoad) params.directions = name;
 
  78       if (Object.keys(params).length > 0) {
 
  79         template = template + "_with_" + Object.keys(params).join("_");
 
  81       return OSM.i18n.t(template, params);
 
  84     function _processDirections(leg) {
 
  85       function getManeuverId({ maneuver, mode, intersections }) {
 
  86         // special case handling
 
  87         if (mode === "ferry") return "ferry";
 
  88         if (intersections.some(i => i.classes?.includes("ferry"))) return "ferry";
 
  89         switch (maneuver.type) {
 
  95             return maneuver.type + " " + (maneuver.modifier.indexOf("left") >= 0 ? "left" : "right");
 
 100           case "exit roundabout":
 
 102             return maneuver.type;
 
 103           case "roundabout turn":
 
 105             return "turn " + maneuver.modifier;
 
 106             // for unknown types the fallback is turn
 
 108             return "turn " + maneuver.modifier;
 
 112         "continue": "straight",
 
 113         "merge right": "merge-right",
 
 114         "merge left": "merge-left",
 
 115         "off ramp right": "exit-right",
 
 116         "off ramp left": "exit-left",
 
 117         "on ramp right": "right",
 
 118         "on ramp left": "left",
 
 119         "fork right": "fork-right",
 
 120         "fork left": "fork-left",
 
 121         "end of road right": "end-of-road-right",
 
 122         "end of road left": "end-of-road-left",
 
 123         "turn straight": "straight",
 
 124         "turn slight right": "slight-right",
 
 125         "turn right": "right",
 
 126         "turn sharp right": "sharp-right",
 
 127         "turn uturn": "u-turn-left",
 
 128         "turn slight left": "slight-left",
 
 130         "turn sharp left": "sharp-left",
 
 131         "roundabout": "roundabout",
 
 132         "rotary": "roundabout",
 
 133         "exit roundabout": "roundabout",
 
 134         "exit rotary": "roundabout",
 
 137         "arrive": "destination"
 
 140       for (const step of leg.steps) step.maneuverId = getManeuverId(step);
 
 142       const steps = leg.steps.map(step => [
 
 143         ICON_MAP[step.maneuverId],
 
 144         getInstructionText(step),
 
 146         L.PolylineUtil.decode(step.geometry, { precision: 5 })
 
 150         line: steps.flatMap(step => step[3]),
 
 152         distance: leg.distance,
 
 154         credit: "OSRM (FOSSGIS)",
 
 155         creditlink: "https://routing.openstreetmap.de/about.html"
 
 161       provider: "fossgis_osrm",
 
 164       getRoute: function (points, signal) {
 
 165         const query = new URLSearchParams({
 
 167           geometries: "polyline",
 
 171         if (cachedHints.length === points.length) {
 
 172           query.set("hints", cachedHints.join(";"));
 
 178         const req_path = "routed-" + vehicleType + "/route/v1/driving/" + points.map(p => p.lng + "," + p.lat).join(";");
 
 180         return fetch(OSM.FOSSGIS_OSRM_URL + req_path + "?" + query, { signal })
 
 181           .then(response => response.json())
 
 183             if (response.code !== "Ok") throw new Error();
 
 184             cachedHints = response.waypoints.map(wp => wp.hint);
 
 185             return _processDirections(response.routes[0].legs[0]);
 
 191   OSM.Directions.addEngine(new FOSSGISOSRMEngine("car", "car"), true);
 
 192   OSM.Directions.addEngine(new FOSSGISOSRMEngine("bicycle", "bike"), true);
 
 193   OSM.Directions.addEngine(new FOSSGISOSRMEngine("foot", "foot"), true);