Skip to content

Commit

Permalink
Add support of GetClusterExpansionZoom
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnaud Leclerc committed Mar 27, 2024
1 parent ab189b6 commit c08f267
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
if(cluster is not null && cluster.Properties.ContainsKey("cluster"))
{
var clusterLeaves = await _datasource.GetClusterLeavesAsync(int.Parse(cluster.Id), 10, 0);
var clusterZoomExpansionLevel = await _datasource.GetClusterExpansionZoomAsync(int.Parse(cluster.Id));

var html = string.Join(string.Empty,
new[] {
Expand Down
1 change: 1 addition & 0 deletions src/AzureMapsControl.Components/Constants/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ internal static class Datasource
{
internal const string GetShapes = "getShapes";
internal const string GetClusterLeaves = "getClusterLeaves";
internal const string GetClusterExpansionZoom = "getClusterExpansionZoom";
}

internal static class GriddedDatasource
Expand Down
19 changes: 18 additions & 1 deletion src/AzureMapsControl.Components/Data/DataSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public async ValueTask<IEnumerable<Shape<Geometry>>> GetShapesAsync()
/// <param name="clusterId">The ID of the cluster</param>
/// <param name="limit">The maximum number of features to return</param>
/// <param name="offset">The number of shapes to skip. Allows you to page through the shapes in the cluster</param>
/// <returns></returns>
/// <returns>Shapes that are within the cluster</returns>
public async ValueTask<IEnumerable<Feature<Geometry>>> GetClusterLeavesAsync(int clusterId, int limit, int offset)
{
Logger?.LogAzureMapsControlInfo(AzureMapLogEvent.DataSource_GetClusterLeavesAsync, "DataSource - GetClusterLeavesAsync");
Expand All @@ -113,6 +113,23 @@ public async ValueTask<IEnumerable<Feature<Geometry>>> GetClusterLeavesAsync(int
return await JSRuntime.InvokeAsync<IEnumerable<Feature<Geometry>>>(Constants.JsConstants.Methods.Datasource.GetClusterLeaves.ToDatasourceNamespace(), Id, clusterId, limit, offset);
}

/// <summary>
/// Calculates a zoom level at which the cluster starts expanding or break apart.
/// </summary>
/// <param name="clusterId">ID of the cluster</param>
/// <returns>Zoom level at which the cluster starts expanding or break apart</returns>
public async ValueTask<int> GetClusterExpansionZoomAsync(int clusterId)
{
Logger?.LogAzureMapsControlInfo(AzureMapLogEvent.DataSource_GetClusterLeavesAsync, "DataSource - GetClusterExpansionZoomAsync");
Logger?.LogAzureMapsControlDebug(AzureMapLogEvent.DataSource_GetClusterLeavesAsync, $"Id: {Id}");
Logger?.LogAzureMapsControlDebug(AzureMapLogEvent.DataSource_GetClusterLeavesAsync, $"ClusterId: {clusterId}");

EnsureJsRuntimeExists();
EnsureNotDisposed();

return await JSRuntime.InvokeAsync<int>(Constants.JsConstants.Methods.Datasource.GetClusterExpansionZoom.ToDatasourceNamespace(), Id, clusterId);
}

internal void DispatchEvent(DataSourceEventArgs eventArgs)
{
switch (eventArgs.Type)
Expand Down
12 changes: 10 additions & 2 deletions src/AzureMapsControl.Components/typescript/sources/datasource.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as azmaps from 'azure-maps-control';
import { Core } from '../core/core';
import { Shape } from '../geometries/geometry';
import { Shape, Feature } from '../geometries/geometry';

export class Datasource {

Expand All @@ -9,7 +9,7 @@ export class Datasource {
return shapes?.map(shape => Core.getSerializableShape(shape));
}

public static async getClusterLeaves(datasourceId: string, clusterId: number, limit: number, offset: number) {
public static async getClusterLeaves(datasourceId: string, clusterId: number, limit: number, offset: number): Promise<(Shape | Feature)[]> {
return new Promise(resolve => {
(Core.getMap().sources.getById(datasourceId) as azmaps.source.DataSource).getClusterLeaves(clusterId, limit, offset).then(clusterLeaves => {

Expand All @@ -28,4 +28,12 @@ export class Datasource {
});
}

public static async getClusterExpansionZoom(datasourceId: string, clusterId: number): Promise<number> {
return new Promise(resolve => {
(Core.getMap().sources.getById(datasourceId) as azmaps.source.DataSource).getClusterExpansionZoom(clusterId).then(zoom => {
resolve(zoom);
});
});
}

}
43 changes: 43 additions & 0 deletions tests/AzureMapsControl.Components.Tests/Data/DataSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1103,5 +1103,48 @@ public async Task Should_NotGetClusterLeaves_DisposedCase()

_jsRuntimeMock.VerifyNoOtherCalls();
}

[Fact]
public async Task Should_GetClusterExpansionZoom()
{
var datasource = new DataSource {
JSRuntime = _jsRuntimeMock.Object
};

var expected = 2;
_jsRuntimeMock.Setup(runtime => runtime.InvokeAsync<int>(It.IsAny<string>(), It.IsAny<object[]>())).ReturnsAsync(expected);

var clusterId = 1;
var result = await datasource.GetClusterExpansionZoomAsync(clusterId);

Assert.Equal(expected, result);

_jsRuntimeMock.Verify(runtime => runtime.InvokeAsync<int>(Constants.JsConstants.Methods.Datasource.GetClusterExpansionZoom.ToDatasourceNamespace(), datasource.Id, clusterId), Times.Once);
_jsRuntimeMock.VerifyNoOtherCalls();
}

[Fact]
public async Task Should_NotGetClusterExpansionZoom_NotAddedToMapCase()
{
var datasource = new DataSource();

await Assert.ThrowsAsync<ComponentNotAddedToMapException>(async () => await datasource.GetClusterExpansionZoomAsync(1));

_jsRuntimeMock.VerifyNoOtherCalls();
}

[Fact]
public async Task Should_NotGetClusterExpansionZoom_DisposedCase()
{
var datasource = new DataSource {
JSRuntime = _jsRuntimeMock.Object
};

await datasource.DisposeAsync();

await Assert.ThrowsAsync<ComponentDisposedException>(async () => await datasource.GetClusterExpansionZoomAsync(1));

_jsRuntimeMock.VerifyNoOtherCalls();
}
}
}

0 comments on commit c08f267

Please sign in to comment.