Keep Context inside the package boundaries

The shell passes the ground picker to the click bridge, so the UI shell no
longer pulls in the annotation and search stack. Context's new modules are
owned by the packages that import them and formatted with the rest.

Co-authored-by: Manjunath Paparaju <[email protected]>
This commit is contained in:
Sameh KhamisandManjunath Paparaju committed 2026-11-16 14:59:01 -08:00
1 parent c351e66af9
commit 902689da8a
11 files changed
+426 -122

No files matched your search

+1
View File
@@ -38,6 +38,7 @@ export function createApplicationControls({
...services,
...operations.surface.controlServices,
searchAndFlyTo: operations.searchAndFlyTo,
pickWorldFromScreen: operations.annotationResolver.pickWorldFromScreen,
fetchRegionalBrief: (...args) =>
operations.requests.regional.getBrief(...args),
...catalogControlServices(catalog),
+271 -69
View File
@@ -18,28 +18,81 @@ import { getContextStore } from './contextStore.js';
* fields that lead a card as its big figures, in order; `name` is what an
* item without a name of its own goes by.
*/
const AIRCRAFT = { kind: 'aircraft', icon: 'flight', moving: true, headline: ['altitudeM', 'speedMps', 'heading'] };
const AIRCRAFT = {
kind: 'aircraft',
icon: 'flight',
moving: true,
headline: ['altitudeM', 'speedMps', 'heading'],
};
const KINDS = Object.freeze({
flights: AIRCRAFT,
military: AIRCRAFT,
'local-adsb': AIRCRAFT,
'ais-live-vessels': { kind: 'vessel', icon: 'directions_boat', moving: true, headline: ['speedKts', 'courseDeg', 'navStatus'] },
satellites: { kind: 'satellite', icon: 'satellite_alt', moving: true, headline: ['altitudeM', 'speedMps'] },
'ais-live-vessels': {
kind: 'vessel',
icon: 'directions_boat',
moving: true,
headline: ['speedKts', 'courseDeg', 'navStatus'],
},
satellites: {
kind: 'satellite',
icon: 'satellite_alt',
moving: true,
headline: ['altitudeM', 'speedMps'],
},
'rocket-launches': { kind: 'launch', icon: 'rocket_launch' },
'local-firms': { kind: 'fire', icon: 'local_fire_department', headline: ['frp', 'confidence'], name: 'Fire detection' },
'fire-perimeters': { kind: 'fire perimeter', icon: 'local_fire_department', headline: ['acres', 'containedPct'] },
earthquakes: { kind: 'earthquake', icon: 'earthquake', headline: ['magnitude', 'depthKm'] },
'weather-cyclones': { kind: 'storm', icon: 'cyclone', headline: ['windKt', 'pressureHpa'] },
'air-quality': { kind: 'air quality station', icon: 'air', headline: ['value', 'band'] },
'local-firms': {
kind: 'fire',
icon: 'local_fire_department',
headline: ['frp', 'confidence'],
name: 'Fire detection',
},
'fire-perimeters': {
kind: 'fire perimeter',
icon: 'local_fire_department',
headline: ['acres', 'containedPct'],
},
earthquakes: {
kind: 'earthquake',
icon: 'earthquake',
headline: ['magnitude', 'depthKm'],
},
'weather-cyclones': {
kind: 'storm',
icon: 'cyclone',
headline: ['windKt', 'pressureHpa'],
},
'air-quality': {
kind: 'air quality station',
icon: 'air',
headline: ['value', 'band'],
},
'military-installations': { kind: 'installation', icon: 'factory' },
'local-dams': { kind: 'dam', icon: 'factory', headline: ['capacity'] },
'local-datacenters': { kind: 'data centre', icon: 'factory', headline: ['capacity'] },
'local-datacenters': {
kind: 'data centre',
icon: 'factory',
headline: ['capacity'],
},
'osm-places': { kind: 'place', icon: 'location_on' },
transit: { kind: 'vehicle', icon: 'train', moving: true, headline: ['speedMps', 'courseDeg'] },
bikeshare: { kind: 'bike station', icon: 'pedal_bike', headline: ['bikesAvailable', 'docksAvailable'] },
transit: {
kind: 'vehicle',
icon: 'train',
moving: true,
headline: ['speedMps', 'courseDeg'],
},
bikeshare: {
kind: 'bike station',
icon: 'pedal_bike',
headline: ['bikesAvailable', 'docksAvailable'],
},
'alpr-cameras': { kind: 'plate reader', icon: 'videocam' },
cctv: { kind: 'camera', icon: 'videocam' },
'drone-airspace': { kind: 'airspace', icon: 'pentagon', headline: ['lowerAltitude', 'upperAltitude'] },
'drone-airspace': {
kind: 'airspace',
icon: 'pentagon',
headline: ['lowerAltitude', 'upperAltitude'],
},
'wildlife-sightings': { kind: 'sighting', icon: 'pets' },
});
@@ -63,7 +116,15 @@ const KEY_FIELDS = ['icao24', 'mmsi', 'noradId', 'usgsId', 'id', 'key'];
*/
const ALL_RECORDS = 100_000;
/** Fields searched by the omnibar, besides the key fields. */
const SEARCH_FIELDS = ['callsign', 'name', 'registration', 'place', 'operator', 'icao24', 'mmsi'];
const SEARCH_FIELDS = [
'callsign',
'name',
'registration',
'place',
'operator',
'icao24',
'mmsi',
];
/** Never shown as facts: positions and identities already on the card. */
const HIDDEN_FACTS = new Set([
@@ -391,8 +452,13 @@ function shipTypeName(code) {
return 'Other';
}
export function formatFact(field, value, { nowMs = Date.now(), kind = '' } = {}) {
if (field === 'shipType' && shipTypeName(value)) return { value: shipTypeName(value) };
export function formatFact(
field,
value,
{ nowMs = Date.now(), kind = '' } = {},
) {
if (field === 'shipType' && shipTypeName(value))
return { value: shipTypeName(value) };
if (value === true) return { value: 'Yes' };
if (value === false || value === null || value === undefined) return null;
const number = typeof value === 'number' ? value : null;
@@ -400,7 +466,10 @@ export function formatFact(field, value, { nowMs = Date.now(), kind = '' } = {})
const round = (n, digits = 0) =>
n.toLocaleString('en-US', { maximumFractionDigits: digits });
if (number !== null) {
if (/AtMs$|^timeMs$|^acqTime$|^observedMs$|^updatedTime$/.test(field) && isEpochMs(number))
if (
/AtMs$|^timeMs$|^acqTime$|^observedMs$|^updatedTime$/.test(field) &&
isEpochMs(number)
)
return { value: '', age: Math.max(0, nowMs - number) };
// Any other moment reads as a date.
if (/(Time|Ms|At)$/.test(field) && isEpochMs(number))
@@ -429,7 +498,8 @@ export function formatFact(field, value, { nowMs = Date.now(), kind = '' } = {})
if (field === 'speedKts') return { value: `${round(number, 1)} kt` };
if (field === 'verticalRateMps')
return { value: `${round(number * 196.85)} ft/min` };
if (/^(heading|courseDeg)$/.test(field)) return { value: `${round(number)}°` };
if (/^(heading|courseDeg)$/.test(field))
return { value: `${round(number)}°` };
if (field === 'depthKm') return { value: `${round(number, 1)} km` };
if (field === 'magnitude') return { value: `M ${round(number, 1)}` };
if (field === 'frp') return { value: `${round(number, 1)} MW` };
@@ -455,7 +525,10 @@ export function formatFact(field, value, { nowMs = Date.now(), kind = '' } = {})
* The facts a card lists for a record: known fields first, in a fixed order,
* then whatever else the record carries, up to a handful.
*/
export function recordFacts(record, { nowMs = Date.now(), skip = [], kind = '' } = {}) {
export function recordFacts(
record,
{ nowMs = Date.now(), skip = [], kind = '' } = {},
) {
const facts = [];
let observedAgeMs = null;
const seen = new Set(skip);
@@ -520,11 +593,11 @@ function recordLinks(record, properties) {
// The language part becomes the link's host, so only a language code is
// let through.
if (/^[a-z]{2,3}(-[a-z0-9]+)*$/.test(lang))
links.push({
kind: 'wikipedia',
label: 'Wikipedia',
href: `https://${lang}.wikipedia.org/wiki/${encodeURIComponent(title.join(':').replace(/ /g, '_'))}`,
});
links.push({
kind: 'wikipedia',
label: 'Wikipedia',
href: `https://${lang}.wikipedia.org/wiki/${encodeURIComponent(title.join(':').replace(/ /g, '_'))}`,
});
}
const wikidata = text(flat.wikidata);
if (/^Q\d+$/.test(wikidata))
@@ -557,8 +630,9 @@ export function layerRecords(dataManager, layerId, maxCount = ALL_RECORDS) {
let records = [];
try {
records =
dataManager?.layers?.get?.(layerId)?.module?.getAnalystRecords?.(maxCount) ||
[];
dataManager?.layers
?.get?.(layerId)
?.module?.getAnalystRecords?.(maxCount) || [];
} catch {
records = [];
}
@@ -640,7 +714,8 @@ function storeRecord(identity) {
let bestKm = Infinity;
for (const record of store.entities.values()) {
if (record?.layerId !== identity.layerId) continue;
if (sameText(text(record.properties?.mmsi), text(identity.id))) return record;
if (sameText(text(record.properties?.mmsi), text(identity.id)))
return record;
if (!sameText(text(record.label), text(identity.label))) continue;
const where = recordPosition(record);
const km = at && where ? distanceKm(at, where) : best ? Infinity : 0;
@@ -686,7 +761,8 @@ export function recordWarnings(record) {
found.push(kind ? `Emergency: ${kind}` : 'Emergency declared');
const squawk = EMERGENCY_SQUAWKS[Number(record.squawk)];
if (squawk && !found.length) found.push(squawk);
if (record.tsunami === true || Number(record.tsunami) === 1) found.push('Tsunami warning issued');
if (record.tsunami === true || Number(record.tsunami) === 1)
found.push('Tsunami warning issued');
// `alert` is left out: feeds use it for a level (an earthquake's PAGER
// colour), not a warning to read out.
for (const field of ['warning', 'advisory']) {
@@ -726,29 +802,59 @@ export function aircraftInsights(record, { nowMs = Date.now() } = {}) {
const out = [];
const altitudeM = finite(record.altitudeM);
const climbMps = finite(record.verticalRateMps);
const speedMps = finite(record.speedMps ?? (finite(record.speedKts) !== null ? record.speedKts * 0.514444 : null));
if (record.onGround === true) out.push({ label: 'Phase', value: 'On the ground' });
const speedMps = finite(
record.speedMps ??
(finite(record.speedKts) !== null ? record.speedKts * 0.514444 : null),
);
if (record.onGround === true)
out.push({ label: 'Phase', value: 'On the ground' });
else if (climbMps !== null) {
const fpm = climbMps * 196.85;
out.push({
label: 'Phase',
value: fpm > 300 ? `Climbing ${Math.round(fpm / 100) * 100} ft/min` : fpm < -300 ? `Descending ${Math.round(-fpm / 100) * 100} ft/min` : 'Cruising, level',
value:
fpm > 300
? `Climbing ${Math.round(fpm / 100) * 100} ft/min`
: fpm < -300
? `Descending ${Math.round(-fpm / 100) * 100} ft/min`
: 'Cruising, level',
});
}
if (altitudeM !== null && altitudeM > 900) out.push({ label: 'Flight level', value: `FL${String(Math.round(altitudeM * 3.28084 / 1000) * 10).padStart(3, '0')}` });
if (altitudeM !== null && altitudeM > 900)
out.push({
label: 'Flight level',
value: `FL${String(Math.round((altitudeM * 3.28084) / 1000) * 10).padStart(3, '0')}`,
});
if (speedMps !== null && altitudeM !== null && speedMps > 60)
out.push({ label: 'Mach', value: `M ${(speedMps / speedOfSound(altitudeM)).toFixed(2)}` });
out.push({
label: 'Mach',
value: `M ${(speedMps / speedOfSound(altitudeM)).toFixed(2)}`,
});
const squawk = Number(record.squawk);
if (SQUAWK_MEANINGS[squawk]) out.push({ label: `Squawk ${record.squawk}`, value: SQUAWK_MEANINGS[squawk] });
if (SQUAWK_MEANINGS[squawk])
out.push({
label: `Squawk ${record.squawk}`,
value: SQUAWK_MEANINGS[squawk],
});
const seen = finite(record.lastContactAtMs ?? record.timeMs);
if (seen !== null) {
const seconds = Math.max(0, Math.round((nowMs - seen) / 1000));
out.push({ label: 'Signal', value: seconds < 90 ? `${seconds} s ago` : `${Math.round(seconds / 60)} min ago` });
out.push({
label: 'Signal',
value:
seconds < 90
? `${seconds} s ago`
: `${Math.round(seconds / 60)} min ago`,
});
}
return out;
}
export function describeEntity(dataManager, identity, { nowMs = Date.now() } = {}) {
export function describeEntity(
dataManager,
identity,
{ nowMs = Date.now() } = {},
) {
if (!identity?.layerId) return null;
const { layerId } = identity;
const kind = entityKind(layerId);
@@ -758,14 +864,19 @@ export function describeEntity(dataManager, identity, { nowMs = Date.now() } = {
const stored =
record && kind.moving
? null
: storeRecord({ ...identity, label: identity.label || recordTitle(record) });
: storeRecord({
...identity,
label: identity.label || recordTitle(record),
});
if (!record && !stored && !identity.label) return null;
const at =
recordPosition(record) ||
recordPosition(stored) ||
recordPosition(identity);
const fromRecord = record ? recordFacts(record, { nowMs, kind: kind.kind }) : null;
const fromRecord = record
? recordFacts(record, { nowMs, kind: kind.kind })
: null;
const fromStore = stored ? propertyFacts(stored.properties) : null;
const facts = [...(fromRecord?.facts || [])];
for (const fact of fromStore?.facts || []) {
@@ -780,7 +891,9 @@ export function describeEntity(dataManager, identity, { nowMs = Date.now() } = {
// An item with only a database number goes by what it is.
const ownTitle = recordTitle(record);
const title =
(kind.name && (!ownTitle || ownTitle === text(record?.id)) ? kind.name : ownTitle) ||
(kind.name && (!ownTitle || ownTitle === text(record?.id))
? kind.name
: ownTitle) ||
text(stored?.label) ||
text(stored?.properties?.name) ||
text(identity.label) ||
@@ -804,11 +917,16 @@ export function describeEntity(dataManager, identity, { nowMs = Date.now() } = {
facts: facts.filter((fact) => fact.value !== title),
warnings: recordWarnings(record),
// For the aircraft card's model and route line.
aircraftClass: kind.kind === 'aircraft' ? text(record?.aircraftClass) || null : null,
insights: kind.kind === 'aircraft' ? aircraftInsights(record, { nowMs }) : [],
aircraftClass:
kind.kind === 'aircraft' ? text(record?.aircraftClass) || null : null,
insights:
kind.kind === 'aircraft' ? aircraftInsights(record, { nowMs }) : [],
operator: text(record?.operator) || null,
heading: finite(record?.heading ?? record?.course),
speedMps: finite(record?.speedMps ?? (finite(record?.speedKts) !== null ? record.speedKts * 0.514444 : null)),
speedMps: finite(
record?.speedMps ??
(finite(record?.speedKts) !== null ? record.speedKts * 0.514444 : null),
),
route:
text(record?.routeOrigin) || text(record?.routeDestination)
? {
@@ -838,7 +956,9 @@ export function describeEntity(dataManager, identity, { nowMs = Date.now() } = {
basis: record ? 'record' : stored ? 'store' : 'identity',
identity: {
layerId,
id: record ? recordKey(layerId, record).slice(layerId.length + 1) : text(identity.id),
id: record
? recordKey(layerId, record).slice(layerId.length + 1)
: text(identity.id),
label: title,
lat: at?.lat ?? null,
lon: at?.lon ?? null,
@@ -923,8 +1043,13 @@ export function nearbyByKind(
{ radiusKm = 250, limit = 4, excludeKey = '', query = '' } = {},
) {
const needle = text(query).toLowerCase();
const groups = { flights: { count: 0, items: [] }, vessels: { count: 0, items: [] }, other: [] };
if (!here || !Number.isFinite(here.lat) || !Number.isFinite(here.lon)) return groups;
const groups = {
flights: { count: 0, items: [] },
vessels: { count: 0, items: [] },
other: [],
};
if (!here || !Number.isFinite(here.lat) || !Number.isFinite(here.lon))
return groups;
const found = { aircraft: [], vessel: [] };
const other = [];
for (const layer of enabledLayers(dataManager)) {
@@ -939,7 +1064,8 @@ export function nearbyByKind(
if (group) group.push({ layerId: layer.id, record, km });
// A search lists every match, not only each layer's closest item.
else if (needle) other.push({ layerId: layer.id, record, km });
else if (!closest || km < closest.km) closest = { layerId: layer.id, record, km };
else if (!closest || km < closest.km)
closest = { layerId: layer.id, record, km };
}
if (closest) other.push(closest);
}
@@ -982,7 +1108,9 @@ function tallyBy(records, labelOf, { keep = 4 } = {}) {
counts.set(label, (counts.get(label) || 0) + 1);
}
const sorted = [...counts].sort((a, b) => b[1] - a[1]);
const parts = sorted.slice(0, keep).map(([label, count]) => ({ label, count }));
const parts = sorted
.slice(0, keep)
.map(([label, count]) => ({ label, count }));
const rest = sorted.slice(keep).reduce((sum, [, count]) => sum + count, 0);
if (rest) {
// A label that is itself "Other" takes the tail, so the name shows once.
@@ -1019,7 +1147,10 @@ const AIRCRAFT_CLASS_NAMES = {
*/
export function layerBreakdown(dataManager, layerId, { rect = null } = {}) {
const kind = entityKind(layerId).kind;
if (!['aircraft', 'vessel', 'satellite', 'earthquake', 'vehicle'].includes(kind)) return null;
if (
!['aircraft', 'vessel', 'satellite', 'earthquake', 'vehicle'].includes(kind)
)
return null;
let records = layerRecords(dataManager, layerId);
if (rect) {
const wraps = rect.west > rect.east;
@@ -1028,7 +1159,9 @@ export function layerBreakdown(dataManager, layerId, { rect = null } = {}) {
return (
at.lat >= rect.south &&
at.lat <= rect.north &&
(wraps ? at.lon >= rect.west || at.lon <= rect.east : at.lon >= rect.west && at.lon <= rect.east)
(wraps
? at.lon >= rect.west || at.lon <= rect.east
: at.lon >= rect.west && at.lon <= rect.east)
);
});
}
@@ -1036,18 +1169,34 @@ export function layerBreakdown(dataManager, layerId, { rect = null } = {}) {
const splits = [];
if (kind === 'aircraft') {
const ground = records.filter((record) => record.onGround === true).length;
const military = records.filter((record) => record.onGround !== true && record.military === true).length;
const military = records.filter(
(record) => record.onGround !== true && record.military === true,
).length;
splits.push({
title: 'Status',
parts: [
{ label: 'Civilian, in the air', count: records.length - ground - military },
{
label: 'Civilian, in the air',
count: records.length - ground - military,
},
{ label: 'Military, in the air', count: military },
{ label: 'On the ground', count: ground },
].filter((part) => part.count),
});
splits.push({ title: 'Type', parts: tallyBy(records, (record) => AIRCRAFT_CLASS_NAMES[record.aircraftClass] || plainWord(text(record.aircraftClass))) });
splits.push({
title: 'Type',
parts: tallyBy(
records,
(record) =>
AIRCRAFT_CLASS_NAMES[record.aircraftClass] ||
plainWord(text(record.aircraftClass)),
),
});
} else if (kind === 'vessel') {
splits.push({ title: 'Type', parts: tallyBy(records, (record) => shipTypeName(record.shipType)) });
splits.push({
title: 'Type',
parts: tallyBy(records, (record) => shipTypeName(record.shipType)),
});
} else if (kind === 'vehicle') {
// What the display shows each vehicle doing, then what kind it is, who
// runs it, the operator's own report and crowding where the feed has it.
@@ -1055,25 +1204,55 @@ export function layerBreakdown(dataManager, layerId, { rect = null } = {}) {
// something these records can say.
const word = (field) => (record) => CODE_WORDS[field][record[field]] || '';
splits.push({ title: 'Motion', parts: tallyBy(records, word('motion')) });
splits.push({ title: 'Mode', parts: tallyBy(records, (record) => plainWord(text(record.mode))) });
splits.push({
title: 'Mode',
parts: tallyBy(records, (record) => plainWord(text(record.mode))),
});
const operators = tallyBy(records, (record) => text(record.operator));
if (operators.length > 1) splits.push({ title: 'Operator', parts: operators });
const reported = records.filter((record) => CODE_WORDS.status[record.status]);
if (operators.length > 1)
splits.push({ title: 'Operator', parts: operators });
const reported = records.filter(
(record) => CODE_WORDS.status[record.status],
);
if (reported.length)
splits.push({ title: 'Last reported', parts: tallyBy(reported, word('status')) });
const crowded = records.filter((record) => CODE_WORDS.occupancy[record.occupancy]);
splits.push({
title: 'Last reported',
parts: tallyBy(reported, word('status')),
});
const crowded = records.filter(
(record) => CODE_WORDS.occupancy[record.occupancy],
);
if (crowded.length)
splits.push({ title: 'Crowding', parts: tallyBy(crowded, word('occupancy')) });
splits.push({
title: 'Crowding',
parts: tallyBy(crowded, word('occupancy')),
});
} else if (kind === 'satellite') {
splits.push({ title: 'Class', parts: tallyBy(records, (record) => plainWord(text(record.satelliteClass || record.group))) });
splits.push({
title: 'Class',
parts: tallyBy(records, (record) =>
plainWord(text(record.satelliteClass || record.group)),
),
});
} else {
const band = (record) => {
const m = finite(record.magnitude);
return m === null ? '' : m >= 6 ? 'M6 and above' : m >= 5 ? 'M5 to 6' : m >= 4 ? 'M4 to 5' : 'Below M4';
return m === null
? ''
: m >= 6
? 'M6 and above'
: m >= 5
? 'M5 to 6'
: m >= 4
? 'M4 to 5'
: 'Below M4';
};
splits.push({ title: 'Magnitude', parts: tallyBy(records, band) });
}
return { total: records.length, splits: splits.filter((split) => split.parts.length) };
return {
total: records.length,
splits: splits.filter((split) => split.parts.length),
};
}
/**
@@ -1088,7 +1267,8 @@ export function recordMatches(record, needle, layerLabel = '') {
if (!needle) return true;
if (text(layerLabel).toLowerCase().includes(needle)) return true;
for (const value of Object.values(record || {})) {
if (typeof value === 'string' && value.toLowerCase().includes(needle)) return true;
if (typeof value === 'string' && value.toLowerCase().includes(needle))
return true;
}
return false;
}
@@ -1103,7 +1283,11 @@ export function recordMatches(record, needle, layerLabel = '') {
* @param {{lat: number, lon: number}|null} [options.near] Ties go to the closer item.
* @returns {Array<object>}
*/
export function searchEntities(dataManager, query, { limit = 4, near = null } = {}) {
export function searchEntities(
dataManager,
query,
{ limit = 4, near = null } = {},
) {
const needle = text(query).toLowerCase();
if (needle.length < 2) return [];
const inWord = new RegExp(
@@ -1165,7 +1349,12 @@ export function searchEntities(dataManager, query, { limit = 4, near = null } =
* @param {number} [options.perLayer] Closest items to list for each layer.
* @returns {Array<{layerId: string, layerName: string, icon: string, count: number, items: Array<object>}>}
*/
export function viewSummary(dataManager, rect, center, { perLayer = 3, query = '' } = {}) {
export function viewSummary(
dataManager,
rect,
center,
{ perLayer = 3, query = '' } = {},
) {
if (!rect || !center) return [];
const needle = text(query).toLowerCase();
const wraps = rect.west > rect.east;
@@ -1173,7 +1362,9 @@ export function viewSummary(dataManager, rect, center, { perLayer = 3, query = '
at &&
at.lat >= rect.south &&
at.lat <= rect.north &&
(wraps ? at.lon >= rect.west || at.lon <= rect.east : at.lon >= rect.west && at.lon <= rect.east);
(wraps
? at.lon >= rect.west || at.lon <= rect.east
: at.lon >= rect.west && at.lon <= rect.east);
const groups = [];
for (const layer of enabledLayers(dataManager)) {
const found = [];
@@ -1221,14 +1412,20 @@ export function viewSummary(dataManager, rect, center, { perLayer = 3, query = '
* @param {number} [area.limit]
* @returns {{count: number, items: Array<object>}}
*/
export function layerMatches(dataManager, layerId, { center, rect = null, radiusKm = 60, test = () => true, limit = 8 }) {
export function layerMatches(
dataManager,
layerId,
{ center, rect = null, radiusKm = 60, test = () => true, limit = 8 },
) {
const found = [];
const wraps = rect && rect.west > rect.east;
for (const record of layerRecords(dataManager, layerId)) {
const at = recordPosition(record);
if (!at) continue;
if (rect) {
const inLon = wraps ? at.lon >= rect.west || at.lon <= rect.east : at.lon >= rect.west && at.lon <= rect.east;
const inLon = wraps
? at.lon >= rect.west || at.lon <= rect.east
: at.lon >= rect.west && at.lon <= rect.east;
if (!inLon || at.lat < rect.south || at.lat > rect.north) continue;
}
const km = distanceKm(center, at);
@@ -1260,8 +1457,13 @@ export function layerMatches(dataManager, layerId, { center, rect = null, radius
* emergency, a quake with a tsunami flag, an advisory), nearest first. These
* are what each feed broadcasts, not confirmed incidents.
*/
export function alertsNear(dataManager, here, { radiusKm = 400, limit = 4 } = {}) {
if (!here || !Number.isFinite(here.lat) || !Number.isFinite(here.lon)) return [];
export function alertsNear(
dataManager,
here,
{ radiusKm = 400, limit = 4 } = {},
) {
if (!here || !Number.isFinite(here.lat) || !Number.isFinite(here.lon))
return [];
const found = [];
for (const layer of enabledLayers(dataManager)) {
for (const record of layerRecords(dataManager, layer.id)) {
+8 -2
View File
@@ -314,7 +314,10 @@ export function createQueries({
// unverified, everything else still treats the flight as routeless.
routeScheduled:
!routeOk && info?.route?.origin?.code && info?.route?.destination?.code
? { from: text(info.route.origin.code), to: text(info.route.destination.code) }
? {
from: text(info.route.origin.code),
to: text(info.route.destination.code),
}
: null,
};
}
@@ -777,7 +780,10 @@ export function createQueries({
* @param {string} icao24
*/
requestRoute(icao24) {
if (icao24) parts.enrichment._requestRouteEnrichment(String(icao24).trim().toLowerCase());
if (icao24)
parts.enrichment._requestRouteEnrichment(
String(icao24).trim().toLowerCase(),
);
},
/**
+1
View File
@@ -197,6 +197,7 @@ export class StyleManager extends ShellFacade {
viewer,
getDataManager: () => this._dataManager,
pinia: applicationPinia(),
pickWorld: services.pickWorldFromScreen,
});
this.mapStackController = mapStackController;
this.placeSearch = placeSearch;
+15 -3
View File
@@ -40,8 +40,18 @@ export const TAB_AFFECTS = {
export const LAYER_TABS = [
// Labels and cards the world overlay draws for the label layers.
{ id: 'labels', label: 'Labels', icon: 'label', when: (on) => TAB_AFFECTS.labels(on).length > 0 },
{ id: 'aircraft', label: '3D aircraft', icon: 'flight', when: (on) => TAB_AFFECTS.aircraft(on).length > 0 },
{
id: 'labels',
label: 'Labels',
icon: 'label',
when: (on) => TAB_AFFECTS.labels(on).length > 0,
},
{
id: 'aircraft',
label: '3D aircraft',
icon: 'flight',
when: (on) => TAB_AFFECTS.aircraft(on).length > 0,
},
];
/** Ids of the layers that are on; Context refreshes it. */
@@ -59,7 +69,9 @@ export const contextReady = ref(false);
/** The tabs available now. */
export const availableTabs = computed(() =>
contextReady.value ? LAYER_TABS.filter((tab) => tab.when(enabledLayerIds.value)) : [],
contextReady.value
? LAYER_TABS.filter((tab) => tab.when(enabledLayerIds.value))
: [],
);
export const hasTab = (id) => availableTabs.value.some((tab) => tab.id === id);
+3 -1
View File
@@ -7,7 +7,9 @@ import { ref } from 'vue';
const KEY = 'gev.contextRadiusKm';
/** The distances the control steps through, in km. */
export const CONTEXT_RADIUS_STEPS = Object.freeze([25, 50, 100, 250, 500, 1000, 2500]);
export const CONTEXT_RADIUS_STEPS = Object.freeze([
25, 50, 100, 250, 500, 1000, 2500,
]);
export const DEFAULT_CONTEXT_RADIUS_KM = 250;
/** The step closest to a distance; the default for anything unusable. */
+9 -2
View File
@@ -9,13 +9,20 @@ export const contextSearchOpen = ref(false);
/** The text as typed. */
export const contextSearchText = ref('');
/** The text to match against: trimmed, lower case, '' when not searching. */
export const contextNeedle = computed(() => (contextSearchOpen.value ? contextSearchText.value.trim().toLowerCase() : ''));
export const contextNeedle = computed(() =>
contextSearchOpen.value ? contextSearchText.value.trim().toLowerCase() : '',
);
/** Whether any of the given strings holds the searched text (true when not searching). */
export function matchesContextSearch(...parts) {
const needle = contextNeedle.value;
if (!needle) return true;
return parts.some((part) => part !== null && part !== undefined && String(part).toLowerCase().includes(needle));
return parts.some(
(part) =>
part !== null &&
part !== undefined &&
String(part).toLowerCase().includes(needle),
);
}
export function closeContextSearch() {
+53 -19
View File
@@ -1,5 +1,4 @@
import * as Cesium from 'cesium';
import { pickWorldFromScreen } from '../annotations/annotationResolver.js';
import { resolvePickedContext } from '../data/pickedContext.js';
import { isPointerFree } from '../data/inputOwnership.js';
import { useCockpitStore } from './stores/cockpit.js';
@@ -19,7 +18,12 @@ const FRAME_RANGE = Object.freeze({
});
const DEFAULT_RANGE = 8_000;
/** Items the voice runtime can keep the camera on. */
const FOLLOWABLE = new Set(['flights', 'military', 'ais-live-vessels', 'satellites']);
const FOLLOWABLE = new Set([
'flights',
'military',
'ais-live-vessels',
'satellites',
]);
/**
* Connects the Context panel's entity card to the globe: a click on anything a
@@ -30,9 +34,17 @@ const FOLLOWABLE = new Set(['flights', 'military', 'ais-live-vessels', 'satellit
* @param {object} options.viewer Cesium viewer.
* @param {() => object} options.getDataManager Layer lifecycle.
* @param {object} options.pinia
* @param {(viewer: object, x: number, y: number) => ({lat: number, lon: number}|null)} [options.pickWorld]
* The ground under a normalized screen point; the view's centre falls back
* to the point under the camera without it.
* @returns {() => void} disconnects
*/
export function installEntityContext({ viewer, getDataManager, pinia }) {
export function installEntityContext({
viewer,
getDataManager,
pinia,
pickWorld,
}) {
const store = useEntityCardStore(pinia);
const omnibar = useOmnibarStore(pinia);
const voice = useVoiceStore(pinia);
@@ -43,7 +55,8 @@ export function installEntityContext({ viewer, getDataManager, pinia }) {
const button = doc?.querySelector(
'.ui-rail--right .ui-rail__item[data-rail-item="global-context-panel"]',
);
if (button && button.getAttribute('aria-pressed') !== 'true') button.click();
if (button && button.getAttribute('aria-pressed') !== 'true')
button.click();
}
function flyTo(card) {
@@ -77,11 +90,19 @@ export function installEntityContext({ viewer, getDataManager, pinia }) {
// Aircraft can be flown from, and a satellite ridden in the orbital view:
// track the craft, then enter Cockpit on it.
const COCKPIT_LAYERS = new Set(['flights', 'military', 'local-adsb', 'satellites']);
const COCKPIT_LAYERS = new Set([
'flights',
'military',
'local-adsb',
'satellites',
]);
async function cockpit(card) {
const run = omnibar.runner;
if (!run || !COCKPIT_LAYERS.has(card.layerId)) return;
await run('track_entity', { query: card.identity.id || card.title, layerId: card.layerId });
await run('track_entity', {
query: card.identity.id || card.title,
layerId: card.layerId,
});
if (card.layerId !== 'satellites') {
await run('control_cockpit', { action: 'enter' });
return;
@@ -124,7 +145,8 @@ export function installEntityContext({ viewer, getDataManager, pinia }) {
ask,
askText,
cockpit,
canCockpit: (card) => Boolean(omnibar.runner) && COCKPIT_LAYERS.has(card?.layerId),
canCockpit: (card) =>
Boolean(omnibar.runner) && COCKPIT_LAYERS.has(card?.layerId),
canFollow: (card) => FOLLOWABLE.has(card?.layerId),
// Where the camera is, so a search prefers what is close by.
here: () => {
@@ -144,7 +166,7 @@ export function installEntityContext({ viewer, getDataManager, pinia }) {
const readView = () => {
const camera = viewer.camera;
const height = camera.positionCartographic?.height ?? 0;
const middle = pickWorldFromScreen(viewer, 0.5, 0.5);
const middle = pickWorld?.(viewer, 0.5, 0.5);
const center = middle
? { lat: middle.lat, lon: middle.lon }
: {
@@ -175,7 +197,12 @@ export function installEntityContext({ viewer, getDataManager, pinia }) {
setTimeout(readView, 0);
/** Layers whose own map click already follows what was clicked. */
const CLICK_FOLLOWS = new Set(['flights', 'military', 'satellites', 'ais-live-vessels']);
const CLICK_FOLLOWS = new Set([
'flights',
'military',
'satellites',
'ais-live-vessels',
]);
// Keep the card's "following" in step with the camera. When the camera
// stops following (Escape, another tool, the craft gone) the card stops
@@ -184,13 +211,16 @@ export function installEntityContext({ viewer, getDataManager, pinia }) {
// A layer switching what it follows lets go for a moment and takes hold
// again in the same turn, so this is checked once that settles.
let trackCheck = 0;
const removeTrackedChanged = viewer.trackedEntityChanged.addEventListener(() => {
clearTimeout(trackCheck);
trackCheck = setTimeout(() => {
if (!viewer.trackedEntity) store.following = null;
else if (CLICK_FOLLOWS.has(store.card?.layerId)) store.following = store.card.key;
}, 250);
});
const removeTrackedChanged = viewer.trackedEntityChanged.addEventListener(
() => {
clearTimeout(trackCheck);
trackCheck = setTimeout(() => {
if (!viewer.trackedEntity) store.following = null;
else if (CLICK_FOLLOWS.has(store.card?.layerId))
store.following = store.card.key;
}, 250);
},
);
const handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
// What is under the pointer is read when the button goes down: by the time
@@ -206,14 +236,17 @@ export function installEntityContext({ viewer, getDataManager, pinia }) {
};
let pressed = null;
handler.setInputAction(({ position }) => {
pressed = isPointerFree() ? { picked: pickAt(position), x: position.x, y: position.y } : null;
pressed = isPointerFree()
? { picked: pickAt(position), x: position.x, y: position.y }
: null;
}, Cesium.ScreenSpaceEventType.LEFT_DOWN);
handler.setInputAction(({ position }) => {
// A tool that owns the pointer (draw, measure) keeps its clicks.
const press = pressed;
pressed = null;
if (!isPointerFree()) return;
const samePlace = press && Math.hypot(press.x - position.x, press.y - position.y) < 6;
const samePlace =
press && Math.hypot(press.x - position.x, press.y - position.y) < 6;
const picked = samePlace ? press.picked : pickAt(position);
const identity = resolvePickedContext(picked, {
dataManager: getDataManager(),
@@ -229,7 +262,8 @@ export function installEntityContext({ viewer, getDataManager, pinia }) {
// photorealistic ground is a tileset, so a click on it counts as ground.
// A click on something else Context cannot describe (a label, a trail)
// leaves the card as it is.
if (!picked || picked.primitive instanceof Cesium.Cesium3DTileset) store.clear();
if (!picked || picked.primitive instanceof Cesium.Cesium3DTileset)
store.clear();
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
// Layers that select through the context store (GeoJSON sites, fires,
+40 -15
View File
@@ -37,9 +37,12 @@ function snapshot(name, load) {
feeds.set(name, { at: Date.now(), promise });
return promise;
}
const quakes = () => snapshot('quakes', () => createUsgsEarthquakeSource().getSnapshot());
const storms = () => snapshot('storms', () => createCycloneSource().getSnapshot());
const perimeters = () => snapshot('perimeters', () => createWfigsPerimeterSource().getSnapshot());
const quakes = () =>
snapshot('quakes', () => createUsgsEarthquakeSource().getSnapshot());
const storms = () =>
snapshot('storms', () => createCycloneSource().getSnapshot());
const perimeters = () =>
snapshot('perimeters', () => createWfigsPerimeterSource().getSnapshot());
const fires = () => snapshot('fires', () => createFirmsSource().getSnapshot());
const COMPASS = ['N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW'];
@@ -49,7 +52,9 @@ export function compassPoint(from, to) {
const y = Math.sin((to.lon - from.lon) * rad) * Math.cos(to.lat * rad);
const x =
Math.cos(from.lat * rad) * Math.sin(to.lat * rad) -
Math.sin(from.lat * rad) * Math.cos(to.lat * rad) * Math.cos((to.lon - from.lon) * rad);
Math.sin(from.lat * rad) *
Math.cos(to.lat * rad) *
Math.cos((to.lon - from.lon) * rad);
const deg = (Math.atan2(y, x) / rad + 360) % 360;
return compassName(deg);
}
@@ -98,14 +103,19 @@ export async function hazardsNear(lat, lon, radiusKm = 250) {
add({
kind: 'quake',
icon: 'earthquake',
title: Number.isFinite(Number.parseFloat(quake.mag)) ? `M${Number(quake.mag).toFixed(1)} earthquake` : 'Earthquake',
title: Number.isFinite(Number.parseFloat(quake.mag))
? `M${Number(quake.mag).toFixed(1)} earthquake`
: 'Earthquake',
detail: quake.place || '',
time: quake.time,
at,
});
}
for (const storm of stormSnap?.storms || []) {
const at = { lat: storm.position?.latitude, lon: storm.position?.longitude };
const at = {
lat: storm.position?.latitude,
lon: storm.position?.longitude,
};
if (!(distanceKm(here, at) <= radiusKm)) continue;
add({
kind: 'storm',
@@ -125,8 +135,12 @@ export async function hazardsNear(lat, lon, radiusKm = 250) {
icon: 'local_fire_department',
title: fire.name || 'Wildfire',
detail: [
Number.isFinite(acres) ? `${Math.round(acres).toLocaleString()} acres` : '',
Number.isFinite(fire.containedPct) ? `${fire.containedPct}% contained` : '',
Number.isFinite(acres)
? `${Math.round(acres).toLocaleString()} acres`
: '',
Number.isFinite(fire.containedPct)
? `${fire.containedPct}% contained`
: '',
fire.category === 'RX' ? 'prescribed burn' : '',
]
.filter(Boolean)
@@ -137,10 +151,15 @@ export async function hazardsNear(lat, lon, radiusKm = 250) {
}
// Satellite fire hits are many small points: count the close ones.
const hits = (Array.isArray(fireSnap?.fires) ? fireSnap.fires : [])
.map((fire) => ({ lat: Number(fire.lat ?? fire.latitude), lon: Number(fire.lon ?? fire.longitude) }))
.map((fire) => ({
lat: Number(fire.lat ?? fire.latitude),
lon: Number(fire.lon ?? fire.longitude),
}))
.filter((at) => distanceKm(here, at) <= radiusKm);
if (hits.length) {
const nearest = hits.reduce((a, b) => (distanceKm(here, a) <= distanceKm(here, b) ? a : b));
const nearest = hits.reduce((a, b) =>
distanceKm(here, a) <= distanceKm(here, b) ? a : b,
);
add({
kind: 'hotspots',
icon: 'local_fire_department',
@@ -151,22 +170,28 @@ export async function hazardsNear(lat, lon, radiusKm = 250) {
});
}
const order = { storm: 0, wildfire: 1, hotspots: 2, quake: 3 };
return found.sort((a, b) => order[a.kind] - order[b.kind] || a.distanceKm - b.distanceKm);
return found.sort(
(a, b) => order[a.kind] - order[b.kind] || a.distanceKm - b.distanceKm,
);
}
/** Title-case copy for a WMO weather code, and the icon that goes with it. */
export function weatherLook(code) {
const value = Number(code);
if (!Number.isFinite(value)) return { label: 'Conditions unknown', icon: 'partly_cloudy_day' };
if (!Number.isFinite(value))
return { label: 'Conditions unknown', icon: 'partly_cloudy_day' };
if (value === 0) return { label: 'Clear', icon: 'sunny' };
if (value <= 2) return { label: 'Partly cloudy', icon: 'partly_cloudy_day' };
if (value === 3) return { label: 'Overcast', icon: 'cloud' };
if (value === 45 || value === 48) return { label: 'Fog', icon: 'foggy' };
if (value >= 51 && value <= 57) return { label: 'Drizzle', icon: 'rainy' };
if (value >= 61 && value <= 67) return { label: 'Rain', icon: 'rainy' };
if (value >= 71 && value <= 77) return { label: 'Snow', icon: 'weather_snowy' };
if (value >= 80 && value <= 82) return { label: 'Rain showers', icon: 'rainy' };
if (value >= 85 && value <= 86) return { label: 'Snow showers', icon: 'weather_snowy' };
if (value >= 71 && value <= 77)
return { label: 'Snow', icon: 'weather_snowy' };
if (value >= 80 && value <= 82)
return { label: 'Rain showers', icon: 'rainy' };
if (value >= 85 && value <= 86)
return { label: 'Snow showers', icon: 'weather_snowy' };
if (value >= 95) return { label: 'Thunderstorm', icon: 'thunderstorm' };
return { label: 'Mixed conditions', icon: 'partly_cloudy_day' };
}
+22 -6
View File
@@ -62,7 +62,10 @@ export const useEntityCardStore = defineStore('gev-entity-card', () => {
const dm = shell.value?.dataManager?.();
const layer = dm?.getAll?.().find((row) => row.id === current.layerId);
const stats = layer?.stats || {};
const raw = stats.sourceVerbatim === true && stats.source ? stats.source : sourceLabel(stats.source || layer?.source);
const raw =
stats.sourceVerbatim === true && stats.source
? stats.source
: sourceLabel(stats.source || layer?.source);
return raw || '';
}
const canCockpit = () => Boolean(shell.value?.canCockpit?.(card.value));
@@ -101,7 +104,9 @@ export const useEntityCardStore = defineStore('gev-entity-card', () => {
function askAboutView(summary) {
const where = view.value;
if (!where) return;
const counts = summary.map((group) => `${group.count} ${group.layerName}`).join(', ');
const counts = summary
.map((group) => `${group.count} ${group.layerName}`)
.join(', ');
shell.value?.askText?.(
`What am I looking at? The view is centred on ${where.center.lat.toFixed(4)}, ${where.center.lon.toFixed(4)} from about ${Math.round(where.heightM)} m up${counts ? `, showing ${counts}` : ''}. What is here and what stands out?`,
);
@@ -170,7 +175,13 @@ export const useEntityCardStore = defineStore('gev-entity-card', () => {
* @returns {boolean} Whether a card could be made.
*/
/** Things that move are followed as soon as they are picked. */
const AUTO_FOLLOW = new Set(['flights', 'military', 'local-adsb', 'satellites', 'ais-live-vessels']);
const AUTO_FOLLOW = new Set([
'flights',
'military',
'local-adsb',
'satellites',
'ais-live-vessels',
]);
/** The key of the item the camera is following, if any. */
const following = ref(null);
@@ -185,7 +196,10 @@ export const useEntityCardStore = defineStore('gev-entity-card', () => {
timer = setInterval(refresh, REFRESH_MS);
shell.value?.openPanel?.();
const current = card.value;
const followNow = autoFollow && AUTO_FOLLOW.has(current?.layerId) && shell.value?.canFollow?.(current);
const followNow =
autoFollow &&
AUTO_FOLLOW.has(current?.layerId) &&
shell.value?.canFollow?.(current);
if (followNow) {
// Tracking can announce the craft again; following what is already
// followed would loop, so it only starts once per item.
@@ -194,7 +208,8 @@ export const useEntityCardStore = defineStore('gev-entity-card', () => {
shell.value?.follow?.(current);
}
} else {
if (following.value && following.value !== current?.key) following.value = null;
if (following.value && following.value !== current?.key)
following.value = null;
if (fly) shell.value?.flyTo?.(current);
}
requestRoute(current?.identity);
@@ -215,7 +230,8 @@ export const useEntityCardStore = defineStore('gev-entity-card', () => {
following.value = card.value.key;
shell.value?.follow?.(card.value);
};
const isFollowing = () => Boolean(card.value && following.value === card.value.key);
const isFollowing = () =>
Boolean(card.value && following.value === card.value.key);
const ask = () => card.value && shell.value?.ask?.(card.value);
const canFollow = () => Boolean(shell.value?.canFollow?.(card.value));
/** Loaded items whose name or id matches typed text, for the omnibar. */
+3 -5
View File
@@ -339,10 +339,8 @@ export function createVoiceCommands({
// The omnibar runs the same actions directly, without a model.
const omnibar = useOmnibarStore(applicationPinia());
omnibar.connectRunner((name, args) => runner(name, args));
session.signal.addEventListener(
'abort',
() => omnibar.connectRunner(null),
{ once: true },
);
session.signal.addEventListener('abort', () => omnibar.connectRunner(null), {
once: true,
});
return controls;
}