Resolve "Plugin support" #68

Merged
leon.hoppe merged 4 commits from feature/plugins into dev 2025-02-05 17:57:00 +01:00
28 changed files with 415 additions and 87 deletions
Showing only changes of commit 13e9af892c - Show all commits

View File

@@ -12,13 +12,32 @@
</component>
<component name="ChangeListManager">
<list default="true" id="0648788e-7696-4e60-bf12-5d5601f33d8c" name="Changes" comment="">
<change afterPath="$PROJECT_DIR$/src/HopFrame.Web/Components/HopFrameCard.razor" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/HopFrame.Web/HopFrameConfiguratorExtensions.cs" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/HopFrame.Web/Models/CustomView.cs" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/HopFrame.Web/Plugins/Annotations/EventHandlerAttribute.cs" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/HopFrame.Web/Plugins/Annotations/PluginConfiguratorAttribute.cs" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/HopFrame.Web/Plugins/Events/EntryEvent.cs" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/HopFrame.Web/Plugins/Events/HopFrameEventArgs.cs" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/HopFrame.Web/Plugins/Events/PageChangeEvent.cs" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/HopFrame.Web/Plugins/Events/PluginEventContainer.cs" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/HopFrame.Web/Plugins/Events/ReloadEvent.cs" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/HopFrame.Web/Plugins/Events/SearchEvent.cs" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/HopFrame.Web/Plugins/Events/ValidationEvent.cs" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/HopFrame.Web/Plugins/HopFramePlugin.cs" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/HopFrame.Web/Plugins/IPluginOrchestrator.cs" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/HopFrame.Web/Plugins/Internal/PluginOrchestrator.cs" afterDir="false" />
<change afterPath="$PROJECT_DIR$/testing/HopFrame.Testing/TestPlugin.cs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/.idea.HopFrame/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/.idea.HopFrame/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/HopFrame.Web/Components/Layout/HopFrameLayout.razor" beforeDir="false" afterPath="$PROJECT_DIR$/src/HopFrame.Web/Components/Layout/HopFrameLayout.razor" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/HopFrame.Web/Components/Layout/HopFrameSideMenu.razor" beforeDir="false" afterPath="$PROJECT_DIR$/src/HopFrame.Web/Components/Layout/HopFrameSideMenu.razor" afterDir="false" />
<change beforePath="$PROJECT_DIR$/testing/HopFrame.Testing/Components/Pages/Counter.razor" beforeDir="false" afterPath="$PROJECT_DIR$/testing/HopFrame.Testing/Components/Pages/Counter.razor" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/HopFrame.Core/Config/DbContextConfig.cs" beforeDir="false" afterPath="$PROJECT_DIR$/src/HopFrame.Core/Config/DbContextConfig.cs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/HopFrame.Core/Config/HopFrameConfig.cs" beforeDir="false" afterPath="$PROJECT_DIR$/src/HopFrame.Core/Config/HopFrameConfig.cs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/HopFrame.Core/Config/TableConfig.cs" beforeDir="false" afterPath="$PROJECT_DIR$/src/HopFrame.Core/Config/TableConfig.cs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/HopFrame.Core/Events/EventTypes.cs" beforeDir="false" afterPath="$PROJECT_DIR$/src/HopFrame.Core/Callbacks/CallbackTypes.cs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/HopFrame.Core/Events/HopEventHandler.cs" beforeDir="false" afterPath="$PROJECT_DIR$/src/HopFrame.Core/Callbacks/HopCallbackHandler.cs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/HopFrame.Core/Events/IEventEmitter.cs" beforeDir="false" afterPath="$PROJECT_DIR$/src/HopFrame.Core/Callbacks/ICallbackEmitter.cs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/HopFrame.Core/ServiceCollectionExtensions.cs" beforeDir="false" afterPath="$PROJECT_DIR$/src/HopFrame.Core/ServiceCollectionExtensions.cs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/HopFrame.Core/Services/Implementations/EventEmitter.cs" beforeDir="false" afterPath="$PROJECT_DIR$/src/HopFrame.Core/Services/Implementations/CallbackEmitter.cs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/HopFrame.Web/Components/Dialogs/HopFrameEditor.razor" beforeDir="false" afterPath="$PROJECT_DIR$/src/HopFrame.Web/Components/Dialogs/HopFrameEditor.razor" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/HopFrame.Web/Components/Pages/HopFrameTablePage.razor" beforeDir="false" afterPath="$PROJECT_DIR$/src/HopFrame.Web/Components/Pages/HopFrameTablePage.razor" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/HopFrame.Web/HopFrameConfiguratorExtensions.cs" beforeDir="false" afterPath="$PROJECT_DIR$/src/HopFrame.Web/HopFrameConfiguratorExtensions.cs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/HopFrame.Web/ServiceCollectionExtensions.cs" beforeDir="false" afterPath="$PROJECT_DIR$/src/HopFrame.Web/ServiceCollectionExtensions.cs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/testing/HopFrame.Testing/Program.cs" beforeDir="false" afterPath="$PROJECT_DIR$/testing/HopFrame.Testing/Program.cs" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
@@ -68,13 +87,16 @@
<setting file="file://$APPLICATION_CONFIG_DIR$/resharper-host/DecompilerCache/decompiler/c73b3c6c598640c592fd3c6fa226c286e90908/fc/6f7933d2/ICollection`1.cs" root0="FORCE_HIGHLIGHTING" />
<setting file="file://$APPLICATION_CONFIG_DIR$/resharper-host/SourcesCache/10c66a9a1e137111895f7182a2ae246eabe06a261578c3fa495a45f6f177d35/IconVariant.cs" root0="FORCE_HIGHLIGHTING" />
<setting file="file://$APPLICATION_CONFIG_DIR$/resharper-host/SourcesCache/1b81cb3be224213a6a73519b6e340a628d9a1fb8629c351a186a26f6376669/List.cs" root0="FORCE_HIGHLIGHTING" />
<setting file="file://$APPLICATION_CONFIG_DIR$/resharper-host/SourcesCache/24dd1164ba47541cb1d3eb011e638e16953dbea3ae3f4dc208c3bbf3e96298a/ServiceCollectionServiceExtensions.cs" root0="FORCE_HIGHLIGHTING" />
<setting file="file://$APPLICATION_CONFIG_DIR$/resharper-host/SourcesCache/26c9a2fb5243863babc926e4be763daf4128d4f97c4a769cdce1e2e3e5c532/FluentButton.razor.cs" root0="SKIP_HIGHLIGHTING" />
<setting file="file://$APPLICATION_CONFIG_DIR$/resharper-host/SourcesCache/2751d5afefca5424bfc4b21347f581372f7a739c0ae4df661ea557fcb97ef20/EnumExtensions.cs" root0="SKIP_HIGHLIGHTING" />
<setting file="file://$APPLICATION_CONFIG_DIR$/resharper-host/SourcesCache/439c4ee753b23e743cc14119593bc889751f9eb0b38997577d8e4c47c4fed/ToCollection.cs" root0="FORCE_HIGHLIGHTING" />
<setting file="file://$APPLICATION_CONFIG_DIR$/resharper-host/SourcesCache/4c41a7d338749915157d56585365d1693fbad6be8231d3d583b1cf10d16896d9/FluentIcon.razor.cs" root0="SKIP_HIGHLIGHTING" />
<setting file="file://$APPLICATION_CONFIG_DIR$/resharper-host/SourcesCache/4ee221fd7e91e9a4c14ff82aae2ee938edecde35a934133e991aba56aa9499/Icon.cs" root0="FORCE_HIGHLIGHTING" />
<setting file="file://$APPLICATION_CONFIG_DIR$/resharper-host/SourcesCache/5a69b82eed595b731b82667db08722b69b82482e275cf32dfb219190e3dc49/CollectionEntry.cs" root0="FORCE_HIGHLIGHTING" />
<setting file="file://$APPLICATION_CONFIG_DIR$/resharper-host/SourcesCache/60e7b22380df80ef6fefe43138047f49ec6eff4b25c12b42ce3d6ed5aac/MethodInvokerCommon.cs" root0="FORCE_HIGHLIGHTING" />
<setting file="file://$APPLICATION_CONFIG_DIR$/resharper-host/SourcesCache/642391624bd5c30b3411a11434588aba4906207335166b784bf3a4325f6c7/NavigationEntry.cs" root0="FORCE_HIGHLIGHTING" />
<setting file="file://$APPLICATION_CONFIG_DIR$/resharper-host/SourcesCache/6d1d64f05e7045295fa180276a8c2aef0302c9e96eb53b3431ab13db4579/FluentAppBarItem.razor.cs" root0="FORCE_HIGHLIGHTING" />
<setting file="file://$APPLICATION_CONFIG_DIR$/resharper-host/SourcesCache/6fe785cceb29ca2d1da78e157315815a7c4372b582a20a71c28b210f9d56e/IconsExtensions.cs" root0="SKIP_HIGHLIGHTING" />
<setting file="file://$APPLICATION_CONFIG_DIR$/resharper-host/SourcesCache/7ad7d2d0ae865063993eb8a03427815ea3bdb6a774e0a2f95512e9f669a4f489/MemberEntry.cs" root0="FORCE_HIGHLIGHTING" />
<setting file="file://$APPLICATION_CONFIG_DIR$/resharper-host/SourcesCache/87c584767b46b5fd42769be76547105558e6690f785614efddca134b2d682/Type.cs" root0="FORCE_HIGHLIGHTING" />
@@ -86,6 +108,7 @@
<setting file="file://$APPLICATION_CONFIG_DIR$/resharper-host/SourcesCache/d04a416cac8afac0341a8be0e859b230f2eae64924298eef48c317ba35916/RenderTreeBuilder.cs" root0="FORCE_HIGHLIGHTING" />
<setting file="file://$APPLICATION_CONFIG_DIR$/resharper-host/SourcesCache/d39923abb31e6a6e7a9e8173e217da584c54925ce63e568126a2b89b9ab/DefaultRazorComponentsServiceOptionsConfiguration.cs" root0="FORCE_HIGHLIGHTING" />
<setting file="file://$APPLICATION_CONFIG_DIR$/resharper-host/SourcesCache/d858ddb35a8e36df5573b7612542f9ad50f426b8ab43818587d1ac65fab14829/DatabaseGeneratedAttribute.cs" root0="FORCE_HIGHLIGHTING" />
<setting file="file://$APPLICATION_CONFIG_DIR$/resharper-host/SourcesCache/dac3553c90d47a746e7e7f02faecb1a5e581090/Components_AppBar_FluentAppBarItem_razor.g.cs" root0="FORCE_HIGHLIGHTING" />
<setting file="file://$APPLICATION_CONFIG_DIR$/resharper-host/SourcesCache/e26a4f2df232f16e374b9719f883c1b2419f6341838d94b7581db9c7d2de17/IconInfo.cs" root0="FORCE_HIGHLIGHTING" />
<setting file="file://$APPLICATION_CONFIG_DIR$/resharper-host/SourcesCache/eab2d6b892f743a27cb49a139ba782855897baf1233febd2dfd2092f3/EntityEntry.cs" root0="FORCE_HIGHLIGHTING" />
<setting file="file://$APPLICATION_CONFIG_DIR$/resharper-host/SourcesCache/ece8533187fe96ce67b3ef1c9cc3502ef8da5510aadb132a9b21c5605d7c2119/PropertyColumn.cs" root0="FORCE_HIGHLIGHTING" />
@@ -111,28 +134,28 @@
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent">{
&quot;keyToString&quot;: {
&quot;.NET Launch Settings Profile.HopFrame.Testing.Api: https.executor&quot;: &quot;Run&quot;,
&quot;.NET Launch Settings Profile.HopFrame.Testing.executor&quot;: &quot;Run&quot;,
&quot;.NET Launch Settings Profile.HopFrame.Testing: https.executor&quot;: &quot;Run&quot;,
&quot;.NET Project.HopFrame.Testing.executor&quot;: &quot;Run&quot;,
&quot;72b118b0-a6fc-4561-acdf-74f0b454dbb8.executor&quot;: &quot;Debug&quot;,
&quot;RunOnceActivity.ShowReadmeOnStart&quot;: &quot;true&quot;,
&quot;RunOnceActivity.git.unshallow&quot;: &quot;true&quot;,
&quot;b5f11219-dfc4-47a1-b02c-90ab603034fb.executor&quot;: &quot;Debug&quot;,
&quot;dcdf1689-dc07-47e4-8824-2e60a4fbf301.executor&quot;: &quot;Debug&quot;,
&quot;git-widget-placeholder&quot;: &quot;!29 on feature/custom-views&quot;,
&quot;list.type.of.created.stylesheet&quot;: &quot;CSS&quot;,
&quot;node.js.detected.package.eslint&quot;: &quot;true&quot;,
&quot;node.js.detected.package.tslint&quot;: &quot;true&quot;,
&quot;node.js.selected.package.eslint&quot;: &quot;(autodetect)&quot;,
&quot;node.js.selected.package.tslint&quot;: &quot;(autodetect)&quot;,
&quot;nodejs_package_manager_path&quot;: &quot;npm&quot;,
&quot;settings.editor.selected.configurable&quot;: &quot;preferences.environmentSetup&quot;,
&quot;vue.rearranger.settings.migration&quot;: &quot;true&quot;
<component name="PropertiesComponent"><![CDATA[{
"keyToString": {
".NET Launch Settings Profile.HopFrame.Testing.Api: https.executor": "Run",
".NET Launch Settings Profile.HopFrame.Testing.executor": "Run",
".NET Launch Settings Profile.HopFrame.Testing: https.executor": "Run",
".NET Project.HopFrame.Testing.executor": "Run",
"72b118b0-a6fc-4561-acdf-74f0b454dbb8.executor": "Debug",
"RunOnceActivity.ShowReadmeOnStart": "true",
"RunOnceActivity.git.unshallow": "true",
"b5f11219-dfc4-47a1-b02c-90ab603034fb.executor": "Debug",
"dcdf1689-dc07-47e4-8824-2e60a4fbf301.executor": "Debug",
"git-widget-placeholder": "!30 on feature/plugins",
"list.type.of.created.stylesheet": "CSS",
"node.js.detected.package.eslint": "true",
"node.js.detected.package.tslint": "true",
"node.js.selected.package.eslint": "(autodetect)",
"node.js.selected.package.tslint": "(autodetect)",
"nodejs_package_manager_path": "npm",
"settings.editor.selected.configurable": "preferences.environmentSetup",
"vue.rearranger.settings.migration": "true"
}
}</component>
}]]></component>
<component name="RunManager" selected=".NET Launch Settings Profile.HopFrame.Testing: https">
<configuration name="HopFrame.Testing: http" type="LaunchSettings" factoryName=".NET Launch Settings Profile">
<option name="LAUNCH_PROFILE_PROJECT_FILE_PATH" value="$PROJECT_DIR$/testing/HopFrame.Testing/HopFrame.Testing.csproj" />
@@ -233,6 +256,9 @@
<workItem from="1738335286481" duration="2039000" />
<workItem from="1738403493974" duration="4231000" />
<workItem from="1738418482606" duration="2795000" />
<workItem from="1738421294144" duration="1651000" />
<workItem from="1738422949337" duration="141000" />
<workItem from="1738512801911" duration="6705000" />
</task>
<task id="LOCAL-00001" summary="Added basic configuration">
<option name="closed" value="true" />
@@ -514,7 +540,15 @@
<option name="project" value="LOCAL" />
<updated>1738407710507</updated>
</task>
<option name="localTasksCounter" value="36" />
<task id="LOCAL-00036" summary="Added custom views">
<option name="closed" value="true" />
<created>1738422931038</created>
<option name="number" value="00036" />
<option name="presentableId" value="LOCAL-00036" />
<option name="project" value="LOCAL" />
<updated>1738422931038</updated>
</task>
<option name="localTasksCounter" value="37" />
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
@@ -565,7 +599,6 @@
<component name="UnityProjectConfiguration" hasMinimizedUI="false" />
<component name="VcsManagerConfiguration">
<option name="CLEAR_INITIAL_COMMIT_MESSAGE" value="true" />
<MESSAGE value="Removed Template" />
<MESSAGE value="Added policy validation, ordering and virtual listing properties" />
<MESSAGE value="Added n -&gt; m relation support" />
<MESSAGE value="Added text area support and DI support for modifier functions" />
@@ -590,6 +623,7 @@
<MESSAGE value="Added missing installation instructions" />
<MESSAGE value="Added modular event system" />
<MESSAGE value="Fixed event emitter service scope" />
<option name="LAST_COMMIT_MESSAGE" value="Fixed event emitter service scope" />
<MESSAGE value="Added custom views" />
<option name="LAST_COMMIT_MESSAGE" value="Added custom views" />
</component>
</project>

View File

@@ -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

View File

@@ -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;

View 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();
}

View File

@@ -1,5 +1,4 @@
using HopFrame.Core.Events;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
namespace HopFrame.Core.Config;

View File

@@ -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>

View File

@@ -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;
});

View File

@@ -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();
}

View File

@@ -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;
}

View File

@@ -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();
}

View File

@@ -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();

View File

@@ -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);

View File

@@ -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;
}
}

View File

@@ -0,0 +1,4 @@
namespace HopFrame.Web.Plugins.Annotations;
[AttributeUsage(AttributeTargets.Method)]
public class EventHandlerAttribute : Attribute;

View File

@@ -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;

View 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; }
}

View 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; }
}

View 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; }
}

View 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; }
}

View File

@@ -0,0 +1,7 @@
using HopFrame.Web.Components.Pages;
namespace HopFrame.Web.Plugins.Events;
public sealed class ReloadEvent(HopFrameTablePage sender) : HopFrameTablePageEventArgs(sender) {
}

View 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; }
}

View 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; }
}

View File

@@ -0,0 +1,7 @@
namespace HopFrame.Web.Plugins;
public abstract class HopFramePlugin {
}

View File

@@ -0,0 +1,5 @@
namespace HopFrame.Web.Plugins;
public interface IPluginOrchestrator {
public Task<TEvent> DispatchEvent<TEvent>(TEvent @event, CancellationToken ct = new());
}

View 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;
}
}

View File

@@ -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();

View File

@@ -80,6 +80,8 @@ builder.Services.AddHopFrame(options => {
options.AddCustomView("Counter", "/counter")
.SetDescription("A custom view")
.SetPolicy("counter.view");
options.AddPlugin<TestPlugin>();
});
var app = builder.Build();

View File

@@ -0,0 +1,26 @@
using HopFrame.Core.Config;
using HopFrame.Testing.Models;
using HopFrame.Web.Plugins;
using HopFrame.Web.Plugins.Annotations;
using HopFrame.Web.Plugins.Events;
namespace HopFrame.Testing;
public class TestPlugin : HopFramePlugin {
[PluginConfigurator]
public static void OnConfiguring(HopFrameConfigurator configurator) {
Console.WriteLine("Configurator invoked!");
configurator.GetDbContext<DatabaseContext>()!
.Table<User>()
.Property(u => u.Email)
.SetDisplayName("Modified by Plugin!");
}
[EventHandler]
public void OnDelete(DeleteEntryEvent e) {
Console.WriteLine("Event called!");
e.SetCancelled(true);
}
}