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
15 $(document).ready(function () {
16 var params = OSM.mapParams();
18 var map = L.map("map", {
23 map.attributionControl.setPrefix('');
25 map.hash = L.hash(map);
27 $(window).on('popstate', function(e) {
28 // popstate is triggered when the hash changes as well as on actual navigation
29 // events. We want to update the hash on the latter and not the former.
30 if (e.originalEvent.state) {
35 var copyright = I18n.t('javascripts.map.copyright', {copyright_url: '/copyright'});
36 var donate = I18n.t('javascripts.map.donate_link_text', {donate_url: 'http://donate.openstreetmap.org'});
40 attribution: copyright + " ♥ " + donate,
43 name: I18n.t("javascripts.map.base.standard")
46 attribution: copyright + ". Tiles courtesy of <a href='http://www.opencyclemap.org/' target='_blank'>Andy Allan</a>",
49 name: I18n.t("javascripts.map.base.cycle_map")
51 new L.OSM.TransportMap({
52 attribution: copyright + ". Tiles courtesy of <a href='http://www.opencyclemap.org/' target='_blank'>Andy Allan</a>",
54 keyid: "transportmap",
55 name: I18n.t("javascripts.map.base.transport_map")
57 new L.OSM.MapQuestOpen({
58 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'>",
61 name: I18n.t("javascripts.map.base.mapquest")
64 attribution: copyright + ". Tiles courtesy of <a href='http://hot.openstreetmap.org/' target='_blank'>Humanitarian OpenStreetMap Team</a>",
67 name: I18n.t("javascripts.map.base.hot")
71 function updateLayers(params) {
72 var layerParam = params.layers || "M";
75 for (var i = layers.length - 1; i >= 0; i--) {
76 if (layerParam.indexOf(layers[i].options.code) >= 0) {
77 map.addLayer(layers[i]);
78 layersAdded = layersAdded + layers[i].options.code;
79 } else if (i == 0 && layersAdded == "") {
80 map.addLayer(layers[i]);
82 map.removeLayer(layers[i]);
89 $(window).on("hashchange", function () {
90 updateLayers(OSM.mapParams());
93 map.on("baselayerchange", function (e) {
94 if (map.getZoom() > e.layer.options.maxZoom) {
95 map.setView(map.getCenter(), e.layer.options.maxZoom, { reset: true });
99 map.noteLayer = new L.LayerGroup();
100 map.noteLayer.options = {code: 'N'};
102 map.dataLayer = new L.OSM.DataLayer(null);
103 map.dataLayer.options.code = 'D';
105 var position = $('html').attr('dir') === 'rtl' ? 'topleft' : 'topright';
107 L.OSM.zoom({position: position})
113 title: I18n.t('javascripts.map.locate.title'),
114 popup: I18n.t('javascripts.map.locate.popup')
118 var sidebar = L.OSM.sidebar('#map-ui')
146 if (OSM.STATUS != 'api_offline' && OSM.STATUS != 'database_offline') {
147 initializeNotes(map);
148 if (params.layers.indexOf(map.noteLayer.options.code) >= 0) {
149 map.addLayer(map.noteLayer);
152 initializeBrowse(map);
153 if (params.layers.indexOf(map.dataLayer.options.code) >= 0) {
154 map.addLayer(map.dataLayer);
158 $('.leaflet-control .control-button').tooltip({placement: 'left', container: 'body'});
160 map.on('moveend layeradd layerremove', function() {
162 map.getCenter().wrap(),
167 var expiry = new Date();
168 expiry.setYear(expiry.getFullYear() + 10);
169 $.cookie("_osm_location", cookieContent(map), { expires: expiry });
171 // Trigger hash update on layer changes.
172 map.hash.onMapMove();
176 map.on('layeradd', function (e) {
177 if (e.layer.options) {
178 var goal = OSM.PIWIK.goals[e.layer.options.keyid];
181 $('body').trigger('piwikgoal', goal);
188 map.fitBounds(params.bounds);
190 map.setView([params.lat, params.lon], params.zoom);
193 var marker = L.marker([0, 0], {icon: getUserIcon()});
196 marker.setLatLng([params.mlat, params.mlon]).addTo(map);
199 $("#homeanchor").on("click", function(e) {
202 var data = $(this).data(),
203 center = L.latLng(data.lat, data.lon);
205 map.setView(center, data.zoom);
206 marker.setLatLng(center).addTo(map);
209 $("a[data-editor=remote]").click(function(e) {
210 remoteEditHandler(map.getBounds());
214 if (OSM.preferred_editor == "remote" && $('body').hasClass("site-edit")) {
215 remoteEditHandler(map.getBounds());
218 if (OSM.params().edit_help) {
223 title: I18n.t('javascripts.edit_help')
227 $('body').one('click', function() {
228 $('#editanchor').tooltip('hide');
232 OSM.Index = function(map) {
235 page.pushstate = page.popstate = function(path) {
236 $("#content").addClass("overlay-sidebar");
237 map.invalidateSize();
238 $('#sidebar_content').load(path + "?xhr=1", function(a, b, xhr) {
239 if (xhr.getResponseHeader('X-Page-Title')) {
240 document.title = xhr.getResponseHeader('X-Page-Title');
245 page.unload = function() {
246 $("#content").removeClass("overlay-sidebar");
247 map.invalidateSize();
253 OSM.Browse = function(map) {
256 page.pushstate = page.popstate = function(path, type, id) {
257 $('#sidebar_content').load(path + "?xhr=1", function(a, b, xhr) {
258 if (xhr.getResponseHeader('X-Page-Title')) {
259 document.title = xhr.getResponseHeader('X-Page-Title');
261 page.load(path, type, id);
265 page.load = function(path, type, id) {
266 if (OSM.STATUS === 'api_offline' || OSM.STATUS === 'database_offline') return;
268 if (type === 'note') {
269 map.noteLayer.showNote(parseInt(id));
271 map.addObject({type: type, id: parseInt(id)}, {zoom: true});
275 page.unload = function() {
282 var history = OSM.History(map);
284 OSM.route = OSM.Router({
286 "/search": OSM.Search(map),
287 "/export": OSM.Export(map),
289 "/user/:display_name/edits": history,
290 "/browse/friends": history,
291 "/browse/nearby": history,
292 "/browse/:type/:id(/history)": OSM.Browse(map)
295 $(document).on("click", "a", function(e) {
296 if (e.isDefaultPrevented() || e.isPropagationStopped()) return;
297 if (this.host === window.location.host && OSM.route(this.pathname + this.search + this.hash)) e.preventDefault();
300 $(".search_form").on("submit", function(e) {
302 $("header").addClass("closed");
303 OSM.route("/search?query=" + encodeURIComponent($(this).find("input[name=query]").val()) + OSM.formatHash(map));
306 $(".describe_location").on("click", function(e) {
308 var precision = zoomPrecision(map.getZoom());
309 OSM.route("/search?query=" + encodeURIComponent(
310 map.getCenter().lat.toFixed(precision) + "," +
311 map.getCenter().lng.toFixed(precision)));