htmgo/framework/h/base.go

97 lines
1.8 KiB
Go
Raw Normal View History

2024-09-11 00:52:18 +00:00
package h
import (
"net/http"
"reflect"
"runtime"
)
type Partial struct {
Headers *Headers
Root *Element
2024-09-11 00:52:18 +00:00
}
type Page struct {
Root Ren
2024-09-11 00:52:18 +00:00
HttpMethod string
}
func NewPage(root Ren) *Page {
2024-09-11 00:52:18 +00:00
return &Page{
HttpMethod: http.MethodGet,
Root: root,
}
}
2024-10-20 12:48:19 +00:00
func EmptyPage() *Page {
return NewPage(Fragment())
}
func NewPageWithHttpMethod(httpMethod string, root *Element) *Page {
2024-09-11 00:52:18 +00:00
return &Page{
HttpMethod: httpMethod,
Root: root,
}
}
func NewPartialWithHeaders(headers *Headers, root *Element) *Partial {
2024-09-11 00:52:18 +00:00
return &Partial{
Headers: headers,
Root: root,
2024-09-11 00:52:18 +00:00
}
}
func NewPartial(root *Element) *Partial {
2024-09-11 00:52:18 +00:00
return &Partial{
Root: root,
2024-09-11 00:52:18 +00:00
}
}
2024-09-21 17:08:23 +00:00
func SwapManyPartialWithHeaders(ctx *RequestContext, headers *Headers, swaps ...*Element) *Partial {
return NewPartialWithHeaders(
headers,
SwapMany(ctx, swaps...),
)
}
2024-10-01 22:19:38 +00:00
func RedirectPartial(path string) *Partial {
return RedirectPartialWithHeaders(path, NewHeaders())
}
func RedirectPartialWithHeaders(path string, headers *Headers) *Partial {
h := *NewHeaders("HX-Redirect", path)
for k, v := range *headers {
h[k] = v
}
return NewPartialWithHeaders(&h, Fragment())
}
2024-09-24 19:51:46 +00:00
func SwapPartial(ctx *RequestContext, swap *Element) *Partial {
return NewPartial(
SwapMany(ctx, swap))
}
2024-10-02 03:26:03 +00:00
func EmptyPartial() *Partial {
return NewPartial(Fragment())
}
2024-09-21 16:52:56 +00:00
func SwapManyPartial(ctx *RequestContext, swaps ...*Element) *Partial {
return NewPartial(
SwapMany(ctx, swaps...),
)
}
func SwapManyXPartial(ctx *RequestContext, swaps ...SwapArg) *Partial {
return NewPartial(
SwapManyX(ctx, swaps...),
)
}
2024-09-24 19:51:46 +00:00
func GetPartialPath(partial PartialFunc) string {
return "/" + runtime.FuncForPC(reflect.ValueOf(partial).Pointer()).Name()
2024-09-11 00:52:18 +00:00
}
2024-09-21 03:59:07 +00:00
func GetPartialPathWithQs(partial func(ctx *RequestContext) *Partial, qs *Qs) string {
2024-10-11 01:17:31 +00:00
return GetPartialPath(partial) + "?" + qs.ToString()
2024-09-11 00:52:18 +00:00
}