Skip to content

Commit

Permalink
Merge pull request #21 from stavroskasidis/feature/prevent-nav-8.0
Browse files Browse the repository at this point in the history
Major updates and features for BlazorDialog
  • Loading branch information
stavroskasidis authored Jun 20, 2024
2 parents f8e6782 + affd00d commit 0177839
Show file tree
Hide file tree
Showing 14 changed files with 123 additions and 26 deletions.
2 changes: 1 addition & 1 deletion BlazorDialog/BlazorDialog.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<Copyright />
<PackageTags>blazor blazor-component blazor-dialog dialog modal blazor-modal blazordialog blazormodaldialog blazormodal razor razor-components razorcomponents</PackageTags>
<VersionSuffix>$(VersionSuffix)</VersionSuffix>
<Version>2.3.0</Version>
<Version>3.0.0</Version>
<Version Condition=" '$(VersionSuffix)' != '' ">$(Version)-$(VersionSuffix)</Version>
<Product>BlazorDialog</Product>
</PropertyGroup>
Expand Down
3 changes: 2 additions & 1 deletion BlazorDialog/BlazorDialogStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ internal class BlazorDialogStore : IBlazorDialogStore
{
private Dictionary<string, Dialog> registeredDialogs = new Dictionary<string, Dialog>();
private Dictionary<string, ComponentDialog> registeredComponentDialogs = new Dictionary<string, ComponentDialog>();

public event Func<Task> OnComponentAsDialogsChanged;

public Dialog GetById(string id)
Expand Down Expand Up @@ -47,7 +48,7 @@ public async Task RegisterComponentDialog(string id, ComponentDialog options)
}
}

public void Unregister(Dialog blazorDialog)
public void Remove(Dialog blazorDialog)
{
if (blazorDialog.Id != null && registeredDialogs.ContainsKey(blazorDialog.Id))
{
Expand Down
6 changes: 4 additions & 2 deletions BlazorDialog/Components/ComponentAsDialogContainer.razor
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
Centered="ComponentDialog.Options.Centered" CssClass="@ComponentDialog.Options.CssClass" Size="ComponentDialog.Options.Size"
OnAfterRender="EventUtil.AsNonRenderingEventCallback((bool firstLoad) => OnAfterDialogRender(firstLoad))"
OnAfterHide="ComponentDialog.Options.OnAfterHide" OnAfterShow="ComponentDialog.Options.OnAfterShow"
OnBeforeHide="ComponentDialog.Options.OnBeforeHide" OnBeforeShow="ComponentDialog.Options.OnBeforeShow">
OnBeforeHide="ComponentDialog.Options.OnBeforeHide" OnBeforeShow="ComponentDialog.Options.OnBeforeShow"
PreventNavigation="ComponentDialog.Options.PreventNavigation">
<DynamicComponent Type="ComponentDialog.Options.ComponentType" Parameters="ComponentDialog.Options.Parameters"></DynamicComponent>
</Dialog>
}
Expand All @@ -16,7 +17,8 @@
<Dialog Id="@Key" IsCustom="ComponentDialog.Options.IsCustom"
OnAfterRender="EventUtil.AsNonRenderingEventCallback((bool firstLoad) => OnAfterDialogRender(firstLoad))"
OnAfterHide="ComponentDialog.Options.OnAfterHide" OnAfterShow="ComponentDialog.Options.OnAfterShow"
OnBeforeHide="ComponentDialog.Options.OnBeforeHide" OnBeforeShow="ComponentDialog.Options.OnBeforeShow">
OnBeforeHide="ComponentDialog.Options.OnBeforeHide" OnBeforeShow="ComponentDialog.Options.OnBeforeShow"
PreventNavigation="ComponentDialog.Options.PreventNavigation">
<DynamicComponent Type="ComponentDialog.Options.ComponentType" Parameters="ComponentDialog.Options.Parameters"></DynamicComponent>
</Dialog>
}
Expand Down
15 changes: 14 additions & 1 deletion BlazorDialog/Components/Dialog.razor
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
@inject IBlazorDialogStore dialogStore
@namespace BlazorDialog
@implements IDisposable
@inject ILocationChangingHandler locationChangingHandler
@if (isShowing)
{
<CascadingValue Value="this" Name="ParentDialog" IsFixed="true">
Expand Down Expand Up @@ -99,6 +100,11 @@
/// </summary>
[Parameter] public EventCallback<bool> OnAfterRender { get; set; }

/// <summary>
/// Allows you to prevent browser navigation when the dialog is shown. Defaults to true.
/// </summary>
[Parameter] public bool PreventNavigation { get; set; } = true;

protected TaskCompletionSource<object> taskCompletionSource;
internal Action<object> OnDialogHide;
protected void NotifyDialogHidden(object result) => OnDialogHide?.Invoke(result);
Expand Down Expand Up @@ -188,9 +194,12 @@
var args = new DialogAfterShowEventArgs(this);
await OnAfterShow.InvokeAsync(args);
}

locationChangingHandler.RegisterLocationChangingHandler(this);
}
else if (isShowingChanged && IsShowing == false && isShowing)
{
locationChangingHandler.RemoveLocationChangingHandler(this);
if (OnBeforeHide.HasDelegate)
{
var args = new DialogBeforeHideEventArgs(this);
Expand All @@ -207,7 +216,8 @@

public void Dispose()
{
dialogStore.Unregister(this);
dialogStore.Remove(this);
locationChangingHandler.RemoveLocationChangingHandler(this);
if (taskCompletionSource != null)
{
taskCompletionSource.TrySetCanceled();
Expand Down Expand Up @@ -260,13 +270,16 @@
taskCompletionSource = null;
}

locationChangingHandler.RegisterLocationChangingHandler(this);

taskCompletionSource = new TaskCompletionSource<object>();
return await taskCompletionSource.Task;
}


public async Task Hide<TResult>(TResult result)
{
locationChangingHandler.RemoveLocationChangingHandler(this);
if (OnBeforeHide.HasDelegate)
{
var args = new DialogBeforeHideEventArgs(this);
Expand Down
7 changes: 6 additions & 1 deletion BlazorDialog/IBlazorDialogStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ public ComponentAsDialogOptions(Type componentType)
/// </summary>
public DialogSize Size { get; set; } = DialogSize.Normal;

/// <summary>
/// Allows you to prevent browser navigation when the dialog is shown. Defaults to true.
/// </summary>
public bool PreventNavigation { get; set; } = true;

/// <summary>
/// An event that is triggered before the dialog appears.
/// </summary>
Expand Down Expand Up @@ -143,7 +148,7 @@ public interface IBlazorDialogStore
{

void Register(Dialog blazorDialog);
void Unregister(Dialog blazorDialog);
void Remove(Dialog blazorDialog);
Dialog GetById(string id);
int GetVisibleDialogsCount();

Expand Down
15 changes: 15 additions & 0 deletions BlazorDialog/ILocationChangingHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Microsoft.AspNetCore.Components.Routing;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BlazorDialog
{
public interface ILocationChangingHandler
{
void RegisterLocationChangingHandler(Dialog dialog);
void RemoveLocationChangingHandler(Dialog dialog);
}
}
68 changes: 68 additions & 0 deletions BlazorDialog/LocationChangingHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Routing;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BlazorDialog
{
public class LocationChangingHandler : ILocationChangingHandler, IDisposable
{
private readonly NavigationManager _navigationManager;
private IDisposable? _currentRegistration;
private List<Dialog> _dialogsStack = new List<Dialog>();

public LocationChangingHandler(NavigationManager navigationManager)
{
_navigationManager = navigationManager;
}

public void RegisterLocationChangingHandler(Dialog dialog)
{
if (!_dialogsStack.Contains(dialog))
{
_dialogsStack.Add(dialog);
}

if(_currentRegistration == null)
{
_currentRegistration = _navigationManager.RegisterLocationChangingHandler(OnLocationChanging);
}
}

private ValueTask OnLocationChanging(LocationChangingContext context)
{
if (!context.IsNavigationIntercepted && _dialogsStack.Any(x => x.PreventNavigation))
{
context.PreventNavigation();
}
return ValueTask.CompletedTask;
}

public void RemoveLocationChangingHandler(Dialog dialog)
{
if (_dialogsStack.Contains(dialog))
{
_dialogsStack.Remove(dialog);
}

// if there are no dialogs left in the stack, we can remove the location changing handler
if (_currentRegistration != null && _dialogsStack.Count == 0)
{
_currentRegistration.Dispose();
_currentRegistration = null;
}
}

public void Dispose()
{
if(_currentRegistration != null)
{
_currentRegistration.Dispose();
_currentRegistration = null;
}
}
}
}
7 changes: 0 additions & 7 deletions BlazorDialog/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,6 @@
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"BlazorDialog": {
"commandName": "Project",
"launchBrowser": true,
Expand Down
1 change: 1 addition & 0 deletions BlazorDialog/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public static IServiceCollection AddBlazorDialog(this IServiceCollection service
{
services.AddScoped<IBlazorDialogStore, BlazorDialogStore>();
services.AddScoped<IBlazorDialogService, BlazorDialogService>();
services.AddScoped<ILocationChangingHandler, LocationChangingHandler>();
return services;
}
}
Expand Down
4 changes: 2 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<Project>
<!-- See https://aka.ms/dotnet/msbuild/customize for more details on customizing your build -->
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<MSPackagesVersion>6.0.26</MSPackagesVersion>
<TargetFramework>net8.0</TargetFramework>
<MSPackagesVersion>8.0.*</MSPackagesVersion>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,13 @@ Make sure that there is a call to `app.UseStaticFiles();` in your server project
</details>
## Release Notes
<details open="open"><summary>2.3</summary>
<details open="open"><summary>3.0</summary>

>- Migrate to .NET 8.0
>- Add PreventNavigation option to prevent navigation when dialog is open.
</details>
<details><summary>2.3</summary>

>- Expose dialog options as cascading parameter when using ComponentAsDialog.
</details>
Expand Down
2 changes: 1 addition & 1 deletion TestApps/BlazorDialog.TestAppsCommon/IndexCommon.razor
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
</DialogInputProvider>
</Dialog>

<Dialog Id="simple-dialog" Size="size" Centered="isCentered" Animation="animation">
<Dialog Id="simple-dialog" Size="size" Centered="isCentered" Animation="animation" PreventNavigation="false">
<DialogInputProvider TInput="string">
<DialogBody>
<h4>@context.Input</h4>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,10 @@
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"BlazorDialog.BlazorTestApp.Server": {
"commandName": "Project",
"launchBrowser": true,
"dotnetRunMessages": true,
"inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
Expand Down
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"sdk": {
"version": "6.0.412"
"version": "8.0.302"
}
}

0 comments on commit 0177839

Please sign in to comment.