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.query
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/query
22 OSM.loadSidebarContent = function(path, callback) {
23 clearTimeout(loaderTimeout);
25 loaderTimeout = setTimeout(function() {
26 $('#sidebar_loader').show();
29 // IE<10 doesn't respect Vary: X-Requested-With header, so
30 // prevent caching the XHR response as a full-page URL.
31 if (path.indexOf('?') >= 0) {
43 complete: function(xhr) {
44 clearTimeout(loaderTimeout);
46 $('#sidebar_loader').hide();
48 var content = $(xhr.responseText);
50 if (xhr.getResponseHeader('X-Page-Title')) {
51 var title = xhr.getResponseHeader('X-Page-Title');
52 document.title = decodeURIComponent(escape(title));
56 .find('link[type="application/atom+xml"]')
60 .append(content.filter('link[type="application/atom+xml"]'));
62 $('#sidebar_content').html(content.not('link[type="application/atom+xml"]'));
72 $(document).ready(function () {
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,
136 if (OSM.STATUS != 'api_offline' && OSM.STATUS != 'database_offline') {
137 initializeNotes(map);
138 if (params.layers.indexOf(map.noteLayer.options.code) >= 0) {
139 map.addLayer(map.noteLayer);
142 initializeBrowse(map);
143 if (params.layers.indexOf(map.dataLayer.options.code) >= 0) {
144 map.addLayer(map.dataLayer);
148 $('.leaflet-control .control-button').tooltip({placement: 'left', container: 'body'});
150 var expiry = new Date();
151 expiry.setYear(expiry.getFullYear() + 10);
153 map.on('moveend layeradd layerremove', function() {
155 map.getCenter().wrap(),
160 $.removeCookie("_osm_location");
161 $.cookie("_osm_location", OSM.locationCookie(map), { expires: expiry, path: "/" });
164 if ($.cookie('_osm_welcome') == 'hide') {
165 $('.welcome').hide();
168 $('.welcome .close').on('click', function() {
169 $('.welcome').hide();
170 $.cookie("_osm_welcome", 'hide', { expires: expiry });
174 map.on('layeradd', function (e) {
175 if (e.layer.options) {
176 var goal = OSM.PIWIK.goals[e.layer.options.keyid];
179 $('body').trigger('piwikgoal', goal);
186 map.fitBounds(params.bounds);
188 map.setView([params.lat, params.lon], params.zoom);
191 var marker = L.marker([0, 0], {icon: getUserIcon()});
194 marker.setLatLng([params.mlat, params.mlon]).addTo(map);
197 $("#homeanchor").on("click", function(e) {
200 var data = $(this).data(),
201 center = L.latLng(data.lat, data.lon);
203 map.setView(center, data.zoom);
204 marker.setLatLng(center).addTo(map);
207 $("a[data-editor=remote]").click(function(e) {
208 var params = OSM.mapParams(this.search);
209 remoteEditHandler(map.getBounds(), params.object);
213 if (OSM.params().edit_help) {
218 title: I18n.t('javascripts.edit_help')
222 $('body').one('click', function() {
223 $('#editanchor').tooltip('hide');
227 OSM.Index = function(map) {
230 page.pushstate = function() {
231 $("#content").addClass("overlay-sidebar");
232 map.invalidateSize({pan: false})
233 .panBy([-350, 0], {animate: false});
234 document.title = I18n.t('layouts.project_name.title');
237 page.load = function() {
238 if (!("autofocus" in document.createElement("input"))) {
239 $("#sidebar .search_form input[name=query]").focus();
241 return map.getState();
244 page.popstate = function() {
245 $("#content").addClass("overlay-sidebar");
246 map.invalidateSize({pan: false});
247 document.title = I18n.t('layouts.project_name.title');
250 page.unload = function() {
251 map.panBy([350, 0], {animate: false});
252 $("#content").removeClass("overlay-sidebar");
253 map.invalidateSize({pan: false});
259 OSM.Browse = function(map, type) {
262 page.pushstate = page.popstate = function(path, id) {
263 OSM.loadSidebarContent(path, function() {
268 page.load = function(path, id) {
269 addObject(type, id, true);
272 function addObject(type, id, center) {
273 var bounds = map.addObject({type: type, id: parseInt(id)}, function(bounds) {
274 if (!window.location.hash && bounds.isValid()) {
275 OSM.router.moveListenerOff();
276 map.once('moveend', OSM.router.moveListenerOn);
277 if (center || !map.getBounds().contains(bounds)) map.fitBounds(bounds);
282 page.unload = function() {
289 var history = OSM.History(map);
291 OSM.router = OSM.Router(map, {
293 "/search": OSM.Search(map),
294 "/export": OSM.Export(map),
295 "/note/new": OSM.NewNote(map),
296 "/history/friends": history,
297 "/history/nearby": history,
299 "/user/:display_name/history": history,
300 "/note/:id": OSM.Note(map),
301 "/node/:id(/history)": OSM.Browse(map, 'node'),
302 "/way/:id(/history)": OSM.Browse(map, 'way'),
303 "/relation/:id(/history)": OSM.Browse(map, 'relation'),
304 "/changeset/:id": OSM.Browse(map, 'changeset'),
305 "/query": OSM.Query(map)
308 if (OSM.preferred_editor == "remote" && document.location.pathname == "/edit") {
309 remoteEditHandler(map.getBounds(), params.object);
310 OSM.router.setCurrentPath("/");
315 $(document).on("click", "a", function(e) {
316 if (e.isDefaultPrevented() || e.isPropagationStopped())
319 // Open links in a new tab as normal.
320 if (e.which > 1 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey)
323 // Ignore cross-protocol and cross-origin links.
324 if (location.protocol !== this.protocol || location.host !== this.host)
327 if (OSM.router.route(this.pathname + this.search + this.hash))
331 $(".search_form").on("submit", function(e) {
333 $("header").addClass("closed");
334 var query = $(this).find("input[name=query]").val();
336 OSM.router.route("/search?query=" + encodeURIComponent(query) + OSM.formatHash(map));
338 OSM.router.route("/" + OSM.formatHash(map));
342 $(".describe_location").on("click", function(e) {
344 var precision = OSM.zoomPrecision(map.getZoom());
345 OSM.router.route("/search?query=" + encodeURIComponent(
346 map.getCenter().lat.toFixed(precision) + "," +
347 map.getCenter().lng.toFixed(precision)));