-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from stavroskasidis/feature/prevent-nav-8.0
Major updates and features for BlazorDialog
- Loading branch information
Showing
14 changed files
with
123 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"sdk": { | ||
"version": "6.0.412" | ||
"version": "8.0.302" | ||
} | ||
} |