Added plugin buttons
This commit is contained in:
24
.idea/.idea.HopFrame/.idea/workspace.xml
generated
24
.idea/.idea.HopFrame/.idea/workspace.xml
generated
@@ -11,12 +11,11 @@
|
||||
<option name="autoReloadType" value="SELECTIVE" />
|
||||
</component>
|
||||
<component name="ChangeListManager">
|
||||
<list default="true" id="0648788e-7696-4e60-bf12-5d5601f33d8c" name="Changes" comment="">
|
||||
<list default="true" id="0648788e-7696-4e60-bf12-5d5601f33d8c" name="Changes" comment="Passed cancellation tokens to event handlers if needed">
|
||||
<change afterPath="$PROJECT_DIR$/src/HopFrame.Web/Plugins/Events/PageInitializedEvent.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/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/Plugins/IPluginOrchestrator.cs" beforeDir="false" afterPath="$PROJECT_DIR$/src/HopFrame.Web/Plugins/IPluginOrchestrator.cs" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/src/HopFrame.Web/Plugins/Internal/PluginOrchestrator.cs" beforeDir="false" afterPath="$PROJECT_DIR$/src/HopFrame.Web/Plugins/Internal/PluginOrchestrator.cs" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/src/HopFrame.Web/Plugins/HopFramePlugin.cs" beforeDir="false" afterPath="$PROJECT_DIR$/src/HopFrame.Web/Plugins/HopFramePlugin.cs" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/testing/HopFrame.Testing/TestPlugin.cs" beforeDir="false" afterPath="$PROJECT_DIR$/testing/HopFrame.Testing/TestPlugin.cs" afterDir="false" />
|
||||
</list>
|
||||
<option name="SHOW_DIALOG" value="false" />
|
||||
@@ -95,6 +94,7 @@
|
||||
<setting file="file://$APPLICATION_CONFIG_DIR$/resharper-host/SourcesCache/ece8533187fe96ce67b3ef1c9cc3502ef8da5510aadb132a9b21c5605d7c2119/PropertyColumn.cs" root0="FORCE_HIGHLIGHTING" />
|
||||
<setting file="file://$APPLICATION_CONFIG_DIR$/resharper-host/SourcesCache/ee4d234452e240d83e3de396c2e85cbf9ac9fb9add618b955eea196c81aaf8/IDialogContentComponent.cs" root0="SKIP_HIGHLIGHTING" />
|
||||
<setting file="file://$APPLICATION_CONFIG_DIR$/resharper-host/SourcesCache/fc2027f7e776fc105cddb56b1a25eeb3895b3ae6f3aac854d786e63bd01f75e2/CallSiteFactory.cs" root0="FORCE_HIGHLIGHTING" />
|
||||
<setting file="file://$APPLICATION_CONFIG_DIR$/resharper-host/SourcesCache/fd57398b7dc3a8ce7da2786f2c67289c3d974658a9e90d0c1e84db3d965fbf1/Console.cs" root0="FORCE_HIGHLIGHTING" />
|
||||
<setting file="file://$APPLICATION_CONFIG_DIR$/resharper-host/SourcesCache/ff37d54b3bf4d2756237fb789635831532603376e940f63d634b869d26d74c/Regular16.cs" root0="FORCE_HIGHLIGHTING" />
|
||||
<setting file="file://$PROJECT_DIR$/src/HopFrame.Core/Config/DbContextConfig.cs" root0="FORCE_HIGHLIGHTING" />
|
||||
</component>
|
||||
@@ -240,7 +240,7 @@
|
||||
<workItem from="1738421294144" duration="1651000" />
|
||||
<workItem from="1738422949337" duration="141000" />
|
||||
<workItem from="1738512801911" duration="6776000" />
|
||||
<workItem from="1738769458367" duration="962000" />
|
||||
<workItem from="1738769458367" duration="3697000" />
|
||||
</task>
|
||||
<task id="LOCAL-00001" summary="Added basic configuration">
|
||||
<option name="closed" value="true" />
|
||||
@@ -538,7 +538,15 @@
|
||||
<option name="project" value="LOCAL" />
|
||||
<updated>1738519603597</updated>
|
||||
</task>
|
||||
<option name="localTasksCounter" value="38" />
|
||||
<task id="LOCAL-00038" summary="Passed cancellation tokens to event handlers if needed">
|
||||
<option name="closed" value="true" />
|
||||
<created>1738770468949</created>
|
||||
<option name="number" value="00038" />
|
||||
<option name="presentableId" value="LOCAL-00038" />
|
||||
<option name="project" value="LOCAL" />
|
||||
<updated>1738770468949</updated>
|
||||
</task>
|
||||
<option name="localTasksCounter" value="39" />
|
||||
<servers />
|
||||
</component>
|
||||
<component name="TypeScriptGeneratedFilesManager">
|
||||
@@ -589,7 +597,6 @@
|
||||
<component name="UnityProjectConfiguration" hasMinimizedUI="false" />
|
||||
<component name="VcsManagerConfiguration">
|
||||
<option name="CLEAR_INITIAL_COMMIT_MESSAGE" value="true" />
|
||||
<MESSAGE value="Added n -> m relation support" />
|
||||
<MESSAGE value="Added text area support and DI support for modifier functions" />
|
||||
<MESSAGE value="Addressed all build warnings" />
|
||||
<MESSAGE value="Added documentation for the configurators and service extensions methods" />
|
||||
@@ -614,6 +621,7 @@
|
||||
<MESSAGE value="Fixed event emitter service scope" />
|
||||
<MESSAGE value="Added custom views" />
|
||||
<MESSAGE value="Added plugin events" />
|
||||
<option name="LAST_COMMIT_MESSAGE" value="Added plugin events" />
|
||||
<MESSAGE value="Passed cancellation tokens to event handlers if needed" />
|
||||
<option name="LAST_COMMIT_MESSAGE" value="Passed cancellation tokens to event handlers if needed" />
|
||||
</component>
|
||||
</project>
|
||||
@@ -31,18 +31,34 @@
|
||||
</FluentButton>
|
||||
}
|
||||
|
||||
@foreach (var button in _pluginButtons.Where(pb => pb.IsForTable(_config)).Where(pb => pb.Position == PluginButtonPosition.TopLeft)) {
|
||||
<FluentButton
|
||||
IconStart="@(button.Icon?.GetInstance())"
|
||||
OnClick="() => button.Handler.Invoke(null!, _config!)">
|
||||
@button.Title
|
||||
</FluentButton>
|
||||
}
|
||||
|
||||
<FluentSpacer />
|
||||
<FluentSearch @oninput="OnSearch" @onchange="OnSearch" Style="width: 350px" />
|
||||
|
||||
@if (_hasCreatePolicy && DisplayActions) {
|
||||
<FluentButton OnClick="async () => { await CreateOrEdit(null); }">Add Entry</FluentButton>
|
||||
}
|
||||
|
||||
@foreach (var button in _pluginButtons.Where(pb => pb.IsForTable(_config)).Where(pb => pb.Position == PluginButtonPosition.TopRight)) {
|
||||
<FluentButton
|
||||
IconStart="@(button.Icon?.GetInstance())"
|
||||
OnClick="() => button.Handler.Invoke(null!, _config!)">
|
||||
@button.Title
|
||||
</FluentButton>
|
||||
}
|
||||
</FluentToolbar>
|
||||
<FluentProgress Visible="_loading" Width="100%" />
|
||||
|
||||
<div style="display: flex; overflow-y: auto; flex-grow: 1">
|
||||
<div style="flex-grow: 1">
|
||||
<FluentDataGrid Items="_currentlyDisplayedModels.AsQueryable()">
|
||||
<FluentDataGrid Items="CurrentlyDisplayedModels.AsQueryable()">
|
||||
@if (DisplaySelection) {
|
||||
<SelectColumn
|
||||
TGridItem="object"
|
||||
@@ -63,6 +79,12 @@
|
||||
|
||||
@if (DisplayActions && (_hasDeletePolicy || _hasUpdatePolicy)) {
|
||||
<TemplateColumn Title="Actions" Align="@Align.End" Style="min-height: 44px; min-width: max-content">
|
||||
@foreach (var button in _pluginButtons.Where(pb => pb.IsForTable(_config)).Where(pb => pb.Position == PluginButtonPosition.OnEntry)) {
|
||||
<FluentButton OnClick="() => button.Handler.Invoke(context, _config!)">
|
||||
<FluentIcon Value="@(button.Icon!.GetInstance())" />
|
||||
</FluentButton>
|
||||
}
|
||||
|
||||
@if (_hasUpdatePolicy) {
|
||||
<FluentButton aria-label="Edit entry" OnClick="async () => { await CreateOrEdit(context); }">
|
||||
<FluentIcon Value="@(new Icons.Regular.Size16.Edit())"/>
|
||||
@@ -146,7 +168,7 @@
|
||||
private TableConfig? _config;
|
||||
private ITableManager? _manager;
|
||||
|
||||
private object[] _currentlyDisplayedModels = [];
|
||||
public object[] CurrentlyDisplayedModels = [];
|
||||
private int _currentPage;
|
||||
private int _totalPages;
|
||||
private string? _searchTerm;
|
||||
@@ -159,6 +181,7 @@
|
||||
private bool _allSelected;
|
||||
|
||||
private readonly CancellationTokenSource _tokenSource = new();
|
||||
private List<PluginButton> _pluginButtons = new();
|
||||
|
||||
protected override void OnInitialized() {
|
||||
_config ??= Explorer.GetTable(TableDisplayName);
|
||||
@@ -174,12 +197,18 @@
|
||||
return;
|
||||
}
|
||||
|
||||
var eventResult = await PluginOrchestrator.DispatchEvent(new PageInitializedEvent(this) {
|
||||
Table = _config!
|
||||
});
|
||||
if (eventResult.IsCanceled) return;
|
||||
_pluginButtons = eventResult.PluginButtons;
|
||||
|
||||
_hasUpdatePolicy = await Handler.IsAuthenticatedAsync(_config?.UpdatePolicy);
|
||||
_hasDeletePolicy = await Handler.IsAuthenticatedAsync(_config?.DeletePolicy);
|
||||
_hasCreatePolicy = await Handler.IsAuthenticatedAsync(_config?.CreatePolicy);
|
||||
|
||||
_manager ??= Explorer.GetTableManager(_config!.PropertyName);
|
||||
_currentlyDisplayedModels = await _manager!.LoadPage(_currentPage, PerPage).ToArrayAsync();
|
||||
CurrentlyDisplayedModels = await _manager!.LoadPage(_currentPage, PerPage).ToArrayAsync();
|
||||
_totalPages = await _manager.TotalPages(PerPage);
|
||||
}
|
||||
|
||||
@@ -228,7 +257,7 @@
|
||||
|
||||
if (!string.IsNullOrEmpty(_searchTerm)) {
|
||||
(var query, _totalPages) = await _manager!.Search(_searchTerm, 0, PerPage);
|
||||
_currentlyDisplayedModels = query.ToArray();
|
||||
CurrentlyDisplayedModels = query.ToArray();
|
||||
}
|
||||
else {
|
||||
await OnInitializedAsync();
|
||||
@@ -329,8 +358,8 @@
|
||||
}
|
||||
|
||||
private void SelectAll() {
|
||||
var selected = _currentlyDisplayedModels.All(DialogData!.SelectedObjects.Contains);
|
||||
foreach (var displayedModel in _currentlyDisplayedModels) {
|
||||
var selected = CurrentlyDisplayedModels.All(DialogData!.SelectedObjects.Contains);
|
||||
foreach (var displayedModel in CurrentlyDisplayedModels) {
|
||||
SelectItem(displayedModel, !selected);
|
||||
}
|
||||
_allSelected = selected;
|
||||
|
||||
93
src/HopFrame.Web/Plugins/Events/PageInitializedEvent.cs
Normal file
93
src/HopFrame.Web/Plugins/Events/PageInitializedEvent.cs
Normal file
@@ -0,0 +1,93 @@
|
||||
using HopFrame.Core.Config;
|
||||
using HopFrame.Web.Components.Pages;
|
||||
using Microsoft.FluentUI.AspNetCore.Components;
|
||||
|
||||
namespace HopFrame.Web.Plugins.Events;
|
||||
|
||||
public class PageInitializedEvent(HopFrameTablePage sender) : HopFrameTablePageEventArgs(sender) {
|
||||
public List<PluginButton> PluginButtons { get; } = new();
|
||||
|
||||
public void AddPageButton(string title, Func<Task> callback, bool pushRight = false, IconInfo? icon = null) {
|
||||
PluginButtons.Add(new() {
|
||||
Title = title,
|
||||
Icon = icon,
|
||||
Position = pushRight ? PluginButtonPosition.TopRight : PluginButtonPosition.TopLeft,
|
||||
Handler = (_, _) => callback.Invoke()
|
||||
});
|
||||
}
|
||||
|
||||
public void AddPageButton(string title, Action callback, bool pushRight = false, IconInfo? icon = null) {
|
||||
AddPageButton(title, () => {
|
||||
callback.Invoke();
|
||||
return Task.CompletedTask;
|
||||
}, pushRight, icon);
|
||||
}
|
||||
|
||||
public void AddEntityButton(IconInfo icon, Func<object, TableConfig, Task> callback) {
|
||||
PluginButtons.Add(new() {
|
||||
Icon = icon,
|
||||
Position = PluginButtonPosition.OnEntry,
|
||||
Handler = callback
|
||||
});
|
||||
}
|
||||
|
||||
public void AddEntityButton(IconInfo icon, Action<object, TableConfig> callback) {
|
||||
AddEntityButton(icon, (obj, cfg) => {
|
||||
callback.Invoke(obj, cfg);
|
||||
return Task.CompletedTask;
|
||||
});
|
||||
}
|
||||
|
||||
public void AddEntityButton<TEntity>(IconInfo icon, Func<TEntity, TableConfig, Task> callback) {
|
||||
PluginButtons.Add(new() {
|
||||
Icon = icon,
|
||||
Position = PluginButtonPosition.OnEntry,
|
||||
Handler = (obj, cfg) => callback.Invoke((TEntity)obj, cfg),
|
||||
TableFilter = typeof(TEntity)
|
||||
});
|
||||
}
|
||||
|
||||
public void AddEntityButton<TEntity>(IconInfo icon, Action<TEntity, TableConfig> callback) {
|
||||
AddEntityButton<TEntity>(icon, (obj, cfg) => {
|
||||
callback.Invoke(obj, cfg);
|
||||
return Task.CompletedTask;
|
||||
});
|
||||
}
|
||||
|
||||
public void AddPageButton<TEntity>(string title, Func<Task> callback, bool pushRight = false, IconInfo? icon = null) {
|
||||
PluginButtons.Add(new() {
|
||||
Title = title,
|
||||
Icon = icon,
|
||||
Position = pushRight ? PluginButtonPosition.TopRight : PluginButtonPosition.TopLeft,
|
||||
Handler = (_, _) => callback.Invoke(),
|
||||
TableFilter = typeof(TEntity)
|
||||
});
|
||||
}
|
||||
|
||||
public void AddPageButton<TEntity>(string title, Action callback, bool pushRight = false, IconInfo? icon = null) {
|
||||
AddPageButton<TEntity>(title, () => {
|
||||
callback.Invoke();
|
||||
return Task.CompletedTask;
|
||||
}, pushRight, icon);
|
||||
}
|
||||
}
|
||||
|
||||
public struct PluginButton {
|
||||
public PluginButtonPosition Position { get; set; }
|
||||
public Func<object, TableConfig, Task> Handler { get; set; }
|
||||
public string? Title { get; set; }
|
||||
public IconInfo? Icon { get; set; }
|
||||
public Type? TableFilter { get; set; }
|
||||
|
||||
internal bool IsForTable(TableConfig? config) {
|
||||
if (config is null) return false;
|
||||
if (TableFilter is null) return true;
|
||||
return config.TableType == TableFilter;
|
||||
}
|
||||
}
|
||||
|
||||
public enum PluginButtonPosition {
|
||||
TopLeft = 0,
|
||||
TopRight = 1,
|
||||
OnEntry = 2
|
||||
}
|
||||
@@ -1,7 +1,3 @@
|
||||
namespace HopFrame.Web.Plugins;
|
||||
|
||||
public abstract class HopFramePlugin {
|
||||
|
||||
|
||||
|
||||
}
|
||||
public abstract class HopFramePlugin;
|
||||
|
||||
@@ -80,8 +80,6 @@ builder.Services.AddHopFrame(options => {
|
||||
options.AddCustomView("Counter", "/counter")
|
||||
.SetDescription("A custom view")
|
||||
.SetPolicy("counter.view");
|
||||
|
||||
options.AddPlugin<TestPlugin>();
|
||||
});
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user