-
Notifications
You must be signed in to change notification settings - Fork 0
/
item.go
77 lines (66 loc) · 1.54 KB
/
item.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Package menu - Content managed by Project Forge, see [projectforge.md] for details.
package menu
import (
"strings"
"github.com/samber/lo"
)
var Separator = &Item{}
type Item struct {
Key string `json:"key"`
Title string `json:"title,omitempty"`
Description string `json:"description,omitempty"`
Badge string `json:"badge,omitempty"`
Icon string `json:"icon,omitempty"`
Route string `json:"route,omitempty"`
Warning string `json:"warning,omitempty"`
Children Items `json:"children,omitempty"`
}
func ItemFromString(bc string, dflt string) *Item {
icon := dflt
if icon == "" {
icon = "file"
}
if iconIdx := strings.Index(bc, "**"); iconIdx > 0 {
icon = bc[iconIdx+2:]
bc = bc[:iconIdx]
}
bcLink := ""
if bci := strings.Index(bc, "||"); bci > 0 {
bcLink = bc[bci+2:]
bc = bc[:bci]
}
return &Item{Key: bc, Title: bc, Icon: icon, Route: bcLink}
}
func (i *Item) AddChild(child *Item) {
i.Children = append(i.Children, child)
}
func (i *Item) Desc() string {
if i.Description != "" {
return i.Description
}
return i.Title
}
type Items []*Item
func (i Items) Get(key string) *Item {
return lo.FindOrElse(i, nil, func(item *Item) bool {
return item.Key == key
})
}
func (i Items) GetByPath(path []string) *Item {
if len(path) == 0 {
return nil
}
ret := i.Get(path[0])
if ret == nil {
return nil
}
if len(path) > 1 {
return ret.Children.GetByPath(path[1:])
}
return ret
}
func (i Items) Keys() []string {
return lo.Map(i, func(x *Item, _ int) string {
return x.Key
})
}