/
menu.go
69 lines (56 loc) · 1.2 KB
/
menu.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
package component
import (
"html/template"
"net/http"
"sync/atomic"
)
// Menuable is a component that provides a menu
type Menuable interface {
Component
Menu(r *http.Request) []MenuItem
}
type MenuItem struct {
Title string
Path template.URL
Active bool
Priority MenuPriority
replaceID uint64 // internal id used to replace an item
}
var dummyCounter uint64
// DummyMenuItem creates a new Dummy Menu Item to be replaced
func DummyMenuItem() MenuItem {
return MenuItem{
replaceID: atomic.AddUint64(&dummyCounter, 1),
}
}
// ReplaceWith replaces this MenuItem with a different MenuItem.
// This method returns true if an appropriate DummyMenuItem exists.
func (mi MenuItem) ReplaceWith(new MenuItem, items []MenuItem) bool {
if mi.replaceID == 0 {
// never replace non-dummy items
return false
}
for i, item := range items {
if mi.replaceID == item.replaceID {
items[i] = new
return true
}
}
return false
}
func MenuItemSort(a, b MenuItem) bool {
return a.Priority < b.Priority
}
type MenuPriority int
// Menu* indicates priorities of the menu
const (
MenuHome MenuPriority = iota
MenuNews
MenuResolver
MenuUser
MenuAdmin
MenuAuth
)
const (
SmallButton MenuPriority = -1
)