Compare commits

...

2 Commits

Author SHA1 Message Date
decea28316 完善分类 2023-03-16 00:56:40 +08:00
f6e2f86ee7 完善分类下拉 2023-03-15 21:40:45 +08:00
3 changed files with 178 additions and 13 deletions

View File

@ -52,10 +52,10 @@
分为对列表页文章数据的修改的插件和对影响整个程序表现的插件 分为对列表页文章数据的修改的插件和对影响整个程序表现的插件
| 列表页文章数据插件 | 整个程序表现的插件 | | 列表页文章数据插件 | 整个程序表现的插件 |
|----------------------|----------------| |----------------------|--------------------------------------|
| passwordProject 密码保护 | enlighter 代码高亮 | | passwordProject 密码保护 | enlighter 代码高亮(需要在后台安装enlighterjs插件) |
| digest 自动生成指定长度的摘要 | | | digest 自动生成指定长度的摘要 | |
#### 其它 #### 其它

91
helper/tree/tree.go Normal file
View 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
}
}

View File

@ -5,6 +5,7 @@ import (
"github.com/fthvgb1/wp-go/helper/maps" "github.com/fthvgb1/wp-go/helper/maps"
"github.com/fthvgb1/wp-go/helper/slice" "github.com/fthvgb1/wp-go/helper/slice"
str "github.com/fthvgb1/wp-go/helper/strings" 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/cache"
"github.com/fthvgb1/wp-go/internal/pkg/constraints" "github.com/fthvgb1/wp-go/internal/pkg/constraints"
"github.com/fthvgb1/wp-go/internal/pkg/constraints/widgets" "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 := str.NewBuilder()
s.WriteString("<ul>\n") s.WriteString("<ul>\n")
isCount := conf["count"].(int64) 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 := "" count := ""
if isCount != 0 { if isCount != 0 {
count = fmt.Sprintf("(%d)", category.Count) count = fmt.Sprintf("(%d)", category.Count)
} }
s.Sprintf(` <li class="cat-item cat-item-%d"> var class []string
<a href="/p/category/%s">%s %s</a>
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> </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[ */ 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 currentCategory = h.Index.Param.Category
} }
showCount := conf["count"].(int64) 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("&nbsp;", deep*2)
selected := "" selected := ""
if category.Name == currentCategory { if category.Name == currentCategory {
selected = "selected" selected = "selected"
@ -133,8 +195,19 @@ func categoryDropdown(h *wp.Handle, args map[string]string, conf map[any]any, ca
if showCount != 0 { if showCount != 0 {
count = fmt.Sprintf("(%d)", category.Count) count = fmt.Sprintf("(%d)", category.Count)
} }
s.Sprintf(` <option %s value="%d">%s %s</option> s.Sprintf(` <option class="%s" %s value="%d">%s%s %s</option>
`, selected, category.Terms.TermId, category.Name, count) `, 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") s.WriteString(" </select>\n")
} }
@ -183,6 +256,7 @@ func parseDropdownCate(h *wp.Handle) (cateName string, r bool) {
if i < 0 { if i < 0 {
return return
} }
r = true
cateName = cc.Name cateName = cc.Name
return return
} }