100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps
Java asset-managementbuildingdashboarddataplatformdevice-managementedge-computingenergyenergy-managementenergy-monitorfleet-managementfleet-telematicsiotiot-applicationiot-platformmodbusmqttmqtt-brokerota-updaterules-enginesmartcity
Fix clicked clusters sometimes not disappearing (#2505)
Koen222d1a758703
diff --git a/ui/app/manager/test/map.test.ts b/ui/app/manager/test/map.test.tsindex 1697882a2..81bf1ef11 100644--- a/ui/app/manager/test/map.test.ts+++ b/ui/app/manager/test/map.test.ts@@ -2,6 +2,7 @@ import { expect } from "@openremote/test";import { adminStatePath, test, userStatePath } from "./fixtures/manager.js";import {preparedAssetsWithLocation as assets,+ assignLocation,assignRandomLocationInArea,commonAttrs,getAssetTypeColour,@@ -266,11 +267,11 @@ test.describe("Marker clustering", () => {test("should display clustered markers", async ({ page, manager, browserName }) => {test.skip(browserName === "firefox", "firefox headless mode does not support webgl required by maplibre");- const bbox = { west: 4.4857, south: 51.9162, east: 4.4865, north: 51.9167 };+ const bbox = { west: 4.4859, south: 51.9163, east: 4.4864, north: 51.9166 };const assetInfos = (await manager.api.AssetModelResource.getAssetInfos()).data;const assets = getAssetsForAllTypes(assetInfos, { bbox });- const outlierbbox = { west: 4.483812, south: 51.916359, east: 4.484017, north: 51.916495 };- assets.push({ ...assignRandomLocationInArea(getAssetAt(assetInfos), outlierbbox), name: "outlier", realm: "smartcity" });+ const outlier = assignLocation(getAssetAt(assetInfos), [4.48106646473, 51.9163295344]);+ assets.push({ ...outlier, name: "outlier", realm: "smartcity" });await manager.setup("smartcity", { assets });await manager.goToRealmStartPage("smartcity");@@ -279,18 +280,18 @@ test.describe("Marker clustering", () => {await page.locator("or-map").evaluate((map: OrMap) => map.flyTo(undefined, 10));const clusterMarker = page.locator("or-cluster-marker");- const coords = await clusterMarker.evaluate((marker: OrClusterMarker) => [marker.lng,marker.lat]);+ await expect(clusterMarker).toHaveCount(1);+ const coords = await clusterMarker.evaluate((marker: OrClusterMarker) => [marker.lng!, marker.lat!]);await expect(clusterMarker.locator("text")).toContainText(`${assets.length}`);const assetTypes = getAssetTypes(assets);expect(await clusterMarker.locator("path[fill]").evaluateAll(el => el.map(e => e.getAttribute("fill")!))).toStrictEqual(assetTypes.map((t) => "#" + getAssetTypeColour(t, assetInfos)));- // await page.locator("or-map").evaluate((map: OrMap) => map.flyTo([4.482259693115793, 51.91756799273], 17));- await page.locator("or-map").evaluate((map: OrMap, [lng,lat]) => map.flyTo([lng!,lat!], 16.1), coords);-+ await page.locator("or-map").evaluate((map: OrMap, [lat, lng]) => map.flyTo([lat, lng], 16), coords);await expect(clusterMarker.locator("text")).toContainText(`${assets.length}`);await expect(page.locator('.or-map-marker')).toHaveCount(1);+ await expect(clusterMarker).toHaveCount(1);await clusterMarker.click();await expect(clusterMarker).not.toBeVisible();diff --git a/ui/component/or-map/src/mapwidget.ts b/ui/component/or-map/src/mapwidget.tsindex 1b7f7fc72..eeefa14c1 100644--- a/ui/component/or-map/src/mapwidget.ts+++ b/ui/component/or-map/src/mapwidget.ts@@ -88,10 +88,20 @@ export class MapWidget {this._clusterConfig = clusterConfig;}- protected _onMove = () => debounce(this._updateMarkers);- protected _onMoveEnd = () => this._updateMarkers();+ protected _onMove = () => this._updateMarkers();+ protected _onMoveEnd = (e: any) => {+ // Ensure marker updates happen after the frame+ requestAnimationFrame(() => {+ // On WebKit browsers the clicked marker may not be removed,+ // if the camera zoom level is directly between 2 levels+ if (e.marker instanceof OrClusterMarker) {+ this._clearMarker(e.marker._clusterId);+ }+ this._updateMarkers();+ });+ };protected _onData = (e: MapSourceDataEvent) => {- if (this._map && e.isSourceLoaded && e.sourceId === "mapPoints") {+ if (this._map && e.isSourceLoaded && e.sourceId === "mapPoints") {this._map.on('move', this._onMove);this._map.on('moveend', this._onMoveEnd);this._updateMarkers();@@ -616,6 +626,11 @@ export class MapWidget {}}+ protected _clearMarker(id: string) {+ this._markersOnScreen[id].remove();+ delete this._assetsOnScreen[id];+ }+protected _updateMarkers() {if (!this._map) return;@@ -669,8 +684,7 @@ export class MapWidget {if (!marker|| marker._element instanceof OrClusterMarker && !marker._element.hasTypes(Object.keys(this._assetTypeColors))) {- this._markersOnScreen[id].remove();- delete this._assetsOnScreen[id];+ this._clearMarker(id);}}this._markersOnScreen = newMarkers;diff --git a/ui/component/or-map/src/markers/or-cluster-marker.ts b/ui/component/or-map/src/markers/or-cluster-marker.tsindex e24a7c47f..291691939 100644--- a/ui/component/or-map/src/markers/or-cluster-marker.ts+++ b/ui/component/or-map/src/markers/or-cluster-marker.ts@@ -129,9 +129,8 @@ export class OrClusterMarker extends LitElement {protected async onClick() {if (this.lng && this.lat) {const zoom = await this._map.getSource<GeoJSONSource>('mapPoints')!.getClusterExpansionZoom(this._clusterId!);- // Offset 0.99 added to ensure cluster marker dissappears- // Essential is enabled to ensure animations are always run for the offset to be applied- this._map.easeTo({ center: [this.lng, this.lat], zoom: zoom + 0.99, essential: true });+ // Pass `this` down as event data so the marker can be cleared programmatically+ this._map.easeTo({ center: [this.lng, this.lat], zoom }, { marker: this });}}}