]> git.openstreetmap.org Git - rails.git/blobdiff - app/assets/javascripts/index/notes.js.erb
Add notes sidebar interaction
[rails.git] / app / assets / javascripts / index / notes.js.erb
index 1646e8b6d2a83680a3719d3e421e5f567d58a9de..75ca5ad50a3382072ea5a05b9b033bee6e5df79b 100644 (file)
@@ -2,8 +2,7 @@
 //= require templates/notes/new
 
 function initializeNotes(map) {
-  var params = OSM.mapParams(),
-      noteLayer = new L.LayerGroup({code: 'N'}),
+  var noteLayer = map.noteLayer,
       notes = {},
       newNote;
 
@@ -25,8 +24,6 @@ function initializeNotes(map) {
     })
   };
 
-  map.noteLayer = noteLayer;
-
   map.on("layeradd", function (e) {
     if (e.layer == noteLayer) {
       loadNotes();
@@ -51,20 +48,17 @@ function initializeNotes(map) {
     }
   });
 
-  if (OSM.STATUS != 'api_offline' && OSM.STATUS != 'database_offline') {
-    if (params.notes || params.layers.indexOf('N') >= 0) map.addLayer(noteLayer);
-    if (params.note) {
-      $.ajax({
-        url: "/api/" + OSM.API_VERSION + "/notes/" + params.note + ".json",
-        success: function (feature) {
-          var marker = updateMarker(notes[feature.properties.id], feature);
-          notes[feature.properties.id] = marker;
-          map.addLayer(noteLayer);
-          marker.openPopup();
-        }
-      });
-    }
-  }
+  noteLayer.showNote = function(id) {
+    $.ajax({
+      url: "/api/" + OSM.API_VERSION + "/notes/" + id + ".json",
+      success: function (feature) {
+        var marker = updateMarker(notes[feature.properties.id], feature);
+        notes[feature.properties.id] = marker;
+        map.addLayer(noteLayer);
+        marker.openPopup();
+      }
+    });
+  };
 
   function updateMarker(marker, feature) {
     if (marker) {
@@ -78,6 +72,7 @@ function initializeNotes(map) {
         icon: noteIcons[feature.properties.status],
         opacity: 0.9
       });
+      marker.id = feature.properties.id;
       marker.addTo(noteLayer).bindPopup(
         createPopupContent(marker, feature.properties),
         popupOptions()
@@ -86,6 +81,10 @@ function initializeNotes(map) {
     return marker;
   }
 
+  noteLayer.getLayerId = function(marker) {
+    return marker.id;
+  };
+
   var noteLoader;
 
   function loadNotes() {
@@ -162,6 +161,8 @@ function initializeNotes(map) {
     return content[0];
   }
 
+  var addNoteButton = $(".control-note .control-button");
+
   function createNote(marker, form, url) {
     var location = marker.getLatLng();
 
@@ -188,7 +189,7 @@ function initializeNotes(map) {
       notes[feature.properties.id] = updateMarker(marker, feature);
       newNote = null;
 
-      $("#createnoteanchor").removeClass("disabled").addClass("geolink");
+      addNoteButton.removeClass("active");
     }
   }
 
@@ -217,12 +218,14 @@ function initializeNotes(map) {
     });
   }
 
-  $(".leaflet-control-attribution").on("click", "#createnoteanchor", function (e) {
+  addNoteButton.on("click", function (e) {
     e.preventDefault();
+    e.stopPropagation();
 
-    if ($(e.target).hasClass("disabled")) return;
+    if (addNoteButton.hasClass("disabled")) return;
+    if (addNoteButton.hasClass("active")) return;
 
-    $(e.target).removeClass("geolink").addClass("disabled");
+    addNoteButton.addClass("active");
 
     map.addLayer(noteLayer);
 
@@ -243,25 +246,23 @@ function initializeNotes(map) {
       draggable: true
     });
 
-    var popupContent = $(JST["templates/notes/new"]({
-        create_url: $(e.target).attr("href")
-    }));
+    var popupContent = $(JST["templates/notes/new"]());
 
     popupContent.find("textarea").on("input", disableWhenBlank);
 
     function disableWhenBlank(e) {
-      $(e.target.form).prop("disabled", $(e.target).val() === "");
+      $(e.target.form.add).prop("disabled", $(e.target).val() === "");
     }
 
     popupContent.find("input[type=submit]").on("click", function (e) {
       e.preventDefault();
-      createNote(newNote, e.target.form, $(e.target).data("url"));
+      createNote(newNote, e.target.form, '/api/0.6/notes.json');
     });
 
     newNote.addTo(noteLayer).bindPopup(popupContent[0], popupOptions()).openPopup();
 
     newNote.on("remove", function (e) {
-      $("#createnoteanchor").removeClass("disabled").addClass("geolink");
+      addNoteButton.removeClass("active");
     }).on("dragstart", function (e) {
       $(newNote).stopTime("removenote");
     }).on("dragend", function (e) {