]> git.openstreetmap.org Git - rails.git/blob - app/views/site/index.html.erb
Fix resizing of the map area in RTL layouts
[rails.git] / app / views / site / index.html.erb
1 <% content_for :greeting do %>
2 <% if @user and !@user.home_lon.nil? and !@user.home_lat.nil? %>
3   <%= link_to_function t('layouts.home'), "setPosition(#{@user.home_lat}, #{@user.home_lon}, 15)", { :title => t('layouts.home_tooltip') } %> |
4 <% end %>
5 <% end %>
6
7 <%= render :partial => 'sidebar', :locals => { :onopen => "resizeMap();", :onclose => "resizeMap();" } %>
8 <%= render :partial => 'key' %>
9 <%= render :partial => 'search' %>
10
11 <noscript>
12   <div id="noscript">
13     <p><%= t 'site.index.js_1' %></p>
14     <p><%= t 'site.index.js_2' %></p>
15     <p><%= t 'site.index.js_3' %></p>
16   </div>
17 </noscript>
18
19 <div id="map">
20   <div id="permalink">
21     <a href="/" id="permalinkanchor"><%= t 'site.index.permalink' %></a><br/>
22     <a href="/" id="shortlinkanchor"><%= t 'site.index.shortlink' %></a>
23   </div>
24 </div>
25
26 <iframe id="linkloader" style="display: none">
27 </iframe>
28
29 <div id="attribution">
30   <table width="100%">
31     <tr>
32       <td class="attribution_license"><%= t'site.index.license.license_url' %></td>
33       <td class="attribution_project"><%= t'site.index.license.project_url' %></td>
34     </tr>
35     <tr>
36       <td colspan="2" class="attribution_notice"><%=
37         t'site.index.license.notice',
38           :license_name => t('site.index.license.license_name'),
39           :project_name => t('site.index.license.project_name')
40         %></td>
41     </tr>
42   </table>
43 </div>
44
45 <%
46 if params['mlon'] and params['mlat']
47     marker = true
48     mlon = h(params['mlon'])
49     mlat = h(params['mlat'])
50 end
51
52 if params['node'] or params['way'] or params['relation']
53     object = true
54     object_zoom = true
55
56     if params['node']
57         object_type = 'node'
58         object_id = h(params['node'])
59     elsif params['way']
60         object_type = 'way'
61         object_id = h(params['way'])
62     elsif params['relation']
63         object_type = 'relation'
64         object_id = h(params['relation'])
65     end
66 end
67
68 # Decide on a lat lon to initialise the map with. Various ways of doing this
69 if params['minlon'] and params['minlat'] and params['maxlon'] and params['maxlat']
70     bbox = true
71     minlon = h(params['minlon'])
72     minlat = h(params['minlat'])
73     maxlon = h(params['maxlon'])
74     maxlat = h(params['maxlat'])
75     layers = h(params['layers'])
76     box = true if params['box']=="yes"
77     object_zoom = false
78 elsif params['lon'] and params['lat']
79     lon =  h(params['lon'])
80     lat =  h(params['lat'])
81     zoom =  h(params['zoom'] || '5')
82     layers = h(params['layers'])
83     object_zoom = false
84 elsif params['mlon'] and params['mlat']
85     lon = h(params['mlon'])
86     lat = h(params['mlat'])
87     zoom =  h(params['zoom'] || '12')
88     layers = h(params['layers'])
89     object_zoom = false
90 elsif cookies.key?("_osm_location")
91     lon,lat,zoom,layers = cookies["_osm_location"].split("|")
92 elsif @user and !@user.home_lon.nil? and !@user.home_lat.nil?
93     lon =  @user.home_lon
94     lat =  @user.home_lat
95     zoom = '10'
96 else
97     unless STATUS == :database_readonly or STATUS == :database_offline
98       session[:location] = OSM::IPLocation(request.env['REMOTE_ADDR']) unless session[:location]
99     end
100
101     if session[:location]
102         bbox = true
103         minlon = session[:location][:minlon]
104         minlat = session[:location][:minlat]
105         maxlon = session[:location][:maxlon]
106         maxlat = session[:location][:maxlat]
107     else
108         lon =  '-0.1'
109         lat =  '51.5'
110         zoom =  h(params['zoom'] || '5')
111     end
112
113     layers = h(params['layers'])
114 end
115 %>
116
117 <%= javascript_include_tag '/openlayers/OpenLayers.js' %>
118 <%= javascript_include_tag '/openlayers/OpenStreetMap.js' %>
119 <%= javascript_include_tag 'map.js' %>
120
121 <%= render :partial => 'resize' %>
122
123 <script type="text/javascript" defer="defer">
124   var marker;
125   var map;
126
127   OpenLayers.Lang.setCode("<%= I18n.locale.to_s %>");
128
129   function mapInit(){
130     map = createMap("map");
131
132     <% unless STATUS == :api_offline or STATUS == :database_offline %>
133       map.dataLayer = new OpenLayers.Layer("<%= I18n.t 'browse.start_rjs.data_layer_name' %>", { "visibility": false });
134       map.dataLayer.events.register("visibilitychanged", map.dataLayer, toggleData);
135       map.addLayer(map.dataLayer);
136     <% end %>
137
138     <% unless object_zoom %>
139       <% if bbox %>
140         var bbox = new OpenLayers.Bounds(<%= minlon %>, <%= minlat %>, <%= maxlon %>, <%= maxlat %>);
141
142         setMapExtent(bbox);
143
144         <% if box %>
145           // IE requires Vector layers be initialised on page load, and not under deferred script conditions
146           Event.observe(window, 'load', function() { addBoxToMap(bbox) });
147         <% end %>
148       <% else %>
149         var centre = new OpenLayers.LonLat(<%= lon %>, <%= lat %>);
150         var zoom = <%= zoom %>;
151
152         <% if params['scale'] and params['scale'].length > 0 then %>
153           zoom = scaleToZoom(<%= params['scale'].to_f() %>);
154         <% end %>
155
156         setMapCenter(centre, zoom);
157       <% end %>
158
159       updateLocation();
160     <% end %>
161
162     <% if !layers.nil? and !layers.empty? %>
163       setMapLayers("<%= layers %>");
164     <% end %>
165
166     <% if marker %>
167       marker = addMarkerToMap(new OpenLayers.LonLat(<%= mlon %>, <%= mlat %>));
168     <% end %>
169
170     <% if object %>
171       var url = "/api/<%= "#{API_VERSION}" %>/<%= object_type %>/<%= object_id %>";
172
173       <% if object_type != "node" %>
174         url += "/full";
175       <% end %>
176
177       // IE requires Vector layers be initialised on page load, and not under deferred script conditions
178       Event.observe(window, 'load', function() { addObjectToMap(url, <%= object_zoom %>) });
179     <% end %>
180
181     map.events.register("moveend", map, updateLocation);
182     map.events.register("changelayer", map, updateLocation);
183
184     handleResize();
185   }
186
187   function toggleData() {
188     if (map.dataLayer.visibility) {
189       <%= remote_function :url => { :controller => 'browse', :action => 'start' } %>
190     } else if (map.dataLayer.active) {
191       closeSidebar();
192     }
193   }
194
195   function getPosition() {
196     return getMapCenter();
197   }
198
199   function getZoom() {
200     return getMapZoom();
201   }
202
203   function setPosition(lat, lon, zoom, min_lon, min_lat, max_lon, max_lat) {
204     var centre = new OpenLayers.LonLat(lon, lat);
205
206     if (min_lon && min_lat && max_lon && max_lat) {
207       var bbox = new OpenLayers.Bounds(min_lon, min_lat, max_lon, max_lat);
208
209       setMapExtent(bbox);
210     } else {
211       setMapCenter(centre, zoom);
212     }
213
214     if (marker)
215       removeMarkerFromMap(marker);
216
217     marker = addMarkerToMap(centre, getArrowIcon());
218   }
219
220   function updateLocation() {
221     var lonlat = getMapCenter();
222     var zoom = map.getZoom();
223     var layers = getMapLayers();
224     var extents = getMapExtent();
225     var expiry = new Date();
226     var objtype;
227     var objid;
228
229     <% if object %>
230       objtype = "<%= object_type %>";
231       objid = <%= object_id %>;
232     <% end %>
233
234     updatelinks(lonlat.lon, lonlat.lat, zoom, layers, extents.left, extents.bottom, extents.right, extents.top, objtype, objid);
235
236     expiry.setYear(expiry.getFullYear() + 10);
237     document.cookie = "_osm_location=" + lonlat.lon + "|" + lonlat.lat + "|" + zoom + "|" + layers + "; expires=" + expiry.toGMTString();
238   }
239
240   function remoteEditHandler(event) {
241     var extent = getMapExtent();
242     var loaded = false;
243
244     $("linkloader").observe("load", function () { loaded = true; });
245     $("linkloader").src = "http://127.0.0.1:8111/load_and_zoom?left=" + extent.left + "&top=" + extent.top + "&right=" + extent.right + "&bottom=" + extent.bottom;
246
247     setTimeout(function () {
248       if (!loaded) alert("<%= escape_javascript(t('site.index.remote_failed')) %>");
249     }, 1000);
250
251     event.stop();
252   }
253
254   function installEditHandler() {
255     $("remoteanchor").observe("click", remoteEditHandler);
256
257     <% if preferred_editor == "remote" %>
258       $("editanchor").observe("click", remoteEditHandler);
259
260       <% if params[:action] == "edit" %>
261         remoteEditHandler();
262       <% end %>
263     <% end %>
264   }
265
266   mapInit();
267
268   Event.observe(window, "load", installEditHandler);
269   Event.observe(window, "load", handleResize);
270
271   Event.observe(window, "resize", function() {
272     var centre = map.getCenter();
273     var zoom = map.getZoom();
274
275     handleResize();
276
277     map.setCenter(centre, zoom);
278   });
279
280   <% if params['action'] == 'export' %>
281   <%= remote_function :url => { :controller => 'export', :action => 'start' } %>
282   <% end %>
283 </script>