-
-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathModal.razor.cs
More file actions
390 lines (312 loc) · 10.9 KB
/
Modal.razor.cs
File metadata and controls
390 lines (312 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
namespace BlazorBootstrap;
public partial class Modal : BlazorBootstrapComponentBase
{
#region Fields and Constants
private Type? childComponent = default!;
private ButtonColor footerButtonColor = ButtonColor.Secondary;
private string footerButtonCSSClass = string.Empty;
private string footerButtonText = string.Empty;
private bool isVisible;
private DotNetObjectReference<Modal> objRef = default!;
private Dictionary<string, object> parameters = default!;
private bool showFooterButton = false;
#endregion
#region Methods
/// <inheritdoc />
protected override async ValueTask DisposeAsyncCore(bool disposing)
{
if (disposing)
{
try
{
if (IsRenderComplete)
await JSRuntime.InvokeVoidAsync("window.blazorBootstrap.modal.dispose", Id);
}
catch (JSDisconnectedException)
{
// do nothing
}
objRef?.Dispose();
if (ModalService is not null && IsServiceModal)
ModalService.OnShow -= OnShowAsync;
}
await base.DisposeAsyncCore(disposing);
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
await JSRuntime.InvokeVoidAsync("window.blazorBootstrap.modal.initialize", Id, UseStaticBackdrop, CloseOnEscape, objRef);
await base.OnAfterRenderAsync(firstRender);
}
protected override async Task OnInitializedAsync()
{
objRef ??= DotNetObjectReference.Create(this);
if (ModalService is not null && IsServiceModal)
ModalService.OnShow += OnShowAsync;
await base.OnInitializedAsync();
}
[JSInvokable]
public async Task bsHiddenModal()
{
isVisible = false;
await OnHidden.InvokeAsync();
if (ModalService is not null && IsServiceModal)
ModalService.OnClose();
}
[JSInvokable]
public async Task bsHideModal() => await OnHiding.InvokeAsync();
[JSInvokable]
public async Task bsHidePreventedModal() => await OnHidePrevented.InvokeAsync();
[JSInvokable]
public async Task bsShowModal() => await OnShowing.InvokeAsync();
[JSInvokable]
public async Task bsShownModal() => await OnShown.InvokeAsync();
/// <summary>
/// Hides a modal.
/// </summary>
public async Task HideAsync()
{
await JSRuntime.InvokeVoidAsync("window.blazorBootstrap.modal.hide", Id);
}
/// <summary>
/// Opens a modal.
/// </summary>
public async Task ShowAsync(string? title = null, string? message = null) => await ShowAsync(title, message, null, null);
/// <summary>
/// Opens a modal.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="title"></param>
/// <param name="message"></param>
/// <param name="parameters"></param>
public async Task ShowAsync<T>(string title, string? message = null, Dictionary<string, object>? parameters = null) => await ShowAsync(title, message, typeof(T), parameters);
private Task OnShowAsync(ModalOption modalOption)
{
if (modalOption is null)
throw new ArgumentNullException(nameof(modalOption));
ModalType = modalOption.Type;
Size = modalOption.Size;
IsVerticallyCentered = modalOption.IsVerticallyCentered;
showFooterButton = modalOption.ShowFooterButton;
if (showFooterButton)
{
footerButtonColor = modalOption.FooterButtonColor;
footerButtonCSSClass = modalOption.FooterButtonCSSClass;
footerButtonText = modalOption.FooterButtonText;
FooterCssClass = "border-top-0";
}
return ShowAsync(modalOption.Title, modalOption.Message, null, null);
}
private async Task ShowAsync(string? title, string? message, Type? type, Dictionary<string, object>? parameters)
{
isVisible = true;
if (!string.IsNullOrWhiteSpace(title))
Title = title;
if (!string.IsNullOrWhiteSpace(message))
Message = message;
childComponent = type;
this.parameters = parameters!;
await InvokeAsync(StateHasChanged);
await JSRuntime.InvokeVoidAsync("window.blazorBootstrap.modal.show", Id);
}
#endregion
#region Properties, Indexers
protected override string? ClassNames =>
BuildClassNames(Class,
(BootstrapClass.Modal, true),
(BootstrapClass.ModalFade, true));
/// <summary>
/// Gets or sets the body CSS class.
/// </summary>
/// <remarks>
/// Default value is null.
/// </remarks>
[Parameter]
public string BodyCssClass { get; set; } = default!;
/// <summary>
/// Gets or sets the body template.
/// </summary>
/// <remarks>
/// Default value is null.
/// </remarks>
[Parameter]
public RenderFragment BodyTemplate { get; set; } = default!;
/// <summary>
/// Gets or sets the close icon color.
/// </summary>
/// <remarks>
/// Default value is <see cref="IconColor.None" />.
/// </remarks>
[Parameter]
public IconColor CloseIconColor { get; set; } = IconColor.None;
/// <summary>
/// Indicates whether the modal closes when escape key is pressed.
/// </summary>
/// <remarks>
/// Default value is true.
/// </remarks>
[Parameter]
public bool CloseOnEscape { get; set; } = true;
/// <summary>
/// Gets or sets the modal dialog (div.modal-dialog) CSS class.
/// </summary>
/// <remarks>
/// Default value is null.
/// </remarks>
[Parameter]
public string DialogCssClass { get; set; } = default!;
/// <summary>
/// Gets or sets the footer CSS class.
/// </summary>
/// <remarks>
/// Default value is null.
/// </remarks>
[Parameter]
public string FooterCssClass { get; set; } = default!;
/// <summary>
/// Gets or sets the footer template.
/// </summary>
/// <remarks>
/// Default value is null.
/// </remarks>
[Parameter]
public RenderFragment FooterTemplate { get; set; } = default!;
/// <summary>
/// Gets or sets the fullscreen behavior of the modal.
/// </summary>
/// <remarks>
/// Default value is <see cref="ModalFullscreen.Disabled" />.
/// </remarks>
[Parameter]
public ModalFullscreen Fullscreen { get; set; } = ModalFullscreen.Disabled;
/// <summary>
/// Gets or sets the header CSS class.
/// </summary>
/// <remarks>
/// Default value is null.
/// </remarks>
[Parameter]
public string HeaderCssClass { get; set; } = default!;
// Modal close "X" button with bootstrap 5.3.3 - https://github.com/vikramlearning/blazorbootstrap/issues/714
// Review this fix after bootstrap 5.3.4 or 5.4 release. Ref: https://github.com/twbs/bootstrap/issues/39798
private string headerCssClassInternal => $"justify-content-between {ModalType.ToModalHeaderColorClass()}".Trim();
/// <summary>
/// Gets or sets the header template.
/// </summary>
/// <remarks>
/// Default value is null.
/// </remarks>
[Parameter]
public RenderFragment HeaderTemplate { get; set; } = default!;
/// <summary>
/// If <see langword="true" />, scroll will be enabled on the modal body.
/// </summary>
/// <remarks>
/// Default value is false.
/// </remarks>
[Parameter]
public bool IsScrollable { get; set; }
/// <summary>
/// Indicates whether the modal is related to a modal service or not.
/// </summary>
/// <remarks>
/// Default value is false.
/// </remarks>
[Parameter]
public bool IsServiceModal { get; set; }
/// <summary>
/// If <see langword="true" />, shows the modal vertically in the center.
/// </summary>
/// <remarks>
/// Default value is false.
/// </remarks>
[Parameter]
public bool IsVerticallyCentered { get; set; }
/// <summary>
/// Gets or sets the message.
/// </summary>
/// <remarks>
/// Default value is null.
/// </remarks>
[Parameter]
public string Message { get; set; } = default!;
private string modalFullscreen => Fullscreen.ToModalFullscreenClass();
[Inject] private ModalService ModalService { get; set; } = default!;
private string modalSize => Size.ToModalSizeClass();
/// <summary>
/// Gets or sets the modal type.
/// </summary>
/// <remarks>
/// Default value is <see cref="ModalType.None" />.
/// </remarks>
[Parameter]
public ModalType ModalType { get; set; } = ModalType.None;
/// <summary>
/// This event is fired when an offcanvas element has been hidden from the user (will wait for CSS transitions to
/// complete).
/// </summary>
[Parameter]
public EventCallback OnHidden { get; set; }
/// <summary>
/// This event is fired when the modal is shown, its backdrop is static and a click outside the modal or an escape key
/// press is performed with the keyboard option or data-bs-keyboard set to false.
/// </summary>
[Parameter]
public EventCallback OnHidePrevented { get; set; }
/// <summary>
/// This event is fired immediately when the hide method has been called.
/// </summary>
[Parameter]
public EventCallback OnHiding { get; set; }
/// <summary>
/// This event fires immediately when the show instance method is called.
/// </summary>
[Parameter]
public EventCallback OnShowing { get; set; }
/// <summary>
/// This event is fired when an offcanvas element has been made visible to the user (will wait for CSS transitions to
/// complete).
/// </summary>
[Parameter]
public EventCallback OnShown { get; set; }
private string scrollable => IsScrollable ? "modal-dialog-scrollable" : "";
/// <summary>
/// If <see langword="true" />, close button will be visible in the modal header.
/// </summary>
/// <remarks>
/// Default value is true.
/// </remarks>
[Parameter]
public bool ShowCloseButton { get; set; } = true;
/// <summary>
/// Gets or sets the modal size.
/// </summary>
/// <remarks>
/// Default value is <see cref="ModalSize.Regular" />.
/// </remarks>
[Parameter]
public ModalSize Size { get; set; } = ModalSize.Regular;
/// <summary>
/// Gets or sets the modal tab index.
/// </summary>
/// <remarks>
/// Default value is -1.
/// </remarks>
[Parameter]
public int TabIndex { get; set; } = -1;
/// <summary>
/// Gets or sets the modal header title.
/// </summary>
[Parameter]
public string Title { get; set; } = default!;
/// <summary>
/// Indicates whether the modal uses a static backdrop.
/// </summary>
/// <remarks>
/// Default value is false.
/// </remarks>
[Parameter]
public bool UseStaticBackdrop { get; set; }
private string verticallyCentered => IsVerticallyCentered ? "modal-dialog-centered" : "";
#endregion
}