]> git.openstreetmap.org Git - rails.git/blobdiff - app/views/site/index.html.erb
Rework notes interface using OpenLayers and rails
[rails.git] / app / views / site / index.html.erb
index 84ffcbe59a52bc7885ceab7c0a4add179c80cdd9..f4a2abd09102f6a23a88141eca5a0117d9aa4768 100644 (file)
@@ -18,8 +18,9 @@
 
 <div id="map">
   <div id="permalink">
-    <a href="/" id="permalinkanchor"><%= t 'site.index.permalink' %></a><br/>
+    <a href="/" id="permalinkanchor" class="geolink llz layers object"><%= t 'site.index.permalink' %></a><br/>
     <a href="/" id="shortlinkanchor"><%= t 'site.index.shortlink' %></a>
+    <a href="#" id="createnoteanchor">Report a problem</a>     
   </div>
 </div>
 
@@ -66,7 +67,13 @@ if params['node'] or params['way'] or params['relation']
 end
 
 # Decide on a lat lon to initialise the map with. Various ways of doing this
-if params['minlon'] and params['minlat'] and params['maxlon'] and params['maxlat']
+if params['bbox']
+    bbox = true
+    minlon, minlat, maxlon, maxlat = h(params['bbox']).split(",")
+    layers = h(params['layers'])
+    box = true if params['box']=="yes"
+    object_zoom = false
+elsif params['minlon'] and params['minlat'] and params['maxlon'] and params['maxlat']
     bbox = true
     minlon = h(params['minlon'])
     minlat = h(params['minlat'])
@@ -120,12 +127,16 @@ end
 
 <%= render :partial => 'resize' %>
 
-<script type="text/javascript" defer="defer">
+<script type="text/javascript">
   var marker;
   var map;
 
   OpenLayers.Lang.setCode("<%= I18n.locale.to_s %>");
 
+  <% if @user %>
+    var loginName = "<%= @user.display_name %>"
+  <% end %>
+
   function mapInit(){
     map = createMap("map");
 
@@ -133,6 +144,65 @@ end
       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.noteLayer = new OpenLayers.Layer.Vector("Notes", {
+          visibility: false,
+          projection: new OpenLayers.Projection("EPSG:4326"),
+          styleMap: new OpenLayers.StyleMap(new OpenLayers.Style({
+              graphicWidth: 22,
+              graphicHeight: 22,
+              graphicOpacity: 0.7,
+              graphicXOffset: -11,
+              graphicYOffset: -11
+          }, {
+              rules: [
+                  new OpenLayers.Rule({
+                      filter: new OpenLayers.Filter.Comparison({
+                          type: OpenLayers.Filter.Comparison.EQUAL_TO,
+                          property: "status",
+                          value: "open"
+                      }),
+                      symbolizer: {
+                          externalGraphic: "<%= image_path 'open_note_marker.png' %>"
+                      }
+                  }),
+                  new OpenLayers.Rule({
+                      filter: new OpenLayers.Filter.Comparison({
+                          type: OpenLayers.Filter.Comparison.EQUAL_TO,
+                          property: "status",
+                          value: "closed"
+                      }),
+                      symbolizer: {
+                          externalGraphic: "<%= image_path 'closed_note_marker.png' %>"
+                      }
+                 })
+              ]
+          })),
+          strategies: [
+              new OpenLayers.Strategy.BBOX()
+          ],
+          protocol: new OpenLayers.Protocol.HTTP({
+              url: "<%= url_for :controller => :note, :action => :list, :format => :json %>",
+              format: new OpenLayers.Format.GeoJSON(),
+              callback: noteCallback
+          })
+      });
+      map.noteLayer.events.register("featureselected", map, noteSelected);
+      map.noteLayer.events.register("featureunselected", map, noteUnselected);
+      map.addLayer(map.noteLayer);
+
+      var noteControl = new OpenLayers.Control.SelectFeature(map.noteLayer, {
+          autoActivate: true
+      });
+      map.addControl(noteControl);
+
+      <% if params[:notes] == "yes" -%>
+      map.noteLayer.setVisibility(true);
+      <% end -%>
+
+      $("createnoteanchor").observe("click", addNote);
+
+      map.events.register("zoomend", map, allowNoteReports);
     <% end %>
 
     <% unless object_zoom %>
@@ -142,8 +212,7 @@ end
         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) });
+          Event.observe(window, "load", function() { addBoxToMap(bbox) });
         <% end %>
       <% else %>
         var centre = new OpenLayers.LonLat(<%= lon %>, <%= lat %>);
@@ -174,8 +243,7 @@ end
         url += "/full";
       <% end %>
 
-      // IE requires Vector layers be initialised on page load, and not under deferred script conditions
-      Event.observe(window, 'load', function() { addObjectToMap(url, <%= object_zoom %>) });
+      Event.observe(window, "load", function() { addObjectToMap(url, <%= object_zoom %>) });
     <% end %>
 
     map.events.register("moveend", map, updateLocation);
@@ -263,10 +331,48 @@ end
     <% end %>
   }
 
-  mapInit();
+  function noteCallback(scope, response) {
+    for (var f = 0; f < response.features.length; f++) {
+      var feature = response.features[f];
+    }
+  }
+
+  function noteSelected(o) {
+    var feature = o.feature;
+    var location = feature.geometry.getBounds().getCenterLonLat();
+
+    feature.popup = new OpenLayers.Popup.FramedCloud(feature.attributes.id,
+                                                     location, null,
+                                                     "<p>" + feature.attributes.id + "</p>",
+                                                     null, true);
+
+    map.addPopup(feature.popup);
+//    feature.popup.show();
+  }
+
+  function noteUnselected(o) {
+    var feature = o.feature;
+
+    map.removePopup(feature.popup);
+    delete feature.popup;
+  }
+
+  function addNote() {
+    map.noteLayer.setVisibility(true);
+    map.noteLayer.addNote(map.getCenter());
+  }
+
+  function allowNoteReports() { 
+    if (map.getZoom() > 11) {
+      $("createnoteanchor").style.visibility = "visible";
+    } else {
+      $("createnoteanchor").style.visibility = "hidden";
+    }
+  }
 
-  Event.observe(window, "load", installEditHandler);
-  Event.observe(window, "load", handleResize);
+  document.observe("dom:loaded", mapInit);
+  document.observe("dom:loaded", installEditHandler);
+  document.observe("dom:loaded", handleResize);
 
   Event.observe(window, "resize", function() {
     var centre = map.getCenter();