/
menu.go
96 lines (81 loc) · 2.14 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package view
import (
"strings"
// "github.com/ungerik/go-start/debug"
)
///////////////////////////////////////////////////////////////////////////////
// Menu
type Menu struct {
ViewBaseWithId
Class string
ItemClass string
ActiveItemClass string
BetweenItems string
Items []LinkModel
Reverse bool
}
func (self *Menu) Render(ctx *Context) (err error) {
ctx.Response.XML.OpenTag("ul")
ctx.Response.XML.AttribIfNotDefault("id", self.id)
ctx.Response.XML.AttribIfNotDefault("class", self.Class)
requestURL := ctx.Request.URLString()
// Find active item
activeIndex := -1
if self.ActiveItemClass != "" {
// First try exact URL match
for i := range self.Items {
url := self.Items[i].URL(ctx)
if url == requestURL {
activeIndex = i
break
}
}
// If no exact URL match is found, search for sub pages
if activeIndex == -1 {
for i := range self.Items {
url := self.Items[i].URL(ctx)
if strings.HasPrefix(requestURL, url) {
activeIndex = i
// todo
// not perfect, what if homepage matches first, but other matches better?
}
}
}
}
for i := range self.Items {
index := i
if self.Reverse {
index = len(self.Items) - 1 - i
}
itemClass := self.ItemClass
linkModel := self.Items[index]
url := linkModel.URL(ctx)
// use i instead of index
if i == activeIndex {
itemClass += " " + self.ActiveItemClass
}
ctx.Response.XML.OpenTag("li")
if self.id != "" {
ctx.Response.XML.Attrib("id", self.id, "_", index)
}
ctx.Response.XML.AttribIfNotDefault("class", itemClass)
if i > 0 && self.BetweenItems != "" {
ctx.Response.XML.Content(self.BetweenItems)
}
ctx.Response.XML.OpenTag("a")
ctx.Response.XML.Attrib("href", url)
ctx.Response.XML.AttribIfNotDefault("title", linkModel.LinkTitle(ctx))
ctx.Response.XML.AttribIfNotDefault("rel", linkModel.LinkRel(ctx))
content := linkModel.LinkContent(ctx)
if content != nil {
err = content.Render(ctx)
if err != nil {
return err
}
}
ctx.Response.XML.CloseTagAlways() // a
ctx.Response.XML.CloseTagAlways() // li
}
ctx.Response.XML.CloseTagAlways() // ul
return nil
}