Offline regions for the whole world: the region list comes from Geofabrik's index (bin/geofabrik-regions-sync.mjs → geofabrik_regions.geojson, 193 countries plus subregions like the US states), keeping the European ISO codes; the picker folds it by continent with a search box and subregions under their country, flags for all of them
CI / check (push) Successful in 19s
CI / deploy (push) Successful in 17s

This commit is contained in:
randy committed 2026-11-19 10:03:47 +01:00
1 parent df69886220
commit ff5924faf1
155 files changed
+5465 -113

No files matched your search

+75 -18
View File
@@ -35,8 +35,12 @@ function formatRunning(status) {
// earlier — see icons.js). Framed in a small rounded-square badge in the
// markup below, the same convention icons.js's MAP_MARKER_ICON already
// uses for framing a category glyph, rather than a new visual language.
function flagIconUrl(code) {
return `/vendor/flag-icons/1x1/${code.toLowerCase()}.svg`;
// A country's flag (a subregion shows its country's: "US-CA" → us); none
// for a region without an ISO code (Canary Islands, ...).
function flagImg(code) {
const iso = code.match(/^([A-Z]{2})(-|$)/)?.[1];
if (!iso) return `<span class="offline-data-panel__flag"></span>`;
return `<img class="offline-data-panel__flag" src="/vendor/flag-icons/1x1/${iso.toLowerCase()}.svg" alt="" width="18" height="18" loading="lazy">`;
}
// meta.sources is one entry per country the current build covers (see
@@ -75,10 +79,12 @@ class RegionPicker {
section.className = "offline-data-panel__regions";
section.innerHTML = `
<p class="offline-data-panel__regions-label">Regions</p>
<ul class="offline-data-panel__region-list"><li class="offline-data-panel__region-status">Loading…</li></ul>
<input type="search" class="offline-data-panel__region-filter" placeholder="Find a country or region">
<div class="offline-data-panel__region-list"><p class="offline-data-panel__region-status">Loading…</p></div>
<button type="button" class="offline-data-panel__apply" hidden>Apply changes</button>
`;
this._list = section.querySelector(".offline-data-panel__region-list");
this._filter = section.querySelector(".offline-data-panel__region-filter");
this._applyButton = section.querySelector(".offline-data-panel__apply");
// Viewing/ticking works for anyone; only saving needs a login — same
// finer-grained gating measure.js/gpx.js use for their own
@@ -95,20 +101,23 @@ class RegionPicker {
fetch("/api/offline-tiles/cache").then((r) => r.json()),
]);
} catch {
this._list.innerHTML = `<li class="offline-data-panel__region-status">Could not load the region list.</li>`;
this._list.innerHTML = `<p class="offline-data-panel__region-status">Could not load the region list.</p>`;
return;
}
const selected = new Set(data.selected);
const cached = new Map(cache.extracts.map((e) => [e.code, e]));
this._list.innerHTML = data.available
.map((region) => {
const extract = cached.get(region.code);
// Downloaded-but-not-(yet)-built is a real state (ticked, not
// Applied yet) as is built-but-no-longer-selected (unticked since)
// — this badge reflects the cache on disk, independent of either.
const cacheInfo = extract
? `
const subregions = new Map();
for (const region of data.available) {
if (region.parent) subregions.set(region.parent, [...(subregions.get(region.parent) || []), region]);
}
const row = (region) => {
const extract = cached.get(region.code);
// Downloaded-but-not-(yet)-built is a real state (ticked, not
// Applied yet) as is built-but-no-longer-selected (unticked since)
// — this badge reflects the cache on disk, independent of either.
const cacheInfo = extract
? `
<span class="offline-data-panel__cache-info">
${formatBytes(extract.size)}
${
@@ -117,24 +126,72 @@ class RegionPicker {
: ""
}
</span>`
: "";
return `
<li>
: "";
return `
<div class="offline-data-panel__region" data-search="${escapeHtml(`${region.name} ${region.code}`.toLowerCase())}">
<label>
<input type="checkbox" value="${escapeHtml(region.code)}" ${selected.has(region.code) ? "checked" : ""}>
<img class="offline-data-panel__flag" src="${flagIconUrl(region.code)}" alt="" width="18" height="18">
${escapeHtml(region.name)}
${flagImg(region.code)}
<span class="offline-data-panel__region-name">${escapeHtml(region.name)}</span>
</label>
${cacheInfo}
</li>`;
</div>`;
};
// Continents (collapsible, open when something in them is selected),
// countries, and a country's subregions (US states, ...) folded under it.
this._list.innerHTML = data.continents
.map((continent) => {
const countries = data.available.filter((r) => r.continent === continent.id && !r.parent);
if (!countries.length) return "";
const codes = data.available.filter((r) => r.continent === continent.id).map((r) => r.code);
return `
<details class="offline-data-panel__continent" ${codes.some((c) => selected.has(c)) ? "open" : ""}>
<summary>${escapeHtml(continent.name)} <span class="offline-data-panel__count"></span></summary>
${countries
.map((country) => {
const subs = subregions.get(country.code);
if (!subs) return row(country);
return `${row(country)}
<details class="offline-data-panel__subregions" ${subs.some((r) => selected.has(r.code)) ? "open" : ""}>
<summary>${subs.length} regions of ${escapeHtml(country.name)}</summary>
${subs.map(row).join("")}
</details>`;
})
.join("")}
</details>`;
})
.join("");
this._list.addEventListener("change", () => this._updateCounts());
this._updateCounts();
this._filter.addEventListener("input", () => this._applyFilter());
this._list.querySelectorAll(".offline-data-panel__cache-delete").forEach((button) => {
button.addEventListener("click", () => this._deleteCache(button));
});
}
// "(2 selected)" on each continent, so a folded one still shows it holds a choice.
_updateCounts() {
this._list.querySelectorAll(".offline-data-panel__continent").forEach((details) => {
const n = details.querySelectorAll("input:checked").length;
details.querySelector(".offline-data-panel__count").textContent = n ? `(${n} selected)` : "";
});
}
// Hides non-matching rows and opens whatever holds a match; empty = as before.
_applyFilter() {
const query = this._filter.value.trim().toLowerCase();
this._list.querySelectorAll(".offline-data-panel__region").forEach((el) => {
el.hidden = query !== "" && !el.dataset.search.includes(query);
});
this._list.querySelectorAll(".offline-data-panel__subregions, .offline-data-panel__continent").forEach((details) => {
const visible = [...details.querySelectorAll(".offline-data-panel__region")].some((el) => !el.hidden);
details.hidden = query !== "" && !visible;
if (query !== "") details.open = visible;
});
}
// Removes just this row's cache badge on success, rather than re-fetching
// and re-rendering the whole list — that would also overwrite whatever
// unticked/ticked-but-not-yet-Applied state the user has pending.