Skip to content

Commit fd58107

Browse files
Merge pull request #1 from stavroskasidis/develop
Release 0.2
2 parents 2a52176 + 922bdf1 commit fd58107

26 files changed

+462
-217
lines changed

BlazorDialog/BlazorDialog.csproj

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,42 @@
1-
<Project Sdk="Microsoft.NET.Sdk.Web">
1+
<Project Sdk="Microsoft.NET.Sdk.Razor">
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
5-
<OutputType>Library</OutputType>
6-
<IsPackable>true</IsPackable>
75
<LangVersion>7.3</LangVersion>
8-
<AddRazorSupportForMvc>true</AddRazorSupportForMvc>
6+
<IsPackable>true</IsPackable>
97
<RazorLangVersion>3.0</RazorLangVersion>
108

119

1210
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
13-
<PackageId>Blazor.Dialog</PackageId>
11+
<PackageId>BlazorDialog</PackageId>
1412
<Authors>Stavros Kasidis (AkiraGTX)</Authors>
1513
<PackageLicenseUrl>https://github.com/stavroskasidis/BlazorDialog/blob/master/LICENSE</PackageLicenseUrl>
1614
<PackageProjectUrl>https://github.com/stavroskasidis/BlazorDialog</PackageProjectUrl>
17-
<Description>A "show and wait for result" dialog component for Blazor</Description>
15+
<Description>Dialog component as a service for Blazor</Description>
1816
<PackageReleaseNotes>https://github.com/stavroskasidis/BlazorDialog/blob/master/README.md</PackageReleaseNotes>
1917
<Copyright />
2018
<PackageTags>blazor blazor-component blazor-dialog dialog modal blazor-modal blazordialog blazormodaldialog blazormodal razor razor-components razorcomponents</PackageTags>
2119
<VersionSuffix>$(VersionSuffix)</VersionSuffix>
22-
<Version>0.1.0</Version>
20+
<Version>0.2.0</Version>
2321
<Version Condition=" '$(VersionSuffix)' != '' ">$(Version)-$(VersionSuffix)</Version>
24-
<Product>Blazor.Dialog</Product>
22+
<Product>BlazorDialog</Product>
2523
</PropertyGroup>
2624

2725
<ItemGroup>
2826
<Compile Remove="node_modules\**" />
2927
<Content Remove="node_modules\**" />
3028
<EmbeddedResource Remove="node_modules\**" />
3129
<None Remove="node_modules\**" />
32-
</ItemGroup>
30+
</ItemGroup>
31+
32+
<ItemGroup>
33+
<Content Update="**\*.json">
34+
<Pack>false</Pack>
35+
</Content>
36+
</ItemGroup>
3337

3438
<ItemGroup>
35-
<PackageReference Include="Microsoft.AspNetCore.Components.Browser" Version="3.0.0-preview7.19365.7" />
39+
<PackageReference Include="Microsoft.AspNetCore.Components" Version="3.0.0" />
40+
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="3.0.0" />
3641
</ItemGroup>
37-
3842
</Project>

BlazorDialog/BlazorDialog.razor

Lines changed: 0 additions & 82 deletions
This file was deleted.

BlazorDialog/BlazorDialogService.cs

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,33 @@ namespace BlazorDialog
77
{
88
public class BlazorDialogService : IBlazorDialogService
99
{
10-
private readonly DialogStates _dialogStates;
11-
private Dictionary<string, TaskCompletionSource<object>> taskCompletionSources = new Dictionary<string, TaskCompletionSource<object>>();
10+
private Dictionary<string, Dialog> registeredDialogs = new Dictionary<string, Dialog>();
1211

13-
public BlazorDialogService(DialogStates states)
12+
public void Register(Dialog blazorDialog)
1413
{
15-
this._dialogStates = states;
16-
this._dialogStates.OnDialogHide += (string dialogId, object result) =>
14+
if(blazorDialog?.Id == null)
1715
{
18-
taskCompletionSources[dialogId].SetResult(result);
19-
taskCompletionSources.Remove(dialogId);
20-
};
16+
throw new ArgumentException("BlazorDialog Id is null", nameof(blazorDialog));
17+
}
18+
registeredDialogs[blazorDialog.Id] = blazorDialog;
19+
}
20+
21+
public void Unregister(Dialog blazorDialog)
22+
{
23+
if (blazorDialog.Id != null && registeredDialogs.ContainsKey(blazorDialog.Id))
24+
{
25+
registeredDialogs.Remove(blazorDialog.Id);
26+
}
2127
}
2228

23-
public void HideDialog(string dialogId, object result)
29+
public async Task HideDialog(string dialogId)
2430
{
25-
_dialogStates.HideDialog(dialogId, result);
31+
await registeredDialogs[dialogId].Hide();
32+
}
33+
34+
public async Task HideDialog(string dialogId, object result)
35+
{
36+
await registeredDialogs[dialogId].Hide(result);
2637
}
2738

2839
public async Task ShowDialog(string dialogId)
@@ -42,13 +53,7 @@ public async Task ShowDialog(string dialogId, object input)
4253

4354
public async Task<TResult> ShowDialog<TResult>(string dialogId, object input)
4455
{
45-
if (taskCompletionSources.ContainsKey(dialogId))
46-
{
47-
taskCompletionSources[dialogId].SetCanceled();
48-
}
49-
taskCompletionSources[dialogId] = new TaskCompletionSource<object>();
50-
_dialogStates.ShowDialog(dialogId, input);
51-
return (TResult)await taskCompletionSources[dialogId].Task;
56+
return await registeredDialogs[dialogId].Show<TResult>(input);
5257
}
5358
}
5459
}
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
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

Comments
 (0)