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() {
31 var copyright = I18n.t('javascripts.map.copyright', {copyright_url: '/copyright'});
35 attribution: copyright,
38 name: I18n.t("javascripts.map.base.standard")
41 attribution: copyright + ". Tiles courtesy of <a href='http://www.opencyclemap.org/' target='_blank'>Andy Allan</a>",
44 name: I18n.t("javascripts.map.base.cycle_map")
46 new L.OSM.TransportMap({
47 attribution: copyright + ". Tiles courtesy of <a href='http://www.opencyclemap.org/' target='_blank'>Andy Allan</a>",
49 keyid: "transportmap",
50 name: I18n.t("javascripts.map.base.transport_map")
52 new L.OSM.MapQuestOpen({
53 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'>",
56 name: I18n.t("javascripts.map.base.mapquest")
59 attribution: copyright + ". Tiles courtesy of <a href='http://hot.openstreetmap.org/' target='_blank'>Humanitarian OpenStreetMap Team</a>",
62 name: I18n.t("javascripts.map.base.hot")
66 function updateLayers(params) {
67 var layerParam = params.layers || "M";
70 for (var i = layers.length - 1; i >= 0; i--) {
71 if (layerParam.indexOf(layers[i].options.code) >= 0) {
72 map.addLayer(layers[i]);
73 layersAdded = layersAdded + layers[i].options.code;
74 } else if (i == 0 && layersAdded == "") {
75 map.addLayer(layers[i]);
77 map.removeLayer(layers[i]);
84 $(window).on("hashchange", function () {
85 updateLayers(OSM.mapParams());
88 map.on("baselayerchange", function (e) {
89 if (map.getZoom() > e.layer.options.maxZoom) {
90 map.setView(map.getCenter(), e.layer.options.maxZoom, { reset: true });
94 map.noteLayer = new L.LayerGroup();
95 map.noteLayer.options = {code: 'N'};
97 map.dataLayer = new L.OSM.DataLayer(null);
98 map.dataLayer.options.code = 'D';
100 if (OSM.STATUS != 'api_offline' && OSM.STATUS != 'database_offline') {
101 if (params.layers.indexOf(map.noteLayer.options.code) >= 0) {
102 map.addLayer(map.noteLayer);
105 if (params.layers.indexOf(map.dataLayer.options.code) >= 0) {
106 map.addLayer(map.dataLayer);
110 var position = $('html').attr('dir') === 'rtl' ? 'topleft' : 'topright';
112 L.OSM.zoom({position: position})
118 title: I18n.t('javascripts.map.locate.title'),
119 popup: I18n.t('javascripts.map.locate.popup')
123 var sidebar = L.OSM.sidebar('#map-ui')
151 $('.leaflet-control .control-button').tooltip({placement: 'left', container: 'body'});
153 map.on('moveend layeradd layerremove', function() {
155 map.getCenter().wrap(),
160 var expiry = new Date();
161 expiry.setYear(expiry.getFullYear() + 10);
162 $.cookie("_osm_location", cookieContent(map), { expires: expiry });
164 // Trigger hash update on layer changes.
165 map.hash.onMapMove();
169 map.on('layeradd', function (e) {
170 if (e.layer.options) {
171 var goal = OSM.PIWIK.goals[e.layer.options.keyid];
174 $('body').trigger('piwikgoal', goal);
181 map.fitBounds(params.bounds);
183 map.setView([params.lat, params.lon], params.zoom);
186 var marker = L.marker([0, 0], {icon: getUserIcon()});
189 marker.setLatLng([params.mlat, params.mlon]).addTo(map);
192 $("#homeanchor").on("click", function(e) {
195 var data = $(this).data(),
196 center = L.latLng(data.lat, data.lon);
198 map.setView(center, data.zoom);
199 marker.setLatLng(center).addTo(map);
202 $("a[data-editor=remote]").click(function(e) {
203 remoteEditHandler(map.getBounds());
207 if (OSM.preferred_editor == "remote" && $('body').hasClass("site-edit")) {
208 remoteEditHandler(map.getBounds());
211 if (OSM.params().edit_help) {
216 title: I18n.t('javascripts.edit_help')
220 $('body').one('click', function() {
221 $('#editanchor').tooltip('hide');
225 initializeBrowse(map);
226 initializeNotes(map);
228 OSM.Index = function(map) {
229 var page = {}, minimized = false;
231 page.pushstate = page.popstate = function(path) {
232 if (minimized) $("#sidebar").addClass("minimized");
233 map.invalidateSize();
234 $("#view_tab").addClass("current");
235 $('#sidebar_content').load(path);
238 page.unload = function() {
239 $("#view_tab").removeClass("current");
242 page.minimizeSidebar = function() {
243 $("#sidebar").addClass("minimized");
244 map.invalidateSize();
248 $(document).on("click", "#sidebar_content .close", page.minimizeSidebar);
253 OSM.Browse = function(map) {
256 page.pushstate = page.popstate = function(path, type, id) {
257 $("#sidebar").removeClass("minimized");
258 map.invalidateSize();
259 $('#sidebar_content').load(path, function() {
260 page.load(path, type, id);
264 page.load = function(path, type, id) {
265 if (OSM.STATUS === 'api_offline' || OSM.STATUS === 'database_offline') return;
267 if (type === 'note') {
268 map.noteLayer.showNote(parseInt(id));
270 map.addObject({type: type, id: parseInt(id)}, {zoom: true});
274 page.unload = function() {
281 var history = OSM.History(map);
283 OSM.route = OSM.Router({
285 "/search": OSM.Search(map),
286 "/export": OSM.Export(map),
288 "/user/:display_name/edits": history,
289 "/browse/friends": history,
290 "/browse/nearby": history,
291 "/browse/:type/:id(/history)": OSM.Browse(map)
294 $(document).on("click", "a", function(e) {
295 if (e.isPropagationStopped()) return;
296 if (OSM.route(this.pathname + this.search + this.hash)) e.preventDefault();
299 $("#search_form").on("submit", function(e) {
301 OSM.route("/search?query=" + encodeURIComponent($("#query").val()) + OSM.formatHash(map));
304 $("#describe_location").on("click", function(e) {
306 OSM.route("/search?query=" + encodeURIComponent(map.getCenter().lat + "," + map.getCenter().lng));