Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<Download></Download>
<Mask></Mask>
<ConnectionHub></ConnectionHub>

<BootstrapBlazorRootOutlet></BootstrapBlazorRootOutlet>
@foreach (var com in Generators)
{
@com.Generator()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License
// See the LICENSE file in the project root for more information.
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone

namespace BootstrapBlazor.Components;

/// <summary>
/// BootstrapBlazorRootContent Component
/// </summary>
public class BootstrapBlazorRootContent : IComponent, IDisposable
{
private object? _registeredIdentifier;

/// <summary>
/// Gets or sets the <see cref="string"/> ID that determines which <see cref="BootstrapBlazorRootOutlet"/> instance will render
/// the content of this instance.
/// </summary>
[Parameter] public string? RootName { get; set; }

/// <summary>
/// Gets or sets the <see cref="object"/> ID that determines which <see cref="BootstrapBlazorRootOutlet"/> instance will render
/// the content of this instance.
/// </summary>
[Parameter] public object? RootId { get; set; }

/// <summary>
/// Gets or sets the content.
/// </summary>
[Parameter]
public RenderFragment? ChildContent { get; set; }

[Inject]
private BootstrapBlazorRootRegisterService RootRegisterService { get; set; } = default!;

/// <summary>
/// <inheritdoc/>
/// </summary>
/// <param name="renderHandle"></param>
void IComponent.Attach(RenderHandle renderHandle)
{

}

/// <summary>
/// <inheritdoc/>
/// </summary>
/// <param name="parameters"></param>
/// <returns></returns>
Task IComponent.SetParametersAsync(ParameterView parameters)
{
parameters.SetParameterProperties(this);

object? identifier = null;

if (RootName is not null && RootId is not null)
{
throw new InvalidOperationException($"{nameof(BootstrapBlazorRootContent)} requires that '{nameof(RootName)}' and '{nameof(RootId)}' cannot both have non-null values.");
}
else if (RootName is not null)
{
identifier = RootName;
}
else if (RootId is not null)
{
identifier = RootId;
}
identifier ??= BootstrapBlazorRootOutlet.DefaultIdentifier;

if (!Equals(identifier, _registeredIdentifier))
{
if (_registeredIdentifier is not null)
{
RootRegisterService.RemoveProvider(_registeredIdentifier, this);
}

RootRegisterService.AddProvider(identifier, this);
_registeredIdentifier = identifier;
}

RootRegisterService.NotifyContentProviderChanged(identifier, this);
return Task.CompletedTask;
}

/// <summary>
/// <inheritdoc/>
/// </summary>
public void Dispose()
{
if (_registeredIdentifier is not null)
{
RootRegisterService.RemoveProvider(_registeredIdentifier, this);
}
GC.SuppressFinalize(this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License
// See the LICENSE file in the project root for more information.
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone

using Microsoft.AspNetCore.Components.Rendering;

namespace BootstrapBlazor.Components;

/// <summary>
/// BootstrapBlazorRootOutlet Component
/// </summary>
public class BootstrapBlazorRootOutlet : IComponent, IDisposable
{
private static readonly RenderFragment _emptyRenderFragment = _ => { };
private object? _subscribedIdentifier;
private RenderHandle _renderHandle;

/// <summary>
/// Gets the default identifier that can be used to subscribe to all <see cref="BootstrapBlazorRootContent"/> instances.
/// </summary>
public static readonly object DefaultIdentifier = new();

[Inject]
private BootstrapBlazorRootRegisterService RootRegisterService { get; set; } = default!;

/// <summary>
/// Gets or sets the <see cref="string"/> ID that determines which <see cref="BootstrapBlazorRootContent"/> instances will provide
/// content to this instance.
/// </summary>
[Parameter]
public string? RootName { get; set; }

/// <summary>
/// Gets or sets the <see cref="object"/> ID that determines which <see cref="BootstrapBlazorRootContent"/> instances will provide
/// content to this instance.
/// </summary>
[Parameter]
public object? RootId { get; set; }

void IComponent.Attach(RenderHandle renderHandle)
{
_renderHandle = renderHandle;
}

/// <summary>
/// <inheritdoc/>
/// </summary>
/// <param name="parameters"></param>
/// <returns></returns>
Task IComponent.SetParametersAsync(ParameterView parameters)
{
parameters.SetParameterProperties(this);

object? identifier = null;

if (RootName is not null && RootId is not null)
{
throw new InvalidOperationException($"{nameof(BootstrapBlazorRootOutlet)} requires that '{nameof(RootName)}' and '{nameof(RootId)}' cannot both have non-null values.");
}
else if (RootName is not null)
{
identifier = RootName;
}
else if (RootId is not null)
{
identifier = RootId;
}
identifier ??= DefaultIdentifier;

if (!Equals(identifier, _subscribedIdentifier))
{
if (_subscribedIdentifier is not null)
{
RootRegisterService.Unsubscribe(_subscribedIdentifier);
}

RootRegisterService.Subscribe(identifier, this);
_subscribedIdentifier = identifier;
}

RenderContent();
return Task.CompletedTask;
}

internal void ContentUpdated(BootstrapBlazorRootContent? provider)
{
RenderContent();
}

private void RenderContent()
{
_renderHandle.Render(BuildRenderTree);
}

/// <summary>
/// <inheritdoc/>
/// </summary>
/// <param name="builder"></param>
private void BuildRenderTree(RenderTreeBuilder builder)
{
if (_subscribedIdentifier is not null)
{
foreach (var content in RootRegisterService.GetProviders(_subscribedIdentifier))
{
builder.OpenComponent<BootstrapBlazorRootOutletContentRenderer>(0);
builder.SetKey(content);
builder.AddAttribute(1, BootstrapBlazorRootOutletContentRenderer.ContentParameterName, content.ChildContent ?? _emptyRenderFragment);
builder.CloseComponent();
}
}
}

/// <summary>
/// <inheritdoc/>
/// </summary>
public void Dispose()
{
if (_subscribedIdentifier is not null)
{
RootRegisterService.Unsubscribe(_subscribedIdentifier);
}
GC.SuppressFinalize(this);
}

internal sealed class BootstrapBlazorRootOutletContentRenderer : IComponent
{
public const string ContentParameterName = "content";

private RenderHandle _renderHandle;

public void Attach(RenderHandle renderHandle)
{
_renderHandle = renderHandle;
}

public Task SetParametersAsync(ParameterView parameters)
{
var fragment = parameters.GetValueOrDefault<RenderFragment>(ContentParameterName)!;
_renderHandle.Render(fragment);
return Task.CompletedTask;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ public static IServiceCollection AddBootstrapBlazor(this IServiceCollection serv
services.TryAddSingleton<IZipArchiveService, DefaultZipArchiveService>();
services.TryAddSingleton(typeof(IDispatchService<>), typeof(DefaultDispatchService<>));

// BootstrapBlazorRootRegisterService 服务
services.AddScoped<BootstrapBlazorRootRegisterService>();

// Html2Pdf 服务
services.TryAddSingleton<IHtml2Pdf, DefaultHtml2PdfService>();

Expand Down
130 changes: 130 additions & 0 deletions src/BootstrapBlazor/Services/BootstrapBlazorRootRegisterService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License
// See the LICENSE file in the project root for more information.
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone

namespace BootstrapBlazor.Components;

/// <summary>
/// BootstrapBlazorRootRegisterService
/// </summary>
public class BootstrapBlazorRootRegisterService
{
private readonly Dictionary<object, BootstrapBlazorRootOutlet> _subscribersByIdentifier = [];
private readonly Dictionary<object, List<BootstrapBlazorRootContent>> _providersByIdentifier = [];

/// <summary>
/// add provider
/// </summary>
/// <param name="identifier"></param>
/// <param name="provider"></param>
public void AddProvider(object identifier, BootstrapBlazorRootContent provider)
{
if (!_providersByIdentifier.TryGetValue(identifier, out var providers))
{
providers = [];
_providersByIdentifier.Add(identifier, providers);
}

providers.Add(provider);
}

/// <summary>
/// remove provider
/// </summary>
/// <param name="identifier"></param>
/// <param name="provider"></param>
public void RemoveProvider(object identifier, BootstrapBlazorRootContent provider)
{
if (!_providersByIdentifier.TryGetValue(identifier, out var providers))
{
throw new InvalidOperationException($"There are no content providers with the given root ID '{identifier}'.");
}

var index = providers.LastIndexOf(provider);
if (index < 0)
{
throw new InvalidOperationException($"The provider was not found in the providers list of the given root ID '{identifier}'.");
}

providers.RemoveAt(index);
if (index == providers.Count)
{
// We just removed the most recently added provider, meaning we need to change
// the current content to that of second most recently added provider.
var contentProvider = GetCurrentProviderContentOrDefault(providers);
NotifyContentChangedForSubscriber(identifier, contentProvider);
}
}

/// <summary>
/// get all providers by identifier
/// </summary>
/// <param name="identifier"></param>
/// <returns></returns>
public List<BootstrapBlazorRootContent> GetProviders(object identifier)
{
_providersByIdentifier.TryGetValue(identifier, out var providers);
return providers ?? [];
}

/// <summary>
/// subscribe
/// </summary>
/// <param name="identifier"></param>
/// <param name="subscriber"></param>
public void Subscribe(object identifier, BootstrapBlazorRootOutlet subscriber)
{
if (_subscribersByIdentifier.ContainsKey(identifier))
{
throw new InvalidOperationException($"There is already a subscriber to the content with the given root ID '{identifier}'.");
}

_subscribersByIdentifier.Add(identifier, subscriber);
}

/// <summary>
/// 取消订阅
/// </summary>
/// <param name="identifier"></param>
public void Unsubscribe(object identifier)
{
if (!_subscribersByIdentifier.Remove(identifier))
{
throw new InvalidOperationException($"The subscriber with the given root ID '{identifier}' is already unsubscribed.");
}
}

/// <summary>
/// Notify content provider changed
/// </summary>
/// <param name="identifier"></param>
/// <param name="provider"></param>
public void NotifyContentProviderChanged(object identifier, BootstrapBlazorRootContent provider)
{
if (!_providersByIdentifier.TryGetValue(identifier, out var providers))
{
throw new InvalidOperationException($"There are no content providers with the given root ID '{identifier}'.");
}

// We only notify content changed for subscribers when the content of the
// most recently added provider changes.
if (providers.Count != 0 && providers[^1] == provider)
{
NotifyContentChangedForSubscriber(identifier, provider);
}
}

private static BootstrapBlazorRootContent? GetCurrentProviderContentOrDefault(List<BootstrapBlazorRootContent> providers)
=> providers.Count != 0
? providers[^1]
: null;

private void NotifyContentChangedForSubscriber(object identifier, BootstrapBlazorRootContent? provider)
{
if (_subscribersByIdentifier.TryGetValue(identifier, out var subscriber))
{
subscriber.ContentUpdated(provider);
}
}
}
Loading