ICAO code expansion

This commit is contained in:
James Pattinson
2025-10-23 20:31:23 +00:00
parent ef273c0c5d
commit 41c7bb352a
2 changed files with 87 additions and 35 deletions

View File

@@ -252,13 +252,40 @@
return `<span class="status ${status.toLowerCase()}">${status}</span>`;
}
// Create PPR item HTML
function createPPRItem(ppr) {
// ICAO code to airport name cache
const airportNameCache = {};
async function getAirportDisplay(code) {
if (!code || code.length !== 4 || !/^[A-Z]{4}$/.test(code)) return code;
if (airportNameCache[code]) return `${code}<br><span style="font-size:0.8em;color:#888;font-style:italic;">${airportNameCache[code]}</span>`;
try {
const resp = await fetch(`/api/v1/airport/lookup/${code}`);
if (resp.ok) {
const data = await resp.json();
if (data && data.length && data[0].name) {
airportNameCache[code] = data[0].name;
return `${code}<br><span style="font-size:0.8em;color:#888;font-style:italic;">${data[0].name}</span>`;
}
}
} catch {}
return code;
}
// Create PPR item HTML (async)
async function createPPRItem(ppr) {
// Display callsign as main item if present, registration below; otherwise show registration
const aircraftDisplay = ppr.ac_call && ppr.ac_call.trim() ?
`${ppr.ac_call}<br><span style="font-size: 0.85em; color: #888; font-style: italic;">${ppr.ac_reg}</span>` :
ppr.ac_reg;
// Lookup airport name for in_from/out_to
let fromDisplay = ppr.in_from;
if (ppr.in_from && ppr.in_from.length === 4 && /^[A-Z]{4}$/.test(ppr.in_from)) {
fromDisplay = await getAirportDisplay(ppr.in_from);
}
let toDisplay = ppr.out_to;
if (ppr.out_to && ppr.out_to.length === 4 && /^[A-Z]{4}$/.test(ppr.out_to)) {
toDisplay = await getAirportDisplay(ppr.out_to);
}
return `
<div class="ppr-item">
<div class="ppr-field">
@@ -269,6 +296,14 @@
<strong>Type</strong>
<span>${ppr.ac_type}</span>
</div>
<div class="ppr-field">
<strong>From</strong>
<span>${fromDisplay || '-'}</span>
</div>
<div class="ppr-field">
<strong>To</strong>
<span>${toDisplay || '-'}</span>
</div>
<div class="ppr-field">
<strong>Time</strong>
<span>${formatDateTime(ppr.eta || ppr.etd)}</span>
@@ -298,19 +333,21 @@
try {
const response = await fetch('/api/v1/public/arrivals');
const arrivals = await response.json();
const loadingEl = document.getElementById('arrivals-loading');
const listEl = document.getElementById('arrivals-list');
const countEl = document.getElementById('arrivals-count');
loadingEl.style.display = 'none';
listEl.style.display = 'block';
if (arrivals.length === 0) {
listEl.innerHTML = createNoDataMessage('arrivals');
countEl.textContent = '0 flights';
} else {
listEl.innerHTML = arrivals.map(createPPRItem).join('');
// Render each item async
const htmlArr = [];
for (const ppr of arrivals) {
htmlArr.push(await createPPRItem(ppr));
}
listEl.innerHTML = htmlArr.join('');
countEl.textContent = `${arrivals.length} flight${arrivals.length !== 1 ? 's' : ''}`;
}
} catch (error) {
@@ -330,19 +367,21 @@
try {
const response = await fetch('/api/v1/public/departures');
const departures = await response.json();
const loadingEl = document.getElementById('departures-loading');
const listEl = document.getElementById('departures-list');
const countEl = document.getElementById('departures-count');
loadingEl.style.display = 'none';
listEl.style.display = 'block';
if (departures.length === 0) {
listEl.innerHTML = createNoDataMessage('departures');
countEl.textContent = '0 flights';
} else {
listEl.innerHTML = departures.map(createPPRItem).join('');
// Render each item async
const htmlArr = [];
for (const ppr of departures) {
htmlArr.push(await createPPRItem(ppr));
}
listEl.innerHTML = htmlArr.join('');
countEl.textContent = `${departures.length} flight${departures.length !== 1 ? 's' : ''}`;
}
} catch (error) {