]> git.openstreetmap.org Git - rails.git/commitdiff
Tidy up some recent commits:
authorTom Hughes <tom@compton.nu>
Sat, 27 Jun 2009 17:11:54 +0000 (17:11 +0000)
committerTom Hughes <tom@compton.nu>
Sat, 27 Jun 2009 17:11:54 +0000 (17:11 +0000)
- Simplify shortlinkPrefix by removing redundant code.
- Factor out duplicate code for displaying objects on maps.
- Reindent some stuff to make it more readable.

app/views/browse/_map.html.erb
app/views/site/index.html.erb
public/javascripts/map.js
public/javascripts/site.js

index ac6f32fc6dbea253fc2165995a7365b2fb11708d..f84a2ec9f3d034d3bd7ebc0b1f58ca0d71f89d73 100644 (file)
@@ -8,8 +8,8 @@
     <span id="loading"><%= t 'browse.map.loading' %></span>
     <a id="area_larger_map" href=""></a>
     <% unless map.instance_of? Changeset %>
-    <br />
-    <a id="object_larger_map" href=""></a>
+      <br />
+      <a id="object_larger_map" href=""></a>
     <% end %>
   <% else %>
     <%= t 'browse.map.deleted' %>
           url += "/full";
         }
 
-        var osm_layer = new OpenLayers.Layer.GML("OSM", url, {
-          format: OpenLayers.Format.OSM,
-          projection: new OpenLayers.Projection("EPSG:4326")
-        });
-
-        osm_layer.events.register("loadend", osm_layer, function() {
+        addObjectToMap(url, true, function(extent) {
           $("loading").innerHTML = "";
 
-          if (this.features.length) {
-            var extent =  this.features[0].geometry.getBounds();
-
-            for (var i = 1; i < this.features.length; i++) {
-              extent.extend(this.features[i].geometry.getBounds());
-            }
-
-            if (extent) {
-              this.map.zoomToExtent(extent);
-            } else {
-              this.map.zoomToMaxExtent();
-            }
+          if (extent) {
+            extent.transform(map.getProjectionObject(), map.displayProjection);
 
-            var center = getMapCenter();
-            $("area_larger_map").href = '/?lat='+center.lat+'&lon='+center.lon+'&zoom='+this.map.getZoom();
+            $("area_larger_map").href = '/?minlon='+extent.left+'&minlat='+extent.bottom+'&maxlon='+extent.right+'&maxlat='+extent.top;
             $("area_larger_map").innerHTML = "<%= t 'browse.map.larger.area' %>";
+
             <% unless map.instance_of? Changeset %>
-            $("object_larger_map").href = '/?<%= map.class.to_s.downcase %>=<%= map.id %>';
-            $("object_larger_map").innerHTML = "<%= t('browse.map.larger.' + map.class.to_s.downcase) %>";
+              $("object_larger_map").href = '/?<%= map.class.to_s.downcase %>=<%= map.id %>';
+              $("object_larger_map").innerHTML = "<%= t('browse.map.larger.' + map.class.to_s.downcase) %>";
             <% end %>
           } else {
             $("small_map").style.display = "none";
           }
         });
-
-        map.addLayer(osm_layer);
-
-        osm_layer.loadGML();
-        osm_layer.loaded = true;
       <% end %>
     }
 
index a1ce52267e71e60d278ea83e7932726956c5014d..40a7a3fbca52e085932194a983ba3ee4ef383e03 100644 (file)
 
 <%
 if params['mlon'] and params['mlat'] 
-       marker = true
-       mlon = h(params['mlon'])
-       mlat = h(params['mlat'])
-end
-
-if params['minlon'] and params['minlat'] and params['maxlon'] and params['maxlat']
-       bbox = true
-       minlon = h(params['minlon'])
-       minlat = h(params['minlat'])
-       maxlon = h(params['maxlon'])
-       maxlat = h(params['maxlat'])
-       box = true if params['box']=="yes" 
+    marker = true
+    mlon = h(params['mlon'])
+    mlat = h(params['mlat'])
 end
 
 if params['node'] or params['way'] or params['relation']
     object = true
+    object_zoom = true
+
     if params['node']
         object_type = 'node'
         object_id = h(params['node'])
@@ -69,45 +62,51 @@ if params['node'] or params['way'] or params['relation']
     end
 end
 
+if params['minlon'] and params['minlat'] and params['maxlon'] and params['maxlat']
+    bbox = true
+    minlon = h(params['minlon'])
+    minlat = h(params['minlat'])
+    maxlon = h(params['maxlon'])
+    maxlat = h(params['maxlat'])
+    box = true if params['box']=="yes" 
+    object_zoom = false
+end
+
 # Decide on a lat lon to initialise the map with. Various ways of doing this
 if params['lon'] and params['lat']
-       # We only want to override the default 'object = true' zoom above
-       # if we get actual GET params, as opposed to say a cookie
-       lat_lon_zoom_via_params = true
-
-       lon =  h(params['lon'])
-       lat =  h(params['lat'])
-       zoom =  h(params['zoom'] || '5')
-       layers = h(params['layers'])
-       
+    lon =  h(params['lon'])
+    lat =  h(params['lat'])
+    zoom =  h(params['zoom'] || '5')
+    layers = h(params['layers'])
+    object_zoom = false
 elsif params['mlon'] and params['mlat']
-       lon = h(params['mlon']) 
-       lat = h(params['mlat'])
-       zoom =  h(params['zoom'] || '12')
-       layers = h(params['layers'])
-       
+    lon = h(params['mlon']) 
+    lat = h(params['mlat'])
+    zoom =  h(params['zoom'] || '12')
+    layers = h(params['layers'])
+    object_zoom = false
 elsif cookies.key?("_osm_location")
-       lon,lat,zoom,layers = cookies["_osm_location"].split("|")
-       
+    lon,lat,zoom,layers = cookies["_osm_location"].split("|")
 elsif @user and !@user.home_lon.nil? and !@user.home_lat.nil? 
-       lon =  @user.home_lon
-       lat =  @user.home_lat
-       zoom = '10'
+    lon =  @user.home_lon
+    lat =  @user.home_lat
+    zoom = '10'
 else
-       session[:location] = OSM::IPLocation(request.env['REMOTE_ADDR']) unless session[:location]
-
-       if session[:location]
-               bbox = true
-               minlon = session[:location][:minlon]
-               minlat = session[:location][:minlat]
-               maxlon = session[:location][:maxlon]
-               maxlat = session[:location][:maxlat]
-       else
-               lon =  '-0.1' 
-               lat =  '51.5' 
-               zoom =  h(params['zoom'] || '5') 
-       end
-       layers = h(params['layers']) 
+    session[:location] = OSM::IPLocation(request.env['REMOTE_ADDR']) unless session[:location]
+
+    if session[:location]
+        bbox = true
+        minlon = session[:location][:minlon]
+        minlat = session[:location][:minlat]
+        maxlon = session[:location][:maxlon]
+        maxlat = session[:location][:maxlat]
+    else
+        lon =  '-0.1' 
+        lat =  '51.5' 
+        zoom =  h(params['zoom'] || '5') 
+    end
+
+    layers = h(params['layers']) 
 end
 %>
 
@@ -115,14 +114,11 @@ end
 <%= javascript_include_tag '/openlayers/OpenStreetMap.js' %>
 <%= javascript_include_tag 'map.js' %>
 
-
 <script type="text/javascript" defer="defer">
   <!--
   var brokenContentSize = $("content").offsetWidth == 0;
   var marker;
   var map;
-  var obj_type = false;
-  var obj_id = false;
 
   OpenLayers.Lang.setCode("<%= I18n.locale.to_s %>");
 
@@ -130,82 +126,55 @@ end
     map = createMap("map");
 
     <% unless OSM_STATUS == :api_offline or OSM_STATUS == :database_offline %>
-    map.dataLayer = new OpenLayers.Layer("<%= I18n.t 'browse.start_rjs.data_layer_name' %>", { "visibility": false });
-    map.dataLayer.events.register("visibilitychanged", map.dataLayer, toggleData);
-    map.addLayer(map.dataLayer);
+      map.dataLayer = new OpenLayers.Layer("<%= I18n.t 'browse.start_rjs.data_layer_name' %>", { "visibility": false });
+      map.dataLayer.events.register("visibilitychanged", map.dataLayer, toggleData);
+      map.addLayer(map.dataLayer);
     <% end %>
 
-    <% if bbox %>
-    var bbox = new OpenLayers.Bounds(<%= minlon %>, <%= minlat %>, <%= maxlon %>, <%= maxlat %>);
-
-    setMapExtent(bbox);
-    <%    if box %>
-    // IE requires Vector layers be initialised on page load, and not under deferred script conditions
-    Event.observe(window, 'load', function() {addBoxToMap(bbox)});
-    <%    end %>
-    <% elsif object %>
-    // Display node/way/relation on the map
-    obj_type = "<%= object_type %>";
-    obj_id = <%= object_id %>;
-    var url = "/api/<%= "#{API_VERSION}" %>/<%= object_type %>/<%= object_id %>";
-
-    if (obj_type != "node") {
-        url += "/full";
-    }
+    <% unless object_zoom %>
+      <% if bbox %>
+        var bbox = new OpenLayers.Bounds(<%= minlon %>, <%= minlat %>, <%= maxlon %>, <%= maxlat %>);
 
-    var osm_layer = new OpenLayers.Layer.GML("OSM", url, {
-        format: OpenLayers.Format.OSM,
-        projection: new OpenLayers.Projection("EPSG:4326")
-    });
-
-    osm_layer.events.register("loadend", osm_layer, function() {
-      if (this.features.length) {
-        var extent =  this.features[0].geometry.getBounds();
-          for (var i = 1; i < this.features.length; i++) {
-            extent.extend(this.features[i].geometry.getBounds());
-           }
-
-           <% unless lat_lon_zoom_via_params %>
-           if (extent) {
-             this.map.zoomToExtent(extent);
-           } else {
-             this.map.zoomToMaxExtent();
-           }
-           <% else %>
-           var center = new OpenLayers.LonLat(<%= lon %>, <%= lat %>);
-           var zoom = <%= zoom %>;
-           <% end %>
-  
-           var center = getMapCenter();
-      }
-    });
-   
-    map.addLayer(osm_layer);
-    osm_layer.loadGML();
-    osm_layer.loaded = true;
-
-    <% else %>
-    var centre = new OpenLayers.LonLat(<%= lon %>, <%= lat %>);
-    var zoom = <%= zoom %>;
-
-    <%    if params['scale'] and params['scale'].length > 0 then %>
-    zoom = scaleToZoom(<%= params['scale'].to_f() %>);
-    <%    end %>
+        setMapExtent(bbox);
 
-    setMapCenter(centre, zoom);
+        <% if box %>
+          // IE requires Vector layers be initialised on page load, and not under deferred script conditions
+          Event.observe(window, 'load', function() { addBoxToMap(bbox) });
+        <% end %>
+      <% else %>
+        var centre = new OpenLayers.LonLat(<%= lon %>, <%= lat %>);
+        var zoom = <%= zoom %>;
+
+        <% if params['scale'] and params['scale'].length > 0 then %>
+          zoom = scaleToZoom(<%= params['scale'].to_f() %>);
+        <% end %>
+
+        setMapCenter(centre, zoom);
+      <% end %>
+
+      updateLocation();
     <% end %>
 
     <% if !layers.nil? and !layers.empty? %>
-    setMapLayers("<%= layers %>");
+      setMapLayers("<%= layers %>");
     <% end %>
 
     <% if marker %>
-    marker = addMarkerToMap(new OpenLayers.LonLat(<%= mlon %>, <%= mlat %>));
+      marker = addMarkerToMap(new OpenLayers.LonLat(<%= mlon %>, <%= mlat %>));
+    <% end %>
+
+    <% if object %>
+      var url = "/api/<%= "#{API_VERSION}" %>/<%= object_type %>/<%= object_id %>";
+
+      <% if object_type != "node" %>
+        url += "/full";
+      <% end %>
+
+      addObjectToMap(url, <%= object_zoom %>);
     <% end %>
 
     map.events.register("moveend", map, updateLocation);
     map.events.register("changelayer", map, updateLocation);
-    updateLocation();
 
     handleResize();
   }
@@ -239,8 +208,15 @@ end
     var layers = getMapLayers();
     var extents = getMapExtent();
     var expiry = new Date();
+    var objtype;
+    var objid;
+
+    <% if object %>
+      objtype = "<%= object_type %>";
+      objid = <%= object_id %>;
+    <% end %>
 
-    updatelinks(lonlat.lon, lonlat.lat, zoom, layers, extents.left, extents.bottom, extents.right, extents.top, obj_type, obj_id);
+    updatelinks(lonlat.lon, lonlat.lat, zoom, layers, extents.left, extents.bottom, extents.right, extents.top, objtype, objid);
 
     expiry.setYear(expiry.getFullYear() + 10); 
     document.cookie = "_osm_location=" + lonlat.lon + "|" + lonlat.lat + "|" + zoom + "|" + layers + "; expires=" + expiry.toGMTString();
index 0457713c591102567392226dd536ba8fe7f188dd..c63461fe203be6c6559464f2c06f649e935ada0d 100644 (file)
@@ -84,7 +84,7 @@ function createMap(divName, options) {
       projection: "EPSG:900913"
    });
    map.addLayer(markers);
-   
+
    return map;
 }
 
@@ -108,6 +108,42 @@ function addMarkerToMap(position, icon, description) {
    return marker;
 }
 
+function addObjectToMap(url, zoom, callback) {
+   var layer = new OpenLayers.Layer.GML("Objects", url, {
+      format: OpenLayers.Format.OSM,
+      projection: new OpenLayers.Projection("EPSG:4326"),
+      displayInLayerSwitcher: false
+   });
+
+   layer.events.register("loadend", layer, function() {
+      var extent;
+
+      if (this.features.length) {
+         extent = this.features[0].geometry.getBounds();
+
+         for (var i = 1; i < this.features.length; i++) {
+            extent.extend(this.features[i].geometry.getBounds());
+         }
+
+         if (zoom) {
+            if (extent) {
+               this.map.zoomToExtent(extent);
+            } else {
+               this.map.zoomToMaxExtent();
+            }
+         }
+      }
+
+      if (callback) {
+         callback(extent);
+      }
+   });
+  
+   map.addLayer(layer);
+
+   layer.loadGML();
+}
+
 function addBoxToMap(boxbounds) {
    if(!vectors) {
      // Be aware that IE requires Vector layers be initialised on page load, and not under deferred script conditions
index 66c5d81fc9814b19d8d8e912587d1f580bcc639f..c509fc5eff6947b54be040c63144c621b4046ffb 100644 (file)
@@ -2,7 +2,7 @@
  * Called as the user scrolls/zooms around to aniplate hrefs of the
  * view tab and various other links
  */
-function updatelinks(lon,lat,zoom,layers,minlon,minlat,maxlon,maxlat, obj_type, obj_id) {
+function updatelinks(lon,lat,zoom,layers,minlon,minlat,maxlon,maxlat,objtype,objid) {
   var decimals = Math.pow(10, Math.floor(zoom/3));
   var node;
 
@@ -18,8 +18,8 @@ function updatelinks(lon,lat,zoom,layers,minlon,minlat,maxlon,maxlat, obj_type,
     if (layers) {
       args["layers"] = layers;
     }
-    if (obj_type && obj_id) {
-      args[obj_type] = obj_id;
+    if (objtype && objid) {
+      args[objtype] = objid;
     }
     node.href = setArgs(node.href, args);
   }
@@ -95,8 +95,8 @@ function updatelinks(lon,lat,zoom,layers,minlon,minlat,maxlon,maxlat, obj_type,
     var prefix = shortlinkPrefix();
 
     // Add ?{node,way,relation}=id to the arguments
-    if (obj_type && obj_id) {
-      args[obj_type] = obj_id;
+    if (objtype && objid) {
+      args[objtype] = objid;
     }
 
     // little hack. may the gods of hardcoding please forgive me, or 
@@ -110,23 +110,20 @@ function updatelinks(lon,lat,zoom,layers,minlon,minlat,maxlon,maxlat, obj_type,
   }
 }
 
-
 /*
- * This is a hack to hardcode opestreetmap.org -> osm.org in the
- * shortlink
+ * Get the URL prefix to use for a short link
  */
 function shortlinkPrefix() {
-    var hostname = window.location.hostname;
-    var normalized_hostname = hostname.replace(/^:www\./i,'');
-    var prefix = '';
-
-    if (normalized_hostname.match(/^openstreetmap\.org$/i)) {
-        prefix = "http://osm.org";
-    }
-
-    return prefix;
+  if (window.location.hostname.match(/^www\.openstreetmap\.org/i)) {
+    return "http://osm.org";
+  } else {
+    return "";     
+  }
 }
 
+/*
+ * Called to get the arguments from a URL as a hash.
+ */
 function getArgs(url) {
   var args = new Object();
   var querystart = url.indexOf("?");
@@ -166,7 +163,7 @@ function setArgs(url, args) {
 }
 
 /*
- * Called to get the arguments from a URL as a hash.
+ * Called to get a CSS property for an element.
  */
 function getStyle(el, property) {
   var style;