From ff14f5cedaf0eb136d09d43aa414c7de27776e62 Mon Sep 17 00:00:00 2001 From: okburkard1 Date: Mon, 16 Nov 2026 21:55:39 -0500 Subject: [PATCH] Keep search result colors stable across response orders Assign marker colors by source and result position. This lets each source draw independently and preserves the colors of earlier results when loading another page. --- .../javascripts/index_modules/search.js | 20 ++++++++----- test/system/search_test.rb | 29 +++++++++++++++++++ 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/app/assets/javascripts/index_modules/search.js b/app/assets/javascripts/index_modules/search.js index 8492efea5..6e1cbdc54 100644 --- a/app/assets/javascripts/index_modules/search.js +++ b/app/assets/javascripts/index_modules/search.js @@ -4,7 +4,6 @@ export default function (map) { .on("click", ".search_results_entry a.set_position", clickSearchResult); const markers = L.layerGroup().addTo(map); - let processedResults = 0; function clickSearchMore(e) { e.preventDefault(); @@ -25,15 +24,23 @@ export default function (map) { }) .then(response => response.text()) .then(html => { + const sources = $("#sidebar_content .search_results_entry[data-href]"); + const source = $target.closest(".search_results_entry[data-href]"); + const sourceIndex = sources.index(source); + if (sourceIndex === -1) return; + + const offset = source.find(".results-list > li").length; const result = $(html); $target.replaceWith(result); - result.filter("ul").children().each(showSearchResult); + // Interleave the sources so each can draw its markers as soon as it responds. + result.filter(".results-list").children().each((i, item) => { + const rowOffset = (offset + i) * sources.length; + showSearchResult($(item), rowOffset + sourceIndex); + }); }); } - function showSearchResult() { - const index = processedResults++; - const listItem = $(this); + function showSearchResult(listItem, index) { const inverseGoldenAngle = (Math.sqrt(5) - 1) * 180; const color = `hwb(${(index * inverseGoldenAngle) % 360}deg 5% 5%)`; listItem.css("--marker-color", color); @@ -87,7 +94,7 @@ export default function (map) { fetchReplace(this.dataset, entry.children().first()) .then(() => { // go to first result of first geocoder - if (index === 0) { + if (index === 0 && this.isConnected) { const firstResult = entry.find("*[data-lat][data-lon]:first").first(); if (firstResult.length) { panToSearchResult(firstResult.data()); @@ -101,7 +108,6 @@ export default function (map) { unload() { markers.clearLayers(); - processedResults = 0; } }; } diff --git a/test/system/search_test.rb b/test/system/search_test.rb index 086837bea..2e59ac949 100644 --- a/test/system/search_test.rb +++ b/test/system/search_test.rb @@ -56,6 +56,35 @@ class SearchTest < ApplicationSystemTestCase assert_link "Broxbourne, Hertfordshire, East of England, England, United Kingdom" end + test "search result colors stay the same whichever source responds first" do + colors = %w[latlon_query nominatim_reverse_query].map do |delayed_source| + visit "/#map=15/51.76320/-0.00760" + page.execute_script(<<~JS, delayed_source) + const delayedPath = arguments[0]; + const fetch = window.fetch.bind(window); + const gate = new Promise(resolve => { window.releaseSearchResult = resolve; }); + window.fetch = async (...args) => { + const response = await fetch(...args); + if (new URL(response.url).pathname.endsWith("/" + delayedPath)) await gate; + return response; + }; + JS + + click_on "Where is this?" + rows = "#sidebar_content .results-list > li" + assert_selector "#{rows}[style*='--marker-color']", :count => 1 + first_color = find(rows).style("--marker-color").fetch("--marker-color") + assert_selector "#map .activatable use[color='#{first_color.strip}']", :visible => :all + + page.execute_script("window.releaseSearchResult()") + assert_selector "#{rows}[style*='--marker-color']", :count => 2 + all(rows).map { |row| row.style("--marker-color").fetch("--marker-color").strip } + end + + assert_equal colors.first, colors.last + assert_equal 2, colors.first.uniq.size + end + test "query search link sets search input value" do visit search_path(:query => "2.341, 7.896") -- 2.47.3