]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/contextmenu.js
Improve context menu initialisation to avoid namespace pollution
[rails.git] / app / assets / javascripts / index / contextmenu.js
1 OSM.initializeContextMenu = function (map) {
2   map.contextmenu.addItem({
3     text: "Directions from here",
4     callback: function directionsFromHere(e) {
5       var precision = OSM.zoomPrecision(map.getZoom()),
6           latlng = e.latlng.wrap(),
7           lat = latlng.lat.toFixed(precision),
8           lng = latlng.lng.toFixed(precision);
9
10       OSM.router.route("/directions?" + querystring.stringify({
11         route: lat + "," + lng + ";" + $("#route_to").val()
12       }));
13     }
14   });
15
16   map.contextmenu.addItem({
17     text: "Directions to here",
18     callback: function directionsToHere(e) {
19       var precision = OSM.zoomPrecision(map.getZoom()),
20           latlng = e.latlng.wrap(),
21           lat = latlng.lat.toFixed(precision),
22           lng = latlng.lng.toFixed(precision);
23
24       OSM.router.route("/directions?" + querystring.stringify({
25         route: $("#route_from").val() + ";" + lat + "," + lng
26       }));
27     }
28   });
29
30   map.contextmenu.addItem({
31     text: "Add a note here",
32     callback: function addNoteHere(e) {
33       // I'd like this, instead of panning, to pass a query parameter about where to place the marker
34       map.panTo(e.latlng.wrap(), {animate: false});
35       OSM.router.route("/note/new");
36     }
37   });
38
39   map.contextmenu.addItem({
40     text: "Show address",
41     callback: function describeLocation(e) {
42       var precision = OSM.zoomPrecision(map.getZoom()),
43           latlng = e.latlng.wrap(),
44           lat = latlng.lat.toFixed(precision),
45           lng = latlng.lng.toFixed(precision);
46
47       OSM.router.route("/search?query=" + encodeURIComponent(lat + "," + lng));
48     }
49   });
50
51   map.contextmenu.addItem({
52     text: "Query features",
53     callback: function queryFeatures(e) {
54       var precision = OSM.zoomPrecision(map.getZoom()),
55           latlng = e.latlng.wrap(),
56           lat = latlng.lat.toFixed(precision),
57           lng = latlng.lng.toFixed(precision);
58
59       OSM.router.route("/query?lat=" + lat + "&lon=" + lng);
60     }
61   });
62
63   map.contextmenu.addItem({
64     text: "Centre map here",
65     callback: function centreMap(e) {
66       map.panTo(e.latlng);
67     }
68   });
69
70   map.on("mousedown", function (e) {
71     if (e.shiftKey) map.contextmenu.disable();
72   }).on("mouseup", function () {
73     map.contextmenu.enable();
74   });
75
76   var updateMenu = function updateMenu () {
77     map.contextmenu.setDisabled(2, map.getZoom() < 12);
78     map.contextmenu.setDisabled(4, map.getZoom() < 14);
79   };
80
81   map.on("zoomend", updateMenu);
82   updateMenu();
83 };