完善分类下拉
This commit is contained in:
parent
cb1ce2e878
commit
f6e2f86ee7
|
@ -53,8 +53,8 @@
|
|||
分为对列表页文章数据的修改的插件和对影响整个程序表现的插件
|
||||
|
||||
| 列表页文章数据插件 | 整个程序表现的插件 |
|
||||
|----------------------|----------------|
|
||||
| passwordProject 密码保护 | enlighter 代码高亮 |
|
||||
|----------------------|--------------------------------------|
|
||||
| passwordProject 密码保护 | enlighter 代码高亮(需要在后台安装enlighterjs插件) |
|
||||
| digest 自动生成指定长度的摘要 | |
|
||||
|
||||
#### 其它
|
||||
|
|
68
helper/tree/tree.go
Normal file
68
helper/tree/tree.go
Normal file
|
@ -0,0 +1,68 @@
|
|||
package tree
|
||||
|
||||
import "github.com/fthvgb1/wp-go/helper/slice"
|
||||
|
||||
type Node[T any, K comparable] struct {
|
||||
Data T
|
||||
Children *[]Node[T, K]
|
||||
Parent K
|
||||
}
|
||||
|
||||
func (n *Node[T, K]) GetChildren() []T {
|
||||
return slice.Map(*n.Children, func(t Node[T, K]) T {
|
||||
return t.Data
|
||||
})
|
||||
}
|
||||
|
||||
func (n *Node[T, K]) ChildrenByOrder(fn func(T, T) bool) []T {
|
||||
a := slice.Map(*n.Children, func(t Node[T, K]) T {
|
||||
return t.Data
|
||||
})
|
||||
slice.Sort(a, fn)
|
||||
return a
|
||||
}
|
||||
|
||||
func (n *Node[T, K]) loop(fn func(T, int), deep int) {
|
||||
for _, nn := range *n.Children {
|
||||
fn(nn.Data, deep)
|
||||
if len(*nn.Children) > 0 {
|
||||
nn.loop(fn, deep+1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (n *Node[T, K]) Loop(fn func(T, int)) {
|
||||
n.loop(fn, 0)
|
||||
}
|
||||
func (n *Node[T, K]) orderByLoop(fn func(T, int), orderBy func(T, T) bool, deep int) {
|
||||
slice.Sort(*n.Children, func(i, j Node[T, K]) bool {
|
||||
return orderBy(i.Data, j.Data)
|
||||
})
|
||||
for _, nn := range *n.Children {
|
||||
fn(nn.Data, deep)
|
||||
if len(*nn.Children) > 0 {
|
||||
nn.orderByLoop(fn, orderBy, deep+1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (n *Node[T, K]) OrderByLoop(fn func(T, int), orderBy func(T, T) bool) {
|
||||
n.orderByLoop(fn, orderBy, 0)
|
||||
}
|
||||
|
||||
func Root[T any, K comparable](a []T, top K, fn func(T) (child, parent K)) *Node[T, K] {
|
||||
m := make(map[K]*Node[T, K])
|
||||
m[top] = &Node[T, K]{Children: new([]Node[T, K])}
|
||||
for _, t := range a {
|
||||
c, p := fn(t)
|
||||
node := Node[T, K]{Parent: p, Data: t, Children: new([]Node[T, K])}
|
||||
m[c] = &node
|
||||
parent, ok := m[p]
|
||||
if !ok {
|
||||
m[p] = &Node[T, K]{Children: new([]Node[T, K])}
|
||||
parent = m[p]
|
||||
}
|
||||
*parent.Children = append(*parent.Children, node)
|
||||
}
|
||||
return m[top]
|
||||
}
|
|
@ -5,6 +5,7 @@ import (
|
|||
"github.com/fthvgb1/wp-go/helper/maps"
|
||||
"github.com/fthvgb1/wp-go/helper/slice"
|
||||
str "github.com/fthvgb1/wp-go/helper/strings"
|
||||
"github.com/fthvgb1/wp-go/helper/tree"
|
||||
"github.com/fthvgb1/wp-go/internal/pkg/cache"
|
||||
"github.com/fthvgb1/wp-go/internal/pkg/constraints"
|
||||
"github.com/fthvgb1/wp-go/internal/pkg/constraints/widgets"
|
||||
|
@ -124,7 +125,9 @@ func categoryDropdown(h *wp.Handle, args map[string]string, conf map[any]any, ca
|
|||
currentCategory = h.Index.Param.Category
|
||||
}
|
||||
showCount := conf["count"].(int64)
|
||||
for _, category := range categories {
|
||||
fn := func(category models.TermsMy, deep int) {
|
||||
lv := fmt.Sprintf("level-%d", deep+1)
|
||||
sep := strings.Repeat(" ", deep*2)
|
||||
selected := ""
|
||||
if category.Name == currentCategory {
|
||||
selected = "selected"
|
||||
|
@ -133,8 +136,19 @@ func categoryDropdown(h *wp.Handle, args map[string]string, conf map[any]any, ca
|
|||
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.Sprintf(` <option class="%s" %s value="%d">%s%s %s</option>
|
||||
`, lv, selected, category.Terms.TermId, sep, category.Name, count)
|
||||
}
|
||||
if conf["hierarchical"].(int64) == 0 {
|
||||
for _, category := range categories {
|
||||
fn(category, 0)
|
||||
}
|
||||
} else {
|
||||
tree.Root(categories, 0, func(t models.TermsMy) (child, parent uint64) {
|
||||
return t.TermTaxonomyId, t.Parent
|
||||
}).Loop(func(category models.TermsMy, deep int) {
|
||||
fn(category, deep)
|
||||
})
|
||||
}
|
||||
s.WriteString(" </select>\n")
|
||||
}
|
||||
|
@ -183,6 +197,7 @@ func parseDropdownCate(h *wp.Handle) (cateName string, r bool) {
|
|||
if i < 0 {
|
||||
return
|
||||
}
|
||||
r = true
|
||||
cateName = cc.Name
|
||||
return
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user