Compare commits
2 Commits
cb1ce2e878
...
decea28316
Author | SHA1 | Date | |
---|---|---|---|
decea28316 | |||
f6e2f86ee7 |
|
@ -52,10 +52,10 @@
|
|||
|
||||
分为对列表页文章数据的修改的插件和对影响整个程序表现的插件
|
||||
|
||||
| 列表页文章数据插件 | 整个程序表现的插件 |
|
||||
|----------------------|----------------|
|
||||
| passwordProject 密码保护 | enlighter 代码高亮 |
|
||||
| digest 自动生成指定长度的摘要 | |
|
||||
| 列表页文章数据插件 | 整个程序表现的插件 |
|
||||
|----------------------|--------------------------------------|
|
||||
| passwordProject 密码保护 | enlighter 代码高亮(需要在后台安装enlighterjs插件) |
|
||||
| digest 自动生成指定长度的摘要 | |
|
||||
|
||||
#### 其它
|
||||
|
||||
|
|
91
helper/tree/tree.go
Normal file
91
helper/tree/tree.go
Normal file
|
@ -0,0 +1,91 @@
|
|||
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 := root(a, top, fn)
|
||||
return m[top]
|
||||
}
|
||||
|
||||
func Roots[T any, K comparable](a []T, top K, fn func(T) (child, parent K)) map[K]*Node[T, K] {
|
||||
return root(a, top, fn)
|
||||
}
|
||||
|
||||
func root[T any, K comparable](a []T, top K, fn func(T) (child, parent K)) map[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
|
||||
}
|
||||
|
||||
func Ancestor[T any, K comparable](root map[K]*Node[T, K], top K, child *Node[T, K]) *Node[T, K] {
|
||||
a := child
|
||||
for {
|
||||
if a.Parent == top {
|
||||
return a
|
||||
}
|
||||
aa, ok := root[a.Parent]
|
||||
if !ok {
|
||||
return a
|
||||
}
|
||||
a = aa
|
||||
}
|
||||
}
|
|
@ -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"
|
||||
|
@ -81,18 +82,77 @@ func categoryUL(h *wp.Handle, args map[string]string, conf map[any]any, categori
|
|||
s := str.NewBuilder()
|
||||
s.WriteString("<ul>\n")
|
||||
isCount := conf["count"].(int64)
|
||||
for _, category := range categories {
|
||||
if conf["hierarchical"].(int64) == 0 {
|
||||
for _, category := range categories {
|
||||
count := ""
|
||||
if isCount != 0 {
|
||||
count = fmt.Sprintf("(%d)", category.Count)
|
||||
}
|
||||
s.Sprintf(` <li class="cat-item cat-item-%d">
|
||||
<a href="/p/category/%s">%s %s</a>
|
||||
</li>
|
||||
`, category.Terms.TermId, category.Name, category.Name, count)
|
||||
}
|
||||
} else {
|
||||
|
||||
m := tree.Roots(categories, 0, func(cate models.TermsMy) (child, parent uint64) {
|
||||
return cate.TermTaxonomyId, cate.Parent
|
||||
})
|
||||
cate := &tree.Node[models.TermsMy, uint64]{Data: models.TermsMy{}}
|
||||
if h.Scene() == constraints.Category {
|
||||
cat := h.C.Param("category")
|
||||
i, ca := slice.SearchFirst(categories, func(my models.TermsMy) bool {
|
||||
return cat == my.Name
|
||||
})
|
||||
if i > 0 {
|
||||
cate = m[ca.TermTaxonomyId]
|
||||
}
|
||||
}
|
||||
|
||||
r := m[0]
|
||||
categoryLi(r, cate, tree.Ancestor(m, 0, cate), isCount, s)
|
||||
}
|
||||
|
||||
s.WriteString("</ul>")
|
||||
return s.String()
|
||||
}
|
||||
|
||||
func categoryLi(root *tree.Node[models.TermsMy, uint64], cate, roots *tree.Node[models.TermsMy, uint64], isCount int64, s *str.Builder) {
|
||||
for _, child := range *root.Children {
|
||||
category := child.Data
|
||||
count := ""
|
||||
if isCount != 0 {
|
||||
count = fmt.Sprintf("(%d)", category.Count)
|
||||
}
|
||||
s.Sprintf(` <li class="cat-item cat-item-%d">
|
||||
<a href="/p/category/%s">%s %s</a>
|
||||
var class []string
|
||||
|
||||
if len(*child.Children) > 0 && cate.Data.TermTaxonomyId > 0 {
|
||||
if category.TermTaxonomyId == cate.Parent {
|
||||
class = append(class, "current-cat-parent")
|
||||
}
|
||||
|
||||
if cate.Parent > 0 && category.TermTaxonomyId == roots.Data.TermTaxonomyId {
|
||||
class = append(class, "current-cat-ancestor")
|
||||
}
|
||||
}
|
||||
aria := ""
|
||||
if category.TermTaxonomyId == cate.Data.TermTaxonomyId {
|
||||
class = append(class, "current-cat")
|
||||
aria = `aria-current="page"`
|
||||
}
|
||||
s.Sprintf(` <li class="cat-item cat-item-%d %s">
|
||||
<a %s href="/p/category/%s">%s %s</a>
|
||||
</li>
|
||||
`, category.Terms.TermId, category.Name, category.Name, count)
|
||||
`, category.Terms.TermId, strings.Join(class, " "), aria, category.Name, category.Name, count)
|
||||
|
||||
if len(*child.Children) > 0 {
|
||||
s.WriteString(`<ul class="children">
|
||||
`)
|
||||
categoryLi(&child, cate, roots, isCount, s)
|
||||
s.WriteString(`</ul>`)
|
||||
}
|
||||
}
|
||||
s.WriteString("</ul>")
|
||||
return s.String()
|
||||
|
||||
}
|
||||
|
||||
var categoryDropdownJs = `/* <![CDATA[ */
|
||||
|
@ -124,7 +184,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 +195,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 +256,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