]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index.js
c9316ebae2df558d6a2d5b30ce23a96e929989cb
[rails.git] / app / assets / javascripts / index.js
1 //= require_self
2 //= require leaflet.layers
3 //= require leaflet.share
4 //= require leaflet.note
5 //= require leaflet.locate
6 //= require leaflet.customzoom
7 //= require index/browse
8 //= require index/export
9 //= require index/key
10 //= require index/notes
11
12 $(document).ready(function () {
13   var params = OSM.mapParams();
14
15   var map = L.map("map", {
16     zoomControl: false,
17     layerControl: false
18   });
19
20   map.attributionControl.setPrefix('');
21
22   var layers = [
23     new L.OSM.Mapnik({
24       attribution: '',
25       code: "M",
26       keyid: "mapnik",
27       name: I18n.t("javascripts.map.base.standard")
28     }),
29     new L.OSM.CycleMap({
30       attribution: "Tiles courtesy of <a href='http://www.opencyclemap.org/' target='_blank'>Andy Allan</a>",
31       code: "C",
32       keyid: "cyclemap",
33       name: I18n.t("javascripts.map.base.cycle_map")
34     }),
35     new L.OSM.TransportMap({
36       attribution: "Tiles courtesy of <a href='http://www.opencyclemap.org/' target='_blank'>Andy Allan</a>",
37       code: "T",
38       keyid: "transportmap",
39       name: I18n.t("javascripts.map.base.transport_map")
40     }),
41     new L.OSM.MapQuestOpen({
42       attribution: "Tiles courtesy of <a href='http://www.mapquest.com/' target='_blank'>MapQuest</a> <img src='http://developer.mapquest.com/content/osm/mq_logo.png'>",
43       code: "Q",
44       keyid: "mapquest",
45       name: I18n.t("javascripts.map.base.mapquest")
46     })
47   ];
48
49   layers[0].addTo(map);
50
51   $("#map").on("resized", function () {
52     map.invalidateSize();
53   });
54
55   L.control.customZoom({position: 'topright'})
56     .addTo(map);
57
58   var uiPane = $('#map-ui')[0];
59
60   L.OSM.layers({
61     position: 'topright',
62     layers: layers,
63     uiPane: uiPane
64   }).addTo(map);
65
66   L.control.share({
67     getUrl: getShortUrl,
68     uiPane: uiPane
69   }).addTo(map);
70
71   L.control.note({ position: 'topright' }).addTo(map);
72   L.control.locate({ position: 'topright' }).addTo(map);
73   L.control.scale().addTo(map);
74
75   map.on('moveend layeradd layerremove', updateLocation);
76
77   map.markerLayer = L.layerGroup().addTo(map);
78
79   if (!params.object_zoom) {
80     if (params.bbox) {
81       var bbox = L.latLngBounds([params.minlat, params.minlon],
82                                 [params.maxlat, params.maxlon]);
83
84       map.fitBounds(bbox);
85
86       if (params.box) {
87         L.rectangle(bbox, {
88           weight: 2,
89           color: '#e90',
90           fillOpacity: 0
91         }).addTo(map);
92       }
93     } else {
94       map.setView([params.lat, params.lon], params.zoom);
95     }
96   }
97
98   if (params.layers) {
99     var foundLayer = false;
100     for (var i = 0; i < layers.length; i++) {
101       if (params.layers.indexOf(layers[i].options.code) >= 0) {
102         map.addLayer(layers[i]);
103         foundLayer = true;
104       } else {
105         map.removeLayer(layers[i]);
106       }
107     }
108     if (!foundLayer) {
109       map.addLayer(layers[0]);
110     }
111   }
112
113   if (params.marker) {
114     L.marker([params.mlat, params.mlon], {icon: getUserIcon()}).addTo(map.markerLayer);
115   }
116
117   if (params.object) {
118     addObjectToMap(params.object, map, { zoom: params.object_zoom });
119   }
120
121   handleResize();
122
123   $("body").on("click", "a.set_position", setPositionLink(map));
124
125   $("a[data-editor=remote]").click(function(e) {
126       remoteEditHandler(map.getBounds());
127       e.preventDefault();
128   });
129
130   if (OSM.preferred_editor == "remote" && $('body').hasClass("site-edit")) {
131     remoteEditHandler(map.getBounds());
132   }
133
134   $(window).resize(handleResize);
135
136   $("#search_form").submit(submitSearch(map));
137
138
139   if ($("#query").val()) {
140     $("#search_form").submit();
141   }
142
143   // Focus the search field for browsers that don't support
144   // the HTML5 'autofocus' attribute
145   if (!("autofocus" in document.createElement("input"))) {
146     $("#query").focus();
147   }
148
149   initializeBrowse(map);
150   initializeNotes(map);
151 });
152
153 function getMapBaseLayerId(map) {
154   for (var i in map._layers) { // TODO: map.eachLayer
155     var layer = map._layers[i];
156     if (layer.options && layer.options.keyid) {
157       return layer.options.keyid;
158     }
159   }
160 }
161
162 function getMapLayers(map) {
163   var layerConfig = '';
164   for (var i in map._layers) { // TODO: map.eachLayer
165     var layer = map._layers[i];
166     if (layer.options && layer.options.code) {
167       layerConfig += layer.options.code;
168     }
169   }
170   return layerConfig;
171 }
172
173 // generate a cookie-safe string of map state
174 function cookieContent(map) {
175   var center = map.getCenter().wrap();
176   return [center.lng, center.lat, map.getZoom(), getMapLayers(map)].join('|');
177 }
178
179 function updateLocation() {
180   updatelinks(this.getCenter().wrap(),
181       this.getZoom(),
182       getMapLayers(this),
183       this.getBounds().wrap(), {});
184
185   var expiry = new Date();
186   expiry.setYear(expiry.getFullYear() + 10);
187   $.cookie("_osm_location", cookieContent(this), { expires: expiry });
188 }
189
190 function setPositionLink(map) {
191   return function(e) {
192       var data = $(this).data(),
193           center = L.latLng(data.lat, data.lon);
194
195       if (data.minLon && data.minLat && data.maxLon && data.maxLat) {
196         map.fitBounds([[data.minLat, data.minLon],
197                        [data.maxLat, data.maxLon]]);
198       } else {
199         map.setView(center, data.zoom);
200       }
201
202       if (data.type && data.id) {
203         addObjectToMap(data, map, { zoom: true, style: { opacity: 0.2, fill: false } });
204       }
205
206       map.markerLayer.clearLayers();
207       L.marker(center, {icon: getUserIcon()}).addTo(map.markerLayer);
208
209       return e.preventDefault();
210   };
211 }
212
213 function submitSearch(map) {
214   return function(e) {
215     var bounds = map.getBounds();
216
217     $("#sidebar_title").html(I18n.t('site.sidebar.search_results'));
218     $("#sidebar_content").load($(this).attr("action"), {
219       query: $("#query").val(),
220       minlon: bounds.getWestLng(),
221       minlat: bounds.getSouthLat(),
222       maxlon: bounds.getEastLng(),
223       maxlat: bounds.getNorthLat()
224     }, openSidebar);
225
226     return e.preventDefault();
227   };
228 }