Added plugin events
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
using HopFrame.Core.Config;
|
||||
|
||||
namespace HopFrame.Core.Events;
|
||||
namespace HopFrame.Core.Callbacks;
|
||||
|
||||
public static class EventTypes {
|
||||
public static class CallbackTypes {
|
||||
|
||||
private const string Prefix = "HopFrame.";
|
||||
private const string CreateEntryPrefix = Prefix + "Entry.Create.";
|
||||
@@ -13,18 +13,18 @@ public static class EventTypes {
|
||||
public static string UpdateEntry(TableConfig config) => UpdateEntryPrefix + config.PropertyName;
|
||||
public static string DeleteEntry(TableConfig config) => DeleteEntryPrefix + config.PropertyName;
|
||||
|
||||
public static string ConstructEventName(EventType type, TableConfig config) {
|
||||
public static string ConstructCallbackName(CallbackType type, TableConfig config) {
|
||||
return type switch {
|
||||
EventType.CreateEntry => CreateEntry(config),
|
||||
EventType.UpdateEntry => UpdateEntry(config),
|
||||
EventType.DeleteEntry => DeleteEntry(config),
|
||||
CallbackType.CreateEntry => CreateEntry(config),
|
||||
CallbackType.UpdateEntry => UpdateEntry(config),
|
||||
CallbackType.DeleteEntry => DeleteEntry(config),
|
||||
_ => Prefix
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public enum EventType {
|
||||
public enum CallbackType {
|
||||
CreateEntry = 0,
|
||||
UpdateEntry = 1,
|
||||
DeleteEntry = 2
|
||||
@@ -1,6 +1,6 @@
|
||||
namespace HopFrame.Core.Events;
|
||||
namespace HopFrame.Core.Callbacks;
|
||||
|
||||
public readonly struct HopEventHandler(string eventType, Func<object, IServiceProvider, Task> handler) {
|
||||
public readonly struct HopCallbackHandler(string eventType, Func<object, IServiceProvider, Task> handler) {
|
||||
public Guid Id { get; } = Guid.CreateVersion7();
|
||||
public Func<object, IServiceProvider, Task> Handler { get; } = handler;
|
||||
public string EventType { get; } = eventType;
|
||||
14
src/HopFrame.Core/Callbacks/ICallbackEmitter.cs
Normal file
14
src/HopFrame.Core/Callbacks/ICallbackEmitter.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
namespace HopFrame.Core.Callbacks;
|
||||
|
||||
public interface ICallbackEmitter {
|
||||
|
||||
Guid RegisterCallbackHandler(string @event, Func<object, IServiceProvider, Task> handler);
|
||||
|
||||
bool RemoveCallbackHandler(Guid id);
|
||||
|
||||
Task DispatchCallback(string @event, object argument = null!);
|
||||
|
||||
void RemoveAllCallbackHandlers(string @event);
|
||||
void RemoveAllCallbackHandlers();
|
||||
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
using HopFrame.Core.Events;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace HopFrame.Core.Config;
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using HopFrame.Core.Events;
|
||||
using HopFrame.Core.Callbacks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace HopFrame.Core.Config;
|
||||
|
||||
@@ -8,18 +9,20 @@ public class HopFrameConfig {
|
||||
public bool DisplayUserInfo { get; set; } = true;
|
||||
public string? BasePolicy { get; set; }
|
||||
public string? LoginPageRewrite { get; set; }
|
||||
public List<HopEventHandler> Handlers { get; } = new();
|
||||
public List<HopCallbackHandler> Handlers { get; } = new();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A helper class for editing the <see cref="HopFrameConfig"/>
|
||||
/// </summary>
|
||||
public class HopFrameConfigurator(HopFrameConfig config) {
|
||||
public class HopFrameConfigurator(HopFrameConfig config, IServiceCollection collection = null!) {
|
||||
|
||||
/// <summary>
|
||||
/// The Internal HopFrame configuration that's modified by the helper functions
|
||||
/// </summary>
|
||||
public HopFrameConfig InnerConfig { get; } = config;
|
||||
|
||||
public IServiceCollection ServiceCollection { get; } = collection;
|
||||
|
||||
/// <summary>
|
||||
/// Adds all tables defined in the DbContext to the HopFrame ui and configures it using the provided configurator
|
||||
@@ -45,6 +48,18 @@ public class HopFrameConfigurator(HopFrameConfig config) {
|
||||
return new DbContextConfigurator<TDbContext>(context);
|
||||
}
|
||||
|
||||
public bool HasDbContext<TDbContext>() where TDbContext : DbContext {
|
||||
return InnerConfig.Contexts.Any(context => context.ContextType == typeof(TDbContext));
|
||||
}
|
||||
|
||||
public DbContextConfigurator<TDbContext>? GetDbContext<TDbContext>() where TDbContext : DbContext {
|
||||
var config = InnerConfig.Contexts
|
||||
.SingleOrDefault(context => context.ContextType == typeof(TDbContext));
|
||||
if (config is null) return null;
|
||||
|
||||
return new DbContextConfigurator<TDbContext>(config);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines if the name of the currently logged-in user should be displayed in the top right corner of the admin ui
|
||||
/// </summary>
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
using HopFrame.Core.Events;
|
||||
using HopFrame.Core.Callbacks;
|
||||
|
||||
namespace HopFrame.Core.Config;
|
||||
|
||||
@@ -191,26 +191,26 @@ public class TableConfigurator<TModel>(TableConfig config) {
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds an event handler of the provided type
|
||||
/// Adds a callback handler of the provided type
|
||||
/// </summary>
|
||||
/// <param name="type">The type of event that triggers the handler</param>
|
||||
/// <param name="type">The type of callback that triggers the handler</param>
|
||||
/// <param name="handler">The handler delegate</param>
|
||||
public TableConfigurator<TModel> AddEventHandler(EventType type, Func<TModel, IServiceProvider, Task> handler) {
|
||||
var eventName = EventTypes.ConstructEventName(type, InnerConfig);
|
||||
var handlerStore = new HopEventHandler(eventName, (o, provider) => handler.Invoke((TModel)o, provider));
|
||||
public TableConfigurator<TModel> AddCallbackHandler(CallbackType type, Func<TModel, IServiceProvider, Task> handler) {
|
||||
var eventName = CallbackTypes.ConstructCallbackName(type, InnerConfig);
|
||||
var handlerStore = new HopCallbackHandler(eventName, (o, provider) => handler.Invoke((TModel)o, provider));
|
||||
InnerConfig.ContextConfig.ParentConfig.Handlers.Add(handlerStore);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds an event handler of the provided type
|
||||
/// Adds a callback handler of the provided type
|
||||
/// </summary>
|
||||
/// <param name="type">The type of event that triggers the handler</param>
|
||||
/// <param name="type">The type of callback that triggers the handler</param>
|
||||
/// <param name="handler">The handler delegate</param>
|
||||
public TableConfigurator<TModel> AddEventHandler(EventType type, Action<TModel, IServiceProvider> handler) {
|
||||
var eventName = EventTypes.ConstructEventName(type, InnerConfig);
|
||||
var handlerStore = new HopEventHandler(eventName, (o, provider) => {
|
||||
public TableConfigurator<TModel> AddCallbackHandler(CallbackType type, Action<TModel, IServiceProvider> handler) {
|
||||
var eventName = CallbackTypes.ConstructCallbackName(type, InnerConfig);
|
||||
var handlerStore = new HopCallbackHandler(eventName, (o, provider) => {
|
||||
handler.Invoke((TModel)o, provider);
|
||||
return Task.CompletedTask;
|
||||
});
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
namespace HopFrame.Core.Events;
|
||||
|
||||
public interface IEventEmitter {
|
||||
|
||||
Guid RegisterEventHandler(string @event, Func<object, IServiceProvider, Task> handler);
|
||||
|
||||
bool RemoveEventHandler(Guid id);
|
||||
|
||||
Task DispatchEvent(string @event, object argument = null!);
|
||||
|
||||
void RemoveAllEventHandlers(string @event);
|
||||
void RemoveAllEventHandlers();
|
||||
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
using HopFrame.Core.Events;
|
||||
using HopFrame.Core.Callbacks;
|
||||
using HopFrame.Core.Services;
|
||||
using HopFrame.Core.Services.Implementations;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
@@ -16,7 +16,7 @@ public static class ServiceCollectionExtensions {
|
||||
public static IServiceCollection AddHopFrameServices(this IServiceCollection services) {
|
||||
services.AddScoped<IContextExplorer, ContextExplorer>();
|
||||
services.TryAddScoped<IHopFrameAuthHandler, DefaultAuthHandler>();
|
||||
services.TryAddScoped<IEventEmitter, EventEmitter>();
|
||||
services.TryAddScoped<ICallbackEmitter, CallbackEmitter>();
|
||||
return services;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,22 +1,22 @@
|
||||
using HopFrame.Core.Config;
|
||||
using HopFrame.Core.Events;
|
||||
using HopFrame.Core.Callbacks;
|
||||
|
||||
namespace HopFrame.Core.Services.Implementations;
|
||||
|
||||
internal sealed class EventEmitter(IServiceProvider provider, HopFrameConfig config) : IEventEmitter {
|
||||
internal sealed class CallbackEmitter(IServiceProvider provider, HopFrameConfig config) : ICallbackEmitter {
|
||||
|
||||
public Guid RegisterEventHandler(string @event, Func<object, IServiceProvider, Task> handler) {
|
||||
var handlerStore = new HopEventHandler(@event, handler);
|
||||
public Guid RegisterCallbackHandler(string @event, Func<object, IServiceProvider, Task> handler) {
|
||||
var handlerStore = new HopCallbackHandler(@event, handler);
|
||||
config.Handlers.Add(handlerStore);
|
||||
return handlerStore.Id;
|
||||
}
|
||||
|
||||
public bool RemoveEventHandler(Guid id) {
|
||||
public bool RemoveCallbackHandler(Guid id) {
|
||||
var count = config.Handlers.RemoveAll(handler => handler.Id == id);
|
||||
return count > 0;
|
||||
}
|
||||
|
||||
public async Task DispatchEvent(string @event, object argument = null!) {
|
||||
public async Task DispatchCallback(string @event, object argument = null!) {
|
||||
var handlers = config.Handlers.Where(handler => handler.EventType == @event);
|
||||
var tasks = new List<Task>();
|
||||
|
||||
@@ -28,11 +28,11 @@ internal sealed class EventEmitter(IServiceProvider provider, HopFrameConfig con
|
||||
await Task.WhenAll(tasks);
|
||||
}
|
||||
|
||||
public void RemoveAllEventHandlers(string @event) {
|
||||
public void RemoveAllCallbackHandlers(string @event) {
|
||||
config.Handlers.RemoveAll(handler => handler.EventType == @event);
|
||||
}
|
||||
|
||||
public void RemoveAllEventHandlers() {
|
||||
public void RemoveAllCallbackHandlers() {
|
||||
config.Handlers.Clear();
|
||||
}
|
||||
|
||||
@@ -6,6 +6,8 @@
|
||||
@using HopFrame.Core.Services
|
||||
@using HopFrame.Web.Models
|
||||
@using HopFrame.Web.Helpers
|
||||
@using HopFrame.Web.Plugins
|
||||
@using HopFrame.Web.Plugins.Events
|
||||
|
||||
<FluentDialogBody>
|
||||
@foreach (var property in Content.Config.Properties.Where(prop => !prop.IsListingProperty).OrderBy(prop => prop.Order)) {
|
||||
@@ -169,6 +171,7 @@
|
||||
@inject IHopFrameAuthHandler Handler
|
||||
@inject IToastService Toasts
|
||||
@inject IServiceProvider Provider
|
||||
@inject IPluginOrchestrator PluginOrchestrator
|
||||
|
||||
@code {
|
||||
[Parameter]
|
||||
@@ -374,6 +377,14 @@
|
||||
|
||||
if (value is null && property.IsRequired)
|
||||
errorList.Add($"{property.Name} is required");
|
||||
|
||||
var eventResult = await PluginOrchestrator.DispatchEvent(new ValidationEvent(this) {
|
||||
Errors = errorList,
|
||||
Property = property,
|
||||
Table = Content.Config
|
||||
});
|
||||
|
||||
if (eventResult.IsCanceled) return false;
|
||||
}
|
||||
|
||||
StateHasChanged();
|
||||
|
||||
@@ -4,9 +4,11 @@
|
||||
@implements IDisposable
|
||||
|
||||
@using HopFrame.Core.Config
|
||||
@using HopFrame.Core.Events
|
||||
@using HopFrame.Core.Callbacks
|
||||
@using HopFrame.Core.Services
|
||||
@using HopFrame.Web.Models
|
||||
@using HopFrame.Web.Plugins
|
||||
@using HopFrame.Web.Plugins.Events
|
||||
@using Microsoft.JSInterop
|
||||
@using Microsoft.EntityFrameworkCore
|
||||
|
||||
@@ -118,7 +120,8 @@
|
||||
@inject IJSRuntime Js
|
||||
@inject IDialogService Dialogs
|
||||
@inject IHopFrameAuthHandler Handler
|
||||
@inject IEventEmitter Emitter
|
||||
@inject ICallbackEmitter Emitter
|
||||
@inject IPluginOrchestrator PluginOrchestrator
|
||||
|
||||
@code {
|
||||
|
||||
@@ -199,11 +202,28 @@
|
||||
_searchCancel = new();
|
||||
|
||||
await Task.Delay(500, _searchCancel.Token);
|
||||
|
||||
var eventResult = await PluginOrchestrator.DispatchEvent(new SearchEvent(this) {
|
||||
SearchTerm = _searchTerm,
|
||||
Table = _config!
|
||||
});
|
||||
if (eventResult.IsCanceled) return;
|
||||
_searchTerm = eventResult.SearchTerm;
|
||||
|
||||
await Reload();
|
||||
}
|
||||
|
||||
private async Task Reload() {
|
||||
_loading = true;
|
||||
|
||||
var eventResult = await PluginOrchestrator.DispatchEvent(new ReloadEvent(this) {
|
||||
Table = _config!
|
||||
});
|
||||
if (eventResult.IsCanceled) {
|
||||
_loading = false;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(_searchTerm)) {
|
||||
(var query, _totalPages) = await _manager!.Search(_searchTerm, 0, PerPage);
|
||||
_currentlyDisplayedModels = query.ToArray();
|
||||
@@ -215,6 +235,15 @@
|
||||
}
|
||||
|
||||
private async Task ChangePage(int page) {
|
||||
var eventResult = await PluginOrchestrator.DispatchEvent(new PageChangeEvent(this) {
|
||||
CurrentPage = _currentPage,
|
||||
NewPage = page,
|
||||
TotalPages = _totalPages,
|
||||
Table = _config!
|
||||
});
|
||||
if (eventResult.IsCanceled) return;
|
||||
page = eventResult.NewPage;
|
||||
|
||||
if (page < 0 || page > _totalPages - 1) return;
|
||||
_currentPage = page;
|
||||
await Reload();
|
||||
@@ -225,13 +254,19 @@
|
||||
Navigator.NavigateTo("/admin", true);
|
||||
return;
|
||||
}
|
||||
|
||||
var eventResult = await PluginOrchestrator.DispatchEvent(new DeleteEntryEvent(this) {
|
||||
Entity = element,
|
||||
Table = _config!
|
||||
});
|
||||
if (eventResult.IsCanceled) return;
|
||||
|
||||
var dialog = await Dialogs.ShowConfirmationAsync("Do you really want to delete this entry?");
|
||||
var result = await dialog.Result;
|
||||
if (result.Cancelled) return;
|
||||
|
||||
await _manager!.DeleteItem(element);
|
||||
await Emitter.DispatchEvent(EventTypes.DeleteEntry(_config!), element);
|
||||
await Emitter.DispatchCallback(CallbackTypes.DeleteEntry(_config!), element);
|
||||
await Reload();
|
||||
}
|
||||
|
||||
@@ -240,6 +275,22 @@
|
||||
Navigator.NavigateTo("/admin", true);
|
||||
return;
|
||||
}
|
||||
|
||||
HopFrameTablePageEventArgs eventArgs;
|
||||
if (element is null) {
|
||||
eventArgs = new CreateEntryEvent(this) {
|
||||
Table = _config!
|
||||
};
|
||||
}
|
||||
else {
|
||||
eventArgs = new UpdateEntryEvent(this) {
|
||||
Table = _config!,
|
||||
Entity = element
|
||||
};
|
||||
}
|
||||
|
||||
var eventResult = await PluginOrchestrator.DispatchEvent(eventArgs);
|
||||
if (eventResult.IsCanceled) return;
|
||||
|
||||
var panel = await Dialogs.ShowPanelAsync<HopFrameEditor>(new EditorDialogData(_config!, element), new DialogParameters {
|
||||
TrapFocus = false
|
||||
@@ -251,17 +302,25 @@
|
||||
|
||||
if (element is null) {
|
||||
await _manager!.AddItem(data!.CurrentObject!);
|
||||
await Emitter.DispatchEvent(EventTypes.CreateEntry(_config!), data.CurrentObject!);
|
||||
await Emitter.DispatchCallback(CallbackTypes.CreateEntry(_config!), data.CurrentObject!);
|
||||
}
|
||||
else {
|
||||
await _manager!.EditItem(data!.CurrentObject!);
|
||||
await Emitter.DispatchEvent(EventTypes.UpdateEntry(_config!), data.CurrentObject!);
|
||||
await Emitter.DispatchCallback(CallbackTypes.UpdateEntry(_config!), data.CurrentObject!);
|
||||
}
|
||||
|
||||
await Reload();
|
||||
}
|
||||
|
||||
private void SelectItem(object item, bool selected) {
|
||||
var eventResult = PluginOrchestrator.DispatchEvent(new SelectEntryEvent(this) {
|
||||
Entity = item,
|
||||
Selected = selected,
|
||||
Table = _config!
|
||||
}).Result;
|
||||
if (eventResult.IsCanceled) return;
|
||||
selected = eventResult.Selected;
|
||||
|
||||
if (!selected)
|
||||
DialogData!.SelectedObjects.Remove(item);
|
||||
else DialogData!.SelectedObjects.Add(item);
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
using HopFrame.Core.Config;
|
||||
using System.Reflection;
|
||||
using HopFrame.Core.Config;
|
||||
using HopFrame.Web.Components.Layout;
|
||||
using HopFrame.Web.Models;
|
||||
using HopFrame.Web.Plugins;
|
||||
using HopFrame.Web.Plugins.Annotations;
|
||||
using HopFrame.Web.Plugins.Internal;
|
||||
|
||||
namespace HopFrame.Web;
|
||||
|
||||
@@ -28,5 +32,23 @@ public static class HopFrameConfiguratorExtensions {
|
||||
configuratorDelegate.Invoke(viewConfigurator);
|
||||
return configurator;
|
||||
}
|
||||
|
||||
public static HopFrameConfigurator AddPlugin<TPlugin>(this HopFrameConfigurator configurator) where TPlugin : HopFramePlugin {
|
||||
PluginOrchestrator.RegisterPlugin(configurator.ServiceCollection, typeof(TPlugin));
|
||||
|
||||
var methods = typeof(TPlugin).GetMethods()
|
||||
.Where(method => method.IsStatic)
|
||||
.Where(method => method.GetCustomAttributes(true)
|
||||
.Any(attr => attr is PluginConfiguratorAttribute))
|
||||
.Where(method => method.GetParameters().Length < 2);
|
||||
|
||||
foreach (var method in methods) {
|
||||
if (method.GetParameters().Length > 0)
|
||||
method.Invoke(null, [configurator]);
|
||||
else method.Invoke(null, []);
|
||||
}
|
||||
|
||||
return configurator;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace HopFrame.Web.Plugins.Annotations;
|
||||
|
||||
[AttributeUsage(AttributeTargets.Method)]
|
||||
public class EventHandlerAttribute : Attribute;
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace HopFrame.Web.Plugins.Annotations;
|
||||
|
||||
/// <summary>
|
||||
/// Configures the method as a plugin configurator, so the method gets called, when the plugin is registered.
|
||||
/// Only works on static methods
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Method)]
|
||||
public class PluginConfiguratorAttribute : Attribute;
|
||||
18
src/HopFrame.Web/Plugins/Events/EntryEvent.cs
Normal file
18
src/HopFrame.Web/Plugins/Events/EntryEvent.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using HopFrame.Web.Components.Pages;
|
||||
|
||||
namespace HopFrame.Web.Plugins.Events;
|
||||
|
||||
public sealed class DeleteEntryEvent(HopFrameTablePage sender) : HopFrameTablePageEventArgs(sender) {
|
||||
public required object Entity { get; init; }
|
||||
}
|
||||
|
||||
public sealed class CreateEntryEvent(HopFrameTablePage sender) : HopFrameTablePageEventArgs(sender);
|
||||
|
||||
public sealed class UpdateEntryEvent(HopFrameTablePage sender) : HopFrameTablePageEventArgs(sender) {
|
||||
public required object Entity { get; init; }
|
||||
}
|
||||
|
||||
public sealed class SelectEntryEvent(HopFrameTablePage sender) : HopFrameTablePageEventArgs(sender) {
|
||||
public required object Entity { get; init; }
|
||||
public required bool Selected { get; set; }
|
||||
}
|
||||
27
src/HopFrame.Web/Plugins/Events/HopFrameEventArgs.cs
Normal file
27
src/HopFrame.Web/Plugins/Events/HopFrameEventArgs.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using HopFrame.Core.Config;
|
||||
using HopFrame.Web.Components.Dialogs;
|
||||
using HopFrame.Web.Components.Pages;
|
||||
|
||||
namespace HopFrame.Web.Plugins.Events;
|
||||
|
||||
public abstract class HopFrameEventArgs(object internalSender) {
|
||||
internal object InternalSender { get; } = internalSender;
|
||||
public bool IsCanceled { get; protected set; }
|
||||
|
||||
|
||||
public void SetCancelled(bool canceled) => IsCanceled = canceled;
|
||||
}
|
||||
|
||||
public abstract class HopFrameEventArgs<TSender>(TSender sender) : HopFrameEventArgs(sender) where TSender : class {
|
||||
public TSender Sender => (TSender)InternalSender;
|
||||
}
|
||||
|
||||
public abstract class HopFrameTablePageEventArgs(HopFrameTablePage sender)
|
||||
: HopFrameEventArgs<HopFrameTablePage>(sender) {
|
||||
public required TableConfig Table { get; init; }
|
||||
}
|
||||
|
||||
public abstract class HopFrameEditorEventArgs(HopFrameEditor sender)
|
||||
: HopFrameEventArgs<HopFrameEditor>(sender) {
|
||||
public required TableConfig Table { get; init; }
|
||||
}
|
||||
9
src/HopFrame.Web/Plugins/Events/PageChangeEvent.cs
Normal file
9
src/HopFrame.Web/Plugins/Events/PageChangeEvent.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using HopFrame.Web.Components.Pages;
|
||||
|
||||
namespace HopFrame.Web.Plugins.Events;
|
||||
|
||||
public sealed class PageChangeEvent(HopFrameTablePage sender) : HopFrameTablePageEventArgs(sender) {
|
||||
public required int CurrentPage { get; init; }
|
||||
public required int TotalPages { get; init; }
|
||||
public required int NewPage { get; set; }
|
||||
}
|
||||
9
src/HopFrame.Web/Plugins/Events/PluginEventContainer.cs
Normal file
9
src/HopFrame.Web/Plugins/Events/PluginEventContainer.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using System.Reflection;
|
||||
|
||||
namespace HopFrame.Web.Plugins.Events;
|
||||
|
||||
internal sealed class PluginEventContainer {
|
||||
public required MethodInfo Handler { get; init; }
|
||||
public required Type EventType { get; init; }
|
||||
public required bool IsAwaitable { get; init; }
|
||||
}
|
||||
7
src/HopFrame.Web/Plugins/Events/ReloadEvent.cs
Normal file
7
src/HopFrame.Web/Plugins/Events/ReloadEvent.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
using HopFrame.Web.Components.Pages;
|
||||
|
||||
namespace HopFrame.Web.Plugins.Events;
|
||||
|
||||
public sealed class ReloadEvent(HopFrameTablePage sender) : HopFrameTablePageEventArgs(sender) {
|
||||
|
||||
}
|
||||
7
src/HopFrame.Web/Plugins/Events/SearchEvent.cs
Normal file
7
src/HopFrame.Web/Plugins/Events/SearchEvent.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
using HopFrame.Web.Components.Pages;
|
||||
|
||||
namespace HopFrame.Web.Plugins.Events;
|
||||
|
||||
public sealed class SearchEvent(HopFrameTablePage sender) : HopFrameTablePageEventArgs(sender) {
|
||||
public required string SearchTerm { get; set; }
|
||||
}
|
||||
9
src/HopFrame.Web/Plugins/Events/ValidationEvent.cs
Normal file
9
src/HopFrame.Web/Plugins/Events/ValidationEvent.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using HopFrame.Core.Config;
|
||||
using HopFrame.Web.Components.Dialogs;
|
||||
|
||||
namespace HopFrame.Web.Plugins.Events;
|
||||
|
||||
public sealed class ValidationEvent(HopFrameEditor sender) : HopFrameEditorEventArgs(sender) {
|
||||
public required IList<string> Errors { get; init; }
|
||||
public required PropertyConfig Property { get; init; }
|
||||
}
|
||||
7
src/HopFrame.Web/Plugins/HopFramePlugin.cs
Normal file
7
src/HopFrame.Web/Plugins/HopFramePlugin.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace HopFrame.Web.Plugins;
|
||||
|
||||
public abstract class HopFramePlugin {
|
||||
|
||||
|
||||
|
||||
}
|
||||
5
src/HopFrame.Web/Plugins/IPluginOrchestrator.cs
Normal file
5
src/HopFrame.Web/Plugins/IPluginOrchestrator.cs
Normal file
@@ -0,0 +1,5 @@
|
||||
namespace HopFrame.Web.Plugins;
|
||||
|
||||
public interface IPluginOrchestrator {
|
||||
public Task<TEvent> DispatchEvent<TEvent>(TEvent @event, CancellationToken ct = new());
|
||||
}
|
||||
46
src/HopFrame.Web/Plugins/Internal/PluginOrchestrator.cs
Normal file
46
src/HopFrame.Web/Plugins/Internal/PluginOrchestrator.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using HopFrame.Web.Plugins.Annotations;
|
||||
using HopFrame.Web.Plugins.Events;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace HopFrame.Web.Plugins.Internal;
|
||||
|
||||
internal sealed class PluginOrchestrator(IServiceProvider services) : IPluginOrchestrator {
|
||||
|
||||
public static void RegisterPlugin(IServiceCollection collection, Type plugin) {
|
||||
var methods = plugin.GetMethods()
|
||||
.Where(method => method.GetCustomAttributes(true)
|
||||
.Any(attr => attr is EventHandlerAttribute));
|
||||
|
||||
foreach (var method in methods) {
|
||||
var awaitable = method.ReturnType.IsAssignableFrom(typeof(Task));
|
||||
var eventType = method
|
||||
.GetParameters()
|
||||
.FirstOrDefault(param => param.ParameterType.IsAssignableTo(typeof(HopFrameEventArgs)))?.ParameterType;
|
||||
|
||||
if (eventType is null) continue;
|
||||
var container = new PluginEventContainer {
|
||||
EventType = eventType,
|
||||
IsAwaitable = awaitable,
|
||||
Handler = method
|
||||
};
|
||||
collection.AddSingleton(container);
|
||||
collection.AddScoped(plugin);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<TEvent> DispatchEvent<TEvent>(TEvent @event, CancellationToken ct = new()) {
|
||||
var eventContainers = services.GetRequiredService<IEnumerable<PluginEventContainer>>()
|
||||
.Where(container => container.EventType == typeof(TEvent));
|
||||
|
||||
foreach (var container in eventContainers) {
|
||||
var plugin = services.GetRequiredService(container.Handler.DeclaringType!);
|
||||
var result = container.Handler.Invoke(plugin, [@event]);
|
||||
|
||||
if (container.IsAwaitable)
|
||||
await (Task)result!;
|
||||
}
|
||||
|
||||
return @event;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,8 +1,10 @@
|
||||
using HopFrame.Core;
|
||||
using HopFrame.Core.Config;
|
||||
using HopFrame.Core.Events;
|
||||
using HopFrame.Core.Callbacks;
|
||||
using HopFrame.Web.Components;
|
||||
using HopFrame.Web.Components.Pages;
|
||||
using HopFrame.Web.Plugins;
|
||||
using HopFrame.Web.Plugins.Internal;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.FluentUI.AspNetCore.Components;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
@@ -21,7 +23,7 @@ public static class ServiceCollectionExtensions {
|
||||
/// <returns>The same service collection that is passed in</returns>
|
||||
public static IServiceCollection AddHopFrame(this IServiceCollection services, Action<HopFrameConfigurator> configurator, LibraryConfiguration? fluentUiLibraryConfiguration = null, bool addRazorComponents = true) {
|
||||
var config = new HopFrameConfig();
|
||||
configurator.Invoke(new HopFrameConfigurator(config));
|
||||
configurator.Invoke(new HopFrameConfigurator(config, services));
|
||||
return AddHopFrame(services, config, fluentUiLibraryConfiguration, addRazorComponents);
|
||||
}
|
||||
|
||||
@@ -38,6 +40,8 @@ public static class ServiceCollectionExtensions {
|
||||
services.AddHopFrameServices();
|
||||
services.AddFluentUIComponents(fluentUiLibraryConfiguration);
|
||||
|
||||
services.AddScoped<IPluginOrchestrator, PluginOrchestrator>();
|
||||
|
||||
if (addRazorComponents) {
|
||||
services.AddRazorComponents()
|
||||
.AddInteractiveServerComponents();
|
||||
|
||||
Reference in New Issue
Block a user