Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Remove dictionary creation from SwapStyleBuilder #55

Merged
merged 5 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/Htmxor/HtmxSwapSpecification.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Htmxor;

/// <summary>
/// Represents an htmx swap specification
/// </summary>
internal sealed record HtmxSwapSpecification
{
public SwapStyle SwapStyle { get; set; }
public string? SwapDelay { get; set; }
public string? SettleDelay { get; set; }
public string? Show { get; set; }
public string? Transition { get; set; }
public string? IgnoreTitle { get; set; }
public string? Scroll { get; set; }
public string? FocusScroll { get; set; }
}
6 changes: 2 additions & 4 deletions src/Htmxor/Http/HtmxResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,9 @@ public HtmxResponse Reswap(SwapStyleBuilder swapStyle)
{
ArgumentNullException.ThrowIfNull(swapStyle);

var (style, modifier) = swapStyle.Build();
headers[HtmxResponseHeaderNames.Reswap] = swapStyle.Build();

return style is SwapStyle.Default
? Reswap(modifier)
: Reswap(style, modifier);
return this;
}

/// <summary>
Expand Down
101 changes: 44 additions & 57 deletions src/Htmxor/SwapStyleBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Runtime.InteropServices.ComTypes;

namespace Htmxor;

Expand All @@ -10,16 +11,15 @@ namespace Htmxor;
[DebuggerDisplay("{ToString(),nq}")]
public sealed class SwapStyleBuilder
{
private readonly SwapStyle style;
private readonly OrderedDictionary modifiers = new(StringComparer.Ordinal);
private readonly HtmxSwapSpecification specification;

/// <summary>
/// Initializes a new instance of the SwapStyleBuilder with a specified swap style.
/// </summary>
/// <param name="style">The initial swap style to be applied.</param>
public SwapStyleBuilder(SwapStyle style = SwapStyle.Default)
{
this.style = style;
specification = new HtmxSwapSpecification() { SwapStyle = style };
}

/// <summary>
Expand All @@ -36,7 +36,9 @@ public SwapStyleBuilder(SwapStyle style = SwapStyle.Default)
/// <returns>This <see cref="SwapStyleBuilder"/> object instance.</returns>
public SwapStyleBuilder AfterSwapDelay(TimeSpan time)
{
AddModifier("swap", time.TotalMilliseconds < 1000 ? $"{time.TotalMilliseconds}ms" : $"{time.TotalSeconds}s");
// swap
specification.SwapDelay =
time.TotalMilliseconds < 1000 ? $"{time.TotalMilliseconds}ms" : $"{time.TotalSeconds}s";

return this;
}
Expand All @@ -55,7 +57,9 @@ public SwapStyleBuilder AfterSwapDelay(TimeSpan time)
/// <returns>This <see cref="SwapStyleBuilder"/> object instance.</returns>
public SwapStyleBuilder AfterSettleDelay(TimeSpan time)
{
AddModifier("settle", time.TotalMilliseconds < 1000 ? $"{time.TotalMilliseconds}ms" : $"{time.TotalSeconds}s");
// settle
specification.SettleDelay =
time.TotalMilliseconds < 1000 ? $"{time.TotalMilliseconds}ms" : $"{time.TotalSeconds}s";

return this;
}
Expand All @@ -73,13 +77,14 @@ public SwapStyleBuilder AfterSettleDelay(TimeSpan time)
/// <returns>This <see cref="SwapStyleBuilder"/> object instance.</returns>
public SwapStyleBuilder Scroll(ScrollDirection direction, string? cssSelector = null)
{
// scroll
switch (direction)
{
case ScrollDirection.top:
AddModifier("scroll", cssSelector is null ? "top" : $"{cssSelector}:top");
specification.Scroll = cssSelector is null ? "top" : $"{cssSelector}:top";
break;
case ScrollDirection.bottom:
AddModifier("scroll", cssSelector is null ? "bottom" : $"{cssSelector}:bottom");
specification.Scroll = cssSelector is null ? "bottom" : $"{cssSelector}:bottom";
break;
}

Expand Down Expand Up @@ -123,7 +128,8 @@ public SwapStyleBuilder Scroll(ScrollDirection direction, string? cssSelector =
/// <returns>This <see cref="SwapStyleBuilder"/> object instance.</returns>
public SwapStyleBuilder IgnoreTitle(bool ignore = true)
{
AddModifier("ignoreTitle", ignore);
// ignoreTitle
specification.IgnoreTitle = ignore ? "true" : "false";

return this;
}
Expand All @@ -149,7 +155,8 @@ public SwapStyleBuilder IgnoreTitle(bool ignore = true)
/// <returns>This <see cref="SwapStyleBuilder"/> object instance.</returns>
public SwapStyleBuilder Transition(bool show)
{
AddModifier("transition", show);
// transition
specification.Transition = show ? "true" : "false";

return this;
}
Expand Down Expand Up @@ -186,7 +193,8 @@ public SwapStyleBuilder Transition(bool show)
/// <returns>This <see cref="SwapStyleBuilder"/> object instance.</returns>
public SwapStyleBuilder ScrollFocus(bool scroll = true)
{
AddModifier("focus-scroll", scroll);
// focus-scroll
specification.FocusScroll = scroll ? "true" : "false";

return this;
}
Expand Down Expand Up @@ -218,13 +226,13 @@ public SwapStyleBuilder ShowOn(ScrollDirection direction, string? cssSelector =
switch (direction)
{
case ScrollDirection.top:
AddModifier("show", cssSelector is null ? "top" : $"{cssSelector}:top");
specification.Show = cssSelector is null ? "top" : $"{cssSelector}:top";
break;
case ScrollDirection.bottom:
AddModifier("show", cssSelector is null ? "bottom" : $"{cssSelector}:bottom");
specification.Show = cssSelector is null ? "bottom" : $"{cssSelector}:bottom";
break;
}

return this;
}

Expand Down Expand Up @@ -264,10 +272,10 @@ public SwapStyleBuilder ShowWindow(ScrollDirection direction)
switch (direction)
{
case ScrollDirection.top:
AddModifier("show", "window:top");
specification.Show = "window:top";
break;
case ScrollDirection.bottom:
AddModifier("show", "window:bottom");
specification.Show = "window:bottom";
break;
}

Expand Down Expand Up @@ -307,58 +315,37 @@ public SwapStyleBuilder ShowWindow(ScrollDirection direction)
/// <returns>This <see cref="SwapStyleBuilder"/> object instance.</returns>
public SwapStyleBuilder ShowNone()
{
AddModifier("show", "none");
specification.Show = "none";

return this;
}

/// <inheritdoc/>
public override string ToString()
{
var (swapStyle, modifier) = Build();
var styleText = swapStyle.ToHtmxString();
var value = !string.IsNullOrWhiteSpace(modifier)
? $"{styleText} {modifier}"
: styleText;
return value;
}
public override string ToString() => Build();

/// <summary>
/// Builds the swap style command string with all specified modifiers.
/// </summary>
/// <returns>A tuple containing the <see cref="SwapStyle"/> and the constructed command string.</returns>
internal (SwapStyle, string) Build()
{
var value = string.Empty;

if (modifiers.Count > 0)
{
value = modifiers
.Cast<DictionaryEntry>()
.Select(entry => $"{entry.Key}:{entry.Value}")
.Aggregate((current, next) => $"{current} {next}");
}

return (style, value);
}

/// <summary>
/// Adds a modifier to modifiers, overriding existing values if present
/// </summary>
/// <param name="modifier"></param>
/// <param name="options"></param>
private void AddModifier(string modifier, string options)
internal string Build()
{
if (modifiers.Contains(modifier))
modifiers.Remove(modifier);

modifiers.Add(modifier, options);
string value = specification.SwapStyle.ToHtmxString();

if (specification.SwapDelay != null)
value += " swap:" + specification.SwapDelay;
if (specification.SettleDelay != null)
value += " settle:" + specification.SettleDelay;
if (specification.Scroll != null)
value += " scroll:" + specification.Scroll;
if (specification.IgnoreTitle != null)
value += " ignoreTitle:" + specification.IgnoreTitle;
if (specification.Transition != null)
value += " transition:" + specification.Transition;
if (specification.FocusScroll != null)
value += " focus-scroll:" + specification.FocusScroll;
if (specification.Show != null)
value += " show:" + specification.Show;

return value.TrimStart();
}

/// <summary>
/// Adds a boolean modifier to modifiers
/// </summary>
/// <param name="modifier"></param>
/// <param name="option"></param>
private void AddModifier(string modifier, bool option) => AddModifier(modifier, option ? "true" : "false");
}
Loading
Loading