Skip to content

Commit d9e0057

Browse files
committed
remove razor files and optimize cascading value creation process
1 parent 3642711 commit d9e0057

File tree

6 files changed

+148
-121
lines changed

6 files changed

+148
-121
lines changed
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk.Web">
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
@@ -7,10 +7,13 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Hez2010.BlazorRouter" Version="1.0.0" />
1110
<PackageReference Include="Microsoft.AspNetCore.Blazor" Version="3.0.0-preview6.19307.2" />
1211
<PackageReference Include="Microsoft.AspNetCore.Blazor.Build" Version="3.0.0-preview6.19307.2" PrivateAssets="all" />
1312
<PackageReference Include="Microsoft.AspNetCore.Blazor.DevServer" Version="3.0.0-preview6.19307.2" PrivateAssets="all" />
1413
</ItemGroup>
1514

15+
<ItemGroup>
16+
<ProjectReference Include="..\BlazorRouter\BlazorRouter.csproj" />
17+
</ItemGroup>
18+
1619
</Project>

BlazorRouter/BlazorRouter.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<PackageId>Hez2010.BlazorRouter</PackageId>
1717
<PackageLicenseExpression>MIT</PackageLicenseExpression>
1818
<Product>BlazorRouter</Product>
19-
<Version>1.0.0</Version>
19+
<Version>1.0.1</Version>
2020
<PackageRequireLicenseAcceptance>False</PackageRequireLicenseAcceptance>
2121
<Company>hez2010</Company>
2222
</PropertyGroup>

BlazorRouter/Route.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Microsoft.AspNetCore.Components;
4+
5+
namespace BlazorRouter
6+
{
7+
public class Route : ComponentBase
8+
{
9+
[CascadingParameter(Name = "SwitchInstance")] protected Switch SwitchInstance { get; set; }
10+
[Parameter] public string Template { get; set; } = "";
11+
[Parameter] public RenderFragment ChildContent { get; set; }
12+
13+
private bool hasRegisterd = false;
14+
15+
protected override Task OnParametersSetAsync()
16+
{
17+
if (!hasRegisterd)
18+
{
19+
hasRegisterd = true;
20+
base.OnParametersSetAsync();
21+
if (SwitchInstance == null)
22+
{
23+
throw new InvalidOperationException("A Route markup must be included in a Switch markup.");
24+
}
25+
return SwitchInstance.RegisterRoute(ChildContent, Template);
26+
}
27+
return Task.CompletedTask;
28+
}
29+
}
30+
}

BlazorRouter/Route.razor

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

BlazorRouter/Switch.cs

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Threading.Tasks;
4+
using Microsoft.AspNetCore.Components;
5+
using Microsoft.AspNetCore.Components.RenderTree;
6+
using Microsoft.AspNetCore.Components.Routing;
7+
8+
namespace BlazorRouter
9+
{
10+
public class Switch : ComponentBase, IDisposable
11+
{
12+
private int CreateCascadingValue<T>(RenderTreeBuilder builder, int seq, T value, string name, RenderFragment child)
13+
{
14+
builder.OpenComponent<CascadingValue<T>>(seq++);
15+
builder.AddAttribute(seq++, "Value", value);
16+
builder.AddAttribute(seq++, "Name", name);
17+
builder.AddAttribute(seq++, "ChildContent", child);
18+
builder.CloseComponent();
19+
return seq;
20+
}
21+
protected override void BuildRenderTree(RenderTreeBuilder builder)
22+
{
23+
var seq = 0;
24+
25+
seq = CreateCascadingValue(builder, seq, this, "SwitchInstance", ChildContent);
26+
CreateCascadingValue(builder, seq, parameters, "RouteParameters", currentFragment);
27+
}
28+
29+
[Inject] private IUriHelper UriHelper { get; set; }
30+
[Inject] private INavigationInterception NavigationInterception { get; set; }
31+
[Inject] private IComponentContext ComponentContext { get; set; }
32+
[Parameter] public RenderFragment ChildContent { get; set; }
33+
[Parameter] public EventHandler<RouteMatchedEventArgs> OnMatch { get; set; }
34+
35+
private readonly RouteTable routes = new RouteTable();
36+
private bool navigationInterceptionEnabled;
37+
private string location = "";
38+
private string baseUri = "";
39+
private RenderFragment currentFragment;
40+
private IDictionary<string, object> parameters;
41+
42+
static readonly char[] queryOrHashStartChar = new[] { '?', '#' };
43+
private async void LocationChanged(object sender, LocationChangedEventArgs e)
44+
{
45+
this.location = e.Location;
46+
47+
await SwitchContent(e.IsNavigationIntercepted);
48+
}
49+
50+
private string StringUntilAny(string str, char[] chars)
51+
{
52+
var firstIndex = str.IndexOfAny(chars);
53+
return firstIndex < 0 ? str : str.Substring(0, firstIndex);
54+
}
55+
56+
private Task SwitchContent(bool isNavigationIntercepted)
57+
{
58+
var path = UriHelper.ToBaseRelativePath(this.baseUri, this.location);
59+
path = "/" + StringUntilAny(path, queryOrHashStartChar);
60+
61+
var context = new RouteContext(path);
62+
routes.Route(context);
63+
64+
if (context.Fragment != null)
65+
{
66+
currentFragment = context.Fragment;
67+
parameters = context.Parameters;
68+
OnMatch?.Invoke(this, new RouteMatchedEventArgs(this.location, context.TemplateText, context.Parameters, context.Fragment));
69+
70+
this.StateHasChanged();
71+
}
72+
else
73+
{
74+
if (isNavigationIntercepted)
75+
{
76+
UriHelper.NavigateTo(this.location, forceLoad: true);
77+
}
78+
}
79+
80+
return Task.CompletedTask;
81+
}
82+
83+
protected override Task OnInitAsync()
84+
{
85+
this.baseUri = UriHelper.GetBaseUri();
86+
this.location = UriHelper.GetAbsoluteUri();
87+
UriHelper.OnLocationChanged += LocationChanged;
88+
return Task.CompletedTask;
89+
}
90+
91+
public Task RegisterRoute(RenderFragment fragment, string template)
92+
{
93+
routes.Add(template, fragment);
94+
return Task.CompletedTask;
95+
}
96+
97+
protected override async Task OnAfterRenderAsync()
98+
{
99+
if (!this.navigationInterceptionEnabled && ComponentContext.IsConnected)
100+
{
101+
this.navigationInterceptionEnabled = true;
102+
await SwitchContent(false);
103+
await NavigationInterception.EnableNavigationInterceptionAsync();
104+
}
105+
}
106+
107+
public void Dispose()
108+
{
109+
UriHelper.OnLocationChanged -= LocationChanged;
110+
}
111+
}
112+
}

BlazorRouter/Switch.razor

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

0 commit comments

Comments
 (0)