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 index/search
10 //= require index/browse
11 //= require index/export
12 //= require index/notes
13 //= require index/history
14 //= require index/note
15 //= require index/new_note
16 //= require index/directions
19 $(document).ready(function () {
22 OSM.loadSidebarContent = function(path, callback) {
23 map.setSidebarOverlaid(false);
25 clearTimeout(loaderTimeout);
27 loaderTimeout = setTimeout(function() {
28 $('#sidebar_loader').show();
31 // IE<10 doesn't respect Vary: X-Requested-With header, so
32 // prevent caching the XHR response as a full-page URL.
33 if (path.indexOf('?') >= 0) {
45 complete: function(xhr) {
46 clearTimeout(loaderTimeout);
48 $('#sidebar_loader').hide();
50 var content = $(xhr.responseText);
52 if (xhr.getResponseHeader('X-Page-Title')) {
53 var title = xhr.getResponseHeader('X-Page-Title');
54 document.title = decodeURIComponent(escape(title));
58 .find('link[type="application/atom+xml"]')
62 .append(content.filter('link[type="application/atom+xml"]'));
64 $('#sidebar_content').html(content.not('link[type="application/atom+xml"]'));
73 var params = OSM.mapParams();
75 var map = new L.OSM.Map("map", {
80 map.attributionControl.setPrefix('');
82 map.updateLayers(params.layers);
84 map.on("baselayerchange", function (e) {
85 if (map.getZoom() > e.layer.options.maxZoom) {
86 map.setView(map.getCenter(), e.layer.options.maxZoom, { reset: true });
90 var position = $('html').attr('dir') === 'rtl' ? 'topleft' : 'topright';
92 L.OSM.zoom({position: position})
98 title: I18n.t('javascripts.map.locate.title'),
99 popup: I18n.t('javascripts.map.locate.popup')
103 var sidebar = L.OSM.sidebar('#map-ui')
108 layers: map.baseLayers,
131 if (OSM.STATUS != 'api_offline' && OSM.STATUS != 'database_offline') {
132 initializeNotes(map);
133 if (params.layers.indexOf(map.noteLayer.options.code) >= 0) {
134 map.addLayer(map.noteLayer);
137 initializeBrowse(map);
138 if (params.layers.indexOf(map.dataLayer.options.code) >= 0) {
139 map.addLayer(map.dataLayer);
143 $('.leaflet-control .control-button').tooltip({placement: 'left', container: 'body'});
145 var expiry = new Date();
146 expiry.setYear(expiry.getFullYear() + 10);
148 map.on('moveend layeradd layerremove', function() {
150 map.getCenter().wrap(),
155 $.removeCookie("_osm_location");
156 $.cookie("_osm_location", OSM.locationCookie(map), { expires: expiry, path: "/" });
159 if ($.cookie('_osm_welcome') == 'hide') {
160 $('.welcome').hide();
163 $('.welcome .close').on('click', function() {
164 $('.welcome').hide();
165 $.cookie("_osm_welcome", 'hide', { expires: expiry });
169 map.on('layeradd', function (e) {
170 if (e.layer.options) {
171 var goal = OSM.PIWIK.goals[e.layer.options.keyid];
174 $('body').trigger('piwikgoal', goal);
181 map.fitBounds(params.bounds);
183 map.setView([params.lat, params.lon], params.zoom);
186 var marker = L.marker([0, 0], {icon: getUserIcon()});
189 marker.setLatLng([params.mlat, params.mlon]).addTo(map);
192 $("#homeanchor").on("click", function(e) {
195 var data = $(this).data(),
196 center = L.latLng(data.lat, data.lon);
198 map.setView(center, data.zoom);
199 marker.setLatLng(center).addTo(map);
202 $("a[data-editor=remote]").click(function(e) {
203 var params = OSM.mapParams(this.search);
204 remoteEditHandler(map.getBounds(), params.object);
208 if (OSM.params().edit_help) {
213 title: I18n.t('javascripts.edit_help')
217 $('body').one('click', function() {
218 $('#editanchor').tooltip('hide');
222 OSM.Index = function(map) {
225 page.pushstate = page.popstate = function() {
226 map.setSidebarOverlaid(true);
227 document.title = I18n.t('layouts.project_name.title');
230 page.load = function() {
231 if (!("autofocus" in document.createElement("input"))) {
232 $("#sidebar .search_form input[name=query]").focus();
234 return map.getState();
240 OSM.Browse = function(map, type) {
243 page.pushstate = page.popstate = function(path, id) {
244 OSM.loadSidebarContent(path, function() {
249 page.load = function(path, id) {
250 addObject(type, id, true);
253 function addObject(type, id, center) {
254 var bounds = map.addObject({type: type, id: parseInt(id)}, function(bounds) {
255 if (!window.location.hash && bounds.isValid()) {
256 OSM.router.moveListenerOff();
257 map.once('moveend', OSM.router.moveListenerOn);
258 if (center || !map.getBounds().contains(bounds)) map.fitBounds(bounds);
263 page.unload = function() {
270 var history = OSM.History(map);
272 OSM.router = OSM.Router(map, {
274 "/search": OSM.Search(map),
275 "/directions": OSM.Directions(map),
276 "/export": OSM.Export(map),
277 "/note/new": OSM.NewNote(map),
278 "/history/friends": history,
279 "/history/nearby": history,
281 "/user/:display_name/history": history,
282 "/note/:id": OSM.Note(map),
283 "/node/:id(/history)": OSM.Browse(map, 'node'),
284 "/way/:id(/history)": OSM.Browse(map, 'way'),
285 "/relation/:id(/history)": OSM.Browse(map, 'relation'),
286 "/changeset/:id": OSM.Browse(map, 'changeset')
289 if (OSM.preferred_editor == "remote" && document.location.pathname == "/edit") {
290 remoteEditHandler(map.getBounds(), params.object);
291 OSM.router.setCurrentPath("/");
296 $(document).on("click", "a", function(e) {
297 if (e.isDefaultPrevented() || e.isPropagationStopped())
300 // Open links in a new tab as normal.
301 if (e.which > 1 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey)
304 // Ignore cross-protocol and cross-origin links.
305 if (location.protocol !== this.protocol || location.host !== this.host)
308 if (OSM.router.route(this.pathname + this.search + this.hash))