|
| 1 | +namespace BlazorExpress.Core; |
| 2 | + |
| 3 | +public abstract class BlazorExpressLayoutComponentCore : LayoutComponentBase, IDisposable, IAsyncDisposable |
| 4 | +{ |
| 5 | + #region Fields and Constants |
| 6 | + |
| 7 | + private bool isAsyncDisposed; |
| 8 | + |
| 9 | + private bool isDisposed; |
| 10 | + |
| 11 | + protected Queue<Func<Task>> queuedTasks = new(); |
| 12 | + |
| 13 | + #endregion |
| 14 | + |
| 15 | + #region Methods |
| 16 | + |
| 17 | + /// <inheritdoc /> |
| 18 | + protected override async Task OnAfterRenderAsync(bool firstRender) |
| 19 | + { |
| 20 | + // process queued tasks |
| 21 | + while (queuedTasks.TryDequeue(out var taskToExecute)) |
| 22 | + await taskToExecute.Invoke(); |
| 23 | + |
| 24 | + if (firstRender) |
| 25 | + IsFirstRenderComplete = true; |
| 26 | + } |
| 27 | + |
| 28 | + /// <inheritdoc /> |
| 29 | + protected override void OnInitialized() |
| 30 | + { |
| 31 | + Id ??= IdUtility.GetNextId(); |
| 32 | + } |
| 33 | + |
| 34 | + public static string BuildClassNames(params (string? cssClass, bool when)[] cssClassList) |
| 35 | + { |
| 36 | + var list = new HashSet<string>(); |
| 37 | + |
| 38 | + if (cssClassList is not null && cssClassList.Any()) |
| 39 | + foreach (var (cssClass, when) in cssClassList) |
| 40 | + if (!string.IsNullOrWhiteSpace(cssClass) && when) |
| 41 | + list.Add(cssClass); |
| 42 | + |
| 43 | + if (list.Any()) |
| 44 | + return string.Join(" ", list); |
| 45 | + |
| 46 | + return string.Empty; |
| 47 | + } |
| 48 | + |
| 49 | + public static string BuildClassNames(string? userDefinedCssClass, params (string? cssClass, bool when)[] cssClassList) |
| 50 | + { |
| 51 | + var list = new HashSet<string>(); |
| 52 | + |
| 53 | + if (cssClassList is not null && cssClassList.Any()) |
| 54 | + foreach (var (cssClass, when) in cssClassList) |
| 55 | + if (!string.IsNullOrWhiteSpace(cssClass) && when) |
| 56 | + list.Add(cssClass); |
| 57 | + |
| 58 | + if (!string.IsNullOrWhiteSpace(userDefinedCssClass)) |
| 59 | + list.Add(userDefinedCssClass.Trim()); |
| 60 | + |
| 61 | + if (list.Any()) |
| 62 | + return string.Join(" ", list); |
| 63 | + |
| 64 | + return string.Empty; |
| 65 | + } |
| 66 | + |
| 67 | + public static string BuildStyleNames(string? userDefinedCssStyle, params (string? cssStyle, bool when)[] cssStyleList) |
| 68 | + { |
| 69 | + var list = new HashSet<string>(); |
| 70 | + |
| 71 | + if (cssStyleList is not null && cssStyleList.Any()) |
| 72 | + foreach (var (cssStyle, when) in cssStyleList) |
| 73 | + if (!string.IsNullOrWhiteSpace(cssStyle) && when) |
| 74 | + list.Add(cssStyle); |
| 75 | + |
| 76 | + if (!string.IsNullOrWhiteSpace(userDefinedCssStyle)) |
| 77 | + list.Add(userDefinedCssStyle.Trim()); |
| 78 | + |
| 79 | + if (list.Any()) |
| 80 | + return string.Join(';', list); |
| 81 | + |
| 82 | + return string.Empty; |
| 83 | + } |
| 84 | + |
| 85 | + /// <inheritdoc /> |
| 86 | + /// <seealso cref="https://learn.microsoft.com/en-us/dotnet/api/system.idisposable?view=net-6.0" /> |
| 87 | + public void Dispose() |
| 88 | + { |
| 89 | + Dispose(true); |
| 90 | + GC.SuppressFinalize(this); |
| 91 | + } |
| 92 | + |
| 93 | + /// <inheritdoc /> |
| 94 | + /// <seealso |
| 95 | + /// cref="https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-disposeasync#implement-both-dispose-and-async-dispose-patterns" /> |
| 96 | + public async ValueTask DisposeAsync() |
| 97 | + { |
| 98 | + await DisposeAsyncCore(true).ConfigureAwait(false); |
| 99 | + |
| 100 | + Dispose(false); |
| 101 | + GC.SuppressFinalize(this); |
| 102 | + } |
| 103 | + |
| 104 | + protected virtual void Dispose(bool disposing) |
| 105 | + { |
| 106 | + if (!isDisposed) |
| 107 | + { |
| 108 | + if (disposing) |
| 109 | + { |
| 110 | + // cleanup |
| 111 | + } |
| 112 | + |
| 113 | + isDisposed = true; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + protected virtual ValueTask DisposeAsyncCore(bool disposing) |
| 118 | + { |
| 119 | + if (!isAsyncDisposed) |
| 120 | + { |
| 121 | + if (disposing) |
| 122 | + { |
| 123 | + // cleanup |
| 124 | + } |
| 125 | + |
| 126 | + isAsyncDisposed = true; |
| 127 | + } |
| 128 | + |
| 129 | + return ValueTask.CompletedTask; |
| 130 | + } |
| 131 | + |
| 132 | + #endregion |
| 133 | + |
| 134 | + #region Properties, Indexers |
| 135 | + |
| 136 | + [Parameter(CaptureUnmatchedValues = true)] public Dictionary<string, object> AdditionalAttributes { get; set; } = default!; |
| 137 | + |
| 138 | + /// <summary> |
| 139 | + /// Gets or sets the CSS class. |
| 140 | + /// <para> |
| 141 | + /// Default value is <see langword="null" />. |
| 142 | + /// </para> |
| 143 | + /// </summary> |
| 144 | + [AddedVersion("1.0.0")] |
| 145 | + [DefaultValue(null)] |
| 146 | + [Description("Gets or sets the CSS class.")] |
| 147 | + [Parameter] |
| 148 | + public string? Class { get; set; } |
| 149 | + |
| 150 | + protected virtual string? ClassNames => Class; |
| 151 | + |
| 152 | + /// <summary> |
| 153 | + /// Gets or sets the associated <see cref="ElementReference" />. |
| 154 | + /// <para> |
| 155 | + /// May be <see langword="null" />, if accessed before the component is rendered. |
| 156 | + /// </para> |
| 157 | + /// </summary> |
| 158 | + [DisallowNull] |
| 159 | + public ElementReference? Element { get; set; } |
| 160 | + |
| 161 | + /// <summary> |
| 162 | + /// Gets or sets the ID. If not set, a unique ID will be generated. |
| 163 | + /// <para> |
| 164 | + /// Default value is <see langword="null" />. |
| 165 | + /// </para> |
| 166 | + /// </summary> |
| 167 | + [AddedVersion("1.0.0")] |
| 168 | + [DefaultValue(null)] |
| 169 | + [Description("Gets or sets the ID. If not set, a unique ID will be generated.")] |
| 170 | + [Parameter] |
| 171 | + public string? Id { get; set; } |
| 172 | + |
| 173 | + protected bool IsFirstRenderComplete { get; private set; } |
| 174 | + |
| 175 | + [Inject] protected IJSRuntime JSRuntime { get; set; } = default!; |
| 176 | + |
| 177 | + /// <summary> |
| 178 | + /// Gets or sets the CSS style. |
| 179 | + /// <para> |
| 180 | + /// Default value is <see langword="null" />. |
| 181 | + /// </para> |
| 182 | + /// </summary> |
| 183 | + [AddedVersion("1.0.0")] |
| 184 | + [DefaultValue(null)] |
| 185 | + [Description("Gets or sets the CSS style.")] |
| 186 | + [Parameter] |
| 187 | + public string? Style { get; set; } |
| 188 | + |
| 189 | + protected virtual string? StyleNames => Style; |
| 190 | + |
| 191 | + #endregion |
| 192 | + |
| 193 | + #region Other |
| 194 | + |
| 195 | + ~BlazorExpressLayoutComponentCore() |
| 196 | + { |
| 197 | + Dispose(false); |
| 198 | + } |
| 199 | + |
| 200 | + #endregion |
| 201 | +} |
0 commit comments