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