|
| 1 | +@inject IBlazorDialogService dialogService |
| 2 | +@namespace BlazorDialog |
| 3 | +@implements IDisposable |
| 4 | +@if (isShowing) |
| 5 | +{ |
| 6 | + <CascadingValue Value="this.Input" Name="DialogInput"> |
| 7 | + <CascadingValue Value="this" Name="ParentDialog"> |
| 8 | + @if (!IsCustom) |
| 9 | + { |
| 10 | + <div class="blazor-dialog-container @(Centered ? "blazor-dialog-centered" : "")"> |
| 11 | + <div class="blazor-dialog-content-wrapper @ContentWrapperCssClass"> |
| 12 | + <div class="blazor-dialog-content"> |
| 13 | + @ChildContent |
| 14 | + </div> |
| 15 | + </div> |
| 16 | + </div> |
| 17 | + } |
| 18 | + else |
| 19 | + { |
| 20 | + @ChildContent |
| 21 | + } |
| 22 | + </CascadingValue> |
| 23 | + </CascadingValue> |
| 24 | +} |
| 25 | + |
| 26 | +@code{ |
| 27 | + [Parameter] public RenderFragment ChildContent { get; set; } |
| 28 | + [Parameter] public string Id { get; set; } |
| 29 | + [Parameter] public bool? IsShowing { get; set; } |
| 30 | + [Parameter] public EventCallback<bool?> IsShowingChanged { get; set; } |
| 31 | + private bool isShowing; |
| 32 | + [Parameter] public object Input { get; set; } |
| 33 | + [Parameter] public EventCallback<DialogBeforeShowEventArgs> OnBeforeShow { get; set; } |
| 34 | + [Parameter] public EventCallback<DialogAfterShowEventArgs> OnAfterShow { get; set; } |
| 35 | + [Parameter] public EventCallback<DialogBeforeHideEventArgs> OnBeforeHide { get; set; } |
| 36 | + [Parameter] public EventCallback<DialogAfterHideEventArgs> OnAfterHide { get; set; } |
| 37 | + [Parameter] public DialogSize Size { get; set; } = DialogSize.Normal; |
| 38 | + [Parameter] public bool Centered { get; set; } |
| 39 | + [Parameter] public bool IsCustom { get; set; } |
| 40 | + |
| 41 | + protected TaskCompletionSource<object> taskCompletionSource; |
| 42 | + internal Action<object> OnDialogHide; |
| 43 | + protected void NotifyDialogHidden(object result) => OnDialogHide?.Invoke(result); |
| 44 | + |
| 45 | + protected string ContentWrapperCssClass |
| 46 | + { |
| 47 | + get |
| 48 | + { |
| 49 | + switch (Size) |
| 50 | + { |
| 51 | + case DialogSize.Normal: |
| 52 | + return ""; |
| 53 | + case DialogSize.Small: |
| 54 | + return "blazor-dialog-content-wrapper-small"; |
| 55 | + case DialogSize.Large: |
| 56 | + return "blazor-dialog-content-wrapper-large"; |
| 57 | + case DialogSize.ExtraLarge: |
| 58 | + return "blazor-dialog-content-wrapper-xlarge"; |
| 59 | + default: |
| 60 | + throw new Exception("Unknown dialog size"); |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + |
| 66 | + protected override void OnInitialized() |
| 67 | + { |
| 68 | + if (Id != null) |
| 69 | + { |
| 70 | + dialogService.Register(this); |
| 71 | + } |
| 72 | + OnDialogHide = (object result) => |
| 73 | + { |
| 74 | + if (taskCompletionSource != null) |
| 75 | + { |
| 76 | + taskCompletionSource.SetResult(result); |
| 77 | + taskCompletionSource = null; |
| 78 | + } |
| 79 | + }; |
| 80 | + } |
| 81 | + |
| 82 | + protected override async Task OnParametersSetAsync() |
| 83 | + { |
| 84 | + if(IsShowing.HasValue && IsShowing.Value && !isShowing) |
| 85 | + { |
| 86 | + if (OnBeforeShow.HasDelegate) |
| 87 | + { |
| 88 | + var args = new DialogBeforeShowEventArgs(this); |
| 89 | + await OnBeforeShow.InvokeAsync(args); |
| 90 | + if (args.PreventShow) return; |
| 91 | + } |
| 92 | + this.isShowing = true; |
| 93 | + if (OnAfterShow.HasDelegate) |
| 94 | + { |
| 95 | + var args = new DialogAfterShowEventArgs(this); |
| 96 | + await OnAfterShow.InvokeAsync(args); |
| 97 | + } |
| 98 | + } |
| 99 | + else if(IsShowing.HasValue && !IsShowing.Value && isShowing) |
| 100 | + { |
| 101 | + if (OnBeforeHide.HasDelegate) |
| 102 | + { |
| 103 | + var args = new DialogBeforeHideEventArgs(this); |
| 104 | + await OnBeforeHide.InvokeAsync(args); |
| 105 | + if (args.PreventHide) return; |
| 106 | + } |
| 107 | + this.isShowing = false; |
| 108 | + if (OnAfterHide.HasDelegate) |
| 109 | + { |
| 110 | + var args = new DialogAfterHideEventArgs(this); |
| 111 | + await OnAfterHide.InvokeAsync(args); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + public void Dispose() |
| 117 | + { |
| 118 | + dialogService.Unregister(this); |
| 119 | + if (taskCompletionSource != null) |
| 120 | + { |
| 121 | + taskCompletionSource.SetCanceled(); |
| 122 | + taskCompletionSource = null; |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + public async Task Show() |
| 127 | + { |
| 128 | + await Show<object>(); |
| 129 | + } |
| 130 | + |
| 131 | + public async Task<TResult> Show<TResult>() |
| 132 | + { |
| 133 | + this.Input = null; |
| 134 | + return (TResult)await ShowInternal(); |
| 135 | + } |
| 136 | + |
| 137 | + public async Task Show(object input) |
| 138 | + { |
| 139 | + await this.Show<object>(input); |
| 140 | + } |
| 141 | + |
| 142 | + public async Task<TResult> Show<TResult>(object input) |
| 143 | + { |
| 144 | + this.Input = input; |
| 145 | + return (TResult)await ShowInternal(); |
| 146 | + } |
| 147 | + |
| 148 | + private async Task<object> ShowInternal() |
| 149 | + { |
| 150 | + if (OnBeforeShow.HasDelegate) |
| 151 | + { |
| 152 | + var args = new DialogBeforeShowEventArgs(this); |
| 153 | + await OnBeforeShow.InvokeAsync(args); |
| 154 | + if (args.PreventShow) return default; |
| 155 | + } |
| 156 | + this.IsShowing = this.isShowing = true; |
| 157 | + await IsShowingChanged.InvokeAsync(isShowing); |
| 158 | + await InvokeAsync(() => StateHasChanged()); |
| 159 | + if (OnAfterShow.HasDelegate) |
| 160 | + { |
| 161 | + var args = new DialogAfterShowEventArgs(this); |
| 162 | + await OnAfterShow.InvokeAsync(args); |
| 163 | + } |
| 164 | + if (taskCompletionSource != null) |
| 165 | + { |
| 166 | + taskCompletionSource.SetCanceled(); |
| 167 | + taskCompletionSource = null; |
| 168 | + } |
| 169 | + |
| 170 | + taskCompletionSource = new TaskCompletionSource<object>(); |
| 171 | + return await taskCompletionSource.Task; |
| 172 | + } |
| 173 | + |
| 174 | + |
| 175 | + public async Task Hide<TResult>(TResult result) |
| 176 | + { |
| 177 | + if (OnBeforeHide.HasDelegate) |
| 178 | + { |
| 179 | + var args = new DialogBeforeHideEventArgs(this); |
| 180 | + await OnBeforeHide.InvokeAsync(args); |
| 181 | + if (args.PreventHide) return; |
| 182 | + } |
| 183 | + this.IsShowing = this.isShowing = false; |
| 184 | + await IsShowingChanged.InvokeAsync(isShowing); |
| 185 | + NotifyDialogHidden(result); |
| 186 | + await InvokeAsync(() => StateHasChanged()); |
| 187 | + if (OnAfterHide.HasDelegate) |
| 188 | + { |
| 189 | + var args = new DialogAfterHideEventArgs(this); |
| 190 | + await OnAfterHide.InvokeAsync(args); |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + public async Task Hide() |
| 195 | + { |
| 196 | + await this.Hide<object>(null); |
| 197 | + } |
| 198 | +} |
0 commit comments