Add an example site that calls every route, served at /examples/
A reader learns the API fastest from working calls: one page per area (circuit browser, track images, GPS traces on a map, grand prix lookup, posters, Kometa) shows the exact request URL next to its answer. Pages call their own origin when the app serves them, and the public service when opened from disk or given ?api=. Posters render only on a button press, since each one fetches map tiles on the server. A body that does not match the asked format is said in red, never shown as success. The image copies examples/ so the deployed app serves the same pages. Claude-Session: https://claude.ai/code/session_01NcbgfJAa38xFpzy69BWrqh
This commit is contained in:
@@ -0,0 +1,219 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>Circuit browser · F1 Circuits API examples</title>
|
||||
<meta name="description" content="Browse the F1 Circuits catalogue: countries, cities, circuits and layouts, with each layout's track image.">
|
||||
<link rel="icon" href="data:,">
|
||||
<link rel="stylesheet" href="site.css">
|
||||
</head>
|
||||
<body>
|
||||
<header class="site">
|
||||
<a class="brand" href="index.html">F1 <span>Circuits</span> API</a>
|
||||
<nav aria-label="Examples">
|
||||
<a href="index.html">Overview</a>
|
||||
<a href="circuits.html" aria-current="page">Circuit browser</a>
|
||||
<a href="tracks.html">Track images</a>
|
||||
<a href="map.html">GPS traces</a>
|
||||
<a href="grand-prix.html">Grand prix lookup</a>
|
||||
<a href="posters.html">Posters</a>
|
||||
<a href="kometa.html">Kometa</a>
|
||||
</nav>
|
||||
<div id="api-base"></div>
|
||||
</header>
|
||||
|
||||
<main>
|
||||
<h1>Circuit browser</h1>
|
||||
<p class="lead">
|
||||
The catalogue is a tree: a country holds cities, a city holds circuits, a circuit holds layouts. Each level has a
|
||||
route; pick an entry to make the next call. Every layout shows its track image.
|
||||
</p>
|
||||
|
||||
<section class="card" id="countries">
|
||||
<h2>1. Countries</h2>
|
||||
<p><code>/circuits</code> lists every country, and nests its whole tree inside each one.</p>
|
||||
<div class="request"></div>
|
||||
<div class="status"></div>
|
||||
<div class="choices" id="country-list"></div>
|
||||
<details><summary>The JSON answer</summary><div class="result"></div></details>
|
||||
</section>
|
||||
|
||||
<section class="card" id="cities">
|
||||
<h2>2. Cities of a country</h2>
|
||||
<p><code>/circuits/{country}</code> answers <code>{"cities": [...]}</code>, each city with its circuits.</p>
|
||||
<div class="request"></div>
|
||||
<div class="status idle">Pick a country above.</div>
|
||||
<div class="choices" id="city-list"></div>
|
||||
<details><summary>The JSON answer</summary><div class="result"></div></details>
|
||||
</section>
|
||||
|
||||
<section class="card" id="circuits">
|
||||
<h2>3. Circuits in a city</h2>
|
||||
<p><code>/circuits/{country}/{city}</code> answers <code>{"circuits": [...]}</code>.</p>
|
||||
<div class="request"></div>
|
||||
<div class="status idle">Pick a city above.</div>
|
||||
<div class="choices" id="circuit-list"></div>
|
||||
<details><summary>The JSON answer</summary><div class="result"></div></details>
|
||||
</section>
|
||||
|
||||
<section class="card" id="circuit">
|
||||
<h2>4. One circuit and its layouts</h2>
|
||||
<p>
|
||||
<code>/circuits/{country}/{city}/{circuit}</code> answers the circuit’s name, its Wikipedia and Fandom pages and
|
||||
its layouts. Each layout’s image below is its own call,
|
||||
<code>/circuits/{country}/{city}/{circuit}/layout/{layout}/svg</code> (see <a href="tracks.html">Track images</a>).
|
||||
</p>
|
||||
<div class="request"></div>
|
||||
<div class="status idle">Pick a circuit above.</div>
|
||||
<div id="circuit-head"></div>
|
||||
<div class="layouts" id="layout-list"></div>
|
||||
<details><summary>The JSON answer</summary><div class="result"></div></details>
|
||||
</section>
|
||||
|
||||
<section class="card" id="layout">
|
||||
<h2>5. One layout</h2>
|
||||
<p>
|
||||
<code>/circuits/{country}/{city}/{circuit}/layout/{layout}</code> answers one layout: its slug, its description
|
||||
and <code>imageUrl</code>, the source drawing on the F1 Fandom wiki.
|
||||
</p>
|
||||
<div class="request"></div>
|
||||
<div class="status idle" data-idle="Press “Details” on a layout above.">Press “Details” on a layout above.</div>
|
||||
<div class="result"></div>
|
||||
</section>
|
||||
</main>
|
||||
|
||||
<footer class="pager">
|
||||
<a rel="prev" href="index.html">← Overview</a>
|
||||
<a class="home" href="index.html">All examples</a>
|
||||
<a rel="next" href="tracks.html">Track images →</a>
|
||||
</footer>
|
||||
|
||||
<script src="site.js"></script>
|
||||
<script>
|
||||
(function () {
|
||||
const { h, api, path, callJSON, setRequest, layoutYears, layoutStart } = F1;
|
||||
const $ = (id) => document.getElementById(id);
|
||||
const want = {
|
||||
country: F1.params.get('country') || 'belgium',
|
||||
city: F1.params.get('city') || 'spa',
|
||||
circuit: F1.params.get('circuit') || 'circuit-de-spa-francorchamps',
|
||||
};
|
||||
const state = {};
|
||||
|
||||
function remember() {
|
||||
const url = new URL(location.href);
|
||||
for (const k of ['country', 'city', 'circuit']) {
|
||||
if (state[k]) url.searchParams.set(k, state[k]); else url.searchParams.delete(k);
|
||||
}
|
||||
history.replaceState(null, '', url.href);
|
||||
}
|
||||
|
||||
function choices(el, items, pressed, onPick) {
|
||||
el.replaceChildren(...items.map((it) => {
|
||||
const b = h('button', { type: 'button', 'aria-pressed': String(it.slug === pressed), title: `slug: ${it.slug}` },
|
||||
it.name, ' ', h('small', {}, it.note));
|
||||
b.addEventListener('click', () => {
|
||||
for (const o of el.querySelectorAll('button')) o.setAttribute('aria-pressed', String(o === b));
|
||||
onPick(it.slug);
|
||||
});
|
||||
return b;
|
||||
}));
|
||||
}
|
||||
|
||||
function clear(...ids) {
|
||||
for (const id of ids) {
|
||||
const card = $(id);
|
||||
card.querySelector('.request').replaceChildren();
|
||||
const s = card.querySelector('.status');
|
||||
s.className = 'status idle';
|
||||
s.textContent = s.dataset.idle || 'Pick an entry above.';
|
||||
card.querySelector('.result').replaceChildren();
|
||||
}
|
||||
if (ids.includes('cities')) $('city-list').replaceChildren();
|
||||
if (ids.includes('circuits')) $('circuit-list').replaceChildren();
|
||||
if (ids.includes('circuit')) { $('circuit-head').replaceChildren(); $('layout-list').replaceChildren(); }
|
||||
}
|
||||
|
||||
const byName = (a, b) => a.name.localeCompare(b.name);
|
||||
const count = (n, one, many) => `${n} ${n === 1 ? one : many}`;
|
||||
|
||||
async function loadCountries() {
|
||||
const v = await callJSON($('countries'), api('/circuits'));
|
||||
if (!v) return;
|
||||
const list = [...v.countries].sort(byName);
|
||||
choices($('country-list'), list.map((c) => ({ slug: c.slug, name: c.name, note: count(Object.keys(c.localities).length, 'city', 'cities') })),
|
||||
want.country, pickCountry);
|
||||
if (list.some((c) => c.slug === want.country)) pickCountry(want.country);
|
||||
}
|
||||
|
||||
async function pickCountry(slug) {
|
||||
state.country = slug; state.city = state.circuit = null; remember();
|
||||
clear('cities', 'circuits', 'circuit', 'layout');
|
||||
const v = await callJSON($('cities'), api(path`/circuits/${slug}`));
|
||||
if (!v) return;
|
||||
const list = [...v.cities].sort(byName);
|
||||
choices($('city-list'), list.map((c) => ({ slug: c.slug, name: c.name, note: count(Object.keys(c.circuits).length, 'circuit', 'circuits') })),
|
||||
want.city, pickCity);
|
||||
const next = list.find((c) => c.slug === want.city) || (list.length === 1 ? list[0] : null);
|
||||
if (next) { $('city-list').querySelector(`[title="slug: ${CSS.escape(next.slug)}"]`).setAttribute('aria-pressed', 'true'); pickCity(next.slug); }
|
||||
}
|
||||
|
||||
async function pickCity(slug) {
|
||||
state.city = slug; state.circuit = null; remember();
|
||||
clear('circuits', 'circuit', 'layout');
|
||||
const v = await callJSON($('circuits'), api(path`/circuits/${state.country}/${slug}`));
|
||||
if (!v) return;
|
||||
const list = [...v.circuits].sort(byName);
|
||||
choices($('circuit-list'), list.map((c) => ({ slug: c.slug, name: c.name, note: count(Object.keys(c.layouts).length, 'layout', 'layouts') })),
|
||||
want.circuit, pickCircuit);
|
||||
const next = list.find((c) => c.slug === want.circuit) || (list.length === 1 ? list[0] : null);
|
||||
if (next) { $('circuit-list').querySelector(`[title="slug: ${CSS.escape(next.slug)}"]`).setAttribute('aria-pressed', 'true'); pickCircuit(next.slug); }
|
||||
}
|
||||
|
||||
async function pickCircuit(slug) {
|
||||
state.circuit = slug; remember();
|
||||
clear('circuit', 'layout');
|
||||
const { country, city } = state;
|
||||
const v = await callJSON($('circuit'), api(path`/circuits/${country}/${city}/${slug}`));
|
||||
if (!v) return;
|
||||
$('circuit-head').replaceChildren(
|
||||
h('h3', {}, v.name),
|
||||
h('p', {}, 'Read more: ',
|
||||
v.urls.wikipedia ? h('a', { href: v.urls.wikipedia, target: '_blank', rel: 'noopener' }, 'Wikipedia') : null,
|
||||
v.urls.wikipedia && v.urls.fandom ? ' · ' : null,
|
||||
v.urls.fandom ? h('a', { href: v.urls.fandom, target: '_blank', rel: 'noopener' }, 'F1 Fandom wiki') : null));
|
||||
const slugs = Object.keys(v.layouts).sort((a, b) => layoutStart(a) - layoutStart(b));
|
||||
$('layout-list').replaceChildren(...slugs.map((l) => layoutCard(country, city, slug, v.layouts[l])));
|
||||
}
|
||||
|
||||
function layoutCard(country, city, circuit, layout) {
|
||||
const imgUrl = api(path`/circuits/${country}/${city}/${circuit}/layout/${layout.slug}/svg`);
|
||||
const req = h('div', { class: 'request compact' });
|
||||
setRequest(req, imgUrl);
|
||||
const img = h('img', { src: imgUrl, alt: `Track layout ${layoutYears(layout.slug)}`, loading: 'lazy' });
|
||||
const tile = h('div', { class: 'track-tile' }, img);
|
||||
img.addEventListener('error', () => tile.replaceChildren(F1.notice('error', 'The image did not load; open its URL to see why.')));
|
||||
const q = new URLSearchParams({ country, city, circuit, layout: layout.slug }).toString();
|
||||
const details = h('button', { type: 'button' }, 'Details');
|
||||
details.addEventListener('click', () => {
|
||||
callJSON($('layout'), api(path`/circuits/${country}/${city}/${circuit}/layout/${layout.slug}`));
|
||||
$('layout').scrollIntoView({ behavior: 'smooth', block: 'start' });
|
||||
});
|
||||
return h('div', { class: 'layout-card' },
|
||||
h('h3', {}, layoutYears(layout.slug), ' ', h('small', {}, h('code', {}, layout.slug))),
|
||||
tile,
|
||||
req,
|
||||
h('p', {}, layout.description || 'No description.'),
|
||||
h('div', { class: 'links' },
|
||||
details,
|
||||
h('a', { href: F1.linkWithKept(`tracks.html?${q}`) }, 'All formats'),
|
||||
h('a', { href: F1.linkWithKept(`map.html?${q}`) }, 'On the map'),
|
||||
layout.imageUrl ? h('a', { href: layout.imageUrl, target: '_blank', rel: 'noopener' }, 'Source drawing') : null));
|
||||
}
|
||||
|
||||
loadCountries();
|
||||
})();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user