Core

/app/lib/menu/item.go (1.1 KB)

 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
package menu

import "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"`
Children Items `json:"children,omitempty"`
}

func (i *Item) AddChild(child *Item) {
i.Children = append(i.Children, child)
}

func (i *Item) Desc() string {
if i.Description != "" {
return i.Title + ": " + 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
})
}