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 index/search
9 //= require index/browse
10 //= require index/export
11 //= require index/notes
12 //= require index/history
13 //= require index/note
14 //= require index/new_note
15 //= require index/changeset
21 OSM.loadSidebarContent = function(path, callback) {
22 clearTimeout(loaderTimeout);
24 loaderTimeout = setTimeout(function() {
25 $('#sidebar_loader').show();
28 // IE<10 doesn't respect Vary: X-Requested-With header, so
29 // prevent caching the XHR response as a full-page URL.
30 if (path.indexOf('?') >= 0) {
42 complete: function(xhr) {
43 clearTimeout(loaderTimeout);
45 $('#sidebar_loader').hide();
47 var content = $(xhr.responseText);
49 if (xhr.getResponseHeader('X-Page-Title')) {
50 var title = xhr.getResponseHeader('X-Page-Title');
51 document.title = decodeURIComponent(escape(title));
55 .find('link[type="application/atom+xml"]')
59 .append(content.filter('link[type="application/atom+xml"]'));
61 $('#sidebar_content').html(content.not('link[type="application/atom+xml"]'));
71 $(document).ready(function () {
72 var params = OSM.mapParams();
74 var map = new L.OSM.Map("map", {
79 map.attributionControl.setPrefix('');
81 map.updateLayers(params.layers);
83 map.on("baselayerchange", function (e) {
84 if (map.getZoom() > e.layer.options.maxZoom) {
85 map.setView(map.getCenter(), e.layer.options.maxZoom, { reset: true });
89 var position = $('html').attr('dir') === 'rtl' ? 'topleft' : 'topright';
91 L.OSM.zoom({position: position})
97 title: I18n.t('javascripts.map.locate.title'),
98 popup: I18n.t('javascripts.map.locate.popup')
102 var sidebar = L.OSM.sidebar('#map-ui')
107 layers: map.baseLayers,
130 if (OSM.STATUS != 'api_offline' && OSM.STATUS != 'database_offline') {
131 initializeNotes(map);
132 if (params.layers.indexOf(map.noteLayer.options.code) >= 0) {
133 map.addLayer(map.noteLayer);
136 initializeBrowse(map);
137 if (params.layers.indexOf(map.dataLayer.options.code) >= 0) {
138 map.addLayer(map.dataLayer);
142 $('.leaflet-control .control-button').tooltip({placement: 'left', container: 'body'});
144 var expiry = new Date();
145 expiry.setYear(expiry.getFullYear() + 10);
147 map.on('moveend layeradd layerremove', function() {
149 map.getCenter().wrap(),
154 $.removeCookie("_osm_location");
155 $.cookie("_osm_location", OSM.locationCookie(map), { expires: expiry, path: "/" });
158 if ($.cookie('_osm_sotm') == 'hide') {
162 $('#sotm .close').on('click', function() {
164 $.cookie("_osm_sotm", 'hide', { expires: expiry });
167 if ($.cookie('_osm_welcome') == 'hide') {
168 $('.welcome').hide();
171 $('.welcome .close').on('click', function() {
172 $('.welcome').hide();
173 $.cookie("_osm_welcome", 'hide', { expires: expiry });
177 map.on('layeradd', function (e) {
178 if (e.layer.options) {
179 var goal = OSM.PIWIK.goals[e.layer.options.keyid];
182 $('body').trigger('piwikgoal', goal);
189 map.fitBounds(params.bounds);
191 map.setView([params.lat, params.lon], params.zoom);
194 var marker = L.marker([0, 0], {icon: getUserIcon()});
197 marker.setLatLng([params.mlat, params.mlon]).addTo(map);
200 $("#homeanchor").on("click", function(e) {
203 var data = $(this).data(),
204 center = L.latLng(data.lat, data.lon);
206 map.setView(center, data.zoom);
207 marker.setLatLng(center).addTo(map);
210 $("a[data-editor=remote]").click(function(e) {
211 var params = OSM.mapParams(this.search);
212 remoteEditHandler(map.getBounds(), params.object);
216 if (OSM.params().edit_help) {
221 title: I18n.t('javascripts.edit_help')
225 $('body').one('click', function() {
226 $('#editanchor').tooltip('hide');
230 OSM.Index = function(map) {
233 page.pushstate = function() {
234 $("#content").addClass("overlay-sidebar");
235 map.invalidateSize({pan: false})
236 .panBy([-350, 0], {animate: false});
237 document.title = I18n.t('layouts.project_name.title');
240 page.load = function() {
241 if (!("autofocus" in document.createElement("input"))) {
242 $("#sidebar .search_form input[name=query]").focus();
244 return map.getState();
247 page.popstate = function() {
248 $("#content").addClass("overlay-sidebar");
249 map.invalidateSize({pan: false});
250 document.title = I18n.t('layouts.project_name.title');
253 page.unload = function() {
254 map.panBy([350, 0], {animate: false});
255 $("#content").removeClass("overlay-sidebar");
256 map.invalidateSize({pan: false});
262 OSM.Browse = function(map, type) {
265 page.pushstate = page.popstate = function(path, id) {
266 OSM.loadSidebarContent(path, function() {
271 page.load = function(path, id) {
272 addObject(type, id, true);
275 function addObject(type, id, center) {
276 var bounds = map.addObject({type: type, id: parseInt(id)}, function(bounds) {
277 if (!window.location.hash && bounds.isValid() &&
278 (center || !map.getBounds().contains(bounds))) {
279 OSM.router.withoutMoveListener(function () {
280 map.fitBounds(bounds);
286 page.unload = function() {
293 var history = OSM.History(map);
295 OSM.router = OSM.Router(map, {
297 "/search": OSM.Search(map),
298 "/export": OSM.Export(map),
299 "/note/new": OSM.NewNote(map),
300 "/history/friends": history,
301 "/history/nearby": history,
303 "/user/:display_name/history": history,
304 "/note/:id": OSM.Note(map),
305 "/node/:id(/history)": OSM.Browse(map, 'node'),
306 "/way/:id(/history)": OSM.Browse(map, 'way'),
307 "/relation/:id(/history)": OSM.Browse(map, 'relation'),
308 "/changeset/:id": OSM.Changeset(map)
311 if (OSM.preferred_editor == "remote" && document.location.pathname == "/edit") {
312 remoteEditHandler(map.getBounds(), params.object);
313 OSM.router.setCurrentPath("/");
318 $(document).on("click", "a", function(e) {
319 if (e.isDefaultPrevented() || e.isPropagationStopped())
322 // Open links in a new tab as normal.
323 if (e.which > 1 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey)
326 // Ignore cross-protocol and cross-origin links.
327 if (location.protocol !== this.protocol || location.host !== this.host)
330 if (OSM.router.route(this.pathname + this.search + this.hash))
334 $(".search_form").on("submit", function(e) {
336 $("header").addClass("closed");
337 var query = $(this).find("input[name=query]").val();
339 OSM.router.route("/search?query=" + encodeURIComponent(query) + OSM.formatHash(map));
341 OSM.router.route("/");
345 $(".describe_location").on("click", function(e) {
347 var center = map.getCenter().wrap(),
348 precision = OSM.zoomPrecision(map.getZoom());
349 OSM.router.route("/search?query=" + encodeURIComponent(
350 center.lat.toFixed(precision) + "," + center.lng.toFixed(precision)