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
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,
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", cookieContent(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 = function() {
226 $("#content").addClass("overlay-sidebar");
227 map.invalidateSize({pan: false})
228 .panBy([-350, 0], {animate: false});
229 document.title = I18n.t('layouts.project_name.title');
232 page.load = function() {
233 if (!("autofocus" in document.createElement("input"))) {
234 $("#sidebar .search_form input[name=query]").focus();
236 return map.getState();
239 page.popstate = function() {
240 $("#content").addClass("overlay-sidebar");
241 map.invalidateSize({pan: false});
242 document.title = I18n.t('layouts.project_name.title');
245 page.unload = function() {
246 map.panBy([350, 0], {animate: false});
247 $("#content").removeClass("overlay-sidebar");
248 map.invalidateSize({pan: false});
254 OSM.Browse = function(map, type) {
257 page.pushstate = page.popstate = function(path, id) {
258 OSM.loadSidebarContent(path, function() {
263 page.load = function(path, id) {
264 addObject(type, id, true);
267 function addObject(type, id, center) {
268 var bounds = map.addObject({type: type, id: parseInt(id)}, function(bounds) {
269 if (!window.location.hash && bounds.isValid()) {
270 OSM.router.moveListenerOff();
271 map.once('moveend', OSM.router.moveListenerOn);
272 if (center || !map.getBounds().contains(bounds)) map.fitBounds(bounds);
277 page.unload = function() {
284 var history = OSM.History(map);
286 OSM.router = OSM.Router(map, {
288 "/search": OSM.Search(map),
289 "/export": OSM.Export(map),
290 "/note/new": OSM.NewNote(map),
291 "/history/friends": history,
292 "/history/nearby": history,
294 "/user/:display_name/history": history,
295 "/note/:id": OSM.Note(map),
296 "/node/:id(/history)": OSM.Browse(map, 'node'),
297 "/way/:id(/history)": OSM.Browse(map, 'way'),
298 "/relation/:id(/history)": OSM.Browse(map, 'relation'),
299 "/changeset/:id": OSM.Browse(map, 'changeset')
302 if (OSM.preferred_editor == "remote" && document.location.pathname == "/edit") {
303 remoteEditHandler(map.getBounds(), params.object);
304 OSM.router.setCurrentPath("/");
309 $(document).on("click", "a", function(e) {
310 if (e.isDefaultPrevented() || e.isPropagationStopped())
313 // Open links in a new tab as normal.
314 if (e.which > 1 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey)
317 // Ignore cross-protocol and cross-origin links.
318 if (location.protocol !== this.protocol || location.host !== this.host)
321 if (OSM.router.route(this.pathname + this.search + this.hash))
325 $(".search_form").on("submit", function(e) {
327 if ($(".query_wrapper.routing").is(":visible")) {
329 OSM.routing.requestRoute();
332 $("header").addClass("closed");
333 var query = $(this).find("input[name=query]").val();
335 OSM.router.route("/search?query=" + encodeURIComponent(query) + OSM.formatHash(map));
337 OSM.router.route("/" + OSM.formatHash(map));
342 $(".describe_location").on("click", function(e) {
344 var precision = zoomPrecision(map.getZoom());
345 OSM.router.route("/search?query=" + encodeURIComponent(
346 map.getCenter().lat.toFixed(precision) + "," +
347 map.getCenter().lng.toFixed(precision)));
350 $(".get_directions").on("click",function(e) {
353 $(".routing").show();
354 $(".query_wrapper.routing [name=route_from]").focus();
357 $(".close_directions").on("click",function(e) {
360 $(".routing").hide();
361 $(".query_wrapper.search [name=query]").focus();
364 OSM.routing = OSM.Routing(map,'OSM.routing',$('.query_wrapper.routing'));