]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/contextmenu.js
Merge remote-tracking branch 'upstream/pull/5936'
[rails.git] / app / assets / javascripts / index / contextmenu.js
1 OSM.initializeContextMenu = function (map) {
2   map.contextmenu.addItem({
3     text: OSM.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: OSM.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: OSM.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: OSM.i18n.t("javascripts.context.show_address"),
37     callback: function describeLocation(e) {
38       const zoom = map.getZoom();
39       const [lat, lon] = OSM.cropLocation(e.latlng, zoom);
40
41       OSM.router.route("/search?" + new URLSearchParams({ lat, lon, zoom }));
42     }
43   });
44
45   map.contextmenu.addItem({
46     text: OSM.i18n.t("javascripts.context.query_features"),
47     callback: function queryFeatures(e) {
48       const [lat, lon] = OSM.cropLocation(e.latlng, map.getZoom());
49
50       OSM.router.route("/query?" + new URLSearchParams({ lat, lon }));
51     }
52   });
53
54   map.contextmenu.addItem({
55     text: OSM.i18n.t("javascripts.context.centre_map"),
56     callback: function centreMap(e) {
57       map.panTo(e.latlng);
58     }
59   });
60
61   map.on("mousedown", function (e) {
62     if (e.originalEvent.shiftKey) map.contextmenu.disable();
63     else map.contextmenu.enable();
64   });
65
66   function getDirectionsEndpointCoordinatesFromInput(input) {
67     if (input.attr("data-lat") && input.attr("data-lon")) {
68       return input.attr("data-lat") + "," + input.attr("data-lon");
69     }
70     return $(input).val();
71   }
72
73   const 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 };