Fixing issue when two menu items have the same name.

This commit is contained in:
spf13 2014-05-14 18:01:13 -04:00
parent 4f75ec985d
commit a4a1e39a51
5 changed files with 43 additions and 29 deletions

View file

@ -20,7 +20,7 @@
<a href="#" class="dropdown-toggle" data-toggle="dropdown">{{.Name}} <b class="caret"></b></a>
<ul class="dropdown-menu">
{{ range .Children }}
<li{{if $currentNode.IsMenuCurrent "main" .Name}} class="active"{{end}}><a href="{{.Url}}"> {{ .Name }} </a> </li>
<li{{if $currentNode.IsMenuCurrent "main" . }} class="active"{{end}}><a href="{{.Url}}"> {{ .Name }} </a> </li>
{{ end }}
</ul>
{{else}}
@ -39,7 +39,7 @@
<a href="#" class="dropdown-toggle" data-toggle="dropdown">{{.Name}} <b class="caret"></b></a>
<ul class="dropdown-menu">
{{ range .Children }}
<li{{if $currentNode.IsMenuCurrent $menu .Name}} class="active"{{end}}><a href="{{.Url}}"> {{ .Name }} </a> </li>
<li{{if $currentNode.IsMenuCurrent $menu . }} class="active"{{end}}><a href="{{.Url}}"> {{ .Name }} </a> </li>
{{ end }}
</ul>
{{else}}

View file

@ -24,6 +24,7 @@ type MenuEntry struct {
Url string
Name string
Menu string
Identifier string
PreName string
PostName string
Weight int
@ -44,6 +45,27 @@ func (me *MenuEntry) HasChildren() bool {
return me.Children != nil
}
func (me *MenuEntry) KeyName() string {
if me.Identifier != "" {
return me.Identifier
}
return me.Name
}
func (me *MenuEntry) hopefullyUniqueId() string {
if me.Identifier != "" {
return me.Identifier
} else if me.Url != "" {
return me.Url
} else {
return me.Name
}
}
func (me *MenuEntry) IsEqual(inme *MenuEntry) bool {
return me.hopefullyUniqueId() == inme.hopefullyUniqueId() && me.Parent == inme.Parent
}
func (me *MenuEntry) MarshallMap(ime map[string]interface{}) {
for k, v := range ime {
loki := strings.ToLower(k)
@ -54,25 +76,14 @@ func (me *MenuEntry) MarshallMap(ime map[string]interface{}) {
me.Weight = cast.ToInt(v)
case "name":
me.Name = cast.ToString(v)
case "identifier":
me.Identifier = cast.ToString(v)
case "parent":
me.Parent = cast.ToString(v)
}
}
}
//func (me *MenuEntry) RelUrl() string {
//link, err := p.permalink()
//if err != nil {
//return "", err
//}
//link.Scheme = ""
//link.Host = ""
//link.User = nil
//link.Opaque = ""
//return link.String(), nil
//}
func (m Menu) Add(me *MenuEntry) Menu {
app := func(slice Menu, x ...*MenuEntry) Menu {
n := len(slice) + len(x)

View file

@ -39,7 +39,7 @@ func (n *Node) Now() time.Time {
func (n *Node) HasMenuCurrent(menu string, me *MenuEntry) bool {
return false
}
func (n *Node) IsMenuCurrent(menu string, name string) bool {
func (n *Node) IsMenuCurrent(menu string, me *MenuEntry) bool {
return false
}

View file

@ -405,7 +405,7 @@ func (page *Page) HasMenuCurrent(menu string, me *MenuEntry) bool {
if m, ok := menus[menu]; ok {
if me.HasChildren() {
for _, child := range me.Children {
if child.Name == m.Name {
if child.IsEqual(m) {
return true
}
}
@ -416,11 +416,11 @@ func (page *Page) HasMenuCurrent(menu string, me *MenuEntry) bool {
}
func (page *Page) IsMenuCurrent(menu string, name string) bool {
func (page *Page) IsMenuCurrent(menu string, inme *MenuEntry) bool {
menus := page.Menus()
if me, ok := menus[menu]; ok {
return me.Name == name
return me.IsEqual(inme)
}
return false

View file

@ -419,14 +419,17 @@ func (s *Site) assembleMenus() {
menuConfig := s.getMenusFromConfig()
for name, menu := range menuConfig {
for _, me := range *menu {
flat[twoD{name, me.Name}] = me
flat[twoD{name, me.KeyName()}] = me
}
}
//creating flat hash
for _, p := range s.Pages {
for name, me := range p.Menus() {
flat[twoD{name, me.Name}] = me
if _, ok := flat[twoD{name, me.KeyName()}]; ok {
jww.ERROR.Printf("Two or more menu items have the same name/identifier in %q Menu. Identified as %q.\n Rename or set a unique identifier. \n", name, me.KeyName())
}
flat[twoD{name, me.KeyName()}] = me
}
}