Skip to content

Commit

Permalink
refactor: use hxor-eventid instead of hx-headers for htmxor event han…
Browse files Browse the repository at this point in the history
…dler id
  • Loading branch information
egil committed May 12, 2024
1 parent 4623107 commit 910f09c
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 30 deletions.
2 changes: 2 additions & 0 deletions src/Htmxor/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public static class Attributes
public const string HxTarget = "hx-target";
public const string HxSwap = "hx-swap";
public const string HxHeaders = "hx-headers";

internal const string HxorEventId = "hxor-eventid";
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions src/Htmxor/HtmxConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public partial record class HtmxConfig

/// <summary>
/// Defaults to <see langword="true" /> if this property is null.
/// Can be used to disable htmx’s use of eval for certain features (e.g. trigger filters).
/// Can be used to disable htmx’s use of eval for certain features (e.g. trigger filters).
/// </summary>
[JsonPropertyName("allowEval")]
public bool? AllowEval { get; set; }
Expand Down Expand Up @@ -218,4 +218,4 @@ public partial record class HtmxConfig

[JsonInclude, JsonPropertyName("antiforgery")]
internal HtmxorAntiforgeryOptions? Antiforgery { get; init; }
}
}
4 changes: 2 additions & 2 deletions src/Htmxor/Http/HtmxRequestHeaderNames.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Htmxor.Http;
namespace Htmxor.Http;

/// <summary>
/// The HTMX request header names.
Expand Down Expand Up @@ -49,5 +49,5 @@ public static class HtmxRequestHeaderNames
/// <summary>
/// The `id` of the event handler to trigger on request.
/// </summary>
internal const string EventHandlerId = "Htmxor-Event-Handler-Id";
internal const string EventHandlerId = "HXOR-Event-Handler-Id";
}
10 changes: 3 additions & 7 deletions src/Htmxor/Rendering/HtmxorRenderer.HtmlWriting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -438,18 +438,14 @@ void EmitFormActionIfNotExplicit(TextWriter output, bool isForm, bool hasExplici

void EmitHtmxorEventHandlerId(TextWriter output)
{
// TODO: handle hx-header already existing.
// hx-headers='{"myHeader": "My Value"}'
if (hasHxAction && hxEventHandlerId > 0)
{
output.Write(' ');
output.Write(Constants.Attributes.HxHeaders);
output.Write(Constants.Attributes.HxorEventId);
output.Write('=');
output.Write("'{\"");
output.Write(HtmxRequestHeaderNames.EventHandlerId);
output.Write("\":\"");
output.Write('\"');
output.Write(htmxorEventsByEventHandlerId[hxEventHandlerId].HtmxorEventId);
output.Write("\"}'");
output.Write('\"');
}
}
}
Expand Down
42 changes: 23 additions & 19 deletions src/Htmxor/wwwroot/htmxor.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
document.addEventListener('htmx:configRequest', (evt) => {
const httpVerb = evt.detail.verb.toUpperCase();
if (httpVerb === 'GET' || httpVerb === 'HEAD' || httpVerb === 'OPTIONS' || httpVerb === 'TRACE')
return;
const httpVerb = evt.detail.verb.toUpperCase();
if (httpVerb === 'GET' || httpVerb === 'HEAD' || httpVerb === 'OPTIONS' || httpVerb === 'TRACE')
return;

const antiforgery = htmx.config.antiforgery;
const antiforgery = htmx.config.antiforgery;

if (antiforgery) {
if (antiforgery) {

// already specified on form, short circuit
if (evt.detail.parameters[antiforgery.formFieldName])
return;
// already specified on form, short circuit
if (evt.detail.parameters[antiforgery.formFieldName])
return;

const requestToken = document.cookie
.split("; ")
.find(row => row.startsWith(antiforgery.cookieName + "="))
.split("=")[1];
const requestToken = document.cookie
.split("; ")
.find(row => row.startsWith(antiforgery.cookieName + "="))
.split("=")[1];

if (antiforgery.headerName) {
evt.detail.headers[antiforgery.headerName] = requestToken;
} else {
evt.detail.parameters[antiforgery.formFieldName] = requestToken;
}
}
});
if (antiforgery.headerName) {
evt.detail.headers[antiforgery.headerName] = requestToken;
} else {
evt.detail.parameters[antiforgery.formFieldName] = requestToken;
}
}
});

document.addEventListener('htmx:configRequest', (evt) => {
evt.detail.headers["HXOR-Event-Handler-Id"] = evt.detail.elt.attributes['hxor-eventid'].value;
});

0 comments on commit 910f09c

Please sign in to comment.