]> git.openstreetmap.org Git - rails.git/blobdiff - app/assets/javascripts/index/new_note.js
Merge remote-tracking branch 'openstreetmap/pull/1449'
[rails.git] / app / assets / javascripts / index / new_note.js
index 4fe3fcaeb93c65aaba07edb9281850fdfe1f66ec..53697e65b584d20686d3ee9bf3d200878fb9fac2 100644 (file)
@@ -77,11 +77,13 @@ OSM.NewNote = function(map) {
   }
 
   page.pushstate = page.popstate = function (path) {
-    OSM.loadSidebarContent(path, page.load);
+    OSM.loadSidebarContent(path, function () {
+      page.load(path);
+    });
   };
 
   function newHalo(loc, a) {
-    if (a == 'dragstart' && map.hasLayer(halo)) {
+    if (a === 'dragstart' && map.hasLayer(halo)) {
       map.removeLayer(halo);
     } else {
       if (map.hasLayer(halo)) map.removeLayer(halo);
@@ -97,7 +99,7 @@ OSM.NewNote = function(map) {
     }
   }
 
-  page.load = function () {
+  page.load = function (path) {
     if (addNoteButton.hasClass("disabled")) return;
     if (addNoteButton.hasClass("active")) return;
 
@@ -105,18 +107,34 @@ OSM.NewNote = function(map) {
 
     map.addLayer(noteLayer);
 
-    var mapSize = map.getSize();
-    var markerPosition;
+    var params = querystring.parse(path.substring(path.indexOf('?') + 1));
+    var markerLatlng;
+
+    if (params.lat && params.lon) {
+      markerLatlng = L.latLng(params.lat, params.lon);
+
+      var markerPosition = map.latLngToContainerPoint(markerLatlng),
+          mapSize = map.getSize(),
+          panBy = L.point(0, 0);
+
+      if (markerPosition.x < 50) {
+        panBy.x = markerPosition.x - 50;
+      } else if (markerPosition.x > mapSize.x - 50) {
+        panBy.x = 50 - mapSize.x + markerPosition.x;
+      }
+
+      if (markerPosition.y < 50) {
+        panBy.y = markerPosition.y - 50;
+      } else if (markerPosition.y > mapSize.y - 50) {
+        panBy.y = 50 - mapSize.y + markerPosition.y;
+      }
 
-    if (mapSize.y > 800) {
-      markerPosition = [mapSize.x / 2, mapSize.y / 2];
-    } else if (mapSize.y > 400) {
-      markerPosition = [mapSize.x / 2, 400];
+      map.panBy(panBy);
     } else {
-      markerPosition = [mapSize.x / 2, mapSize.y];
+      markerLatlng = map.getCenter();
     }
 
-    newNote = L.marker(map.containerPointToLatLng(markerPosition), {
+    newNote = L.marker(markerLatlng, {
       icon: noteIcons["new"],
       opacity: 0.9,
       draggable: true