mirror of https://github.com/abpframework/abp.git
18 changed files with 460 additions and 0 deletions
@ -0,0 +1,31 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using JetBrains.Annotations; |
||||
|
|
||||
|
namespace Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.PageToolbars |
||||
|
{ |
||||
|
public class AbpPageToolbarOptions |
||||
|
{ |
||||
|
public PageToolbarDictionary Toolbars { get; } |
||||
|
|
||||
|
public AbpPageToolbarOptions() |
||||
|
{ |
||||
|
Toolbars = new PageToolbarDictionary(); |
||||
|
} |
||||
|
|
||||
|
public void Configure<TPage>([NotNull] Action<PageToolbar> configureAction) |
||||
|
{ |
||||
|
// ReSharper disable once AssignNullToNotNullAttribute
|
||||
|
Configure(typeof(TPage).FullName, configureAction); |
||||
|
} |
||||
|
|
||||
|
public void Configure([NotNull]string pageName, [NotNull]Action<PageToolbar> configureAction) |
||||
|
{ |
||||
|
Check.NotNullOrWhiteSpace(pageName, nameof(pageName)); |
||||
|
Check.NotNull(configureAction, nameof(configureAction)); |
||||
|
|
||||
|
var toolbar = Toolbars.GetOrAdd(pageName, () => new PageToolbar(pageName)); |
||||
|
configureAction(toolbar); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,9 @@ |
|||||
|
using System.Threading.Tasks; |
||||
|
|
||||
|
namespace Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.PageToolbars |
||||
|
{ |
||||
|
public interface IPageToolbarContributor |
||||
|
{ |
||||
|
Task ContributeAsync(PageToolbarContributionContext context); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,9 @@ |
|||||
|
using System.Threading.Tasks; |
||||
|
|
||||
|
namespace Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.PageToolbars |
||||
|
{ |
||||
|
public interface IPageToolbarManager |
||||
|
{ |
||||
|
Task<PageToolbarItem[]> GetItemsAsync(string pageName); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,17 @@ |
|||||
|
using JetBrains.Annotations; |
||||
|
|
||||
|
namespace Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.PageToolbars |
||||
|
{ |
||||
|
public class PageToolbar |
||||
|
{ |
||||
|
public string PageName { get; } |
||||
|
|
||||
|
public PageToolbarContributorList Contributors { get; set; } |
||||
|
|
||||
|
public PageToolbar([NotNull] string pageName) |
||||
|
{ |
||||
|
PageName = Check.NotNullOrEmpty(pageName, nameof(pageName)); |
||||
|
Contributors = new PageToolbarContributorList(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,27 @@ |
|||||
|
using System; |
||||
|
using JetBrains.Annotations; |
||||
|
|
||||
|
namespace Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.PageToolbars |
||||
|
{ |
||||
|
public class PageToolbarContributionContext |
||||
|
{ |
||||
|
[NotNull] |
||||
|
public string PageName { get; } |
||||
|
|
||||
|
[NotNull] |
||||
|
public IServiceProvider ServiceProvider { get; } |
||||
|
|
||||
|
[NotNull] |
||||
|
public PageToolbarItemList Items { get; } |
||||
|
|
||||
|
public PageToolbarContributionContext( |
||||
|
[NotNull] string pageName, |
||||
|
[NotNull] IServiceProvider serviceProvider) |
||||
|
{ |
||||
|
PageName = Check.NotNull(pageName, nameof(pageName)); |
||||
|
ServiceProvider = Check.NotNull(serviceProvider, nameof(serviceProvider)); |
||||
|
|
||||
|
Items = new PageToolbarItemList(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,9 @@ |
|||||
|
using System.Threading.Tasks; |
||||
|
|
||||
|
namespace Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.PageToolbars |
||||
|
{ |
||||
|
public abstract class PageToolbarContributor : IPageToolbarContributor |
||||
|
{ |
||||
|
public abstract Task ContributeAsync(PageToolbarContributionContext context); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,8 @@ |
|||||
|
using System.Collections.Generic; |
||||
|
|
||||
|
namespace Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.PageToolbars |
||||
|
{ |
||||
|
public class PageToolbarContributorList : List<IPageToolbarContributor> |
||||
|
{ |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,9 @@ |
|||||
|
using System.Collections.Generic; |
||||
|
|
||||
|
namespace Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.PageToolbars |
||||
|
{ |
||||
|
public class PageToolbarDictionary : Dictionary<string, PageToolbar> |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,84 @@ |
|||||
|
using System; |
||||
|
using Localization.Resources.AbpUi; |
||||
|
using Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers; |
||||
|
using Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Button; |
||||
|
using Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.Pages.Shared.Components.AbpPageToolbar.Button; |
||||
|
using Volo.Abp.Localization; |
||||
|
|
||||
|
namespace Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.PageToolbars |
||||
|
{ |
||||
|
public static class PageToolbarExtensions |
||||
|
{ |
||||
|
public static PageToolbar AddComponent<TComponent>( |
||||
|
this PageToolbar toolbar, |
||||
|
object argument = null, |
||||
|
int order = 0, |
||||
|
string requiredPolicyName = null) |
||||
|
{ |
||||
|
return toolbar.AddComponent( |
||||
|
typeof(TComponent), |
||||
|
argument, |
||||
|
order, |
||||
|
requiredPolicyName |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
public static PageToolbar AddComponent( |
||||
|
this PageToolbar toolbar, |
||||
|
Type componentType, |
||||
|
object argument = null, |
||||
|
int order = 0, |
||||
|
string requiredPolicyName = null) |
||||
|
{ |
||||
|
toolbar.Contributors.Add( |
||||
|
new SimplePageToolbarContributor( |
||||
|
componentType, |
||||
|
argument, |
||||
|
order, |
||||
|
requiredPolicyName |
||||
|
) |
||||
|
); |
||||
|
|
||||
|
return toolbar; |
||||
|
} |
||||
|
|
||||
|
public static PageToolbar AddButton( |
||||
|
this PageToolbar toolbar, |
||||
|
ILocalizableString text, |
||||
|
string icon = null, |
||||
|
string name = null, |
||||
|
string id = null, |
||||
|
ILocalizableString busyText = null, |
||||
|
FontIconType iconType = FontIconType.FontAwesome, |
||||
|
AbpButtonType type = AbpButtonType.Primary, |
||||
|
AbpButtonSize size = AbpButtonSize.Small, |
||||
|
bool disabled = false, |
||||
|
int order = 0, |
||||
|
string requiredPolicyName = null) |
||||
|
{ |
||||
|
if (busyText == null) |
||||
|
{ |
||||
|
busyText = new LocalizableString(typeof(AbpUiResource), "ProcessingWithThreeDot"); |
||||
|
} |
||||
|
|
||||
|
toolbar.AddComponent<AbpPageToolbarButtonViewComponent>( |
||||
|
new |
||||
|
{ |
||||
|
text, |
||||
|
icon, |
||||
|
name, |
||||
|
id, |
||||
|
busyText, |
||||
|
iconType, |
||||
|
type, |
||||
|
size, |
||||
|
disabled |
||||
|
}, |
||||
|
order, |
||||
|
requiredPolicyName |
||||
|
); |
||||
|
|
||||
|
return toolbar; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,26 @@ |
|||||
|
using System; |
||||
|
using JetBrains.Annotations; |
||||
|
|
||||
|
namespace Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.PageToolbars |
||||
|
{ |
||||
|
public class PageToolbarItem |
||||
|
{ |
||||
|
[NotNull] |
||||
|
public Type ComponentType { get; } |
||||
|
|
||||
|
[CanBeNull] |
||||
|
public object Arguments { get; set; } |
||||
|
|
||||
|
public int Order { get; set; } |
||||
|
|
||||
|
public PageToolbarItem( |
||||
|
[NotNull] Type componentType, |
||||
|
[CanBeNull] object arguments = null, |
||||
|
int order = 0) |
||||
|
{ |
||||
|
ComponentType = Check.NotNull(componentType, nameof(componentType)); |
||||
|
Arguments = arguments; |
||||
|
Order = order; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,9 @@ |
|||||
|
using System.Collections.Generic; |
||||
|
|
||||
|
namespace Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.PageToolbars |
||||
|
{ |
||||
|
public class PageToolbarItemList : List<PageToolbarItem> |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,44 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
using System.Threading.Tasks; |
||||
|
using Microsoft.Extensions.Options; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
|
||||
|
namespace Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.PageToolbars |
||||
|
{ |
||||
|
public class PageToolbarManager : IPageToolbarManager, ITransientDependency |
||||
|
{ |
||||
|
protected AbpPageToolbarOptions Options { get; } |
||||
|
protected IHybridServiceScopeFactory ServiceScopeFactory { get; } |
||||
|
|
||||
|
public PageToolbarManager( |
||||
|
IOptions<AbpPageToolbarOptions> options, |
||||
|
IHybridServiceScopeFactory serviceScopeFactory) |
||||
|
{ |
||||
|
Options = options.Value; |
||||
|
ServiceScopeFactory = serviceScopeFactory; |
||||
|
} |
||||
|
|
||||
|
public virtual async Task<PageToolbarItem[]> GetItemsAsync(string pageName) |
||||
|
{ |
||||
|
var toolbar = Options.Toolbars.GetOrDefault(pageName); |
||||
|
if (toolbar == null || !toolbar.Contributors.Any()) |
||||
|
{ |
||||
|
return Array.Empty<PageToolbarItem>(); |
||||
|
} |
||||
|
|
||||
|
using (var scope = ServiceScopeFactory.CreateScope()) |
||||
|
{ |
||||
|
var context = new PageToolbarContributionContext(pageName, scope.ServiceProvider); |
||||
|
|
||||
|
foreach (var contributor in toolbar.Contributors) |
||||
|
{ |
||||
|
await contributor.ContributeAsync(context); |
||||
|
} |
||||
|
|
||||
|
return context.Items.OrderBy(i => i.Order).ToArray(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,52 @@ |
|||||
|
using System; |
||||
|
using System.Threading.Tasks; |
||||
|
using Microsoft.AspNetCore.Authorization; |
||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
|
||||
|
namespace Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.PageToolbars |
||||
|
{ |
||||
|
public class SimplePageToolbarContributor : IPageToolbarContributor |
||||
|
{ |
||||
|
public Type ComponentType { get; } |
||||
|
|
||||
|
public object Argument { get; set; } |
||||
|
|
||||
|
public int Order { get; } |
||||
|
|
||||
|
public string RequiredPolicyName { get; } |
||||
|
|
||||
|
public SimplePageToolbarContributor( |
||||
|
Type componentType, |
||||
|
object argument = null, |
||||
|
int order = 0, |
||||
|
string requiredPolicyName = null) |
||||
|
{ |
||||
|
ComponentType = componentType; |
||||
|
Argument = argument; |
||||
|
Order = order; |
||||
|
RequiredPolicyName = requiredPolicyName; |
||||
|
} |
||||
|
|
||||
|
public async Task ContributeAsync(PageToolbarContributionContext context) |
||||
|
{ |
||||
|
if(await ShouldAddComponentAsync(context)) |
||||
|
{ |
||||
|
context.Items.Add(new PageToolbarItem(ComponentType, Argument, Order)); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
protected virtual async Task<bool> ShouldAddComponentAsync(PageToolbarContributionContext context) |
||||
|
{ |
||||
|
if (RequiredPolicyName != null) |
||||
|
{ |
||||
|
var authorizationService = context.ServiceProvider.GetRequiredService<IAuthorizationService>(); |
||||
|
if (!await authorizationService.IsGrantedAsync(RequiredPolicyName)) |
||||
|
{ |
||||
|
return false; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return true; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,22 @@ |
|||||
|
using System.Threading.Tasks; |
||||
|
using Microsoft.AspNetCore.Mvc; |
||||
|
using Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.PageToolbars; |
||||
|
|
||||
|
namespace Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.Pages.Shared.Components.AbpPageToolbar |
||||
|
{ |
||||
|
public class AbpPageToolbarViewComponent : AbpViewComponent |
||||
|
{ |
||||
|
private readonly IPageToolbarManager _toolbarManager; |
||||
|
|
||||
|
public AbpPageToolbarViewComponent(IPageToolbarManager toolbarManager) |
||||
|
{ |
||||
|
_toolbarManager = toolbarManager; |
||||
|
} |
||||
|
|
||||
|
public async Task<IViewComponentResult> InvokeAsync(string pageName) |
||||
|
{ |
||||
|
var items = await _toolbarManager.GetItemsAsync(pageName); |
||||
|
return View("~/Pages/Shared/Components/AbpPageToolbar/Default.cshtml", items); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,82 @@ |
|||||
|
using Microsoft.AspNetCore.Mvc; |
||||
|
using Microsoft.Extensions.Localization; |
||||
|
using Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers; |
||||
|
using Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Button; |
||||
|
using Volo.Abp.Localization; |
||||
|
|
||||
|
namespace Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.Pages.Shared.Components.AbpPageToolbar.Button |
||||
|
{ |
||||
|
public class AbpPageToolbarButtonViewComponent : AbpViewComponent |
||||
|
{ |
||||
|
protected IStringLocalizerFactory StringLocalizerFactory { get; } |
||||
|
|
||||
|
public AbpPageToolbarButtonViewComponent(IStringLocalizerFactory stringLocalizerFactory) |
||||
|
{ |
||||
|
StringLocalizerFactory = stringLocalizerFactory; |
||||
|
} |
||||
|
|
||||
|
public IViewComponentResult Invoke( |
||||
|
ILocalizableString text, |
||||
|
string name, |
||||
|
string icon, |
||||
|
string id, |
||||
|
ILocalizableString busyText, |
||||
|
FontIconType iconType, |
||||
|
AbpButtonType type, |
||||
|
AbpButtonSize size, |
||||
|
bool disabled) |
||||
|
{ |
||||
|
Check.NotNull(text, nameof(text)); |
||||
|
|
||||
|
return View( |
||||
|
"~/Pages/Shared/Components/AbpPageToolbar/Button/Default.cshtml", |
||||
|
new AbpPageToolbarButtonViewModel( |
||||
|
text.Localize(StringLocalizerFactory), |
||||
|
name, |
||||
|
icon, |
||||
|
id, |
||||
|
busyText?.Localize(StringLocalizerFactory), |
||||
|
iconType, |
||||
|
type, |
||||
|
size, |
||||
|
disabled |
||||
|
) |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
public class AbpPageToolbarButtonViewModel |
||||
|
{ |
||||
|
public string Text { get; } |
||||
|
public string Name { get; } |
||||
|
public string Icon { get; } |
||||
|
public string Id { get; } |
||||
|
public string BusyText { get; } |
||||
|
public FontIconType IconType { get; } |
||||
|
public AbpButtonType Type { get; } |
||||
|
public AbpButtonSize Size { get; } |
||||
|
public bool Disabled { get; } |
||||
|
|
||||
|
public AbpPageToolbarButtonViewModel( |
||||
|
string text, |
||||
|
string name, |
||||
|
string icon, |
||||
|
string id, |
||||
|
string busyText, |
||||
|
FontIconType iconType, |
||||
|
AbpButtonType type, |
||||
|
AbpButtonSize size, |
||||
|
bool disabled) |
||||
|
{ |
||||
|
Text = text; |
||||
|
Name = name; |
||||
|
Icon = icon; |
||||
|
Id = id; |
||||
|
BusyText = busyText; |
||||
|
IconType = iconType; |
||||
|
Type = type; |
||||
|
Size = size; |
||||
|
Disabled = disabled; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,13 @@ |
|||||
|
@model Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.Pages.Shared.Components.AbpPageToolbar.Button.AbpPageToolbarButtonViewComponent.AbpPageToolbarButtonViewModel |
||||
|
<abp-button |
||||
|
text="@Model.Text" |
||||
|
name="@Model.Name" |
||||
|
icon="@Model.Icon" |
||||
|
id="@Model.Id" |
||||
|
busy-text="@Model.BusyText" |
||||
|
icon-type="@Model.IconType" |
||||
|
button-type="@Model.Type" |
||||
|
size="@Model.Size" |
||||
|
disabled="@Model.Disabled" |
||||
|
class="mx-1" |
||||
|
/> |
||||
@ -0,0 +1,5 @@ |
|||||
|
@model Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.PageToolbars.PageToolbarItem[] |
||||
|
@foreach (var toolbarItem in Model) |
||||
|
{ |
||||
|
@(await Component.InvokeAsync(toolbarItem.ComponentType, toolbarItem.Arguments)) |
||||
|
} |
||||
@ -0,0 +1,4 @@ |
|||||
|
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers |
||||
|
@addTagHelper *, Volo.Abp.AspNetCore.Mvc.UI |
||||
|
@addTagHelper *, Volo.Abp.AspNetCore.Mvc.UI.Bootstrap |
||||
|
@addTagHelper *, Volo.Abp.AspNetCore.Mvc.UI.Bundling |
||||
Loading…
Reference in new issue