Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 5e28714215 | |||
| 28cec1cb70 |
@@ -6,8 +6,6 @@
|
||||
|
||||
htmx uses attributes and HTTP headers to attain its interactivity; the libraries here contain extensions to both Giraffe and Giraffe View Engine to enable strongly-typed development of htmx applications.
|
||||
|
||||
> **NOTE** htmx v4 is curently in beta; this library is supporting it on the [`htmx-v4` branch](https://git.bitbadger.solutions/bit-badger/Giraffe.Htmx/src/branch/htmx-v4/). Beta versions of the v4 library include warnings about obsolete constructs. The htmx project intends to continue supporting the 2.x line (just as they still do the 1.x line, which we track on the [`htmx-v1` branch](https://git.bitbadger.solutions/bit-badger/Giraffe.Htmx/src/branch/htmx-v1/)); the upgrade is available, not forced. See the [htmx v4 migration docs](https://four.htmx.org/docs#migration) for more information.
|
||||
|
||||
## Installation
|
||||
|
||||
`Giraffe.Htmx` provides extensions that facilitate using htmx on the server side, primarily reading and setting headers. `Giraffe.ViewEngine.Htmx` provides attributes and helpers to produce views that utilize htmx. Both can be installed from NuGet via standard methods.
|
||||
@@ -16,8 +14,6 @@ htmx uses attributes and HTTP headers to attain its interactivity; the libraries
|
||||
|---|---|
|
||||
|[](https://www.nuget.org/packages/Giraffe.Htmx/)|[](https://www.nuget.org/packages/Giraffe.ViewEngine.Htmx/)|
|
||||
|
||||
Both of these packages will also install `Giraffe.Htmx.Common`, which has some common definitions and provides a local-to-your-project version of the htmx JavaScript _(as of v2.0.8)_.
|
||||
|
||||
## Server Side (`Giraffe.Htmx`)
|
||||
|
||||
In addition to the regular HTTP request payloads, htmx sets [one or more headers](https://htmx.org/docs/#request_headers) along with the request. Once `Giraffe.Htmx` is opened, these are available as properties on `HttpContext.Request.Headers`. These consist of the header name, translated to a .NET name (ex. `HX-Current-URL` becomes `HxCurrentUrl`), and a strongly-typed property based on the expected value of that header. Additionally, they are all exposed as `Option`s, as they may or may not be present for any given request.
|
||||
@@ -46,8 +42,6 @@ let theHandler : HttpHandler =
|
||||
|
||||
Of note is that the `HX-Trigger` headers can take either one or more events. For a single event with no parameters, use `withHxTrigger`; for a single event with parameters, or multiple events, use `withHxTriggerMany`. Both these have `AfterSettle` and `AfterSwap` versions as well.
|
||||
|
||||
`HtmxScript.local` provides an `HtmlString` with a script tag to load the package-provided htmx library. This can be used in code, Razor templates, etc. (If you're using Giraffe.ViewEngine, see below.)
|
||||
|
||||
## View Engine (`Giraffe.ViewEngine.Htmx`)
|
||||
|
||||
As htmx uses [attributes](https://htmx.org/docs/#attributes) to extend HTML, the primary part of this library defines attributes that can be used within Giraffe views. Simply open `Giraffe.ViewEngine.Htmx`, and these attributes, along with support modules, will be visible.
|
||||
@@ -72,7 +66,7 @@ let shiftClick =
|
||||
]
|
||||
```
|
||||
|
||||
If you want to use the package-provided htmx library, `Htmx.Script.local` will create the `script` tag for you. To load htmx from jsDelivr, `Htmx.Script.cdnMinified` or `Htmx.Script.cdnUnminified` can be used to load the script in your HTML trees. In this case, if you are using a Content Security Policy (CSP) header, `cdn.jsdelivr.net` will need to be added to the `script-src` list.
|
||||
If you want to load htmx from unpkg, `Htmx.Script.minified` or `Htmx.Script.unminified` can be used to load the script in your HTML trees.
|
||||
|
||||
## Feedback / Help
|
||||
|
||||
|
||||
+4
-18
@@ -2,12 +2,6 @@
|
||||
[<AutoOpen>]
|
||||
module Giraffe.Htmx.Common
|
||||
|
||||
/// <summary>The version of htmx embedded in the package</summary>
|
||||
let HtmxVersion = "2.0.10"
|
||||
|
||||
/// <summary>The path for the provided htmx script</summary>
|
||||
let internal htmxLocalScript = $"/_content/Giraffe.Htmx.Common/htmx.min.js?ver={HtmxVersion}"
|
||||
|
||||
/// <summary>Serialize a list of key/value pairs to JSON (very rudimentary)</summary>
|
||||
/// <param name="pairs">The key/value pairs to be serialized to JSON</param>
|
||||
/// <returns>A string with the key/value pairs serialized to JSON</returns>
|
||||
@@ -17,43 +11,35 @@ let internal toJson (pairs: (string * string) list) =
|
||||
|> String.concat ", "
|
||||
|> sprintf "{ %s }"
|
||||
|
||||
/// <summary>Convert a boolean to lowercase "true" or "false"</summary>
|
||||
/// <summary>Convert a boolean to lowercase <tt>true</tt> or <tt>false</tt></summary>
|
||||
/// <param name="boolValue">The boolean value to convert</param>
|
||||
/// <returns>"true" for <c>true</c>, "false" for <c>false</c></returns>
|
||||
/// <returns>"true" for <tt>true</tt>, "false" for <tt>false</tt></returns>
|
||||
let internal toLowerBool (boolValue: bool) =
|
||||
(string boolValue).ToLowerInvariant()
|
||||
|
||||
|
||||
/// <summary>Valid values for the <c>hx-swap</c> attribute / <c>HX-Reswap</c> header</summary>
|
||||
/// <remarks>May be combined with <c>swap</c> / <c>settle</c> / <c>scroll</c> / <c>show</c> config)</remarks>
|
||||
/// <seealso href="https://htmx.org/attributes/hx-swap/">Documentation</seealso>
|
||||
/// <summary>Valid values for the <tt>hx-swap</tt> attribute / <tt>HX-Reswap</tt> header</summary>
|
||||
/// <remarks>May be combined with <tt>swap</tt> / <tt>settle</tt> / <tt>scroll</tt> / <tt>show</tt> config)</remarks>
|
||||
[<RequireQualifiedAccess>]
|
||||
module HxSwap =
|
||||
|
||||
/// <summary>The default, replace the inner HTML of the target element</summary>
|
||||
[<Literal>]
|
||||
let InnerHtml = "innerHTML"
|
||||
|
||||
/// <summary>Replace the entire target element with the response</summary>
|
||||
[<Literal>]
|
||||
let OuterHtml = "outerHTML"
|
||||
|
||||
/// <summary>Insert the response before the target element</summary>
|
||||
[<Literal>]
|
||||
let BeforeBegin = "beforebegin"
|
||||
|
||||
/// <summary>Insert the response before the first child of the target element</summary>
|
||||
[<Literal>]
|
||||
let AfterBegin = "afterbegin"
|
||||
|
||||
/// <summary>Insert the response after the last child of the target element</summary>
|
||||
[<Literal>]
|
||||
let BeforeEnd = "beforeend"
|
||||
|
||||
/// <summary>Insert the response after the target element</summary>
|
||||
[<Literal>]
|
||||
let AfterEnd = "afterend"
|
||||
|
||||
/// <summary>Does not append content from response (out of band items will still be processed).</summary>
|
||||
[<Literal>]
|
||||
let None = "none"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Razor">
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
## Giraffe.Htmx.Common
|
||||
|
||||
This package contains common code shared between [`Giraffe.Htmx`](https://www.nuget.org/packages/Giraffe.Htmx) and [`Giraffe.ViewEngine.Htmx`](https://www.nuget.org/packages/Giraffe.ViewEngine.Htmx), and will be automatically installed when you install either one. It also contains htmx as a static web asset, allowing it to be loaded from your local (or published) project.
|
||||
This package contains common code shared between [`Giraffe.Htmx`](https://www.nuget.org/packages/Giraffe.Htmx) and [`Giraffe.ViewEngine.Htmx`](https://www.nuget.org/packages/Giraffe.ViewEngine.Htmx), and will be automatically installed when you install either one.
|
||||
|
||||
**htmx version: 2.0.10**
|
||||
**htmx version: 2.0.4**
|
||||
|
||||
Vendored
-1
File diff suppressed because one or more lines are too long
@@ -1,13 +1,10 @@
|
||||
<?xml version="1.0" encoding="utf-8" standalone="no"?>
|
||||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>net8.0;net9.0;net10.0</TargetFrameworks>
|
||||
<VersionPrefix>2.0.10</VersionPrefix>
|
||||
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
|
||||
<VersionPrefix>2.0.5</VersionPrefix>
|
||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||
<PackageReleaseNotes>- Updates provided script and CDN links to serve htmx 2.0.10 (no header or attribute changes)
|
||||
|
||||
NOTE: As of 2.0.6, the CDN for htmx changed from unpkg.com to cdn.jsdelivr.net; sites with Content-Security-Policy headers will want to update their allowed script-src domains accordingly
|
||||
</PackageReleaseNotes>
|
||||
<PackageReleaseNotes>Add full packaged XML documentation; update script tags to pull htmx 2.0.5 (no header or attribute changes)</PackageReleaseNotes>
|
||||
<Authors>danieljsummers</Authors>
|
||||
<Company>Bit Badger Solutions</Company>
|
||||
<PackageProjectUrl>https://git.bitbadger.solutions/bit-badger/Giraffe.Htmx</PackageProjectUrl>
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 16
|
||||
VisualStudioVersion = 16.0.30114.105
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Giraffe.Htmx", "Htmx\Giraffe.Htmx.fsproj", "{8AB3085C-5236-485A-8565-A09106E72E1E}"
|
||||
EndProject
|
||||
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Giraffe.ViewEngine.Htmx", "ViewEngine.Htmx\Giraffe.ViewEngine.Htmx.fsproj", "{F718B3C1-EE01-4F04-ABCE-BF2AE700FDA9}"
|
||||
EndProject
|
||||
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Giraffe.Htmx.Common", "Common\Giraffe.Htmx.Common.fsproj", "{75D66845-F93A-4463-AD29-A8B16E4D4BA9}"
|
||||
EndProject
|
||||
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Tests", "Tests\Tests.fsproj", "{39823773-4311-4E79-9CA0-F9DDC40CAF6A}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{8AB3085C-5236-485A-8565-A09106E72E1E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{8AB3085C-5236-485A-8565-A09106E72E1E}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{8AB3085C-5236-485A-8565-A09106E72E1E}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{8AB3085C-5236-485A-8565-A09106E72E1E}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{F718B3C1-EE01-4F04-ABCE-BF2AE700FDA9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{F718B3C1-EE01-4F04-ABCE-BF2AE700FDA9}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{F718B3C1-EE01-4F04-ABCE-BF2AE700FDA9}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{F718B3C1-EE01-4F04-ABCE-BF2AE700FDA9}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{75D66845-F93A-4463-AD29-A8B16E4D4BA9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{75D66845-F93A-4463-AD29-A8B16E4D4BA9}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{75D66845-F93A-4463-AD29-A8B16E4D4BA9}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{75D66845-F93A-4463-AD29-A8B16E4D4BA9}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{39823773-4311-4E79-9CA0-F9DDC40CAF6A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{39823773-4311-4E79-9CA0-F9DDC40CAF6A}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{39823773-4311-4E79-9CA0-F9DDC40CAF6A}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{39823773-4311-4E79-9CA0-F9DDC40CAF6A}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
@@ -1,6 +0,0 @@
|
||||
<Solution>
|
||||
<Project Path="Common/Giraffe.Htmx.Common.fsproj" />
|
||||
<Project Path="Htmx/Giraffe.Htmx.fsproj" />
|
||||
<Project Path="Tests/Tests.fsproj" />
|
||||
<Project Path="ViewEngine.Htmx/Giraffe.ViewEngine.Htmx.fsproj" />
|
||||
</Solution>
|
||||
+35
-57
@@ -11,7 +11,7 @@ let private hdr (headers : IHeaderDictionary) hdr =
|
||||
/// Extensions to the header dictionary
|
||||
type IHeaderDictionary with
|
||||
|
||||
/// <summary>Indicates that the request is via an element using <c>hx-boost</c></summary>
|
||||
/// <summary>Indicates that the request is via an element using <tt>hx-boost</tt></summary>
|
||||
member this.HxBoosted
|
||||
with get () = hdr this "HX-Boosted" |> Option.map bool.Parse
|
||||
|
||||
@@ -19,27 +19,29 @@ type IHeaderDictionary with
|
||||
member this.HxCurrentUrl
|
||||
with get () = hdr this "HX-Current-URL" |> Option.map Uri
|
||||
|
||||
/// <summary><c>true</c> if the request is for history restoration after a miss in the local history cache</summary>
|
||||
/// <summary>
|
||||
/// <tt>true</tt> if the request is for history restoration after a miss in the local history cache
|
||||
/// </summary>
|
||||
member this.HxHistoryRestoreRequest
|
||||
with get () = hdr this "HX-History-Restore-Request" |> Option.map bool.Parse
|
||||
|
||||
/// <summary>The user response to an <c>hx-prompt</c></summary>
|
||||
/// <summary>The user response to an <tt>hx-prompt</tt></summary>
|
||||
member this.HxPrompt
|
||||
with get () = hdr this "HX-Prompt"
|
||||
|
||||
/// <summary><c>true</c> if the request came from htmx</summary>
|
||||
/// <summary><tt>true</tt> if the request came from htmx</summary>
|
||||
member this.HxRequest
|
||||
with get () = hdr this "HX-Request" |> Option.map bool.Parse
|
||||
|
||||
/// <summary>The <c>id</c> attribute of the target element if it exists</summary>
|
||||
/// <summary>The <tt>id</tt> attribute of the target element if it exists</summary>
|
||||
member this.HxTarget
|
||||
with get () = hdr this "HX-Target"
|
||||
|
||||
/// <summary>The <c>id</c> attribute of the triggered element if it exists</summary>
|
||||
/// <summary>The <tt>id</tt> attribute of the triggered element if it exists</summary>
|
||||
member this.HxTrigger
|
||||
with get () = hdr this "HX-Trigger"
|
||||
|
||||
/// <summary>The <c>name</c> attribute of the triggered element if it exists</summary>
|
||||
/// <summary>The <tt>name</tt> attribute of the triggered element if it exists</summary>
|
||||
member this.HxTriggerName
|
||||
with get () = hdr this "HX-Trigger-Name"
|
||||
|
||||
@@ -62,123 +64,99 @@ module Handlers =
|
||||
|
||||
open Giraffe.Htmx.Common
|
||||
|
||||
/// <summary>Instruct htmx to perform a client-side redirect for content</summary>
|
||||
/// <param name="path">The path where the content should be found</param>
|
||||
/// <returns>An HTTP handler with the <c>HX-Location</c> header set</returns>
|
||||
/// <seealso href="https://htmx.org/headers/hx-location/">Documentation</seealso>
|
||||
let withHxLocation (path: string) : HttpHandler =
|
||||
setHttpHeader "HX-Location" path
|
||||
|
||||
/// <summary>Pushes a new url into the history stack</summary>
|
||||
/// <param name="url">The URL to be pushed</param>
|
||||
/// <returns>An HTTP handler with the <c>HX-Push-Url</c> header set</returns>
|
||||
/// <remarks>Use <see cref="withHxNoPushUrl" /> to explicitly not push a new URL</remarks>
|
||||
/// <seealso href="https://htmx.org/headers/hx-push-url/">Documentation</seealso>
|
||||
/// <returns>An HTTP handler with the <tt>HX-Push-Url</tt> header set</returns>
|
||||
let withHxPushUrl (url: string) : HttpHandler =
|
||||
setHttpHeader "HX-Push-Url" url
|
||||
|
||||
/// <summary>Explicitly do not push a new URL into the history stack</summary>
|
||||
/// <returns>An HTTP handler with the <c>HX-Push-Url</c> header set to <c>false</c></returns>
|
||||
/// <seealso href="https://htmx.org/headers/hx-push-url/">Documentation</seealso>
|
||||
/// <returns>An HTTP handler with the <tt>HX-Push-Url</tt> header set to <tt>false</tt></returns>
|
||||
let withHxNoPushUrl : HttpHandler =
|
||||
toLowerBool false |> withHxPushUrl
|
||||
|
||||
/// Pushes a new url into the history stack
|
||||
[<Obsolete "Use withHxPushUrl; HX-Push was replaced by HX-Push-Url in v1.8.0">]
|
||||
let withHxPush = withHxPushUrl
|
||||
|
||||
/// Explicitly do not push a new URL into the history stack
|
||||
[<Obsolete "Use withHxNoPushUrl; HX-Push was replaced by HX-Push-Url in v1.8.0">]
|
||||
let withHxNoPush = withHxNoPushUrl
|
||||
|
||||
/// <summary>Can be used to do a client-side redirect to a new location</summary>
|
||||
/// <param name="url">The URL to which the client should be redirected</param>
|
||||
/// <returns>An HTTP handler with the <c>HX-Redirect</c> header set</returns>
|
||||
/// <seealso href="https://htmx.org/headers/hx-redirect/">Documentation</seealso>
|
||||
/// <returns>An HTTP handler with the <tt>HX-Redirect</tt> header set</returns>
|
||||
let withHxRedirect (url: string) : HttpHandler =
|
||||
setHttpHeader "HX-Redirect" url
|
||||
|
||||
/// <summary>If set to <c>true</c> the client side will do a full refresh of the page</summary>
|
||||
/// <summary>If set to <tt>true</tt> the client side will do a full refresh of the page</summary>
|
||||
/// <param name="shouldRefresh">Whether the client should refresh their page</param>
|
||||
/// <returns>An HTTP handler with the <c>HX-Refresh</c> header set</returns>
|
||||
/// <returns>An HTTP handler with the <tt>HX-Refresh</tt> header set</returns>
|
||||
let withHxRefresh shouldRefresh : HttpHandler =
|
||||
(toLowerBool >> setHttpHeader "HX-Refresh") shouldRefresh
|
||||
|
||||
/// <summary>Replaces the current URL in the history stack</summary>
|
||||
/// <param name="url">The URL to place in the history stack in place of the current one</param>
|
||||
/// <returns>An HTTP handler with the <c>HX-Replace-URL</c> header set</returns>
|
||||
/// <remarks>Use <see cref="withHxNoRelaceUrl" /> to explicitly not replace the current URL</remarks>
|
||||
/// <seealso href="https://htmx.org/headers/hx-replace-url/">Documentation</seealso>
|
||||
/// <returns>An HTTP handler with the <tt>HX-Replace-URL</tt> header set</returns>
|
||||
let withHxReplaceUrl url : HttpHandler =
|
||||
setHttpHeader "HX-Replace-Url" url
|
||||
|
||||
/// <summary>Explicitly do not replace the current URL in the history stack</summary>
|
||||
/// <returns>An HTTP handler with the <c>HX-Replace-URL</c> header set to <c>false</c></returns>
|
||||
/// <seealso href="https://htmx.org/headers/hx-replace-url/">Documentation</seealso>
|
||||
/// <returns>An HTTP handler with the <tt>HX-Replace-URL</tt> header set to <tt>false</tt></returns>
|
||||
let withHxNoReplaceUrl : HttpHandler =
|
||||
toLowerBool false |> withHxReplaceUrl
|
||||
|
||||
/// <summary>Override which portion of the response will be swapped into the target document</summary>
|
||||
/// <param name="target">The selector for the new response target</param>
|
||||
/// <returns>An HTTP handler with the <c>HX-Reselect</c> header set</returns>
|
||||
/// <returns>An HTTP handler with the <tt>HX-Reselect</tt> header set</returns>
|
||||
let withHxReselect (target: string) : HttpHandler =
|
||||
setHttpHeader "HX-Reselect" target
|
||||
|
||||
/// <summary>Override the <c>hx-swap</c> attribute from the initiating element</summary>
|
||||
/// <summary>Override the <tt>hx-swap</tt> attribute from the initiating element</summary>
|
||||
/// <param name="swap">The swap value to override</param>
|
||||
/// <returns>An HTTP handler with the <c>HX-Reswap</c> header set</returns>
|
||||
/// <returns>An HTTP handler with the <tt>HX-Reswap</tt> header set</returns>
|
||||
/// <remarks>Use <see cref="T:Giraffe.Htmx.Common.HxSwap">HxSwap</see> constants for best results</remarks>
|
||||
let withHxReswap (swap: string) : HttpHandler =
|
||||
setHttpHeader "HX-Reswap" swap
|
||||
|
||||
/// <summary>Allows you to override the <c>hx-target</c> attribute</summary>
|
||||
/// <summary>Allows you to override the <tt>hx-target</tt> attribute</summary>
|
||||
/// <param name="target">The new target for the response</param>
|
||||
/// <returns>An HTTP handler with the <c>HX-Retarget</c> header set</returns>
|
||||
/// <returns>An HTTP handler with the <tt>HX-Retarget</tt> header set</returns>
|
||||
let withHxRetarget (target: string) : HttpHandler =
|
||||
setHttpHeader "HX-Retarget" target
|
||||
|
||||
/// <summary>Allows you to trigger a single client side event</summary>
|
||||
/// <param name="evt">The call to the event that should be triggered</param>
|
||||
/// <returns>An HTTP handler with the <c>HX-Trigger</c> header set</returns>
|
||||
/// <seealso href="https://htmx.org/headers/hx-trigger/">Documentation</seealso>
|
||||
/// <returns>An HTTP handler with the <tt>HX-Trigger</tt> header set</returns>
|
||||
let withHxTrigger (evt: string) : HttpHandler =
|
||||
setHttpHeader "HX-Trigger" evt
|
||||
|
||||
/// <summary>Allows you to trigger multiple client side events</summary>
|
||||
/// <param name="evts">The calls to events that should be triggered</param>
|
||||
/// <returns>An HTTP handler with the <c>HX-Trigger</c> header set for all given events</returns>
|
||||
/// <seealso href="https://htmx.org/headers/hx-trigger/">Documentation</seealso>
|
||||
/// <returns>An HTTP handler with the <tt>HX-Trigger</tt> header set for all given events</returns>
|
||||
let withHxTriggerMany evts : HttpHandler =
|
||||
toJson evts |> setHttpHeader "HX-Trigger"
|
||||
|
||||
/// <summary>Allows you to trigger a single client side event after changes have settled</summary>
|
||||
/// <param name="evt">The call to the event that should be triggered</param>
|
||||
/// <returns>An HTTP handler with the <c>HX-Trigger-After-Settle</c> header set</returns>
|
||||
/// <seealso href="https://htmx.org/headers/hx-trigger/">Documentation</seealso>
|
||||
/// <returns>An HTTP handler with the <tt>HX-Trigger-After-Settle</tt> header set</returns>
|
||||
let withHxTriggerAfterSettle (evt: string) : HttpHandler =
|
||||
setHttpHeader "HX-Trigger-After-Settle" evt
|
||||
|
||||
/// <summary>Allows you to trigger multiple client side events after changes have settled</summary>
|
||||
/// <param name="evts">The calls to events that should be triggered</param>
|
||||
/// <returns>An HTTP handler with the <c>HX-Trigger-After-Settle</c> header set for all given events</returns>
|
||||
/// <seealso href="https://htmx.org/headers/hx-trigger/">Documentation</seealso>
|
||||
/// <returns>An HTTP handler with the <tt>HX-Trigger-After-Settle</tt> header set for all given events</returns>
|
||||
let withHxTriggerManyAfterSettle evts : HttpHandler =
|
||||
toJson evts |> setHttpHeader "HX-Trigger-After-Settle"
|
||||
|
||||
/// <summary>Allows you to trigger a single client side event after DOM swapping occurs</summary>
|
||||
/// <param name="evt">The call to the event that should be triggered</param>
|
||||
/// <returns>An HTTP handler with the <c>HX-Trigger-After-Swap</c> header set</returns>
|
||||
/// <seealso href="https://htmx.org/headers/hx-trigger/">Documentation</seealso>
|
||||
/// <returns>An HTTP handler with the <tt>HX-Trigger-After-Swap</tt> header set</returns>
|
||||
let withHxTriggerAfterSwap (evt: string) : HttpHandler =
|
||||
setHttpHeader "HX-Trigger-After-Swap" evt
|
||||
|
||||
/// <summary>Allows you to trigger multiple client side events after DOM swapping occurs</summary>
|
||||
/// <param name="evts">The calls to events that should be triggered</param>
|
||||
/// <returns>An HTTP handler with the <c>HX-Trigger-After-Swap</c> header set for all given events</returns>
|
||||
/// <seealso href="https://htmx.org/headers/hx-trigger/">Documentation</seealso>
|
||||
/// <returns>An HTTP handler with the <tt>HX-Trigger-After-Swap</tt> header set for all given events</returns>
|
||||
let withHxTriggerManyAfterSwap evts : HttpHandler =
|
||||
toJson evts |> setHttpHeader "HX-Trigger-After-Swap"
|
||||
|
||||
|
||||
/// <summary>Load the package-provided version of the htmx script</summary>
|
||||
[<RequireQualifiedAccess>]
|
||||
module HtmxScript =
|
||||
|
||||
open Giraffe.Htmx.Common
|
||||
open Microsoft.AspNetCore.Html
|
||||
|
||||
/// <summary><c>script</c> tag to load the package-provided version of the htmx script</summary>
|
||||
let local = HtmlString $"""<script src="{htmxLocalScript}"></script>"""
|
||||
|
||||
+1
-3
@@ -2,7 +2,7 @@
|
||||
|
||||
This package enables server-side support for [htmx](https://htmx.org) within [Giraffe](https://giraffe.wiki) and ASP.NET's `HttpContext`.
|
||||
|
||||
**htmx version: 2.0.10**
|
||||
**htmx version: 2.0.4**
|
||||
|
||||
_Upgrading from v1.x: the [migration guide](https://htmx.org/migration-guide-htmx-1/) does not currently specify any request or response header changes. This means that there are no required code changes in moving from v1.* to v2.*._
|
||||
|
||||
@@ -34,8 +34,6 @@ let myHandler : HttpHander =
|
||||
|
||||
The `HxSwap` module has constants to use for the `HX-Reswap` header. These may be extended with settle, show, and other qualifiers; see the htmx documentation for the `hx-swap` attribute for more information.
|
||||
|
||||
To load the package-provided htmx library without using Giraffe.ViewEngine, use `HtmxScript.local`.
|
||||
|
||||
### Learn
|
||||
|
||||
The naming conventions of this library were selected to mirror those provided by htmx. The header properties become `Hx*` on the `ctx.Request.Headers` object, and the response handlers are `withHx*` based on the header being set. The only part that does not line up is `withHxTrigger*` and `withHxTriggerMany`; the former set work with a single string (to trigger a single event with no arguments), while the latter set supports both arguments and multiple events.
|
||||
+1
-7
@@ -3,12 +3,6 @@ module Common
|
||||
open Expecto
|
||||
open Giraffe.Htmx
|
||||
|
||||
/// Test to ensure the version was updated
|
||||
let version =
|
||||
test "HtmxVersion is correct" {
|
||||
Expect.equal HtmxVersion "2.0.10" "htmx version incorrect"
|
||||
}
|
||||
|
||||
/// Tests for the HxSwap module
|
||||
let swap =
|
||||
testList "HxSwap" [
|
||||
@@ -36,4 +30,4 @@ let swap =
|
||||
]
|
||||
|
||||
/// All tests for this module
|
||||
let allTests = testList "Htmx.Common" [ version; swap ]
|
||||
let allTests = testList "Htmx.Common" [ swap ]
|
||||
|
||||
+1
-21
@@ -3,7 +3,6 @@ module Htmx
|
||||
open System
|
||||
open Expecto
|
||||
open Giraffe.Htmx
|
||||
open Microsoft.AspNetCore.Html
|
||||
open Microsoft.AspNetCore.Http
|
||||
open NSubstitute
|
||||
|
||||
@@ -208,14 +207,6 @@ let next (ctx: HttpContext) = Task.FromResult(Some ctx)
|
||||
/// Tests for the HttpHandler functions provided in the Handlers module
|
||||
let handlers =
|
||||
testList "HandlerTests" [
|
||||
testTask "withHxLocation succeeds" {
|
||||
let ctx = Substitute.For<HttpContext>()
|
||||
let dic = HeaderDictionary()
|
||||
ctx.Response.Headers.ReturnsForAnyArgs dic |> ignore
|
||||
let! _ = withHxLocation "/pagina-otro.html" next ctx
|
||||
Expect.isTrue (dic.ContainsKey "HX-Location") "The HX-Location header should be present"
|
||||
Expect.equal dic["HX-Location"].[0] "/pagina-otro.html" "The HX-Location value was incorrect"
|
||||
}
|
||||
testTask "withHxPushUrl succeeds" {
|
||||
let ctx = Substitute.For<HttpContext> ()
|
||||
let dic = HeaderDictionary ()
|
||||
@@ -355,16 +346,5 @@ let handlers =
|
||||
}
|
||||
]
|
||||
|
||||
/// Tests for the HtmxScript module
|
||||
let script =
|
||||
testList "HtmxScript" [
|
||||
test "local generates correct link" {
|
||||
Expect.equal
|
||||
(string HtmxScript.local)
|
||||
$"""<script src="/_content/Giraffe.Htmx.Common/htmx.min.js?ver={HtmxVersion}"></script>"""
|
||||
"htmx script link is incorrect"
|
||||
}
|
||||
]
|
||||
|
||||
/// All tests for this module
|
||||
let allTests = testList "Htmx" [ dictExtensions; reqExtensions; handlers; script ]
|
||||
let allTests = testList "Htmx" [ dictExtensions; reqExtensions; handlers ]
|
||||
|
||||
+12
-52
@@ -406,8 +406,8 @@ let hxEvent =
|
||||
Expect.equal (XhrProgress.ToHxOnString()) "xhr:progress" "XhrProgress hx-on event name not correct"
|
||||
}
|
||||
]
|
||||
]
|
||||
|
||||
]
|
||||
/// Tests for the HxHeaders module
|
||||
let hxHeaders =
|
||||
testList "HxHeaders" [
|
||||
@@ -497,32 +497,6 @@ let hxRequest =
|
||||
]
|
||||
]
|
||||
|
||||
/// Tests for the HxSync module
|
||||
let hxSync =
|
||||
testList "HxSync" [
|
||||
test "Drop is correct" {
|
||||
Expect.equal HxSync.Drop "drop" "Drop is incorrect"
|
||||
}
|
||||
test "Abort is correct" {
|
||||
Expect.equal HxSync.Abort "abort" "Abort is incorrect"
|
||||
}
|
||||
test "Replace is correct" {
|
||||
Expect.equal HxSync.Replace "replace" "Replace is incorrect"
|
||||
}
|
||||
test "Queue is correct" {
|
||||
Expect.equal HxSync.Queue "queue" "Queue is incorrect"
|
||||
}
|
||||
test "QueueFirst is correct" {
|
||||
Expect.equal HxSync.QueueFirst "queue first" "QueueFirst is incorrect"
|
||||
}
|
||||
test "QueueLast is correct" {
|
||||
Expect.equal HxSync.QueueLast "queue last" "QueueLast is incorrect"
|
||||
}
|
||||
test "QueueAll is correct" {
|
||||
Expect.equal HxSync.QueueAll "queue all" "QueueAll is incorrect"
|
||||
}
|
||||
]
|
||||
|
||||
/// Tests for the HxTrigger module
|
||||
let hxTrigger =
|
||||
testList "HxTrigger" [
|
||||
@@ -662,9 +636,6 @@ let hxTrigger =
|
||||
test "succeeds when it is not the first modifier" {
|
||||
Expect.equal (HxTrigger.Queue "def" "click") "click queue:def" "Queue modifier incorrect"
|
||||
}
|
||||
test "succeeds when no type of queueing is given" {
|
||||
Expect.equal (HxTrigger.Queue "" "blur") "blur queue" "Queue modifier incorrect"
|
||||
}
|
||||
]
|
||||
testList "QueueFirst" [
|
||||
test "succeeds when it is the first modifier" {
|
||||
@@ -755,7 +726,7 @@ let attributes =
|
||||
|> shouldRender """<figure hx-headers="{ "X-Special-Header": "some-header" }"></figure>"""
|
||||
}
|
||||
test "_hxHistory succeeds" {
|
||||
span [ _hxHistory false ] [] |> shouldRender """<span hx-history="false"></span>"""
|
||||
span [ _hxHistory "false" ] [] |> shouldRender """<span hx-history="false"></span>"""
|
||||
}
|
||||
test "_hxHistoryElt succeeds" {
|
||||
table [ _hxHistoryElt ] [] |> shouldRender """<table hx-history-elt></table>"""
|
||||
@@ -786,7 +757,7 @@ let attributes =
|
||||
hr [ _hxPost "/hear-ye-hear-ye" ] |> shouldRender """<hr hx-post="/hear-ye-hear-ye">"""
|
||||
}
|
||||
test "_hxPreserve succeeds" {
|
||||
img [ _hxPreserve ] |> shouldRender """<img hx-preserve>"""
|
||||
img [ _hxPreserve ] |> shouldRender """<img hx-preserve="true">"""
|
||||
}
|
||||
test "_hxPrompt succeeds" {
|
||||
strong [ _hxPrompt "Who goes there?" ] []
|
||||
@@ -821,8 +792,7 @@ let attributes =
|
||||
li [ _hxSwapOob "true" ] [] |> shouldRender """<li hx-swap-oob="true"></li>"""
|
||||
}
|
||||
test "_hxSync succeeds" {
|
||||
nav [ _hxSync "closest form" HxSync.Abort ] []
|
||||
|> shouldRender """<nav hx-sync="closest form:abort"></nav>"""
|
||||
nav [ _hxSync "closest form:abort" ] [] |> shouldRender """<nav hx-sync="closest form:abort"></nav>"""
|
||||
}
|
||||
test "_hxTarget succeeds" {
|
||||
header [ _hxTarget "#somewhereElse" ] [] |> shouldRender """<header hx-target="#somewhereElse"></header>"""
|
||||
@@ -842,31 +812,22 @@ let attributes =
|
||||
}
|
||||
]
|
||||
|
||||
open Giraffe.Htmx.Common
|
||||
|
||||
/// Tests for the Script module
|
||||
let script =
|
||||
testList "Script" [
|
||||
test "local succeeds" {
|
||||
let html = RenderView.AsString.htmlNode Script.local
|
||||
test "minified succeeds" {
|
||||
let html = RenderView.AsString.htmlNode Script.minified
|
||||
Expect.equal
|
||||
html
|
||||
$"""<script src="/_content/Giraffe.Htmx.Common/htmx.min.js?ver={HtmxVersion}"></script>"""
|
||||
"Local script tag is incorrect"
|
||||
"""<script src="https://unpkg.com/htmx.org@2.0.4" integrity="sha384-HGfztofotfshcF7+8n44JQL2oJmowVChPTg48S+jvZoztPfvwD79OC/LTtG6dMp+" crossorigin="anonymous"></script>"""
|
||||
"Minified script tag is incorrect"
|
||||
}
|
||||
test "cdnMinified succeeds" {
|
||||
let html = RenderView.AsString.htmlNode Script.cdnMinified
|
||||
test "unminified succeeds" {
|
||||
let html = RenderView.AsString.htmlNode Script.unminified
|
||||
Expect.equal
|
||||
html
|
||||
$"""<script src="https://cdn.jsdelivr.net/npm/htmx.org@{HtmxVersion}/dist/htmx.min.js" integrity="sha384-H5SrcfygHmAuTDZphMHqBJLc3FhssKjG7w/CeCpFReSfwBWDTKpkzPP8c+cLsK+V" crossorigin="anonymous"></script>"""
|
||||
"CDN minified script tag is incorrect"
|
||||
}
|
||||
test "cdnUnminified succeeds" {
|
||||
let html = RenderView.AsString.htmlNode Script.cdnUnminified
|
||||
Expect.equal
|
||||
html
|
||||
$"""<script src="https://cdn.jsdelivr.net/npm/htmx.org@{HtmxVersion}/dist/htmx.js" integrity="sha384-Q+Dky3iHVJOr6wUjQ4ulh6uQ76an/t+ak1+PjMVaxRjbZamFLAG+u9InkfjbsEQf" crossorigin="anonymous"></script>"""
|
||||
"CDN unminified script tag is incorrect"
|
||||
"""<script src="https://unpkg.com/htmx.org@2.0.4/dist/htmx.js" integrity="sha384-oeUn82QNXPuVkGCkcrInrS1twIxKhkZiFfr2TdiuObZ3n3yIeMiqcRzkIcguaof1" crossorigin="anonymous"></script>"""
|
||||
"Unminified script tag is incorrect"
|
||||
}
|
||||
]
|
||||
|
||||
@@ -1025,7 +986,6 @@ let allTests =
|
||||
hxHeaders
|
||||
hxParams
|
||||
hxRequest
|
||||
hxSync
|
||||
hxTrigger
|
||||
hxVals
|
||||
attributes
|
||||
|
||||
+153
-455
@@ -1,16 +1,14 @@
|
||||
/// <summary>Types and functions supporting htmx attributes in Giraffe View Engine</summary>
|
||||
module Giraffe.ViewEngine.Htmx
|
||||
|
||||
/// <summary>Valid values for the <c>hx-encoding</c> attribute</summary>
|
||||
/// <summary>Valid values for the <tt>hx-encoding</tt> attribute</summary>
|
||||
[<RequireQualifiedAccess>]
|
||||
module HxEncoding =
|
||||
|
||||
/// <summary>A standard HTTP form</summary>
|
||||
[<Literal>]
|
||||
let Form = "application/x-www-form-urlencoded"
|
||||
|
||||
/// <summary>A multipart form (used for file uploads)</summary>
|
||||
[<Literal>]
|
||||
let MultipartForm = "multipart/form-data"
|
||||
|
||||
|
||||
@@ -197,11 +195,11 @@ type HxEvent =
|
||||
/// <summary>The htmx event name</summary>
|
||||
override this.ToString() = fst HxEvent.Values[this]
|
||||
|
||||
/// <summary>The <c>hx-on</c> variant of the htmx event name</summary>
|
||||
/// <summary>The <tt>hx-on</tt> variant of the htmx event name</summary>
|
||||
member this.ToHxOnString() = snd HxEvent.Values[this]
|
||||
|
||||
|
||||
/// <summary>Helper to create the <c>hx-headers</c> attribute</summary>
|
||||
/// <summary>Helper to create the <tt>hx-headers</tt> attribute</summary>
|
||||
[<RequireQualifiedAccess>]
|
||||
module HxHeaders =
|
||||
|
||||
@@ -209,34 +207,30 @@ module HxHeaders =
|
||||
let From = Giraffe.Htmx.Common.toJson
|
||||
|
||||
|
||||
/// <summary>Values / helpers for the <c>hx-params</c> attribute</summary>
|
||||
/// <seealso href="https://htmx.org/attributes/hx-params/">Documentation</seealso>
|
||||
/// <summary>Values / helpers for the <tt>hx-params</tt> attribute</summary>
|
||||
[<RequireQualifiedAccess>]
|
||||
module HxParams =
|
||||
|
||||
/// <summary>Include all parameters</summary>
|
||||
[<Literal>]
|
||||
let All = "*"
|
||||
|
||||
/// <summary>Include no parameters</summary>
|
||||
[<Literal>]
|
||||
let None = "none"
|
||||
|
||||
/// <summary>Include the specified parameters</summary>
|
||||
/// <param name="fields">One or more fields to include in the request</param>
|
||||
/// <returns>The list of fields for the <c>hx-params</c> attribute value</returns>
|
||||
/// <returns>The list of fields for the <tt>hx-params</tt> attribute value</returns>
|
||||
let With fields =
|
||||
match fields with [] -> "" | _ -> fields |> List.reduce (fun acc it -> $"{acc},{it}")
|
||||
|
||||
/// <summary>Exclude the specified parameters</summary>
|
||||
/// <param name="fields">One or more fields to exclude from the request</param>
|
||||
/// <returns>The list of fields for the <c>hx-params</c> attribute value prefixed with "not"</returns>
|
||||
/// <returns>The list of fields for the <tt>hx-params</tt> attribute value prefixed with "not"</returns>
|
||||
let Except fields =
|
||||
With fields |> sprintf "not %s"
|
||||
|
||||
|
||||
/// <summary>Helpers to define <c>hx-request</c> attribute values</summary>
|
||||
/// <seealso href="https://htmx.org/attributes/hx-request/">Documentation</seealso>
|
||||
/// <summary>Helpers to define <tt>hx-request</tt> attribute values</summary>
|
||||
[<RequireQualifiedAccess>]
|
||||
module HxRequest =
|
||||
|
||||
@@ -257,60 +251,21 @@ module HxRequest =
|
||||
$"\"timeout\": {ms}"
|
||||
|
||||
/// <summary>Include or exclude credentials from the request</summary>
|
||||
/// <param name="send"><c>true</c> if credentials should be sent, <c>false</c> if not</param>
|
||||
/// <param name="send"><tt>true</tt> if credentials should be sent, <tt>false</tt> if not</param>
|
||||
/// <returns>A string with the configured credential options</returns>
|
||||
let Credentials send =
|
||||
(toLowerBool >> sprintf "\"credentials\": %s") send
|
||||
|
||||
/// <summary>Exclude or include headers from the request</summary>
|
||||
/// <param name="exclude">
|
||||
/// <c>true</c> if no headers should be sent; <c>false</c> if headers should be sent
|
||||
/// <tt>true</tt> if no headers should be sent; <tt>false</tt> if headers should be sent
|
||||
/// </param>
|
||||
/// <returns>A string with the configured header options</returns>
|
||||
let NoHeaders exclude =
|
||||
(toLowerBool >> sprintf "\"noHeaders\": %s") exclude
|
||||
|
||||
|
||||
/// <summary>Helpers for the <c>hx-sync</c> attribute</summary>
|
||||
/// <seealso href="https://htmx.org/attributes/hx-sync/">Documentation</seealso>
|
||||
[<RequireQualifiedAccess>]
|
||||
module HxSync =
|
||||
|
||||
/// <summary>Drop (ignore) this request if a request is already in flight</summary>
|
||||
/// <remarks>This is the default for <c>hx-sync</c></remarks>
|
||||
[<Literal>]
|
||||
let Drop = "drop"
|
||||
|
||||
/// <summary>
|
||||
/// Drop (ignore) this request if a request is already in flight, and if another request occurs while this one is in
|
||||
/// flight, abort this request
|
||||
/// </summary>
|
||||
[<Literal>]
|
||||
let Abort = "abort"
|
||||
|
||||
/// <summary>Abort any current in-flight request and replace it with this one</summary>
|
||||
[<Literal>]
|
||||
let Replace = "replace"
|
||||
|
||||
/// <summary>Place this request in an element-associated queue</summary>
|
||||
[<Literal>]
|
||||
let Queue = "queue"
|
||||
|
||||
/// <summary>Queue only the first request received while another request is in flight</summary>
|
||||
[<Literal>]
|
||||
let QueueFirst = "queue first"
|
||||
|
||||
/// <summary>Queue only the last request received while another request is in flight</summary>
|
||||
[<Literal>]
|
||||
let QueueLast = "queue last"
|
||||
|
||||
/// <summary>Queue all requests received while another request is in flight</summary>
|
||||
[<Literal>]
|
||||
let QueueAll = "queue all"
|
||||
|
||||
|
||||
/// <summary>Helpers for the <c>hx-trigger</c> attribute</summary>
|
||||
/// <seealso href="https://htmx.org/attributes/hx-trigger/">Documentation</seealso>
|
||||
/// Helpers for the `hx-trigger` attribute
|
||||
[<RequireQualifiedAccess>]
|
||||
module HxTrigger =
|
||||
|
||||
@@ -322,164 +277,96 @@ module HxTrigger =
|
||||
$"{parts[0]}[{parts[1]}&&{filter}]"
|
||||
| false -> $"{trigger}[{filter}]"
|
||||
|
||||
/// <summary>Trigger the event on a click</summary>
|
||||
[<Literal>]
|
||||
/// Trigger the event on a click
|
||||
let Click = "click"
|
||||
|
||||
/// <summary>Trigger the event on page load</summary>
|
||||
[<Literal>]
|
||||
/// Trigger the event on page load
|
||||
let Load = "load"
|
||||
|
||||
/// <summary>Trigger the event when the item is visible</summary>
|
||||
[<Literal>]
|
||||
/// Trigger the event when the item is visible
|
||||
let Revealed = "revealed"
|
||||
|
||||
/// <summary>Trigger this event every [timing declaration]</summary>
|
||||
/// <param name="duration">The duration on which this trigger should fire (e.g., "1s", "500ms")</param>
|
||||
/// <returns>A trigger timing specification</returns>
|
||||
let Every duration =
|
||||
$"every %s{duration}"
|
||||
/// Trigger this event every [timing declaration]
|
||||
let Every (duration : string) = $"every {duration}"
|
||||
|
||||
/// <summary>Helpers for defining filters</summary>
|
||||
/// Helpers for defining filters
|
||||
module Filter =
|
||||
|
||||
/// <summary>Only trigger the event if the <c>ALT</c> key is pressed</summary>
|
||||
/// Only trigger the event if the `ALT` key is pressed
|
||||
let Alt = appendFilter "altKey"
|
||||
|
||||
/// <summary>Only trigger the event if the <c>CTRL</c> key is pressed</summary>
|
||||
/// Only trigger the event if the `CTRL` key is pressed
|
||||
let Ctrl = appendFilter "ctrlKey"
|
||||
|
||||
/// <summary>Only trigger the event if the <c>SHIFT</c> key is pressed</summary>
|
||||
/// Only trigger the event if the `SHIFT` key is pressed
|
||||
let Shift = appendFilter "shiftKey"
|
||||
|
||||
/// <summary>Only trigger the event if <c>CTRL</c> and <c>ALT</c> are pressed</summary>
|
||||
/// Only trigger the event if `CTRL+ALT` are pressed
|
||||
let CtrlAlt = Ctrl >> Alt
|
||||
|
||||
/// <summary>Only trigger the event if <c>CTRL</c> and <c>SHIFT</c> are pressed</summary>
|
||||
/// Only trigger the event if `CTRL+SHIFT` are pressed
|
||||
let CtrlShift = Ctrl >> Shift
|
||||
|
||||
/// <summary>Only trigger the event if <c>CTRL</c>, <c>ALT</c>, and <c>SHIFT</c> are pressed</summary>
|
||||
/// Only trigger the event if `CTRL+ALT+SHIFT` are pressed
|
||||
let CtrlAltShift = CtrlAlt >> Shift
|
||||
|
||||
/// <summary>Only trigger the event if <c>ALT</c> and <c>SHIFT</c> are pressed</summary>
|
||||
/// Only trigger the event if `ALT+SHIFT` are pressed
|
||||
let AltShift = Alt >> Shift
|
||||
|
||||
/// Append a modifier to the current trigger
|
||||
let private appendModifier modifier current =
|
||||
if current = "" then modifier else $"{current} {modifier}"
|
||||
|
||||
/// <summary>Only trigger once</summary>
|
||||
/// <param name="action">The action which should only be fired once</param>
|
||||
/// <returns>A trigger spec to fire the given action once</returns>
|
||||
let Once action =
|
||||
appendModifier "once" action
|
||||
/// Only trigger once
|
||||
let Once = appendModifier "once"
|
||||
|
||||
/// <summary>Trigger when changed</summary>
|
||||
/// <param name="elt">The element from which the <c>onchange</c> event will be emitted</param>
|
||||
/// <returns>A trigger spec to fire when the given element changes</returns>
|
||||
let Changed elt =
|
||||
appendModifier "changed" elt
|
||||
/// Trigger when changed
|
||||
let Changed = appendModifier "changed"
|
||||
|
||||
/// <summary>Delay execution; resets every time the event is seen</summary>
|
||||
/// <param name="duration">The duration for the delay (e.g., "1s", "500ms")</param>
|
||||
/// <param name="action">The action which should be fired after the given delay</param>
|
||||
/// <returns>A trigger spec to fire the given action after the specified delay</returns>
|
||||
let Delay duration action =
|
||||
appendModifier $"delay:%s{duration}" action
|
||||
/// Delay execution; resets every time the event is seen
|
||||
let Delay = sprintf "delay:%s" >> appendModifier
|
||||
|
||||
/// <summary>Throttle execution; ignore other events, fire when duration passes</summary>
|
||||
/// <param name="duration">The duration for the throttling (e.g., "1s", "500ms")</param>
|
||||
/// <param name="action">The action which should be fired after the given duration</param>
|
||||
/// <returns>A trigger spec to fire the given action after the specified duration</returns>
|
||||
let Throttle duration action =
|
||||
appendModifier $"throttle:%s{duration}" action
|
||||
/// Throttle execution; ignore other events, fire when duration passes
|
||||
let Throttle = sprintf "throttle:%s" >> appendModifier
|
||||
|
||||
/// <summary>Trigger this event from a CSS selector</summary>
|
||||
/// <param name="selector">A CSS selector to identify elements which may fire this trigger</param>
|
||||
/// <param name="action">The action to be fired</param>
|
||||
/// <returns>A trigger spec to fire from the given element(s)</returns>
|
||||
let From selector action =
|
||||
appendModifier $"from:%s{selector}" action
|
||||
/// Trigger this event from a CSS selector
|
||||
let From = sprintf "from:%s" >> appendModifier
|
||||
|
||||
/// <summary>Trigger this event from the <c>document</c> object</summary>
|
||||
/// <param name="action">The action to be fired</param>
|
||||
/// <returns>A trigger spec to fire from the <c>document</c> element</returns>
|
||||
let FromDocument action =
|
||||
From "document" action
|
||||
/// Trigger this event from the `document` object
|
||||
let FromDocument = From "document"
|
||||
|
||||
/// <summary>Trigger this event from the <c>window</c> object</summary>
|
||||
/// <param name="action">The action to be fired</param>
|
||||
/// <returns>A trigger spec to fire from the <c>window</c> object</returns>
|
||||
let FromWindow action =
|
||||
From "window" action
|
||||
/// Trigger this event from the `window` object
|
||||
let FromWindow = From "window"
|
||||
|
||||
/// <summary>Trigger this event from the closest parent CSS selector</summary>
|
||||
/// <param name="selector">The CSS selector from which the action should be fired</param>
|
||||
/// <param name="action">The action to be fired</param>
|
||||
/// <returns>A trigger spec to fire from the closest element</returns>
|
||||
let FromClosest selector action =
|
||||
From $"closest %s{selector}" action
|
||||
/// Trigger this event from the closest parent CSS selector
|
||||
let FromClosest = sprintf "closest %s" >> From
|
||||
|
||||
/// <summary>Trigger this event from the closest child CSS selector</summary>
|
||||
/// <param name="selector">The child CSS selector from which the action should be fired</param>
|
||||
/// <param name="action">The action to be fired</param>
|
||||
/// <returns>A trigger spec to fire from the closest child element</returns>
|
||||
let FromFind selector action =
|
||||
From $"find %s{selector}" action
|
||||
/// Trigger this event from the closest child CSS selector
|
||||
let FromFind = sprintf "find %s" >> From
|
||||
|
||||
/// <summary>Target the given CSS selector with the results of this event</summary>
|
||||
/// <param name="selector">The CSS selector to which the result of the action will be applied</param>
|
||||
/// <param name="action">The action to be fired</param>
|
||||
/// <returns>A trigger spec to target the given selector</returns>
|
||||
let Target selector action =
|
||||
appendModifier $"target:%s{selector}" action
|
||||
/// Target the given CSS selector with the results of this event
|
||||
let Target = sprintf "target:%s" >> appendModifier
|
||||
|
||||
/// <summary>Prevent any further events from occurring after this one fires</summary>
|
||||
/// <param name="action">The action to be fired</param>
|
||||
/// <returns>A trigger spec to fire the given action and prevent further events</returns>
|
||||
let Consume action =
|
||||
appendModifier "consume" action
|
||||
/// Prevent any further events from occurring after this one fires
|
||||
let Consume = appendModifier "consume"
|
||||
|
||||
/// <summary>
|
||||
/// Configure queueing when events fire when others are in flight; if unspecified, the default is <c>last</c>
|
||||
/// </summary>
|
||||
/// <param name="how">
|
||||
/// How the request should be queued (consider <see cref="QueueFirst" />, <see cref="QueueLast" />,
|
||||
/// <see cref="QueueAll" />, and <see cref="QueueNone" />)
|
||||
/// </param>
|
||||
/// <param name="action">The action to be fired</param>
|
||||
/// <returns>A trigger spec to queue the given action</returns>
|
||||
let Queue how action =
|
||||
let qSpec = if how = "" then "" else $":{how}"
|
||||
appendModifier $"queue{qSpec}" action
|
||||
/// Configure queueing when events fire when others are in flight; if unspecified, the default is "last"
|
||||
let Queue = sprintf "queue:%s" >> appendModifier
|
||||
|
||||
/// <summary>Queue the first event, discard all others (i.e., a FIFO queue of 1)</summary>
|
||||
/// <param name="action">The action to be fired</param>
|
||||
/// <returns>A trigger spec to queue the given action</returns>
|
||||
let QueueFirst action =
|
||||
Queue "first" action
|
||||
/// Queue the first event, discard all others (i.e., a FIFO queue of 1)
|
||||
let QueueFirst = Queue "first"
|
||||
|
||||
/// <summary>Queue the last event; discards current when another is received (i.e., a LIFO queue of 1)</summary>
|
||||
/// <param name="action">The action to be fired</param>
|
||||
/// <returns>A trigger spec to queue the given action</returns>
|
||||
let QueueLast action =
|
||||
Queue "last" action
|
||||
/// Queue the last event; discards current when another is received (i.e., a LIFO queue of 1)
|
||||
let QueueLast = Queue "last"
|
||||
|
||||
/// <summary>Queue all events; discard none</summary>
|
||||
/// <param name="action">The action to be fired</param>
|
||||
/// <returns>A trigger spec to queue the given action</returns>
|
||||
let QueueAll action =
|
||||
Queue "all" action
|
||||
/// Queue all events; discard none
|
||||
let QueueAll = Queue "all"
|
||||
|
||||
/// <summary>Queue no events; discard all</summary>
|
||||
/// <param name="action">The action to be fired</param>
|
||||
/// <returns>A trigger spec to queue the given action</returns>
|
||||
let QueueNone action =
|
||||
Queue "none" action
|
||||
/// Queue no events; discard all
|
||||
let QueueNone = Queue "none"
|
||||
|
||||
|
||||
/// <summary>Helper to create the <c>hx-vals</c> attribute</summary>
|
||||
/// <seealso href="https://htmx.org/attributes/hx-vals/">Documentation</seealso>
|
||||
/// <summary>Helper to create the `hx-vals` attribute</summary>
|
||||
[<RequireQualifiedAccess>]
|
||||
module HxVals =
|
||||
|
||||
@@ -487,336 +374,168 @@ module HxVals =
|
||||
let From = Giraffe.Htmx.Common.toJson
|
||||
|
||||
|
||||
open Giraffe.Htmx
|
||||
|
||||
/// <summary>Attributes and flags for htmx</summary>
|
||||
[<AutoOpen>]
|
||||
module HtmxAttrs =
|
||||
|
||||
/// <summary>Progressively enhances anchors and forms to use AJAX requests</summary>
|
||||
/// <remarks>Use <c>_hxNoBoost</c> to set to false</remarks>
|
||||
/// <seealso href="https://htmx.org/attributes/hx-boost/">Documentation</seealso>
|
||||
/// <remarks>Use <tt>_hxNoBoost</tt> to set to false</remarks>
|
||||
let _hxBoost = attr "hx-boost" "true"
|
||||
|
||||
/// <summary>Shows a <c>confirm()</c> dialog before issuing a request</summary>
|
||||
/// <summary>Shows a <tt>confirm()</tt> dialog before issuing a request</summary>
|
||||
/// <param name="prompt">The prompt to present to the user when seeking their confirmation</param>
|
||||
/// <returns>A configured <c>hx-confirm</c> attribute</returns>
|
||||
/// <seealso href="https://htmx.org/attributes/hx-confirm/">Documentation</seealso>
|
||||
/// <returns>A configured <tt>hx-confirm</tt> attribute</returns>
|
||||
let _hxConfirm prompt =
|
||||
attr "hx-confirm" prompt
|
||||
|
||||
/// <summary>Issues a <c>DELETE</c> to the specified URL</summary>
|
||||
/// <param name="url">The URL to which the <c>DELETE</c> request should be sent</param>
|
||||
/// <returns>A configured <c>hx-delete</c> attribute</returns>
|
||||
/// <seealso href="https://htmx.org/attributes/hx-delete/">Documentation</seealso>
|
||||
/// <summary>Issues a <tt>DELETE</tt> to the specified URL</summary>
|
||||
/// <param name="url">The URL to which the <tt>DELETE</tt> request should be sent</param>
|
||||
/// <returns>A configured <tt>hx-delete</tt> attribute</returns>
|
||||
let _hxDelete url =
|
||||
attr "hx-delete" url
|
||||
|
||||
/// <summary>Disables htmx processing for the given node and any children nodes</summary>
|
||||
/// <seealso href="https://htmx.org/attributes/hx-disable/">Documentation</seealso>
|
||||
let _hxDisable = flag "hx-disable"
|
||||
|
||||
/// <summary>Specifies elements that should be disabled when an htmx request is in flight</summary>
|
||||
/// <param name="elt">The element to disable when an htmx request is in flight</param>
|
||||
/// <returns>A configured <c>hx-disabled-elt</c> attribute</returns>
|
||||
/// <seealso href="https://htmx.org/attributes/hx-disabled-elt/">Documentation</seealso>
|
||||
/// <returns>A configured <tt>hx-disabled-elt</tt> attribute</returns>
|
||||
let _hxDisabledElt elt =
|
||||
attr "hx-disabled-elt" elt
|
||||
|
||||
/// <summary>Disinherit all ("*") or specific htmx attributes</summary>
|
||||
/// <param name="hxAttrs">The htmx attributes to disinherit (should start with "hx-")</param>
|
||||
/// <returns>A configured <c>hx-disinherit</c> attribute</returns>
|
||||
/// <seealso href="https://htmx.org/attributes/hx-disinherit/">Documentation</seealso>
|
||||
/// <returns>A configured <tt>hx-disinherit</tt> attribute</returns>
|
||||
let _hxDisinherit hxAttrs =
|
||||
attr "hx-disinherit" hxAttrs
|
||||
|
||||
/// <summary>Changes the request encoding type</summary>
|
||||
/// <param name="enc">The encoding type (use <c>HxEncoding</c> constants)</param>
|
||||
/// <returns>A configured <c>hx-encoding</c> attribute</returns>
|
||||
/// <seealso cref="HxEncoding" />
|
||||
/// <seealso href="https://htmx.org/attributes/hx-encoding/">Documentation</seealso>
|
||||
/// <param name="enc">The encoding type (use <tt>HxEncoding</tt> constants)</param>
|
||||
/// <returns>A configured <tt>hx-encoding</tt> attribute</returns>
|
||||
let _hxEncoding enc =
|
||||
attr "hx-encoding" enc
|
||||
|
||||
/// <summary>Extensions to use for this element</summary>
|
||||
/// <param name="exts">A list of extensions to apply to this element</param>
|
||||
/// <returns>A configured <c>hx-ext</c> attribute</returns>
|
||||
/// <seealso href="https://htmx.org/attributes/hx-ext/">Documentation</seealso>
|
||||
/// <returns>A configured <tt>hx-ext</tt> attribute</returns>
|
||||
let _hxExt exts =
|
||||
attr "hx-ext" exts
|
||||
|
||||
/// <summary>Issues a <c>GET</c> to the specified URL</summary>
|
||||
/// <param name="url">The URL to which the <c>GET</c> request should be sent</param>
|
||||
/// <returns>A configured <c>hx-get</c> attribute</returns>
|
||||
/// <seealso href="https://htmx.org/attributes/hx-get/">Documentation</seealso>
|
||||
/// <summary>Issues a <tt>GET</tt> to the specified URL</summary>
|
||||
/// <param name="url">The URL to which the <tt>GET</tt> request should be sent</param>
|
||||
/// <returns>A configured <tt>hx-get</tt> attribute</returns>
|
||||
let _hxGet url =
|
||||
attr "hx-get" url
|
||||
|
||||
/// <summary>Adds to the headers that will be submitted with the request</summary>
|
||||
/// <param name="hdrs">The headers to include with the request</param>
|
||||
/// <returns>A configured <c>hx-headers</c> attribute</returns>
|
||||
/// <seealso href="https://htmx.org/attributes/hx-headers/">Documentation</seealso>
|
||||
let _hxHeaders hdrs =
|
||||
attr "hx-headers" hdrs
|
||||
[<System.Obsolete "Convert this parameter">]
|
||||
let _hxHeaders = attr "hx-headers"
|
||||
|
||||
/// <summary>
|
||||
/// Set to "false" to prevent pages with sensitive information from being stored in the history cache
|
||||
/// </summary>
|
||||
/// <param name="shouldStore">Whether the page should be stored in the history cache</param>
|
||||
/// <returns>A configured <c>hx-history</c> attribute</returns>
|
||||
/// <seealso href="https://htmx.org/attributes/hx-history/">Documentation</seealso>
|
||||
let _hxHistory shouldStore =
|
||||
attr "hx-history" (toLowerBool shouldStore)
|
||||
let _hxHistory = attr "hx-history"
|
||||
|
||||
/// <summary>The element to snapshot and restore during history navigation</summary>
|
||||
/// <seealso href="https://htmx.org/attributes/hx-history-elt/">Documentation</seealso>
|
||||
let _hxHistoryElt =
|
||||
flag "hx-history-elt"
|
||||
/// The element to snapshot and restore during history navigation
|
||||
let _hxHistoryElt = flag "hx-history-elt"
|
||||
|
||||
/// <summary>Includes additional data in AJAX requests</summary>
|
||||
/// <param name="spec">The specification of what should be included in the request</param>
|
||||
/// <returns>A configured <c>hx-include</c> attribute</returns>
|
||||
/// <seealso href="https://htmx.org/attributes/hx-include/">Documentation</seealso>
|
||||
let _hxInclude spec =
|
||||
attr "hx-include" spec
|
||||
/// Includes additional data in AJAX requests
|
||||
let _hxInclude = attr "hx-include"
|
||||
|
||||
/// <summary>The element to put the <c>htmx-request</c> class on during the AJAX request</summary>
|
||||
/// <param name="selector">The selector for the indicator element</param>
|
||||
/// <returns>A configured <c>hx-indicator</c> attribute</returns>
|
||||
/// <seealso href="https://htmx.org/attributes/hx-indicator/">Documentation</seealso>
|
||||
let _hxIndicator selector =
|
||||
attr "hx-indicator" selector
|
||||
/// The element to put the htmx-request class on during the AJAX request
|
||||
let _hxIndicator = attr "hx-indicator"
|
||||
|
||||
/// <summary>Overrides a previous <c>hx-boost</c> (hx-boost="false")</summary>
|
||||
/// <seealso href="https://htmx.org/attributes/hx-boost/">Documentation</seealso>
|
||||
let _hxNoBoost =
|
||||
attr "hx-boost" "false"
|
||||
/// Overrides a previous `hx-boost`
|
||||
let _hxNoBoost = attr "hx-boost" "false"
|
||||
|
||||
/// <summary>Attach an event handler for DOM events</summary>
|
||||
/// <param name="evtName">The name of the event</param>
|
||||
/// <param name="handler">The script to be executed when the event occurs</param>
|
||||
/// <returns>A configured <c>hx-on</c> attribute</returns>
|
||||
/// <seealso href="https://htmx.org/attributes/hx-on/">Documentation</seealso>
|
||||
let _hxOnEvent evtName handler =
|
||||
attr $"hx-on:%s{evtName}" handler
|
||||
/// Attach an event handler for DOM events
|
||||
let _hxOnEvent evtName =
|
||||
attr $"hx-on:%s{evtName}"
|
||||
|
||||
/// <summary>Attach an event handler for htmx events</summary>
|
||||
/// <param name="hxEvent">The <c>HxEvent</c> to be handled</param>
|
||||
/// <param name="handler">The script to be executed when the event occurs</param>
|
||||
/// <returns>A configured <c>hx-on::</c> attribute</returns>
|
||||
/// <seealso cref="HxEvent" />
|
||||
/// <seealso href="https://htmx.org/attributes/hx-on/">Documentation</seealso>
|
||||
let _hxOnHxEvent (hxEvent: HxEvent) handler =
|
||||
_hxOnEvent $":{hxEvent.ToHxOnString()}" handler
|
||||
/// Attach an event handler for htmx events
|
||||
let _hxOnHxEvent (hxEvent: HxEvent) =
|
||||
_hxOnEvent $":{hxEvent.ToHxOnString()}"
|
||||
|
||||
/// <summary>Filters the parameters that will be submitted with a request</summary>
|
||||
/// <param name="toInclude">The fields to include (use <c>HxParams</c> to generate this value)</param>
|
||||
/// <returns>A configured <c>hx-params</c> attribute</returns>
|
||||
/// <seealso cref="HxParams" />
|
||||
/// <seealso href="https://htmx.org/attributes/hx-params/">Documentation</seealso>
|
||||
let _hxParams toInclude =
|
||||
attr "hx-params" toInclude
|
||||
/// Filters the parameters that will be submitted with a request
|
||||
let _hxParams = attr "hx-params"
|
||||
|
||||
/// <summary>Issues a <c>PATCH</c> to the specified URL</summary>
|
||||
/// <param name="url">The URL to which the request should be directed</param>
|
||||
/// <returns>A configured <c>hx-patch</c> attribute</returns>
|
||||
/// <seealso href="https://htmx.org/attributes/hx-patch/">Documentation</seealso>
|
||||
let _hxPatch url =
|
||||
attr "hx-patch" url
|
||||
/// Issues a PATCH to the specified URL
|
||||
let _hxPatch = attr "hx-patch"
|
||||
|
||||
/// <summary>Issues a <c>POST</c> to the specified URL</summary>
|
||||
/// <param name="url">The URL to which the request should be directed</param>
|
||||
/// <returns>A configured <c>hx-post</c> attribute</returns>
|
||||
/// <seealso href="https://htmx.org/attributes/hx-post/">Documentation</seealso>
|
||||
let _hxPost url =
|
||||
attr "hx-post" url
|
||||
/// Issues a POST to the specified URL
|
||||
let _hxPost = attr "hx-post"
|
||||
|
||||
/// <summary>Preserves an element between requests</summary>
|
||||
/// <seealso href="https://htmx.org/attributes/hx-preserve/">Documentation</seealso>
|
||||
let _hxPreserve =
|
||||
flag "hx-preserve"
|
||||
/// Preserves an element between requests
|
||||
let _hxPreserve = attr "hx-preserve" "true"
|
||||
|
||||
/// <summary>Shows a <c>prompt()</c> dialog before submitting a request</summary>
|
||||
/// <param name="text">The text for the prompt</param>
|
||||
/// <returns>A configured <c>hx-prompt</c> attribute</returns>
|
||||
/// <remarks>The value provided will be in the <c>HX-Prompt</c> request header</remarks>
|
||||
/// <seealso href="https://htmx.org/attributes/hx-prompt/">Documentation</seealso>
|
||||
let _hxPrompt text =
|
||||
attr "hx-prompt" text
|
||||
/// Shows a prompt before submitting a request
|
||||
let _hxPrompt = attr "hx-prompt"
|
||||
|
||||
/// <summary>Pushes the URL into the location bar, creating a new history entry</summary>
|
||||
/// <param name="spec">
|
||||
/// <ul>
|
||||
/// <li>"true" to push the fetched URL</li>
|
||||
/// <li>"false" to explicitly not push the fetched URL</li>
|
||||
/// <li>A specific URL to push</li>
|
||||
/// </ul>
|
||||
/// </param>
|
||||
/// <returns>A configured <c>hx-push-url</c> attribute</returns>
|
||||
/// <seealso href="https://htmx.org/attributes/hx-push-url/">Documentation</seealso>
|
||||
let _hxPushUrl spec =
|
||||
attr "hx-push-url" spec
|
||||
/// Pushes the URL into the location bar, creating a new history entry
|
||||
let _hxPushUrl = attr "hx-push-url"
|
||||
|
||||
/// <summary>Issues a <c>PUT</c> to the specified URL</summary>
|
||||
/// <param name="url">The URL to which the request should be directed</param>
|
||||
/// <returns>A configured <c>hx-put</c> attribute</returns>
|
||||
/// <seealso href="https://htmx.org/attributes/hx-put/">Documentation</seealso>
|
||||
let _hxPut url =
|
||||
attr "hx-put" url
|
||||
/// Issues a PUT to the specified URL
|
||||
let _hxPut = attr "hx-put"
|
||||
|
||||
/// <summary>Replaces the current URL in the browser's history stack</summary>
|
||||
/// <param name="spec">
|
||||
/// <ul>
|
||||
/// <li>"true" to replace the current URL with the fetched one</li>
|
||||
/// <li>"false" to explicitly replace nothing</li>
|
||||
/// <li>A specific URL to replace in the browser's history</li>
|
||||
/// </ul>
|
||||
/// </param>
|
||||
/// <returns>A configured <c>hx-replace-url</c> attribute</returns>
|
||||
/// <seealso href="https://htmx.org/attributes/hx-replace-url/">Documentation</seealso>
|
||||
let _hxReplaceUrl spec =
|
||||
attr "hx-replace-url" spec
|
||||
/// Replaces the current URL in the browser's history stack
|
||||
let _hxReplaceUrl = attr "hx-replace-url"
|
||||
|
||||
/// <summary>Configures various aspects of the request</summary>
|
||||
/// <param name="spec">The configuration spec (use <c>HxRequest.Configure</c> to create value)</param>
|
||||
/// <returns>A configured <c>hx-request</c> attribute</returns>
|
||||
/// <seealso cref="HxRequest.Configure" />
|
||||
/// <seealso href="https://htmx.org/attributes/hx-request/">Documentation</seealso>
|
||||
let _hxRequest spec =
|
||||
attr "hx-request" spec
|
||||
/// Configures various aspects of the request
|
||||
let _hxRequest = attr "hx-request"
|
||||
|
||||
/// <summary>Selects a subset of the server response to process</summary>
|
||||
/// <param name="selector">A CSS selector for the content to be selected</param>
|
||||
/// <returns>A configured <c>hx-select</c> attribute</returns>
|
||||
/// <seealso href="https://htmx.org/attributes/hx-select/">Documentation</seealso>
|
||||
let _hxSelect selector =
|
||||
attr "hx-select" selector
|
||||
/// Selects a subset of the server response to process
|
||||
let _hxSelect = attr "hx-select"
|
||||
|
||||
/// <summary>Selects a subset of an out-of-band server response</summary>
|
||||
/// <param name="selectors">One or more comma-delimited CSS selectors for the content to be selected</param>
|
||||
/// <returns>A configured <c>hx-select-oob</c> attribute</returns>
|
||||
/// <seealso href="https://htmx.org/attributes/hx-select-oob/">Documentation</seealso>
|
||||
let _hxSelectOob selectors =
|
||||
attr "hx-select-oob" selectors
|
||||
/// Selects a subset of an out-of-band server response
|
||||
let _hxSelectOob = attr "hx-select-oob"
|
||||
|
||||
/// <summary>
|
||||
/// Controls how the response content is swapped into the DOM (e.g. <c>outerHTML</c> or <c>beforeEnd</c>)
|
||||
/// </summary>
|
||||
/// <param name="swap">The type of swap to perform (use <c>HxSwap</c> values)</param>
|
||||
/// <returns>A configured <c>hx-swap</c> attribute</returns>
|
||||
/// <seealso href="https://htmx.org/attributes/hx-swap/">Documentation</seealso>
|
||||
let _hxSwap swap =
|
||||
attr "hx-swap" swap
|
||||
/// Controls how the response content is swapped into the DOM (e.g. 'outerHTML' or 'beforeEnd')
|
||||
let _hxSwap = attr "hx-swap"
|
||||
|
||||
/// <summary>
|
||||
/// Controls how the response content is swapped into the DOM (e.g. <c>outerHTML</c> or <c>beforeEnd</c>), enabling
|
||||
/// CSS transitions
|
||||
/// </summary>
|
||||
/// <param name="swap">The type of swap to perform (use <c>HxSwap</c> values)</param>
|
||||
/// <returns>A configured <c>hx-swap</c> attribute</returns>
|
||||
/// <seealso href="https://htmx.org/attributes/hx-swap/">Documentation</seealso>
|
||||
let _hxSwapWithTransition swap =
|
||||
_hxSwap $"%s{swap} transition:true"
|
||||
/// Controls how the response content is swapped into the DOM (e.g. 'outerHTML' or 'beforeEnd'), enabling CSS
|
||||
/// transitions
|
||||
let _hxSwapWithTransition = sprintf "%s transition:true" >> _hxSwap
|
||||
|
||||
/// <summary>
|
||||
/// Marks content in a response as being "Out of Band", i.e. swapped somewhere other than the target
|
||||
/// </summary>
|
||||
/// <param name="swap">
|
||||
/// <ul>
|
||||
/// <li>"true" to mark this as an OOB swap</li>
|
||||
/// <li>Any <c>HxSwap</c> value</li>
|
||||
/// <li>Any <c>HxSwap</c> value, followed by a colon (<c>:</c>) and a CSS selector</li>
|
||||
/// </ul>
|
||||
/// </param>
|
||||
/// <returns>A configured <c>hx-swap-oob</c> attribute</returns>
|
||||
/// <seealso href="https://htmx.org/attributes/hx-swap-oob/">Documentation</seealso>
|
||||
let _hxSwapOob swap =
|
||||
attr "hx-swap-oob" swap
|
||||
let _hxSwapOob = attr "hx-swap-oob"
|
||||
|
||||
/// <summary>Synchronize events based on another element</summary>
|
||||
/// <param name="selector">A CSS selector for the element with which this one should sync</param>
|
||||
/// <param name="action">The request synchronization action to perform (use <c>HxSync</c> values)</param>
|
||||
/// <returns>A configured <c>hx-sync</c> attribute</returns>
|
||||
/// <seealso cref="HxSync" />
|
||||
/// <seealso href="https://htmx.org/attributes/hx-sync/">Documentation</seealso>
|
||||
let _hxSync selector action =
|
||||
attr "hx-sync" $"%s{selector}:%s{action}"
|
||||
/// Synchronize events based on another element
|
||||
let _hxSync = attr "hx-sync"
|
||||
|
||||
/// <summary>Specifies the target element to be swapped</summary>
|
||||
/// <param name="selector">A CSS selector or relative reference (or both) to identify the target</param>
|
||||
/// <returns>A configured <c>hx-target</c> attribute</returns>
|
||||
/// <seealso href="https://htmx.org/attributes/hx-target/">Documentation</seealso>
|
||||
let _hxTarget selector =
|
||||
attr "hx-target" selector
|
||||
/// Specifies the target element to be swapped
|
||||
let _hxTarget = attr "hx-target"
|
||||
|
||||
/// <summary>Specifies the event that triggers the request</summary>
|
||||
/// <param name="spec">The trigger specification (use <c>HxTrigger</c> to create)</param>
|
||||
/// <returns>A configured <c>hx-trigger</c> attribute</returns>
|
||||
/// <seealso cref="HxTrigger" />
|
||||
/// <seealso href="https://htmx.org/attributes/hx-trigger/">Documentation</seealso>
|
||||
let _hxTrigger spec =
|
||||
attr "hx-trigger" spec
|
||||
/// Specifies the event that triggers the request
|
||||
let _hxTrigger = attr "hx-trigger"
|
||||
|
||||
/// <summary>Validate an input element (uses HTML5 validation API)</summary>
|
||||
/// <seealso href="https://htmx.org/attributes/hx-validate/">Documentation</seealso>
|
||||
let _hxValidate =
|
||||
flag "hx-validate"
|
||||
/// Validate an input element (uses HTML5 validation API)
|
||||
let _hxValidate = flag "hx-validate"
|
||||
|
||||
/// <summary>Adds to the parameters that will be submitted with the request</summary>
|
||||
/// <param name="values">The values for the parameters (use <c>HxVals.From</c> to create)</param>
|
||||
/// <returns>A configured <c>hx-vals</c> attribute</returns>
|
||||
/// <seealso cref="HxVals.From" />
|
||||
/// <seealso href="https://htmx.org/attributes/hx-vals/">Documentation</seealso>
|
||||
let _hxVals values =
|
||||
attr "hx-vals" values
|
||||
/// Adds to the parameters that will be submitted with the request
|
||||
let _hxVals = attr "hx-vals"
|
||||
|
||||
/// <summary>The URL of the SSE server</summary>
|
||||
/// <param name="url">The URL from which events will be received</param>
|
||||
/// <returns>A configured <c>sse-connect</c> attribute</returns>
|
||||
/// <seealso href="https://htmx.org/extensions/sse/#usage">Extension Docs</seealso>
|
||||
let _sseConnect url =
|
||||
attr "sse-connect" url
|
||||
/// The name of the message to swap into the DOM.
|
||||
let _sseSwap = attr "sse-swap"
|
||||
|
||||
/// <summary>The name(s) of the message(s) to swap into the DOM</summary>
|
||||
/// <param name="messages">The message names (comma-delimited) to swap (use "message" for unnamed events)</param>
|
||||
/// <returns>A configured <c>sse-swap</c> attribute</returns>
|
||||
/// <seealso href="https://htmx.org/extensions/sse/#usage">Extension Docs</seealso>
|
||||
let _sseSwap messages =
|
||||
attr "sse-swap" messages
|
||||
/// The URL of the SSE server.
|
||||
let _sseConnect = attr "sse-connect"
|
||||
|
||||
|
||||
/// <summary>Script tags to pull htmx into a web page</summary>
|
||||
/// Script tags to pull htmx into a web page
|
||||
module Script =
|
||||
|
||||
open System
|
||||
|
||||
/// <summary>Script tag to load the package-provided version of htmx</summary>
|
||||
let local = script [ _src htmxLocalScript ] []
|
||||
|
||||
/// <summary>Script tag to load the minified version from jsdelivr.net</summary>
|
||||
/// <remarks>Ensure <c>cdn.jsdelivr.net</c> is in your CSP <c>script-src</c> list (if applicable)</remarks>
|
||||
let cdnMinified =
|
||||
script [ _src $"https://cdn.jsdelivr.net/npm/htmx.org@{HtmxVersion}/dist/htmx.min.js"
|
||||
_integrity "sha384-H5SrcfygHmAuTDZphMHqBJLc3FhssKjG7w/CeCpFReSfwBWDTKpkzPP8c+cLsK+V"
|
||||
/// Script tag to load the minified version from unpkg.com
|
||||
let minified =
|
||||
script [ _src "https://unpkg.com/htmx.org@2.0.4"
|
||||
_integrity "sha384-HGfztofotfshcF7+8n44JQL2oJmowVChPTg48S+jvZoztPfvwD79OC/LTtG6dMp+"
|
||||
_crossorigin "anonymous" ] []
|
||||
|
||||
/// <summary>Script tag to load the unminified version from jsdelivr.net</summary>
|
||||
/// <remarks>Ensure <c>cdn.jsdelivr.net</c> is in your CSP <c>script-src</c> list (if applicable)</remarks>
|
||||
let cdnUnminified =
|
||||
script [ _src $"https://cdn.jsdelivr.net/npm/htmx.org@{HtmxVersion}/dist/htmx.js"
|
||||
_integrity "sha384-Q+Dky3iHVJOr6wUjQ4ulh6uQ76an/t+ak1+PjMVaxRjbZamFLAG+u9InkfjbsEQf"
|
||||
/// Script tag to load the unminified version from unpkg.com
|
||||
let unminified =
|
||||
script [ _src "https://unpkg.com/htmx.org@2.0.4/dist/htmx.js"
|
||||
_integrity "sha384-oeUn82QNXPuVkGCkcrInrS1twIxKhkZiFfr2TdiuObZ3n3yIeMiqcRzkIcguaof1"
|
||||
_crossorigin "anonymous" ] []
|
||||
|
||||
/// <summary>Script tag to load the minified version from jsdelivr.net</summary>
|
||||
[<Obsolete "Use cdnMinified instead; this will be removed in v4">]
|
||||
let minified = cdnMinified
|
||||
|
||||
/// <summary>Script tag to load the unminified version from jsdelivr.net</summary>
|
||||
[<Obsolete "Use cdnUnminified instead; this will be removed in v4">]
|
||||
let unminified = cdnUnminified
|
||||
|
||||
|
||||
/// <summary>Functions to extract and render an HTML fragment from a document</summary>
|
||||
/// Functions to extract and render an HTML fragment from a document
|
||||
[<RequireQualifiedAccess>]
|
||||
module RenderFragment =
|
||||
|
||||
@@ -832,80 +551,59 @@ module RenderFragment =
|
||||
let private nodeNotFound (nodeId : string) =
|
||||
$"<em>– ID {nodeId} not found –</em>"
|
||||
|
||||
/// <summary>Find the node with the named ID</summary>
|
||||
/// <param name="nodeId">The <c>id</c> attribute to find</param>
|
||||
/// <param name="node">The node tree to search</param>
|
||||
/// <returns>The node with the requested <c>id</c> attribute, or <c>None</c> if it was not found</returns>
|
||||
/// Find the node with the named ID
|
||||
let rec findIdNode nodeId (node : XmlNode) : XmlNode option =
|
||||
match node with
|
||||
| Text _ -> None
|
||||
| VoidElement elt -> if isIdElement nodeId elt then Some node else None
|
||||
| ParentNode (elt, children) ->
|
||||
if isIdElement nodeId elt then Some node else children |> List.tryPick (findIdNode nodeId)
|
||||
if isIdElement nodeId elt then Some node else children |> List.tryPick (fun c -> findIdNode nodeId c)
|
||||
|
||||
/// <summary>Functions to render a fragment as a string</summary>
|
||||
/// Functions to render a fragment as a string
|
||||
[<RequireQualifiedAccess>]
|
||||
module AsString =
|
||||
|
||||
/// <summary>Render to HTML for the given ID</summary>
|
||||
/// <param name="nodeId">The <c>id</c> attribute for the node to be rendered</param>
|
||||
/// <param name="nodes">The node trees to search</param>
|
||||
/// <returns>The HTML for the given <c>id</c> node, or an error message if it was not found</returns>
|
||||
/// Render to HTML for the given ID
|
||||
let htmlFromNodes nodeId (nodes : XmlNode list) =
|
||||
match nodes |> List.tryPick (findIdNode nodeId) with
|
||||
match nodes |> List.tryPick(fun node -> findIdNode nodeId node) with
|
||||
| Some idNode -> RenderView.AsString.htmlNode idNode
|
||||
| None -> nodeNotFound nodeId
|
||||
|
||||
/// <summary>Render to HTML for the given ID</summary>
|
||||
/// <param name="nodeId">The <c>id</c> attribute for the node to be rendered</param>
|
||||
/// <param name="node">The node tree to search</param>
|
||||
/// <returns>The HTML for the given <c>id</c> node, or an error message if it was not found</returns>
|
||||
/// Render to HTML for the given ID
|
||||
let htmlFromNode nodeId node =
|
||||
match findIdNode nodeId node with
|
||||
| Some idNode -> RenderView.AsString.htmlNode idNode
|
||||
| None -> nodeNotFound nodeId
|
||||
|
||||
/// <summary>Functions to render a fragment as bytes</summary>
|
||||
/// Functions to render a fragment as bytes
|
||||
[<RequireQualifiedAccess>]
|
||||
module AsBytes =
|
||||
|
||||
let private utf8 = System.Text.Encoding.UTF8
|
||||
|
||||
/// <summary>Render to bytes for the given ID</summary>
|
||||
/// <param name="nodeId">The <c>id</c> attribute for the node to be rendered</param>
|
||||
/// <param name="nodes">The node trees to search</param>
|
||||
/// <returns>The bytes for the given <c>id</c> node, or an error message if it was not found</returns>
|
||||
/// Render to HTML for the given ID
|
||||
let htmlFromNodes nodeId (nodes : XmlNode list) =
|
||||
match nodes |> List.tryPick (findIdNode nodeId) with
|
||||
match nodes |> List.tryPick(fun node -> findIdNode nodeId node) with
|
||||
| Some idNode -> RenderView.AsBytes.htmlNode idNode
|
||||
| None -> nodeNotFound nodeId |> utf8.GetBytes
|
||||
|
||||
/// <summary>Render to bytes for the given ID</summary>
|
||||
/// <param name="nodeId">The <c>id</c> attribute for the node to be rendered</param>
|
||||
/// <param name="node">The node tree to search</param>
|
||||
/// <returns>The bytes for the given <c>id</c> node, or an error message if it was not found</returns>
|
||||
/// Render to HTML for the given ID
|
||||
let htmlFromNode nodeId node =
|
||||
match findIdNode nodeId node with
|
||||
| Some idNode -> RenderView.AsBytes.htmlNode idNode
|
||||
| None -> nodeNotFound nodeId |> utf8.GetBytes
|
||||
|
||||
/// <summary>Functions to render a fragment into a StringBuilder</summary>
|
||||
/// Functions to render a fragment into a StringBuilder
|
||||
[<RequireQualifiedAccess>]
|
||||
module IntoStringBuilder =
|
||||
|
||||
/// <summary>Render HTML into a <c>StringBuilder</c> for the given ID</summary>
|
||||
/// <param name="sb">The <c>StringBuilder</c> into which the bytes will be rendered</param>
|
||||
/// <param name="nodeId">The <c>id</c> attribute for the node to be rendered</param>
|
||||
/// <param name="nodes">The node trees to search</param>
|
||||
/// Render to HTML for the given ID
|
||||
let htmlFromNodes sb nodeId (nodes : XmlNode list) =
|
||||
match nodes |> List.tryPick (findIdNode nodeId) with
|
||||
match nodes |> List.tryPick(fun node -> findIdNode nodeId node) with
|
||||
| Some idNode -> RenderView.IntoStringBuilder.htmlNode sb idNode
|
||||
| None -> nodeNotFound nodeId |> sb.Append |> ignore
|
||||
|
||||
/// <summary>Render HTML into a <c>StringBuilder</c> for the given ID</summary>
|
||||
/// <param name="sb">The <c>StringBuilder</c> into which the bytes will be rendered</param>
|
||||
/// <param name="nodeId">The <c>id</c> attribute for the node to be rendered</param>
|
||||
/// <param name="node">The node tree to search</param>
|
||||
/// Render to HTML for the given ID
|
||||
let htmlFromNode sb nodeId node =
|
||||
match findIdNode nodeId node with
|
||||
| Some idNode -> RenderView.IntoStringBuilder.htmlNode sb idNode
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
This package enables [htmx](https://htmx.org) support within the [Giraffe](https://giraffe.wiki) view engine.
|
||||
|
||||
**htmx version: 2.0.10**
|
||||
**htmx version: 2.0.4**
|
||||
|
||||
_Upgrading from v1.x: see [the migration guide](https://htmx.org/migration-guide-htmx-1/) for changes_
|
||||
|
||||
@@ -29,7 +29,7 @@ Support modules include:
|
||||
- `HxTrigger`
|
||||
- `HxVals`
|
||||
|
||||
`Htmx.Script.local` creates an `XmlNode` to load the package-provided htmx library. There are also two `XmlNode`s that will load the htmx script from jsdelivr; `Htmx.Script.cdnMinified` loads the minified version, and `Htmx.Script.cdnUnminified` loads the unminified version (useful for debugging). When using the CDN nodes and a Content Security Policy (CSP) header, `cdn.jsdelivr.net` needs to be listed as an allowable `script-src`.
|
||||
There are two `XmlNode`s that will load the htmx script from unpkg; `Htmx.Script.minified` loads the minified version, and `Htmx.Script.unminified` loads the unminified version (useful for debugging).
|
||||
|
||||
This also supports [fragment rendering](https://bitbadger.solutions/blog/2022/fragment-rendering-in-giraffe-view-engine.html), providing the flexibility to render an entire template, or only a portion of it (based on the element's `id` attribute).
|
||||
|
||||
|
||||
Reference in New Issue
Block a user