2152 lines
97 KiB
JavaScript
2152 lines
97 KiB
JavaScript
/* Site script, served at /assets/app.js (deferred). Vanilla JS, no build step.
|
||
|
||
1. Preferences: theme, length order and base map (saved in localStorage; applied
|
||
early by prefs.js), and the theme menu in the header
|
||
1b. Saved trails: the Save buttons, the count in the header (lh-saved in localStorage)
|
||
1c. The Saved page (/saved/): the visitor's saved trails, or a list from a link
|
||
2. Maps: .mapfig[data-map] figures. A frame is never empty: the page carries a static
|
||
drawing of its tracks (olt/sketch.py). A moment after the page has loaded (idle, or
|
||
after 500 ms at most), and never on a scroll, a touch, a key or focus, every map starts
|
||
by itself: the script loads MapLibre (and the contour add-on) from the addresses on its
|
||
own <script> tag, and fetches the figure's data
|
||
from data-src (same origin). The live map opens on the same ground as the drawing
|
||
(data-frame), and the drawing gives way to it once it has drawn the tracks. Where
|
||
the base map cannot load, the drawing stays. A trail page's map (data-outdoor) is an
|
||
outdoor map, with relief and contours, and a switch to the plain one.
|
||
3. The filter bar (home, lists, places, Routes): search, filters, counts, sort, URL state, highlight
|
||
4. Copy-credit buttons
|
||
|
||
Every page works without this script: the list holds every trail and each map
|
||
frame keeps its size, border and caption. */
|
||
(function () {
|
||
'use strict';
|
||
|
||
var root = document.documentElement;
|
||
var SCRIPT = document.currentScript; /* this tag: carries the map libraries' addresses (data-maplibre-js ...) */
|
||
var darkQuery = window.matchMedia('(prefers-color-scheme: dark)');
|
||
var KM_PER_MI = 1.609344;
|
||
var fmt = new Intl.NumberFormat('en-US');
|
||
|
||
/* 1. Preferences ----------------------------------------------------------- */
|
||
|
||
/* False once storage has refused a read or a write: the choices then last
|
||
for this page only, and are not read back from storage. */
|
||
var prefsKept = true;
|
||
function readPref(key) {
|
||
try { return localStorage.getItem(key); } catch (e) { prefsKept = false; return null; }
|
||
}
|
||
function writePref(key, value) {
|
||
try {
|
||
if (value) localStorage.setItem(key, value);
|
||
else localStorage.removeItem(key);
|
||
} catch (e) { prefsKept = false; /* storage blocked: the choice lasts for this page only */ }
|
||
}
|
||
|
||
function isDark() {
|
||
var t = root.dataset.theme;
|
||
return t ? t === 'dark' : darkQuery.matches;
|
||
}
|
||
function milesFirst() { return root.dataset.units !== 'km'; }
|
||
|
||
function setTheme(value) {
|
||
if (value === 'light' || value === 'dark') root.dataset.theme = value;
|
||
else delete root.dataset.theme;
|
||
writePref('lh-theme', value === 'light' || value === 'dark' ? value : null);
|
||
document.dispatchEvent(new Event('olt:theme'));
|
||
}
|
||
|
||
function setUnits(value) {
|
||
if (value === 'km') root.dataset.units = 'km';
|
||
else delete root.dataset.units;
|
||
writePref('lh-units', value === 'km' ? 'km' : null);
|
||
syncRadios('input[name="units"]', value === 'km' ? 'km' : 'mi');
|
||
document.dispatchEvent(new Event('olt:units'));
|
||
}
|
||
|
||
/* The base map of the trail maps: 'outdoor' (the default) or 'plain'. */
|
||
function basemapChoice() { return root.dataset.basemap === 'plain' ? 'plain' : 'outdoor'; }
|
||
|
||
function setBasemap(value) {
|
||
if (value === 'plain') root.dataset.basemap = 'plain';
|
||
else delete root.dataset.basemap;
|
||
writePref('lh-basemap', value === 'plain' ? 'plain' : null);
|
||
document.dispatchEvent(new Event('olt:basemap'));
|
||
}
|
||
|
||
function syncRadios(selector, value) {
|
||
document.querySelectorAll(selector).forEach(function (input) { input.checked = input.value === value; });
|
||
}
|
||
|
||
function syncPrefControls() {
|
||
syncRadios('input[name="theme"]', root.dataset.theme || 'auto');
|
||
syncRadios('input[name="units"]', milesFirst() ? 'mi' : 'km');
|
||
}
|
||
|
||
/* Bring the page back in step with the stored choices, and the switches in
|
||
step with the page. A page shown again by Back may be an old copy from the
|
||
browser's cache, made before a choice changed on another page; and a browser
|
||
may put a form's old values back after this script has run, without a change
|
||
event. Another tab changing a choice comes here too (the storage event). */
|
||
function resyncPrefs() {
|
||
var theme = readPref('lh-theme');
|
||
var units = readPref('lh-units') === 'km' ? 'km' : '';
|
||
var basemap = readPref('lh-basemap') === 'plain' ? 'plain' : '';
|
||
if (theme !== 'light' && theme !== 'dark') theme = '';
|
||
if (prefsKept) {
|
||
[['theme', theme], ['units', units], ['basemap', basemap]].forEach(function (pair) {
|
||
if ((root.dataset[pair[0]] || '') === pair[1]) return;
|
||
if (pair[1]) root.dataset[pair[0]] = pair[1];
|
||
else delete root.dataset[pair[0]];
|
||
document.dispatchEvent(new Event('olt:' + pair[0]));
|
||
});
|
||
}
|
||
syncPrefControls();
|
||
}
|
||
|
||
syncPrefControls();
|
||
window.addEventListener('pageshow', resyncPrefs);
|
||
window.addEventListener('storage', function (e) {
|
||
if (e.key === null || e.key === 'lh-theme' || e.key === 'lh-units' || e.key === 'lh-basemap') resyncPrefs();
|
||
});
|
||
document.querySelectorAll('input[name="theme"]').forEach(function (input) {
|
||
input.addEventListener('change', function () { if (input.checked) setTheme(input.value); });
|
||
});
|
||
document.querySelectorAll('input[name="units"]').forEach(function (input) {
|
||
input.addEventListener('change', function () { if (input.checked) setUnits(input.value); });
|
||
});
|
||
darkQuery.addEventListener('change', function () {
|
||
if (!root.dataset.theme) document.dispatchEvent(new Event('olt:theme'));
|
||
});
|
||
|
||
/* The theme menu in the header: a <details> whose button shows the theme in
|
||
use (by CSS, from html[data-theme]) and whose menu holds the three themes
|
||
as radios. A choice made with the pointer, or Enter, closes it; the arrow
|
||
keys move through the choices and leave it open. Escape, a press outside
|
||
it, or the focus leaving it closes it too. */
|
||
document.querySelectorAll('[data-theme-menu]').forEach(function (menu) {
|
||
var summary = menu.querySelector('summary');
|
||
var byPointer = false;
|
||
function close(refocus) {
|
||
if (!menu.open) return;
|
||
menu.open = false;
|
||
if (refocus) summary.focus();
|
||
}
|
||
menu.addEventListener('keydown', function (e) {
|
||
byPointer = false;
|
||
if (e.key === 'Escape' && menu.open) { e.preventDefault(); close(true); }
|
||
else if (e.key === 'Enter' && e.target.name === 'theme') { e.preventDefault(); close(true); }
|
||
});
|
||
menu.querySelector('fieldset').addEventListener('pointerdown', function () { byPointer = true; });
|
||
menu.addEventListener('change', function () {
|
||
if (byPointer) close(true);
|
||
byPointer = false;
|
||
});
|
||
menu.addEventListener('focusout', function (e) {
|
||
if (e.relatedTarget && !menu.contains(e.relatedTarget)) close(false);
|
||
});
|
||
document.addEventListener('pointerdown', function (e) {
|
||
if (menu.open && !menu.contains(e.target)) close(false);
|
||
});
|
||
});
|
||
|
||
/* A length in both units, in the reader's order: "800 mi (1,287 km)". */
|
||
function lengthText(km) {
|
||
var k = fmt.format(km) + ' km';
|
||
var m = fmt.format(Math.round(km / KM_PER_MI)) + ' mi';
|
||
return milesFirst() ? m + ' (' + k + ')' : k + ' (' + m + ')';
|
||
}
|
||
|
||
/* Resolve a colour token (light-dark() included) to an rgb() string. */
|
||
var probe = null;
|
||
function token(name) {
|
||
if (!probe) {
|
||
probe = document.createElement('span');
|
||
probe.hidden = true;
|
||
document.body.appendChild(probe);
|
||
}
|
||
probe.style.color = 'var(' + name + ')';
|
||
return getComputedStyle(probe).color;
|
||
}
|
||
|
||
/* 1b. Saved trails -----------------------------------------------------------
|
||
The visitor's saved trails: their ids in the order saved, a JSON array in
|
||
localStorage under lh-saved. Nothing leaves the browser. Every read and
|
||
write is guarded: when storage is missing, blocked or full, the list is
|
||
kept for this page only, and a note says so the first time a save cannot
|
||
be kept. Ids that match no trail are ignored; a page that lists every
|
||
trail (ol[data-every-trail]: home, /saved/) also drops them from storage.
|
||
Each [data-save] button saves or unsaves its trail (aria-pressed); each
|
||
[data-saved-count] shows how many are saved. A change made in another tab
|
||
arrives by the storage event. Every change fires olt:saved. */
|
||
|
||
var SAVED_KEY = 'lh-saved';
|
||
var ID_FORM = /^[a-z0-9][a-z0-9-]{0,99}$/;
|
||
var savedHere = null; /* the list, while storage will not keep it */
|
||
var everyTrail = null; /* id -> true, on a page that lists every trail */
|
||
|
||
(function () {
|
||
var all = document.querySelector('ol.entries[data-every-trail]');
|
||
if (!all) return;
|
||
everyTrail = {};
|
||
all.querySelectorAll('.entry[data-id]').forEach(function (li) { everyTrail[li.dataset.id] = true; });
|
||
})();
|
||
|
||
/* Well-formed ids, each once, and where the page knows every trail, only
|
||
trails that exist. */
|
||
function cleanIds(ids) {
|
||
var seen = {};
|
||
return ids.filter(function (id) {
|
||
if (typeof id !== 'string' || !ID_FORM.test(id) || seen[id]) return false;
|
||
seen[id] = true;
|
||
return !everyTrail || everyTrail[id] === true;
|
||
});
|
||
}
|
||
|
||
function storedSaved() {
|
||
var raw = null;
|
||
try { raw = localStorage.getItem(SAVED_KEY); } catch (e) { return []; }
|
||
try {
|
||
var list = JSON.parse(raw || '[]');
|
||
return Array.isArray(list) ? list : [];
|
||
} catch (e) { return []; }
|
||
}
|
||
|
||
function readSaved() {
|
||
return savedHere ? savedHere.slice() : cleanIds(storedSaved());
|
||
}
|
||
|
||
/* Store the list; false when storage will not keep it. */
|
||
function storeSaved(list) {
|
||
try {
|
||
if (list.length) localStorage.setItem(SAVED_KEY, JSON.stringify(list));
|
||
else localStorage.removeItem(SAVED_KEY);
|
||
return true;
|
||
} catch (e) { return false; }
|
||
}
|
||
|
||
/* A note at the foot of the window, once a page, when a save cannot be kept.
|
||
It is a live region from the start, so it is announced when filled. */
|
||
var savedNote = document.createElement('p');
|
||
savedNote.className = 'save-note';
|
||
savedNote.setAttribute('role', 'status');
|
||
document.body.appendChild(savedNote);
|
||
var savedNoteShown = false;
|
||
function sayNotKept() {
|
||
if (savedNoteShown) return;
|
||
savedNoteShown = true;
|
||
savedNote.textContent = 'This browser is not keeping saved trails, so they last only while this page is open.';
|
||
var clear = function () { savedNote.textContent = ''; };
|
||
savedNote.addEventListener('click', clear);
|
||
setTimeout(clear, 15000);
|
||
}
|
||
|
||
function syncSaved() {
|
||
var list = readSaved();
|
||
var on = {};
|
||
list.forEach(function (id) { on[id] = true; });
|
||
document.querySelectorAll('[data-save]').forEach(function (button) {
|
||
var saved = on[button.dataset.save] === true;
|
||
button.setAttribute('aria-pressed', saved ? 'true' : 'false');
|
||
if (button.hasAttribute('title')) button.title = saved ? 'Saved' : 'Save';
|
||
var word = button.querySelector('.save__word');
|
||
if (word) word.textContent = saved ? 'Saved' : 'Save';
|
||
});
|
||
document.querySelectorAll('[data-saved-count]').forEach(function (count) {
|
||
count.textContent = list.length ? String(list.length) : '';
|
||
count.hidden = !list.length;
|
||
});
|
||
document.dispatchEvent(new CustomEvent('olt:saved', { detail: { ids: list } }));
|
||
}
|
||
|
||
function writeSaved(list) {
|
||
list = cleanIds(list);
|
||
if (storeSaved(list)) savedHere = null;
|
||
else { savedHere = list.slice(); sayNotKept(); }
|
||
syncSaved();
|
||
return list;
|
||
}
|
||
|
||
function toggleSaved(id) {
|
||
var list = readSaved();
|
||
var i = list.indexOf(id);
|
||
if (i === -1) list.push(id); else list.splice(i, 1);
|
||
writeSaved(list);
|
||
}
|
||
|
||
/* The buttons are in the page from the start; one listener serves them all. */
|
||
document.addEventListener('click', function (e) {
|
||
var button = e.target.closest ? e.target.closest('[data-save]') : null;
|
||
if (button) toggleSaved(button.dataset.save);
|
||
});
|
||
window.addEventListener('storage', function (e) {
|
||
if (e.key !== SAVED_KEY && e.key !== null) return;
|
||
savedHere = null;
|
||
syncSaved();
|
||
});
|
||
/* A page shown again by Back from the browser's cache is an old copy: a trail
|
||
may have been saved on another page since. */
|
||
window.addEventListener('pageshow', function (e) { if (e.persisted) syncSaved(); });
|
||
/* Here every trail is known: forget saved ids that match none (a trail
|
||
taken off the site). Only those; the rest of the list is untouched. */
|
||
if (everyTrail && !savedHere) {
|
||
var stored = storedSaved();
|
||
var kept = cleanIds(stored);
|
||
if (kept.length !== stored.length) storeSaved(kept);
|
||
}
|
||
syncSaved();
|
||
|
||
/* 1c. The Saved page ---------------------------------------------------------
|
||
/saved/ holds every trail's row, hidden (olt/saved.py). With no t= in the
|
||
address it shows the visitor's saved trails; with t=<id>,<id>,... it shows
|
||
that list, marked as from a link, and changes nothing until "Save these".
|
||
A link that holds the visitor's own list is shown as theirs. On their own
|
||
list a trail they unsave stays in place, dimmed, so it can be saved again;
|
||
trails saved meanwhile (in another tab) are added at the end. The map
|
||
shows the trails listed; the link to the list is written out in full, to
|
||
copy by hand or with the Copy link button (section 5). Runs before the
|
||
maps start, so the map opens on the right trails. */
|
||
|
||
function initSavedPage(page) {
|
||
var title = page.querySelector('[data-saved-title]');
|
||
var lede = page.querySelector('[data-saved-lede]');
|
||
var from = page.querySelector('[data-saved-from]');
|
||
var dropNote = page.querySelector('[data-saved-dropped]');
|
||
var merged = page.querySelector('[data-saved-merged]');
|
||
var share = page.querySelector('[data-saved-share]');
|
||
var url = page.querySelector('[data-saved-url]');
|
||
var empty = document.querySelector('[data-saved-empty]');
|
||
var none = document.querySelector('[data-saved-none]');
|
||
var listBox = document.querySelector('[data-saved-list]');
|
||
var results = listBox.querySelector('.results');
|
||
var total = results.querySelector('[data-saved-total]');
|
||
var fig = listBox.querySelector('.mapfig[data-map]');
|
||
var drawable = {};
|
||
((fig && fig.dataset.drawable) || '').split(' ').forEach(function (id) { if (id) drawable[id] = true; });
|
||
var rows = {};
|
||
results.querySelectorAll('.entry[data-id]').forEach(function (li) { rows[li.dataset.id] = li; });
|
||
|
||
var params = new URLSearchParams(window.location.search);
|
||
var fromLink = params.has('t');
|
||
var linkIds = [];
|
||
var dropped = 0;
|
||
if (fromLink) {
|
||
var seen = {};
|
||
(params.get('t') || '').split(',').forEach(function (id) {
|
||
id = id.trim().toLowerCase();
|
||
if (!id || seen[id]) return;
|
||
seen[id] = true;
|
||
if (rows.hasOwnProperty(id)) linkIds.push(id); else dropped += 1;
|
||
});
|
||
var own = readSaved();
|
||
if (linkIds.length && own.length === linkIds.length &&
|
||
linkIds.every(function (id) { return own.indexOf(id) !== -1; })) {
|
||
/* The visitor's own list: the address stops naming it, so that after a
|
||
trail is unsaved a reload does not show the old list as someone's link. */
|
||
fromLink = false;
|
||
params.delete('t');
|
||
var rest = params.toString();
|
||
window.history.replaceState(null, '', window.location.pathname + (rest ? '?' + rest : '') + window.location.hash);
|
||
}
|
||
}
|
||
var shown = fromLink ? linkIds.slice() : readSaved();
|
||
var mapOn = false;
|
||
|
||
function plural(n, one, many) { return n + ' ' + (n === 1 ? one : many); }
|
||
|
||
function render() {
|
||
var own = readSaved();
|
||
var ownSet = {};
|
||
own.forEach(function (id) { ownSet[id] = true; });
|
||
var view = fromLink ? (linkIds.length ? 'link' : 'none') : (shown.length ? 'own' : 'empty');
|
||
var heading = fromLink ? 'A list from a link' : 'Saved trails';
|
||
if (title.textContent !== heading) {
|
||
var cut = document.title.indexOf(' · ');
|
||
title.textContent = heading;
|
||
document.title = heading + (cut === -1 ? '' : document.title.slice(cut));
|
||
}
|
||
lede.hidden = fromLink;
|
||
from.hidden = view !== 'link';
|
||
dropNote.hidden = !dropped;
|
||
dropNote.textContent = dropped ? plural(dropped, 'trail', 'trails') + ' in the link ' +
|
||
(dropped === 1 ? 'is not on this site, so it is' : 'are not on this site, so they are') + ' left out.' : '';
|
||
empty.hidden = view !== 'empty';
|
||
none.hidden = view !== 'none';
|
||
listBox.hidden = view === 'empty' || view === 'none';
|
||
|
||
var order = {};
|
||
shown.forEach(function (id, i) { order[id] = i; });
|
||
Object.keys(rows).forEach(function (id) {
|
||
var li = rows[id];
|
||
var listed = order.hasOwnProperty(id);
|
||
li.hidden = !listed;
|
||
if (listed) li.dataset.order = String(order[id]); else delete li.dataset.order;
|
||
li.classList.toggle('is-unsaved', view === 'own' && listed && !ownSet[id]);
|
||
});
|
||
var n = fromLink ? linkIds.length : own.length;
|
||
total.innerHTML = '';
|
||
var strong = document.createElement('strong');
|
||
strong.textContent = String(n);
|
||
total.appendChild(strong);
|
||
total.appendChild(document.createTextNode(n === 1 ? ' trail' : ' trails'));
|
||
|
||
var ids = fromLink ? linkIds : own;
|
||
share.hidden = view === 'none' || !ids.length;
|
||
url.textContent = window.location.origin + '/saved/?t=' + ids.join(',');
|
||
|
||
var onMap = shown.filter(function (id) { return drawable[id]; });
|
||
if (fig) fig.hidden = !onMap.length;
|
||
return onMap;
|
||
}
|
||
|
||
var onMap = render();
|
||
var mapIds = onMap.join(' ');
|
||
/* The map starts only when it has a trail to draw. */
|
||
if (fig && onMap.length) {
|
||
fig.dataset.ids = mapIds;
|
||
mapOn = true;
|
||
} else if (fig) {
|
||
fig.removeAttribute('data-map');
|
||
}
|
||
|
||
var mergeButton = page.querySelector('[data-saved-merge]');
|
||
mergeButton.addEventListener('click', function () {
|
||
var own = readSaved();
|
||
var added = 0;
|
||
linkIds.forEach(function (id) {
|
||
if (own.indexOf(id) === -1) { own.push(id); added += 1; }
|
||
});
|
||
if (added) writeSaved(own);
|
||
merged.textContent = added ? 'Added ' + plural(added, 'trail', 'trails') + ' to your saved trails.'
|
||
: 'All of these were saved already.';
|
||
});
|
||
|
||
document.addEventListener('olt:saved', function (e) {
|
||
if (!fromLink) {
|
||
e.detail.ids.forEach(function (id) { if (shown.indexOf(id) === -1) shown.push(id); });
|
||
}
|
||
var ids = render();
|
||
if (ids.length && fig && !mapOn) { window.location.reload(); return; }
|
||
if (ids.length && ids.join(' ') !== mapIds) {
|
||
mapIds = ids.join(' ');
|
||
maps.forEach(function (m) { if (m.kind === 'overview') setVisible(m, ids); });
|
||
}
|
||
var select = results.querySelector('[data-sort-select]');
|
||
applySortUI(results, select ? select.value : 'saved', false);
|
||
});
|
||
}
|
||
|
||
var savedPage = document.querySelector('[data-saved-page]');
|
||
if (savedPage) initSavedPage(savedPage);
|
||
|
||
/* 2. Maps ------------------------------------------------------------------- */
|
||
|
||
var STYLE_URL = 'https://tiles.openfreemap.org/styles/positron';
|
||
|
||
/* The outdoor base map of a trail page: the style named by the figure's
|
||
data-outdoor (web/assets/map/outdoor.json), with relief from its olt-dem
|
||
elevation source and contour lines made in the browser by maplibre-contour
|
||
(window.mlcontour, loaded after MapLibre on trail pages only). Each layer
|
||
carries its own dark-theme paint (metadata "olt:dark") and its layout for
|
||
a reader who puts km and m first (metadata "olt:km"); the contour steps for
|
||
each unit are in the style's metadata "olt:contours". Relief and contours
|
||
are extras: they stay hidden until the map is ready, so a slow or blocked
|
||
elevation host never holds the map up, and their errors never fail it. */
|
||
var ELEVATION = { 'olt-dem': true, 'olt-contours': true };
|
||
var outdoorStyles = {};
|
||
var demSource;
|
||
|
||
function fetchOutdoor(url) {
|
||
if (!outdoorStyles[url]) {
|
||
outdoorStyles[url] = fetch(url, { credentials: 'same-origin' })
|
||
.then(function (r) {
|
||
if (!r.ok) throw new Error(r.status);
|
||
return r.json();
|
||
})
|
||
.catch(function () { return null; });
|
||
}
|
||
return outdoorStyles[url];
|
||
}
|
||
|
||
/* One elevation tile source for the page, shared by the relief and the
|
||
contours; null when the add-on is missing (relief only, then). */
|
||
function contourSource(dem) {
|
||
if (demSource !== undefined) return demSource;
|
||
demSource = null;
|
||
if (window.mlcontour && window.mlcontour.DemSource) {
|
||
try {
|
||
demSource = new window.mlcontour.DemSource({
|
||
url: dem.tiles[0], encoding: dem.encoding, maxzoom: dem.maxzoom, worker: true, timeoutMs: 10000
|
||
});
|
||
demSource.setupMaplibre(window.maplibregl);
|
||
} catch (e) { demSource = null; }
|
||
}
|
||
return demSource;
|
||
}
|
||
|
||
function contourUrl(base) {
|
||
var opts = base.metadata['olt:contours'][milesFirst() ? 'ft' : 'm'];
|
||
return demSource.contourProtocolUrl({ multiplier: opts.multiplier, thresholds: opts.thresholds });
|
||
}
|
||
|
||
function assign(target, values) {
|
||
Object.keys(values).forEach(function (k) { target[k] = values[k]; });
|
||
return target;
|
||
}
|
||
|
||
/* The style to hand MapLibre: the file in the reader's theme and units, its
|
||
elevation tiles fetched through the add-on when it is here. */
|
||
function outdoorStyle(base, hideElevation) {
|
||
var style = JSON.parse(JSON.stringify(base));
|
||
var dark = isDark();
|
||
var km = !milesFirst();
|
||
var dem = style.sources['olt-dem'];
|
||
var contours = dem ? contourSource(dem) : null;
|
||
if (contours) {
|
||
dem.tiles = [contours.sharedDemProtocolUrl];
|
||
style.sources['olt-contours'].tiles = [contourUrl(base)];
|
||
} else {
|
||
delete style.sources['olt-contours'];
|
||
style.layers = style.layers.filter(function (layer) { return layer.source !== 'olt-contours'; });
|
||
}
|
||
style.layers.forEach(function (layer) {
|
||
var meta = layer.metadata || {};
|
||
if (dark && meta['olt:dark']) layer.paint = assign(layer.paint || {}, meta['olt:dark']);
|
||
if (km && meta['olt:km']) layer.layout = assign(layer.layout || {}, meta['olt:km']);
|
||
if (hideElevation && ELEVATION[layer.source]) layer.layout = assign(layer.layout || {}, { visibility: 'none' });
|
||
});
|
||
return style;
|
||
}
|
||
|
||
/* A theme or units change on a live outdoor map: the same metadata, set
|
||
layer by layer (MapLibre skips a value that is already set). */
|
||
function restyleOutdoor(map, base) {
|
||
var dark = isDark();
|
||
var km = !milesFirst();
|
||
base.layers.forEach(function (layer) {
|
||
if (!map.getLayer(layer.id)) return;
|
||
var night = (layer.metadata || {})['olt:dark'] || {};
|
||
var metric = (layer.metadata || {})['olt:km'] || {};
|
||
Object.keys(night).forEach(function (prop) {
|
||
map.setPaintProperty(layer.id, prop, dark ? night[prop] : (layer.paint || {})[prop]);
|
||
});
|
||
Object.keys(metric).forEach(function (prop) {
|
||
map.setLayoutProperty(layer.id, prop, km ? metric[prop] : (layer.layout || {})[prop]);
|
||
});
|
||
});
|
||
}
|
||
|
||
function showElevation(map) {
|
||
map.getStyle().layers.forEach(function (layer) {
|
||
if (ELEVATION[layer.source]) map.setLayoutProperty(layer.id, 'visibility', 'visible');
|
||
});
|
||
}
|
||
|
||
/* The Outdoor / Plain switch: a MapLibre control of two toggle buttons, top
|
||
left, clear of the zoom buttons, the scale bar and the credits. */
|
||
function basemapControl() {
|
||
var box;
|
||
function sync() {
|
||
var choice = basemapChoice();
|
||
box.querySelectorAll('button').forEach(function (b) {
|
||
b.setAttribute('aria-pressed', b.dataset.basemap === choice ? 'true' : 'false');
|
||
});
|
||
}
|
||
return {
|
||
sync: sync,
|
||
onAdd: function () {
|
||
box = document.createElement('div');
|
||
box.className = 'maplibregl-ctrl maplibregl-ctrl-group basemap-switch';
|
||
box.setAttribute('role', 'group');
|
||
box.setAttribute('aria-label', 'Base map');
|
||
[['outdoor', 'Outdoor'], ['plain', 'Plain']].forEach(function (pair) {
|
||
var b = document.createElement('button');
|
||
b.type = 'button';
|
||
b.dataset.basemap = pair[0];
|
||
b.textContent = pair[1];
|
||
b.addEventListener('click', function () { if (basemapChoice() !== pair[0]) setBasemap(pair[0]); });
|
||
box.appendChild(b);
|
||
});
|
||
sync();
|
||
return box;
|
||
},
|
||
onRemove: function () { if (box.parentNode) box.parentNode.removeChild(box); }
|
||
};
|
||
}
|
||
|
||
function linesOf(feature) {
|
||
var g = feature.geometry || {};
|
||
if (g.type === 'LineString') return [g.coordinates];
|
||
return g.type === 'MultiLineString' ? g.coordinates : [];
|
||
}
|
||
|
||
function pointsOf(feature) {
|
||
var g = feature.geometry || {};
|
||
if (g.type === 'Point') return [g.coordinates];
|
||
return g.type === 'MultiPoint' ? g.coordinates : [];
|
||
}
|
||
|
||
function allCoords(feature) {
|
||
var out = [];
|
||
linesOf(feature).forEach(function (line) { line.forEach(function (p) { out.push(p); }); });
|
||
pointsOf(feature).forEach(function (p) { out.push(p); });
|
||
return out;
|
||
}
|
||
|
||
function bboxOf(fc) {
|
||
if (fc.bbox && fc.bbox.length === 4) return fc.bbox;
|
||
var b = [180, 90, -180, -90];
|
||
fc.features.forEach(function (f) {
|
||
allCoords(f).forEach(function (p) {
|
||
if (p[0] < b[0]) b[0] = p[0];
|
||
if (p[1] < b[1]) b[1] = p[1];
|
||
if (p[0] > b[2]) b[2] = p[0];
|
||
if (p[1] > b[3]) b[3] = p[1];
|
||
});
|
||
});
|
||
return b;
|
||
}
|
||
|
||
function bboxOfFeature(feature) {
|
||
var b = [180, 90, -180, -90];
|
||
var seen = false;
|
||
allCoords(feature).forEach(function (p) {
|
||
seen = true;
|
||
if (p[0] < b[0]) b[0] = p[0];
|
||
if (p[1] < b[1]) b[1] = p[1];
|
||
if (p[0] > b[2]) b[2] = p[0];
|
||
if (p[1] > b[3]) b[3] = p[1];
|
||
});
|
||
return seen ? b : null;
|
||
}
|
||
|
||
/* A single sketch point has no span; give it about a county of ground. */
|
||
function ensureSpan(bbox) {
|
||
var b = bbox.slice();
|
||
if (b[2] - b[0] < 0.02) { var cx = (b[0] + b[2]) / 2; b[0] = cx - 0.25; b[2] = cx + 0.25; }
|
||
if (b[3] - b[1] < 0.02) { var cy = (b[1] + b[3]) / 2; b[1] = cy - 0.18; b[3] = cy + 0.18; }
|
||
return b;
|
||
}
|
||
|
||
function mercY(lat) { return Math.log(Math.tan(Math.PI / 4 + (lat * Math.PI) / 360)); }
|
||
function invMercY(y) { return (360 / Math.PI) * Math.atan(Math.exp(y)) - 90; }
|
||
|
||
function dms(value, pos, neg) {
|
||
var a = Math.abs(value);
|
||
var d = Math.floor(a);
|
||
var m = Math.round((a - d) * 60);
|
||
if (m === 60) { d += 1; m = 0; }
|
||
return d + '°' + (m < 10 ? '0' : '') + m + '′' + (value >= 0 ? pos : neg);
|
||
}
|
||
|
||
/* The ground a W x H frame shows when it fits bbox with pad pixels around it (Web Mercator,
|
||
as MapLibre's fitBounds does). */
|
||
function extentOf(bbox, W, H, pad) {
|
||
var rad = Math.PI / 180;
|
||
var cx = (bbox[0] + bbox[2]) / 2;
|
||
var y0 = mercY(bbox[1]);
|
||
var y1 = mercY(bbox[3]);
|
||
var cy = (y0 + y1) / 2;
|
||
var k = Math.min((W - 2 * pad) / Math.max((bbox[2] - bbox[0]) * rad, 1e-6),
|
||
(H - 2 * pad) / Math.max(y1 - y0, 1e-6));
|
||
return { cx: cx, cy: cy, k: k, rad: rad,
|
||
w: cx - W / 2 / k / rad, e: cx + W / 2 / k / rad, n: invMercY(cy + H / 2 / k), s: invMercY(cy - H / 2 / k) };
|
||
}
|
||
|
||
/* Lines on a plain graticule (Web Mercator), no tiles. Returns the drawn extent. */
|
||
function drawFallback(host, fc, bbox, routeMain) {
|
||
bbox = ensureSpan(bbox);
|
||
var rect = host.getBoundingClientRect();
|
||
var W = Math.max(Math.round(rect.width), 280);
|
||
var H = Math.max(Math.round(rect.height), 180);
|
||
var ext = extentOf(bbox, W, H, 28);
|
||
var rad = ext.rad, cx = ext.cx, cy = ext.cy, k = ext.k;
|
||
function x(lon) { return W / 2 + (lon - cx) * rad * k; }
|
||
function y(lat) { return H / 2 - (mercY(lat) - cy) * k; }
|
||
|
||
var span = Math.max(ext.e - ext.w, ext.n - ext.s);
|
||
var step = [0.25, 0.5, 1, 2, 5, 10, 15].filter(function (s) { return span / s <= 9; })[0] || 20;
|
||
var grat = '';
|
||
for (var lon = Math.ceil(ext.w / step) * step; lon <= ext.e; lon += step) grat += 'M' + x(lon).toFixed(1) + ' 0V' + H;
|
||
for (var lat = Math.ceil(ext.s / step) * step; lat <= ext.n; lat += step) grat += 'M0 ' + y(lat).toFixed(1) + 'H' + W;
|
||
|
||
var ns = 'http://www.w3.org/2000/svg';
|
||
var svg = document.createElementNS(ns, 'svg');
|
||
svg.setAttribute('viewBox', '0 0 ' + W + ' ' + H);
|
||
svg.setAttribute('preserveAspectRatio', 'xMidYMid slice');
|
||
svg.setAttribute('aria-hidden', 'true');
|
||
svg.setAttribute('focusable', 'false');
|
||
function path(cls, d, id) {
|
||
var p = document.createElementNS(ns, 'path');
|
||
p.setAttribute('class', cls);
|
||
p.setAttribute('d', d);
|
||
if (id) p.setAttribute('data-id', id);
|
||
svg.appendChild(p);
|
||
}
|
||
path('fallback__grat', grat);
|
||
var drawn = fc.features.map(function (f) {
|
||
var d = linesOf(f).map(function (line) {
|
||
return 'M' + line.map(function (p) { return x(p[0]).toFixed(1) + ' ' + y(p[1]).toFixed(1); }).join('L');
|
||
}).join('');
|
||
var p = f.properties || {};
|
||
var isRoute = p.kind === 'route' || (routeMain && (p.role === 'main' || p.sketch));
|
||
var mod = p.sketch ? ' fallback__line--sketch' + (isRoute ? ' fallback__line--sketch-route' : '')
|
||
: p.role === 'alternate' ? ' fallback__line--alternate'
|
||
: isRoute ? ' fallback__line--route' : '';
|
||
return { d: d, mod: mod, id: p.id || '', sketch: !!p.sketch };
|
||
}).filter(function (l) { return l.d; });
|
||
drawn.filter(function (l) { return !l.sketch; }).forEach(function (l) { path('fallback__casing', l.d, l.id); });
|
||
drawn.forEach(function (l) { path('fallback__line' + l.mod, l.d, l.id); });
|
||
fc.features.forEach(function (f) {
|
||
var p = f.properties || {};
|
||
var ringCls = 'fallback__ring' + ((p.kind === 'route' || routeMain) ? ' fallback__ring--route' : '');
|
||
pointsOf(f).forEach(function (pt) {
|
||
var c = document.createElementNS(ns, 'circle');
|
||
c.setAttribute('class', ringCls);
|
||
c.setAttribute('cx', x(pt[0]).toFixed(1));
|
||
c.setAttribute('cy', y(pt[1]).toFixed(1));
|
||
c.setAttribute('r', '4');
|
||
if (p.id) c.setAttribute('data-id', p.id);
|
||
svg.appendChild(c);
|
||
});
|
||
});
|
||
host.replaceChildren(svg);
|
||
return ext;
|
||
}
|
||
|
||
function webglAvailable() {
|
||
try {
|
||
var c = document.createElement('canvas');
|
||
return !!(c.getContext('webgl2') || c.getContext('webgl'));
|
||
} catch (e) { return false; }
|
||
}
|
||
|
||
/* Night version of the Positron base map: invert lightness, keep hue. */
|
||
var COLOR_PROPS = ['background-color', 'fill-color', 'fill-outline-color', 'line-color', 'text-color', 'text-halo-color'];
|
||
var ctx2d = null;
|
||
|
||
function parseColor(str) {
|
||
if (typeof str !== 'string' || !/^(#|rgb|hsl)/i.test(str.trim())) return null;
|
||
if (!ctx2d) ctx2d = document.createElement('canvas').getContext('2d');
|
||
ctx2d.fillStyle = '#000';
|
||
ctx2d.fillStyle = str;
|
||
var v = ctx2d.fillStyle;
|
||
var m;
|
||
if (v.charAt(0) === '#') return [parseInt(v.slice(1, 3), 16), parseInt(v.slice(3, 5), 16), parseInt(v.slice(5, 7), 16), 1];
|
||
if ((m = v.match(/rgba?\(([^)]+)\)/))) {
|
||
var parts = m[1].split(',').map(Number);
|
||
return [parts[0], parts[1], parts[2], parts.length > 3 ? parts[3] : 1];
|
||
}
|
||
return null;
|
||
}
|
||
|
||
function toHsl(r, g, b) {
|
||
r /= 255; g /= 255; b /= 255;
|
||
var max = Math.max(r, g, b), min = Math.min(r, g, b), l = (max + min) / 2, h = 0, s = 0;
|
||
if (max !== min) {
|
||
var d = max - min;
|
||
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
|
||
h = max === r ? (g - b) / d + (g < b ? 6 : 0) : max === g ? (b - r) / d + 2 : (r - g) / d + 4;
|
||
h *= 60;
|
||
}
|
||
return [h, s, l];
|
||
}
|
||
|
||
function nightColor(value, prop, layerId) {
|
||
if (Array.isArray(value)) return value.map(function (v) { return nightColor(v, prop, layerId); });
|
||
if (value && typeof value === 'object') {
|
||
var out = {};
|
||
Object.keys(value).forEach(function (k) { out[k] = nightColor(value[k], prop, layerId); });
|
||
return out;
|
||
}
|
||
var c = parseColor(value);
|
||
if (!c) return value;
|
||
if (layerId === 'water') return 'hsla(205, 22%, 6%, ' + c[3] + ')';
|
||
var hsl = toHsl(c[0], c[1], c[2]);
|
||
var l = prop === 'text-color' ? 0.92 - hsl[2] * 0.55 : 0.07 + (1 - hsl[2]) * 0.55;
|
||
return 'hsla(' + hsl[0].toFixed(0) + ', ' + (hsl[1] * 80).toFixed(0) + '%, ' + (l * 100).toFixed(1) + '%, ' + c[3] + ')';
|
||
}
|
||
|
||
function tintBasemap(map, originals) {
|
||
var dark = isDark();
|
||
map.getStyle().layers.forEach(function (layer) {
|
||
if (layer.id.indexOf('olt-') === 0) return;
|
||
COLOR_PROPS.forEach(function (prop) {
|
||
var key = layer.id + '|' + prop;
|
||
if (!(key in originals)) {
|
||
try { originals[key] = map.getPaintProperty(layer.id, prop); } catch (e) { originals[key] = undefined; }
|
||
}
|
||
var orig = originals[key];
|
||
if (orig === undefined) return;
|
||
try { map.setPaintProperty(layer.id, prop, dark ? nightColor(orig, prop, layer.id) : orig); } catch (e) { /* no such property */ }
|
||
});
|
||
});
|
||
}
|
||
|
||
/* Track features carry no sketch property at all, so has/!has is the
|
||
type-safe test (a != against true would compare boolean with null). */
|
||
var NOT_SKETCH = ['!', ['has', 'sketch']];
|
||
var IS_SKETCH = ['has', 'sketch'];
|
||
|
||
function layerDefs(kind, routeMain) {
|
||
if (kind === 'overview') {
|
||
return [
|
||
{ id: 'olt-casing', color: '--casing', width: [3, 4, 9, 9], filter: NOT_SKETCH },
|
||
{ id: 'olt-hi', color: '--ink', width: [3, 7, 9, 11], filter: ['==', ['get', 'id'], ''] },
|
||
{ id: 'olt-hi-point', type: 'circle', stroke: '--ink', strokeWidth: 3, radius: [3, 5, 9, 9], filter: ['==', ['get', 'id'], ''] },
|
||
{ id: 'olt-line', color: '--trail', width: [3, 1.6, 9, 4], filter: ['all', NOT_SKETCH, ['!=', ['get', 'kind'], 'route']], hit: true },
|
||
{ id: 'olt-route', color: '--route', width: [3, 1.6, 9, 4], dash: [1.6, 1.1], filter: ['all', NOT_SKETCH, ['==', ['get', 'kind'], 'route']], hit: true },
|
||
{ id: 'olt-sketch', color: '--trail', width: [3, 2, 9, 3.5], dash: [0, 2], cap: 'round', filter: ['all', IS_SKETCH, ['!=', ['get', 'kind'], 'route']], hit: true },
|
||
{ id: 'olt-sketch-route', color: '--route', width: [3, 2, 9, 3.5], dash: [0, 2], cap: 'round', filter: ['all', IS_SKETCH, ['==', ['get', 'kind'], 'route']], hit: true },
|
||
{ id: 'olt-sketch-points', type: 'circle', strokeByKind: true, strokeWidth: 2, radius: [3, 3, 9, 6], filter: IS_SKETCH, hit: true }
|
||
];
|
||
}
|
||
/* A trail page with no track: the map draws no layer at all. The start and the end are
|
||
labelled markers (placeTermini in initMap), and nothing joins them. */
|
||
if (kind === 'sketch') return [];
|
||
return [
|
||
{ id: 'olt-casing', color: '--casing', width: [5, 4, 12, 8] },
|
||
{ id: 'olt-alt', color: '--trail', width: [5, 1.5, 12, 3], dash: [2, 1.6], filter: ['==', ['get', 'role'], 'alternate'] },
|
||
{ id: 'olt-main', color: routeMain ? '--route' : '--trail', width: [5, 2.5, 12, 5],
|
||
dash: routeMain ? [2, 1.2] : null, filter: ['==', ['get', 'role'], 'main'] }
|
||
];
|
||
}
|
||
|
||
function circleStroke(def) {
|
||
if (def.strokeByKind) return ['match', ['get', 'kind'], 'route', token('--route'), token('--trail')];
|
||
return token(def.stroke || '--trail');
|
||
}
|
||
|
||
/* Trail lines go under the base map's labels, as on a printed map (and over
|
||
the contour labels, which sit with their lines below the water). */
|
||
function addLineLayers(map, fc, defs) {
|
||
var firstLabel = map.getStyle().layers.filter(function (l) { return l.type === 'symbol' && !ELEVATION[l.source]; })[0];
|
||
map.addSource('olt', { type: 'geojson', data: fc });
|
||
defs.forEach(function (def) {
|
||
var layer;
|
||
if (def.type === 'circle') {
|
||
layer = {
|
||
id: def.id, type: 'circle', source: 'olt',
|
||
paint: {
|
||
'circle-radius': ['interpolate', ['linear'], ['zoom'], def.radius[0], def.radius[1], def.radius[2], def.radius[3]],
|
||
'circle-color': token('--casing'),
|
||
'circle-stroke-color': circleStroke(def),
|
||
'circle-stroke-width': def.strokeWidth || 2
|
||
}
|
||
};
|
||
} else {
|
||
layer = {
|
||
id: def.id,
|
||
type: 'line',
|
||
source: 'olt',
|
||
layout: { 'line-join': 'round', 'line-cap': def.cap || (def.dash ? 'butt' : 'round') },
|
||
paint: {
|
||
'line-color': token(def.color),
|
||
'line-width': ['interpolate', ['linear'], ['zoom'], def.width[0], def.width[1], def.width[2], def.width[3]]
|
||
}
|
||
};
|
||
if (def.dash) layer.paint['line-dasharray'] = def.dash;
|
||
if (def.opacity) layer.paint['line-opacity'] = def.opacity;
|
||
}
|
||
if (def.filter) layer.filter = def.filter;
|
||
map.addLayer(layer, firstLabel && firstLabel.id);
|
||
});
|
||
}
|
||
|
||
var maps = [];
|
||
|
||
/* MapLibre's style and script, and the contour add-on on a trail page, are
|
||
loaded when the first map starts, from the addresses on this script's own
|
||
tag (layout.py). The scripts run in the order added (async = false), so the
|
||
add-on finds MapLibre. Resolves when all have loaded or failed; a missing
|
||
library is noticed by the map, not here. */
|
||
var libsLoaded = null;
|
||
function loadLibraries() {
|
||
if (libsLoaded) return libsLoaded;
|
||
var d = (SCRIPT && SCRIPT.dataset) || {};
|
||
function done(el, resolve, before) {
|
||
el.addEventListener('load', resolve);
|
||
el.addEventListener('error', resolve);
|
||
document.head.insertBefore(el, before || null);
|
||
}
|
||
var jobs = [];
|
||
if (d.maplibreCss && !document.querySelector('link[data-maplibre-css]')) {
|
||
jobs.push(new Promise(function (resolve) {
|
||
var link = document.createElement('link');
|
||
link.rel = 'stylesheet';
|
||
link.href = d.maplibreCss;
|
||
link.setAttribute('data-maplibre-css', '');
|
||
/* Before the site's stylesheet, where the page used to carry it: the
|
||
site's rules for the map's frame must win over MapLibre's. */
|
||
done(link, resolve, document.head.querySelector('link[rel="stylesheet"]'));
|
||
}));
|
||
}
|
||
[window.maplibregl ? '' : d.maplibreJs, window.mlcontour ? '' : d.contourJs].forEach(function (src) {
|
||
if (!src) return;
|
||
jobs.push(new Promise(function (resolve) {
|
||
var script = document.createElement('script');
|
||
script.src = src;
|
||
script.async = false;
|
||
done(script, resolve);
|
||
}));
|
||
});
|
||
libsLoaded = Promise.all(jobs);
|
||
return libsLoaded;
|
||
}
|
||
|
||
function initMap(fig) {
|
||
var host = fig.querySelector('.map');
|
||
var fallback = host.querySelector('.map__fallback');
|
||
var status = host.querySelector('.map__status');
|
||
var nw = fig.querySelector('.mapfig__corner--nw');
|
||
var se = fig.querySelector('.mapfig__corner--se');
|
||
var kind = fig.dataset.map;
|
||
var routeMain = fig.dataset.route === 'yes';
|
||
var defs = layerDefs(kind, routeMain);
|
||
var onlyIds = (fig.dataset.ids || '').trim().split(/\s+/).filter(Boolean);
|
||
/* A trail page's map: fetch its outdoor style now, beside the track data. */
|
||
var outdoorUrl = kind === 'overview' ? '' : (fig.dataset.outdoor || '');
|
||
var entry = { kind: kind, map: null, fc: null, defs: defs, fallback: fallback,
|
||
visibleIds: onlyIds.length ? onlyIds : null, ready: false,
|
||
mapStarted: false, fullBounds: null, boundsById: null };
|
||
maps.push(entry);
|
||
|
||
var bbox = (fig.dataset.bbox || '').split(',').map(Number);
|
||
if (bbox.length !== 4 || bbox.some(isNaN)) bbox = null;
|
||
/* The ground the static drawing shows, and the live map opens on with no padding of its own. */
|
||
var frame = (fig.dataset.frame || '').split(',').map(Number);
|
||
if (frame.length !== 4 || frame.some(isNaN)) frame = null;
|
||
entry.frame = frame;
|
||
entry.initialIds = onlyIds.length ? onlyIds : null;
|
||
|
||
/* The page's own drawing of the tracks (class map__sketch) stays until the live map has
|
||
drawn its first frame. Until then nothing is redrawn over it. */
|
||
function hasSketch() { return !!fallback.querySelector('.map__sketch'); }
|
||
function say(text) {
|
||
status.textContent = text;
|
||
status.dataset.say = '1';
|
||
}
|
||
|
||
function corners(ext) {
|
||
/* A wide map of Alaska reaches past 180°: name that longitude as the east one it is. */
|
||
var wrap = function (lon) { return ((lon + 540) % 360) - 180; };
|
||
if (nw) nw.textContent = dms(ext.n, 'N', 'S') + ' ' + dms(wrap(ext.w), 'E', 'W');
|
||
if (se) se.textContent = dms(ext.s, 'N', 'S') + ' ' + dms(wrap(ext.e), 'E', 'W');
|
||
}
|
||
function sketchCorners() {
|
||
if (!frame || !hasSketch()) return;
|
||
var r = host.getBoundingClientRect();
|
||
if (r.width && r.height) corners(extentOf(frame, r.width, r.height, 0));
|
||
placeTermini();
|
||
}
|
||
|
||
/* The start and the end of a trail with no track: a dot and a label each, from the
|
||
page's own list of them (.terminus-item: the record's places and positions). They sit
|
||
over the drawing and over the live map alike, and are text for screen readers in that
|
||
list, so the layer is hidden from them. A northern end's label goes above its dot, a
|
||
southern one's below, and each is kept inside the frame. */
|
||
var termini = [];
|
||
var terminiLayer = host.querySelector('.map__termini');
|
||
if (terminiLayer) {
|
||
Array.prototype.forEach.call(fig.querySelectorAll('.terminus-item'), function (li) {
|
||
var el = document.createElement('div');
|
||
el.className = 'terminus terminus--' + li.dataset.role;
|
||
var dot = document.createElement('span');
|
||
dot.className = 'terminus__dot';
|
||
var label = document.createElement('span');
|
||
label.className = 'terminus__label';
|
||
label.textContent = li.dataset.label;
|
||
el.appendChild(dot);
|
||
el.appendChild(label);
|
||
terminiLayer.appendChild(el);
|
||
termini.push({ el: el, label: label, lon: Number(li.dataset.lon), lat: Number(li.dataset.lat) });
|
||
});
|
||
}
|
||
function placeTermini() {
|
||
if (!termini.length) return;
|
||
var r = host.getBoundingClientRect();
|
||
if (!r.width || !r.height) return;
|
||
var W = r.width, H = r.height;
|
||
var ext = entry.ready && entry.map ? null : (frame ? extentOf(frame, W, H, 0) : null);
|
||
var xy = termini.map(function (t) {
|
||
if (ext) return { x: W / 2 + (t.lon - ext.cx) * ext.rad * ext.k, y: H / 2 - (mercY(t.lat) - ext.cy) * ext.k };
|
||
var p = entry.map.project([t.lon, t.lat]);
|
||
return { x: p.x, y: p.y };
|
||
});
|
||
/* Each label goes above or below its dot, as far as possible outward (the northern end's
|
||
above, the southern end's below), kept inside the frame, and never over the other label
|
||
or the other dot: the four ways of placing two labels are scored and the best one used. */
|
||
var HALF = 13;
|
||
var boxes = termini.map(function (t, i) {
|
||
var w = t.label.offsetWidth, h = t.label.offsetHeight, p = xy[i];
|
||
var x0 = p.x - w / 2;
|
||
var dx = x0 < 8 ? 8 - x0 : (x0 + w > W - 8 ? (W - 8) - (x0 + w) : 0);
|
||
return { w: w, h: h, dx: dx, left: x0 + dx };
|
||
});
|
||
function rect(i, above) {
|
||
var b = boxes[i], p = xy[i];
|
||
var top = above ? p.y - HALF - b.h : p.y + HALF;
|
||
return { l: b.left, r: b.left + b.w, t: top, b: top + b.h };
|
||
}
|
||
function overlap(a, c) {
|
||
var w = Math.min(a.r, c.r) - Math.max(a.l, c.l), h = Math.min(a.b, c.b) - Math.max(a.t, c.t);
|
||
return w > 0 && h > 0 ? w * h : 0;
|
||
}
|
||
function dotBox(i) { return { l: xy[i].x - 10, r: xy[i].x + 10, t: xy[i].y - 10, b: xy[i].y + 10 }; }
|
||
function cost(sides) {
|
||
var c = 0;
|
||
var rs = sides.map(function (above, i) { return rect(i, above); });
|
||
rs.forEach(function (r, i) {
|
||
c += Math.max(0, 4 - r.t) * 40 + Math.max(0, r.b - (H - 4)) * 40;
|
||
xy.forEach(function (_, j) { if (j !== i) c += overlap(r, dotBox(j)) * 20; });
|
||
if (i > 0) c += overlap(r, rs[0]) * 20;
|
||
});
|
||
return c;
|
||
}
|
||
var outward = xy.length > 1 && xy[1].y < xy[0].y ? [false, true] : [true, false];
|
||
var choices = xy.length === 1 ? [[true], [false]]
|
||
: [outward, [!outward[0], !outward[1]], [true, true], [false, false]];
|
||
var best = choices[0], bestCost = Infinity;
|
||
choices.forEach(function (c) {
|
||
var k = cost(c);
|
||
if (k < bestCost) { best = c; bestCost = k; }
|
||
});
|
||
termini.forEach(function (t, i) {
|
||
var p = xy[i];
|
||
t.el.style.transform = 'translate(' + p.x.toFixed(1) + 'px,' + p.y.toFixed(1) + 'px)';
|
||
t.el.classList.toggle('terminus--above', best[i]);
|
||
t.el.classList.toggle('terminus--below', !best[i]);
|
||
t.label.style.setProperty('--dx', boxes[i].dx.toFixed(1) + 'px');
|
||
});
|
||
}
|
||
entry.placeTermini = placeTermini;
|
||
function redrawFallback() {
|
||
if (hasSketch()) { sketchCorners(); return; }
|
||
if (!entry.ready && entry.fc) {
|
||
corners(drawFallback(fallback, entry.fc, bbox || bboxOf(entry.fc), routeMain));
|
||
if (entry.visibleIds) setVisible(entry, entry.visibleIds);
|
||
}
|
||
}
|
||
function fail(reason) {
|
||
if (entry.ready) return;
|
||
host.dataset.state = 'fallback';
|
||
say('Base map unavailable (' + reason + '). ' +
|
||
(kind === 'sketch' ? 'The start and end are drawn on a plain ground.' : 'The track is drawn on a plain ground.'));
|
||
}
|
||
|
||
/* Until the map starts the frame holds its size and its drawing (the page's own, or one
|
||
made from the data); the page loads nothing more for it. */
|
||
sketchCorners();
|
||
var dataReady = null;
|
||
function fetchData() {
|
||
return fetch(fig.dataset.src, { credentials: 'same-origin' })
|
||
.then(function (r) {
|
||
if (!r.ok) throw new Error(r.status);
|
||
return r.json();
|
||
})
|
||
.then(function (fc) {
|
||
entry.fc = fc;
|
||
bbox = bbox || bboxOf(fc);
|
||
entry.fullBounds = bbox.slice();
|
||
if (kind === 'overview') {
|
||
entry.boundsById = {};
|
||
fc.features.forEach(function (f) {
|
||
var id = (f.properties || {}).id;
|
||
var fb = id && bboxOfFeature(f);
|
||
if (!fb) return;
|
||
var prev = entry.boundsById[id];
|
||
entry.boundsById[id] = prev
|
||
? [Math.min(prev[0], fb[0]), Math.min(prev[1], fb[1]), Math.max(prev[2], fb[2]), Math.max(prev[3], fb[3])]
|
||
: fb;
|
||
});
|
||
}
|
||
redrawFallback();
|
||
return fc;
|
||
})
|
||
.catch(function () {
|
||
host.dataset.state = 'error';
|
||
say('The track data did not load. Downloads on this page are unaffected.');
|
||
return null;
|
||
});
|
||
}
|
||
|
||
var resizeTimer;
|
||
window.addEventListener('resize', function () {
|
||
clearTimeout(resizeTimer);
|
||
resizeTimer = setTimeout(redrawFallback, 150);
|
||
});
|
||
|
||
/* The MapLibre half of the map. begin() (below) fetches the data, which
|
||
draws the fallback, and loads MapLibre in the same breath; this part
|
||
runs once MapLibre has loaded. If it never arrives the fallback drawing
|
||
stays and fail() says why. */
|
||
function startMapLibre() {
|
||
if (!window.maplibregl) { dataReady.then(function (fc) { if (fc) fail('the map library did not load'); }); return; }
|
||
if (!webglAvailable()) { dataReady.then(function (fc) { if (fc) fail('WebGL is off in this browser'); }); return; }
|
||
var timer = setTimeout(function () { fail('tiles did not load'); }, 15000);
|
||
if (outdoorUrl) fetchOutdoor(outdoorUrl).then(function (base) { createMap(base, timer); });
|
||
else createMap(null, timer);
|
||
}
|
||
|
||
/* base: the outdoor style of a trail map, or null for a map with the plain
|
||
style only (the overview maps, or when the style file did not load). */
|
||
function createMap(base, timer) {
|
||
var current = base && basemapChoice() === 'outdoor' ? 'outdoor' : 'plain';
|
||
var contourMiles = milesFirst(); /* the units the contour tiles are made in */
|
||
var settled = false; /* the current style is loaded and dressed */
|
||
var styleGen = 0;
|
||
var switcher = null;
|
||
var options = {
|
||
container: host.querySelector('.map__canvas'),
|
||
style: current === 'outdoor' ? outdoorStyle(base, true) : STYLE_URL,
|
||
attributionControl: { compact: true },
|
||
cooperativeGestures: true,
|
||
dragRotate: false,
|
||
pitchWithRotate: false,
|
||
touchPitch: false
|
||
};
|
||
if (frame) {
|
||
/* The ground the page's drawing shows, fitted with no padding of its own: the track
|
||
does not move when the live map takes over. */
|
||
options.bounds = [[frame[0], frame[1]], [frame[2], frame[3]]];
|
||
options.fitBoundsOptions = { padding: 0 };
|
||
} else if (bbox) {
|
||
options.bounds = [[bbox[0], bbox[1]], [bbox[2], bbox[3]]];
|
||
options.fitBoundsOptions = { padding: 32 };
|
||
}
|
||
var map = new maplibregl.Map(options);
|
||
entry.map = map;
|
||
map.touchZoomRotate.disableRotation();
|
||
map.addControl(new maplibregl.NavigationControl({ showCompass: false }), 'top-right');
|
||
var scale = new maplibregl.ScaleControl({ unit: milesFirst() ? 'imperial' : 'metric', maxWidth: 110 });
|
||
map.addControl(scale, 'bottom-left');
|
||
document.addEventListener('olt:units', function () {
|
||
scale.setUnit(milesFirst() ? 'imperial' : 'metric');
|
||
if (settled && current === 'outdoor') restyle();
|
||
});
|
||
|
||
/* A narrow frame has no room for the open attribution beside the scale
|
||
bar; keep it behind its button there, resizing included. */
|
||
function collapseAttribution() {
|
||
if (host.clientWidth >= 560) return;
|
||
host.querySelectorAll('.maplibregl-ctrl-attrib').forEach(function (el) {
|
||
el.classList.remove('maplibregl-compact-show');
|
||
el.removeAttribute('open');
|
||
});
|
||
}
|
||
map.on('resize', collapseAttribution);
|
||
|
||
var originals = {};
|
||
map.on('error', function (e) {
|
||
if (e && e.sourceId && ELEVATION[e.sourceId]) return; /* no relief or contours here: the map stands */
|
||
if (!map.isStyleLoaded()) fail('the map style did not load');
|
||
});
|
||
|
||
/* The outdoor style in the reader's theme and units; contour tiles are
|
||
remade only when the units change. */
|
||
function restyle() {
|
||
restyleOutdoor(map, base);
|
||
var source = map.getSource('olt-contours');
|
||
if (source && demSource && contourMiles !== milesFirst()) {
|
||
contourMiles = milesFirst();
|
||
source.setTiles([contourUrl(base)]);
|
||
}
|
||
}
|
||
|
||
/* A freshly loaded base map: the theme, then the trail on top. */
|
||
function dress() {
|
||
if (current === 'plain') {
|
||
originals = {};
|
||
tintBasemap(map, originals);
|
||
} else {
|
||
restyle();
|
||
}
|
||
addLineLayers(map, entry.fc, defs);
|
||
}
|
||
|
||
var styleReady = new Promise(function (resolve) { map.on('load', resolve); });
|
||
Promise.all([dataReady, styleReady]).then(function (results) {
|
||
var fc = results[0];
|
||
if (!fc) return;
|
||
clearTimeout(timer);
|
||
dress();
|
||
if (!bbox) {
|
||
bbox = bboxOf(fc);
|
||
entry.fullBounds = bbox.slice();
|
||
map.fitBounds([[bbox[0], bbox[1]], [bbox[2], bbox[3]]], { padding: 32, animate: false });
|
||
}
|
||
entry.ready = true;
|
||
settled = true;
|
||
placeTermini();
|
||
/* The live map shows (and the drawing gives way, in the CSS) once it has drawn its first
|
||
frame with the tracks: the base map fills in behind a track that holds still. */
|
||
var shown = false;
|
||
function reveal() {
|
||
if (shown) return;
|
||
shown = true;
|
||
host.dataset.state = 'ready';
|
||
status.textContent = '';
|
||
delete status.dataset.say;
|
||
}
|
||
/* Once the tracks' source is loaded and the map has drawn a frame since: the base map
|
||
is still filling in, and the tracks it draws are where the drawing's were. */
|
||
function trackDrawn() {
|
||
if (!map.isSourceLoaded('olt')) return;
|
||
map.off('sourcedata', trackDrawn);
|
||
map.once('render', reveal);
|
||
map.triggerRepaint();
|
||
}
|
||
map.on('sourcedata', trackDrawn);
|
||
trackDrawn();
|
||
setTimeout(reveal, 2500);
|
||
collapseAttribution();
|
||
if (entry.visibleIds) setVisible(entry, entry.visibleIds);
|
||
if (kind === 'overview') wireOverview(entry, map, defs);
|
||
if (current === 'outdoor') showElevation(map);
|
||
if (base) {
|
||
switcher = basemapControl();
|
||
map.addControl(switcher, 'top-left');
|
||
/* The choice may have changed while the map was loading (another tab,
|
||
or Back): the change event came before the map was ready to follow it. */
|
||
applyBasemap();
|
||
}
|
||
mapCorners();
|
||
});
|
||
|
||
/* Outdoor <-> Plain. setStyle drops every added layer, so the trail is
|
||
drawn again on the new style, with the theme, the units and the
|
||
visible-ids filter; the camera stays where the reader left it. */
|
||
function applyBasemap() {
|
||
if (switcher) switcher.sync();
|
||
var want = base && basemapChoice() === 'outdoor' ? 'outdoor' : 'plain';
|
||
if (!entry.ready || want === current) return;
|
||
current = want;
|
||
settled = false;
|
||
var gen = ++styleGen;
|
||
if (want === 'outdoor') contourMiles = milesFirst();
|
||
map.setStyle(want === 'outdoor' ? outdoorStyle(base, false) : STYLE_URL, { diff: false });
|
||
map.once('style.load', function () {
|
||
if (gen !== styleGen) return; /* a later switch replaced this style */
|
||
dress();
|
||
if (entry.visibleIds) setVisible(entry, entry.visibleIds);
|
||
settled = true;
|
||
});
|
||
}
|
||
if (base) document.addEventListener('olt:basemap', applyBasemap);
|
||
|
||
function mapCorners() {
|
||
var b = map.getBounds();
|
||
corners({ w: b.getWest(), e: b.getEast(), n: b.getNorth(), s: b.getSouth() });
|
||
}
|
||
map.on('moveend', function () { if (entry.ready) mapCorners(); });
|
||
if (termini.length) {
|
||
map.on('move', placeTermini);
|
||
map.on('resize', placeTermini);
|
||
map.on('load', placeTermini);
|
||
}
|
||
document.addEventListener('olt:theme', function () {
|
||
if (!entry.ready || !settled) return; /* a style still loading is dressed in the new theme */
|
||
if (current === 'outdoor') restyle();
|
||
else tintBasemap(map, originals);
|
||
defs.forEach(function (def) {
|
||
if (def.type === 'circle') {
|
||
map.setPaintProperty(def.id, 'circle-color', token('--casing'));
|
||
map.setPaintProperty(def.id, 'circle-stroke-color', circleStroke(def));
|
||
} else {
|
||
map.setPaintProperty(def.id, 'line-color', token(def.color));
|
||
}
|
||
});
|
||
});
|
||
}
|
||
|
||
/* Start this map: the data, the base map's style and the libraries all
|
||
load now, side by side. Once only. */
|
||
function begin() {
|
||
if (entry.mapStarted) return;
|
||
entry.mapStarted = true;
|
||
if (outdoorUrl) fetchOutdoor(outdoorUrl);
|
||
dataReady = dataReady || fetchData();
|
||
loadLibraries().then(startMapLibre);
|
||
}
|
||
|
||
/* When: every map starts by itself, once, a moment after the page has loaded: when the browser
|
||
is idle, or after 500 ms at most. Where the map sits, how big the screen is, and whether the
|
||
visitor scrolls, touches, types or does nothing make no difference. The drawing in the frame
|
||
holds the place of the map until its first frame is drawn. */
|
||
function afterLoad(fn) {
|
||
if (document.readyState === 'complete') fn();
|
||
else window.addEventListener('load', fn, { once: true });
|
||
}
|
||
afterLoad(function () {
|
||
if (window.requestIdleCallback) requestIdleCallback(begin, { timeout: 500 });
|
||
else setTimeout(begin, 100);
|
||
});
|
||
/* A frame the page came without a drawing for (the saved trails, known only here) is drawn
|
||
from the data as soon as this script has it. */
|
||
if (!hasSketch()) dataReady = fetchData();
|
||
}
|
||
|
||
function entryFor(id) {
|
||
return document.querySelector('.entry[data-id="' + (window.CSS && CSS.escape ? CSS.escape(id) : id) + '"]');
|
||
}
|
||
|
||
function highlight(id) {
|
||
maps.forEach(function (m) {
|
||
if (!m.map || !m.ready) return;
|
||
if (m.map.getLayer('olt-hi')) m.map.setFilter('olt-hi', ['==', ['get', 'id'], id || '']);
|
||
if (m.map.getLayer('olt-hi-point')) m.map.setFilter('olt-hi-point', ['==', ['get', 'id'], id || '']);
|
||
});
|
||
document.querySelectorAll('.entry.is-highlit').forEach(function (li) { li.classList.remove('is-highlit'); });
|
||
if (id) {
|
||
var li = entryFor(id);
|
||
if (li) li.classList.add('is-highlit');
|
||
}
|
||
}
|
||
|
||
function wireOverview(entry, map, defs) {
|
||
var popup = new maplibregl.Popup({ closeButton: true, maxWidth: '18rem' });
|
||
/* The trail the open popup is about, so that it closes when the filters
|
||
leave that trail out, and its length follows the units. */
|
||
var shown = null;
|
||
popup.on('close', function () { shown = null; });
|
||
entry.closePopupUnless = function (set) { if (shown && !set[shown.id]) popup.remove(); };
|
||
document.addEventListener('olt:units', function () { if (shown) shown.meta.textContent = shown.text(); });
|
||
defs.filter(function (d) { return d.hit; }).forEach(function (def) {
|
||
map.on('mousemove', def.id, function (e) {
|
||
map.getCanvas().style.cursor = 'pointer';
|
||
highlight(e.features[0].properties.id);
|
||
});
|
||
map.on('mouseleave', def.id, function () {
|
||
map.getCanvas().style.cursor = '';
|
||
highlight('');
|
||
});
|
||
map.on('click', def.id, function (e) {
|
||
var p = e.features[0].properties;
|
||
var box = document.createElement('div');
|
||
var a = document.createElement('a');
|
||
a.href = '/trails/' + encodeURIComponent(p.id) + '/';
|
||
a.textContent = p.name;
|
||
var meta = document.createElement('p');
|
||
var text = function () { return lengthText(Number(p.length_km)) + (p.kind === 'route' ? ' · Route' : ''); };
|
||
meta.textContent = text();
|
||
box.appendChild(a);
|
||
box.appendChild(meta);
|
||
if (p.sketch) {
|
||
var g = e.features[0].geometry || {};
|
||
var pointOnly = (g.type === 'MultiPoint' && g.coordinates.length === 1) || g.type === 'Point';
|
||
var note = document.createElement('p');
|
||
note.textContent = pointOnly
|
||
? 'No GPX here. The ring only marks where it starts and ends; it is not the trail\u2019s path.'
|
||
: 'No GPX here. The dotted line only joins the start and the end; it is not the trail\u2019s path.';
|
||
box.appendChild(note);
|
||
}
|
||
popup.setLngLat(e.lngLat).setDOMContent(box).addTo(map);
|
||
shown = { id: p.id, meta: meta, text: text };
|
||
});
|
||
});
|
||
}
|
||
|
||
/* Fit the overview map to the lines still shown; when every line is shown
|
||
again that is the full extent the map opened at. */
|
||
function fitToVisible(entry, ids) {
|
||
var map = entry.map;
|
||
if (!map || !entry.ready || entry.kind !== 'overview' || !entry.boundsById) return;
|
||
var union = null;
|
||
var shown = 0;
|
||
var total = Object.keys(entry.boundsById).length;
|
||
ids.forEach(function (id) {
|
||
var b = entry.boundsById[id];
|
||
if (!b) return;
|
||
shown += 1;
|
||
union = union
|
||
? [Math.min(union[0], b[0]), Math.min(union[1], b[1]), Math.max(union[2], b[2]), Math.max(union[3], b[3])]
|
||
: b.slice();
|
||
});
|
||
if (!union) return;
|
||
var target = shown >= total && entry.fullBounds ? entry.fullBounds : union;
|
||
var reduceMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
|
||
var padding = 32;
|
||
/* The trails the page opened on are the ground its drawing shows: that view needs no move. */
|
||
var first = entry.initialIds || Object.keys(entry.boundsById);
|
||
var same = ids.length === first.length && ids.every(function (id) { return first.indexOf(id) !== -1; });
|
||
if (entry.frame && same) { target = entry.frame; padding = 0; }
|
||
map.fitBounds([[target[0], target[1]], [target[2], target[3]]],
|
||
{ padding: padding, maxZoom: 11, animate: !reduceMotion });
|
||
}
|
||
|
||
function setVisible(entry, ids) {
|
||
entry.visibleIds = ids;
|
||
var set = {};
|
||
ids.forEach(function (id) { set[id] = true; });
|
||
/* Without the base map, the plain drawing is redrawn on the tracks still
|
||
shown, so the map shows what the filters leave at a useful scale. */
|
||
if (!entry.ready && entry.fc && entry.kind === 'overview') {
|
||
var sub = entry.fc.features.filter(function (f) { return set[(f.properties || {}).id]; });
|
||
var whole = sub.length === entry.fc.features.length;
|
||
/* The page's own drawing stands while the filters leave the trails it opened on. */
|
||
var opened = entry.initialIds ? entry.initialIds.length : Object.keys(entry.boundsById || {}).length;
|
||
var onSketch = entry.fallback.querySelector('.map__sketch');
|
||
if (onSketch && ids.length === opened) { sub = []; }
|
||
if (sub.length) drawFallback(entry.fallback, whole ? entry.fc : { type: 'FeatureCollection', features: sub },
|
||
whole && entry.fullBounds ? entry.fullBounds : bboxOf({ features: sub }), false);
|
||
}
|
||
entry.fallback.querySelectorAll('[data-id]').forEach(function (path) {
|
||
path.style.display = set[path.getAttribute('data-id')] ? '' : 'none';
|
||
});
|
||
var map = entry.map;
|
||
if (!map || !entry.ready) return;
|
||
if (entry.closePopupUnless) entry.closePopupUnless(set);
|
||
var inSet = ['in', ['get', 'id'], ['literal', ids]];
|
||
entry.defs.forEach(function (def) {
|
||
if (def.id.indexOf('olt-hi') === 0) return;
|
||
map.setFilter(def.id, def.filter ? ['all', def.filter, inSet] : inSet);
|
||
});
|
||
fitToVisible(entry, ids);
|
||
}
|
||
|
||
/* Each map starts by itself shortly after the page has loaded (initMap). The
|
||
finder and the other controls below never wait for a map. */
|
||
document.querySelectorAll('.mapfig[data-map]').forEach(initMap);
|
||
|
||
/* 2b. Sorting every list of trails -----------------------------------------
|
||
The same orders as facets.SORTS in Python, over the rows' data-*
|
||
attributes. The "Sort by" select is the one control for keyboard and
|
||
screen-reader users (the column-head buttons are aria-hidden pointer
|
||
shortcuts that always agree with it); each new order is announced in
|
||
the [data-sort-status] live region. ?sort=<key> is written with
|
||
replaceState when the order is not the page's default, and honoured
|
||
on load and on back/forward. */
|
||
|
||
var SORT_KEYS = { 'name': 1, 'name-desc': 1, 'length-desc': 1, 'length-asc': 1,
|
||
'time-asc': 1, 'time-desc': 1, 'climb-desc': 1, 'climb-asc': 1 };
|
||
var SORT_OLD = { 'long': 'length-desc', 'short': 'length-asc', 'quick': 'time-asc', 'climb': 'climb-desc' };
|
||
var SORT_COLUMN = { 'name': 'name', 'name-desc': 'name', 'length-desc': 'length', 'length-asc': 'length',
|
||
'time-asc': 'time', 'time-desc': 'time', 'climb-desc': 'climb', 'climb-asc': 'climb' };
|
||
var SORT_ARROW = { 'name': '↑', 'name-desc': '↓', 'length-desc': '↓', 'length-asc': '↑',
|
||
'time-asc': '↑', 'time-desc': '↓', 'climb-desc': '↓', 'climb-asc': '↑' };
|
||
var COLUMN_PRIMARY = { 'name': 'name', 'length': 'length-desc', 'time': 'time-asc', 'climb': 'climb-desc' };
|
||
var COLUMN_TOGGLE = { 'name': 'name-desc', 'name-desc': 'name', 'length-desc': 'length-asc', 'length-asc': 'length-desc',
|
||
'time-asc': 'time-desc', 'time-desc': 'time-asc', 'climb-desc': 'climb-asc', 'climb-asc': 'climb-desc' };
|
||
/* The Saved page's list also has the order saved (rows numbered in data-order by section 1c). */
|
||
if (savedPage) SORT_KEYS.saved = 1;
|
||
/* A ranking (/numbers/) has its rank as an order (rows numbered in data-rank by the page). */
|
||
if (document.querySelector('.entry[data-rank]')) SORT_KEYS.rank = 1;
|
||
|
||
function normalizeSortKey(value, dflt) {
|
||
if (SORT_KEYS[value]) return value;
|
||
if (SORT_OLD[value]) return SORT_OLD[value];
|
||
return SORT_KEYS[dflt] ? dflt : 'name';
|
||
}
|
||
function cmpStr(a, b) { return a < b ? -1 : a > b ? 1 : 0; }
|
||
function cmpNum(a, b) { return a < b ? -1 : a > b ? 1 : 0; }
|
||
|
||
function compareEntries(a, b, key) {
|
||
key = normalizeSortKey(key, 'name');
|
||
var an = a.dataset.name || '', bn = b.dataset.name || '';
|
||
if (key === 'saved') {
|
||
var ao = a.dataset.order ? Number(a.dataset.order) : Infinity;
|
||
var bo = b.dataset.order ? Number(b.dataset.order) : Infinity;
|
||
return cmpNum(ao, bo) || cmpStr(an, bn);
|
||
}
|
||
if (key === 'rank') return cmpNum(Number(a.dataset.rank || Infinity), Number(b.dataset.rank || Infinity));
|
||
if (key === 'name') return cmpStr(an, bn);
|
||
if (key === 'name-desc') return cmpStr(bn, an);
|
||
if (key === 'climb-desc' || key === 'climb-asc') {
|
||
var ac = a.dataset.climb === '' || a.dataset.climb == null ? null : Number(a.dataset.climb);
|
||
var bc = b.dataset.climb === '' || b.dataset.climb == null ? null : Number(b.dataset.climb);
|
||
if (ac === null || bc === null) {
|
||
if (ac === null && bc === null) return cmpStr(an, bn);
|
||
return ac === null ? 1 : -1;
|
||
}
|
||
return (key === 'climb-desc' ? cmpNum(bc, ac) : cmpNum(ac, bc)) || cmpStr(an, bn);
|
||
}
|
||
var av = Number(key.indexOf('length') === 0 ? a.dataset.km : a.dataset.days);
|
||
var bv = Number(key.indexOf('length') === 0 ? b.dataset.km : b.dataset.days);
|
||
return (key.slice(-5) === '-desc' ? cmpNum(bv, av) : cmpNum(av, bv)) || cmpStr(an, bn);
|
||
}
|
||
|
||
function sortEntriesIn(root, key) {
|
||
root.querySelectorAll('ol.entries').forEach(function (list) {
|
||
var rows = Array.prototype.slice.call(list.querySelectorAll('.entry'));
|
||
var sorted = rows.slice().sort(function (a, b) { return compareEntries(a, b, key); });
|
||
/* The page is written in its default order, so on load the rows are
|
||
already where they belong: moving 200 of them to the same place costs
|
||
a full restyle for nothing. */
|
||
if (sorted.every(function (li, i) { return li === rows[i]; })) return;
|
||
sorted.forEach(function (li) { list.appendChild(li); });
|
||
});
|
||
}
|
||
|
||
function defaultSortOf(root) {
|
||
return normalizeSortKey(root && root.dataset ? root.dataset.defaultSort : '', 'name');
|
||
}
|
||
|
||
function syncSortHeads(root, key) {
|
||
root.querySelectorAll('[data-sort-col]').forEach(function (btn) {
|
||
var active = SORT_COLUMN[key] === btn.dataset.sortCol;
|
||
btn.classList.toggle('is-active', active);
|
||
var arrow = btn.querySelector('[data-arrow]');
|
||
if (arrow) arrow.textContent = active ? SORT_ARROW[key] : '↕';
|
||
});
|
||
}
|
||
|
||
function announceSort(root, key) {
|
||
if (!root) return;
|
||
var status = root.querySelector('[data-sort-status]');
|
||
var select = root.querySelector('[data-sort-select]');
|
||
if (!status || !select) return;
|
||
var label = key;
|
||
Array.prototype.forEach.call(select.options, function (o) { if (o.value === key) label = o.textContent; });
|
||
var text = 'Sorted by ' + label + '.';
|
||
if (status.textContent !== text) status.textContent = text;
|
||
}
|
||
|
||
function applySortUI(root, key, announce) {
|
||
key = normalizeSortKey(key, defaultSortOf(root));
|
||
sortEntriesIn(root, key);
|
||
var select = root.querySelector('[data-sort-select]');
|
||
if (select && select.value !== key) select.value = key;
|
||
syncSortHeads(root, key);
|
||
if (announce) announceSort(root, key);
|
||
return key;
|
||
}
|
||
|
||
function nextSortForColumn(col, current) {
|
||
current = normalizeSortKey(current, 'name');
|
||
return SORT_COLUMN[current] === col ? COLUMN_TOGGLE[current] : COLUMN_PRIMARY[col];
|
||
}
|
||
|
||
function wireSortHeads(root, getKey, setKey) {
|
||
root.querySelectorAll('[data-sort-col]').forEach(function (btn) {
|
||
btn.addEventListener('click', function () { setKey(nextSortForColumn(btn.dataset.sortCol, getKey())); });
|
||
});
|
||
}
|
||
|
||
function sortFromUrl(root) {
|
||
return normalizeSortKey(new URLSearchParams(window.location.search).get('sort'), defaultSortOf(root));
|
||
}
|
||
|
||
function replaceSortInUrl(root, key) {
|
||
var p = new URLSearchParams(window.location.search);
|
||
if (key === defaultSortOf(root)) p.delete('sort'); else p.set('sort', key);
|
||
var q = p.toString().replace(/%2C/g, ','); /* a Saved list's t=a,b,c stays readable */
|
||
window.history.replaceState(null, '', (q ? '?' + q : window.location.pathname) + window.location.hash);
|
||
}
|
||
|
||
function initSortableLists() {
|
||
document.querySelectorAll('.results[data-sortable]').forEach(function (root) {
|
||
if (root.hasAttribute('data-finder')) return; /* the filter bar wires its own list */
|
||
var select = root.querySelector('[data-sort-select]');
|
||
var current = defaultSortOf(root);
|
||
function setSort(key, updateUrl, announce) {
|
||
current = applySortUI(root, key, announce);
|
||
if (updateUrl) replaceSortInUrl(root, current);
|
||
}
|
||
if (select) select.addEventListener('change', function () { setSort(select.value, true, true); });
|
||
wireSortHeads(root, function () { return current; }, function (k) { setSort(k, true, true); });
|
||
setSort(sortFromUrl(root), false, false);
|
||
window.addEventListener('popstate', function () { setSort(sortFromUrl(root), false, false); });
|
||
/* Back or a reload may put the select's old value back after this ran. */
|
||
window.addEventListener('pageshow', function () { setSort(sortFromUrl(root), false, false); });
|
||
});
|
||
}
|
||
|
||
/* 3. The filter bar --------------------------------------------------------
|
||
One bar and one set of panels, on the home page and on every other page
|
||
that shows a set of trails large enough to narrow (olt/home.py listing()).
|
||
The filters are the attributes every page names: Where, Length, Time,
|
||
Season, Terrain, Kind, Walking, Shape and GPX. Each row carries its values
|
||
as data-* attributes (olt/home.py entry()), and everything here is worked
|
||
out from the rows on the page: which rows show, the number beside each
|
||
choice, and which choices and filters are offered at all. A filter on a
|
||
facet that a trail has no value for leaves that trail out, and the note
|
||
under the filter (data-missing) says how many trails have no value on
|
||
file. The state is in the address (?where=…&time=…), so a narrowed page
|
||
can be shared, reloaded and gone back to; nothing is kept between pages. */
|
||
|
||
var MULTI = ['time', 'terrain', 'walking', 'shape', 'gpx'];
|
||
var MORE = ['walking', 'shape', 'gpx'];
|
||
/* Addresses from before the site had these filters still work. */
|
||
var OLD_POLICY = { open: 'free', sold: 'sold', withheld: 'unpublished', unclear: 'elsewhere' };
|
||
|
||
/* facet-counts:begin — pure, with no page: web/tests/test_filter_counts.py runs this under node.
|
||
A row: {search, where: [], km, time, months: [], terrain: [], kind, walking, shape, gpx}.
|
||
A state: {words: [], where, min, max (km; max may be Infinity), month, kind, and a list for
|
||
each of time, terrain, walking, shape, gpx}. */
|
||
var FacetCount = (function () {
|
||
var FACETS = ['where', 'length', 'time', 'month', 'terrain', 'kind', 'walking', 'shape', 'gpx'];
|
||
function has(list, v) { return list.indexOf(v) !== -1; }
|
||
|
||
/* Does the row pass the search and every filter but `skip`? Within Time, Terrain, Walking,
|
||
Shape and GPX the choices are alternatives: a row with any one of them passes. */
|
||
function passes(d, s, skip) {
|
||
if (s.words.some(function (w) { return d.search.indexOf(w) === -1; })) return false;
|
||
if (skip !== 'where' && s.where && !has(d.where, s.where)) return false;
|
||
if (skip !== 'length' && (d.km < s.min || d.km > s.max)) return false;
|
||
if (skip !== 'time' && s.time.length && !has(s.time, d.time)) return false;
|
||
if (skip !== 'month' && s.month && !has(d.months, s.month)) return false;
|
||
if (skip !== 'terrain' && s.terrain.length && !d.terrain.some(function (t) { return has(s.terrain, t); })) return false;
|
||
if (skip !== 'kind' && s.kind && d.kind !== s.kind) return false;
|
||
if (skip !== 'walking' && s.walking.length && !has(s.walking, d.walking)) return false;
|
||
if (skip !== 'shape' && s.shape.length && !has(s.shape, d.shape)) return false;
|
||
if (skip !== 'gpx' && s.gpx.length && !has(s.gpx, d.gpx)) return false;
|
||
return true;
|
||
}
|
||
|
||
function valuesOf(d, f) {
|
||
if (f === 'where') return d.where;
|
||
if (f === 'month') return d.months;
|
||
if (f === 'terrain') return d.terrain;
|
||
return d[f] ? [d[f]] : [];
|
||
}
|
||
|
||
/* The numbers for every choice. Each facet's choices are counted against the search and all
|
||
the other facets as they are set, never against the facet's own choice: the number beside a
|
||
choice is what choosing it (instead, in Where, Season and Kind; on its own, in a list of
|
||
boxes) shows. base[f]: the rows the other facets leave, which is what the facet's "any"
|
||
shows. steps: [[value, km]] of the length lists; min[value] counts the rows from that length
|
||
up to the chosen longest, max[value] those up to that length from the chosen shortest. */
|
||
function count(rows, s, steps) {
|
||
var out = { total: 0, base: {}, min: {}, max: {} };
|
||
FACETS.forEach(function (f) { out.base[f] = 0; if (f !== 'length') out[f] = {}; });
|
||
(steps || []).forEach(function (st) { out.min[st[0]] = 0; out.max[st[0]] = 0; });
|
||
rows.forEach(function (d) {
|
||
if (passes(d, s, '')) out.total += 1;
|
||
FACETS.forEach(function (f) {
|
||
if (!passes(d, s, f)) return;
|
||
out.base[f] += 1;
|
||
if (f === 'length') {
|
||
(steps || []).forEach(function (st) {
|
||
if (d.km >= st[1] && d.km <= s.max) out.min[st[0]] += 1;
|
||
if (d.km <= st[1] && d.km >= s.min) out.max[st[0]] += 1;
|
||
});
|
||
return;
|
||
}
|
||
valuesOf(d, f).forEach(function (v) { out[f][v] = (out[f][v] || 0) + 1; });
|
||
});
|
||
});
|
||
return out;
|
||
}
|
||
|
||
/* The words after the figure of the count above a list: "trails" when every one is shown,
|
||
"of 35 trails" when some are left out. */
|
||
function countWords(shown, total, noun) {
|
||
return (shown === total ? '' : 'of ' + total + ' ') + noun;
|
||
}
|
||
|
||
return { passes: passes, count: count, countWords: countWords };
|
||
})();
|
||
/* facet-counts:end */
|
||
|
||
function initFinder(form) {
|
||
var resultsRoot = document.querySelector('.results[data-finder]');
|
||
if (!resultsRoot) return;
|
||
function split(v) { return (v || '').split(' ').filter(Boolean); }
|
||
var rows = Array.prototype.map.call(resultsRoot.querySelectorAll('.entry[data-id]'), function (li) {
|
||
var d = li.dataset;
|
||
return { li: li, id: d.id, search: d.search || '', where: split(d.where), km: Number(d.km), time: d.time || '',
|
||
months: split(d.months), terrain: split(d.terrain), kind: d.kind || '', walking: d.walking || '',
|
||
shape: d.shape || '', gpx: d.gpx || '' };
|
||
});
|
||
var lists = Array.prototype.slice.call(resultsRoot.querySelectorAll('ol.entries'));
|
||
var count = resultsRoot.querySelector('.results-count');
|
||
var noun = (count && count.dataset.noun) || 'trails';
|
||
var empty = resultsRoot.querySelector('.empty-state');
|
||
var active = form.querySelector('.filters__active');
|
||
var lenHint = document.getElementById('len-hint');
|
||
var listHead = resultsRoot.querySelector('.list-head');
|
||
var el = form.elements;
|
||
var sort = resultsRoot.querySelector('[data-sort-select]');
|
||
var defaultSort = defaultSortOf(resultsRoot);
|
||
var more = form.querySelector('.filters__more');
|
||
var none = form.querySelector('.filters__none');
|
||
var fields = Array.prototype.slice.call(form.querySelectorAll('[data-facet]'));
|
||
var total = rows.length;
|
||
/* The bar: the Filters control opens the panels, and Clear shows only when something is set. */
|
||
var toggle = form.querySelector('.filters__toggle');
|
||
var panels = form.querySelector('.filters__panels');
|
||
var clear = form.querySelector('.filters__clear');
|
||
function showPanels(open) {
|
||
if (!panels) return;
|
||
if (open) panels.setAttribute('data-open', ''); else panels.removeAttribute('data-open');
|
||
if (toggle) toggle.setAttribute('aria-expanded', open ? 'true' : 'false');
|
||
}
|
||
/* The open panels sit in the page between the bar and the rows, which move down: nothing is
|
||
laid over a row. Show the trails (or Escape) closes them and brings the bar, and the first
|
||
row under it, back into view. */
|
||
function closePanels() {
|
||
showPanels(false);
|
||
if (toggle) toggle.focus({ preventScroll: true });
|
||
if (form.getBoundingClientRect().top < 0) form.scrollIntoView({ block: 'start' });
|
||
}
|
||
if (toggle) toggle.addEventListener('click', function () { showPanels(!panels.hasAttribute('data-open')); });
|
||
var done = form.querySelector('.filters__done');
|
||
if (done) done.addEventListener('click', closePanels);
|
||
form.addEventListener('keydown', function (e) {
|
||
if (e.key === 'Escape' && panels && panels.hasAttribute('data-open')) closePanels();
|
||
});
|
||
|
||
/* The count is the one live region. Keep it quiet through the first
|
||
application below so nothing is announced on load, and write to it
|
||
only when its text changes. The length hint is a description of the
|
||
length fields, not a live region of its own. */
|
||
if (count) count.setAttribute('aria-live', 'off');
|
||
if (lenHint) {
|
||
lenHint.removeAttribute('aria-live');
|
||
lenHint.removeAttribute('role');
|
||
[el.min, el.max].forEach(function (field) {
|
||
if (!field) return;
|
||
var described = (field.getAttribute('aria-describedby') || '').split(/\s+/).filter(Boolean);
|
||
if (described.indexOf('len-hint') === -1) described.push('len-hint');
|
||
field.setAttribute('aria-describedby', described.join(' '));
|
||
});
|
||
}
|
||
|
||
function setText(node, text) {
|
||
if (node && node.textContent !== text) node.textContent = text;
|
||
}
|
||
|
||
function hasOption(select, value) {
|
||
return !!select && Array.prototype.some.call(select.options, function (o) { return o.value === value; });
|
||
}
|
||
|
||
function boxes(name) {
|
||
return Array.prototype.slice.call(form.querySelectorAll('input[type="checkbox"][name="' + name + '"]'));
|
||
}
|
||
function checked(name) {
|
||
return boxes(name).filter(function (b) { return b.checked; }).map(function (b) { return b.value; });
|
||
}
|
||
function radio(name) {
|
||
var r = form.querySelector('input[name="' + name + '"]:checked');
|
||
return r ? r.value : '';
|
||
}
|
||
function value(field) { return field ? field.value : ''; }
|
||
|
||
/* The choices of Where and Season as the page wrote them, before any is taken out for
|
||
showing nothing: [{value, label, counted, group, def}]. */
|
||
function masterOf(select) {
|
||
if (!select) return [];
|
||
return Array.prototype.map.call(select.options, function (o) {
|
||
var group = o.parentNode && o.parentNode.tagName === 'OPTGROUP' ? o.parentNode.label : '';
|
||
return { value: o.value, label: o.dataset.label || o.textContent, counted: !!o.dataset.label, group: group,
|
||
def: o.value === '' };
|
||
});
|
||
}
|
||
var masters = { where: masterOf(el.where), month: masterOf(el.month), min: [], max: [] };
|
||
|
||
function read() {
|
||
var min = stepKm(value(el.min)) || 0;
|
||
var max = stepKm(value(el.max)) || Infinity;
|
||
var minRaw = value(el.min), maxRaw = value(el.max);
|
||
var swapped = min > max;
|
||
if (swapped) { var keep = min; min = max; max = keep; keep = minRaw; minRaw = maxRaw; maxRaw = keep; }
|
||
var q = value(el.q).trim();
|
||
var s = {
|
||
q: q,
|
||
words: q.toLowerCase().split(/\s+/).filter(Boolean),
|
||
where: value(el.where),
|
||
min: min,
|
||
max: max,
|
||
minRaw: minRaw,
|
||
maxRaw: maxRaw,
|
||
swapped: swapped,
|
||
month: value(el.month),
|
||
kind: radio('kind')
|
||
};
|
||
MULTI.forEach(function (name) { s[name] = checked(name); });
|
||
return s;
|
||
}
|
||
|
||
/* A length step's value is "100mi" or "160km": round in its own unit, so the
|
||
list offered follows the reader's units. Filtering compares kilometres. */
|
||
function stepKm(v) {
|
||
var m = /^(\d+(?:\.\d+)?)(mi|km)$/.exec(v || '');
|
||
return m ? Number(m[1]) * (m[2] === 'mi' ? KM_PER_MI : 1) : 0;
|
||
}
|
||
|
||
function stepLabel(n, unit) {
|
||
var km = unit === 'mi' ? n * KM_PER_MI : n;
|
||
var other = unit === 'mi' ? fmt.format(Math.round(km)) + ' km' : fmt.format(Math.round(km / KM_PER_MI)) + ' mi';
|
||
return fmt.format(n) + ' ' + unit + ' · ' + other;
|
||
}
|
||
|
||
/* The value of the step in a list nearest to a length, or '' for none. */
|
||
function nearestStep(select, km) {
|
||
var best = '', gap = Infinity;
|
||
if (!(km > 0)) return best;
|
||
(masters[select.name] || []).forEach(function (o) {
|
||
var k = stepKm(o.value);
|
||
if (!k) return;
|
||
var d = Math.abs(Math.log(k / km));
|
||
if (d < gap) { gap = d; best = o.value; }
|
||
});
|
||
return best;
|
||
}
|
||
|
||
/* Write a select's choices from its master list: each with its number, and none that would
|
||
show nothing unless it is the one chosen (so it can be unchosen). The options themselves
|
||
are taken out, not hidden: a hidden option is not hidden in every browser. */
|
||
function renderSelect(select, n) {
|
||
if (!select) return;
|
||
var chosen = select.value;
|
||
var parts = [];
|
||
masters[select.name].forEach(function (o) {
|
||
var k = o.counted ? (n(o.value) || 0) : null;
|
||
if (o.value && k === 0 && o.value !== chosen) return;
|
||
parts.push({ o: o, k: k });
|
||
});
|
||
var sig = parts.map(function (p) { return p.o.value + ':' + p.k; }).join('|');
|
||
if (select.getAttribute('data-sig') === sig) return;
|
||
select.setAttribute('data-sig', sig);
|
||
select.textContent = '';
|
||
var groupEl = null;
|
||
parts.forEach(function (p) {
|
||
var opt = document.createElement('option');
|
||
opt.value = p.o.value;
|
||
opt.textContent = p.k === null ? p.o.label : p.o.label + ' (' + fmt.format(p.k) + ')';
|
||
/* A form's reset picks the option marked as default, or else the first one. */
|
||
if (p.o.def) opt.defaultSelected = true;
|
||
var parent = select;
|
||
if (p.o.group) {
|
||
if (!groupEl || groupEl.label !== p.o.group) {
|
||
groupEl = document.createElement('optgroup');
|
||
groupEl.label = p.o.group;
|
||
select.appendChild(groupEl);
|
||
}
|
||
parent = groupEl;
|
||
} else groupEl = null;
|
||
parent.appendChild(opt);
|
||
});
|
||
select.value = chosen;
|
||
if (select.value !== chosen) select.value = '';
|
||
}
|
||
|
||
/* Offer round steps in the reader's units. The server writes the mile steps;
|
||
a reader who chose kilometres gets the kilometre steps (data-steps-km), and
|
||
a chosen step moves to the nearest one in the new list. */
|
||
function buildLengthOptions() {
|
||
var unit = milesFirst() ? 'mi' : 'km';
|
||
form.querySelectorAll('select[data-length-options]').forEach(function (select) {
|
||
var kind = select.dataset.lengthOptions;
|
||
var steps = (select.getAttribute('data-steps-' + unit) || '').split(',').filter(Boolean).map(Number);
|
||
var wasKm = stepKm(select.value);
|
||
var master = [];
|
||
if (kind === 'min') master.push({ value: '', label: 'Any', counted: false, group: '', def: true });
|
||
steps.slice(kind === 'min' ? 0 : 1, kind === 'min' ? -1 : undefined).forEach(function (n) {
|
||
master.push({ value: n + unit, label: stepLabel(n, unit), counted: true, group: '', def: false });
|
||
});
|
||
if (kind === 'max') master.push({ value: '', label: 'Any', counted: false, group: '', def: true });
|
||
masters[kind] = master;
|
||
/* Every step is written once, so the nearest can be found; counting then takes out the empty ones. */
|
||
select.removeAttribute('data-sig');
|
||
renderSelect(select, function () { return 1; });
|
||
select.value = wasKm ? nearestStep(select, wasKm) : '';
|
||
});
|
||
}
|
||
|
||
function lengthSteps() {
|
||
/* Shortest and Longest offer the same steps: each is listed once, or it is counted twice. */
|
||
var out = [], seen = {};
|
||
['min', 'max'].forEach(function (k) {
|
||
masters[k].forEach(function (o) {
|
||
if (o.value && !seen[o.value]) { seen[o.value] = true; out.push([o.value, stepKm(o.value)]); }
|
||
});
|
||
});
|
||
return out;
|
||
}
|
||
|
||
function describeLength(s) {
|
||
var text;
|
||
if (!s.min && s.max === Infinity) text = 'Any length.';
|
||
else if (!s.min) text = 'Up to ' + lengthText(Math.round(s.max)) + '.';
|
||
else if (s.max === Infinity) text = 'From ' + lengthText(Math.round(s.min)) + ' up.';
|
||
else text = 'From ' + lengthText(Math.round(s.min)) + ' to ' + lengthText(Math.round(s.max)) + '.';
|
||
if (s.swapped) text += ' Shortest and longest were swapped.';
|
||
return text;
|
||
}
|
||
|
||
function relabelLengths() {
|
||
buildLengthOptions();
|
||
apply(true);
|
||
}
|
||
|
||
/* URL query <-> form. Only values that differ from the defaults are written. */
|
||
function toQuery(s) {
|
||
var p = new URLSearchParams();
|
||
if (s.q) p.set('q', s.q);
|
||
if (s.where) p.set('where', s.where);
|
||
if (s.min) p.set('min', s.minRaw);
|
||
if (s.max !== Infinity) p.set('max', s.maxRaw);
|
||
if (s.month) p.set('month', s.month);
|
||
if (s.kind) p.set('kind', s.kind);
|
||
MULTI.forEach(function (name) { if (s[name].length) p.set(name, s[name].join(',')); });
|
||
if (sort && sort.value !== defaultSort) p.set('sort', sort.value);
|
||
return p.toString();
|
||
}
|
||
|
||
/* Before reading the address, every choice the page has is offered, so that one the address
|
||
names is found even when the last counting took it out. */
|
||
function offerAll() {
|
||
['where', 'month', 'min', 'max'].forEach(function (name) {
|
||
if (!el[name] || !el[name].tagName) return;
|
||
el[name].removeAttribute('data-sig');
|
||
renderSelect(el[name], function () { return 1; });
|
||
});
|
||
}
|
||
|
||
function fromQuery() {
|
||
var p = new URLSearchParams(window.location.search);
|
||
offerAll();
|
||
/* Left alone when it already says the same, so a space being typed stays. */
|
||
if (el.q.value.trim() !== (p.get('q') || '').trim()) el.q.value = p.get('q') || '';
|
||
if (el.where) {
|
||
var where = p.get('where') || p.get('region') || '';
|
||
if (!where && (p.get('country') === 'US' || p.get('country') === 'CA')) where = p.get('country');
|
||
el.where.value = hasOption(el.where, where) ? where : '';
|
||
}
|
||
/* A length in the address may be a step from the other unit's list (a link
|
||
from a reader who uses kilometres, opened by one who uses miles), or a
|
||
step this list no longer has: take the nearest step offered. */
|
||
if (el.min && el.max) {
|
||
['min', 'max'].forEach(function (name) {
|
||
var v = p.get(name) || '';
|
||
el[name].value = hasOption(el[name], v) ? v : nearestStep(el[name], stepKm(v));
|
||
});
|
||
/* An address that gives them the wrong way round is rewritten by apply()
|
||
with the shorter one first: the controls say the same. */
|
||
if (stepKm(el.min.value) > stepKm(el.max.value) && stepKm(el.max.value)) {
|
||
var low = el.max.value, high = el.min.value;
|
||
el.min.value = hasOption(el.min, low) ? low : nearestStep(el.min, stepKm(low));
|
||
el.max.value = hasOption(el.max, high) ? high : nearestStep(el.max, stepKm(high));
|
||
}
|
||
}
|
||
if (el.month) {
|
||
var month = p.get('month') || '';
|
||
el.month.value = hasOption(el.month, month) ? month : '';
|
||
}
|
||
var kind = p.get('kind') || '';
|
||
if (kind === 'designated') kind = 'trail';
|
||
syncRadios('#finder-form input[name="kind"]', kind === 'trail' || kind === 'route' ? kind : '');
|
||
var chosen = {};
|
||
MULTI.forEach(function (name) { chosen[name] = (p.get(name) || '').split(',').filter(Boolean); });
|
||
if (p.get('line') === '1' && !chosen.gpx.length) chosen.gpx = ['here', 'partial'];
|
||
if (p.get('policy') && OLD_POLICY[p.get('policy')] && !chosen.gpx.length) chosen.gpx = [OLD_POLICY[p.get('policy')]];
|
||
MULTI.forEach(function (name) {
|
||
boxes(name).forEach(function (b) { b.checked = chosen[name].indexOf(b.value) !== -1; });
|
||
});
|
||
if (sort) sort.value = normalizeSortKey(p.get('sort'), defaultSort);
|
||
}
|
||
|
||
function applySort() {
|
||
if (sort) applySortUI(resultsRoot, sort.value, false);
|
||
}
|
||
|
||
/* Is something chosen in the facet? */
|
||
function isSet(f, s) {
|
||
if (f === 'length') return !!s.min || s.max !== Infinity;
|
||
if (MULTI.indexOf(f) !== -1) return s[f].length > 0;
|
||
return !!s[f];
|
||
}
|
||
|
||
/* [[kind, value]] of every choice a field offers: kind is the facet, or min / max for length. */
|
||
function choicesOf(f) {
|
||
var out = [];
|
||
function from(list, k) { list.forEach(function (o) { if (o.value) out.push([k, o.value]); }); }
|
||
if (f === 'length') { from(masters.min, 'min'); from(masters.max, 'max'); }
|
||
else if (f === 'where' || f === 'month') from(masters[f], f);
|
||
else if (f === 'kind') { out.push([f, 'trail']); out.push([f, 'route']); }
|
||
else boxes(f).forEach(function (b) { out.push([f, b.value]); });
|
||
return out;
|
||
}
|
||
|
||
/* The numbers beside the choices, the choices that would show nothing taken out, and a
|
||
filter that can no longer narrow what the others leave put away with its heading. */
|
||
function relabel(s, c) {
|
||
renderSelect(el.where, function (v) { return v ? c.where[v] : c.base.where; });
|
||
renderSelect(el.month, function (v) { return c.month[v]; });
|
||
if (el.min && el.min.tagName) renderSelect(el.min, function (v) { return c.min[v]; });
|
||
if (el.max && el.max.tagName) renderSelect(el.max, function (v) { return c.max[v]; });
|
||
MULTI.forEach(function (name) {
|
||
boxes(name).forEach(function (b) {
|
||
var n = c[name][b.value] || 0;
|
||
var label = b.closest('label');
|
||
setText(label.querySelector('.check__count'), fmt.format(n));
|
||
label.hidden = n === 0 && !b.checked;
|
||
});
|
||
});
|
||
form.querySelectorAll('input[name="kind"]').forEach(function (b) {
|
||
var span = b.nextElementSibling;
|
||
if (!b.value || !span || !span.dataset.label) return;
|
||
var n = c.kind[b.value] || 0;
|
||
setText(span, span.dataset.label + ' (' + fmt.format(n) + ')');
|
||
b.closest('label').hidden = n === 0 && !b.checked;
|
||
});
|
||
var shown = 0;
|
||
fields.forEach(function (field) {
|
||
var f = field.dataset.facet;
|
||
var base = c.base[f];
|
||
/* Only the choices the field offers count: a row's other places (a trail that crosses
|
||
out of the page's part of the country) are not choices here. */
|
||
var counts = f === 'length' ? null : c[f];
|
||
var narrows = choicesOf(f).some(function (pair) {
|
||
var n = pair[0] === 'min' ? c.min[pair[1]] : pair[0] === 'max' ? c.max[pair[1]] : counts[pair[1]];
|
||
return n > 0 && n < base;
|
||
});
|
||
field.hidden = !narrows && !isSet(f, s);
|
||
if (!field.hidden) shown += 1;
|
||
});
|
||
if (more) more.hidden = !Array.prototype.some.call(more.querySelectorAll('[data-facet]'), function (f) { return !f.hidden; });
|
||
if (none) none.hidden = shown > 0;
|
||
}
|
||
|
||
function apply(updateUrl) {
|
||
var s = read();
|
||
var c = FacetCount.count(rows, s, lengthSteps());
|
||
var shown = [];
|
||
rows.forEach(function (d) {
|
||
var ok = FacetCount.passes(d, s, '');
|
||
d.li.hidden = !ok;
|
||
if (ok) shown.push(d.id);
|
||
});
|
||
/* A heading over rows (Routes, by author) goes when none of its rows is left. */
|
||
lists.forEach(function (ol) {
|
||
var head = ol.previousElementSibling;
|
||
var any = !!ol.querySelector('.entry:not([hidden])');
|
||
ol.hidden = !any;
|
||
if (head && head.classList.contains('results__group')) head.hidden = !any;
|
||
});
|
||
var words = FacetCount.countWords(shown.length, total, noun);
|
||
var text = shown.length + ' ' + words;
|
||
if (count && count.textContent !== text) {
|
||
count.innerHTML = '';
|
||
var strong = document.createElement('strong');
|
||
strong.textContent = String(shown.length);
|
||
count.appendChild(strong);
|
||
count.appendChild(document.createTextNode(' ' + words));
|
||
}
|
||
if (empty) empty.hidden = shown.length > 0;
|
||
if (listHead) listHead.hidden = shown.length === 0;
|
||
relabel(s, c);
|
||
setText(lenHint, describeLength(s));
|
||
|
||
/* Say how many trails a filter leaves out for having no value on file. */
|
||
var on = { season: !!s.month, terrain: s.terrain.length > 0, walking: s.walking.length > 0, shape: s.shape.length > 0 };
|
||
form.querySelectorAll('[data-missing]').forEach(function (note) { note.hidden = !on[note.dataset.missing]; });
|
||
|
||
var n = MORE.reduce(function (sum, name) { return sum + (s[name].length ? 1 : 0); }, 0);
|
||
if (active) active.textContent = n ? n + ' on' : '';
|
||
var all = (s.where ? 1 : 0) + (s.min ? 1 : 0) + (s.max !== Infinity ? 1 : 0) + (s.month ? 1 : 0) + (s.kind ? 1 : 0) +
|
||
(s.time.length ? 1 : 0) + (s.terrain.length ? 1 : 0) + n;
|
||
setText(form.querySelector('.filters__count'), all ? String(all) : '');
|
||
/* Clear is offered only when something is set: a search, or any filter. */
|
||
if (clear) clear.hidden = !(s.q || all);
|
||
|
||
maps.forEach(function (m) { if (m.kind === 'overview') setVisible(m, shown); });
|
||
if (updateUrl) {
|
||
var q = toQuery(s);
|
||
window.history.replaceState(null, '', (q ? '?' + q : window.location.pathname) + window.location.hash);
|
||
}
|
||
}
|
||
|
||
form.addEventListener('input', function () { apply(true); });
|
||
form.addEventListener('change', function () { apply(true); });
|
||
form.addEventListener('submit', function (e) { e.preventDefault(); });
|
||
form.addEventListener('reset', function () { setTimeout(function () { apply(true); }, 0); });
|
||
resultsRoot.querySelectorAll('[data-reset]').forEach(function (b) {
|
||
b.addEventListener('click', function () { form.reset(); el.q.focus(); });
|
||
});
|
||
if (sort) sort.addEventListener('change', function () { applySort(); apply(true); announceSort(resultsRoot, sort.value); });
|
||
if (sort) wireSortHeads(resultsRoot, function () { return sort.value; }, function (k) {
|
||
sort.value = k; applySort(); apply(true); announceSort(resultsRoot, sort.value);
|
||
});
|
||
document.addEventListener('olt:units', relabelLengths);
|
||
|
||
rows.forEach(function (d) {
|
||
var li = d.li;
|
||
li.addEventListener('mouseenter', function () { highlight(li.dataset.id); });
|
||
li.addEventListener('mouseleave', function () { highlight(''); });
|
||
li.addEventListener('focusin', function () { highlight(li.dataset.id); });
|
||
li.addEventListener('focusout', function () { highlight(''); });
|
||
});
|
||
|
||
/* The address is rewritten too, so that it says what the controls show: a
|
||
length given in the other unit becomes the step chosen for it. */
|
||
buildLengthOptions();
|
||
fromQuery();
|
||
applySort();
|
||
apply(true);
|
||
/* The count can announce from here on; the first application is done. */
|
||
setTimeout(function () { if (count) count.removeAttribute('aria-live'); }, 0);
|
||
/* The filters behind "More" stay folded, unless the address already sets one. */
|
||
var s0 = read();
|
||
if (more && MORE.some(function (name) { return s0[name].length; })) more.open = true;
|
||
/* The panels start closed on every page, whatever the address sets (the Filters control
|
||
says how many are on). An address ending #filters opens them: a link can offer them. */
|
||
if (window.location.hash === '#filters') showPanels(true);
|
||
window.addEventListener('popstate', function () { fromQuery(); applySort(); apply(false); });
|
||
/* A page reached by Back, Forward or a reload: the browser may put the old
|
||
values of the controls back after this script has run, or show an old copy
|
||
of the whole page. The address is the record of the filters, so set the
|
||
controls from it again and filter again. */
|
||
window.addEventListener('pageshow', function (e) {
|
||
var nav = window.performance && performance.getEntriesByType ? performance.getEntriesByType('navigation')[0] : null;
|
||
if (!e.persisted && !(nav && (nav.type === 'back_forward' || nav.type === 'reload'))) return;
|
||
buildLengthOptions();
|
||
fromQuery();
|
||
applySort();
|
||
apply(true);
|
||
});
|
||
}
|
||
|
||
var finder = document.getElementById('finder-form');
|
||
if (finder) initFinder(finder);
|
||
initSortableLists();
|
||
|
||
/* 3b. A trail page's At a glance panel on a narrow screen: the button opens the rest of the facts. */
|
||
document.querySelectorAll('[data-glance-toggle]').forEach(function (button) {
|
||
var panel = button.closest('.glance');
|
||
button.addEventListener('click', function () {
|
||
var all = !panel.hasAttribute('data-all');
|
||
if (all) panel.setAttribute('data-all', ''); else panel.removeAttribute('data-all');
|
||
button.setAttribute('aria-expanded', all ? 'true' : 'false');
|
||
button.textContent = all ? 'Fewer facts' : 'All the facts';
|
||
});
|
||
});
|
||
|
||
/* 4. A link to a part of the page inside a closed <details> (About this data)
|
||
opens it, so the target is shown rather than hidden. */
|
||
function revealHash() {
|
||
var id = decodeURIComponent(window.location.hash.slice(1));
|
||
var target = id && document.getElementById(id);
|
||
var opened = false;
|
||
for (var el = target; el; el = el.parentElement) {
|
||
if (el.tagName === 'DETAILS' && !el.open) { el.open = true; opened = true; }
|
||
}
|
||
if (opened) target.scrollIntoView();
|
||
}
|
||
window.addEventListener('hashchange', revealHash);
|
||
if (window.location.hash) revealHash();
|
||
|
||
/* 5. Copy a required credit ------------------------------------------------ */
|
||
|
||
document.querySelectorAll('[data-copy]').forEach(function (button) {
|
||
var target = document.querySelector(button.dataset.copy);
|
||
var done = document.querySelector(button.dataset.copyStatus);
|
||
if (!target) return;
|
||
button.hidden = false;
|
||
button.addEventListener('click', function () {
|
||
var text = target.textContent.trim();
|
||
function ok() { if (done) done.textContent = button.dataset.copyDone || 'Copied: ' + text; }
|
||
function manual() {
|
||
var range = document.createRange();
|
||
range.selectNodeContents(target);
|
||
var sel = window.getSelection();
|
||
sel.removeAllRanges();
|
||
sel.addRange(range);
|
||
if (done) done.textContent = 'Selected. Press Ctrl+C or ⌘C to copy.';
|
||
}
|
||
if (navigator.clipboard && navigator.clipboard.writeText) navigator.clipboard.writeText(text).then(ok, manual);
|
||
else manual();
|
||
});
|
||
});
|
||
})();
|