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/contextmenu
12 //= require index/search
13 //= require index/browse
14 //= require index/export
15 //= require index/notes
16 //= require index/history
17 //= require index/note
18 //= require index/new_note
19 //= require index/directions
20 //= require index/changeset
21 //= require index/query
24 $(document).ready(function () {
27 OSM.loadSidebarContent = function(path, callback) {
28 map.setSidebarOverlaid(false);
30 clearTimeout(loaderTimeout);
32 loaderTimeout = setTimeout(function() {
33 $('#sidebar_loader').show();
36 // IE<10 doesn't respect Vary: X-Requested-With header, so
37 // prevent caching the XHR response as a full-page URL.
38 if (path.indexOf('?') >= 0) {
50 complete: function(xhr) {
51 clearTimeout(loaderTimeout);
53 $('#sidebar_loader').hide();
55 var content = $(xhr.responseText);
57 if (xhr.getResponseHeader('X-Page-Title')) {
58 var title = xhr.getResponseHeader('X-Page-Title');
59 document.title = decodeURIComponent(title);
63 .find('link[type="application/atom+xml"]')
67 .append(content.filter('link[type="application/atom+xml"]'));
69 $('#sidebar_content').html(content.not('link[type="application/atom+xml"]'));
78 var params = OSM.mapParams();
80 var map = new L.OSM.Map("map", {
86 map.attributionControl.setPrefix('');
88 map.updateLayers(params.layers);
90 map.on("baselayerchange", function (e) {
91 if (map.getZoom() > e.layer.options.maxZoom) {
92 map.setView(map.getCenter(), e.layer.options.maxZoom, { reset: true });
96 var position = $('html').attr('dir') === 'rtl' ? 'topleft' : 'topright';
98 L.OSM.zoom({position: position})
101 var locate = L.control.locate({
103 icon: 'icon geolocate',
104 iconLoading: 'icon geolocate',
106 title: I18n.t('javascripts.map.locate.title'),
107 popup: I18n.t('javascripts.map.locate.popup')
111 var locateContainer = locate.getContainer();
114 .removeClass('leaflet-control-locate leaflet-bar')
115 .addClass('control-locate')
118 .removeClass('leaflet-bar-part leaflet-bar-part-single')
119 .addClass('control-button');
121 var sidebar = L.OSM.sidebar('#map-ui')
126 layers: map.baseLayers,
154 OSM.initializeContextMenu(map);
156 if (OSM.STATUS !== 'api_offline' && OSM.STATUS !== 'database_offline') {
157 OSM.initializeNotes(map);
158 if (params.layers.indexOf(map.noteLayer.options.code) >= 0) {
159 map.addLayer(map.noteLayer);
162 OSM.initializeBrowse(map);
163 if (params.layers.indexOf(map.dataLayer.options.code) >= 0) {
164 map.addLayer(map.dataLayer);
167 if (params.layers.indexOf(map.gpsLayer.options.code) >= 0) {
168 map.addLayer(map.gpsLayer);
172 var placement = $('html').attr('dir') === 'rtl' ? 'right' : 'left';
173 $('.leaflet-control .control-button').tooltip({placement: placement, container: 'body'});
175 var expiry = new Date();
176 expiry.setYear(expiry.getFullYear() + 10);
178 map.on('moveend layeradd layerremove', function() {
180 map.getCenter().wrap(),
185 $.removeCookie('_osm_location');
186 $.cookie('_osm_location', OSM.locationCookie(map), { expires: expiry, path: '/' });
189 if ($.cookie('_osm_welcome') === 'hide') {
190 $('.welcome').hide();
193 $('.welcome .close-wrap').on('click', function() {
194 $('.welcome').hide();
195 $.cookie('_osm_welcome', 'hide', { expires: expiry, path: '/' });
198 var bannerExpiry = new Date();
199 bannerExpiry.setYear(bannerExpiry.getFullYear() + 1);
201 $('#banner .close-wrap').on('click', function(e) {
202 var cookieId = e.target.id;
206 $.cookie(cookieId, 'hide', { expires: bannerExpiry, path: '/' });
211 map.on('layeradd', function (e) {
212 if (e.layer.options) {
213 var goal = OSM.PIWIK.goals[e.layer.options.keyid];
216 $('body').trigger('piwikgoal', goal);
223 map.fitBounds(params.bounds);
225 map.setView([params.lat, params.lon], params.zoom);
229 L.marker([params.mlat, params.mlon]).addTo(map);
232 $("#homeanchor").on("click", function(e) {
235 var data = $(this).data(),
236 center = L.latLng(data.lat, data.lon);
238 map.setView(center, data.zoom);
239 L.marker(center, {icon: OSM.getUserIcon()}).addTo(map);
242 function remoteEditHandler(bbox, object) {
244 url = "http://127.0.0.1:8111/load_and_zoom?",
246 left: bbox.getWest() - 0.0001,
247 top: bbox.getNorth() + 0.0001,
248 right: bbox.getEast() + 0.0001,
249 bottom: bbox.getSouth() - 0.0001
252 if (object) query.select = object.type + object.id;
254 var iframe = $('<iframe>')
257 .attr("src", url + querystring.stringify(query))
258 .on('load', function() {
263 setTimeout(function () {
265 alert(I18n.t('site.index.remote_failed'));
273 $("a[data-editor=remote]").click(function(e) {
274 var params = OSM.mapParams(this.search);
275 remoteEditHandler(map.getBounds(), params.object);
279 if (OSM.params().edit_help) {
284 title: I18n.t('javascripts.edit_help')
288 $('body').one('click', function() {
289 $('#editanchor').tooltip('hide');
293 OSM.Index = function(map) {
296 page.pushstate = page.popstate = function() {
297 map.setSidebarOverlaid(true);
298 document.title = I18n.t('layouts.project_name.title');
301 page.load = function() {
302 var params = querystring.parse(location.search.substring(1));
304 $("#sidebar .search_form input[name=query]").value(params.query);
306 if (!("autofocus" in document.createElement("input"))) {
307 $("#sidebar .search_form input[name=query]").focus();
309 return map.getState();
315 OSM.Browse = function(map, type) {
318 page.pushstate = page.popstate = function(path, id) {
319 OSM.loadSidebarContent(path, function() {
324 page.load = function(path, id) {
325 addObject(type, id, true);
328 function addObject(type, id, center) {
329 map.addObject({type: type, id: parseInt(id)}, function(bounds) {
330 if (!window.location.hash && bounds.isValid() &&
331 (center || !map.getBounds().contains(bounds))) {
332 OSM.router.withoutMoveListener(function () {
333 map.fitBounds(bounds);
338 $('.colour-preview-box').each(function (index, value) {
339 $(this).css("background-color", $(this).data("colour"));
343 page.unload = function() {
350 var history = OSM.History(map);
352 OSM.router = OSM.Router(map, {
354 "/search": OSM.Search(map),
355 "/directions": OSM.Directions(map),
356 "/export": OSM.Export(map),
357 "/note/new": OSM.NewNote(map),
358 "/history/friends": history,
359 "/history/nearby": history,
361 "/user/:display_name/history": history,
362 "/note/:id": OSM.Note(map),
363 "/node/:id(/history)": OSM.Browse(map, 'node'),
364 "/way/:id(/history)": OSM.Browse(map, 'way'),
365 "/relation/:id(/history)": OSM.Browse(map, 'relation'),
366 "/changeset/:id": OSM.Changeset(map),
367 "/query": OSM.Query(map)
370 if (OSM.preferred_editor === "remote" && document.location.pathname === "/edit") {
371 remoteEditHandler(map.getBounds(), params.object);
372 OSM.router.setCurrentPath("/");
377 $(document).on("click", "a", function(e) {
378 if (e.isDefaultPrevented() || e.isPropagationStopped())
381 // Open links in a new tab as normal.
382 if (e.which > 1 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey)
385 // Ignore cross-protocol and cross-origin links.
386 if (location.protocol !== this.protocol || location.host !== this.host)
389 if (OSM.router.route(this.pathname + this.search + this.hash))