diff --git a/frontend/app.js b/frontend/app.js
index c7e3181..d6c2536 100644
--- a/frontend/app.js
+++ b/frontend/app.js
@@ -39,6 +39,7 @@ let comparisonChart = null;
let schoolDetailChart = null;
let modalMap = null;
let resultsMapInstance = null;
+let resultsMapMarkers = new Map(); // Store markers by school URN
// Chart colors
const CHART_COLORS = [
@@ -541,15 +542,16 @@ async function loadSchools() {
// Show location info banner if location search is active
updateLocationInfoBanner(data.search_location);
- renderSchools(state.schools);
+ // Render appropriate view based on current state
+ if (state.resultsView === "map" && state.locationSearch.active) {
+ renderCompactSchoolList(state.schools);
+ initializeResultsMap(state.schools);
+ } else {
+ renderSchools(state.schools);
+ }
// Update view toggle visibility
updateViewToggle();
-
- // If map view is active, reinitialize the map with new results
- if (state.resultsView === "map" && state.locationSearch.active) {
- initializeResultsMap(state.schools);
- }
}
async function loadFeaturedSchools() {
@@ -999,6 +1001,9 @@ function initializeResultsMap(schools) {
.addTo(resultsMapInstance)
.bindPopup(`Search location
${state.locationSearch.postcode}`);
+ // Clear existing markers
+ resultsMapMarkers.clear();
+
// Add school markers
const bounds = L.latLngBounds([[lat, lng]]);
schools.forEach((school) => {
@@ -1010,9 +1015,16 @@ function initializeResultsMap(schools) {
${school.distance !== undefined ? school.distance.toFixed(1) + " miles away" : ""}
`);
+ // Store marker reference
+ resultsMapMarkers.set(school.urn, {
+ marker,
+ lat: school.latitude,
+ lng: school.longitude,
+ });
+
// Click handler to highlight card
marker.on("click", () => {
- highlightSchoolCard(school.urn);
+ highlightSchoolCard(school.urn, false); // Don't center map, already at marker
});
bounds.extend([school.latitude, school.longitude]);
@@ -1028,19 +1040,28 @@ function initializeResultsMap(schools) {
/**
* Highlight a school card and scroll it into view
+ * @param {number} urn - School URN
+ * @param {boolean} centerMap - Whether to center the map on the school (default: true)
*/
-function highlightSchoolCard(urn) {
- // Remove highlight from all cards
- document.querySelectorAll(".school-card").forEach((card) => {
+function highlightSchoolCard(urn, centerMap = true) {
+ // Remove highlight from all cards and compact items
+ document.querySelectorAll(".school-card, .school-list-item").forEach((card) => {
card.classList.remove("highlighted");
});
- // Add highlight to selected card
- const card = document.querySelector(`.school-card[data-urn="${urn}"]`);
+ // Add highlight to selected card/item
+ const card = document.querySelector(`.school-card[data-urn="${urn}"], .school-list-item[data-urn="${urn}"]`);
if (card) {
card.classList.add("highlighted");
card.scrollIntoView({ behavior: "smooth", block: "center" });
}
+
+ // Center map on the school and open popup
+ if (centerMap && resultsMapInstance && resultsMapMarkers.has(urn)) {
+ const { marker, lat, lng } = resultsMapMarkers.get(urn);
+ resultsMapInstance.setView([lat, lng], 15, { animate: true });
+ marker.openPopup();
+ }
}
/**
@@ -1055,6 +1076,7 @@ function destroyResultsMap() {
}
resultsMapInstance = null;
}
+ resultsMapMarkers.clear();
}
/**
@@ -1084,9 +1106,13 @@ function setResultsView(view) {
btn.classList.toggle("active", btn.dataset.view === view);
});
- // Update container class
+ // Update container class and render appropriate view
if (view === "map") {
elements.resultsContainer.classList.add("map-view");
+ // Render compact list for map view
+ if (state.schools.length > 0) {
+ renderCompactSchoolList(state.schools);
+ }
// Initialize map if location search is active
if (state.locationSearch.active) {
initializeResultsMap(state.schools);
@@ -1094,9 +1120,72 @@ function setResultsView(view) {
} else {
elements.resultsContainer.classList.remove("map-view");
destroyResultsMap();
+ // Re-render full cards for list view
+ if (state.schools.length > 0 && state.locationSearch.active) {
+ renderSchools(state.schools);
+ }
}
}
+/**
+ * Render compact school list items for map view
+ */
+function renderCompactSchoolList(schools) {
+ const html = schools
+ .map((school) => {
+ const distanceBadge =
+ school.distance !== undefined && school.distance !== null
+ ? `${school.distance.toFixed(1)} mi`
+ : "";
+
+ return `
+