| 1 |
// Copyright 2019 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package navigation |
| 15 |
|
| 16 |
import ( |
| 17 |
"fmt" |
| 18 |
|
| 19 |
"github.com/gohugoio/hugo/common/hmaps" |
| 20 |
"github.com/gohugoio/hugo/common/types" |
| 21 |
"github.com/mitchellh/mapstructure" |
| 22 |
|
| 23 |
"github.com/spf13/cast" |
| 24 |
) |
| 25 |
|
| 26 |
type PageMenusProvider interface { |
| 27 |
PageMenusGetter |
| 28 |
MenuQueryProvider |
| 29 |
} |
| 30 |
|
| 31 |
type PageMenusGetter interface { |
| 32 |
Menus() PageMenus |
| 33 |
} |
| 34 |
|
| 35 |
type MenusGetter interface { |
| 36 |
Menus() Menus |
| 37 |
} |
| 38 |
|
| 39 |
type MenuQueryProvider interface { |
| 40 |
HasMenuCurrent(menuID string, me *MenuEntry) bool |
| 41 |
IsMenuCurrent(menuID string, inme *MenuEntry) bool |
| 42 |
} |
| 43 |
|
| 44 |
func PageMenusFromPage(ms any, p Page) (PageMenus, error) { |
| 45 |
if ms == nil { |
| 46 |
return nil, nil |
| 47 |
} |
| 48 |
pm := PageMenus{} |
| 49 |
me := MenuEntry{} |
| 50 |
SetPageValues(&me, p) |
| 51 |
|
| 52 |
// Could be the name of the menu to attach it to |
| 53 |
mname, err := cast.ToStringE(ms) |
| 54 |
|
| 55 |
if err == nil { |
| 56 |
me.Menu = mname |
| 57 |
pm[mname] = &me |
| 58 |
return pm, nil |
| 59 |
} |
| 60 |
|
| 61 |
// Could be a slice of strings |
| 62 |
mnames, err := cast.ToStringSliceE(ms) |
| 63 |
|
| 64 |
if err == nil { |
| 65 |
for _, mname := range mnames { |
| 66 |
me.Menu = mname |
| 67 |
pm[mname] = &me |
| 68 |
} |
| 69 |
return pm, nil |
| 70 |
} |
| 71 |
|
| 72 |
wrapErr := func(err error) error { |
| 73 |
return fmt.Errorf("unable to process menus for page %q: %w", p.Path(), err) |
| 74 |
} |
| 75 |
|
| 76 |
// Could be a structured menu entry |
| 77 |
menus, err := hmaps.ToStringMapE(ms) |
| 78 |
if err != nil { |
| 79 |
return pm, wrapErr(err) |
| 80 |
} |
| 81 |
|
| 82 |
for name, menu := range menus { |
| 83 |
menuEntry := MenuEntry{Menu: name} |
| 84 |
if menu != nil { |
| 85 |
ime, err := hmaps.ToStringMapE(menu) |
| 86 |
if err != nil { |
| 87 |
return pm, wrapErr(err) |
| 88 |
} |
| 89 |
if err := mapstructure.WeakDecode(ime, &menuEntry.MenuConfig); err != nil { |
| 90 |
return pm, err |
| 91 |
} |
| 92 |
} |
| 93 |
SetPageValues(&menuEntry, p) |
| 94 |
pm[name] = &menuEntry |
| 95 |
} |
| 96 |
|
| 97 |
return pm, nil |
| 98 |
} |
| 99 |
|
| 100 |
func NewMenuQueryProvider( |
| 101 |
pagem PageMenusGetter, |
| 102 |
sitem MenusGetter, |
| 103 |
p Page, |
| 104 |
) MenuQueryProvider { |
| 105 |
return &pageMenus{ |
| 106 |
p: p, |
| 107 |
pagem: pagem, |
| 108 |
sitem: sitem, |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
type pageMenus struct { |
| 113 |
pagem PageMenusGetter |
| 114 |
sitem MenusGetter |
| 115 |
p Page |
| 116 |
} |
| 117 |
|
| 118 |
func (pm *pageMenus) HasMenuCurrent(menuID string, me *MenuEntry) bool { |
| 119 |
if !types.IsNil(me.Page) && me.Page.IsSection() { |
| 120 |
if ok := me.Page.IsAncestor(pm.p); ok { |
| 121 |
return true |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
if !me.HasChildren() { |
| 126 |
return false |
| 127 |
} |
| 128 |
|
| 129 |
menus := pm.pagem.Menus() |
| 130 |
|
| 131 |
if m, ok := menus[menuID]; ok { |
| 132 |
for _, child := range me.Children { |
| 133 |
if child.isEqual(m) { |
| 134 |
return true |
| 135 |
} |
| 136 |
if pm.HasMenuCurrent(menuID, child) { |
| 137 |
return true |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
if pm.p == nil { |
| 143 |
return false |
| 144 |
} |
| 145 |
|
| 146 |
for _, child := range me.Children { |
| 147 |
if child.isSamePage(pm.p) { |
| 148 |
return true |
| 149 |
} |
| 150 |
|
| 151 |
if pm.HasMenuCurrent(menuID, child) { |
| 152 |
return true |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
return false |
| 157 |
} |
| 158 |
|
| 159 |
func (pm *pageMenus) IsMenuCurrent(menuID string, inme *MenuEntry) bool { |
| 160 |
menus := pm.pagem.Menus() |
| 161 |
|
| 162 |
if me, ok := menus[menuID]; ok { |
| 163 |
if me.isEqual(inme) { |
| 164 |
return true |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
if pm.p == nil { |
| 169 |
return false |
| 170 |
} |
| 171 |
|
| 172 |
if !inme.isSamePage(pm.p) { |
| 173 |
return false |
| 174 |
} |
| 175 |
|
| 176 |
// This resource may be included in several menus. |
| 177 |
// Search for it to make sure that it is in the menu with the given menuId. |
| 178 |
if menu, ok := pm.sitem.Menus()[menuID]; ok { |
| 179 |
for _, menuEntry := range menu { |
| 180 |
if menuEntry.isSameResource(inme) { |
| 181 |
return true |
| 182 |
} |
| 183 |
|
| 184 |
descendantFound := pm.isSameAsDescendantMenu(inme, menuEntry) |
| 185 |
if descendantFound { |
| 186 |
return descendantFound |
| 187 |
} |
| 188 |
|
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
return false |
| 193 |
} |
| 194 |
|
| 195 |
func (pm *pageMenus) isSameAsDescendantMenu(inme *MenuEntry, parent *MenuEntry) bool { |
| 196 |
if parent.HasChildren() { |
| 197 |
for _, child := range parent.Children { |
| 198 |
if child.isSameResource(inme) { |
| 199 |
return true |
| 200 |
} |
| 201 |
descendantFound := pm.isSameAsDescendantMenu(inme, child) |
| 202 |
if descendantFound { |
| 203 |
return descendantFound |
| 204 |
} |
| 205 |
} |
| 206 |
} |
| 207 |
return false |
| 208 |
} |
| 209 |
|
| 210 |
var NopPageMenus = new(nopPageMenus) |
| 211 |
|
| 212 |
type nopPageMenus int |
| 213 |
|
| 214 |
func (m nopPageMenus) Menus() PageMenus { |
| 215 |
return PageMenus{} |
| 216 |
} |
| 217 |
|
| 218 |
func (m nopPageMenus) HasMenuCurrent(menuID string, me *MenuEntry) bool { |
| 219 |
return false |
| 220 |
} |
| 221 |
|
| 222 |
func (m nopPageMenus) IsMenuCurrent(menuID string, inme *MenuEntry) bool { |
| 223 |
return false |
| 224 |
} |