|
| 1 | +using System.Diagnostics.CodeAnalysis; |
| 2 | +using Htmxor.Http; |
| 3 | +using Microsoft.AspNetCore.Components; |
| 4 | +using Microsoft.AspNetCore.Components.Rendering; |
| 5 | + |
| 6 | +namespace Htmxor.Components; |
| 7 | + |
| 8 | +public class HtmxFragmentElement : HtmxFragment |
| 9 | +{ |
| 10 | + [Parameter(CaptureUnmatchedValues = true)] |
| 11 | + public IDictionary<string, object>? AdditionalAttributes { get; set; } |
| 12 | + |
| 13 | + /// <summary> |
| 14 | + /// The ID of the element that <see cref="HtmxAsyncLoad"/> should render it's content inside. |
| 15 | + /// This is also the ID that htmx will use to target the element to replace it's content. |
| 16 | + /// </summary> |
| 17 | + [Parameter, EditorRequired] |
| 18 | + public required string Id { get; set; } |
| 19 | + |
| 20 | + /// <summary> |
| 21 | + /// The element type that <see cref="HtmxAsyncLoad"/> should render |
| 22 | + /// <see cref="ChildContent"/> and <see cref="Loading"/> into. |
| 23 | + /// </summary> |
| 24 | + /// <remarks>Default is a <c>div</c> element.</remarks> |
| 25 | + [Parameter] |
| 26 | + public string Element { get; set; } = "div"; |
| 27 | + |
| 28 | + public override bool ShouldOutput([NotNull] HtmxContext context, int directConditionalChildren, int conditionalChildren) |
| 29 | + => (OnStandardRequest && context.Request.RoutingMode is RoutingMode.Standard) |
| 30 | + || (Match?.Invoke(context.Request) ?? context.Request.Target == Id); |
| 31 | + |
| 32 | + protected override void OnParametersSet() |
| 33 | + { |
| 34 | + Element = string.IsNullOrWhiteSpace(Element) ? "div" : Element.Trim(); |
| 35 | + Id = string.IsNullOrWhiteSpace(Id) ? Id : Id.Trim(); |
| 36 | + |
| 37 | + if (AdditionalAttributes is null) |
| 38 | + { |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + RemoveControlledAttributeAndThrow(AdditionalAttributes, Constants.Attributes.HxTarget); |
| 43 | + RemoveControlledAttributeAndThrow(AdditionalAttributes, Constants.Attributes.HxSwap); |
| 44 | + } |
| 45 | + |
| 46 | + protected override void BuildRenderTree([NotNull] RenderTreeBuilder builder) |
| 47 | + { |
| 48 | + if (!ShouldOutput(Context, 0, 0)) |
| 49 | + { |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + var request = Context.Request; |
| 54 | + builder.OpenElement(1, Element); |
| 55 | + builder.AddAttribute(2, Constants.Attributes.Id, Id); |
| 56 | + builder.AddAttribute(3, Constants.Attributes.HxSwap, Constants.SwapStyles.OuterHTML); |
| 57 | + |
| 58 | + if (AdditionalAttributes is not null) |
| 59 | + { |
| 60 | + builder.AddMultipleAttributes(4, AdditionalAttributes); |
| 61 | + } |
| 62 | + |
| 63 | + builder.AddContent(5, ChildContent); |
| 64 | + builder.CloseElement(); |
| 65 | + } |
| 66 | + |
| 67 | + private static void RemoveControlledAttributeAndThrow(IDictionary<string, object> attributes, string attributeName) |
| 68 | + { |
| 69 | + if (attributes.Remove(attributeName)) |
| 70 | + { |
| 71 | + throw new ArgumentException($"The '{attributeName}' attribute is controlled by the {nameof(HtmxFragmentElement)} components and should not be set explicitly."); |
| 72 | + } |
| 73 | + } |
| 74 | +} |
0 commit comments