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 // TODO this currently doesn't work correctly - I think the "route" needs to be chained to ensure it comes once the pan has finished.
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 map.attributionControl.setPrefix('');
149 map.updateLayers(params.layers);
151 map.on("baselayerchange", function (e) {
152 if (map.getZoom() > e.layer.options.maxZoom) {
153 map.setView(map.getCenter(), e.layer.options.maxZoom, { reset: true });
157 var position = $('html').attr('dir') === 'rtl' ? 'topleft' : 'topright';
159 L.OSM.zoom({position: position})
165 title: I18n.t('javascripts.map.locate.title'),
166 popup: I18n.t('javascripts.map.locate.popup')
170 var sidebar = L.OSM.sidebar('#map-ui')
175 layers: map.baseLayers,
203 if (OSM.STATUS !== 'api_offline' && OSM.STATUS !== 'database_offline') {
204 OSM.initializeNotes(map);
205 if (params.layers.indexOf(map.noteLayer.options.code) >= 0) {
206 map.addLayer(map.noteLayer);
209 OSM.initializeBrowse(map);
210 if (params.layers.indexOf(map.dataLayer.options.code) >= 0) {
211 map.addLayer(map.dataLayer);
215 var placement = $('html').attr('dir') === 'rtl' ? 'right' : 'left';
216 $('.leaflet-control .control-button').tooltip({placement: placement, container: 'body'});
218 var expiry = new Date();
219 expiry.setYear(expiry.getFullYear() + 10);
221 map.on('moveend layeradd layerremove', function() {
223 map.getCenter().wrap(),
228 $.removeCookie("_osm_location");
229 $.cookie("_osm_location", OSM.locationCookie(map), { expires: expiry, path: "/" });
232 if ($.cookie('_osm_welcome') === 'hide') {
233 $('.welcome').hide();
236 $('.welcome .close').on('click', function() {
237 $('.welcome').hide();
238 $.cookie("_osm_welcome", 'hide', { expires: expiry });
242 map.on('layeradd', function (e) {
243 if (e.layer.options) {
244 var goal = OSM.PIWIK.goals[e.layer.options.keyid];
247 $('body').trigger('piwikgoal', goal);
254 map.fitBounds(params.bounds);
256 map.setView([params.lat, params.lon], params.zoom);
259 var marker = L.marker([0, 0], {icon: OSM.getUserIcon()});
262 marker.setLatLng([params.mlat, params.mlon]).addTo(map);
265 $("#homeanchor").on("click", function(e) {
268 var data = $(this).data(),
269 center = L.latLng(data.lat, data.lon);
271 map.setView(center, data.zoom);
272 marker.setLatLng(center).addTo(map);
275 function remoteEditHandler(bbox, object) {
277 url = document.location.protocol === "https:" ?
278 "https://127.0.0.1:8112/load_and_zoom?" :
279 "http://127.0.0.1:8111/load_and_zoom?",
281 left: bbox.getWest() - 0.0001,
282 top: bbox.getNorth() + 0.0001,
283 right: bbox.getEast() + 0.0001,
284 bottom: bbox.getSouth() - 0.0001
287 if (object) query.select = object.type + object.id;
289 var iframe = $('<iframe>')
292 .attr("src", url + querystring.stringify(query))
293 .on('load', function() {
298 setTimeout(function () {
300 alert(I18n.t('site.index.remote_failed'));
308 $("a[data-editor=remote]").click(function(e) {
309 var params = OSM.mapParams(this.search);
310 remoteEditHandler(map.getBounds(), params.object);
314 if (OSM.params().edit_help) {
319 title: I18n.t('javascripts.edit_help')
323 $('body').one('click', function() {
324 $('#editanchor').tooltip('hide');
328 OSM.Index = function(map) {
331 page.pushstate = page.popstate = function() {
332 map.setSidebarOverlaid(true);
333 document.title = I18n.t('layouts.project_name.title');
336 page.load = function() {
337 var params = querystring.parse(location.search.substring(1));
339 $("#sidebar .search_form input[name=query]").value(params.query);
341 if (!("autofocus" in document.createElement("input"))) {
342 $("#sidebar .search_form input[name=query]").focus();
344 return map.getState();
350 OSM.Browse = function(map, type) {
353 page.pushstate = page.popstate = function(path, id) {
354 OSM.loadSidebarContent(path, function() {
359 page.load = function(path, id) {
360 addObject(type, id, true);
363 function addObject(type, id, center) {
364 map.addObject({type: type, id: parseInt(id)}, function(bounds) {
365 if (!window.location.hash && bounds.isValid() &&
366 (center || !map.getBounds().contains(bounds))) {
367 OSM.router.withoutMoveListener(function () {
368 map.fitBounds(bounds);
374 page.unload = function() {
381 var history = OSM.History(map);
383 OSM.router = OSM.Router(map, {
385 "/search": OSM.Search(map),
386 "/directions": OSM.Directions(map),
387 "/export": OSM.Export(map),
388 "/note/new": OSM.NewNote(map),
389 "/history/friends": history,
390 "/history/nearby": history,
392 "/user/:display_name/history": history,
393 "/note/:id": OSM.Note(map),
394 "/node/:id(/history)": OSM.Browse(map, 'node'),
395 "/way/:id(/history)": OSM.Browse(map, 'way'),
396 "/relation/:id(/history)": OSM.Browse(map, 'relation'),
397 "/changeset/:id": OSM.Changeset(map),
398 "/query": OSM.Query(map)
401 if (OSM.preferred_editor === "remote" && document.location.pathname === "/edit") {
402 remoteEditHandler(map.getBounds(), params.object);
403 OSM.router.setCurrentPath("/");
408 $(document).on("click", "a", function(e) {
409 if (e.isDefaultPrevented() || e.isPropagationStopped())
412 // Open links in a new tab as normal.
413 if (e.which > 1 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey)
416 // Ignore cross-protocol and cross-origin links.
417 if (location.protocol !== this.protocol || location.host !== this.host)
420 if (OSM.router.route(this.pathname + this.search + this.hash))