2020-05-03 17:22:33 +08:00
|
|
|
|
using System;
|
2019-12-16 18:48:03 +08:00
|
|
|
|
using System.Collections.Generic;
|
2020-04-22 19:37:06 +08:00
|
|
|
|
using System.Text.Json;
|
2019-12-16 18:48:03 +08:00
|
|
|
|
using Microsoft.JSInterop;
|
|
|
|
|
|
|
|
|
|
namespace AntBlazor.JsInterop
|
|
|
|
|
{
|
|
|
|
|
public class DomEventService
|
|
|
|
|
{
|
2020-04-23 17:13:56 +08:00
|
|
|
|
private Dictionary<string, Func<object>> _domEventListeners = new Dictionary<string, Func<object>>();
|
2019-12-16 18:48:03 +08:00
|
|
|
|
|
|
|
|
|
private readonly IJSRuntime _jsRuntime;
|
|
|
|
|
|
|
|
|
|
public DomEventService(IJSRuntime jsRuntime)
|
|
|
|
|
{
|
|
|
|
|
_jsRuntime = jsRuntime;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-22 19:37:06 +08:00
|
|
|
|
private void AddEventListenerInternal<T>(string dom, string eventName, Action<T> callback)
|
2019-12-16 18:48:03 +08:00
|
|
|
|
{
|
2020-04-23 17:13:56 +08:00
|
|
|
|
if (!_domEventListeners.ContainsKey($"{dom}-{eventName}"))
|
2019-12-16 18:48:03 +08:00
|
|
|
|
{
|
2020-03-11 16:40:07 +08:00
|
|
|
|
_jsRuntime.InvokeAsync<string>(JSInteropConstants.addDomEventListener, dom, eventName, DotNetObjectReference.Create(new Invoker<T>((p) =>
|
2020-04-22 19:37:06 +08:00
|
|
|
|
{
|
|
|
|
|
callback?.Invoke(p);
|
|
|
|
|
})));
|
2019-12-16 18:48:03 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-22 19:37:06 +08:00
|
|
|
|
|
2020-05-03 17:22:33 +08:00
|
|
|
|
private void AddEventListenerToFirstChildInternal<T>(string dom, string eventName, Action<T> callback)
|
|
|
|
|
{
|
|
|
|
|
if (!_domEventListeners.ContainsKey($"{dom}-{eventName}"))
|
|
|
|
|
{
|
|
|
|
|
_jsRuntime.InvokeAsync<string>(JSInteropConstants.addDomEventListenerToFirstChild, dom, eventName, DotNetObjectReference.Create(new Invoker<T>((p) =>
|
|
|
|
|
{
|
|
|
|
|
callback?.Invoke(p);
|
|
|
|
|
})));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-22 19:37:06 +08:00
|
|
|
|
public void AddEventListener(string dom, string eventName, Action<JsonElement> callback)
|
|
|
|
|
{
|
|
|
|
|
AddEventListenerInternal<string>(dom, eventName, (e) =>
|
|
|
|
|
{
|
|
|
|
|
JsonElement jsonElement = JsonDocument.Parse(e).RootElement;
|
|
|
|
|
callback(jsonElement);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void AddEventListener<T>(string dom, string eventName, Action<T> callback)
|
|
|
|
|
{
|
|
|
|
|
AddEventListenerInternal<string>(dom, eventName, (e) =>
|
|
|
|
|
{
|
|
|
|
|
T obj = JsonSerializer.Deserialize<T>(e);
|
|
|
|
|
callback(obj);
|
|
|
|
|
});
|
|
|
|
|
}
|
2020-05-03 17:22:33 +08:00
|
|
|
|
|
|
|
|
|
public void AddEventListenerToFirstChild(string dom, string eventName, Action<JsonElement> callback)
|
|
|
|
|
{
|
|
|
|
|
AddEventListenerToFirstChildInternal<string>(dom, eventName, (e) =>
|
|
|
|
|
{
|
|
|
|
|
JsonElement jsonElement = JsonDocument.Parse(e).RootElement;
|
|
|
|
|
callback(jsonElement);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void AddEventListenerToFirstChild<T>(string dom, string eventName, Action<T> callback)
|
|
|
|
|
{
|
|
|
|
|
AddEventListenerToFirstChildInternal<string>(dom, eventName, (e) =>
|
|
|
|
|
{
|
|
|
|
|
T obj = JsonSerializer.Deserialize<T>(e);
|
|
|
|
|
callback(obj);
|
|
|
|
|
});
|
|
|
|
|
}
|
2019-12-16 18:48:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-11 16:40:07 +08:00
|
|
|
|
public class Invoker<T>
|
2019-12-16 18:48:03 +08:00
|
|
|
|
{
|
2020-04-23 17:13:56 +08:00
|
|
|
|
private Action<T> _action;
|
2019-12-16 18:48:03 +08:00
|
|
|
|
|
2020-03-11 16:40:07 +08:00
|
|
|
|
public Invoker(Action<T> invoker)
|
2019-12-16 18:48:03 +08:00
|
|
|
|
{
|
2020-04-23 17:13:56 +08:00
|
|
|
|
this._action = invoker;
|
2019-12-16 18:48:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[JSInvokable]
|
2020-03-11 16:40:07 +08:00
|
|
|
|
public void Invoke(T param)
|
2019-12-16 18:48:03 +08:00
|
|
|
|
{
|
2020-04-23 17:13:56 +08:00
|
|
|
|
_action.Invoke(param);
|
2019-12-16 18:48:03 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-23 17:13:56 +08:00
|
|
|
|
}
|