2024-01-22 15:22:16 +00:00
|
|
|
package h
|
|
|
|
|
|
2024-09-11 00:52:18 +00:00
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
2024-09-11 17:31:40 +00:00
|
|
|
"fmt"
|
2024-09-11 00:52:18 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
|
"html"
|
|
|
|
|
"net/url"
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
2024-01-22 15:22:16 +00:00
|
|
|
type Node struct {
|
2024-09-11 00:52:18 +00:00
|
|
|
id string
|
2024-01-22 15:22:16 +00:00
|
|
|
tag string
|
|
|
|
|
attributes map[string]string
|
2024-09-11 23:41:21 +00:00
|
|
|
children []Renderable
|
2024-01-22 15:22:16 +00:00
|
|
|
text string
|
|
|
|
|
value string
|
2024-09-11 00:52:18 +00:00
|
|
|
changed bool
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func (node *Node) Render() *Node {
|
|
|
|
|
return node
|
2024-09-11 00:52:18 +00:00
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func (node *Node) AppendChild(child Renderable) Renderable {
|
2024-09-11 00:52:18 +00:00
|
|
|
node.children = append(node.children, child)
|
|
|
|
|
return node
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func (node *Node) SetChanged(changed bool) Renderable {
|
2024-09-11 00:52:18 +00:00
|
|
|
node.changed = changed
|
|
|
|
|
return node
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func Data(data map[string]any) Renderable {
|
2024-09-11 00:52:18 +00:00
|
|
|
serialized, err := json.Marshal(data)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return Empty()
|
|
|
|
|
}
|
|
|
|
|
return Attribute("x-data", string(serialized))
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func ClassIf(condition bool, value string) Renderable {
|
2024-09-11 00:52:18 +00:00
|
|
|
if condition {
|
|
|
|
|
return Class(value)
|
|
|
|
|
}
|
|
|
|
|
return Empty()
|
2024-01-22 15:22:16 +00:00
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func Class(value ...string) Renderable {
|
2024-01-22 15:22:16 +00:00
|
|
|
return &Node{
|
|
|
|
|
tag: "class",
|
2024-09-11 00:52:18 +00:00
|
|
|
value: MergeClasses(value...),
|
2024-01-22 15:22:16 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 00:52:18 +00:00
|
|
|
func MergeClasses(classes ...string) string {
|
|
|
|
|
builder := ""
|
|
|
|
|
for _, s := range classes {
|
|
|
|
|
builder += s + " "
|
|
|
|
|
}
|
|
|
|
|
return builder
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func Id(value string) Renderable {
|
2024-09-11 17:31:40 +00:00
|
|
|
if strings.HasPrefix(value, "#") {
|
|
|
|
|
value = value[1:]
|
|
|
|
|
}
|
2024-01-22 15:22:16 +00:00
|
|
|
return Attribute("id", value)
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func Attributes(attrs map[string]string) Renderable {
|
2024-01-22 15:22:16 +00:00
|
|
|
return &Node{
|
2024-09-11 17:31:40 +00:00
|
|
|
tag: "attribute",
|
|
|
|
|
attributes: attrs,
|
2024-01-22 15:22:16 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func Attribute(key string, value string) Renderable {
|
2024-09-11 17:31:40 +00:00
|
|
|
return Attributes(map[string]string{key: value})
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func Disabled() Renderable {
|
2024-09-11 17:31:40 +00:00
|
|
|
return Attribute("disabled", "")
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func Get(path string) Renderable {
|
2024-09-11 00:52:18 +00:00
|
|
|
return Attribute("hx-get", path)
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func GetPartial(partial func(ctx *fiber.Ctx) *Partial) Renderable {
|
2024-09-11 17:31:40 +00:00
|
|
|
return Get(GetPartialPath(partial))
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func GetPartialWithQs(partial func(ctx *fiber.Ctx) *Partial, qs string) Renderable {
|
2024-09-11 17:31:40 +00:00
|
|
|
return Get(GetPartialPathWithQs(partial, qs))
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 00:52:18 +00:00
|
|
|
func CreateTriggers(triggers ...string) []string {
|
|
|
|
|
return triggers
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type ReloadParams struct {
|
|
|
|
|
Triggers []string
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func ViewOnLoad(partial func(ctx *fiber.Ctx) *Partial) Renderable {
|
2024-09-11 17:31:40 +00:00
|
|
|
return View(partial, ReloadParams{
|
|
|
|
|
Triggers: CreateTriggers("load"),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func View(partial func(ctx *fiber.Ctx) *Partial, params ReloadParams) Renderable {
|
2024-09-11 17:31:40 +00:00
|
|
|
return Div(Attributes(map[string]string{
|
|
|
|
|
"hx-get": GetPartialPath(partial),
|
|
|
|
|
"hx-trigger": strings.Join(params.Triggers, ", "),
|
|
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func ViewWithTriggers(partial func(ctx *fiber.Ctx) *Partial, triggers ...string) Renderable {
|
2024-09-11 17:31:40 +00:00
|
|
|
return Div(Attributes(map[string]string{
|
|
|
|
|
"hx-get": GetPartialPath(partial),
|
|
|
|
|
"hx-trigger": strings.Join(triggers, ", "),
|
|
|
|
|
}))
|
2024-09-11 00:52:18 +00:00
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func GetWithQs(path string, qs map[string]string) Renderable {
|
2024-09-11 00:52:18 +00:00
|
|
|
u, err := url.Parse(path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return Empty()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
q := u.Query()
|
|
|
|
|
|
|
|
|
|
for s := range qs {
|
|
|
|
|
q.Add(s, qs[s])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
u.RawQuery = q.Encode()
|
|
|
|
|
|
|
|
|
|
return Get(u.String())
|
2024-01-22 15:22:16 +00:00
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func Post(url string) Renderable {
|
2024-01-22 15:22:16 +00:00
|
|
|
return Attribute("hx-post", url)
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func Trigger(trigger string) Renderable {
|
2024-01-22 15:22:16 +00:00
|
|
|
return Attribute("hx-trigger", trigger)
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func Text(text string) Renderable {
|
2024-09-11 00:52:18 +00:00
|
|
|
return &Node{
|
|
|
|
|
tag: "text",
|
|
|
|
|
text: text,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func Pf(format string, args ...interface{}) Renderable {
|
2024-09-11 17:31:40 +00:00
|
|
|
return P(fmt.Sprintf(format, args...))
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func Target(target string) Renderable {
|
2024-01-22 15:22:16 +00:00
|
|
|
return Attribute("hx-target", target)
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func Name(name string) Renderable {
|
2024-01-22 15:22:16 +00:00
|
|
|
return Attribute("name", name)
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func Confirm(message string) Renderable {
|
2024-01-22 15:22:16 +00:00
|
|
|
return Attribute("hx-confirm", message)
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func Href(path string) Renderable {
|
2024-01-22 15:22:16 +00:00
|
|
|
return Attribute("href", path)
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func Type(name string) Renderable {
|
2024-01-22 15:22:16 +00:00
|
|
|
return Attribute("type", name)
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func Placeholder(placeholder string) Renderable {
|
2024-01-22 15:22:16 +00:00
|
|
|
return Attribute("placeholder", placeholder)
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func OutOfBandSwap(selector string) Renderable {
|
2024-09-11 17:31:40 +00:00
|
|
|
return Attribute("hx-swap-oob",
|
|
|
|
|
Ternary(selector == "", "true", selector))
|
2024-01-22 15:22:16 +00:00
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func Click(value string) Renderable {
|
2024-01-22 15:22:16 +00:00
|
|
|
return Attribute("onclick", value)
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func Tag(tag string, children ...Renderable) Renderable {
|
2024-01-22 15:22:16 +00:00
|
|
|
return &Node{
|
2024-09-11 17:31:40 +00:00
|
|
|
tag: tag,
|
2024-01-22 15:22:16 +00:00
|
|
|
children: children,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func Html(children ...Renderable) Renderable {
|
2024-09-11 17:31:40 +00:00
|
|
|
return Tag("html", children...)
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func Head(children ...Renderable) Renderable {
|
2024-09-11 17:31:40 +00:00
|
|
|
return Tag("head", children...)
|
2024-01-22 15:22:16 +00:00
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func Body(children ...Renderable) Renderable {
|
2024-09-11 17:31:40 +00:00
|
|
|
return Tag("body", children...)
|
2024-01-22 15:22:16 +00:00
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func Script(url string) Renderable {
|
2024-01-22 15:22:16 +00:00
|
|
|
return &Node{
|
|
|
|
|
tag: "script",
|
|
|
|
|
attributes: map[string]string{
|
|
|
|
|
"src": url,
|
|
|
|
|
},
|
2024-09-11 23:41:21 +00:00
|
|
|
children: make([]Renderable, 0),
|
2024-01-22 15:22:16 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func Raw(text string) Renderable {
|
2024-09-11 00:52:18 +00:00
|
|
|
return &Node{
|
|
|
|
|
tag: "raw",
|
2024-09-11 23:41:21 +00:00
|
|
|
children: make([]Renderable, 0),
|
2024-09-11 00:52:18 +00:00
|
|
|
value: text,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func RawScript(text string) Renderable {
|
2024-09-11 00:52:18 +00:00
|
|
|
return Raw("<script>" + text + "</script>")
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func Div(children ...Renderable) Renderable {
|
2024-09-11 17:31:40 +00:00
|
|
|
return Tag("div", children...)
|
2024-01-22 15:22:16 +00:00
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func Input(inputType string, children ...Renderable) Renderable {
|
2024-01-22 15:22:16 +00:00
|
|
|
return &Node{
|
|
|
|
|
tag: "input",
|
|
|
|
|
attributes: map[string]string{
|
|
|
|
|
"type": inputType,
|
|
|
|
|
},
|
|
|
|
|
children: children,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func List[T any](items []T, mapper func(item T, index int) Renderable) Renderable {
|
2024-01-22 15:22:16 +00:00
|
|
|
node := &Node{
|
|
|
|
|
tag: "",
|
2024-09-11 23:41:21 +00:00
|
|
|
children: make([]Renderable, len(items)),
|
2024-01-22 15:22:16 +00:00
|
|
|
}
|
|
|
|
|
for index, value := range items {
|
2024-09-11 18:09:55 +00:00
|
|
|
node.children[index] = mapper(value, index)
|
2024-01-22 15:22:16 +00:00
|
|
|
}
|
|
|
|
|
return node
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func Fragment(children ...Renderable) Renderable {
|
2024-01-22 15:22:16 +00:00
|
|
|
return &Node{
|
|
|
|
|
tag: "",
|
|
|
|
|
children: children,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func AttributeList(children ...Renderable) Renderable {
|
2024-09-11 00:52:18 +00:00
|
|
|
return &Node{
|
|
|
|
|
tag: FlagAttributeList,
|
|
|
|
|
children: children,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func AppendChildren(node *Node, children ...Renderable) Renderable {
|
2024-09-11 00:52:18 +00:00
|
|
|
node.children = append(node.children, children...)
|
|
|
|
|
return node
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func Button(children ...Renderable) Renderable {
|
2024-09-11 17:31:40 +00:00
|
|
|
return Tag("button", children...)
|
2024-01-22 15:22:16 +00:00
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func Indicator(tag string) Renderable {
|
2024-09-11 00:52:18 +00:00
|
|
|
return Attribute("hx-indicator", tag)
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func P(text string, children ...Renderable) Renderable {
|
2024-01-22 15:22:16 +00:00
|
|
|
return &Node{
|
|
|
|
|
tag: "p",
|
|
|
|
|
children: children,
|
|
|
|
|
text: text,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func Form(children ...Renderable) Renderable {
|
2024-09-11 18:09:55 +00:00
|
|
|
return Tag("form", children...)
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func A(text string, children ...Renderable) Renderable {
|
2024-01-22 15:22:16 +00:00
|
|
|
return &Node{
|
|
|
|
|
tag: "a",
|
|
|
|
|
children: children,
|
|
|
|
|
text: text,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func Nav(children ...Renderable) Renderable {
|
2024-09-11 17:31:40 +00:00
|
|
|
return Tag("nav", children...)
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func Empty() Renderable {
|
2024-01-22 15:22:16 +00:00
|
|
|
return &Node{
|
|
|
|
|
tag: "",
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func BeforeRequestSetHtml(children ...Renderable) Renderable {
|
2024-09-11 00:52:18 +00:00
|
|
|
serialized := Render(Fragment(children...))
|
|
|
|
|
return Attribute("hx-on::before-request", `this.innerHTML = '`+html.EscapeString(serialized)+`'`)
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func BeforeRequestSetAttribute(key string, value string) Renderable {
|
2024-09-11 17:31:40 +00:00
|
|
|
return Attribute("hx-on::before-request", `this.setAttribute('`+key+`', '`+value+`')`)
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func BeforeRequestSetText(text string) Renderable {
|
2024-09-11 17:31:40 +00:00
|
|
|
return Attribute("hx-on::before-request", `this.innerText = '`+text+`'`)
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func AfterRequestRemoveAttribute(key string, value string) Renderable {
|
2024-09-11 17:31:40 +00:00
|
|
|
return Attribute("hx-on::after-request", `this.removeAttribute('`+key+`')`)
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func IfQueryParam(key string, node *Node) Renderable {
|
2024-09-11 17:31:40 +00:00
|
|
|
return Fragment(Attribute("hx-if-qp:"+key, "true"), node)
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func Hidden() Renderable {
|
2024-09-11 17:31:40 +00:00
|
|
|
return Attribute("style", "display:none")
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func MatchQueryParam(defaultValue string, active string, m map[string]*Node) Renderable {
|
2024-09-11 17:31:40 +00:00
|
|
|
|
|
|
|
|
rendered := make(map[string]string)
|
|
|
|
|
for s, node := range m {
|
|
|
|
|
rendered[s] = Render(node)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
root := Tag("span",
|
|
|
|
|
m[active],
|
|
|
|
|
Trigger("url"),
|
|
|
|
|
Attribute("hx-match-qp", "true"),
|
|
|
|
|
Attribute("hx-match-qp-default", defaultValue),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
for s, node := range rendered {
|
2024-09-11 23:41:21 +00:00
|
|
|
root = AppendChildren(root.Render(), Attribute("hx-match-qp-mapping:"+s, ``+html.EscapeString(node)+``))
|
2024-09-11 17:31:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return root
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func AfterRequestSetHtml(children ...Renderable) Renderable {
|
2024-09-11 00:52:18 +00:00
|
|
|
serialized := Render(Fragment(children...))
|
|
|
|
|
return Attribute("hx-on::after-request", `this.innerHTML = '`+html.EscapeString(serialized)+`'`)
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func Children(children []Renderable) Renderable {
|
2024-09-11 17:31:40 +00:00
|
|
|
return &Node{
|
|
|
|
|
tag: FlagChildrenList,
|
|
|
|
|
children: children,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func Label(text string) Renderable {
|
2024-09-11 18:09:55 +00:00
|
|
|
return Tag("label", Text(text))
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func If(condition bool, node Renderable) Renderable {
|
2024-01-22 15:22:16 +00:00
|
|
|
if condition {
|
|
|
|
|
return node
|
|
|
|
|
} else {
|
|
|
|
|
return Empty()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func IfElse(condition bool, node Renderable, node2 Renderable) Renderable {
|
2024-01-22 15:22:16 +00:00
|
|
|
if condition {
|
|
|
|
|
return node
|
|
|
|
|
} else {
|
|
|
|
|
return node2
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-11 17:31:40 +00:00
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func IfElseLazy(condition bool, cb1 func() Renderable, cb2 func() Renderable) Renderable {
|
2024-09-11 17:31:40 +00:00
|
|
|
if condition {
|
|
|
|
|
return cb1()
|
|
|
|
|
} else {
|
|
|
|
|
return cb2()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func IfHtmxRequest(ctx *fiber.Ctx, node Renderable) Renderable {
|
2024-09-11 17:31:40 +00:00
|
|
|
if ctx.Get("HX-Request") != "" {
|
|
|
|
|
return node
|
|
|
|
|
}
|
|
|
|
|
return Empty()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type SwapArg struct {
|
|
|
|
|
Selector string
|
|
|
|
|
Content *Node
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewSwap(selector string, content *Node) SwapArg {
|
|
|
|
|
return SwapArg{
|
|
|
|
|
Selector: selector,
|
|
|
|
|
Content: content,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func Swap(ctx *fiber.Ctx, content Renderable) Renderable {
|
2024-09-11 17:31:40 +00:00
|
|
|
return SwapWithSelector(ctx, "", content)
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func SwapWithSelector(ctx *fiber.Ctx, selector string, content Renderable) Renderable {
|
2024-09-11 17:31:40 +00:00
|
|
|
if ctx == nil || ctx.Get("HX-Request") == "" {
|
|
|
|
|
return Empty()
|
|
|
|
|
}
|
2024-09-11 23:41:21 +00:00
|
|
|
c := content.Render()
|
|
|
|
|
return c.AppendChild(OutOfBandSwap(selector))
|
2024-09-11 17:31:40 +00:00
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func SwapMany(ctx *fiber.Ctx, args ...SwapArg) Renderable {
|
2024-09-11 17:31:40 +00:00
|
|
|
if ctx.Get("HX-Request") == "" {
|
|
|
|
|
return Empty()
|
|
|
|
|
}
|
|
|
|
|
for _, arg := range args {
|
|
|
|
|
arg.Content.AppendChild(OutOfBandSwap(arg.Selector))
|
|
|
|
|
}
|
2024-09-11 23:41:21 +00:00
|
|
|
return Fragment(Map(args, func(arg SwapArg) Renderable {
|
2024-09-11 17:31:40 +00:00
|
|
|
return arg.Content
|
|
|
|
|
})...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type OnRequestSwapArgs struct {
|
|
|
|
|
Target string
|
|
|
|
|
Get string
|
|
|
|
|
Default *Node
|
|
|
|
|
BeforeRequest *Node
|
|
|
|
|
AfterRequest *Node
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:41:21 +00:00
|
|
|
func OnRequestSwap(args OnRequestSwapArgs) Renderable {
|
2024-09-11 17:31:40 +00:00
|
|
|
return Div(args.Default,
|
|
|
|
|
BeforeRequestSetHtml(args.BeforeRequest),
|
|
|
|
|
AfterRequestSetHtml(args.AfterRequest),
|
|
|
|
|
Get(args.Get),
|
|
|
|
|
Target(args.Target),
|
|
|
|
|
)
|
|
|
|
|
}
|