diff --git a/src/Htmxor/Constants.cs b/src/Htmxor/Constants.cs index 01dd979..7315e78 100644 --- a/src/Htmxor/Constants.cs +++ b/src/Htmxor/Constants.cs @@ -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"; } /// diff --git a/src/Htmxor/HtmxConfig.cs b/src/Htmxor/HtmxConfig.cs index e5c4315..f450432 100644 --- a/src/Htmxor/HtmxConfig.cs +++ b/src/Htmxor/HtmxConfig.cs @@ -91,7 +91,7 @@ public SwapStyle? DefaultSwapStyle /// /// Defaults to 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). /// [JsonPropertyName("allowEval")] public bool? AllowEval { get; set; } @@ -218,4 +218,4 @@ public SwapStyle? DefaultSwapStyle [JsonInclude, JsonPropertyName("antiforgery")] internal HtmxorAntiforgeryOptions? Antiforgery { get; init; } -} \ No newline at end of file +} diff --git a/src/Htmxor/Http/HtmxRequestHeaderNames.cs b/src/Htmxor/Http/HtmxRequestHeaderNames.cs index 4b53e71..0ffe6b1 100644 --- a/src/Htmxor/Http/HtmxRequestHeaderNames.cs +++ b/src/Htmxor/Http/HtmxRequestHeaderNames.cs @@ -1,4 +1,4 @@ -namespace Htmxor.Http; +namespace Htmxor.Http; /// /// The HTMX request header names. @@ -49,5 +49,5 @@ public static class HtmxRequestHeaderNames /// /// The `id` of the event handler to trigger on request. /// - internal const string EventHandlerId = "Htmxor-Event-Handler-Id"; + internal const string EventHandlerId = "HXOR-Event-Handler-Id"; } diff --git a/src/Htmxor/Rendering/HtmxorRenderer.HtmlWriting.cs b/src/Htmxor/Rendering/HtmxorRenderer.HtmlWriting.cs index 833c912..fda3fa2 100644 --- a/src/Htmxor/Rendering/HtmxorRenderer.HtmlWriting.cs +++ b/src/Htmxor/Rendering/HtmxorRenderer.HtmlWriting.cs @@ -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('\"'); } } } diff --git a/src/Htmxor/wwwroot/htmxor.js b/src/Htmxor/wwwroot/htmxor.js index 9940008..d130e66 100644 --- a/src/Htmxor/wwwroot/htmxor.js +++ b/src/Htmxor/wwwroot/htmxor.js @@ -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; - } - } -}); \ No newline at end of file + 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; +});