wp-go/app/theme/wp/components.go

214 lines
6.3 KiB
Go
Raw Normal View History

2023-04-29 14:05:24 +00:00
package wp
import (
2023-05-04 12:37:06 +00:00
"github.com/fthvgb1/wp-go/app/cmd/reload"
"github.com/fthvgb1/wp-go/app/pkg/constraints"
2023-04-29 14:05:24 +00:00
"github.com/fthvgb1/wp-go/helper/maps"
"github.com/fthvgb1/wp-go/helper/slice"
str "github.com/fthvgb1/wp-go/helper/strings"
"strings"
)
func (h *Handle) DeleteComponents(scene, name string) {
h.componentHook[scene] = append(h.componentHook[scene], func(c Components[string]) (Components[string], bool) {
return c, c.Name != name
})
}
func (h *Handle) ReplaceComponents(scene, name string, components Components[string]) {
h.componentHook[scene] = append(h.componentHook[scene], func(c Components[string]) (Components[string], bool) {
if c.Name == name {
c = components
}
return c, true
})
}
func (h *Handle) HookComponents(scene string, fn func(Components[string]) (Components[string], bool)) {
h.componentHook[scene] = append(h.componentHook[scene], fn)
}
func CalComponents(h *Handle) {
2023-04-30 13:17:33 +00:00
componentss := reload.GetAnyValMapBy("scene-components", str.Join("allScene-", h.scene), h, func(h *Handle) map[string][]Components[string] {
return maps.MergeBy(func(k string, v1, v2 []Components[string]) ([]Components[string], bool) {
vv := append(v1, v2...)
return vv, vv != nil
}, nil, h.components[h.scene], h.components[constraints.AllScene])
})
for k, components := range componentss {
2023-06-12 16:17:15 +00:00
key := str.Join("calComponents-", h.scene, "-", k)
2023-04-29 14:05:24 +00:00
ss := reload.GetAnyValMapBy("calComponents", key, h, func(h *Handle) []Components[string] {
r := slice.FilterAndMap(components, func(t Components[string]) (Components[string], bool) {
fns, ok := h.componentHook[k]
if !ok {
return t, true
}
for _, fn := range fns {
c, ok := fn(t)
if !ok {
return c, false
}
t = c
}
return t, true
})
slice.Sort(r, func(i, j Components[string]) bool {
return i.Order > j.Order
})
return r
})
var s = make([]string, 0, len(ss))
for _, component := range ss {
if component.Val != "" {
s = append(s, component.Val)
continue
}
if component.Fn != nil {
v := ""
if component.Cached {
v = reload.GetAnyValMapBy("cacheComponents", component.Name, h, component.Fn)
} else {
v = component.Fn(h)
}
if v != "" {
s = append(s, v)
}
}
}
h.ginH[k] = strings.Join(s, "\n")
}
}
2023-04-30 13:17:33 +00:00
func (h *Handle) PushComponents(scene, componentType string, components ...Components[string]) {
c, ok := h.components[scene]
if !ok {
c = make(map[string][]Components[string])
h.components[scene] = c
}
c[componentType] = append(c[componentType], components...)
2023-04-29 14:05:24 +00:00
}
2023-06-15 10:00:57 +00:00
func (h *Handle) PushGroupComponentStr(scene, componentType, name string, order float64, strs ...string) {
2023-06-13 04:02:46 +00:00
var component = Components[string]{
Val: strings.Join(slice.FilterAndMap(strs, func(t string) (string, bool) {
2023-06-13 04:03:52 +00:00
t = strings.Trim(t, " \n\r\t\v\x00")
2023-06-13 04:02:46 +00:00
if t == "" {
return "", false
}
return t, true
}), "\n"),
Order: order,
Name: name,
2023-04-29 14:05:24 +00:00
}
2023-06-13 04:02:46 +00:00
h.PushComponents(scene, componentType, component)
2023-04-29 14:05:24 +00:00
}
2023-06-15 10:00:57 +00:00
func (h *Handle) PushCacheGroupHeadScript(scene, name string, order float64, fns ...func(*Handle) string) {
2023-04-30 13:17:33 +00:00
h.PushGroupCacheComponentFn(scene, constraints.HeadScript, name, order, fns...)
2023-04-29 14:05:24 +00:00
}
2023-04-30 13:17:33 +00:00
func (h *Handle) PushFooterScript(scene string, components ...Components[string]) {
h.PushComponents(scene, constraints.FooterScript, components...)
2023-04-29 14:05:24 +00:00
}
2023-06-15 10:00:57 +00:00
func (h *Handle) PushGroupFooterScript(scene, name string, order float64, strs ...string) {
2023-04-30 13:17:33 +00:00
h.PushGroupComponentStr(scene, constraints.FooterScript, name, order, strs...)
2023-04-29 14:05:24 +00:00
}
2023-06-15 10:00:57 +00:00
func (h *Handle) PushCacheGroupFooterScript(scene, name string, order float64, fns ...func(*Handle) string) {
2023-04-30 13:17:33 +00:00
h.PushGroupCacheComponentFn(scene, constraints.FooterScript, name, order, fns...)
2023-04-29 14:05:24 +00:00
}
2023-06-15 10:00:57 +00:00
func (h *Handle) PushGroupCacheComponentFn(scene, componentType, name string, order float64, fns ...func(*Handle) string) {
2023-04-30 13:17:33 +00:00
h.PushComponents(scene, componentType, NewComponent(name, "", true, order, func(h *Handle) string {
2023-04-29 14:05:24 +00:00
return strings.Join(slice.Map(fns, func(t func(*Handle) string) string {
return t(h)
}), "\n")
}))
}
2023-06-15 10:00:57 +00:00
func NewComponent(name, val string, cached bool, order float64, fn func(handle *Handle) string) Components[string] {
2023-04-30 13:17:33 +00:00
return Components[string]{Fn: fn, Name: name, Cached: cached, Order: order, Val: val}
2023-04-29 14:05:24 +00:00
}
2023-06-15 10:00:57 +00:00
func (h *Handle) AddCacheComponent(scene, componentType, name string, order float64, fn func(*Handle) string) {
2023-04-30 13:17:33 +00:00
h.PushComponents(scene, componentType, NewComponent(name, "", true, order, fn))
2023-04-29 14:05:24 +00:00
}
2023-04-30 13:17:33 +00:00
func (h *Handle) PushHeadScript(scene string, components ...Components[string]) {
h.PushComponents(scene, constraints.HeadScript, components...)
2023-04-29 14:05:24 +00:00
}
2023-06-15 10:00:57 +00:00
func (h *Handle) PushGroupHeadScript(scene, name string, order float64, str ...string) {
2023-04-30 13:17:33 +00:00
h.PushGroupComponentStr(scene, constraints.HeadScript, name, order, str...)
2023-04-29 14:05:24 +00:00
}
func GetComponentsArgs[T any](h *Handle, k string, defaults T) T {
v, ok := h.componentsArgs[k]
if ok {
vv, ok := v.(T)
if ok {
return vv
}
}
return defaults
}
func PushComponentsArgsForSlice[T any](h *Handle, name string, v ...T) {
val, ok := h.componentsArgs[name]
if !ok {
var vv []T
vv = append(vv, v...)
h.componentsArgs[name] = vv
return
}
vv, ok := val.([]T)
if ok {
vv = append(vv, v...)
h.componentsArgs[name] = vv
}
}
func SetComponentsArgsForMap[K comparable, V any](h *Handle, name string, key K, v V) {
val, ok := h.componentsArgs[name]
if !ok {
vv := make(map[K]V)
vv[key] = v
h.componentsArgs[name] = vv
return
}
vv, ok := val.(map[K]V)
if ok {
vv[key] = v
h.componentsArgs[name] = vv
}
}
func MergeComponentsArgsForMap[K comparable, V any](h *Handle, name string, m map[K]V) {
val, ok := h.componentsArgs[name]
if !ok {
h.componentsArgs[name] = m
return
}
vv, ok := val.(map[K]V)
if ok {
h.componentsArgs[name] = maps.Merge(vv, m)
}
}
2023-07-17 14:43:15 +00:00
func SetComponentsArgs(h *Handle, key string, value any) {
2023-04-29 14:05:24 +00:00
h.componentsArgs[key] = value
}
func (h *Handle) ComponentFilterFn(name string) ([]func(*Handle, string, ...any) string, bool) {
fn, ok := h.componentFilterFn[name]
return fn, ok
}
2023-08-26 13:29:55 +00:00
func (h *Handle) AddActionFilter(name string, fns ...func(*Handle, string, ...any) string) {
2023-04-29 14:05:24 +00:00
h.componentFilterFn[name] = append(h.componentFilterFn[name], fns...)
}
2023-08-26 13:29:55 +00:00
func (h *Handle) DoActionFilter(name, s string, args ...any) string {
2023-04-29 14:05:24 +00:00
calls, ok := h.componentFilterFn[name]
if ok {
return slice.Reduce(calls, func(fn func(*Handle, string, ...any) string, r string) string {
return fn(h, r, args...)
}, s)
}
return s
}