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