]> git.openstreetmap.org Git - rails.git/commitdiff
Add viewbox param to Nominatim link in search results
authorAnton Khorev <tony29@yandex.ru>
Mon, 31 Mar 2025 17:01:16 +0000 (20:01 +0300)
committerAnton Khorev <tony29@yandex.ru>
Mon, 31 Mar 2025 17:01:16 +0000 (20:01 +0300)
app/assets/javascripts/index/search.js
test/system/search_test.rb

index 4c1213320a70672bbe7f2c72e44ecee242ddf857..05a4c449a26ca0f084b9b196297018c9ddbbc9e4 100644 (file)
@@ -18,9 +18,15 @@ OSM.Search = function (map) {
   $(".search_form").on("submit", function (e) {
     e.preventDefault();
     $("header").addClass("closed");
   $(".search_form").on("submit", function (e) {
     e.preventDefault();
     $("header").addClass("closed");
-    const query = $(this).find("input[name=query]").val();
-    let search = "/";
-    if (query) search = "/search?" + new URLSearchParams({ query });
+    const params = new URLSearchParams({
+      query: this.elements.query.value,
+      zoom: map.getZoom(),
+      minlon: map.getBounds().getWest(),
+      minlat: map.getBounds().getSouth(),
+      maxlon: map.getBounds().getEast(),
+      maxlat: map.getBounds().getNorth()
+    });
+    const search = params.get("query") ? `/search?${params}` : "/";
     OSM.router.route(search + OSM.formatHash(map));
   });
 
     OSM.router.route(search + OSM.formatHash(map));
   });
 
@@ -118,14 +124,7 @@ OSM.Search = function (map) {
       const entry = $(this);
       fetch(entry.data("href"), {
         method: "POST",
       const entry = $(this);
       fetch(entry.data("href"), {
         method: "POST",
-        body: new URLSearchParams({
-          zoom: map.getZoom(),
-          minlon: map.getBounds().getWest(),
-          minlat: map.getBounds().getSouth(),
-          maxlon: map.getBounds().getEast(),
-          maxlat: map.getBounds().getNorth(),
-          ...OSM.csrf
-        })
+        body: new URLSearchParams(OSM.csrf)
       })
         .then(response => response.text())
         .then(function (html) {
       })
         .then(response => response.text())
         .then(function (html) {
index ced1e1beb2b0955cdbb124bdefff0858ad96be14..992d0b8004f13c3b4ba0fc282d60c0b7e9ddece3 100644 (file)
@@ -29,4 +29,28 @@ class SearchTest < ApplicationSystemTestCase
 
     assert_field "Search", :with => "4.321, 9.876"
   end
 
     assert_field "Search", :with => "4.321, 9.876"
   end
+
+  test "search adds viewbox param to Nominatim link" do
+    stub_request(:get, %r{^https://nominatim\.openstreetmap\.org/search\?})
+      .to_return(:status => 404)
+
+    visit "/"
+
+    fill_in "query", :with => "paris"
+    click_on "Go"
+
+    assert_link "OpenStreetMap Nominatim", :href => /&viewbox=/
+  end
+
+  test "search adds zoom param to reverse Nominatim link" do
+    stub_request(:get, %r{^https://nominatim\.openstreetmap\.org/reverse\?})
+      .to_return(:status => 404)
+
+    visit "/#map=7/1.234/6.789"
+
+    fill_in "query", :with => "60 30"
+    click_on "Go"
+
+    assert_link "OpenStreetMap Nominatim", :href => /&zoom=7/
+  end
 end
 end