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 // TODO only allow this if zoomed in enough
102 var context_addnote = function(e){
103 // TODO this currently doesn't work correctly - I think the "route" needs to be chained to ensure it comes once the pan has finished.
104 map.panTo(e.latlng, {animate: false});
105 OSM.router.route('/note/new');
108 var context_centrehere = function(e){
112 var context_queryhere = function(e) {
113 var precision = OSM.zoomPrecision(map.getZoom()),
114 latlng = e.latlng.wrap(),
115 lat = latlng.lat.toFixed(precision),
116 lng = latlng.lng.toFixed(precision);
118 OSM.router.route("/query?lat=" + lat + "&lon=" + lng);
121 // TODO internationalisation of the context menu strings
122 var map = new L.OSM.Map("map", {
126 contextmenuWidth: 140,
128 text: 'Directions from here',
129 callback: context_directionsfrom
131 text: 'Directions to here',
132 callback: context_directionsto
134 text: 'Add a note here',
135 callback: context_addnote
137 text: 'Show address',
138 callback: context_describe
140 text: 'Query features',
141 callback: context_queryhere
143 text: 'Centre map here',
144 callback: context_centrehere
148 $(document).on('mousedown', function(e){
150 map.contextmenu.disable(); // on firefox, shift disables our contextmenu. we explicitly do this for all browsers.
152 map.contextmenu.enable();
156 map.attributionControl.setPrefix('');
158 map.updateLayers(params.layers);
160 map.on("baselayerchange", function (e) {
161 if (map.getZoom() > e.layer.options.maxZoom) {
162 map.setView(map.getCenter(), e.layer.options.maxZoom, { reset: true });
166 var position = $('html').attr('dir') === 'rtl' ? 'topleft' : 'topright';
168 L.OSM.zoom({position: position})
174 title: I18n.t('javascripts.map.locate.title'),
175 popup: I18n.t('javascripts.map.locate.popup')
179 var sidebar = L.OSM.sidebar('#map-ui')
184 layers: map.baseLayers,
212 if (OSM.STATUS !== 'api_offline' && OSM.STATUS !== 'database_offline') {
213 OSM.initializeNotes(map);
214 if (params.layers.indexOf(map.noteLayer.options.code) >= 0) {
215 map.addLayer(map.noteLayer);
218 OSM.initializeBrowse(map);
219 if (params.layers.indexOf(map.dataLayer.options.code) >= 0) {
220 map.addLayer(map.dataLayer);
224 var placement = $('html').attr('dir') === 'rtl' ? 'right' : 'left';
225 $('.leaflet-control .control-button').tooltip({placement: placement, container: 'body'});
227 var expiry = new Date();
228 expiry.setYear(expiry.getFullYear() + 10);
230 map.on('moveend layeradd layerremove', function() {
232 map.getCenter().wrap(),
237 $.removeCookie("_osm_location");
238 $.cookie("_osm_location", OSM.locationCookie(map), { expires: expiry, path: "/" });
241 if ($.cookie('_osm_welcome') === 'hide') {
242 $('.welcome').hide();
245 $('.welcome .close').on('click', function() {
246 $('.welcome').hide();
247 $.cookie("_osm_welcome", 'hide', { expires: expiry });
251 map.on('layeradd', function (e) {
252 if (e.layer.options) {
253 var goal = OSM.PIWIK.goals[e.layer.options.keyid];
256 $('body').trigger('piwikgoal', goal);
263 map.fitBounds(params.bounds);
265 map.setView([params.lat, params.lon], params.zoom);
268 var marker = L.marker([0, 0], {icon: OSM.getUserIcon()});
271 marker.setLatLng([params.mlat, params.mlon]).addTo(map);
274 $("#homeanchor").on("click", function(e) {
277 var data = $(this).data(),
278 center = L.latLng(data.lat, data.lon);
280 map.setView(center, data.zoom);
281 marker.setLatLng(center).addTo(map);
284 function remoteEditHandler(bbox, object) {
286 url = document.location.protocol === "https:" ?
287 "https://127.0.0.1:8112/load_and_zoom?" :
288 "http://127.0.0.1:8111/load_and_zoom?",
290 left: bbox.getWest() - 0.0001,
291 top: bbox.getNorth() + 0.0001,
292 right: bbox.getEast() + 0.0001,
293 bottom: bbox.getSouth() - 0.0001
296 if (object) query.select = object.type + object.id;
298 var iframe = $('<iframe>')
301 .attr("src", url + querystring.stringify(query))
302 .on('load', function() {
307 setTimeout(function () {
309 alert(I18n.t('site.index.remote_failed'));
317 $("a[data-editor=remote]").click(function(e) {
318 var params = OSM.mapParams(this.search);
319 remoteEditHandler(map.getBounds(), params.object);
323 if (OSM.params().edit_help) {
328 title: I18n.t('javascripts.edit_help')
332 $('body').one('click', function() {
333 $('#editanchor').tooltip('hide');
337 OSM.Index = function(map) {
340 page.pushstate = page.popstate = function() {
341 map.setSidebarOverlaid(true);
342 document.title = I18n.t('layouts.project_name.title');
345 page.load = function() {
346 var params = querystring.parse(location.search.substring(1));
348 $("#sidebar .search_form input[name=query]").value(params.query);
350 if (!("autofocus" in document.createElement("input"))) {
351 $("#sidebar .search_form input[name=query]").focus();
353 return map.getState();
359 OSM.Browse = function(map, type) {
362 page.pushstate = page.popstate = function(path, id) {
363 OSM.loadSidebarContent(path, function() {
368 page.load = function(path, id) {
369 addObject(type, id, true);
372 function addObject(type, id, center) {
373 map.addObject({type: type, id: parseInt(id)}, function(bounds) {
374 if (!window.location.hash && bounds.isValid() &&
375 (center || !map.getBounds().contains(bounds))) {
376 OSM.router.withoutMoveListener(function () {
377 map.fitBounds(bounds);
383 page.unload = function() {
390 var history = OSM.History(map);
392 OSM.router = OSM.Router(map, {
394 "/search": OSM.Search(map),
395 "/directions": OSM.Directions(map),
396 "/export": OSM.Export(map),
397 "/note/new": OSM.NewNote(map),
398 "/history/friends": history,
399 "/history/nearby": history,
401 "/user/:display_name/history": history,
402 "/note/:id": OSM.Note(map),
403 "/node/:id(/history)": OSM.Browse(map, 'node'),
404 "/way/:id(/history)": OSM.Browse(map, 'way'),
405 "/relation/:id(/history)": OSM.Browse(map, 'relation'),
406 "/changeset/:id": OSM.Changeset(map),
407 "/query": OSM.Query(map)
410 if (OSM.preferred_editor === "remote" && document.location.pathname === "/edit") {
411 remoteEditHandler(map.getBounds(), params.object);
412 OSM.router.setCurrentPath("/");
417 $(document).on("click", "a", function(e) {
418 if (e.isDefaultPrevented() || e.isPropagationStopped())
421 // Open links in a new tab as normal.
422 if (e.which > 1 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey)
425 // Ignore cross-protocol and cross-origin links.
426 if (location.protocol !== this.protocol || location.host !== this.host)
429 if (OSM.router.route(this.pathname + this.search + this.hash))