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