2 // Doesn't yet support hints
 
   5   function FOSSGISOSRMEngine(modeId, vehicleType) {
 
   8     function _processDirections(route) {
 
   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",
 
  34         "arrive": "destination"
 
  37         "continue": "straight",
 
  38         "merge right": "merge-right",
 
  39         "merge left": "merge-left",
 
  40         "off ramp right": "exit-right",
 
  41         "off ramp left": "exit-left",
 
  42         "on ramp right": "right",
 
  43         "on ramp left": "left",
 
  44         "fork right": "fork-right",
 
  45         "fork left": "fork-left",
 
  46         "end of road right": "end-of-road-right",
 
  47         "end of road left": "end-of-road-left",
 
  48         "turn straight": "straight",
 
  49         "turn slight right": "slight-right",
 
  50         "turn right": "right",
 
  51         "turn sharp right": "sharp-right",
 
  52         "turn uturn": "u-turn-left",
 
  53         "turn slight left": "slight-left",
 
  55         "turn sharp left": "sharp-left",
 
  56         "roundabout": "roundabout",
 
  57         "rotary": "roundabout",
 
  58         "exit roundabout": "roundabout",
 
  59         "exit rotary": "roundabout",
 
  61         "arrive": "destination"
 
  63       function numToWord(num) {
 
  64         return ["first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth"][num - 1];
 
  66       function getManeuverId(maneuver) {
 
  67         // special case handling
 
  68         switch (maneuver.type) {
 
  74             return maneuver.type + " " + (maneuver.modifier.indexOf("left") >= 0 ? "left" : "right");
 
  79           case "exit roundabout":
 
  82           case "roundabout turn":
 
  84             return "turn " + maneuver.modifier;
 
  85             // for unknown types the fallback is turn
 
  87             return "turn " + maneuver.modifier;
 
  91       const steps = route.legs.flatMap(
 
  92         leg => leg.steps.map(function (step, idx) {
 
  93           const maneuver_id = getManeuverId(step.maneuver);
 
  95           const instrPrefix = "javascripts.directions.instructions.";
 
  96           let template = instrPrefix + INSTRUCTION_TEMPLATE[maneuver_id];
 
  98           const step_geometry = L.PolylineUtil.decode(step.geometry, { precision: 5 }).map(L.latLng);
 
 100           let instText = "<b>" + (idx + 1) + ".</b> ";
 
 101           const destinations = "<b>" + step.destinations + "</b>";
 
 102           let namedRoad = true;
 
 105           if (step.name && step.ref) {
 
 106             name = "<b>" + step.name + " (" + step.ref + ")</b>";
 
 107           } else if (step.name) {
 
 108             name = "<b>" + step.name + "</b>";
 
 109           } else if (step.ref) {
 
 110             name = "<b>" + step.ref + "</b>";
 
 112             name = OSM.i18n.t(instrPrefix + "unnamed");
 
 116           if (step.maneuver.type.match(/^exit (rotary|roundabout)$/)) {
 
 117             instText += OSM.i18n.t(template, { name: name });
 
 118           } else if (step.maneuver.type.match(/^(rotary|roundabout)$/)) {
 
 119             if (step.maneuver.exit) {
 
 120               if (step.maneuver.exit <= 10) {
 
 121                 instText += OSM.i18n.t(template + "_with_exit_ordinal", { exit: OSM.i18n.t(instrPrefix + "exit_counts." + numToWord(step.maneuver.exit)), name: name });
 
 123                 instText += OSM.i18n.t(template + "_with_exit", { exit: step.maneuver.exit, name: name });
 
 126               instText += OSM.i18n.t(template + "_without_exit", { name: name });
 
 128           } else if (step.maneuver.type.match(/^(on ramp|off ramp)$/)) {
 
 130             if (step.exits && step.maneuver.type.match(/^(off ramp)$/)) params.exit = step.exits;
 
 131             if (step.destinations) params.directions = destinations;
 
 132             if (namedRoad) params.directions = name;
 
 133             if (Object.keys(params).length > 0) {
 
 134               template = template + "_with_" + Object.keys(params).join("_");
 
 136             instText += OSM.i18n.t(template, params);
 
 138             instText += OSM.i18n.t(template + "_without_exit", { name: name });
 
 140           return [[step.maneuver.location[1], step.maneuver.location[0]], ICON_MAP[maneuver_id], instText, step.distance, step_geometry];
 
 145         line: steps.flatMap(step => step[4]),
 
 147         distance: route.distance,
 
 154       provider: "fossgis_osrm",
 
 155       creditline: "<a href=\"https://routing.openstreetmap.de/about.html\" target=\"_blank\">OSRM (FOSSGIS)</a>",
 
 158       getRoute: function (points, signal) {
 
 159         const query = new URLSearchParams({
 
 161           geometries: "polyline",
 
 165         if (cachedHints.length === points.length) {
 
 166           query.set("hints", cachedHints.join(";"));
 
 172         const req_path = "routed-" + vehicleType + "/route/v1/driving/" + points.map(p => p.lng + "," + p.lat).join(";");
 
 174         return fetch(OSM.FOSSGIS_OSRM_URL + req_path + "?" + query, { signal })
 
 175           .then(response => response.json())
 
 177             if (response.code !== "Ok") throw new Error();
 
 178             cachedHints = response.waypoints.map(wp => wp.hint);
 
 179             return _processDirections(response.routes[0]);
 
 185   OSM.Directions.addEngine(new FOSSGISOSRMEngine("car", "car"), true);
 
 186   OSM.Directions.addEngine(new FOSSGISOSRMEngine("bicycle", "bike"), true);
 
 187   OSM.Directions.addEngine(new FOSSGISOSRMEngine("foot", "foot"), true);