Files
gods-eye-view/src/layers/installations/selection.js
T
Denis Groshev 9ed6f11c58 Installations: a label box takes a click only when nothing else was hit
The fallback to the label box the shared overlay paints for a site now runs
only when the click hit nothing drawn (the map itself), reads the overlay in
CSS pixels, and accepts the site only when its label is the one painted on
top there: an aircraft or vessel, or another layer's label, over a site's
label keeps its own click. Tested against the overlay's hit rects, not a stub.
2026-11-17 16:28:04 +01:00

94 lines
3.3 KiB
JavaScript

import { inputFor } from '../../input/dispatcher.js';
import { LAYER_ID } from './policy.js';
export function createSelection({
state: layerState,
services,
parts,
source,
}) {
const { clearSelectedEntityContextForLayer } = services.context;
let removeContextListeners = null;
function syncContextSelection(event) {
if (!layerState.enabled) return;
// Tracking layers publish this event before replacing the shared context.
const selected =
event?.type === 'gev:awareness-subject-selected'
? event.detail
: services.context.getSelectedEntityContext();
const next =
selected?.layerId === LAYER_ID && layerState.recordById.has(selected.id)
? selected.id
: null;
if (next === layerState.selectedId) return;
layerState.selectedId = next;
parts.rendering.renderRecords();
}
function selectRecord(id) {
const record = layerState.recordById.get(id);
if (!record || !layerState.sets) return false;
layerState.selectedId = id;
parts.rendering.renderRecords({ claimSelection: true });
// renderRecords drops selectedId when the record produced no marker.
return layerState.selectedId === id;
}
function installInteraction(viewer) {
if (layerState.clickSubscription) return;
const events = [
'gev:entity-selected',
'gev:entity-selection-cleared',
'gev:awareness-subject-selected',
];
for (const event of events)
globalThis.window?.addEventListener?.(event, syncContextSelection);
removeContextListeners = () => {
for (const event of events)
globalThis.window?.removeEventListener?.(event, syncContextSelection);
};
const input = inputFor(viewer);
layerState.clickSubscription = input.onClick((event) => {
if (!layerState.enabled) return;
// A site's dot, outline or fill, or a named marker: each draw item
// names its installation in its `meta`. Else, only when the click hit
// nothing drawn (the map itself), the label box the shared overlay
// paints for the site on top there (controls.findAt reads its hit
// rects, in CSS px), which is what a click lands on from far out; an
// aircraft or vessel drawn over a label keeps its own click.
const hit = event.drawHit;
const id =
(hit?.set === parts.namedMarkers?.setId ||
parts.rendering.ownsSet(hit?.set)
? hit.meta?.installationId
: null) ??
(event.topHit
? null
: parts.controls?.methods?.findAt?.(
event.screenPosition ?? event.position,
)?.id) ??
null;
if (id && layerState.recordById.has(id) && id !== layerState.selectedId) {
selectRecord(id);
} else if (layerState.selectedId) {
// Clicking the selected site again, empty map, or another contact
// releases this layer's selection. Clear only our shared context so a
// sibling click handler's newly selected aircraft/site stays intact.
layerState.selectedId = null;
clearSelectedEntityContextForLayer(LAYER_ID);
parts.rendering.renderRecords();
}
});
}
return {
selectRecord,
installInteraction,
destroy() {
removeContextListeners?.();
removeContextListeners = null;
},
};
}