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 index/search
9 //= require index/browse
10 //= require index/export
11 //= require index/notes
12 //= require index/history
13 //= require index/note
16 $(document).ready(function () {
17 var params = OSM.mapParams();
19 var map = L.map("map", {
24 map.attributionControl.setPrefix('');
26 map.hash = L.hash(map);
28 $(window).on('popstate', function(e) {
29 // popstate is triggered when the hash changes as well as on actual navigation
30 // events. We want to update the hash on the latter and not the former.
31 if (e.originalEvent.state) {
36 var copyright = I18n.t('javascripts.map.copyright', {copyright_url: '/copyright'});
37 var donate = I18n.t('javascripts.map.donate_link_text', {donate_url: 'http://donate.openstreetmap.org'});
41 attribution: copyright + " ♥ " + donate,
44 name: I18n.t("javascripts.map.base.standard")
47 attribution: copyright + ". Tiles courtesy of <a href='http://www.opencyclemap.org/' target='_blank'>Andy Allan</a>",
50 name: I18n.t("javascripts.map.base.cycle_map")
52 new L.OSM.TransportMap({
53 attribution: copyright + ". Tiles courtesy of <a href='http://www.opencyclemap.org/' target='_blank'>Andy Allan</a>",
55 keyid: "transportmap",
56 name: I18n.t("javascripts.map.base.transport_map")
58 new L.OSM.MapQuestOpen({
59 attribution: copyright + ". Tiles courtesy of <a href='http://www.mapquest.com/' target='_blank'>MapQuest</a> <img src='http://developer.mapquest.com/content/osm/mq_logo.png'>",
62 name: I18n.t("javascripts.map.base.mapquest")
65 attribution: copyright + ". Tiles courtesy of <a href='http://hot.openstreetmap.org/' target='_blank'>Humanitarian OpenStreetMap Team</a>",
68 name: I18n.t("javascripts.map.base.hot")
72 function updateLayers(params) {
73 var layerParam = params.layers || "M";
76 for (var i = layers.length - 1; i >= 0; i--) {
77 if (layerParam.indexOf(layers[i].options.code) >= 0) {
78 map.addLayer(layers[i]);
79 layersAdded = layersAdded + layers[i].options.code;
80 } else if (i == 0 && layersAdded == "") {
81 map.addLayer(layers[i]);
83 map.removeLayer(layers[i]);
90 $(window).on("hashchange", function () {
91 updateLayers(OSM.mapParams());
94 map.on("baselayerchange", function (e) {
95 if (map.getZoom() > e.layer.options.maxZoom) {
96 map.setView(map.getCenter(), e.layer.options.maxZoom, { reset: true });
100 map.noteLayer = new L.LayerGroup();
101 map.noteLayer.options = {code: 'N'};
103 map.dataLayer = new L.OSM.DataLayer(null);
104 map.dataLayer.options.code = 'D';
106 var position = $('html').attr('dir') === 'rtl' ? 'topleft' : 'topright';
108 L.OSM.zoom({position: position})
114 title: I18n.t('javascripts.map.locate.title'),
115 popup: I18n.t('javascripts.map.locate.popup')
119 var sidebar = L.OSM.sidebar('#map-ui')
147 if (OSM.STATUS != 'api_offline' && OSM.STATUS != 'database_offline') {
148 initializeNotes(map);
149 if (params.layers.indexOf(map.noteLayer.options.code) >= 0) {
150 map.addLayer(map.noteLayer);
153 initializeBrowse(map);
154 if (params.layers.indexOf(map.dataLayer.options.code) >= 0) {
155 map.addLayer(map.dataLayer);
159 $('.leaflet-control .control-button').tooltip({placement: 'left', container: 'body'});
161 map.on('moveend layeradd layerremove', function() {
163 map.getCenter().wrap(),
168 var expiry = new Date();
169 expiry.setYear(expiry.getFullYear() + 10);
170 $.cookie("_osm_location", cookieContent(map), { expires: expiry });
172 // Trigger hash update on layer changes.
173 map.hash.onMapMove();
177 map.on('layeradd', function (e) {
178 if (e.layer.options) {
179 var goal = OSM.PIWIK.goals[e.layer.options.keyid];
182 $('body').trigger('piwikgoal', goal);
189 map.fitBounds(params.bounds);
191 map.setView([params.lat, params.lon], params.zoom);
194 var marker = L.marker([0, 0], {icon: getUserIcon()});
197 marker.setLatLng([params.mlat, params.mlon]).addTo(map);
200 $("#homeanchor").on("click", function(e) {
203 var data = $(this).data(),
204 center = L.latLng(data.lat, data.lon);
206 map.setView(center, data.zoom);
207 marker.setLatLng(center).addTo(map);
210 $("a[data-editor=remote]").click(function(e) {
211 remoteEditHandler(map.getBounds());
215 if (OSM.preferred_editor == "remote" && $('body').hasClass("site-edit")) {
216 remoteEditHandler(map.getBounds());
219 if (OSM.params().edit_help) {
224 title: I18n.t('javascripts.edit_help')
228 $('body').one('click', function() {
229 $('#editanchor').tooltip('hide');
233 OSM.Index = function(map) {
236 page.pushstate = page.popstate = function(path) {
237 $("#content").addClass("overlay-sidebar");
238 map.invalidateSize();
239 $('#sidebar_content').load(path + "?xhr=1", function(a, b, xhr) {
240 if (xhr.getResponseHeader('X-Page-Title')) {
241 document.title = xhr.getResponseHeader('X-Page-Title');
246 page.unload = function() {
247 $("#content").removeClass("overlay-sidebar");
248 map.invalidateSize();
254 OSM.Browse = function(map) {
257 page.pushstate = page.popstate = function(path, type, id) {
258 $('#sidebar_content').load(path + "?xhr=1", function(a, b, xhr) {
259 if (xhr.getResponseHeader('X-Page-Title')) {
260 document.title = xhr.getResponseHeader('X-Page-Title');
262 page.load(path, type, id);
266 page.load = function(path, type, id) {
267 if (OSM.STATUS === 'api_offline' || OSM.STATUS === 'database_offline') return;
269 if (type === 'note') {
270 map.noteLayer.showNote(parseInt(id));
272 map.addObject({type: type, id: parseInt(id)}, {zoom: true});
276 page.unload = function() {
283 var history = OSM.History(map),
284 note = OSM.Note(map);
286 OSM.route = OSM.Router({
288 "/search": OSM.Search(map),
289 "/export": OSM.Export(map),
291 "/user/:display_name/edits": history,
292 "/browse/friends": history,
293 "/browse/nearby": history,
294 "/browse/note/:id": note,
295 "/browse/:type/:id(/history)": OSM.Browse(map)
298 $(document).on("click", "a", function(e) {
299 if (e.isDefaultPrevented() || e.isPropagationStopped()) return;
300 if (this.host === window.location.host && OSM.route(this.pathname + this.search + this.hash)) e.preventDefault();
303 $(".search_form").on("submit", function(e) {
305 $("header").addClass("closed");
306 OSM.route("/search?query=" + encodeURIComponent($(this).find("input[name=query]").val()) + OSM.formatHash(map));
309 $(".describe_location").on("click", function(e) {
311 var precision = zoomPrecision(map.getZoom());
312 OSM.route("/search?query=" + encodeURIComponent(
313 map.getCenter().lat.toFixed(precision) + "," +
314 map.getCenter().lng.toFixed(precision)));