From 2ff244536bd103766fbce272f4fc4230453f5404 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Wed, 23 Sep 2020 21:18:22 +0300 Subject: [PATCH] Introduce toolbar system for the Blazor UI --- .../Toolbars/AbpToolbarOptions.cs | 16 +++++ .../Toolbars/IToolbarConfigurationContext.cs | 29 ++++++++ .../Toolbars/IToolbarContributor.cs | 9 +++ .../Toolbars/IToolbarManager.cs | 9 +++ .../Toolbars/StandardToolbars.cs | 7 ++ .../Toolbars/Toolbar.cs | 18 +++++ .../Toolbars/ToolbarConfigurationContext.cs | 68 +++++++++++++++++++ .../Toolbars/ToolbarItem.cs | 23 +++++++ .../Toolbars/ToolbarManager.cs | 39 +++++++++++ 9 files changed, 218 insertions(+) create mode 100644 framework/src/Volo.Abp.AspNetCore.Components.WebAssembly.Theming/Toolbars/AbpToolbarOptions.cs create mode 100644 framework/src/Volo.Abp.AspNetCore.Components.WebAssembly.Theming/Toolbars/IToolbarConfigurationContext.cs create mode 100644 framework/src/Volo.Abp.AspNetCore.Components.WebAssembly.Theming/Toolbars/IToolbarContributor.cs create mode 100644 framework/src/Volo.Abp.AspNetCore.Components.WebAssembly.Theming/Toolbars/IToolbarManager.cs create mode 100644 framework/src/Volo.Abp.AspNetCore.Components.WebAssembly.Theming/Toolbars/StandardToolbars.cs create mode 100644 framework/src/Volo.Abp.AspNetCore.Components.WebAssembly.Theming/Toolbars/Toolbar.cs create mode 100644 framework/src/Volo.Abp.AspNetCore.Components.WebAssembly.Theming/Toolbars/ToolbarConfigurationContext.cs create mode 100644 framework/src/Volo.Abp.AspNetCore.Components.WebAssembly.Theming/Toolbars/ToolbarItem.cs create mode 100644 framework/src/Volo.Abp.AspNetCore.Components.WebAssembly.Theming/Toolbars/ToolbarManager.cs diff --git a/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly.Theming/Toolbars/AbpToolbarOptions.cs b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly.Theming/Toolbars/AbpToolbarOptions.cs new file mode 100644 index 0000000000..a2b006117f --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly.Theming/Toolbars/AbpToolbarOptions.cs @@ -0,0 +1,16 @@ +using System.Collections.Generic; +using JetBrains.Annotations; + +namespace Volo.Abp.AspNetCore.Components.WebAssembly.Theming.Toolbars +{ + public class AbpToolbarOptions + { + [NotNull] + public List Contributors { get; } + + public AbpToolbarOptions() + { + Contributors = new List(); + } + } +} diff --git a/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly.Theming/Toolbars/IToolbarConfigurationContext.cs b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly.Theming/Toolbars/IToolbarConfigurationContext.cs new file mode 100644 index 0000000000..825b72b3f4 --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly.Theming/Toolbars/IToolbarConfigurationContext.cs @@ -0,0 +1,29 @@ +using System; +using System.Threading.Tasks; +using JetBrains.Annotations; +using Microsoft.AspNetCore.Authorization; +using Microsoft.Extensions.Localization; +using Volo.Abp.DependencyInjection; + +namespace Volo.Abp.AspNetCore.Components.WebAssembly.Theming.Toolbars +{ + public interface IToolbarConfigurationContext : IServiceProviderAccessor + { + Toolbar Toolbar { get; } + + IAuthorizationService AuthorizationService { get; } + + IStringLocalizerFactory StringLocalizerFactory { get; } + + Task IsGrantedAsync(string policyName); + + [CanBeNull] + IStringLocalizer GetDefaultLocalizer(); + + [NotNull] + public IStringLocalizer GetLocalizer(); + + [NotNull] + public IStringLocalizer GetLocalizer(Type resourceType); + } +} diff --git a/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly.Theming/Toolbars/IToolbarContributor.cs b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly.Theming/Toolbars/IToolbarContributor.cs new file mode 100644 index 0000000000..363741c22c --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly.Theming/Toolbars/IToolbarContributor.cs @@ -0,0 +1,9 @@ +using System.Threading.Tasks; + +namespace Volo.Abp.AspNetCore.Components.WebAssembly.Theming.Toolbars +{ + public interface IToolbarContributor + { + Task ConfigureToolbarAsync(IToolbarConfigurationContext context); + } +} \ No newline at end of file diff --git a/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly.Theming/Toolbars/IToolbarManager.cs b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly.Theming/Toolbars/IToolbarManager.cs new file mode 100644 index 0000000000..0f1a6b29d3 --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly.Theming/Toolbars/IToolbarManager.cs @@ -0,0 +1,9 @@ +using System.Threading.Tasks; + +namespace Volo.Abp.AspNetCore.Components.WebAssembly.Theming.Toolbars +{ + public interface IToolbarManager + { + Task GetAsync(string name); + } +} diff --git a/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly.Theming/Toolbars/StandardToolbars.cs b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly.Theming/Toolbars/StandardToolbars.cs new file mode 100644 index 0000000000..dfdf773422 --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly.Theming/Toolbars/StandardToolbars.cs @@ -0,0 +1,7 @@ +namespace Volo.Abp.AspNetCore.Components.WebAssembly.Theming.Toolbars +{ + public static class StandardToolbars + { + public const string Main = "Main"; + } +} \ No newline at end of file diff --git a/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly.Theming/Toolbars/Toolbar.cs b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly.Theming/Toolbars/Toolbar.cs new file mode 100644 index 0000000000..8ca6dfab24 --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly.Theming/Toolbars/Toolbar.cs @@ -0,0 +1,18 @@ +using System.Collections.Generic; +using JetBrains.Annotations; + +namespace Volo.Abp.AspNetCore.Components.WebAssembly.Theming.Toolbars +{ + public class Toolbar + { + public string Name { get; } + + public List Items { get; } + + public Toolbar([NotNull] string name) + { + Name = Check.NotNull(name, nameof(name)); + Items = new List(); + } + } +} diff --git a/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly.Theming/Toolbars/ToolbarConfigurationContext.cs b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly.Theming/Toolbars/ToolbarConfigurationContext.cs new file mode 100644 index 0000000000..7174dbe431 --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly.Theming/Toolbars/ToolbarConfigurationContext.cs @@ -0,0 +1,68 @@ +using System; +using System.Threading.Tasks; +using JetBrains.Annotations; +using Microsoft.AspNetCore.Authorization; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Localization; + +namespace Volo.Abp.AspNetCore.Components.WebAssembly.Theming.Toolbars +{ + public class ToolbarConfigurationContext : IToolbarConfigurationContext + { + public IServiceProvider ServiceProvider { get; } + private readonly object _serviceProviderLock = new object(); + + private TRef LazyGetRequiredService(Type serviceType, ref TRef reference) + { + if (reference == null) + { + lock (_serviceProviderLock) + { + if (reference == null) + { + reference = (TRef)ServiceProvider.GetRequiredService(serviceType); + } + } + } + + return reference; + } + + public IAuthorizationService AuthorizationService => LazyGetRequiredService(typeof(IAuthorizationService), ref _authorizationService); + private IAuthorizationService _authorizationService; + + private IStringLocalizerFactory _stringLocalizerFactory; + public IStringLocalizerFactory StringLocalizerFactory => LazyGetRequiredService(typeof(IStringLocalizerFactory),ref _stringLocalizerFactory); + + public Toolbar Toolbar { get; } + + public ToolbarConfigurationContext(Toolbar toolbar, IServiceProvider serviceProvider) + { + Toolbar = toolbar; + ServiceProvider = serviceProvider; + } + + public Task IsGrantedAsync(string policyName) + { + return AuthorizationService.IsGrantedAsync(policyName); + } + + [CanBeNull] + public IStringLocalizer GetDefaultLocalizer() + { + return StringLocalizerFactory.CreateDefaultOrNull(); + } + + [NotNull] + public IStringLocalizer GetLocalizer() + { + return StringLocalizerFactory.Create(); + } + + [NotNull] + public IStringLocalizer GetLocalizer(Type resourceType) + { + return StringLocalizerFactory.Create(resourceType); + } + } +} diff --git a/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly.Theming/Toolbars/ToolbarItem.cs b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly.Theming/Toolbars/ToolbarItem.cs new file mode 100644 index 0000000000..8400a9c88f --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly.Theming/Toolbars/ToolbarItem.cs @@ -0,0 +1,23 @@ +using System; +using JetBrains.Annotations; + +namespace Volo.Abp.AspNetCore.Components.WebAssembly.Theming.Toolbars +{ + public class ToolbarItem + { + public Type ComponentType + { + get => _componentType; + set => _componentType = Check.NotNull(value, nameof(value)); + } + private Type _componentType; + + public int Order { get; set; } + + public ToolbarItem([NotNull] Type componentType, int order = 0) + { + Order = order; + ComponentType = Check.NotNull(componentType, nameof(componentType)); + } + } +} \ No newline at end of file diff --git a/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly.Theming/Toolbars/ToolbarManager.cs b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly.Theming/Toolbars/ToolbarManager.cs new file mode 100644 index 0000000000..311795ac4f --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly.Theming/Toolbars/ToolbarManager.cs @@ -0,0 +1,39 @@ +using System; +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; +using Volo.Abp.DependencyInjection; + +namespace Volo.Abp.AspNetCore.Components.WebAssembly.Theming.Toolbars +{ + public class ToolbarManager : IToolbarManager, ITransientDependency + { + protected AbpToolbarOptions Options { get; } + protected IServiceProvider ServiceProvider { get; } + + public ToolbarManager( + IOptions options, + IServiceProvider serviceProvider) + { + ServiceProvider = serviceProvider; + Options = options.Value; + } + + public async Task GetAsync(string name) + { + var toolbar = new Toolbar(name); + + using (var scope = ServiceProvider.CreateScope()) + { + var context = new ToolbarConfigurationContext(toolbar, scope.ServiceProvider); + + foreach (var contributor in Options.Contributors) + { + await contributor.ConfigureToolbarAsync(context); + } + } + + return toolbar; + } + } +}