Simplify UriHelper by making EnableNavigationInteception automatic
This commit is contained in:
parent
e1aab02228
commit
68f6ede3a7
|
|
@ -44,11 +44,9 @@ namespace Microsoft.AspNetCore.Blazor.Browser.Routing
|
||||||
public void Init(RenderHandle renderHandle)
|
public void Init(RenderHandle renderHandle)
|
||||||
{
|
{
|
||||||
_renderHandle = renderHandle;
|
_renderHandle = renderHandle;
|
||||||
|
|
||||||
UriHelper.EnableNavigationInteception();
|
|
||||||
UriHelper.OnLocationChanged += OnLocationChanged;
|
|
||||||
_baseUriPrefix = UriHelper.GetBaseUriPrefix();
|
_baseUriPrefix = UriHelper.GetBaseUriPrefix();
|
||||||
_locationAbsolute = UriHelper.GetAbsoluteUri();
|
_locationAbsolute = UriHelper.GetAbsoluteUri();
|
||||||
|
UriHelper.OnLocationChanged += OnLocationChanged;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
|
|
|
||||||
|
|
@ -7,8 +7,6 @@ using System;
|
||||||
namespace Microsoft.AspNetCore.Blazor.Browser.Routing
|
namespace Microsoft.AspNetCore.Blazor.Browser.Routing
|
||||||
{
|
{
|
||||||
// TODO: Make this not static, and wrap it in an interface that can be injected through DI.
|
// TODO: Make this not static, and wrap it in an interface that can be injected through DI.
|
||||||
// We can make EnableNavigationInteception private, and call it automatically when the any
|
|
||||||
// concrete instance is instantiated.
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Helpers for working with URIs and navigation state.
|
/// Helpers for working with URIs and navigation state.
|
||||||
|
|
@ -19,19 +17,26 @@ namespace Microsoft.AspNetCore.Blazor.Browser.Routing
|
||||||
static string _currentAbsoluteUri;
|
static string _currentAbsoluteUri;
|
||||||
static string _baseUriString;
|
static string _baseUriString;
|
||||||
static Uri _baseUri;
|
static Uri _baseUri;
|
||||||
|
static EventHandler<string> _onLocationChanged;
|
||||||
|
static bool _hasEnabledNavigationInterception;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// An event that fires when the navigation location has changed.
|
/// An event that fires when the navigation location has changed.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static event EventHandler<string> OnLocationChanged;
|
public static event EventHandler<string> OnLocationChanged
|
||||||
|
{
|
||||||
/// <summary>
|
add
|
||||||
/// Prevents default navigation on all links whose href is inside the base URI space,
|
{
|
||||||
/// causing clicks on those links to trigger <see cref="OnLocationChanged"/> instead.
|
EnsureNavigationInteceptionEnabled();
|
||||||
/// </summary>
|
_onLocationChanged += value;
|
||||||
public static void EnableNavigationInteception()
|
}
|
||||||
=> RegisteredFunction.InvokeUnmarshalled<object>(
|
remove
|
||||||
$"{_functionPrefix}.enableNavigationInteception");
|
{
|
||||||
|
// We could consider deactivating the JS-side enableNavigationInteception
|
||||||
|
// if there are no remaining listeners, but we don't need that currently.
|
||||||
|
_onLocationChanged -= value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the URI prefix that can be prepended before URI paths to produce an absolute URI.
|
/// Gets the URI prefix that can be prepended before URI paths to produce an absolute URI.
|
||||||
|
|
@ -115,7 +120,20 @@ namespace Microsoft.AspNetCore.Blazor.Browser.Routing
|
||||||
private static void NotifyLocationChanged(string newAbsoluteUri)
|
private static void NotifyLocationChanged(string newAbsoluteUri)
|
||||||
{
|
{
|
||||||
_currentAbsoluteUri = newAbsoluteUri;
|
_currentAbsoluteUri = newAbsoluteUri;
|
||||||
OnLocationChanged?.Invoke(null, newAbsoluteUri);
|
_onLocationChanged?.Invoke(null, newAbsoluteUri);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void EnsureNavigationInteceptionEnabled()
|
||||||
|
{
|
||||||
|
// Don't need thread safety because:
|
||||||
|
// (1) there's only one UI thread
|
||||||
|
// (2) doesn't matter if we call enableNavigationInteception more than once anyway
|
||||||
|
if (!_hasEnabledNavigationInterception)
|
||||||
|
{
|
||||||
|
_hasEnabledNavigationInterception = true;
|
||||||
|
RegisteredFunction.InvokeUnmarshalled<object>(
|
||||||
|
$"{_functionPrefix}.enableNavigationInteception");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue