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/changeset
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 var copyright = I18n.t('javascripts.map.copyright', {copyright_url: '/copyright'});
31 attribution: copyright,
34 name: I18n.t("javascripts.map.base.standard")
37 attribution: copyright + ". Tiles courtesy of <a href='http://www.opencyclemap.org/' target='_blank'>Andy Allan</a>",
40 name: I18n.t("javascripts.map.base.cycle_map")
42 new L.OSM.TransportMap({
43 attribution: copyright + ". Tiles courtesy of <a href='http://www.opencyclemap.org/' target='_blank'>Andy Allan</a>",
45 keyid: "transportmap",
46 name: I18n.t("javascripts.map.base.transport_map")
48 new L.OSM.MapQuestOpen({
49 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'>",
52 name: I18n.t("javascripts.map.base.mapquest")
55 attribution: copyright + ". Tiles courtesy of <a href='http://hot.openstreetmap.org/' target='_blank'>Humanitarian OpenStreetMap Team</a>",
58 name: I18n.t("javascripts.map.base.hot")
62 function updateLayers(params) {
63 var layerParam = params.layers || "M";
66 for (var i = layers.length - 1; i >= 0; i--) {
67 if (layerParam.indexOf(layers[i].options.code) >= 0) {
68 map.addLayer(layers[i]);
69 layersAdded = layersAdded + layers[i].options.code;
70 } else if (i == 0 && layersAdded == "") {
71 map.addLayer(layers[i]);
73 map.removeLayer(layers[i]);
80 $(window).on("hashchange", function () {
81 updateLayers(OSM.mapParams());
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 map.noteLayer = new L.LayerGroup();
91 map.noteLayer.options = {code: 'N'};
93 map.dataLayer = new L.OSM.DataLayer(null);
94 map.dataLayer.options.code = 'D';
96 if (OSM.STATUS != 'api_offline' && OSM.STATUS != 'database_offline') {
97 if (params.layers.indexOf(map.noteLayer.options.code) >= 0) {
98 map.addLayer(map.noteLayer);
101 if (params.layers.indexOf(map.dataLayer.options.code) >= 0) {
102 map.addLayer(map.dataLayer);
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 $('.leaflet-control .control-button').tooltip({placement: 'left', container: 'body'});
149 map.on('moveend layeradd layerremove', function() {
151 map.getCenter().wrap(),
156 var expiry = new Date();
157 expiry.setYear(expiry.getFullYear() + 10);
158 $.cookie("_osm_location", cookieContent(map), { expires: expiry });
160 // Trigger hash update on layer changes.
161 map.hash.onMapMove();
165 map.on('layeradd', function (e) {
166 if (e.layer.options) {
167 var goal = OSM.PIWIK.goals[e.layer.options.keyid];
170 $('body').trigger('piwikgoal', goal);
177 map.fitBounds(params.bounds);
179 map.setView([params.lat, params.lon], params.zoom);
182 var marker = L.marker([0, 0], {icon: getUserIcon()});
185 marker.setLatLng([params.mlat, params.mlon]).addTo(map);
188 $("#homeanchor").on("click", function(e) {
191 var data = $(this).data(),
192 center = L.latLng(data.lat, data.lon);
194 map.setView(center, data.zoom);
195 marker.setLatLng(center).addTo(map);
198 $("a[data-editor=remote]").click(function(e) {
199 remoteEditHandler(map.getBounds());
203 if (OSM.preferred_editor == "remote" && $('body').hasClass("site-edit")) {
204 remoteEditHandler(map.getBounds());
207 if (OSM.params().edit_help) {
212 title: I18n.t('javascripts.edit_help')
216 $('body').one('click', function() {
217 $('#editanchor').tooltip('hide');
221 initializeBrowse(map);
222 initializeNotes(map);
224 OSM.Index = function(map) {
225 var page = {}, minimized = false;
227 page.pushstate = page.popstate = function(path) {
228 if (minimized) $("#sidebar").addClass("minimized");
229 map.invalidateSize();
230 $("#view_tab").addClass("current");
231 $('#sidebar_content').load(path);
234 page.unload = function() {
235 $("#view_tab").removeClass("current");
238 page.minimizeSidebar = function() {
239 $("#sidebar").addClass("minimized");
240 map.invalidateSize();
244 $(document).on("click", "#sidebar_content .close", page.minimizeSidebar);
249 OSM.Browse = function(map) {
252 page.pushstate = page.popstate = function(path, type, id) {
253 $("#sidebar").removeClass("minimized");
254 map.invalidateSize();
255 $('#sidebar_content').load(path, function() {
256 page.load(path, type, id);
260 page.load = function(path, type, id) {
261 if (OSM.STATUS === 'api_offline' || OSM.STATUS === 'database_offline') return;
263 if (type === 'note') {
264 map.noteLayer.showNote(parseInt(id));
266 map.addObject({type: type, id: parseInt(id)}, {zoom: true});
270 page.unload = function() {
277 var router = OSM.Router({
279 "/search": OSM.Search(map),
280 "/export": OSM.Export(map),
281 "/browse/changesets": OSM.ChangesetList(map),
282 "/browse/:type/:id(/history)": OSM.Browse(map)
285 $(document).on("click", "a", function(e) {
286 if (e.isPropagationStopped()) return;
287 if (router(this.pathname + this.search + this.hash)) e.preventDefault();
290 $("#search_form").on("submit", function(e) {
292 router("/search?query=" + encodeURIComponent($("#query").val()) + OSM.formatHash(map));
295 $("#describe_location").on("click", function(e) {
297 router("/search?query=" + encodeURIComponent(map.getCenter().lat + "," + map.getCenter().lng));