ytdl-web/pkg/htmx/htmx.go

121 lines
3.0 KiB
Go
Raw Permalink Normal View History

2023-08-14 18:14:08 -04:00
package htmx
import "net/http"
type HTMX struct {
w http.ResponseWriter
r *http.Request
}
func New(w http.ResponseWriter, r *http.Request) *HTMX {
return &HTMX{
w: w,
r: r,
}
}
//
// Request header methods
//
// True when the request is an HTMX request
func (h *HTMX) IsHtmxRequest() bool {
return h.r.Header.Get("HX-Request") == "true"
}
// Indicates that the request is via an element using hx-boost
func (h *HTMX) IsBoosted() bool {
return h.r.Header.Get("HX-Boosted") == "true"
}
// The current URL of the browser
func (h *HTMX) CurrentUrl() string {
return h.r.Header.Get("HX-Current-URL")
}
// If the request is for history restoration after a miss in the local history cache
func (h *HTMX) IsHistoryRestore() bool {
return h.r.Header.Get("HX-History-Restore") == "true"
}
// The id of the element that triggered the request
func (h *HTMX) TriggerId() string {
return h.r.Header.Get("HX-Trigger")
}
// The name of the element that triggered the request
func (h *HTMX) TriggerName() string {
return h.r.Header.Get("HX-Trigger-Name")
}
// The id of the target element
func (h *HTMX) TargetId() string {
return h.r.Header.Get("HX-Target")
}
// The value entered by the user when prompted via hx-prompt
func (h *HTMX) Prompt() string {
return h.r.Header.Get("HX-Prompt")
}
//
// Response header methods
//
// Pushe a new url into the history stack
func (h *HTMX) PushUrl(url string) {
h.w.Header().Set("HX-Push", url)
}
// Trigger a client-side redirect to a new location
func (h *HTMX) RedirectTo(url string) {
h.w.Header().Set("HX-Redirect", url)
}
// Triggers a client-side redirect to a new location that acts as a swap
func (h *HTMX) Location(url string) {
h.w.Header().Set("Location", url)
}
// Replace the current URL in the location bar
func (h *HTMX) ReplaceUrl(url string) {
h.w.Header().Set("HX-Replace-Url", url)
}
// Sets refresh to true causing the client side to do a full refresh of the page
func (h *HTMX) Refresh() {
h.w.Header().Set("HX-Refresh", "true")
}
// Trigger client side events
func (h *HTMX) Trigger(name string) {
h.w.Header().Set("HX-Trigger", name)
}
// Trigger client side events after the swap step
func (h *HTMX) TriggerAfterSwap(name string) {
h.w.Header().Set("HX-Trigger-After-Swap", name)
}
// Trigger client side events after the settle step
func (h *HTMX) TriggerAfterSettle(name string) {
h.w.Header().Set("HX-Trigger-After-Settle", name)
}
// Specify how the response will be swapped. See hx-swap for possible values
func (h *HTMX) Reswap(value string) {
h.w.Header().Set("HX-Reswap", value)
}
// Update the target of the content update to a different element on the page
// Value should be a CSS selector.
func (h *HTMX) Retarget(value string) {
h.w.Header().Set("HX-Retarget", value)
}
// Choose which part of the response is used to be swapped in. Overrides an existing hx-select on the triggering element.
// Value should be a CSS selector.
func (h *HTMX) Reselect(value string) {
h.w.Header().Set("HX-Reselect", value)
}