-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add response-targets and event-header
- Loading branch information
Showing
6 changed files
with
166 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"go.lintTool": "golangci-lint", | ||
"go.lintFlags": ["--fast"], | ||
"cSpell.words": ["classtools", "gomponents"] | ||
"cSpell.words": ["classtools", "eventheader", "templ", "gomponents"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// package eventheader the Triggering-Event header to requests. The value of the header is a JSON serialized version of the event that triggered the request. | ||
// | ||
// # Install | ||
// | ||
// <script src="https://unpkg.com/[email protected]/dist/ext/event-header.js"></script> | ||
// | ||
// # Usage | ||
// | ||
// <button { hx.Ext(eventheader.Extension)... } > | ||
// Click Me! | ||
// </button> | ||
// | ||
// Sends something like this: | ||
// | ||
// Triggering-Event: '{ "isTrusted": false, "htmx-internal-data": { "handled": true }, "screenX": 0, "screenY": 0, "clientX": 0, "clientY": 0, "ctrlKey": false, "shiftKey": false, "altKey": false, "metaKey": false, "button": 0, "buttons": 0, "relatedTarget": null, "pageX": 0, "pageY": 0, "x": 0, "y": 0, "offsetX": 0, "offsetY": 0, "movementX": 0, "movementY": 0, "fromElement": null, "toElement": "button", "layerX": 0, "layerY": 0, "view": "Window", "detail": 0, "sourceCapabilities": null, "which": 1, "NONE": 0, "CAPTURING_PHASE": 1, "AT_TARGET": 2, "BUBBLING_PHASE": 3, "type": "click", "target": "button", "currentTarget": "button", "eventPhase": 2, "bubbles": true, "cancelable": true, "defaultPrevented": true, "composed": true, "timeStamp": 188.86999995447695, "srcElement": "button", "returnValue": false, "cancelBubble": false, "path": [ "button", "div#work-area", "body", "html", "Node", "Window" ] }' | ||
// | ||
// Extension: [event-header] | ||
// | ||
// [event-header]: https://htmx.org/extensions/event-header/ | ||
package eventheader | ||
|
||
import "github.com/will-wow/typed-htmx-go/htmx" | ||
|
||
const Extension htmx.Extension = "event-header" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
// package responsetargets allows you to specify different target elements to be swapped when different HTTP response codes are received. | ||
// | ||
// # Install | ||
// | ||
// <script src="https://unpkg.com/[email protected]/dist/ext/response-targets.js"></script> | ||
// | ||
// Extension: [response-targets] | ||
// | ||
// [response-targets]: https://htmx.org/extensions/response-targets/ | ||
package responsetargets | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/will-wow/typed-htmx-go/htmx" | ||
) | ||
|
||
// Extension allows you to specify different target elements to be swapped when different HTTP response codes are received. | ||
// | ||
// # Install | ||
// | ||
// <script src="https://unpkg.com/[email protected]/dist/ext/response-targets.js"></script> | ||
// | ||
// Extension: [response-targets] | ||
// | ||
// [response-targets]: https://htmx.org/extensions/response-targets/ | ||
const Extension htmx.Extension = "response-targets" | ||
|
||
// A Code is a complete or partial HTTP response code. | ||
type Code interface { | ||
code() string | ||
} | ||
|
||
// A Status is a complete HTTP response code. You can wrap the http.Status* constants with [Status]. | ||
type Status int | ||
|
||
var _ Code = Status(0) | ||
|
||
func (s Status) code() string { | ||
return strconv.Itoa(int(s)) | ||
} | ||
|
||
// an errorCode is the string "error", used to cover all 4xx and 5xx HTTP response codes. | ||
type errorCode string | ||
|
||
var _ Code = errorCode("") | ||
|
||
// Error is a status code that covers all 4xx and 5xx HTTP response codes. | ||
const Error errorCode = "error" | ||
|
||
func (e errorCode) code() string { | ||
return string(e) | ||
} | ||
|
||
// A wildcard is a partial HTTP response code with a wildcard component. | ||
type wildcard []int | ||
|
||
var _ Code = (wildcard)(nil) | ||
|
||
// Wildcard creates a wildcard code with the given digits. | ||
// For example, Wildcard(4, 1) results in hx-target-41*, and matches all 41x HTTP response codes. | ||
func Wildcard(digits ...int) wildcard { | ||
return digits | ||
} | ||
|
||
func (w wildcard) code() string { | ||
builder := strings.Builder{} | ||
for _, digit := range w { | ||
_, _ = builder.WriteString(strconv.Itoa(digit)) | ||
} | ||
_ = builder.WriteByte('*') | ||
return builder.String() | ||
} | ||
|
||
type wildcardX []int | ||
|
||
// WildcardX creates a wildcard code with the given digits, and uses an 'x' instead of a '*' in the generated attribute. | ||
// For example, WildcardX(4, 1) results in hx-target-41x, and matches all 41x HTTP response codes. | ||
func WildcardX(digits ...int) wildcardX { | ||
return digits | ||
} | ||
|
||
func (w wildcardX) code() string { | ||
builder := strings.Builder{} | ||
for _, digit := range w { | ||
_, _ = builder.WriteString(strconv.Itoa(digit)) | ||
} | ||
_ = builder.WriteByte('x') | ||
return builder.String() | ||
} | ||
|
||
// Target specifies a target element to be swapped when specific HTTP response codes are received. | ||
// | ||
// Extension: [response-targets] | ||
// | ||
// [response-targets]: https://htmx.org/extensions/response-targets/ | ||
func Target[T any](hx htmx.HX[T], code Code, extendedSelector htmx.TargetSelector) T { | ||
attr := fmt.Sprintf("hx-target-%s", code.code()) | ||
return hx.Attr(htmx.Attribute(attr), string(extendedSelector)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package responsetargets_test | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/will-wow/typed-htmx-go/htmx" | ||
"github.com/will-wow/typed-htmx-go/htmx/ext/responsetargets" | ||
) | ||
|
||
var hx = htmx.NewStringAttrs() | ||
|
||
func ExampleTarget_code() { | ||
attr := responsetargets.Target(hx, responsetargets.Code(http.StatusNotFound), htmx.TargetRelative(htmx.Next, "div")) | ||
fmt.Println(attr) | ||
// Output: hx-target-404='next div' | ||
} | ||
|
||
func ExampleTarget_error() { | ||
attr := responsetargets.Target(hx, responsetargets.Error, htmx.TargetThis) | ||
fmt.Println(attr) | ||
// Output: hx-target-error='this' | ||
} | ||
|
||
func ExampleTarget_wildcard() { | ||
attr := responsetargets.Target(hx, responsetargets.Wildcard(4, 0), htmx.TargetRelative(htmx.Next, "div")) | ||
fmt.Println(attr) | ||
// Output: hx-target-40*='next div' | ||
} | ||
|
||
func ExampleTarget_wildcardX() { | ||
attr := responsetargets.Target(hx, responsetargets.WildcardX(4, 0), htmx.TargetRelative(htmx.Next, "div")) | ||
fmt.Println(attr) | ||
// Output: hx-target-40x='next div' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters