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 index/search
11 //= require index/browse
12 //= require index/export
13 //= require index/notes
14 //= require index/history
15 //= require index/note
16 //= require index/new_note
17 //= require index/directions
18 //= require index/changeset
19 //= require index/query
22 $(document).ready(function () {
25 OSM.loadSidebarContent = function(path, callback) {
26 map.setSidebarOverlaid(false);
28 clearTimeout(loaderTimeout);
30 loaderTimeout = setTimeout(function() {
31 $('#sidebar_loader').show();
34 // IE<10 doesn't respect Vary: X-Requested-With header, so
35 // prevent caching the XHR response as a full-page URL.
36 if (path.indexOf('?') >= 0) {
48 complete: function(xhr) {
49 clearTimeout(loaderTimeout);
51 $('#sidebar_loader').hide();
53 var content = $(xhr.responseText);
55 if (xhr.getResponseHeader('X-Page-Title')) {
56 var title = xhr.getResponseHeader('X-Page-Title');
57 document.title = decodeURIComponent(title);
61 .find('link[type="application/atom+xml"]')
65 .append(content.filter('link[type="application/atom+xml"]'));
67 $('#sidebar_content').html(content.not('link[type="application/atom+xml"]'));
76 var params = OSM.mapParams();
78 var map = new L.OSM.Map("map", {
83 map.attributionControl.setPrefix('');
85 map.updateLayers(params.layers);
87 map.on("baselayerchange", function (e) {
88 if (map.getZoom() > e.layer.options.maxZoom) {
89 map.setView(map.getCenter(), e.layer.options.maxZoom, { reset: true });
93 var position = $('html').attr('dir') === 'rtl' ? 'topleft' : 'topright';
95 L.OSM.zoom({position: position})
101 title: I18n.t('javascripts.map.locate.title'),
102 popup: I18n.t('javascripts.map.locate.popup')
106 var sidebar = L.OSM.sidebar('#map-ui')
111 layers: map.baseLayers,
139 if (OSM.STATUS !== 'api_offline' && OSM.STATUS !== 'database_offline') {
140 OSM.initializeNotes(map);
141 if (params.layers.indexOf(map.noteLayer.options.code) >= 0) {
142 map.addLayer(map.noteLayer);
145 OSM.initializeBrowse(map);
146 if (params.layers.indexOf(map.dataLayer.options.code) >= 0) {
147 map.addLayer(map.dataLayer);
151 var placement = $('html').attr('dir') === 'rtl' ? 'right' : 'left';
152 $('.leaflet-control .control-button').tooltip({placement: placement, container: 'body'});
154 var expiry = new Date();
155 expiry.setYear(expiry.getFullYear() + 10);
157 map.on('moveend layeradd layerremove', function() {
159 map.getCenter().wrap(),
164 $.removeCookie('_osm_location');
165 $.cookie('_osm_location', OSM.locationCookie(map), { expires: expiry, path: '/' });
168 if ($.cookie('_osm_welcome') === 'hide') {
169 $('.welcome').hide();
172 $('.welcome .close-wrap').on('click', function() {
173 $('.welcome').hide();
174 $.cookie('_osm_welcome', 'hide', { expires: expiry });
177 $('#banner .close-wrap').on('click', function(e) {
178 var cookieId = e.target.id;
182 $.cookie(cookieId, 'hide', { expires: expiry });
187 map.on('layeradd', function (e) {
188 if (e.layer.options) {
189 var goal = OSM.PIWIK.goals[e.layer.options.keyid];
192 $('body').trigger('piwikgoal', goal);
199 map.fitBounds(params.bounds);
201 map.setView([params.lat, params.lon], params.zoom);
205 L.marker([params.mlat, params.mlon]).addTo(map);
208 $("#homeanchor").on("click", function(e) {
211 var data = $(this).data(),
212 center = L.latLng(data.lat, data.lon);
214 map.setView(center, data.zoom);
215 L.marker(center, {icon: OSM.getUserIcon()}).addTo(map);
218 function remoteEditHandler(bbox, object) {
220 url = document.location.protocol === "https:" ?
221 "https://127.0.0.1:8112/load_and_zoom?" :
222 "http://127.0.0.1:8111/load_and_zoom?",
224 left: bbox.getWest() - 0.0001,
225 top: bbox.getNorth() + 0.0001,
226 right: bbox.getEast() + 0.0001,
227 bottom: bbox.getSouth() - 0.0001
230 if (object) query.select = object.type + object.id;
232 var iframe = $('<iframe>')
235 .attr("src", url + querystring.stringify(query))
236 .on('load', function() {
241 setTimeout(function () {
243 alert(I18n.t('site.index.remote_failed'));
251 $("a[data-editor=remote]").click(function(e) {
252 var params = OSM.mapParams(this.search);
253 remoteEditHandler(map.getBounds(), params.object);
257 if (OSM.params().edit_help) {
262 title: I18n.t('javascripts.edit_help')
266 $('body').one('click', function() {
267 $('#editanchor').tooltip('hide');
271 OSM.Index = function(map) {
274 page.pushstate = page.popstate = function() {
275 map.setSidebarOverlaid(true);
276 document.title = I18n.t('layouts.project_name.title');
279 page.load = function() {
280 var params = querystring.parse(location.search.substring(1));
282 $("#sidebar .search_form input[name=query]").value(params.query);
284 if (!("autofocus" in document.createElement("input"))) {
285 $("#sidebar .search_form input[name=query]").focus();
287 return map.getState();
293 OSM.Browse = function(map, type) {
296 page.pushstate = page.popstate = function(path, id) {
297 OSM.loadSidebarContent(path, function() {
302 page.load = function(path, id) {
303 addObject(type, id, true);
306 function addObject(type, id, center) {
307 map.addObject({type: type, id: parseInt(id)}, function(bounds) {
308 if (!window.location.hash && bounds.isValid() &&
309 (center || !map.getBounds().contains(bounds))) {
310 OSM.router.withoutMoveListener(function () {
311 map.fitBounds(bounds);
317 page.unload = function() {
324 var history = OSM.History(map);
326 OSM.router = OSM.Router(map, {
328 "/search": OSM.Search(map),
329 "/directions": OSM.Directions(map),
330 "/export": OSM.Export(map),
331 "/note/new": OSM.NewNote(map),
332 "/history/friends": history,
333 "/history/nearby": history,
335 "/user/:display_name/history": history,
336 "/note/:id": OSM.Note(map),
337 "/node/:id(/history)": OSM.Browse(map, 'node'),
338 "/way/:id(/history)": OSM.Browse(map, 'way'),
339 "/relation/:id(/history)": OSM.Browse(map, 'relation'),
340 "/changeset/:id": OSM.Changeset(map),
341 "/query": OSM.Query(map)
344 if (OSM.preferred_editor === "remote" && document.location.pathname === "/edit") {
345 remoteEditHandler(map.getBounds(), params.object);
346 OSM.router.setCurrentPath("/");
351 $(document).on("click", "a", function(e) {
352 if (e.isDefaultPrevented() || e.isPropagationStopped())
355 // Open links in a new tab as normal.
356 if (e.which > 1 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey)
359 // Ignore cross-protocol and cross-origin links.
360 if (location.protocol !== this.protocol || location.host !== this.host)
363 if (OSM.router.route(this.pathname + this.search + this.hash))