]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index.js
Port map key to new UI
[rails.git] / app / assets / javascripts / index.js
1 //= require_self
2 //= require leaflet.customzoom
3 //= require leaflet.locate
4 //= require leaflet.layers
5 //= require leaflet.key
6 //= require leaflet.note
7 //= require leaflet.share
8 //= require index/browse
9 //= require index/export
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.noteLayer = new L.LayerGroup({code: 'N'});
52   map.dataLayer = new L.OSM.DataLayer(null);
53
54   $("#map").on("resized", function () {
55     map.invalidateSize();
56   });
57
58   L.control.customZoom({position: 'topright'})
59     .addTo(map);
60
61   L.control.locate({position: 'topright'})
62     .addTo(map);
63
64   var uiPane = $('#map-ui')[0];
65
66   L.OSM.layers({
67     position: 'topright',
68     layers: layers,
69     uiPane: uiPane
70   }).addTo(map);
71
72   L.OSM.key({
73     position: 'topright',
74     uiPane: uiPane
75   }).addTo(map);
76
77   L.OSM.note({
78     position: 'topright',
79     uiPane: uiPane
80   }).addTo(map);
81
82   L.OSM.share({
83     getUrl: getShortUrl,
84     uiPane: uiPane
85   }).addTo(map);
86
87   L.control.scale()
88     .addTo(map);
89
90   map.on('moveend layeradd layerremove', updateLocation);
91
92   map.markerLayer = L.layerGroup().addTo(map);
93
94   if (!params.object_zoom) {
95     if (params.bbox) {
96       var bbox = L.latLngBounds([params.minlat, params.minlon],
97                                 [params.maxlat, params.maxlon]);
98
99       map.fitBounds(bbox);
100
101       if (params.box) {
102         L.rectangle(bbox, {
103           weight: 2,
104           color: '#e90',
105           fillOpacity: 0
106         }).addTo(map);
107       }
108     } else {
109       map.setView([params.lat, params.lon], params.zoom);
110     }
111   }
112
113   if (params.layers) {
114     var foundLayer = false;
115     for (var i = 0; i < layers.length; i++) {
116       if (params.layers.indexOf(layers[i].options.code) >= 0) {
117         map.addLayer(layers[i]);
118         foundLayer = true;
119       } else {
120         map.removeLayer(layers[i]);
121       }
122     }
123     if (!foundLayer) {
124       map.addLayer(layers[0]);
125     }
126   }
127
128   if (params.marker) {
129     L.marker([params.mlat, params.mlon], {icon: getUserIcon()}).addTo(map.markerLayer);
130   }
131
132   if (params.object) {
133     addObjectToMap(params.object, map, { zoom: params.object_zoom });
134   }
135
136   handleResize();
137
138   $("body").on("click", "a.set_position", setPositionLink(map));
139
140   $("a[data-editor=remote]").click(function(e) {
141       remoteEditHandler(map.getBounds());
142       e.preventDefault();
143   });
144
145   if (OSM.preferred_editor == "remote" && $('body').hasClass("site-edit")) {
146     remoteEditHandler(map.getBounds());
147   }
148
149   $(window).resize(handleResize);
150
151   $("#search_form").submit(submitSearch(map));
152
153
154   if ($("#query").val()) {
155     $("#search_form").submit();
156   }
157
158   // Focus the search field for browsers that don't support
159   // the HTML5 'autofocus' attribute
160   if (!("autofocus" in document.createElement("input"))) {
161     $("#query").focus();
162   }
163
164   initializeBrowse(map);
165   initializeNotes(map);
166 });
167
168 function getMapBaseLayerId(map) {
169   for (var i in map._layers) { // TODO: map.eachLayer
170     var layer = map._layers[i];
171     if (layer.options && layer.options.keyid) {
172       return layer.options.keyid;
173     }
174   }
175 }
176
177 function getMapLayers(map) {
178   var layerConfig = '';
179   for (var i in map._layers) { // TODO: map.eachLayer
180     var layer = map._layers[i];
181     if (layer.options && layer.options.code) {
182       layerConfig += layer.options.code;
183     }
184   }
185   return layerConfig;
186 }
187
188 // generate a cookie-safe string of map state
189 function cookieContent(map) {
190   var center = map.getCenter().wrap();
191   return [center.lng, center.lat, map.getZoom(), getMapLayers(map)].join('|');
192 }
193
194 function updateLocation() {
195   updatelinks(this.getCenter().wrap(),
196       this.getZoom(),
197       getMapLayers(this),
198       this.getBounds().wrap(), {});
199
200   var expiry = new Date();
201   expiry.setYear(expiry.getFullYear() + 10);
202   $.cookie("_osm_location", cookieContent(this), { expires: expiry });
203 }
204
205 function setPositionLink(map) {
206   return function(e) {
207       var data = $(this).data(),
208           center = L.latLng(data.lat, data.lon);
209
210       if (data.minLon && data.minLat && data.maxLon && data.maxLat) {
211         map.fitBounds([[data.minLat, data.minLon],
212                        [data.maxLat, data.maxLon]]);
213       } else {
214         map.setView(center, data.zoom);
215       }
216
217       if (data.type && data.id) {
218         addObjectToMap(data, map, { zoom: true, style: { opacity: 0.2, fill: false } });
219       }
220
221       map.markerLayer.clearLayers();
222       L.marker(center, {icon: getUserIcon()}).addTo(map.markerLayer);
223
224       return e.preventDefault();
225   };
226 }
227
228 function submitSearch(map) {
229   return function(e) {
230     var bounds = map.getBounds();
231
232     $("#sidebar_title").html(I18n.t('site.sidebar.search_results'));
233     $("#sidebar_content").load($(this).attr("action"), {
234       query: $("#query").val(),
235       minlon: bounds.getWestLng(),
236       minlat: bounds.getSouthLat(),
237       maxlon: bounds.getEastLng(),
238       maxlat: bounds.getNorthLat()
239     }, openSidebar);
240
241     return e.preventDefault();
242   };
243 }