Skip to content

Commit 0f07475

Browse files
Merge pull request #22 from stavroskasidis/release/3.0.0
Release 3.0.0
2 parents f8e6782 + 0177839 commit 0f07475

14 files changed

Lines changed: 123 additions & 26 deletions

File tree

BlazorDialog/BlazorDialog.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<Copyright />
1313
<PackageTags>blazor blazor-component blazor-dialog dialog modal blazor-modal blazordialog blazormodaldialog blazormodal razor razor-components razorcomponents</PackageTags>
1414
<VersionSuffix>$(VersionSuffix)</VersionSuffix>
15-
<Version>2.3.0</Version>
15+
<Version>3.0.0</Version>
1616
<Version Condition=" '$(VersionSuffix)' != '' ">$(Version)-$(VersionSuffix)</Version>
1717
<Product>BlazorDialog</Product>
1818
</PropertyGroup>

BlazorDialog/BlazorDialogStore.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ internal class BlazorDialogStore : IBlazorDialogStore
1111
{
1212
private Dictionary<string, Dialog> registeredDialogs = new Dictionary<string, Dialog>();
1313
private Dictionary<string, ComponentDialog> registeredComponentDialogs = new Dictionary<string, ComponentDialog>();
14+
1415
public event Func<Task> OnComponentAsDialogsChanged;
1516

1617
public Dialog GetById(string id)
@@ -47,7 +48,7 @@ public async Task RegisterComponentDialog(string id, ComponentDialog options)
4748
}
4849
}
4950

50-
public void Unregister(Dialog blazorDialog)
51+
public void Remove(Dialog blazorDialog)
5152
{
5253
if (blazorDialog.Id != null && registeredDialogs.ContainsKey(blazorDialog.Id))
5354
{

BlazorDialog/Components/ComponentAsDialogContainer.razor

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
Centered="ComponentDialog.Options.Centered" CssClass="@ComponentDialog.Options.CssClass" Size="ComponentDialog.Options.Size"
88
OnAfterRender="EventUtil.AsNonRenderingEventCallback((bool firstLoad) => OnAfterDialogRender(firstLoad))"
99
OnAfterHide="ComponentDialog.Options.OnAfterHide" OnAfterShow="ComponentDialog.Options.OnAfterShow"
10-
OnBeforeHide="ComponentDialog.Options.OnBeforeHide" OnBeforeShow="ComponentDialog.Options.OnBeforeShow">
10+
OnBeforeHide="ComponentDialog.Options.OnBeforeHide" OnBeforeShow="ComponentDialog.Options.OnBeforeShow"
11+
PreventNavigation="ComponentDialog.Options.PreventNavigation">
1112
<DynamicComponent Type="ComponentDialog.Options.ComponentType" Parameters="ComponentDialog.Options.Parameters"></DynamicComponent>
1213
</Dialog>
1314
}
@@ -16,7 +17,8 @@
1617
<Dialog Id="@Key" IsCustom="ComponentDialog.Options.IsCustom"
1718
OnAfterRender="EventUtil.AsNonRenderingEventCallback((bool firstLoad) => OnAfterDialogRender(firstLoad))"
1819
OnAfterHide="ComponentDialog.Options.OnAfterHide" OnAfterShow="ComponentDialog.Options.OnAfterShow"
19-
OnBeforeHide="ComponentDialog.Options.OnBeforeHide" OnBeforeShow="ComponentDialog.Options.OnBeforeShow">
20+
OnBeforeHide="ComponentDialog.Options.OnBeforeHide" OnBeforeShow="ComponentDialog.Options.OnBeforeShow"
21+
PreventNavigation="ComponentDialog.Options.PreventNavigation">
2022
<DynamicComponent Type="ComponentDialog.Options.ComponentType" Parameters="ComponentDialog.Options.Parameters"></DynamicComponent>
2123
</Dialog>
2224
}

BlazorDialog/Components/Dialog.razor

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
@inject IBlazorDialogStore dialogStore
33
@namespace BlazorDialog
44
@implements IDisposable
5+
@inject ILocationChangingHandler locationChangingHandler
56
@if (isShowing)
67
{
78
<CascadingValue Value="this" Name="ParentDialog" IsFixed="true">
@@ -99,6 +100,11 @@
99100
/// </summary>
100101
[Parameter] public EventCallback<bool> OnAfterRender { get; set; }
101102

103+
/// <summary>
104+
/// Allows you to prevent browser navigation when the dialog is shown. Defaults to true.
105+
/// </summary>
106+
[Parameter] public bool PreventNavigation { get; set; } = true;
107+
102108
protected TaskCompletionSource<object> taskCompletionSource;
103109
internal Action<object> OnDialogHide;
104110
protected void NotifyDialogHidden(object result) => OnDialogHide?.Invoke(result);
@@ -188,9 +194,12 @@
188194
var args = new DialogAfterShowEventArgs(this);
189195
await OnAfterShow.InvokeAsync(args);
190196
}
197+
198+
locationChangingHandler.RegisterLocationChangingHandler(this);
191199
}
192200
else if (isShowingChanged && IsShowing == false && isShowing)
193201
{
202+
locationChangingHandler.RemoveLocationChangingHandler(this);
194203
if (OnBeforeHide.HasDelegate)
195204
{
196205
var args = new DialogBeforeHideEventArgs(this);
@@ -207,7 +216,8 @@
207216

208217
public void Dispose()
209218
{
210-
dialogStore.Unregister(this);
219+
dialogStore.Remove(this);
220+
locationChangingHandler.RemoveLocationChangingHandler(this);
211221
if (taskCompletionSource != null)
212222
{
213223
taskCompletionSource.TrySetCanceled();
@@ -260,13 +270,16 @@
260270
taskCompletionSource = null;
261271
}
262272

273+
locationChangingHandler.RegisterLocationChangingHandler(this);
274+
263275
taskCompletionSource = new TaskCompletionSource<object>();
264276
return await taskCompletionSource.Task;
265277
}
266278

267279

268280
public async Task Hide<TResult>(TResult result)
269281
{
282+
locationChangingHandler.RemoveLocationChangingHandler(this);
270283
if (OnBeforeHide.HasDelegate)
271284
{
272285
var args = new DialogBeforeHideEventArgs(this);

BlazorDialog/IBlazorDialogStore.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ public ComponentAsDialogOptions(Type componentType)
5353
/// </summary>
5454
public DialogSize Size { get; set; } = DialogSize.Normal;
5555

56+
/// <summary>
57+
/// Allows you to prevent browser navigation when the dialog is shown. Defaults to true.
58+
/// </summary>
59+
public bool PreventNavigation { get; set; } = true;
60+
5661
/// <summary>
5762
/// An event that is triggered before the dialog appears.
5863
/// </summary>
@@ -143,7 +148,7 @@ public interface IBlazorDialogStore
143148
{
144149

145150
void Register(Dialog blazorDialog);
146-
void Unregister(Dialog blazorDialog);
151+
void Remove(Dialog blazorDialog);
147152
Dialog GetById(string id);
148153
int GetVisibleDialogsCount();
149154

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using Microsoft.AspNetCore.Components.Routing;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace BlazorDialog
9+
{
10+
public interface ILocationChangingHandler
11+
{
12+
void RegisterLocationChangingHandler(Dialog dialog);
13+
void RemoveLocationChangingHandler(Dialog dialog);
14+
}
15+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using Microsoft.AspNetCore.Components;
2+
using Microsoft.AspNetCore.Components.Routing;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace BlazorDialog
10+
{
11+
public class LocationChangingHandler : ILocationChangingHandler, IDisposable
12+
{
13+
private readonly NavigationManager _navigationManager;
14+
private IDisposable? _currentRegistration;
15+
private List<Dialog> _dialogsStack = new List<Dialog>();
16+
17+
public LocationChangingHandler(NavigationManager navigationManager)
18+
{
19+
_navigationManager = navigationManager;
20+
}
21+
22+
public void RegisterLocationChangingHandler(Dialog dialog)
23+
{
24+
if (!_dialogsStack.Contains(dialog))
25+
{
26+
_dialogsStack.Add(dialog);
27+
}
28+
29+
if(_currentRegistration == null)
30+
{
31+
_currentRegistration = _navigationManager.RegisterLocationChangingHandler(OnLocationChanging);
32+
}
33+
}
34+
35+
private ValueTask OnLocationChanging(LocationChangingContext context)
36+
{
37+
if (!context.IsNavigationIntercepted && _dialogsStack.Any(x => x.PreventNavigation))
38+
{
39+
context.PreventNavigation();
40+
}
41+
return ValueTask.CompletedTask;
42+
}
43+
44+
public void RemoveLocationChangingHandler(Dialog dialog)
45+
{
46+
if (_dialogsStack.Contains(dialog))
47+
{
48+
_dialogsStack.Remove(dialog);
49+
}
50+
51+
// if there are no dialogs left in the stack, we can remove the location changing handler
52+
if (_currentRegistration != null && _dialogsStack.Count == 0)
53+
{
54+
_currentRegistration.Dispose();
55+
_currentRegistration = null;
56+
}
57+
}
58+
59+
public void Dispose()
60+
{
61+
if(_currentRegistration != null)
62+
{
63+
_currentRegistration.Dispose();
64+
_currentRegistration = null;
65+
}
66+
}
67+
}
68+
}

BlazorDialog/Properties/launchSettings.json

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,6 @@
88
}
99
},
1010
"profiles": {
11-
"IIS Express": {
12-
"commandName": "IISExpress",
13-
"launchBrowser": true,
14-
"environmentVariables": {
15-
"ASPNETCORE_ENVIRONMENT": "Development"
16-
}
17-
},
1811
"BlazorDialog": {
1912
"commandName": "Project",
2013
"launchBrowser": true,

BlazorDialog/ServiceCollectionExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public static IServiceCollection AddBlazorDialog(this IServiceCollection service
1212
{
1313
services.AddScoped<IBlazorDialogStore, BlazorDialogStore>();
1414
services.AddScoped<IBlazorDialogService, BlazorDialogService>();
15+
services.AddScoped<ILocationChangingHandler, LocationChangingHandler>();
1516
return services;
1617
}
1718
}

Directory.Build.props

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<Project>
22
<!-- See https://aka.ms/dotnet/msbuild/customize for more details on customizing your build -->
33
<PropertyGroup>
4-
<TargetFramework>net6.0</TargetFramework>
5-
<MSPackagesVersion>6.0.26</MSPackagesVersion>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<MSPackagesVersion>8.0.*</MSPackagesVersion>
66
<Nullable>enable</Nullable>
77
<ImplicitUsings>enable</ImplicitUsings>
88
</PropertyGroup>

0 commit comments

Comments
 (0)