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 leaflet.query
10 //= require leaflet.contextmenu
11 //= require index/search
12 //= require index/browse
13 //= require index/export
14 //= require index/notes
15 //= require index/history
16 //= require index/note
17 //= require index/new_note
18 //= require index/directions
19 //= require index/changeset
20 //= require index/query
23 $(document).ready(function () {
26 OSM.loadSidebarContent = function(path, callback) {
27 map.setSidebarOverlaid(false);
29 clearTimeout(loaderTimeout);
31 loaderTimeout = setTimeout(function() {
32 $('#sidebar_loader').show();
35 // IE<10 doesn't respect Vary: X-Requested-With header, so
36 // prevent caching the XHR response as a full-page URL.
37 if (path.indexOf('?') >= 0) {
49 complete: function(xhr) {
50 clearTimeout(loaderTimeout);
52 $('#sidebar_loader').hide();
54 var content = $(xhr.responseText);
56 if (xhr.getResponseHeader('X-Page-Title')) {
57 var title = xhr.getResponseHeader('X-Page-Title');
58 document.title = decodeURIComponent(title);
62 .find('link[type="application/atom+xml"]')
66 .append(content.filter('link[type="application/atom+xml"]'));
68 $('#sidebar_content').html(content.not('link[type="application/atom+xml"]'));
77 var params = OSM.mapParams();
79 // TODO consider using a separate js file for the context menu additions
80 var context_describe = function(e){
81 var precision = OSM.zoomPrecision(map.getZoom());
82 OSM.router.route("/search?query=" + encodeURIComponent(
83 e.latlng.lat.toFixed(precision) + "," + e.latlng.lng.toFixed(precision)
87 var context_directionsfrom = function(e){
88 var precision = OSM.zoomPrecision(map.getZoom());
89 OSM.router.route("/directions?" + querystring.stringify({
90 route: e.latlng.lat.toFixed(precision) + ',' + e.latlng.lng.toFixed(precision) + ';' + $('#route_to').val()
94 var context_directionsto = function(e){
95 var precision = OSM.zoomPrecision(map.getZoom());
96 OSM.router.route("/directions?" + querystring.stringify({
97 route: $('#route_from').val() + ';' + e.latlng.lat.toFixed(precision) + ',' + e.latlng.lng.toFixed(precision)
101 var context_addnote = function(e){
102 // I'd like this, instead of panning, to pass a query parameter about where to place the marker
103 map.panTo(e.latlng, {animate: false});
104 OSM.router.route('/note/new');
107 var context_centrehere = function(e){
111 var context_queryhere = function(e) {
112 var precision = OSM.zoomPrecision(map.getZoom()),
113 latlng = e.latlng.wrap(),
114 lat = latlng.lat.toFixed(precision),
115 lng = latlng.lng.toFixed(precision);
117 OSM.router.route("/query?lat=" + lat + "&lon=" + lng);
120 // TODO internationalisation of the context menu strings
121 var map = new L.OSM.Map("map", {
125 contextmenuWidth: 140,
127 text: 'Directions from here',
128 callback: context_directionsfrom
130 text: 'Directions to here',
131 callback: context_directionsto
133 text: 'Add a note here',
134 callback: context_addnote
136 text: 'Show address',
137 callback: context_describe
139 text: 'Query features',
140 callback: context_queryhere
142 text: 'Centre map here',
143 callback: context_centrehere
147 $(document).on('mousedown', function(e){
149 map.contextmenu.disable(); // on firefox, shift disables our contextmenu. we explicitly do this for all browsers.
151 map.contextmenu.enable();
152 // we also decide whether to disable some options that only like high zoom
153 map.contextmenu.setDisabled(3, map.getZoom() < 12);
154 map.contextmenu.setDisabled(5, map.getZoom() < 14);
158 map.attributionControl.setPrefix('');
160 map.updateLayers(params.layers);
162 map.on("baselayerchange", function (e) {
163 if (map.getZoom() > e.layer.options.maxZoom) {
164 map.setView(map.getCenter(), e.layer.options.maxZoom, { reset: true });
168 var position = $('html').attr('dir') === 'rtl' ? 'topleft' : 'topright';
170 L.OSM.zoom({position: position})
176 title: I18n.t('javascripts.map.locate.title'),
177 popup: I18n.t('javascripts.map.locate.popup')
181 var sidebar = L.OSM.sidebar('#map-ui')
186 layers: map.baseLayers,
214 if (OSM.STATUS !== 'api_offline' && OSM.STATUS !== 'database_offline') {
215 OSM.initializeNotes(map);
216 if (params.layers.indexOf(map.noteLayer.options.code) >= 0) {
217 map.addLayer(map.noteLayer);
220 OSM.initializeBrowse(map);
221 if (params.layers.indexOf(map.dataLayer.options.code) >= 0) {
222 map.addLayer(map.dataLayer);
226 var placement = $('html').attr('dir') === 'rtl' ? 'right' : 'left';
227 $('.leaflet-control .control-button').tooltip({placement: placement, container: 'body'});
229 var expiry = new Date();
230 expiry.setYear(expiry.getFullYear() + 10);
232 map.on('moveend layeradd layerremove', function() {
234 map.getCenter().wrap(),
239 $.removeCookie("_osm_location");
240 $.cookie("_osm_location", OSM.locationCookie(map), { expires: expiry, path: "/" });
243 if ($.cookie('_osm_welcome') === 'hide') {
244 $('.welcome').hide();
247 $('.welcome .close').on('click', function() {
248 $('.welcome').hide();
249 $.cookie("_osm_welcome", 'hide', { expires: expiry });
253 map.on('layeradd', function (e) {
254 if (e.layer.options) {
255 var goal = OSM.PIWIK.goals[e.layer.options.keyid];
258 $('body').trigger('piwikgoal', goal);
265 map.fitBounds(params.bounds);
267 map.setView([params.lat, params.lon], params.zoom);
270 var marker = L.marker([0, 0], {icon: OSM.getUserIcon()});
273 marker.setLatLng([params.mlat, params.mlon]).addTo(map);
276 $("#homeanchor").on("click", function(e) {
279 var data = $(this).data(),
280 center = L.latLng(data.lat, data.lon);
282 map.setView(center, data.zoom);
283 marker.setLatLng(center).addTo(map);
286 function remoteEditHandler(bbox, object) {
288 url = document.location.protocol === "https:" ?
289 "https://127.0.0.1:8112/load_and_zoom?" :
290 "http://127.0.0.1:8111/load_and_zoom?",
292 left: bbox.getWest() - 0.0001,
293 top: bbox.getNorth() + 0.0001,
294 right: bbox.getEast() + 0.0001,
295 bottom: bbox.getSouth() - 0.0001
298 if (object) query.select = object.type + object.id;
300 var iframe = $('<iframe>')
303 .attr("src", url + querystring.stringify(query))
304 .on('load', function() {
309 setTimeout(function () {
311 alert(I18n.t('site.index.remote_failed'));
319 $("a[data-editor=remote]").click(function(e) {
320 var params = OSM.mapParams(this.search);
321 remoteEditHandler(map.getBounds(), params.object);
325 if (OSM.params().edit_help) {
330 title: I18n.t('javascripts.edit_help')
334 $('body').one('click', function() {
335 $('#editanchor').tooltip('hide');
339 OSM.Index = function(map) {
342 page.pushstate = page.popstate = function() {
343 map.setSidebarOverlaid(true);
344 document.title = I18n.t('layouts.project_name.title');
347 page.load = function() {
348 var params = querystring.parse(location.search.substring(1));
350 $("#sidebar .search_form input[name=query]").value(params.query);
352 if (!("autofocus" in document.createElement("input"))) {
353 $("#sidebar .search_form input[name=query]").focus();
355 return map.getState();
361 OSM.Browse = function(map, type) {
364 page.pushstate = page.popstate = function(path, id) {
365 OSM.loadSidebarContent(path, function() {
370 page.load = function(path, id) {
371 addObject(type, id, true);
374 function addObject(type, id, center) {
375 map.addObject({type: type, id: parseInt(id)}, function(bounds) {
376 if (!window.location.hash && bounds.isValid() &&
377 (center || !map.getBounds().contains(bounds))) {
378 OSM.router.withoutMoveListener(function () {
379 map.fitBounds(bounds);
385 page.unload = function() {
392 var history = OSM.History(map);
394 OSM.router = OSM.Router(map, {
396 "/search": OSM.Search(map),
397 "/directions": OSM.Directions(map),
398 "/export": OSM.Export(map),
399 "/note/new": OSM.NewNote(map),
400 "/history/friends": history,
401 "/history/nearby": history,
403 "/user/:display_name/history": history,
404 "/note/:id": OSM.Note(map),
405 "/node/:id(/history)": OSM.Browse(map, 'node'),
406 "/way/:id(/history)": OSM.Browse(map, 'way'),
407 "/relation/:id(/history)": OSM.Browse(map, 'relation'),
408 "/changeset/:id": OSM.Changeset(map),
409 "/query": OSM.Query(map)
412 if (OSM.preferred_editor === "remote" && document.location.pathname === "/edit") {
413 remoteEditHandler(map.getBounds(), params.object);
414 OSM.router.setCurrentPath("/");
419 $(document).on("click", "a", function(e) {
420 if (e.isDefaultPrevented() || e.isPropagationStopped())
423 // Open links in a new tab as normal.
424 if (e.which > 1 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey)
427 // Ignore cross-protocol and cross-origin links.
428 if (location.protocol !== this.protocol || location.host !== this.host)
431 if (OSM.router.route(this.pathname + this.search + this.hash))