分类
This commit is contained in:
parent
a313210f71
commit
8d398016ac
3
internal/pkg/cache/cache.go
vendored
3
internal/pkg/cache/cache.go
vendored
@ -124,6 +124,9 @@ func (a *Arch) getArchiveCache(ctx context.Context) []models.PostArchive {
|
|||||||
return a.data
|
return a.data
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CategoriesTags categories or tags
|
||||||
|
//
|
||||||
|
// t is constraints.Tag or constraints.Category
|
||||||
func CategoriesTags(ctx context.Context, t ...int) []models.TermsMy {
|
func CategoriesTags(ctx context.Context, t ...int) []models.TermsMy {
|
||||||
r, err := categoryAndTagsCaches.GetCache(ctx, time.Second, ctx)
|
r, err := categoryAndTagsCaches.GetCache(ctx, time.Second, ctx)
|
||||||
logs.ErrPrintln(err, "get category err")
|
logs.ErrPrintln(err, "get category err")
|
||||||
|
@ -5,4 +5,5 @@ const (
|
|||||||
RecentPostsArgs = "RecentPostsArgs"
|
RecentPostsArgs = "RecentPostsArgs"
|
||||||
RecentCommentsArgs = "RecentCommentsArgs"
|
RecentCommentsArgs = "RecentCommentsArgs"
|
||||||
ArchiveArgs = "ArchiveArgs"
|
ArchiveArgs = "ArchiveArgs"
|
||||||
|
Categories = "Categories"
|
||||||
)
|
)
|
||||||
|
@ -10,6 +10,7 @@ import (
|
|||||||
"github.com/fthvgb1/wp-go/internal/plugins/wphandle"
|
"github.com/fthvgb1/wp-go/internal/plugins/wphandle"
|
||||||
"github.com/fthvgb1/wp-go/internal/theme/wp"
|
"github.com/fthvgb1/wp-go/internal/theme/wp"
|
||||||
"github.com/fthvgb1/wp-go/internal/theme/wp/components"
|
"github.com/fthvgb1/wp-go/internal/theme/wp/components"
|
||||||
|
"github.com/fthvgb1/wp-go/internal/theme/wp/components/widget"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -33,7 +34,7 @@ func Init(fs embed.FS) {
|
|||||||
logs.ErrPrintln(err, "解析colorscheme失败")
|
logs.ErrPrintln(err, "解析colorscheme失败")
|
||||||
}
|
}
|
||||||
|
|
||||||
var pipe = wp.HandlePipe(wp.Render, dispatch)
|
var pipe = wp.HandlePipe(wp.Render, widget.IsCategory, dispatch)
|
||||||
|
|
||||||
func Hook(h *wp.Handle) {
|
func Hook(h *wp.Handle) {
|
||||||
pipe(h)
|
pipe(h)
|
||||||
|
@ -12,6 +12,7 @@ import (
|
|||||||
"github.com/fthvgb1/wp-go/internal/theme/wp"
|
"github.com/fthvgb1/wp-go/internal/theme/wp"
|
||||||
"github.com/fthvgb1/wp-go/internal/wpconfig"
|
"github.com/fthvgb1/wp-go/internal/wpconfig"
|
||||||
"github.com/fthvgb1/wp-go/safety"
|
"github.com/fthvgb1/wp-go/safety"
|
||||||
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -31,6 +32,11 @@ var categoryConfig = func() safety.Var[map[any]any] {
|
|||||||
"{$after_title}": "</h2>",
|
"{$after_title}": "</h2>",
|
||||||
"{$before_sidebar}": "",
|
"{$before_sidebar}": "",
|
||||||
"{$after_sidebar}": "",
|
"{$after_sidebar}": "",
|
||||||
|
"{$class}": "postform",
|
||||||
|
"{$show_option_none}": "选择分类",
|
||||||
|
"{$name}": "cat",
|
||||||
|
"{$id}": "cat",
|
||||||
|
"{$required}": "",
|
||||||
"{$nav}": "",
|
"{$nav}": "",
|
||||||
"{$navCloser}": "",
|
"{$navCloser}": "",
|
||||||
"{$title}": "",
|
"{$title}": "",
|
||||||
@ -50,7 +56,7 @@ var categoryTemplate = `{$before_widget}
|
|||||||
`
|
`
|
||||||
|
|
||||||
func Category(h *wp.Handle) string {
|
func Category(h *wp.Handle) string {
|
||||||
args := wp.GetComponentsArgs(h, widgets.ArchiveArgs, categoryArgs.Load())
|
args := wp.GetComponentsArgs(h, widgets.Categories, categoryArgs.Load())
|
||||||
args = maps.FilterZeroMerge(categoryArgs.Load(), args)
|
args = maps.FilterZeroMerge(categoryArgs.Load(), args)
|
||||||
conf := wpconfig.GetPHPArrayVal("widget_categories", categoryConfig.Load(), int64(2))
|
conf := wpconfig.GetPHPArrayVal("widget_categories", categoryConfig.Load(), int64(2))
|
||||||
conf = maps.FilterZeroMerge(categoryConfig.Load(), conf)
|
conf = maps.FilterZeroMerge(categoryConfig.Load(), conf)
|
||||||
@ -59,6 +65,7 @@ func Category(h *wp.Handle) string {
|
|||||||
dropdown := conf["dropdown"].(int64)
|
dropdown := conf["dropdown"].(int64)
|
||||||
categories := cache.CategoriesTags(h.C, constraints.Category)
|
categories := cache.CategoriesTags(h.C, constraints.Category)
|
||||||
if dropdown == 1 {
|
if dropdown == 1 {
|
||||||
|
t = strings.ReplaceAll(t, "{$html}", categoryDropdown(h, args, conf, categories))
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
t = strings.ReplaceAll(t, "{$html}", categoryUL(h, args, conf, categories))
|
t = strings.ReplaceAll(t, "{$html}", categoryUL(h, args, conf, categories))
|
||||||
@ -87,3 +94,92 @@ func categoryUL(h *wp.Handle, args map[string]string, conf map[any]any, categori
|
|||||||
s.WriteString("</ul>")
|
s.WriteString("</ul>")
|
||||||
return s.String()
|
return s.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var categoryDropdownJs = `/* <![CDATA[ */
|
||||||
|
(function() {
|
||||||
|
var dropdown = document.getElementById( "%s" );
|
||||||
|
function onCatChange() {
|
||||||
|
if ( dropdown.options[ dropdown.selectedIndex ].value > 0 ) {
|
||||||
|
dropdown.parentNode.submit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
dropdown.onchange = onCatChange;
|
||||||
|
})();
|
||||||
|
/* ]]> */
|
||||||
|
`
|
||||||
|
|
||||||
|
func categoryDropdown(h *wp.Handle, args map[string]string, conf map[any]any, categories []models.TermsMy) string {
|
||||||
|
s := str.NewBuilder()
|
||||||
|
s.WriteString(`<form action="/" method="get">
|
||||||
|
`)
|
||||||
|
s.Sprintf(` <label class="screen-reader-text" for="%s">%s</label>
|
||||||
|
`, args["{$id}"], args["{$title}"])
|
||||||
|
if len(categories) > 0 {
|
||||||
|
s.Sprintf(` <select %s name="%s" id="%s" class="%s">
|
||||||
|
`, args["{$required}"], args["{$name}"], args["{$id}"], args["{$class}"])
|
||||||
|
s.Sprintf(` <option value="%[1]s">%[1]s</option>
|
||||||
|
`, args["{$show_option_none}"])
|
||||||
|
currentCategory := ""
|
||||||
|
if h.Scene() == constraints.Category {
|
||||||
|
currentCategory = h.Index.Param.Category
|
||||||
|
}
|
||||||
|
showCount := conf["count"].(int64)
|
||||||
|
for _, category := range categories {
|
||||||
|
selected := ""
|
||||||
|
if category.Name == currentCategory {
|
||||||
|
selected = "selected"
|
||||||
|
}
|
||||||
|
count := ""
|
||||||
|
if showCount != 0 {
|
||||||
|
count = fmt.Sprintf("(%d)", category.Count)
|
||||||
|
}
|
||||||
|
s.Sprintf(` <option %s value="%d">%s %s</option>
|
||||||
|
`, selected, category.Terms.TermId, category.Name, count)
|
||||||
|
}
|
||||||
|
s.WriteString(" </select>\n")
|
||||||
|
}
|
||||||
|
s.WriteString("</form>\n")
|
||||||
|
attr := ""
|
||||||
|
if !slice.IsContained(h.CommonThemeMods().ThemeSupport.HTML5, "script") {
|
||||||
|
attr = ` type="text/javascript"`
|
||||||
|
}
|
||||||
|
s.Sprintf(`<script%s>
|
||||||
|
`, attr)
|
||||||
|
s.Sprintf(categoryDropdownJs, args["{$id}"])
|
||||||
|
s.WriteString("</script>\n")
|
||||||
|
return s.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func IsCategory(next wp.HandleFn[*wp.Handle], h *wp.Handle) {
|
||||||
|
if h.Scene() != constraints.Home {
|
||||||
|
next(h)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
name, ok := parseDropdownCate(h)
|
||||||
|
if !ok {
|
||||||
|
next(h)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
h.C.Redirect(http.StatusMovedPermanently, fmt.Sprintf("/p/category/%s", name))
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseDropdownCate(h *wp.Handle) (cateName string, r bool) {
|
||||||
|
cate := wp.GetComponentsArgs[map[string]string](h, widgets.Categories, categoryArgs.Load())
|
||||||
|
name, ok := cate["{$name}"]
|
||||||
|
if !ok || name == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
cat := h.C.Query(name)
|
||||||
|
if cat == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
cates := slice.SimpleToMap(cache.CategoriesTags(h.C, constraints.Category), func(v models.TermsMy) uint64 {
|
||||||
|
return v.Terms.TermId
|
||||||
|
})
|
||||||
|
cc, r := cates[str.ToInteger[uint64](cat, 0)]
|
||||||
|
if !r {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
cateName = cc.Name
|
||||||
|
return
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user