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
20 OSM.loadSidebarContent = function(path, callback) {
21 clearTimeout(loaderTimeout);
23 loaderTimeout = setTimeout(function() {
24 $('#sidebar_loader').show();
27 // IE<10 doesn't respect Vary: X-Requested-With header, so
28 // prevent caching the XHR response as a full-page URL.
29 if (path.indexOf('?') >= 0) {
41 complete: function(xhr) {
42 clearTimeout(loaderTimeout);
44 $('#sidebar_loader').hide();
46 var content = $(xhr.responseText);
48 if (xhr.getResponseHeader('X-Page-Title')) {
49 var title = xhr.getResponseHeader('X-Page-Title');
50 document.title = decodeURIComponent(escape(title));
54 .find('link[type="application/atom+xml"]')
58 .append(content.filter('link[type="application/atom+xml"]'));
60 $('#sidebar_content').html(content.not('link[type="application/atom+xml"]'));
70 $(document).ready(function () {
71 var params = OSM.mapParams();
73 var map = new L.OSM.Map("map", {
78 map.attributionControl.setPrefix('');
80 map.updateLayers(params.layers);
82 map.on("baselayerchange", function (e) {
83 if (map.getZoom() > e.layer.options.maxZoom) {
84 map.setView(map.getCenter(), e.layer.options.maxZoom, { reset: true });
88 var position = $('html').attr('dir') === 'rtl' ? 'topleft' : 'topright';
90 L.OSM.zoom({position: position})
96 title: I18n.t('javascripts.map.locate.title'),
97 popup: I18n.t('javascripts.map.locate.popup')
101 var sidebar = L.OSM.sidebar('#map-ui')
106 layers: map.baseLayers,
129 if (OSM.STATUS != 'api_offline' && OSM.STATUS != 'database_offline') {
130 initializeNotes(map);
131 if (params.layers.indexOf(map.noteLayer.options.code) >= 0) {
132 map.addLayer(map.noteLayer);
135 initializeBrowse(map);
136 if (params.layers.indexOf(map.dataLayer.options.code) >= 0) {
137 map.addLayer(map.dataLayer);
141 $('.leaflet-control .control-button').tooltip({placement: 'left', container: 'body'});
143 var expiry = new Date();
144 expiry.setYear(expiry.getFullYear() + 10);
146 map.on('moveend layeradd layerremove', function() {
148 map.getCenter().wrap(),
153 $.removeCookie("_osm_location");
154 $.cookie("_osm_location", OSM.locationCookie(map), { expires: expiry, path: "/" });
157 if ($.cookie('_osm_welcome') == 'hide') {
158 $('.welcome').hide();
161 $('.welcome .close').on('click', function() {
162 $('.welcome').hide();
163 $.cookie("_osm_welcome", 'hide', { expires: expiry });
167 map.on('layeradd', function (e) {
168 if (e.layer.options) {
169 var goal = OSM.PIWIK.goals[e.layer.options.keyid];
172 $('body').trigger('piwikgoal', goal);
179 map.fitBounds(params.bounds);
181 map.setView([params.lat, params.lon], params.zoom);
184 var marker = L.marker([0, 0], {icon: getUserIcon()});
187 marker.setLatLng([params.mlat, params.mlon]).addTo(map);
190 $("#homeanchor").on("click", function(e) {
193 var data = $(this).data(),
194 center = L.latLng(data.lat, data.lon);
196 map.setView(center, data.zoom);
197 marker.setLatLng(center).addTo(map);
200 $("a[data-editor=remote]").click(function(e) {
201 var params = OSM.mapParams(this.search);
202 remoteEditHandler(map.getBounds(), params.object);
206 if (OSM.params().edit_help) {
211 title: I18n.t('javascripts.edit_help')
215 $('body').one('click', function() {
216 $('#editanchor').tooltip('hide');
220 OSM.Index = function(map) {
223 page.pushstate = function() {
224 $("#content").addClass("overlay-sidebar");
225 map.invalidateSize({pan: false})
226 .panBy([-350, 0], {animate: false});
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();
237 page.popstate = function() {
238 $("#content").addClass("overlay-sidebar");
239 map.invalidateSize({pan: false});
240 document.title = I18n.t('layouts.project_name.title');
243 page.unload = function() {
244 map.panBy([350, 0], {animate: false});
245 $("#content").removeClass("overlay-sidebar");
246 map.invalidateSize({pan: false});
252 OSM.Browse = function(map, type) {
255 page.pushstate = page.popstate = function(path, id) {
256 OSM.loadSidebarContent(path, function() {
261 page.load = function(path, id) {
262 addObject(type, id, true);
265 function addObject(type, id, center) {
266 var bounds = map.addObject({type: type, id: parseInt(id)}, function(bounds) {
267 if (!window.location.hash && bounds.isValid()) {
268 OSM.router.moveListenerOff();
269 map.once('moveend', OSM.router.moveListenerOn);
270 if (center || !map.getBounds().contains(bounds)) map.fitBounds(bounds);
275 page.unload = function() {
282 var history = OSM.History(map);
284 OSM.router = OSM.Router(map, {
286 "/search": OSM.Search(map),
287 "/export": OSM.Export(map),
288 "/note/new": OSM.NewNote(map),
289 "/history/friends": history,
290 "/history/nearby": history,
292 "/user/:display_name/history": history,
293 "/note/:id": OSM.Note(map),
294 "/node/:id(/history)": OSM.Browse(map, 'node'),
295 "/way/:id(/history)": OSM.Browse(map, 'way'),
296 "/relation/:id(/history)": OSM.Browse(map, 'relation'),
297 "/changeset/:id": OSM.Browse(map, 'changeset')
300 if (OSM.preferred_editor == "remote" && document.location.pathname == "/edit") {
301 remoteEditHandler(map.getBounds(), params.object);
302 OSM.router.setCurrentPath("/");
307 $(document).on("click", "a", function(e) {
308 if (e.isDefaultPrevented() || e.isPropagationStopped())
311 // Open links in a new tab as normal.
312 if (e.which > 1 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey)
315 // Ignore cross-protocol and cross-origin links.
316 if (location.protocol !== this.protocol || location.host !== this.host)
319 if (OSM.router.route(this.pathname + this.search + this.hash))
323 $(".search_form").on("submit", function(e) {
325 $("header").addClass("closed");
326 var query = $(this).find("input[name=query]").val();
328 OSM.router.route("/search?query=" + encodeURIComponent(query) + OSM.formatHash(map));
330 OSM.router.route("/" + OSM.formatHash(map));
334 $(".describe_location").on("click", function(e) {
336 var precision = OSM.zoomPrecision(map.getZoom());
337 OSM.router.route("/search?query=" + encodeURIComponent(
338 map.getCenter().lat.toFixed(precision) + "," +
339 map.getCenter().lng.toFixed(precision)));