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 (center || !map.getBounds().contains(bounds))) {
276 OSM.router.withoutMoveListener(function () {
277 map.fitBounds(bounds);
283 page.unload = function() {
290 var history = OSM.History(map);
292 OSM.router = OSM.Router(map, {
294 "/search": OSM.Search(map),
295 "/export": OSM.Export(map),
296 "/note/new": OSM.NewNote(map),
297 "/history/friends": history,
298 "/history/nearby": history,
300 "/user/:display_name/history": history,
301 "/note/:id": OSM.Note(map),
302 "/node/:id(/history)": OSM.Browse(map, 'node'),
303 "/way/:id(/history)": OSM.Browse(map, 'way'),
304 "/relation/:id(/history)": OSM.Browse(map, 'relation'),
305 "/changeset/:id": OSM.Browse(map, 'changeset'),
306 "/query": OSM.Query(map)
309 if (OSM.preferred_editor == "remote" && document.location.pathname == "/edit") {
310 remoteEditHandler(map.getBounds(), params.object);
311 OSM.router.setCurrentPath("/");
316 $(document).on("click", "a", function(e) {
317 if (e.isDefaultPrevented() || e.isPropagationStopped())
320 // Open links in a new tab as normal.
321 if (e.which > 1 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey)
324 // Ignore cross-protocol and cross-origin links.
325 if (location.protocol !== this.protocol || location.host !== this.host)
328 if (OSM.router.route(this.pathname + this.search + this.hash))
332 $(".search_form").on("submit", function(e) {
334 $("header").addClass("closed");
335 var query = $(this).find("input[name=query]").val();
337 OSM.router.route("/search?query=" + encodeURIComponent(query) + OSM.formatHash(map));
339 OSM.router.route("/" + OSM.formatHash(map));
343 $(".describe_location").on("click", function(e) {
345 var precision = OSM.zoomPrecision(map.getZoom());
346 OSM.router.route("/search?query=" + encodeURIComponent(
347 map.getCenter().lat.toFixed(precision) + "," +
348 map.getCenter().lng.toFixed(precision)));