Files
gods-eye-view/src/ui/layerBindings.js
T
German KhokhlovandClaude Opus 5.5 6390a3346f Merge origin/main: Google Street View, KartaView and Panoramax on the slim Street Level
Main gained three imagery providers (Google Street View, KartaView,
Panoramax), shared line-coverage, cone and photo-viewer modules, one source
lit at a time and the translated interface. Their shared modules still
carried what this branch removes, so the merge ports them onto it:

- lineCoverage.js is this branch's coverage.js made provider-neutral
  (draped lines near the ground only, the selection drawn over them, key
  and rate-limit blocking); sequenceCones.js is sequences.js the same way.
  Mapillary, KartaView and Panoramax all build on them; Mapillary moves
  back to providers/mapillary/ beside the others.
- No ground casting, mesh sampling, surface modes, FOLLOW, legend, prewarm,
  orbit overview points (Panoramax's density dots included), stale-tile
  swaps or water-filled point budget come back.
- Kept from main because the new providers need them: requiresMapStack and
  attachMapStackController (Street View on Google 3D only), ground clicks,
  openNearest with an optional provider nearestImage, openImage {frame},
  reportError, the view-cone marker (now an SVG glyph), filters /
  coverageLabel / sinceBasis, OPEN STREET VIEW, keyAlternatives and i18n.
- photoViewer.js treats the viewer as expanded when its wrap is full screen
  or the iPhone popover, as EXPAND now is.
- The fixture browser gate keeps its 8 steps and adds one each for
  KartaView and Panoramax; catalogs, docs, boundaries and format scope
  follow.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-19 13:51:45 +04:00

299 lines
9.9 KiB
JavaScript

import { isExplicitLayerStateOrigin } from '../data/layerState.js';
import {
registerCctvFocusRequestListener,
routeCctvFocusRequest,
} from '../cctvFocusRequest.js';
import {
flyToWorldTarget,
registerWorldFocusRequestListener,
routeWorldFocusRequest,
} from '../worldFocus.js';
import { registerNavigationAuthorityListener } from '../navigationPolicy.js';
/** Own manager subscriptions and the camera-entry events that outlive controls. */
export class LayerBindings {
constructor({
viewer,
services,
readControls,
operations,
feedback,
shareRestoration,
}) {
Object.assign(
this,
{
viewer,
services,
readControls,
_feedback: feedback,
_shareRestoration: shareRestoration,
},
operations,
);
this._disposed = false;
this._dataManager = null;
this._directionsShellModule = null;
this._weatherShellModules = [];
this._cctvRequestFocusHandler = null;
this._removeCctvRequestFocusListener = null;
this._worldRequestFocusHandler = null;
this._removeWorldRequestFocusListener = null;
this._removeNavigationAuthorityListener = null;
this._navigationOwnerChangedRemover = null;
this._awarenessSelectedHandler = null;
this._awarenessClearedHandler = null;
}
get hud() {
return this.readControls().hud;
}
get _contextControls() {
return this.readControls()._contextControls;
}
get _cctvControls() {
return this.readControls()._cctvControls;
}
get _radioControls() {
return this.readControls()._radioControls;
}
get _streetLevelControls() {
return this.readControls()._streetLevelControls;
}
observeCamera() {
this._cctvRequestFocusHandler = (event) =>
routeCctvFocusRequest(
event,
(activate, focus) => this._runExplicitCctvFocus(activate, focus),
(cameraId, durationSec) =>
this.services.cctvLayer.focusCamera(cameraId, durationSec),
);
this._removeCctvRequestFocusListener = registerCctvFocusRequestListener(
window,
this._cctvRequestFocusHandler,
);
this._worldRequestFocusHandler = (event) =>
routeWorldFocusRequest(
event,
(detail, fly) => this._runExplicitWorldFocus(detail, fly),
(detail) => flyToWorldTarget(this.viewer, detail),
);
this._removeWorldRequestFocusListener = registerWorldFocusRequestListener(
window,
this._worldRequestFocusHandler,
);
this._navigationOwnerChangedRemover =
this.viewer.trackedEntityChanged.addEventListener((entity) => {
if (entity && !this._disposed)
this._stampNavigation({ cancelPendingSelection: false });
});
// Vessel/installation focus flies without ever assigning a tracked entity,
// so it cannot reach the listener above. It announces instead.
this._removeNavigationAuthorityListener =
registerNavigationAuthorityListener(window, (event) => {
if (this._disposed) return;
this._stampNavigation({
cancelPendingSelection:
event?.detail?.cancelPendingSelection !== false,
});
});
}
_connectDirectionsCamera() {
if (!this._dataManager) {
// Detaching: the layer outlives this shell, so it must not keep calling
// a facade whose viewer is going away.
this._directionsShellModule?.attachShellServices?.(null);
this._directionsShellModule = null;
return;
}
const directions = this._dataManager.layers?.get('directions')?.module;
if (this._directionsShellModule !== directions) {
this._directionsShellModule?.attachShellServices?.(null);
this._directionsShellModule = null;
}
if (typeof directions?.attachShellServices !== 'function') return;
this._directionsShellModule = directions;
directions.attachShellServices({
runNavigation: (navigate) =>
this.runImmediateNavigation('route', navigate),
floorFn: (lat, lon) => this.services.cachedGroundFloor(lat, lon),
warmFn: (cells) => this.services.warmGroundFloor(cells),
showToast: (message) => this._showToast(message),
});
}
_connectWeatherCamera() {
for (const layer of this._weatherShellModules)
layer.attachShellServices?.(null);
this._weatherShellModules = [];
for (const id of [
'wind',
'weather-radar',
'weather-satellite',
'weather-lightning',
'weather-cyclones',
]) {
const layer = this._dataManager?.layers?.get(id)?.module;
if (typeof layer?.attachShellServices !== 'function') continue;
const noun = id === 'weather-cyclones' ? 'cyclone' : 'weather';
layer.attachShellServices({
runNavigation: (navigate) =>
this.runImmediateNavigation(noun, navigate),
imageryHost: this.services.imageryHost,
});
this._weatherShellModules.push(layer);
}
}
_persistAwarenessSelection(event, cleared = false) {
if (!this._dataManager) return;
const origin = String(event?.detail?.origin || 'programmatic');
if (!isExplicitLayerStateOrigin(origin)) return;
const layerId = String(event?.detail?.layerId || '');
const config = {
flights: {
key: 'selectedFlightsTrackingId',
normalize: (value) =>
String(value ?? '')
.trim()
.toLowerCase() || null,
},
military: {
key: 'selectedMilitaryTrackingId',
normalize: (value) =>
String(value ?? '')
.trim()
.toLowerCase() || null,
},
satellites: {
key: 'selectedSatTrackingId',
normalize: (value) => {
const candidate = Number(value);
return Number.isFinite(candidate) && candidate > 0
? Math.trunc(candidate)
: null;
},
},
}[layerId];
if (!config) return;
const selectedValue = cleared ? null : config.normalize(event?.detail?.id);
if (cleared || selectedValue === null) {
this._dataManager.adoptLayerParams?.(
layerId,
{
[config.key]: selectedValue,
},
{ origin },
);
return;
}
// A direct selection promotes a Context-owned tracker dependency into
// durable visibility before its selected ID is normalized. Context exit
// also keeps this adopted layer instead of tearing down the user's track.
const visibilityAdopted = this._dataManager.adoptLayerVisibility?.(
layerId,
true,
{ origin, adoptedFromSelection: true },
);
if (visibilityAdopted === false) return;
// Clear the prior family before publishing the replacement. Otherwise the
// coordinator briefly sees two IDs and correctly treats them as an
// ambiguous incoming state, which would discard the new durable target.
for (const [otherLayerId, otherKey] of [
['flights', 'selectedFlightsTrackingId'],
['military', 'selectedMilitaryTrackingId'],
['satellites', 'selectedSatTrackingId'],
]) {
if (otherLayerId === layerId) continue;
this._dataManager.setLayerParams(
otherLayerId,
{ [otherKey]: null },
{ origin },
);
}
this._dataManager.adoptLayerParams?.(
layerId,
{
[config.key]: selectedValue,
},
{ origin },
);
}
attachDataManager(dataManager) {
if (this._disposed) return;
this._dataManager = dataManager || null;
this.hud.attachDataManager(this._dataManager);
this._updateTrafficSyncChip();
if (this._dataManagerUnsubscribe) {
this._dataManagerUnsubscribe();
this._dataManagerUnsubscribe = null;
}
this._contextControls.connect(this._dataManager);
if (typeof this._dataManager?.subscribe === 'function') {
this._dataManagerUnsubscribe = this._dataManager.subscribe((change) => {
this._feedback._loadingFeedbackEvent = change;
this._updateGlobalLoadingFeedback(performance.now());
});
}
this._updateGlobalLoadingFeedback(performance.now());
this._syncContextModeButtons();
this._cctvControls.connect();
this._radioControls.connect();
// Its switch-on requests come from the manager, which arrives after it.
this._streetLevelControls?.connect();
this._connectDirectionsCamera();
this._connectWeatherCamera();
if (!this._awarenessSelectedHandler) {
this._awarenessSelectedHandler = (event) =>
this._persistAwarenessSelection(event, false);
this._awarenessClearedHandler = (event) =>
this._persistAwarenessSelection(event, true);
window.addEventListener(
'gev:awareness-subject-selected',
this._awarenessSelectedHandler,
);
window.addEventListener(
'gev:awareness-subject-cleared',
this._awarenessClearedHandler,
);
}
this._shareRestoration.connect(this._dataManager);
}
stop() {
if (this._disposed) return;
this._disposed = true;
if (this._awarenessSelectedHandler) {
window.removeEventListener(
'gev:awareness-subject-selected',
this._awarenessSelectedHandler,
);
this._awarenessSelectedHandler = null;
}
if (this._awarenessClearedHandler) {
window.removeEventListener(
'gev:awareness-subject-cleared',
this._awarenessClearedHandler,
);
this._awarenessClearedHandler = null;
}
this._removeCctvRequestFocusListener?.();
this._removeCctvRequestFocusListener = null;
this._cctvRequestFocusHandler = null;
this._removeWorldRequestFocusListener?.();
this._removeWorldRequestFocusListener = null;
this._worldRequestFocusHandler = null;
this._navigationOwnerChangedRemover?.();
this._navigationOwnerChangedRemover = null;
this._removeNavigationAuthorityListener?.();
this._removeNavigationAuthorityListener = null;
}
disconnect() {
this._dataManagerUnsubscribe?.();
this._dataManagerUnsubscribe = null;
this._directionsShellModule?.attachShellServices?.(null);
this._directionsShellModule = null;
this._dataManager = null;
this._connectWeatherCamera();
}
}