Skip to content

Commit

Permalink
Merge pull request #156 from DJMJP/master
Browse files Browse the repository at this point in the history
InvokeAsync for Polyline&Polygon
  • Loading branch information
valentasm1 authored Oct 11, 2021
2 parents 09c2fc8 + 58058c8 commit 4052183
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 2 deletions.
23 changes: 22 additions & 1 deletion GoogleMapsComponents/Maps/ListableEntityBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using OneOf;

namespace GoogleMapsComponents.Maps
{
Expand All @@ -14,7 +15,7 @@ public class ListableEntityBase<TEntityOptions> : IDisposable, IJsObjectRef
public readonly Dictionary<string, List<MapEventListener>> EventListeners;

public Guid Guid => _jsObjectRef.Guid;

internal ListableEntityBase(JsObjectRef jsObjectRef)
{
_jsObjectRef = jsObjectRef;
Expand Down Expand Up @@ -98,5 +99,25 @@ public virtual async Task ClearListeners(string eventName)
EventListeners[eventName].Clear();
}
}

public Task InvokeAsync(string functionName, params object[] args)
{
return _jsObjectRef.InvokeAsync(functionName, args);
}

public Task<T> InvokeAsync<T>(string functionName, params object[] args)
{
return _jsObjectRef.InvokeAsync<T>(functionName, args);
}

public Task<OneOf<T, U>> InvokeAsync<T, U>(string functionName, params object[] args)
{
return _jsObjectRef.InvokeAsync<T, U>(functionName, args);
}

public Task<OneOf<T, U, V>> InvokeAsync<T, U, V>(string functionName, params object[] args)
{
return _jsObjectRef.InvokeAsync<T, U, V>(functionName, args);
}
}
}
23 changes: 23 additions & 0 deletions GoogleMapsComponents/Maps/Polygon.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.JSInterop;
using OneOf;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -10,6 +11,8 @@ public class Polygon : IDisposable
{
protected readonly JsObjectRef _jsObjectRef;
private Map _map;

public Guid Guid => _jsObjectRef.Guid;

/// <summary>
/// Create a polygon using the passed PolygonOptions, which specify the polygon's path, the stroke style for the polygon's edges, and the fill style for the polygon's interior regions.
Expand Down Expand Up @@ -180,6 +183,26 @@ public Task SetVisible(bool visible)
visible);
}

public Task InvokeAsync(string functionName, params object[] args)
{
return _jsObjectRef.InvokeAsync(functionName, args);
}

public Task<T> InvokeAsync<T>(string functionName, params object[] args)
{
return _jsObjectRef.InvokeAsync<T>(functionName, args);
}

public Task<OneOf<T, U>> InvokeAsync<T, U>(string functionName, params object[] args)
{
return _jsObjectRef.InvokeAsync<T, U>(functionName, args);
}

public Task<OneOf<T, U, V>> InvokeAsync<T, U, V>(string functionName, params object[] args)
{
return _jsObjectRef.InvokeAsync<T, U, V>(functionName, args);
}

public async Task<MapEventListener> AddListener(string eventName, Action handler)
{
var listenerRef = await _jsObjectRef.InvokeWithReturnedObjectRefAsync(
Expand Down
36 changes: 35 additions & 1 deletion ServerSideDemo/Pages/MapPolyline.razor
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
@page "/mapPolyline"
@using GoogleMapsComponents
@using GoogleMapsComponents.Maps
@using System.Diagnostics

<h1>Polylines</h1>

Expand Down Expand Up @@ -72,6 +71,22 @@

path.Clear();

// Add Listener to Paths from Polyline
await polyline.InvokeAsync("AddListeners").ConfigureAwait(true);

await polyline.AddListener("insert_at", async () =>
{
path = (await polyline.GetPath().ConfigureAwait(true)).ToList();
});
await polyline.AddListener("set_at", async () =>
{
path = (await polyline.GetPath().ConfigureAwait(true)).ToList();
});
await polyline.AddListener("remove_at", async () =>
{
path = (await polyline.GetPath().ConfigureAwait(true)).ToList();
});

await polyline.AddListener("click", () =>
{
_events.Insert(0, "click polyline");
Expand Down Expand Up @@ -125,6 +140,22 @@
Map = map1.InteropObject
});

// Add Listener to Paths from Polyline
await polygon.InvokeAsync("AddListeners").ConfigureAwait(true);

await polygon.AddListener("insert_at", async () =>
{
path = (await polygon.GetPath().ConfigureAwait(true)).ToList();
});
await polygon.AddListener("set_at", async () =>
{
path = (await polygon.GetPath().ConfigureAwait(true)).ToList();
});
await polygon.AddListener("remove_at", async () =>
{
path = (await polygon.GetPath().ConfigureAwait(true)).ToList();
});

path.Clear();
}

Expand Down Expand Up @@ -172,6 +203,9 @@
//Debug.WriteLine($"Path length : {path.Count()}");
await polyline.SetPath(path);

// Add Listener to Paths from Polyline
await polyline.InvokeAsync("AddListeners").ConfigureAwait(true);
}
else if (polygon != null)
{
Expand Down
63 changes: 63 additions & 0 deletions ServerSideDemo/wwwroot/js/serverSideScripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,67 @@
div.innerHTML = '<img src="' + icon + '"> ' + name;
legend.appendChild(div);
}
}

google.maps.Polyline.prototype.AddListeners = function()
{
// getpath
console.log('Add Listeners Called.');
var poly = this;
var path = this.getPath();

// addlistener insert_at
event.initEvent('insert_at', true, true);
google.maps.event.addListener(path, 'insert_at', function(vertex) {
// event auf polyline auslösen
console.log('Vertex ' + vertex + ' inserted to path.');
google.maps.event.trigger(poly, 'insert_at', vertex);
});

// addlistener set_at
event.initEvent('set_at', true, true);
google.maps.event.addListener(path, 'set_at', function(vertex) {
// event auf polyline auslösen
console.log('Vertex ' + vertex + ' set on path.');
google.maps.event.trigger(poly, 'set_at', vertex);
});

// addlistener remove_at
event.initEvent('remove_at', true, true);
google.maps.event.addListener(path, 'remove_at', function(vertex) {
// event auf polyline auslösen
console.log('Vertex ' + vertex + ' removed from path.');
google.maps.event.trigger(poly, 'remove_at', vertex);
});
}
google.maps.Polygon.prototype.AddListeners = function()
{
// getpath
console.log('Add Listeners Called.');
var poly = this;
var path = this.getPath();

// addlistener insert_at
event.initEvent('insert_at', true, true);
google.maps.event.addListener(path, 'insert_at', function(vertex) {
// event auf polyline auslösen
console.log('Vertex ' + vertex + ' inserted to path.');
google.maps.event.trigger(poly, 'insert_at', vertex);
});

// addlistener set_at
event.initEvent('set_at', true, true);
google.maps.event.addListener(path, 'set_at', function(vertex) {
// event auf polyline auslösen
console.log('Vertex ' + vertex + ' set on path.');
google.maps.event.trigger(poly, 'set_at', vertex);
});

// addlistener remove_at
event.initEvent('remove_at', true, true);
google.maps.event.addListener(path, 'remove_at', function(vertex) {
// event auf polyline auslösen
console.log('Vertex ' + vertex + ' removed from path.');
google.maps.event.trigger(poly, 'remove_at', vertex);
});
}

0 comments on commit 4052183

Please sign in to comment.