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'});
43 name: I18n.t("javascripts.map.base.standard")
46 attribution: copyright,
49 name: I18n.t("javascripts.map.base.standard")
52 attribution: copyright + ". Tiles courtesy of <a href='http://www.opencyclemap.org/' target='_blank'>Andy Allan</a>",
55 name: I18n.t("javascripts.map.base.cycle_map")
57 new L.OSM.TransportMap({
58 attribution: copyright + ". Tiles courtesy of <a href='http://www.opencyclemap.org/' target='_blank'>Andy Allan</a>",
60 keyid: "transportmap",
61 name: I18n.t("javascripts.map.base.transport_map")
63 new L.OSM.MapQuestOpen({
64 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'>",
67 name: I18n.t("javascripts.map.base.mapquest")
70 attribution: copyright + ". Tiles courtesy of <a href='http://hot.openstreetmap.org/' target='_blank'>Humanitarian OpenStreetMap Team</a>",
73 name: I18n.t("javascripts.map.base.hot")
77 function updateLayers(params) {
78 var layerParam = params.layers || "M";
81 for (var i = layers.length - 1; i >= 0; i--) {
82 if (layerParam.indexOf(layers[i].options.code) >= 0) {
83 map.addLayer(layers[i]);
84 layersAdded = layersAdded + layers[i].options.code;
85 } else if (i == 0 && layersAdded == "") {
86 map.addLayer(layers[i]);
88 map.removeLayer(layers[i]);
95 $(window).on("hashchange", function () {
96 updateLayers(OSM.mapParams());
99 map.on("baselayerchange", function (e) {
100 if (map.getZoom() > e.layer.options.maxZoom) {
101 map.setView(map.getCenter(), e.layer.options.maxZoom, { reset: true });
105 map.noteLayer = new L.LayerGroup();
106 map.noteLayer.options = {code: 'N'};
108 map.dataLayer = new L.OSM.DataLayer(null);
109 map.dataLayer.options.code = 'D';
111 if (OSM.STATUS != 'api_offline' && OSM.STATUS != 'database_offline') {
112 if (params.layers.indexOf(map.noteLayer.options.code) >= 0) {
113 map.addLayer(map.noteLayer);
116 if (params.layers.indexOf(map.dataLayer.options.code) >= 0) {
117 map.addLayer(map.dataLayer);
121 var position = $('html').attr('dir') === 'rtl' ? 'topleft' : 'topright';
123 L.OSM.zoom({position: position})
129 title: I18n.t('javascripts.map.locate.title'),
130 popup: I18n.t('javascripts.map.locate.popup')
134 var sidebar = L.OSM.sidebar('#map-ui')
162 $('.leaflet-control .control-button').tooltip({placement: 'left', container: 'body'});
164 map.on('moveend layeradd layerremove', function() {
166 map.getCenter().wrap(),
171 var expiry = new Date();
172 expiry.setYear(expiry.getFullYear() + 10);
173 $.cookie("_osm_location", cookieContent(map), { expires: expiry });
175 // Trigger hash update on layer changes.
176 map.hash.onMapMove();
180 map.on('layeradd', function (e) {
181 if (e.layer.options) {
182 var goal = OSM.PIWIK.goals[e.layer.options.keyid];
185 $('body').trigger('piwikgoal', goal);
192 map.fitBounds(params.bounds);
194 map.setView([params.lat, params.lon], params.zoom);
197 var marker = L.marker([0, 0], {icon: getUserIcon()});
200 marker.setLatLng([params.mlat, params.mlon]).addTo(map);
203 $("#homeanchor").on("click", function(e) {
206 var data = $(this).data(),
207 center = L.latLng(data.lat, data.lon);
209 map.setView(center, data.zoom);
210 marker.setLatLng(center).addTo(map);
213 $("a[data-editor=remote]").click(function(e) {
214 remoteEditHandler(map.getBounds());
218 if (OSM.preferred_editor == "remote" && $('body').hasClass("site-edit")) {
219 remoteEditHandler(map.getBounds());
222 if (OSM.params().edit_help) {
227 title: I18n.t('javascripts.edit_help')
231 $('body').one('click', function() {
232 $('#editanchor').tooltip('hide');
236 initializeBrowse(map);
237 initializeNotes(map);
239 OSM.Index = function(map) {
240 var page = {}, minimized = false;
242 page.pushstate = page.popstate = function(path) {
243 if (minimized) $("#sidebar").addClass("minimized");
244 map.invalidateSize();
245 $("#view_tab").addClass("current");
246 $('#sidebar_content').load(path);
249 page.unload = function() {
250 $("#view_tab").removeClass("current");
253 page.minimizeSidebar = function() {
254 $("#sidebar").addClass("minimized");
255 map.invalidateSize();
259 $(document).on("click", "#sidebar_content .close", page.minimizeSidebar);
264 OSM.Browse = function(map) {
267 page.pushstate = page.popstate = function(path, type, id) {
268 $("#sidebar").removeClass("minimized");
269 map.invalidateSize();
270 $('#sidebar_content').load(path, function() {
271 page.load(path, type, id);
275 page.load = function(path, type, id) {
276 if (OSM.STATUS === 'api_offline' || OSM.STATUS === 'database_offline') return;
278 if (type === 'note') {
279 map.noteLayer.showNote(parseInt(id));
281 map.addObject({type: type, id: parseInt(id)}, {zoom: true});
285 page.unload = function() {
292 var history = OSM.History(map);
294 OSM.route = OSM.Router({
296 "/search": OSM.Search(map),
297 "/export": OSM.Export(map),
299 "/user/:display_name/edits": history,
300 "/browse/friends": history,
301 "/browse/nearby": history,
302 "/browse/:type/:id(/history)": OSM.Browse(map)
305 $(document).on("click", "a", function(e) {
306 if (e.isDefaultPrevented() || e.isPropagationStopped()) return;
307 if (this.host === window.location.host && OSM.route(this.pathname + this.search + this.hash)) e.preventDefault();
310 $("#search_form").on("submit", function(e) {
312 OSM.route("/search?query=" + encodeURIComponent($("#query").val()) + OSM.formatHash(map));
315 $("#describe_location").on("click", function(e) {
317 var precision = zoomPrecision(map.getZoom());
318 OSM.route("/search?query=" + encodeURIComponent(
319 map.getCenter().lat.toFixed(precision) + "," +
320 map.getCenter().lng.toFixed(precision)));