|
3 | 3 | // See the LICENSE file in the project root for more information. |
4 | 4 | // Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone |
5 | 5 |
|
| 6 | +using Microsoft.AspNetCore.Components.Rendering; |
| 7 | + |
6 | 8 | namespace BootstrapBlazor.Components; |
7 | 9 |
|
8 | 10 | /// <summary> |
9 | | -/// BootstrapBlazorRootOutlet 组件 |
| 11 | +/// BootstrapBlazorRootOutlet Component |
10 | 12 | /// </summary> |
11 | | -public partial class BootstrapBlazorRootOutlet : ComponentBase |
| 13 | +public class BootstrapBlazorRootOutlet : IComponent, IDisposable |
12 | 14 | { |
| 15 | + private static readonly RenderFragment _emptyRenderFragment = _ => { }; |
| 16 | + private object? _subscribedIdentifier; |
| 17 | + private RenderHandle _renderHandle; |
| 18 | + |
| 19 | + /// <summary> |
| 20 | + /// Gets the default identifier that can be used to subscribe to all <see cref="BootstrapBlazorRootContent"/> instances. |
| 21 | + /// </summary> |
| 22 | + public static readonly object DefaultIdentifier = new(); |
| 23 | + |
| 24 | + [Inject] |
| 25 | + private BootstrapBlazorRootRegisterService RootRegisterService { get; set; } = default!; |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Gets or sets the <see cref="string"/> ID that determines which <see cref="BootstrapBlazorRootContent"/> instances will provide |
| 29 | + /// content to this instance. |
| 30 | + /// </summary> |
| 31 | + [Parameter] |
| 32 | + public string? RootName { get; set; } |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Gets or sets the <see cref="object"/> ID that determines which <see cref="BootstrapBlazorRootContent"/> instances will provide |
| 36 | + /// content to this instance. |
| 37 | + /// </summary> |
| 38 | + [Parameter] |
| 39 | + public object? RootId { get; set; } |
| 40 | + |
| 41 | + void IComponent.Attach(RenderHandle renderHandle) |
| 42 | + { |
| 43 | + _renderHandle = renderHandle; |
| 44 | + } |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// <inheritdoc/> |
| 48 | + /// </summary> |
| 49 | + /// <param name="parameters"></param> |
| 50 | + /// <returns></returns> |
| 51 | + Task IComponent.SetParametersAsync(ParameterView parameters) |
| 52 | + { |
| 53 | + parameters.SetParameterProperties(this); |
| 54 | + |
| 55 | + object? identifier = null; |
| 56 | + |
| 57 | + if (RootName is not null && RootId is not null) |
| 58 | + { |
| 59 | + throw new InvalidOperationException($"{nameof(BootstrapBlazorRootOutlet)} requires that '{nameof(RootName)}' and '{nameof(RootId)}' cannot both have non-null values."); |
| 60 | + } |
| 61 | + else if (RootName is not null) |
| 62 | + { |
| 63 | + identifier = RootName; |
| 64 | + } |
| 65 | + else if (RootId is not null) |
| 66 | + { |
| 67 | + identifier = RootId; |
| 68 | + } |
| 69 | + identifier ??= DefaultIdentifier; |
| 70 | + |
| 71 | + if (!Equals(identifier, _subscribedIdentifier)) |
| 72 | + { |
| 73 | + if (_subscribedIdentifier is not null) |
| 74 | + { |
| 75 | + RootRegisterService.Unsubscribe(_subscribedIdentifier); |
| 76 | + } |
| 77 | + |
| 78 | + RootRegisterService.Subscribe(identifier, this); |
| 79 | + _subscribedIdentifier = identifier; |
| 80 | + } |
| 81 | + |
| 82 | + RenderContent(); |
| 83 | + return Task.CompletedTask; |
| 84 | + } |
| 85 | + |
| 86 | + internal void ContentUpdated(BootstrapBlazorRootContent? provider) |
| 87 | + { |
| 88 | + RenderContent(); |
| 89 | + } |
| 90 | + |
| 91 | + private void RenderContent() |
| 92 | + { |
| 93 | + _renderHandle.Render(BuildRenderTree); |
| 94 | + } |
| 95 | + |
| 96 | + /// <summary> |
| 97 | + /// <inheritdoc/> |
| 98 | + /// </summary> |
| 99 | + /// <param name="builder"></param> |
| 100 | + private void BuildRenderTree(RenderTreeBuilder builder) |
| 101 | + { |
| 102 | + if (_subscribedIdentifier is not null) |
| 103 | + { |
| 104 | + foreach (var content in RootRegisterService.GetProviders(_subscribedIdentifier)) |
| 105 | + { |
| 106 | + builder.OpenComponent<BootstrapBlazorRootOutletContentRenderer>(0); |
| 107 | + builder.SetKey(content); |
| 108 | + builder.AddAttribute(1, BootstrapBlazorRootOutletContentRenderer.ContentParameterName, content.ChildContent ?? _emptyRenderFragment); |
| 109 | + builder.CloseComponent(); |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + /// <summary> |
| 115 | + /// <inheritdoc/> |
| 116 | + /// </summary> |
| 117 | + public void Dispose() |
| 118 | + { |
| 119 | + if (_subscribedIdentifier is not null) |
| 120 | + { |
| 121 | + RootRegisterService.Unsubscribe(_subscribedIdentifier); |
| 122 | + } |
| 123 | + GC.SuppressFinalize(this); |
| 124 | + } |
| 125 | + |
| 126 | + internal sealed class BootstrapBlazorRootOutletContentRenderer : IComponent |
| 127 | + { |
| 128 | + public const string ContentParameterName = "content"; |
| 129 | + |
| 130 | + private RenderHandle _renderHandle; |
| 131 | + |
| 132 | + public void Attach(RenderHandle renderHandle) |
| 133 | + { |
| 134 | + _renderHandle = renderHandle; |
| 135 | + } |
13 | 136 |
|
| 137 | + public Task SetParametersAsync(ParameterView parameters) |
| 138 | + { |
| 139 | + var fragment = parameters.GetValueOrDefault<RenderFragment>(ContentParameterName)!; |
| 140 | + _renderHandle.Render(fragment); |
| 141 | + return Task.CompletedTask; |
| 142 | + } |
| 143 | + } |
14 | 144 | } |
0 commit comments