mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-18 01:02:23 +00:00
- Street Level: coverage sequences are ground corridor sets of up to 120, draped on terrain and tiles as Cesium's ground polylines were; the marker and view cones are sprite movers turned to their compass bearing on screen and culled 1 km under WGS84. The layer no longer loads the Cesium SDK on Navara. - Traffic: vehicles are two point mover sets (an outline disc under each fill) scaled and faded with Cesium's NearFarScalar maths; congestion heat lines are ground corridor sets draped on tiles. Heights, depth, readiness and the surface key go through the engine, and Navara no longer loads the Cesium SDK for traffic. - Directions: the route is one ground corridor set in the annotation route's dash pattern, maneuvers are point movers and the A/B markers a disc and a letter sprite; the in-layer Navara branch and the Cesium point collection go. Known gaps against Cesium, to close in the contract: the traffic heat lines lose their glow and pulse and the route its flowing dashes (no animated ground fills yet), and dense Street Level coverage on Navara is costly as ground corridors (about 80 to 190 ms a frame at 6,000 to 14,400 sequences) until corridors are binned set-wide. Coupling: Navara renderer lines fall from 3349 to 2767, Cesium-importing files from 51 to 32, `viewer` lines from 1591 to 1373, Cesium SDK loads on Navara from 6 to 4.
785 lines
27 KiB
JavaScript
785 lines
27 KiB
JavaScript
import * as geo from '../../geo/index.js';
|
|
import { createState } from './state.js';
|
|
import { createMarker } from './marker.js';
|
|
import { createCameraFraming } from './cameraFraming.js';
|
|
import { createCredits } from './credits.js';
|
|
import { createPickRouter } from './pickRouter.js';
|
|
import { createSelection } from './selection.js';
|
|
import { createViewerHost } from './viewerHost.js';
|
|
import { filtersOf, requiresKeyIdFor, validateProviders } from './registry.js';
|
|
import { normalizeFilter, resolveFilter, sameFilter } from './filter.js';
|
|
import { decodeParams, encodeParams } from './params.js';
|
|
import {
|
|
composeUIState,
|
|
keyAlternatives,
|
|
summarizeCoverage,
|
|
} from './uiState.js';
|
|
import { groundUnderCamera, viewCentre } from './view.js';
|
|
import { createGroundClick, groundClickHeightOk } from './groundClick.js';
|
|
import { createStreetLevelDisplay } from './render/display.js';
|
|
import { msg, t, translateKnown } from '../../i18n/index.js';
|
|
import {
|
|
AIM_AT_GROUND_MESSAGE,
|
|
keyStatusLabel,
|
|
NEAREST_RADIUS_M,
|
|
POSITION_PICK_ID,
|
|
STREET_LEVEL_LAYER_ID,
|
|
} from './policy.js';
|
|
|
|
export { STREET_LEVEL_LAYER_ID } from './policy.js';
|
|
|
|
/** Longest wait for a provider's key status; past it, the provider decides. */
|
|
const KEY_STATUS_TIMEOUT_MS = 10_000;
|
|
/** Re-measures of a ground not loaded yet (Google 3D), a second apart. */
|
|
const GROUND_RETRY_LIMIT = 5;
|
|
const GROUND_RETRY_MS = 1000;
|
|
|
|
/**
|
|
* The Street Level layer over imagery providers (contract in registry.js).
|
|
* The core owns what they share: the filter and switches, the photo viewer
|
|
* host, the position marker, the click handler and nearest-image lookups.
|
|
* Providers own coverage, sequences and their viewer. One source is lit at
|
|
* a time. Everything on the globe goes through the display on the engine it
|
|
* is attached to: `services.createDisplay(engine)` (the app's renderer
|
|
* registry), else render/display.js; a display of null draws nothing.
|
|
* @param {{providers: Array<import('./registry.js').StreetLevelProvider>, services?: object}} options
|
|
*/
|
|
export function createStreetLevelLayer({
|
|
providers: definitions,
|
|
services = {},
|
|
}) {
|
|
const defs = validateProviders(definitions);
|
|
const state = createState({ services });
|
|
const parts = {};
|
|
const context = { state, parts };
|
|
parts.credits = createCredits();
|
|
parts.marker = createMarker(context);
|
|
parts.framing = createCameraFraming(context);
|
|
parts.router = createPickRouter(() => state.providers.values(), {
|
|
positionId: POSITION_PICK_ID,
|
|
});
|
|
parts.viewerHost = createViewerHost(context);
|
|
parts.hasSelectedSequence = () =>
|
|
[...state.providers.values()].some(
|
|
(entry) => entry.instance.sequenceStats?.()?.selectedId,
|
|
);
|
|
parts.clearSequences = () => {
|
|
for (const entry of state.providers.values())
|
|
entry.instance.clearSequence?.();
|
|
};
|
|
parts.selection = createSelection(context);
|
|
parts.openAtGround = createGroundClick({
|
|
state,
|
|
openNearest: (point, options) => layer.openNearest(point, options),
|
|
isAvailable: (entry) => providerAvailable(entry),
|
|
});
|
|
|
|
let waitingForView = false;
|
|
let notifyQueued = false;
|
|
function notify() {
|
|
if (notifyQueued) return;
|
|
notifyQueued = true;
|
|
queueMicrotask(() => {
|
|
notifyQueued = false;
|
|
const snapshot = getUIState();
|
|
for (const listener of [...state.listeners]) {
|
|
try {
|
|
listener(snapshot);
|
|
} catch (error) {
|
|
console.warn('[Data:StreetLevel] listener error:', error);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
state.notify = notify;
|
|
|
|
function providerContext(entry) {
|
|
return Object.freeze({
|
|
services: state.services,
|
|
/** Resolved against now, so "since N days" keeps up with the clock. */
|
|
getFilter: () => resolveFilter(state.filter),
|
|
/** The layer and this provider's switch are both on. */
|
|
isActive: () => state.enabled && entry.on,
|
|
waitingForView: () => waitingForView,
|
|
notify,
|
|
actions: {
|
|
/**
|
|
* `{frame: false}` opens it without moving the camera. A provider
|
|
* switched off (or a layer turned off) answers late: ignored.
|
|
*/
|
|
openImage: (imageId, options) =>
|
|
state.enabled && entry.on
|
|
? openImage(entry.def.id, imageId, options)
|
|
: Promise.resolve(false),
|
|
/** This provider's image nearest a point the user picked on the map. */
|
|
openNearest: (point) =>
|
|
layer.openNearest(point, {
|
|
providerIds: [entry.def.id],
|
|
frame: false,
|
|
}),
|
|
/**
|
|
* Show a provider error, or withdraw it (null). A withdrawal clears
|
|
* only this provider's own error, never a newer one from elsewhere.
|
|
*/
|
|
reportError: (message) => {
|
|
if (message) {
|
|
// Another source owns the panel now; its late failure is not news.
|
|
if (!state.enabled || !entry.on) return;
|
|
state.street.error = entry.reportedError = message;
|
|
} else {
|
|
if (!entry.reportedError) return;
|
|
if (state.street.error === entry.reportedError)
|
|
state.street.error = null;
|
|
entry.reportedError = null;
|
|
}
|
|
notify();
|
|
},
|
|
},
|
|
});
|
|
}
|
|
|
|
/** `def` if its key is ready, else the first keyless provider not refused. */
|
|
function keylessInstead(def, refused = () => false) {
|
|
if (def.keyReady?.() !== false) return def;
|
|
return defs.find((d) => !d.requiresKeyId && !refused(d.id)) || def;
|
|
}
|
|
|
|
// One source at a time: the first provider that defaults on. Without its
|
|
// key, the first keyless source instead, so a fresh keyless install opens
|
|
// on coverage rather than KEY REQUIRED.
|
|
const preferred = defs.find((def) => def.defaultOn !== false);
|
|
const startsOn = preferred && keylessInstead(preferred);
|
|
for (const def of defs) {
|
|
const entry = {
|
|
def,
|
|
instance: null,
|
|
on: def === startsOn,
|
|
status: null,
|
|
/** The status request in flight; overlapping checks share it. */
|
|
statusCheck: null,
|
|
/** The error this provider last reported, until it withdraws it. */
|
|
reportedError: null,
|
|
};
|
|
entry.instance = def.create(providerContext(entry));
|
|
state.providers.set(def.id, entry);
|
|
}
|
|
|
|
let mapStack = null;
|
|
let unsubscribeMapStack = null;
|
|
|
|
/** Usable only on its map stack (Google's terms: Street View on Google 3D only). */
|
|
function providerAvailable(entry) {
|
|
const required = entry.def.requiresMapStack;
|
|
return !required || mapStack?.getActiveId?.() === required;
|
|
}
|
|
|
|
/** Leaving a provider's map stack closes its image, viewer and credit. */
|
|
function syncProviderAvailability() {
|
|
const owner = state.providers.get(state.street.providerId);
|
|
if (state.street.open && owner && !providerAvailable(owner))
|
|
layer.closeViewer();
|
|
for (const entry of state.providers.values()) {
|
|
if (!entry.def.requiresMapStack) continue;
|
|
if (state.enabled && entry.on && providerAvailable(entry))
|
|
parts.credits.show(state.map, entry.def);
|
|
else {
|
|
parts.credits.hide(state.map, entry.def);
|
|
parts.viewerHost.unmount(entry.def.id);
|
|
}
|
|
}
|
|
remeasureGround();
|
|
notify();
|
|
}
|
|
|
|
let groundRetry = null;
|
|
let groundRetries = 0;
|
|
function cancelGroundRetry() {
|
|
clearTimeout(groundRetry);
|
|
groundRetry = null;
|
|
}
|
|
|
|
/**
|
|
* Whether a ground click can open an image: the camera is at street zoom.
|
|
* Under Google 3D the ground is unknown until its tiles load, so an unknown
|
|
* ground is measured again a few times instead of waiting for the next move.
|
|
*/
|
|
function syncGroundClickReady() {
|
|
cancelGroundRetry();
|
|
const ready = state.enabled && groundClickHeightOk(state.map);
|
|
if (
|
|
state.enabled &&
|
|
state.map &&
|
|
!ready &&
|
|
groundRetries < GROUND_RETRY_LIMIT &&
|
|
groundUnderCamera(state.map) === null
|
|
) {
|
|
groundRetries++;
|
|
groundRetry = setTimeout(syncGroundClickReady, GROUND_RETRY_MS);
|
|
}
|
|
if (ready === state.groundClickReady) return;
|
|
state.groundClickReady = ready;
|
|
notify();
|
|
}
|
|
|
|
/** A camera move or a map change measures afresh. */
|
|
function remeasureGround() {
|
|
groundRetries = 0;
|
|
syncGroundClickReady();
|
|
}
|
|
|
|
let removeCameraListener = null;
|
|
function watchCamera(view) {
|
|
removeCameraListener?.();
|
|
removeCameraListener =
|
|
view?.engine?.camera.moveEnd.addEventListener(remeasureGround) || null;
|
|
}
|
|
|
|
const activeEntries = () =>
|
|
[...state.providers.values()].filter((entry) => entry.on);
|
|
|
|
/** A provider's key status. A hung server must not hold LOADING, or the next check, forever. */
|
|
function checkStatus(entry) {
|
|
entry.statusCheck ||= (async () => {
|
|
try {
|
|
entry.status = await entry.instance.status({
|
|
signal: AbortSignal.timeout(KEY_STATUS_TIMEOUT_MS),
|
|
});
|
|
} catch {
|
|
entry.status = null;
|
|
}
|
|
notify();
|
|
})().finally(() => {
|
|
entry.statusCheck = null;
|
|
});
|
|
return entry.statusCheck;
|
|
}
|
|
|
|
function activate(entry) {
|
|
if (!state.map) return;
|
|
entry.instance.activate(state.map);
|
|
// Without a known key, a switch-on asks again: the server may be back,
|
|
// or have a key now.
|
|
if (entry.status?.configured !== true) checkStatus(entry);
|
|
if (providerAvailable(entry)) parts.credits.show(state.map, entry.def);
|
|
}
|
|
|
|
function deactivate(entry) {
|
|
parts.viewerHost.unmount(entry.def.id);
|
|
entry.instance.deactivate();
|
|
parts.credits.hide(state.map, entry.def);
|
|
}
|
|
|
|
/** The nearest-image lookup in flight; any newer user action aborts it. */
|
|
let nearestLookup = null;
|
|
|
|
function abortNearest() {
|
|
nearestLookup?.abort();
|
|
nearestLookup = null;
|
|
}
|
|
|
|
/** Ground under the screen centre; null for sky, undefined when it cannot aim. */
|
|
function screenCentreGround() {
|
|
const engine = state.map?.engine;
|
|
const size = engine?.host.size();
|
|
const pick = state.services.scenePick?.pickGroundPosition;
|
|
if (!pick || !size?.width || !size?.height) return undefined;
|
|
const position = pick(engine, { x: size.width / 2, y: size.height / 2 });
|
|
const carto = position && geo.cartographic.fromCartesian(position);
|
|
return carto
|
|
? {
|
|
lon: geo.math.toDegrees(carto.longitude),
|
|
lat: geo.math.toDegrees(carto.latitude),
|
|
}
|
|
: null;
|
|
}
|
|
|
|
/** `frame: false` keeps the camera where it is (the user picked the spot). */
|
|
function openImage(providerId, imageId, { frame = true } = {}) {
|
|
abortNearest();
|
|
const entry = state.providers.get(providerId);
|
|
if (entry && !providerAvailable(entry)) {
|
|
state.street.loading = false;
|
|
state.street.error = unavailableReason(entry);
|
|
notify();
|
|
return Promise.resolve(false);
|
|
}
|
|
return parts.viewerHost.open(providerId, imageId, { frame });
|
|
}
|
|
|
|
function unavailableReason(entry) {
|
|
return t('{name} needs the Google 3D map: choose it under MAP SOURCE', {
|
|
name: translateKnown(entry.def.name),
|
|
});
|
|
}
|
|
|
|
/** One imagery source at a time: lighting a provider darkens the others. */
|
|
function setProviderEnabled(providerId, on) {
|
|
const entry = state.providers.get(providerId);
|
|
if (!entry) return false;
|
|
const before = litIds();
|
|
const next = on !== false;
|
|
if (next)
|
|
for (const other of state.providers.values())
|
|
if (other !== entry && other.on) switchProvider(other, false);
|
|
switchProvider(entry, next);
|
|
sourceChanged(before);
|
|
notify();
|
|
return true;
|
|
}
|
|
|
|
const litIds = () =>
|
|
[...state.providers]
|
|
.filter(([, entry]) => entry.on)
|
|
.map(([id]) => id)
|
|
.join();
|
|
|
|
/**
|
|
* After a chip, voice or a link changed the lit source: a lookup still
|
|
* running and its spinner or error were about the source switched away
|
|
* (an open photo keeps its own state).
|
|
*/
|
|
function sourceChanged(before) {
|
|
if (litIds() === before) return;
|
|
abortNearest();
|
|
if (state.street.open) return;
|
|
state.street.error = null;
|
|
state.street.loading = false;
|
|
}
|
|
|
|
function switchProvider(entry, on) {
|
|
if (entry.on === on) return;
|
|
entry.on = on;
|
|
if (!state.enabled) return;
|
|
if (on) activate(entry);
|
|
else deactivate(entry);
|
|
}
|
|
|
|
/**
|
|
* The one provider params leave lit: of those they switch on, the one
|
|
* already on (reapplying never flips), else the first; with none, the lit
|
|
* one unless they switch it off.
|
|
*/
|
|
function litByParams(switches) {
|
|
const asked = [...state.providers.keys()].filter(
|
|
(id) => switches.get(id) === true,
|
|
);
|
|
const current = [...state.providers]
|
|
.filter(([, entry]) => entry.on)
|
|
.map(([id]) => id);
|
|
if (asked.length)
|
|
return asked.find((id) => current.includes(id)) ?? asked[0];
|
|
return current.find((id) => switches.get(id) !== false) ?? null;
|
|
}
|
|
|
|
function setCoverageFilter(next) {
|
|
const filter = normalizeFilter(next, state.filter);
|
|
if (sameFilter(filter, state.filter)) return;
|
|
state.filter = filter;
|
|
const resolved = resolveFilter(filter);
|
|
for (const entry of state.providers.values())
|
|
entry.instance.setFilter(resolved);
|
|
notify();
|
|
}
|
|
|
|
/** The hint a ground-click provider shows, or the provider's own. */
|
|
function hintFor(entry, stats) {
|
|
if (entry.def.groundClick && stats.hint && !state.groundClickReady)
|
|
// No-break spaces keep a provider name (Google Street View) on one line.
|
|
return t('zoom in to a street to open {name}', {
|
|
name: translateKnown(entry.def.name).replace(/ /g, '\u00a0'),
|
|
});
|
|
return stats.hint || '';
|
|
}
|
|
|
|
/** Providers usable on this map stack; the others are not shown at all. */
|
|
const listedEntries = () =>
|
|
[...state.providers.values()].filter((entry) => providerAvailable(entry));
|
|
|
|
function providerSnapshots() {
|
|
return listedEntries().map((entry) => {
|
|
const stats = entry.instance.coverageStats();
|
|
return {
|
|
id: entry.def.id,
|
|
name: entry.def.name,
|
|
label: entry.def.label,
|
|
on: entry.on,
|
|
configured: entry.status ? entry.status.configured === true : null,
|
|
// A rejected key gates the provider exactly like a missing one.
|
|
keyRequired: stats.keyRequired === true || stats.keyRejected === true,
|
|
keyRejected: stats.keyRejected === true,
|
|
requiresKeyId: entry.def.requiresKeyId || null,
|
|
loading: stats.loading === true,
|
|
count: stats.count || 0,
|
|
hint: hintFor(entry, stats),
|
|
error: stats.error || null,
|
|
color: entry.def.colors.coverage,
|
|
coverageLabel: entry.def.coverageLabel || null,
|
|
sinceBasis: entry.def.sinceBasis === 'upload' ? 'upload' : 'capture',
|
|
groundClick: entry.def.groundClick === true,
|
|
filters: [...filtersOf(entry.def)],
|
|
};
|
|
});
|
|
}
|
|
|
|
/** The selected sequence; the provider showing the open image answers first. */
|
|
function sequenceSnapshot() {
|
|
const owner = state.providers.get(state.street.providerId);
|
|
const candidates = owner
|
|
? [owner, ...[...state.providers.values()].filter((e) => e !== owner)]
|
|
: [...state.providers.values()];
|
|
for (const entry of candidates) {
|
|
const stats = entry.instance.sequenceStats?.();
|
|
if (stats?.selectedId || stats?.loading || stats?.error)
|
|
return {
|
|
providerId: entry.def.id,
|
|
selectedId: stats.selectedId || null,
|
|
images: stats.images || 0,
|
|
loading: stats.loading === true,
|
|
error: stats.error || null,
|
|
};
|
|
}
|
|
return {
|
|
providerId: null,
|
|
selectedId: null,
|
|
images: 0,
|
|
loading: false,
|
|
error: null,
|
|
};
|
|
}
|
|
|
|
function getUIState() {
|
|
const { host, ...street } = state.street;
|
|
const ui = composeUIState({
|
|
enabled: state.enabled,
|
|
groundClickReady: state.groundClickReady,
|
|
filter: state.filter,
|
|
providers: providerSnapshots(),
|
|
street,
|
|
sequence: sequenceSnapshot(),
|
|
});
|
|
// The lit source cannot run on this map: say so, not a blank panel.
|
|
const stranded = [...state.providers.values()].find(
|
|
(entry) => entry.on && !providerAvailable(entry),
|
|
);
|
|
if (stranded && !ui.providers.some((p) => p.on))
|
|
ui.coverage.hint = unavailableReason(stranded);
|
|
return ui;
|
|
}
|
|
|
|
const layer = {
|
|
id: STREET_LEVEL_LAYER_ID,
|
|
name: 'Street Level',
|
|
icon: '📷',
|
|
/** The sources usable on this map (Street View only on Google 3D). */
|
|
get source() {
|
|
return listedEntries()
|
|
.map((entry) => entry.def.name)
|
|
.join(' · ');
|
|
},
|
|
updateInterval: 0,
|
|
statsRefreshInterval: 1000,
|
|
/** The key the switched-on providers share (or null), for the layer row. */
|
|
get requiresKeyId() {
|
|
const on = activeEntries();
|
|
return requiresKeyIdFor(on.length ? on.map((entry) => entry.def) : defs);
|
|
},
|
|
/** Registered providers, in chip order. */
|
|
providerIds: defs.map((def) => def.id),
|
|
getSourceAvailability() {
|
|
const available = defs.length > 0;
|
|
return {
|
|
available,
|
|
source: STREET_LEVEL_LAYER_ID,
|
|
reason: available
|
|
? null
|
|
: 'No street-level imagery providers configured',
|
|
};
|
|
},
|
|
|
|
init(scene, options) {
|
|
this.attachRenderer(scene, options);
|
|
for (const entry of state.providers.values()) checkStatus(entry);
|
|
},
|
|
/** Draws on `engine` through its display; the first argument is not read. */
|
|
attachRenderer(
|
|
_scene,
|
|
{ engine = null, waitForCameraRestore = false } = {},
|
|
) {
|
|
const createDisplay = services.createDisplay ?? createStreetLevelDisplay;
|
|
const map = engine ? (createDisplay(engine) ?? null) : null;
|
|
state.map = map;
|
|
waitingForView = waitForCameraRestore;
|
|
if (!map) return;
|
|
parts.marker.ensure(map);
|
|
parts.marker.setVisible(state.enabled);
|
|
for (const entry of state.providers.values()) {
|
|
entry.instance.init(map);
|
|
if (state.enabled && entry.on) {
|
|
entry.instance.activate(map);
|
|
entry.instance.repaintMap?.();
|
|
if (providerAvailable(entry)) parts.credits.show(map, entry.def);
|
|
}
|
|
}
|
|
if (state.enabled) {
|
|
parts.selection.install(map);
|
|
watchCamera(map);
|
|
parts.marker.set(
|
|
state.street.position,
|
|
state.street.bearing,
|
|
state.providers.get(state.street.providerId)?.def.marker,
|
|
);
|
|
remeasureGround();
|
|
}
|
|
},
|
|
resumeViewport() {
|
|
if (!waitingForView) return;
|
|
waitingForView = false;
|
|
if (state.enabled && state.map) {
|
|
for (const entry of activeEntries()) entry.instance.refreshCoverage?.();
|
|
remeasureGround();
|
|
}
|
|
},
|
|
detachRenderer() {
|
|
const map = state.map;
|
|
if (!map) return;
|
|
waitingForView = true;
|
|
removeCameraListener?.();
|
|
removeCameraListener = null;
|
|
cancelGroundRetry();
|
|
parts.framing.cancel();
|
|
parts.selection.uninstall();
|
|
for (const entry of state.providers.values()) {
|
|
if (entry.instance.detachMap) entry.instance.detachMap();
|
|
else if (entry.on) entry.instance.deactivate();
|
|
}
|
|
parts.marker.destroy();
|
|
parts.credits.hideAll(map);
|
|
map.destroy?.();
|
|
unsubscribeMapStack?.();
|
|
unsubscribeMapStack = null;
|
|
mapStack = null;
|
|
state.map = null;
|
|
},
|
|
enable() {
|
|
state.enabled = true;
|
|
parts.marker.setVisible(true);
|
|
parts.selection.install(state.map);
|
|
for (const entry of activeEntries()) activate(entry);
|
|
watchCamera(state.map);
|
|
remeasureGround();
|
|
notify();
|
|
},
|
|
|
|
disable() {
|
|
state.enabled = false;
|
|
abortNearest();
|
|
removeCameraListener?.();
|
|
removeCameraListener = null;
|
|
cancelGroundRetry();
|
|
parts.viewerHost.unmount();
|
|
for (const entry of state.providers.values()) entry.instance.deactivate();
|
|
syncGroundClickReady();
|
|
parts.credits.hideAll(state.map);
|
|
parts.selection.uninstall();
|
|
parts.marker.setVisible(false);
|
|
notify();
|
|
},
|
|
|
|
async update() {
|
|
return state.enabled;
|
|
},
|
|
|
|
destroy() {
|
|
const map = state.map;
|
|
layer.disable();
|
|
for (const entry of state.providers.values()) entry.instance.destroy();
|
|
parts.marker.destroy();
|
|
map?.destroy?.();
|
|
parts.framing.attachNavigation(null);
|
|
unsubscribeMapStack?.();
|
|
unsubscribeMapStack = null;
|
|
mapStack = null;
|
|
state.listeners.clear();
|
|
state.map = null;
|
|
},
|
|
|
|
getStats() {
|
|
// The lifecycle polls this every second: summarise, don't snapshot.
|
|
// English data: the layer panel translates these through translateKnown.
|
|
const snapshots = providerSnapshots();
|
|
const coverage = summarizeCoverage(snapshots);
|
|
const keyLabel = keyStatusLabel(coverage);
|
|
let loadingLabel = '';
|
|
if (keyLabel) loadingLabel = keyLabel;
|
|
else if (coverage.loading) loadingLabel = msg('loading coverage...');
|
|
else if (coverage.hint && state.enabled) loadingLabel = coverage.hint;
|
|
return {
|
|
source: layer.source,
|
|
count: coverage.count,
|
|
loading: coverage.loading,
|
|
keyRequired: coverage.keyRequired,
|
|
keyAlternatives: keyAlternatives(snapshots),
|
|
// A rejected key's message names the fix; a missing one says so.
|
|
error: keyLabel
|
|
? coverage.keyRejected
|
|
? coverage.error || keyLabel
|
|
: keyLabel
|
|
: coverage.error,
|
|
loadingLabel,
|
|
};
|
|
},
|
|
|
|
/** A photo frames itself only if it still owns the camera once loaded. */
|
|
attachNavigation(navigation) {
|
|
parts.framing.attachNavigation(navigation);
|
|
},
|
|
|
|
/** The application map stack; Street View is usable only on Google 3D. */
|
|
attachMapStackController(controller) {
|
|
unsubscribeMapStack?.();
|
|
mapStack = controller || null;
|
|
unsubscribeMapStack =
|
|
mapStack?.subscribe?.(syncProviderAvailability) || null;
|
|
syncProviderAvailability();
|
|
},
|
|
|
|
/** Share-link and stored state: provider switches plus the filter. */
|
|
getParams() {
|
|
return encodeParams({
|
|
providers: [...state.providers].map(([id, entry]) => [id, entry.on]),
|
|
filter: state.filter,
|
|
});
|
|
},
|
|
|
|
/** Unknown providers and malformed values are ignored, so any link applies. */
|
|
setParams(params = {}) {
|
|
const decoded = decodeParams(params, {
|
|
providerIds: state.providers.keys(),
|
|
filter: state.filter,
|
|
});
|
|
// A restored "Mapillary on" (the codec default) would light a source a
|
|
// keyless install cannot draw. Only redirect a provider the params switch
|
|
// on: a filter-only update keeps whatever the user chose.
|
|
const asked = litByParams(decoded.providers);
|
|
const lit =
|
|
asked && decoded.providers.get(asked) === true
|
|
? keylessInstead(
|
|
state.providers.get(asked).def,
|
|
(id) => decoded.providers.get(id) === false,
|
|
).id
|
|
: asked;
|
|
const before = litIds();
|
|
for (const [id, entry] of state.providers)
|
|
if (id !== lit) switchProvider(entry, false);
|
|
if (lit) switchProvider(state.providers.get(lit), true);
|
|
sourceChanged(before);
|
|
notify();
|
|
setCoverageFilter(decoded.filter);
|
|
return true;
|
|
},
|
|
|
|
// ── Public surface used by the panel ────────────────────────────────
|
|
subscribe(listener) {
|
|
if (typeof listener !== 'function') return () => {};
|
|
state.listeners.add(listener);
|
|
return () => state.listeners.delete(listener);
|
|
},
|
|
getUIState,
|
|
/** The DOM element provider viewers render into. */
|
|
attachViewerHost(element) {
|
|
parts.viewerHost.attach(element);
|
|
},
|
|
setProviderEnabled,
|
|
openImage,
|
|
/**
|
|
* Open the nearest image of the active providers (or `providerIds`) near
|
|
* a point, else the view centre, or with `aim: 'screen-centre'` the ground
|
|
* under the screen centre (sky: nothing). Providers without
|
|
* `nearestImage` are skipped.
|
|
*/
|
|
async openNearest(
|
|
point,
|
|
{ providerIds = null, frame = true, aim = 'view' } = {},
|
|
) {
|
|
let view = point;
|
|
if (!view && aim === 'screen-centre') {
|
|
view = screenCentreGround();
|
|
if (view === null) {
|
|
abortNearest();
|
|
state.street.loading = false;
|
|
state.street.error = AIM_AT_GROUND_MESSAGE;
|
|
notify();
|
|
return false;
|
|
}
|
|
}
|
|
view ||= viewCentre(state.map);
|
|
if (!Number.isFinite(view?.lat) || !Number.isFinite(view?.lon))
|
|
return false;
|
|
abortNearest();
|
|
const lookup = new AbortController();
|
|
nearestLookup = lookup;
|
|
const { signal } = lookup;
|
|
state.street.loading = true;
|
|
state.street.error = null;
|
|
notify();
|
|
let lastError = null;
|
|
const asked = (entry) =>
|
|
(!providerIds || providerIds.includes(entry.def.id)) &&
|
|
typeof entry.instance.nearestImage === 'function' &&
|
|
providerAvailable(entry);
|
|
const entries = activeEntries().filter(asked);
|
|
for (const entry of entries) {
|
|
let imageId = null;
|
|
try {
|
|
imageId = await entry.instance.nearestImage(
|
|
{ lat: view.lat, lon: view.lon },
|
|
{ signal },
|
|
);
|
|
} catch (error) {
|
|
// Overtaken: whatever replaced it owns the panel, errors included.
|
|
if (signal.aborted) return false;
|
|
// One provider failing (no key, offline) must not hide the others.
|
|
lastError = error;
|
|
continue;
|
|
}
|
|
if (signal.aborted) return false;
|
|
if (!state.enabled) break;
|
|
// Switched off, or its map left, while it looked: try the next.
|
|
if (!imageId || !entry.on || !providerAvailable(entry)) continue;
|
|
nearestLookup = null;
|
|
return openImage(entry.def.id, imageId, { frame });
|
|
}
|
|
if (nearestLookup === lookup) nearestLookup = null;
|
|
// Switched off while it looked: nothing to open, nothing to report.
|
|
const stillOn = entries.some(
|
|
(entry) => entry.on && providerAvailable(entry),
|
|
);
|
|
// A provider's own refusal is English data that the panel translates;
|
|
// the core's refusal is built here, for the panel only, in the language.
|
|
if (state.enabled && stillOn)
|
|
state.street.error =
|
|
lastError?.message ||
|
|
(point
|
|
? t('No street-level imagery within {radius} m of that point', {
|
|
radius: NEAREST_RADIUS_M,
|
|
})
|
|
: t(
|
|
'No street-level imagery within {radius} m of the view centre',
|
|
{ radius: NEAREST_RADIUS_M },
|
|
));
|
|
state.street.loading = false;
|
|
notify();
|
|
return false;
|
|
},
|
|
/** Close the image and deselect its sequence on the map. */
|
|
closeViewer() {
|
|
abortNearest();
|
|
parts.viewerHost.close();
|
|
parts.clearSequences();
|
|
},
|
|
setViewerRenderMode: (mode) => parts.viewerHost.setRenderMode(mode),
|
|
resizeViewer: () => parts.viewerHost.resize(),
|
|
};
|
|
return layer;
|
|
}
|