]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index.js
Fix undefined=undefined in query strings
[rails.git] / app / assets / javascripts / index.js
1 //= require_self
2 //= require leaflet.sidebar
3 //= require leaflet.locate
4 //= require leaflet.layers
5 //= require leaflet.key
6 //= require leaflet.note
7 //= require leaflet.share
8 //= require index/browse
9 //= require index/export
10 //= require index/notes
11
12 $(document).ready(function () {
13   var params = OSM.mapParams();
14
15   var map = L.map("map", {
16     zoomControl: false,
17     layerControl: false
18   });
19
20   map.attributionControl.setPrefix('');
21
22   var layers = [
23     new L.OSM.Mapnik({
24       attribution: '',
25       code: "M",
26       keyid: "mapnik",
27       name: I18n.t("javascripts.map.base.standard")
28     }),
29     new L.OSM.CycleMap({
30       attribution: "Tiles courtesy of <a href='http://www.opencyclemap.org/' target='_blank'>Andy Allan</a>",
31       code: "C",
32       keyid: "cyclemap",
33       name: I18n.t("javascripts.map.base.cycle_map")
34     }),
35     new L.OSM.TransportMap({
36       attribution: "Tiles courtesy of <a href='http://www.opencyclemap.org/' target='_blank'>Andy Allan</a>",
37       code: "T",
38       keyid: "transportmap",
39       name: I18n.t("javascripts.map.base.transport_map")
40     }),
41     new L.OSM.MapQuestOpen({
42       attribution: "Tiles courtesy of <a href='http://www.mapquest.com/' target='_blank'>MapQuest</a> <img src='http://developer.mapquest.com/content/osm/mq_logo.png'>",
43       code: "Q",
44       keyid: "mapquest",
45       name: I18n.t("javascripts.map.base.mapquest")
46     })
47   ];
48
49   layers[0].addTo(map);
50
51   map.noteLayer = new L.LayerGroup({code: 'N'});
52   map.dataLayer = new L.OSM.DataLayer(null);
53
54   $("#map").on("resized", function () {
55     map.invalidateSize();
56   });
57
58   L.OSM.zoom({position: 'topright'})
59     .addTo(map);
60
61   L.control.locate({position: 'topright'})
62     .addTo(map);
63
64   var sidebar = L.OSM.sidebar('#map-ui');
65
66   L.OSM.layers({
67     position: 'topright',
68     layers: layers,
69     sidebar: sidebar
70   }).addTo(map);
71
72   L.OSM.key({
73     position: 'topright',
74     sidebar: sidebar
75   }).addTo(map);
76
77   L.OSM.share({
78     getShortUrl: getShortUrl,
79     getUrl: getUrl,
80     sidebar: sidebar,
81     short: true
82   }).addTo(map);
83
84   L.OSM.note({
85     position: 'topright',
86     sidebar: sidebar
87   }).addTo(map);
88
89   L.control.scale()
90     .addTo(map);
91
92   map.on('moveend layeradd layerremove', updateLocation);
93
94   map.markerLayer = L.layerGroup().addTo(map);
95
96   if (!params.object_zoom) {
97     if (params.bbox) {
98       var bbox = L.latLngBounds([params.minlat, params.minlon],
99                                 [params.maxlat, params.maxlon]);
100
101       map.fitBounds(bbox);
102
103       if (params.box) {
104         L.rectangle(bbox, {
105           weight: 2,
106           color: '#e90',
107           fillOpacity: 0
108         }).addTo(map);
109       }
110     } else {
111       map.setView([params.lat, params.lon], params.zoom);
112     }
113   }
114
115   if (params.layers) {
116     var foundLayer = false;
117     for (var i = 0; i < layers.length; i++) {
118       if (params.layers.indexOf(layers[i].options.code) >= 0) {
119         map.addLayer(layers[i]);
120         foundLayer = true;
121       } else {
122         map.removeLayer(layers[i]);
123       }
124     }
125     if (!foundLayer) {
126       map.addLayer(layers[0]);
127     }
128   }
129
130   if (params.marker) {
131     L.marker([params.mlat, params.mlon], {icon: getUserIcon()}).addTo(map.markerLayer);
132   }
133
134   if (params.object) {
135     addObjectToMap(params.object, map, { zoom: params.object_zoom });
136   }
137
138   handleResize();
139
140   $("body").on("click", "a.set_position", setPositionLink(map));
141
142   $("a[data-editor=remote]").click(function(e) {
143       remoteEditHandler(map.getBounds());
144       e.preventDefault();
145   });
146
147   if (OSM.preferred_editor == "remote" && $('body').hasClass("site-edit")) {
148     remoteEditHandler(map.getBounds());
149   }
150
151   $(window).resize(handleResize);
152
153   $("#search_form").submit(submitSearch(map));
154
155
156   if ($("#query").val()) {
157     $("#search_form").submit();
158   }
159
160   // Focus the search field for browsers that don't support
161   // the HTML5 'autofocus' attribute
162   if (!("autofocus" in document.createElement("input"))) {
163     $("#query").focus();
164   }
165
166   initializeExport(map);
167   initializeBrowse(map);
168   initializeNotes(map);
169 });
170
171 function updateLocation() {
172   updatelinks(this.getCenter().wrap(),
173       this.getZoom(),
174       this.getLayersCode(),
175       this.getBounds().wrap());
176
177   var expiry = new Date();
178   expiry.setYear(expiry.getFullYear() + 10);
179   $.cookie("_osm_location", cookieContent(this), { expires: expiry });
180 }
181
182 function setPositionLink(map) {
183   return function(e) {
184       var data = $(this).data(),
185           center = L.latLng(data.lat, data.lon);
186
187       if (data.minLon && data.minLat && data.maxLon && data.maxLat) {
188         map.fitBounds([[data.minLat, data.minLon],
189                        [data.maxLat, data.maxLon]]);
190       } else {
191         map.setView(center, data.zoom);
192       }
193
194       if (data.type && data.id) {
195         addObjectToMap(data, map, { zoom: true, style: { opacity: 0.2, fill: false } });
196       }
197
198       map.markerLayer.clearLayers();
199       L.marker(center, {icon: getUserIcon()}).addTo(map.markerLayer);
200
201       return e.preventDefault();
202   };
203 }
204
205 function submitSearch(map) {
206   return function(e) {
207     var bounds = map.getBounds();
208
209     $("#sidebar_title").html(I18n.t('site.sidebar.search_results'));
210     $("#sidebar_content").load($(this).attr("action"), {
211       query: $("#query").val(),
212       minlon: bounds.getWestLng(),
213       minlat: bounds.getSouthLat(),
214       maxlon: bounds.getEastLng(),
215       maxlat: bounds.getNorthLat()
216     }, openSidebar);
217
218     return e.preventDefault();
219   };
220 }