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/changeset
17 //= require index/query
23 OSM.loadSidebarContent = function(path, callback) {
24 clearTimeout(loaderTimeout);
26 loaderTimeout = setTimeout(function() {
27 $('#sidebar_loader').show();
30 // IE<10 doesn't respect Vary: X-Requested-With header, so
31 // prevent caching the XHR response as a full-page URL.
32 if (path.indexOf('?') >= 0) {
44 complete: function(xhr) {
45 clearTimeout(loaderTimeout);
47 $('#sidebar_loader').hide();
49 var content = $(xhr.responseText);
51 if (xhr.getResponseHeader('X-Page-Title')) {
52 var title = xhr.getResponseHeader('X-Page-Title');
53 document.title = decodeURIComponent(escape(title));
57 .find('link[type="application/atom+xml"]')
61 .append(content.filter('link[type="application/atom+xml"]'));
63 $('#sidebar_content').html(content.not('link[type="application/atom+xml"]'));
73 $(document).ready(function () {
74 var params = OSM.mapParams();
76 var map = new L.OSM.Map("map", {
81 map.attributionControl.setPrefix('');
83 map.updateLayers(params.layers);
85 map.on("baselayerchange", function (e) {
86 if (map.getZoom() > e.layer.options.maxZoom) {
87 map.setView(map.getCenter(), e.layer.options.maxZoom, { reset: true });
91 var position = $('html').attr('dir') === 'rtl' ? 'topleft' : 'topright';
93 L.OSM.zoom({position: position})
99 title: I18n.t('javascripts.map.locate.title'),
100 popup: I18n.t('javascripts.map.locate.popup')
104 var sidebar = L.OSM.sidebar('#map-ui')
109 layers: map.baseLayers,
137 if (OSM.STATUS != 'api_offline' && OSM.STATUS != 'database_offline') {
138 initializeNotes(map);
139 if (params.layers.indexOf(map.noteLayer.options.code) >= 0) {
140 map.addLayer(map.noteLayer);
143 initializeBrowse(map);
144 if (params.layers.indexOf(map.dataLayer.options.code) >= 0) {
145 map.addLayer(map.dataLayer);
149 $('.leaflet-control .control-button').tooltip({placement: 'left', container: 'body'});
151 var expiry = new Date();
152 expiry.setYear(expiry.getFullYear() + 10);
154 map.on('moveend layeradd layerremove', function() {
156 map.getCenter().wrap(),
161 $.removeCookie("_osm_location");
162 $.cookie("_osm_location", OSM.locationCookie(map), { expires: expiry, path: "/" });
165 if ($.cookie('_osm_sotm') == 'hide') {
169 $('#sotm .close').on('click', function() {
171 $.cookie("_osm_sotm", 'hide', { expires: expiry });
174 if ($.cookie('_osm_welcome') == 'hide') {
175 $('.welcome').hide();
178 $('.welcome .close').on('click', function() {
179 $('.welcome').hide();
180 $.cookie("_osm_welcome", 'hide', { expires: expiry });
184 map.on('layeradd', function (e) {
185 if (e.layer.options) {
186 var goal = OSM.PIWIK.goals[e.layer.options.keyid];
189 $('body').trigger('piwikgoal', goal);
196 map.fitBounds(params.bounds);
198 map.setView([params.lat, params.lon], params.zoom);
201 var marker = L.marker([0, 0], {icon: getUserIcon()});
204 marker.setLatLng([params.mlat, params.mlon]).addTo(map);
207 $("#homeanchor").on("click", function(e) {
210 var data = $(this).data(),
211 center = L.latLng(data.lat, data.lon);
213 map.setView(center, data.zoom);
214 marker.setLatLng(center).addTo(map);
217 $("a[data-editor=remote]").click(function(e) {
218 var params = OSM.mapParams(this.search);
219 remoteEditHandler(map.getBounds(), params.object);
223 if (OSM.params().edit_help) {
228 title: I18n.t('javascripts.edit_help')
232 $('body').one('click', function() {
233 $('#editanchor').tooltip('hide');
237 OSM.Index = function(map) {
240 page.pushstate = function() {
241 $("#content").addClass("overlay-sidebar");
242 map.invalidateSize({pan: false})
243 .panBy([-350, 0], {animate: false});
244 document.title = I18n.t('layouts.project_name.title');
247 page.load = function() {
248 if (!("autofocus" in document.createElement("input"))) {
249 $("#sidebar .search_form input[name=query]").focus();
251 return map.getState();
254 page.popstate = function() {
255 $("#content").addClass("overlay-sidebar");
256 map.invalidateSize({pan: false});
257 document.title = I18n.t('layouts.project_name.title');
260 page.unload = function() {
261 map.panBy([350, 0], {animate: false});
262 $("#content").removeClass("overlay-sidebar");
263 map.invalidateSize({pan: false});
269 OSM.Browse = function(map, type) {
272 page.pushstate = page.popstate = function(path, id) {
273 OSM.loadSidebarContent(path, function() {
278 page.load = function(path, id) {
279 addObject(type, id, true);
282 function addObject(type, id, center) {
283 var bounds = map.addObject({type: type, id: parseInt(id)}, function(bounds) {
284 if (!window.location.hash && bounds.isValid() &&
285 (center || !map.getBounds().contains(bounds))) {
286 OSM.router.withoutMoveListener(function () {
287 map.fitBounds(bounds);
293 page.unload = function() {
300 var history = OSM.History(map);
302 OSM.router = OSM.Router(map, {
304 "/search": OSM.Search(map),
305 "/export": OSM.Export(map),
306 "/note/new": OSM.NewNote(map),
307 "/history/friends": history,
308 "/history/nearby": history,
310 "/user/:display_name/history": history,
311 "/note/:id": OSM.Note(map),
312 "/node/:id(/history)": OSM.Browse(map, 'node'),
313 "/way/:id(/history)": OSM.Browse(map, 'way'),
314 "/relation/:id(/history)": OSM.Browse(map, 'relation'),
315 "/changeset/:id": OSM.Changeset(map),
316 "/query": OSM.Query(map)
319 if (OSM.preferred_editor == "remote" && document.location.pathname == "/edit") {
320 remoteEditHandler(map.getBounds(), params.object);
321 OSM.router.setCurrentPath("/");
326 $(document).on("click", "a", function(e) {
327 if (e.isDefaultPrevented() || e.isPropagationStopped())
330 // Open links in a new tab as normal.
331 if (e.which > 1 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey)
334 // Ignore cross-protocol and cross-origin links.
335 if (location.protocol !== this.protocol || location.host !== this.host)
338 if (OSM.router.route(this.pathname + this.search + this.hash))
342 $(".search_form").on("submit", function(e) {
344 $("header").addClass("closed");
345 var query = $(this).find("input[name=query]").val();
347 OSM.router.route("/search?query=" + encodeURIComponent(query) + OSM.formatHash(map));
349 OSM.router.route("/");
353 $(".describe_location").on("click", function(e) {
355 var center = map.getCenter().wrap(),
356 precision = OSM.zoomPrecision(map.getZoom());
357 OSM.router.route("/search?query=" + encodeURIComponent(
358 center.lat.toFixed(precision) + "," + center.lng.toFixed(precision)