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 leaflet.polyline
9 //= require index/search
10 //= require index/browse
11 //= require index/export
12 //= require index/notes
13 //= require index/history
14 //= require index/note
15 //= require index/new_note
18 //= require_tree ./routing_engines
23 OSM.loadSidebarContent = function(path, callback) {
24 clearTimeout(loaderTimeout);
26 loaderTimeout = setTimeout(function() {
27 $('#sidebar_loader').show();
30 // IE<10 doesn't respect Vary: X-Requested-With header, so
31 // prevent caching the XHR response as a full-page URL.
32 if (path.indexOf('?') >= 0) {
44 complete: function(xhr) {
45 clearTimeout(loaderTimeout);
47 $('#sidebar_loader').hide();
49 var content = $(xhr.responseText);
51 if (xhr.getResponseHeader('X-Page-Title')) {
52 var title = xhr.getResponseHeader('X-Page-Title');
53 document.title = decodeURIComponent(escape(title));
57 .find('link[type="application/atom+xml"]')
61 .append(content.filter('link[type="application/atom+xml"]'));
63 $('#sidebar_content').html(content.not('link[type="application/atom+xml"]'));
73 $(document).ready(function () {
74 var params = OSM.mapParams();
76 var map = new L.OSM.Map("map", {
81 map.attributionControl.setPrefix('');
83 map.updateLayers(params.layers);
85 map.on("baselayerchange", function (e) {
86 if (map.getZoom() > e.layer.options.maxZoom) {
87 map.setView(map.getCenter(), e.layer.options.maxZoom, { reset: true });
91 var position = $('html').attr('dir') === 'rtl' ? 'topleft' : 'topright';
93 L.OSM.zoom({position: position})
99 title: I18n.t('javascripts.map.locate.title'),
100 popup: I18n.t('javascripts.map.locate.popup')
104 var sidebar = L.OSM.sidebar('#map-ui')
109 layers: map.baseLayers,
132 if (OSM.STATUS != 'api_offline' && OSM.STATUS != 'database_offline') {
133 initializeNotes(map);
134 if (params.layers.indexOf(map.noteLayer.options.code) >= 0) {
135 map.addLayer(map.noteLayer);
138 initializeBrowse(map);
139 if (params.layers.indexOf(map.dataLayer.options.code) >= 0) {
140 map.addLayer(map.dataLayer);
144 $('.leaflet-control .control-button').tooltip({placement: 'left', container: 'body'});
146 var expiry = new Date();
147 expiry.setYear(expiry.getFullYear() + 10);
149 map.on('moveend layeradd layerremove', function() {
151 map.getCenter().wrap(),
156 $.removeCookie("_osm_location");
157 $.cookie("_osm_location", cookieContent(map), { expires: expiry, path: "/" });
160 if ($.cookie('_osm_welcome') == 'hide') {
161 $('.welcome').hide();
164 $('.welcome .close').on('click', function() {
165 $('.welcome').hide();
166 $.cookie("_osm_welcome", 'hide', { expires: expiry });
170 map.on('layeradd', function (e) {
171 if (e.layer.options) {
172 var goal = OSM.PIWIK.goals[e.layer.options.keyid];
175 $('body').trigger('piwikgoal', goal);
182 map.fitBounds(params.bounds);
184 map.setView([params.lat, params.lon], params.zoom);
187 var marker = L.marker([0, 0], {icon: getUserIcon()});
190 marker.setLatLng([params.mlat, params.mlon]).addTo(map);
193 $("#homeanchor").on("click", function(e) {
196 var data = $(this).data(),
197 center = L.latLng(data.lat, data.lon);
199 map.setView(center, data.zoom);
200 marker.setLatLng(center).addTo(map);
203 $("a[data-editor=remote]").click(function(e) {
204 var params = OSM.mapParams(this.search);
205 remoteEditHandler(map.getBounds(), params.object);
209 if (OSM.params().edit_help) {
214 title: I18n.t('javascripts.edit_help')
218 $('body').one('click', function() {
219 $('#editanchor').tooltip('hide');
223 OSM.Index = function(map) {
226 page.pushstate = function() {
227 $("#content").addClass("overlay-sidebar");
228 map.invalidateSize({pan: false})
229 .panBy([-350, 0], {animate: false});
230 document.title = I18n.t('layouts.project_name.title');
233 page.load = function() {
234 if (!("autofocus" in document.createElement("input"))) {
235 $("#sidebar .search_form input[name=query]").focus();
237 return map.getState();
240 page.popstate = function() {
241 $("#content").addClass("overlay-sidebar");
242 map.invalidateSize({pan: false});
243 document.title = I18n.t('layouts.project_name.title');
246 page.unload = function() {
247 map.panBy([350, 0], {animate: false});
248 $("#content").removeClass("overlay-sidebar");
249 map.invalidateSize({pan: false});
255 OSM.Browse = function(map, type) {
258 page.pushstate = page.popstate = function(path, id) {
259 OSM.loadSidebarContent(path, function() {
264 page.load = function(path, id) {
265 addObject(type, id, true);
268 function addObject(type, id, center) {
269 var bounds = map.addObject({type: type, id: parseInt(id)}, function(bounds) {
270 if (!window.location.hash && bounds.isValid()) {
271 OSM.router.moveListenerOff();
272 map.once('moveend', OSM.router.moveListenerOn);
273 if (center || !map.getBounds().contains(bounds)) map.fitBounds(bounds);
278 page.unload = function() {
285 var history = OSM.History(map);
287 OSM.router = OSM.Router(map, {
289 "/search": OSM.Search(map),
290 "/export": OSM.Export(map),
291 "/note/new": OSM.NewNote(map),
292 "/history/friends": history,
293 "/history/nearby": history,
295 "/user/:display_name/history": history,
296 "/note/:id": OSM.Note(map),
297 "/node/:id(/history)": OSM.Browse(map, 'node'),
298 "/way/:id(/history)": OSM.Browse(map, 'way'),
299 "/relation/:id(/history)": OSM.Browse(map, 'relation'),
300 "/changeset/:id": OSM.Browse(map, 'changeset')
303 if (OSM.preferred_editor == "remote" && document.location.pathname == "/edit") {
304 remoteEditHandler(map.getBounds(), params.object);
305 OSM.router.setCurrentPath("/");
310 $(document).on("click", "a", function(e) {
311 if (e.isDefaultPrevented() || e.isPropagationStopped())
314 // Open links in a new tab as normal.
315 if (e.which > 1 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey)
318 // Ignore cross-protocol and cross-origin links.
319 if (location.protocol !== this.protocol || location.host !== this.host)
322 if (OSM.router.route(this.pathname + this.search + this.hash))
326 $(".search_form").on("submit", function(e) {
328 if ($(".query_wrapper.routing").is(":visible")) {
330 OSM.routing.requestRoute(true, false);
333 $("header").addClass("closed");
334 var query = $(this).find("input[name=query]").val();
336 OSM.router.route("/search?query=" + encodeURIComponent(query) + OSM.formatHash(map));
338 OSM.router.route("/" + OSM.formatHash(map));
343 $(".describe_location").on("click", function(e) {
345 var precision = zoomPrecision(map.getZoom());
346 OSM.router.route("/search?query=" + encodeURIComponent(
347 map.getCenter().lat.toFixed(precision) + "," +
348 map.getCenter().lng.toFixed(precision)));
351 OSM.routing = OSM.Routing(map,'OSM.routing',$('.query_wrapper.routing'));
352 OSM.routing.chooseEngine('javascripts.directions.engines.osrm_car');
354 $(".get_directions").on("click",function(e) {
357 $(".routing").show();
358 $(".query_wrapper.routing [name=route_from]").focus();
359 $("#map").on('dragend dragover',function(e) { e.preventDefault(); });
360 $("#map").on('drop',function(e) { OSM.routing.handleDrop(e); e.preventDefault(); });
361 $(".routing_marker").on('dragstart',function(e) {
362 e.originalEvent.dataTransfer.effectAllowed = 'move';
363 e.originalEvent.dataTransfer.setData('id', this.id);
367 $(".close_directions").on("click",function(e) {
370 $(".routing").hide();
372 $("#map").off('dragend drop dragover');
373 $(".routing_marker").off('dragstart');
374 $(".query_wrapper.search [name=query]").focus();