Files
gods-eye-view/src/layers/installations/labelBoxClick.test.mjs
T
Denis Groshev dac8738b80 Installations: a label takes a click only where nothing else is drawn
An interactive overlay entry is a card that takes every click from what is
drawn under it, so a base's label kept an aircraft or vessel under it from
being selected. The labels are no longer interactive entries. The layer
claims a click on a label box through the pick registry's click claim, as
CCTV counts do, only where nothing another layer draws is under it; over
the map or the layer's own dots and fills the label takes it, and another
layer's card painted over the label keeps its click. The label box is read
from the box the overlay painted for it (overlayHost.paintRect).
2026-11-20 13:12:21 +01:00

84 lines
2.5 KiB
JavaScript

// src/layers/installations/labelBoxClick.test.mjs — a click on a mapped
// installation's label box finds the site, read from the box the overlay
// painted for it, unless another layer's card or label that takes clicks is
// painted there.
import assert from 'node:assert/strict';
import test from 'node:test';
import { createControls } from './controls.js';
const RECORD = Object.freeze({
id: 'base',
kind: 'installation',
name: 'Base',
class: 'base',
latitude: 10,
longitude: 20,
});
const inside = (box, { x, y }) =>
x >= box.x && x <= box.x + box.w && y >= box.y && y <= box.y + box.h;
/**
* The installations controls over a fake overlay: `cards` are the hit rects
* of entries that take clicks (other layers'), `labels` the boxes painted
* for this layer's labels, which are not cards.
*/
function controlsWith({ cards = [], labels = {} }) {
const overlayHost = {
// As the world overlay does: the last painted card under the point wins.
hitTest(x, y) {
for (let i = cards.length - 1; i >= 0; i--)
if (inside(cards[i], { x, y }))
return { sourceId: cards[i].sourceId, entryId: cards[i].entryId };
return null;
},
};
const namedMarkers = {
labelAt: (position) =>
Object.keys(labels).find((id) => inside(labels[id], position)) ?? null,
};
const controls = createControls({
state: {
enabled: true,
records: [RECORD],
recordById: new Map([[RECORD.id, RECORD]]),
},
services: {},
parts: {
rendering: { installationSurfaceHeightM: () => 0 },
namedMarkers,
},
source: {},
overlayHost,
});
return controls.methods;
}
const BASE_LABEL = { x: 100, y: 100, w: 80, h: 20 };
test("a click on the site's own label box finds the site", () => {
const methods = controlsWith({ labels: { base: BASE_LABEL } });
assert.equal(methods.findAt({ x: 120, y: 110 })?.id, 'base');
assert.equal(methods.findAt({ x: 10, y: 10 }), null, 'outside every box');
assert.equal(methods.findAt(null), null);
});
test("another layer's card painted over the site's label keeps the click", () => {
const methods = controlsWith({
labels: { base: BASE_LABEL },
cards: [
{ sourceId: 'flights', entryId: 'abc123', x: 110, y: 105, w: 40, h: 12 },
],
});
assert.equal(
methods.findAt({ x: 120, y: 110 }),
null,
'the flight card is on top',
);
assert.equal(
methods.findAt({ x: 170, y: 110 })?.id,
'base',
'past the card, the site is found',
);
});