htmgo/framework/h/attribute.go

185 lines
3.5 KiB
Go
Raw Normal View History

package h
2024-09-21 16:52:56 +00:00
import (
"fmt"
"github.com/maddalax/htmgo/framework/hx"
"strings"
)
type AttributeMap map[string]any
func (m *AttributeMap) ToMap() map[string]string {
result := make(map[string]string)
for k, v := range *m {
switch v.(type) {
case AttributeMap:
m2 := v.(*AttributeMap).ToMap()
for _, a := range m2 {
result[k] = a
}
case string:
result[k] = v.(string)
default:
result[k] = fmt.Sprintf("%v", v)
}
}
return result
}
2024-09-21 16:52:56 +00:00
2024-09-27 15:10:00 +00:00
func Attribute(key string, value string) *AttributeR {
return &AttributeR{
Name: key,
Value: value,
}
2024-09-21 16:52:56 +00:00
}
2024-09-27 15:10:00 +00:00
func AttributeList(children ...*AttributeR) *AttributeMap {
2024-09-21 16:52:56 +00:00
m := make(AttributeMap)
for _, child := range children {
2024-09-27 15:10:00 +00:00
m[child.Name] = child.Value
2024-09-21 16:52:56 +00:00
}
return &m
}
func Attributes(attrs *AttributeMap) *AttributeMap {
return attrs
}
func AttributePairs(pairs ...string) *AttributeMap {
if len(pairs)%2 != 0 {
return &AttributeMap{}
}
m := make(AttributeMap)
for i := 0; i < len(pairs); i++ {
m[pairs[i]] = pairs[i+1]
i++
}
return &m
}
func Checked() Ren {
return Attribute("checked", "true")
}
func Id(value string) Ren {
if strings.HasPrefix(value, "#") {
value = value[1:]
}
return Attribute("id", value)
}
func Disabled() Ren {
return Attribute("disabled", "")
}
func HxTarget(target string) Ren {
return Attribute(hx.TargetAttr, target)
}
func Name(name string) Ren {
return Attribute("name", name)
}
func HxConfirm(message string) Ren {
return Attribute(hx.ConfirmAttr, message)
}
// HxInclude https://htmx.org/attributes/hx-include/
func HxInclude(selector string) Ren {
return Attribute(hx.IncludeAttr, selector)
}
2024-09-27 15:10:00 +00:00
func HxIndicator(tag string) *AttributeR {
2024-09-21 16:52:56 +00:00
return Attribute(hx.IndicatorAttr, tag)
}
func TriggerChildren() Ren {
return HxExtension("trigger-children")
}
2024-09-27 15:10:00 +00:00
func HxTriggerString(triggers ...string) *AttributeR {
2024-09-21 16:52:56 +00:00
trigger := hx.NewStringTrigger(strings.Join(triggers, ", "))
return Attribute(hx.TriggerAttr, trigger.ToString())
}
2024-09-27 15:10:00 +00:00
func HxTrigger(opts ...hx.TriggerEvent) *AttributeR {
2024-09-21 16:52:56 +00:00
return Attribute(hx.TriggerAttr, hx.NewTrigger(opts...).ToString())
}
2024-09-27 15:10:00 +00:00
func HxTriggerClick(opts ...hx.Modifier) *AttributeR {
2024-09-21 16:52:56 +00:00
return HxTrigger(hx.OnClick(opts...))
}
func HxExtension(value string) Ren {
return Attribute(hx.ExtAttr, value)
}
func Href(path string) Ren {
return Attribute("href", path)
}
2024-09-27 02:15:04 +00:00
func Target(target string) Ren {
return Attribute("target", target)
}
func D(value string) Ren {
return Attribute("d", value)
}
2024-09-27 14:46:45 +00:00
func Alt(value string) Ren {
return Attribute("alt", value)
}
func For(value string) Ren {
return Attribute("for", value)
}
2024-09-21 16:52:56 +00:00
func Type(name string) Ren {
return Attribute("type", name)
}
func Placeholder(placeholder string) Ren {
return Attribute("placeholder", placeholder)
}
func Hidden() Ren {
return Attribute("style", "display:none")
}
2024-09-27 15:10:00 +00:00
func Class(value ...string) *AttributeR {
2024-09-21 16:52:56 +00:00
return Attribute("class", MergeClasses(value...))
}
func ClassX(value string, m ClassMap) Ren {
builder := strings.Builder{}
builder.WriteString(value)
builder.WriteString(" ")
for k, v := range m {
if v {
builder.WriteString(k)
builder.WriteString(" ")
}
}
return Class(builder.String())
}
func MergeClasses(classes ...string) string {
if len(classes) == 1 {
return classes[0]
}
builder := strings.Builder{}
for _, s := range classes {
builder.WriteString(s)
builder.WriteString(" ")
}
return builder.String()
}
func Boost() Ren {
return Attribute(hx.BoostAttr, "true")
}
func IfQueryParam(key string, node *Element) Ren {
return Fragment(Attribute("hx-if-qp:"+key, "true"), node)
}