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_welcome') == 'hide') {
166 $('.welcome').hide();
169 $('.welcome .close').on('click', function() {
170 $('.welcome').hide();
171 $.cookie("_osm_welcome", 'hide', { expires: expiry });
175 map.on('layeradd', function (e) {
176 if (e.layer.options) {
177 var goal = OSM.PIWIK.goals[e.layer.options.keyid];
180 $('body').trigger('piwikgoal', goal);
187 map.fitBounds(params.bounds);
189 map.setView([params.lat, params.lon], params.zoom);
192 var marker = L.marker([0, 0], {icon: getUserIcon()});
195 marker.setLatLng([params.mlat, params.mlon]).addTo(map);
198 $("#homeanchor").on("click", function(e) {
201 var data = $(this).data(),
202 center = L.latLng(data.lat, data.lon);
204 map.setView(center, data.zoom);
205 marker.setLatLng(center).addTo(map);
208 $("a[data-editor=remote]").click(function(e) {
209 var params = OSM.mapParams(this.search);
210 remoteEditHandler(map.getBounds(), params.object);
214 if (OSM.params().edit_help) {
219 title: I18n.t('javascripts.edit_help')
223 $('body').one('click', function() {
224 $('#editanchor').tooltip('hide');
228 OSM.Index = function(map) {
231 page.pushstate = function() {
232 $("#content").addClass("overlay-sidebar");
233 map.invalidateSize({pan: false})
234 .panBy([-350, 0], {animate: false});
235 document.title = I18n.t('layouts.project_name.title');
238 page.load = function() {
239 if (!("autofocus" in document.createElement("input"))) {
240 $("#sidebar .search_form input[name=query]").focus();
242 return map.getState();
245 page.popstate = function() {
246 $("#content").addClass("overlay-sidebar");
247 map.invalidateSize({pan: false});
248 document.title = I18n.t('layouts.project_name.title');
251 page.unload = function() {
252 map.panBy([350, 0], {animate: false});
253 $("#content").removeClass("overlay-sidebar");
254 map.invalidateSize({pan: false});
260 OSM.Browse = function(map, type) {
263 page.pushstate = page.popstate = function(path, id) {
264 OSM.loadSidebarContent(path, function() {
269 page.load = function(path, id) {
270 addObject(type, id, true);
273 function addObject(type, id, center) {
274 var bounds = map.addObject({type: type, id: parseInt(id)}, function(bounds) {
275 if (!window.location.hash && bounds.isValid() &&
276 (center || !map.getBounds().contains(bounds))) {
277 OSM.router.withoutMoveListener(function () {
278 map.fitBounds(bounds);
284 page.unload = function() {
291 var history = OSM.History(map);
293 OSM.router = OSM.Router(map, {
295 "/search": OSM.Search(map),
296 "/export": OSM.Export(map),
297 "/note/new": OSM.NewNote(map),
298 "/history/friends": history,
299 "/history/nearby": history,
301 "/user/:display_name/history": history,
302 "/note/:id": OSM.Note(map),
303 "/node/:id(/history)": OSM.Browse(map, 'node'),
304 "/way/:id(/history)": OSM.Browse(map, 'way'),
305 "/relation/:id(/history)": OSM.Browse(map, 'relation'),
306 "/changeset/:id": OSM.Changeset(map),
307 "/query": OSM.Query(map)
310 if (OSM.preferred_editor == "remote" && document.location.pathname == "/edit") {
311 remoteEditHandler(map.getBounds(), params.object);
312 OSM.router.setCurrentPath("/");
317 $(document).on("click", "a", function(e) {
318 if (e.isDefaultPrevented() || e.isPropagationStopped())
321 // Open links in a new tab as normal.
322 if (e.which > 1 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey)
325 // Ignore cross-protocol and cross-origin links.
326 if (location.protocol !== this.protocol || location.host !== this.host)
329 if (OSM.router.route(this.pathname + this.search + this.hash))
333 $(".search_form").on("submit", function(e) {
335 $("header").addClass("closed");
336 var query = $(this).find("input[name=query]").val();
338 OSM.router.route("/search?query=" + encodeURIComponent(query) + OSM.formatHash(map));
340 OSM.router.route("/");
344 $(".describe_location").on("click", function(e) {
346 var center = map.getCenter().wrap(),
347 precision = OSM.zoomPrecision(map.getZoom());
348 OSM.router.route("/search?query=" + encodeURIComponent(
349 center.lat.toFixed(precision) + "," + center.lng.toFixed(precision)