Skip to content

Commit 3102f65

Browse files
committedMay 1, 2024
refactor: rename PartialBase to FragmentBase
1 parent 74747ed commit 3102f65

7 files changed

+57
-52
lines changed
 

‎src/Htmxor/Builder/HtmxorComponentEndpointMatcherPolicy.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public Task ApplyAsync(HttpContext httpContext, CandidateSet candidates)
3333
var endpoint = candidates[i].Endpoint;
3434
var htmxorEndpointMetadata = endpoint.Metadata.GetMetadata<HtmxorEndpointMetadata>();
3535

36-
if (htmxorEndpointMetadata is null && htmxContext.Request.IsHtmxRequest && !htmxContext.Request.IsBoosted)
36+
if (htmxorEndpointMetadata is null && !htmxContext.Request.IsFullPageRequest)
3737
{
3838
candidates.SetValidity(i, false);
3939
continue;

‎src/Htmxor/Components/FragmentBase.cs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using Microsoft.AspNetCore.Components;
2+
3+
namespace Htmxor.Components;
4+
5+
public abstract class FragmentBase : ComponentBase
6+
{
7+
internal bool WillRender() => ShouldRender();
8+
}
9+

‎src/Htmxor/Components/HtmxPartial.cs

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,30 @@
11
using Microsoft.AspNetCore.Components;
2+
using Microsoft.AspNetCore.Components.Rendering;
23

34
namespace Htmxor.Components;
45

5-
public sealed class HtmxPartial : PartialBase
6+
public sealed class HtmxPartial : FragmentBase
67
{
8+
[Parameter, EditorRequired]
9+
public required RenderFragment ChildContent { get; set; }
10+
711
[Parameter] public bool Condition { get; set; } = true;
812

913
public override Task SetParametersAsync(ParameterView parameters)
1014
{
15+
if (!parameters.TryGetValue<RenderFragment>(nameof(ChildContent), out var childContent))
16+
{
17+
throw new ArgumentException($"{nameof(HtmxPartial)} requires a value for the parameter {nameof(ChildContent)}.");
18+
}
19+
20+
ChildContent = childContent;
1121
Condition = parameters.GetValueOrDefault(nameof(Condition), true);
1222
return base.SetParametersAsync(parameters);
1323
}
1424

15-
protected internal override bool ShouldRender() => Condition;
25+
protected override void BuildRenderTree(RenderTreeBuilder builder)
26+
=> builder.AddContent(0, ChildContent);
27+
28+
protected override bool ShouldRender() => Condition;
1629
}
1730

‎src/Htmxor/Components/PartialBase.cs

-31
This file was deleted.

‎src/Htmxor/Http/HtmxRequest.cs

+24-5
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,34 @@
22

33
namespace Htmxor.Http;
44

5-
public sealed class HtmxRequest(HttpContext context)
5+
public sealed class HtmxRequest
66
{
7+
private readonly HttpContext context;
8+
9+
/// <summary>
10+
/// Gets whether or not the current request should be treated as a full page request.
11+
/// </summary>
12+
internal bool IsFullPageRequest => !IsHtmxRequest || IsBoosted;
13+
714
/// <summary>
815
/// Gets the HTTP method of the current request.
916
/// </summary>
10-
public string Method => context.Request.Method;
17+
public string Method { get; }
1118

1219
/// <summary>
1320
/// Gets whether or not the current request is an Htmx triggered request.
1421
/// </summary>
15-
public bool IsHtmxRequest => context.Request.Headers.ContainsKey(HtmxRequestHeaderNames.HtmxRequest);
22+
public bool IsHtmxRequest { get; }
1623

1724
/// <summary>
1825
/// Gets whether or not the current request is an request initiated via an element using hx-boost.
1926
/// </summary>
20-
public bool IsBoosted => IsHtmxRequest && context.Request.Headers.ContainsKey(HtmxRequestHeaderNames.Boosted);
27+
public bool IsBoosted { get; }
2128

2229
/// <summary>
2330
/// Gets whether or not the current request is an Htmx history restore request.
2431
/// </summary>
25-
public bool IsHistoryRestoreRequest => IsHtmxRequest && context.Request.Headers.ContainsKey(HtmxRequestHeaderNames.HistoryRestoreRequest);
32+
public bool IsHistoryRestoreRequest { get; }
2633

2734
/// <summary>
2835
/// Gets the current URL of the browser.
@@ -83,4 +90,16 @@ public string? Trigger
8390
&& !string.IsNullOrWhiteSpace(values[0])
8491
? values[0]
8592
: null;
93+
94+
/// <summary>
95+
/// Creates a new instance of <see cref="HtmxRequest"/>.
96+
/// </summary>
97+
public HtmxRequest(HttpContext context)
98+
{
99+
this.context = context;
100+
var ishtmx = IsHtmxRequest = context.Request.Headers.ContainsKey(HtmxRequestHeaderNames.HtmxRequest);
101+
IsBoosted = ishtmx && context.Request.Headers.ContainsKey(HtmxRequestHeaderNames.Boosted);
102+
IsHistoryRestoreRequest = ishtmx && context.Request.Headers.ContainsKey(HtmxRequestHeaderNames.HistoryRestoreRequest);
103+
Method = context.Request.Method;
104+
}
86105
}

‎src/Htmxor/Rendering/HtmxorRenderer.HtmlWriting.cs

+1-6
Original file line numberDiff line numberDiff line change
@@ -514,12 +514,7 @@ private int RenderChildren(int componentId, TextWriter output, ArrayRange<Render
514514
private int RenderChildComponent(TextWriter output, ArrayRange<RenderTreeFrame> frames, int position)
515515
{
516516
ref var frame = ref frames.Array[position];
517-
518-
if (frame.Component is not PartialBase htmxPartial || htmxPartial.ShouldRender())
519-
{
520-
RenderChildComponent(output, ref frame);
521-
}
522-
517+
RenderChildComponent(output, ref frame);
523518
return position + frame.ComponentSubtreeLength;
524519
}
525520
}

‎src/Htmxor/Rendering/HtmxorRenderer.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -145,17 +145,17 @@ internal static void InitializeStandardComponentServices(
145145
internal void WriteComponentHtml(int componentId, TextWriter output)
146146
{
147147
var htmxContext = httpContext.GetHtmxContext();
148-
if (htmxContext.Request.IsHtmxRequest && !htmxContext.Request.IsBoosted)
148+
if (htmxContext.Request.IsFullPageRequest)
149+
{
150+
WriteComponent(componentId, output);
151+
}
152+
else
149153
{
150154
var matchingPartialComponentId = FindPartialComponentMatchingRequest(componentId);
151155
WriteComponent(
152156
matchingPartialComponentId.HasValue ? matchingPartialComponentId.Value : componentId,
153157
output);
154158
}
155-
else
156-
{
157-
WriteComponent(componentId, output);
158-
}
159159
}
160160

161161
private int? FindPartialComponentMatchingRequest(int componentId)
@@ -168,9 +168,9 @@ internal void WriteComponentHtml(int componentId, TextWriter output)
168168

169169
if (frame.FrameType is RenderTreeFrameType.Component)
170170
{
171-
if (frame.Component is PartialBase partial)
171+
if (frame.Component is FragmentBase partial)
172172
{
173-
if (partial.ShouldRender())
173+
if (partial.WillRender())
174174
{
175175
return frame.ComponentId;
176176
}

0 commit comments

Comments
 (0)