]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/contextmenu.js
Don't apply extra encodeURIComponent to lat/lon search params
[rails.git] / app / assets / javascripts / index / contextmenu.js
1 OSM.initializeContextMenu = function (map) {
2   map.contextmenu.addItem({
3     text: I18n.t("javascripts.context.directions_from"),
4     callback: function directionsFromHere(e) {
5       const latlng = OSM.cropLocation(e.latlng, map.getZoom());
6
7       OSM.router.route("/directions?" + new URLSearchParams({
8         from: latlng.join(","),
9         to: getDirectionsEndpointCoordinatesFromInput($("#route_to"))
10       }));
11     }
12   });
13
14   map.contextmenu.addItem({
15     text: I18n.t("javascripts.context.directions_to"),
16     callback: function directionsToHere(e) {
17       const latlng = OSM.cropLocation(e.latlng, map.getZoom());
18
19       OSM.router.route("/directions?" + new URLSearchParams({
20         from: getDirectionsEndpointCoordinatesFromInput($("#route_from")),
21         to: latlng.join(",")
22       }));
23     }
24   });
25
26   map.contextmenu.addItem({
27     text: I18n.t("javascripts.context.add_note"),
28     callback: function addNoteHere(e) {
29       const [lat, lon] = OSM.cropLocation(e.latlng, map.getZoom());
30
31       OSM.router.route("/note/new?" + new URLSearchParams({ lat, lon }));
32     }
33   });
34
35   map.contextmenu.addItem({
36     text: I18n.t("javascripts.context.show_address"),
37     callback: function describeLocation(e) {
38       const [lat, lon] = OSM.cropLocation(e.latlng, map.getZoom());
39
40       OSM.router.route("/search?" + new URLSearchParams({ lat, lon }));
41     }
42   });
43
44   map.contextmenu.addItem({
45     text: I18n.t("javascripts.context.query_features"),
46     callback: function queryFeatures(e) {
47       const [lat, lon] = OSM.cropLocation(e.latlng, map.getZoom());
48
49       OSM.router.route("/query?" + new URLSearchParams({ lat, lon }));
50     }
51   });
52
53   map.contextmenu.addItem({
54     text: I18n.t("javascripts.context.centre_map"),
55     callback: function centreMap(e) {
56       map.panTo(e.latlng);
57     }
58   });
59
60   map.on("mousedown", function (e) {
61     if (e.originalEvent.shiftKey) map.contextmenu.disable();
62     else map.contextmenu.enable();
63   });
64
65   function getDirectionsEndpointCoordinatesFromInput(input) {
66     if (input.attr("data-lat") && input.attr("data-lon")) {
67       return input.attr("data-lat") + "," + input.attr("data-lon");
68     } else {
69       return $(input).val();
70     }
71   }
72
73   var updateMenu = function updateMenu() {
74     map.contextmenu.setDisabled(2, map.getZoom() < 12);
75     map.contextmenu.setDisabled(4, map.getZoom() < 14);
76   };
77
78   map.on("zoomend", updateMenu);
79   updateMenu();
80 };