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