Skip to content

Commit

Permalink
Merge pull request #6647 from abpframework/ui-extensions-blazor
Browse files Browse the repository at this point in the history
Blazor UI extensions
  • Loading branch information
hikalkan committed Mar 24, 2021
2 parents 36a62b2 + d76b7a6 commit 10decfc
Show file tree
Hide file tree
Showing 65 changed files with 2,133 additions and 253 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Volo.Abp.AspNetCore.Components.Web\Volo.Abp.AspNetCore.Components.Web.csproj" />
<ProjectReference Include="..\Volo.Abp.Http.Client\Volo.Abp.Http.Client.csproj" />
<ProjectReference Include="..\Volo.Abp.AspNetCore.SignalR\Volo.Abp.AspNetCore.SignalR.csproj" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Volo.Abp.AspNetCore.Auditing;
using Volo.Abp.AspNetCore.Components.Web;
using Volo.Abp.AspNetCore.SignalR;
using Volo.Abp.AspNetCore.Uow;
using Volo.Abp.Http.Client;
using Volo.Abp.Modularity;

namespace Volo.Abp.AspNetCore.Components.Server
{
[DependsOn(
typeof(AbpHttpClientModule),
typeof(AbpAspNetCoreComponentsWebModule),
typeof(AbpAspNetCoreSignalRModule)
)]
public class AbpAspNetCoreComponentsServerModule : AbpModule
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using System;
using System.Globalization;
using System.Linq.Dynamic.Core;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Primitives;
using Volo.Abp.AspNetCore.Components.Web.Extensibility;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Http.Client;
using Volo.Abp.Http.Client.Authentication;
using Volo.Abp.MultiTenancy;

namespace Volo.Abp.AspNetCore.Components.Server.Extensibility
{
public class BlazorServerLookupApiRequestService : ILookupApiRequestService, ITransientDependency
{
public IHttpClientFactory HttpClientFactory { get; }
public IRemoteServiceHttpClientAuthenticator HttpClientAuthenticator { get; }

public AbpRemoteServiceOptions RemoteServiceOptions { get; }

public ICurrentTenant CurrentTenant { get; }
public IHttpContextAccessor HttpContextAccessor { get; }
public NavigationManager NavigationManager { get; }

public BlazorServerLookupApiRequestService(IHttpClientFactory httpClientFactory,
IRemoteServiceHttpClientAuthenticator httpClientAuthenticator,
ICurrentTenant currentTenant,
IOptions<AbpRemoteServiceOptions> remoteServiceOptions,
IHttpContextAccessor httpContextAccessor,
NavigationManager navigationManager)
{
HttpClientFactory = httpClientFactory;
HttpClientAuthenticator = httpClientAuthenticator;
RemoteServiceOptions = remoteServiceOptions.Value;
CurrentTenant = currentTenant;
HttpContextAccessor = httpContextAccessor;
NavigationManager = navigationManager;
}

public async Task<string> SendAsync(string url)
{
var client = HttpClientFactory.CreateClient();
var requestMessage = new HttpRequestMessage(HttpMethod.Get, url);

var uri = new Uri(url, UriKind.RelativeOrAbsolute);
if (!uri.IsAbsoluteUri)
{
var baseUrl = string.Empty;
try
{
//Blazor tiered -- mode
var remoteServiceConfig = RemoteServiceOptions.RemoteServices.GetConfigurationOrDefault("Default");
baseUrl = remoteServiceConfig.BaseUrl;
client.BaseAddress = new Uri(baseUrl);
AddHeaders(requestMessage);
await HttpClientAuthenticator.Authenticate(new RemoteServiceHttpClientAuthenticateContext(client,
requestMessage, new RemoteServiceConfiguration(baseUrl), string.Empty));
}
catch (AbpException) // Blazor-Server mode.
{
baseUrl = NavigationManager.BaseUri;
client.BaseAddress = new Uri(baseUrl);
foreach (var header in HttpContextAccessor.HttpContext.Request.Headers)
{
requestMessage.Headers.Add(header.Key, header.Value.ToArray());
}
}
}

var response = await client.SendAsync(requestMessage);
return await response.Content.ReadAsStringAsync();
}

protected virtual void AddHeaders(HttpRequestMessage requestMessage)
{
if (CurrentTenant.Id.HasValue)
{
requestMessage.Headers.Add(TenantResolverConsts.DefaultTenantKey, CurrentTenant.Id.Value.ToString());
}

var currentCulture = CultureInfo.CurrentUICulture.Name ?? CultureInfo.CurrentCulture.Name;
if (!currentCulture.IsNullOrEmpty())
{
requestMessage.Headers.AcceptLanguage.Add(new(currentCulture));
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<Row Class="entry-row">
@using Blazorise
<Row Class="entry-row">
<Column ColumnSize="ColumnSize.IsAuto">
<h1 class="content-header-title">@Title</h1>
</Column>
Expand Down Expand Up @@ -31,7 +32,12 @@
}
<Column>
<Row Class="justify-content-end mx-n1">
@ChildContent
@foreach (var toolbarItemRender in ToolbarItemRenders)
{
<Column ColumnSize="ColumnSize.IsAuto" Class="px-1 pt-2">
@toolbarItemRender
</Column>
}
</Row>
</Column>
</Row>
</Row>
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System;
using Microsoft.AspNetCore.Components;
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.AspNetCore.Components.Web.Theming.PageToolbars;
using Volo.Abp.BlazoriseUI;

namespace Volo.Abp.AspNetCore.Components.Web.Theming.Layout
{
public partial class PageHeader : ComponentBase
{
protected List<RenderFragment> ToolbarItemRenders { get; set; }

public IPageToolbarManager PageToolbarManager { get; set; }

[Parameter]
public string Title { get; set; }

[Parameter]
public bool BreadcrumbShowHome { get; set; } = true;

[Parameter]
public bool BreadcrumbShowCurrent { get; set; } = true;

[Parameter]
public RenderFragment ChildContent { get; set; }

[Parameter]
public List<BreadcrumbItem> BreadcrumbItems { get; set; }

[Parameter]
public PageToolbar Toolbar { get; set; }

[Parameter]
public string PageName { get; set; }

public PageHeader()
{
BreadcrumbItems = new List<BreadcrumbItem>();
ToolbarItemRenders = new List<RenderFragment>();
}

protected override async Task OnParametersSetAsync()
{
await base.OnParametersSetAsync();
if (Toolbar!=null)
{
Console.WriteLine("Toolbar is not null");
var toolbarItems = await PageToolbarManager.GetItemsAsync(Toolbar);
Console.WriteLine($"Toolbar item count:{toolbarItems.Length}");
ToolbarItemRenders.Clear();

foreach (var item in toolbarItems)
{
var sequence = 0;
ToolbarItemRenders.Add(builder =>
{
builder.OpenComponent(sequence, item.ComponentType);
if (item.Arguments != null)
{
foreach (var argument in item.Arguments)
{
sequence++;
builder.AddAttribute(sequence, argument.Key, argument.Value);
}
}
builder.CloseComponent();
});
}
}
}

protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Threading.Tasks;

namespace Volo.Abp.AspNetCore.Components.Web.Theming.PageToolbars
{
public interface IPageToolbarContributor
{
Task ContributeAsync(PageToolbarContributionContext context);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Threading.Tasks;

namespace Volo.Abp.AspNetCore.Components.Web.Theming.PageToolbars
{
public interface IPageToolbarManager
{
Task<PageToolbarItem[]> GetItemsAsync(PageToolbar toolbar);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Volo.Abp.AspNetCore.Components.Web.Theming.PageToolbars
{
public class PageToolbar
{
public PageToolbarContributorList Contributors { get; set; }

public PageToolbar()
{
Contributors = new PageToolbarContributorList();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using JetBrains.Annotations;
using System;

namespace Volo.Abp.AspNetCore.Components.Web.Theming.PageToolbars
{
public class PageToolbarContributionContext
{
[NotNull]
public IServiceProvider ServiceProvider { get; }

[NotNull]
public PageToolbarItemList Items { get; }

public PageToolbarContributionContext(
[NotNull] IServiceProvider serviceProvider)
{
ServiceProvider = Check.NotNull(serviceProvider, nameof(serviceProvider));
Items = new PageToolbarItemList();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Threading.Tasks;

namespace Volo.Abp.AspNetCore.Components.Web.Theming.PageToolbars
{
public abstract class PageToolbarContributor : IPageToolbarContributor
{
public abstract Task ContributeAsync(PageToolbarContributionContext context);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System.Collections.Generic;

namespace Volo.Abp.AspNetCore.Components.Web.Theming.PageToolbars
{
public class PageToolbarContributorList : List<IPageToolbarContributor>
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Collections.Generic;

namespace Volo.Abp.AspNetCore.Components.Web.Theming.PageToolbars
{
public class PageToolbarDictionary : Dictionary<string, PageToolbar>
{

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using Blazorise;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.BlazoriseUI.Components;
using Volo.Abp.Localization;

namespace Volo.Abp.AspNetCore.Components.Web.Theming.PageToolbars
{
public static class PageToolbarExtensions
{
public static PageToolbar AddComponent<TComponent>(
this PageToolbar toolbar,
Dictionary<string, object> arguments = null,
int order = 0,
string requiredPolicyName = null)
{
return toolbar.AddComponent(
typeof(TComponent),
arguments,
order,
requiredPolicyName
);
}

public static PageToolbar AddComponent(
this PageToolbar toolbar,
Type componentType,
Dictionary<string, object> arguments = null,
int order = 0,
string requiredPolicyName = null)
{
toolbar.Contributors.Add(
new SimplePageToolbarContributor(
componentType,
arguments,
order,
requiredPolicyName
)
);

return toolbar;
}

public static PageToolbar AddButton(
this PageToolbar toolbar,
string text,
Func<Task> clicked,
object icon = null,
Color color = Color.Primary,
bool disabled = false,
int order = 0,
string requiredPolicyName = null)
{
toolbar.AddComponent<ToolbarButton>(
new Dictionary<string, object>
{
{ nameof(ToolbarButton.Color), color},
{ nameof(ToolbarButton.Text), text},
{ nameof(ToolbarButton.Disabled), disabled},
{ nameof(ToolbarButton.Icon), icon},
{ nameof(ToolbarButton.Clicked),clicked},
},
order,
requiredPolicyName
);

return toolbar;
}
}
}

0 comments on commit 10decfc

Please sign in to comment.