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 // TODO internationalisation of the context menu strings
112 var map = new L.OSM.Map("map", {
116 contextmenuWidth: 140,
118 text: 'Directions from here',
119 callback: context_directionsfrom
121 text: 'Directions to here',
122 callback: context_directionsto
124 text: 'Add a note here',
125 callback: context_addnote
127 text: 'Show address',
128 callback: context_describe
130 text: 'Centre map here',
131 callback: context_centrehere
135 map.attributionControl.setPrefix('');
137 map.updateLayers(params.layers);
139 map.on("baselayerchange", function (e) {
140 if (map.getZoom() > e.layer.options.maxZoom) {
141 map.setView(map.getCenter(), e.layer.options.maxZoom, { reset: true });
145 var position = $('html').attr('dir') === 'rtl' ? 'topleft' : 'topright';
147 L.OSM.zoom({position: position})
153 title: I18n.t('javascripts.map.locate.title'),
154 popup: I18n.t('javascripts.map.locate.popup')
158 var sidebar = L.OSM.sidebar('#map-ui')
163 layers: map.baseLayers,
191 if (OSM.STATUS !== 'api_offline' && OSM.STATUS !== 'database_offline') {
192 OSM.initializeNotes(map);
193 if (params.layers.indexOf(map.noteLayer.options.code) >= 0) {
194 map.addLayer(map.noteLayer);
197 OSM.initializeBrowse(map);
198 if (params.layers.indexOf(map.dataLayer.options.code) >= 0) {
199 map.addLayer(map.dataLayer);
203 var placement = $('html').attr('dir') === 'rtl' ? 'right' : 'left';
204 $('.leaflet-control .control-button').tooltip({placement: placement, container: 'body'});
206 var expiry = new Date();
207 expiry.setYear(expiry.getFullYear() + 10);
209 map.on('moveend layeradd layerremove', function() {
211 map.getCenter().wrap(),
216 $.removeCookie("_osm_location");
217 $.cookie("_osm_location", OSM.locationCookie(map), { expires: expiry, path: "/" });
220 if ($.cookie('_osm_welcome') === 'hide') {
221 $('.welcome').hide();
224 $('.welcome .close').on('click', function() {
225 $('.welcome').hide();
226 $.cookie("_osm_welcome", 'hide', { expires: expiry });
230 map.on('layeradd', function (e) {
231 if (e.layer.options) {
232 var goal = OSM.PIWIK.goals[e.layer.options.keyid];
235 $('body').trigger('piwikgoal', goal);
242 map.fitBounds(params.bounds);
244 map.setView([params.lat, params.lon], params.zoom);
247 var marker = L.marker([0, 0], {icon: OSM.getUserIcon()});
250 marker.setLatLng([params.mlat, params.mlon]).addTo(map);
253 $("#homeanchor").on("click", function(e) {
256 var data = $(this).data(),
257 center = L.latLng(data.lat, data.lon);
259 map.setView(center, data.zoom);
260 marker.setLatLng(center).addTo(map);
263 function remoteEditHandler(bbox, object) {
265 url = document.location.protocol === "https:" ?
266 "https://127.0.0.1:8112/load_and_zoom?" :
267 "http://127.0.0.1:8111/load_and_zoom?",
269 left: bbox.getWest() - 0.0001,
270 top: bbox.getNorth() + 0.0001,
271 right: bbox.getEast() + 0.0001,
272 bottom: bbox.getSouth() - 0.0001
275 if (object) query.select = object.type + object.id;
277 var iframe = $('<iframe>')
280 .attr("src", url + querystring.stringify(query))
281 .on('load', function() {
286 setTimeout(function () {
288 alert(I18n.t('site.index.remote_failed'));
296 $("a[data-editor=remote]").click(function(e) {
297 var params = OSM.mapParams(this.search);
298 remoteEditHandler(map.getBounds(), params.object);
302 if (OSM.params().edit_help) {
307 title: I18n.t('javascripts.edit_help')
311 $('body').one('click', function() {
312 $('#editanchor').tooltip('hide');
316 OSM.Index = function(map) {
319 page.pushstate = page.popstate = function() {
320 map.setSidebarOverlaid(true);
321 document.title = I18n.t('layouts.project_name.title');
324 page.load = function() {
325 var params = querystring.parse(location.search.substring(1));
327 $("#sidebar .search_form input[name=query]").value(params.query);
329 if (!("autofocus" in document.createElement("input"))) {
330 $("#sidebar .search_form input[name=query]").focus();
332 return map.getState();
338 OSM.Browse = function(map, type) {
341 page.pushstate = page.popstate = function(path, id) {
342 OSM.loadSidebarContent(path, function() {
347 page.load = function(path, id) {
348 addObject(type, id, true);
351 function addObject(type, id, center) {
352 map.addObject({type: type, id: parseInt(id)}, function(bounds) {
353 if (!window.location.hash && bounds.isValid() &&
354 (center || !map.getBounds().contains(bounds))) {
355 OSM.router.withoutMoveListener(function () {
356 map.fitBounds(bounds);
362 page.unload = function() {
369 var history = OSM.History(map);
371 OSM.router = OSM.Router(map, {
373 "/search": OSM.Search(map),
374 "/directions": OSM.Directions(map),
375 "/export": OSM.Export(map),
376 "/note/new": OSM.NewNote(map),
377 "/history/friends": history,
378 "/history/nearby": history,
380 "/user/:display_name/history": history,
381 "/note/:id": OSM.Note(map),
382 "/node/:id(/history)": OSM.Browse(map, 'node'),
383 "/way/:id(/history)": OSM.Browse(map, 'way'),
384 "/relation/:id(/history)": OSM.Browse(map, 'relation'),
385 "/changeset/:id": OSM.Changeset(map),
386 "/query": OSM.Query(map)
389 if (OSM.preferred_editor === "remote" && document.location.pathname === "/edit") {
390 remoteEditHandler(map.getBounds(), params.object);
391 OSM.router.setCurrentPath("/");
396 $(document).on("click", "a", function(e) {
397 if (e.isDefaultPrevented() || e.isPropagationStopped())
400 // Open links in a new tab as normal.
401 if (e.which > 1 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey)
404 // Ignore cross-protocol and cross-origin links.
405 if (location.protocol !== this.protocol || location.host !== this.host)
408 if (OSM.router.route(this.pathname + this.search + this.hash))