Hiking trails: the main checkbox loads the countries in view from zoom 10 (and those panned into), not all at once — together ~1 GB of GeoJSON the browser never finished; /api/hiking-trails also sends each country's bbox
This commit is contained in:
1 parent
477f68f5b3
commit
f0ff5f390f
5 files changed
+52
-20
No files matched your search
+38
-15
@@ -170,6 +170,12 @@ export class HikingTrailsControl {
|
||||
this._trailData = new Map(); // code -> fetched GeoJSON, kept across toggles/style swaps
|
||||
this._routeInfo = new Map(); // String(relation id) -> feature properties, for the panel
|
||||
this._lanes = null; // the current LaneLayer, null when no country is shown
|
||||
this._bboxes = {}; // code -> [w, s, e, n]
|
||||
this._inView = false; // main checkbox, offline: the countries in view
|
||||
|
||||
// Each country entered while the main checkbox is on joins in.
|
||||
// ponytail: a country left behind stays loaded until unticked.
|
||||
map.on("moveend", () => this._inView && this._addCountriesInView());
|
||||
|
||||
map.on("click", (event) => this._handleClick(event));
|
||||
|
||||
@@ -206,7 +212,7 @@ export class HikingTrailsControl {
|
||||
this._countryList = li.querySelector(".hiking-trail-countries");
|
||||
|
||||
this._checkbox.addEventListener("change", (event) => {
|
||||
if (this._offlineCountries.length) this._toggleAllCountries(event.target.checked);
|
||||
if (this._offlineCountries.length) this._toggleInView(event.target.checked);
|
||||
else this._toggleOnline(event.target.checked);
|
||||
});
|
||||
this._nameButton.addEventListener("click", () => {
|
||||
@@ -223,15 +229,17 @@ export class HikingTrailsControl {
|
||||
// both offered at once, since once a real local extract exists there's
|
||||
// no reason to still fall back to a third-party tile server for any of
|
||||
// it.
|
||||
setOfflineCountries(countries) {
|
||||
setOfflineCountries(countries, bboxes = {}) {
|
||||
this._offlineCountries = countries;
|
||||
this._bboxes = bboxes;
|
||||
if (countries.length === 0) return;
|
||||
|
||||
if (this._active) this._toggleOnline(false);
|
||||
// The main checkbox stays: offline it switches every country at once
|
||||
// (indeterminate while only some are on); the name opens the list.
|
||||
// The main checkbox stays: offline it shows the countries in view,
|
||||
// never all at once (together they're ~1 GB of GeoJSON, more than a
|
||||
// browser parses); the name opens the list to pick some by hand.
|
||||
this._checkbox.checked = false;
|
||||
this._checkbox.title = "All countries — click the name to pick some";
|
||||
this._checkbox.title = `Countries in view, from zoom ${LANES_MIN_ZOOM} — click the name to pick some`;
|
||||
|
||||
this._countryList.innerHTML = countries
|
||||
.map(
|
||||
@@ -262,20 +270,35 @@ export class HikingTrailsControl {
|
||||
this._syncMainCheckbox();
|
||||
}
|
||||
|
||||
_toggleAllCountries(active) {
|
||||
this._countryList.querySelectorAll(".hiking-trail-countries__toggle").forEach((input) => {
|
||||
if (input.checked === active) return;
|
||||
input.checked = active;
|
||||
this._toggleCountry(input.value, active);
|
||||
});
|
||||
_toggleInView(active) {
|
||||
this._inView = active;
|
||||
if (active) {
|
||||
this._addCountriesInView();
|
||||
} else {
|
||||
this._countryList.querySelectorAll(".hiking-trail-countries__toggle:checked").forEach((input) => {
|
||||
input.checked = false;
|
||||
this._toggleCountry(input.value, false);
|
||||
});
|
||||
}
|
||||
this._syncMainCheckbox();
|
||||
}
|
||||
|
||||
// Trails are drawn from LANES_MIN_ZOOM only, so nothing loads before.
|
||||
_addCountriesInView() {
|
||||
if (this._map.getZoom() < LANES_MIN_ZOOM) return;
|
||||
const view = this._map.getBounds();
|
||||
this._countryList.querySelectorAll(".hiking-trail-countries__toggle:not(:checked)").forEach((input) => {
|
||||
const [w, s, e, n] = this._bboxes[input.value] || [];
|
||||
if (n === undefined || w > view.getEast() || e < view.getWest() || s > view.getNorth() || n < view.getSouth()) return;
|
||||
input.checked = true;
|
||||
this._toggleCountry(input.value, true);
|
||||
});
|
||||
}
|
||||
|
||||
_syncMainCheckbox() {
|
||||
const inputs = [...this._countryList.querySelectorAll(".hiking-trail-countries__toggle")];
|
||||
const on = inputs.filter((input) => input.checked).length;
|
||||
this._checkbox.checked = on > 0 && on === inputs.length;
|
||||
this._checkbox.indeterminate = on > 0 && on < inputs.length;
|
||||
const on = this._countryList.querySelectorAll(".hiking-trail-countries__toggle:checked").length;
|
||||
this._checkbox.checked = this._inView;
|
||||
this._checkbox.indeterminate = !this._inView && on > 0;
|
||||
}
|
||||
|
||||
_toggleOnline(active) {
|
||||
|
||||
Reference in new issue
Block a user