| 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 hugolib |
| 15 |
|
| 16 |
import ( |
| 17 |
"context" |
| 18 |
"sync" |
| 19 |
|
| 20 |
"github.com/gohugoio/hugo/navigation" |
| 21 |
) |
| 22 |
|
| 23 |
type pageMenus struct { |
| 24 |
p *pageState |
| 25 |
|
| 26 |
q navigation.MenuQueryProvider |
| 27 |
|
| 28 |
pmInit sync.Once |
| 29 |
pm navigation.PageMenus |
| 30 |
} |
| 31 |
|
| 32 |
func (p *pageMenus) HasMenuCurrent(menuID string, me *navigation.MenuEntry) bool { |
| 33 |
_ = p.p.s.init.menus.Value(context.Background()) |
| 34 |
p.init() |
| 35 |
return p.q.HasMenuCurrent(menuID, me) |
| 36 |
} |
| 37 |
|
| 38 |
func (p *pageMenus) IsMenuCurrent(menuID string, inme *navigation.MenuEntry) bool { |
| 39 |
_ = p.p.s.init.menus.Value(context.Background()) |
| 40 |
p.init() |
| 41 |
return p.q.IsMenuCurrent(menuID, inme) |
| 42 |
} |
| 43 |
|
| 44 |
func (p *pageMenus) Menus() navigation.PageMenus { |
| 45 |
// There is a reverse dependency here. |
| 46 |
_ = p.p.s.init.menus.Value(context.Background()) |
| 47 |
return p.menus() |
| 48 |
} |
| 49 |
|
| 50 |
func (p *pageMenus) menus() navigation.PageMenus { |
| 51 |
p.init() |
| 52 |
return p.pm |
| 53 |
} |
| 54 |
|
| 55 |
func (p *pageMenus) init() { |
| 56 |
p.pmInit.Do(func() { |
| 57 |
p.q = navigation.NewMenuQueryProvider( |
| 58 |
p, |
| 59 |
p.p.s, |
| 60 |
p.p, |
| 61 |
) |
| 62 |
|
| 63 |
params := p.p.Params() |
| 64 |
|
| 65 |
var menus any |
| 66 |
var ok bool |
| 67 |
|
| 68 |
if p.p.m.pageConfig.Menus != nil { |
| 69 |
menus = p.p.m.pageConfig.Menus |
| 70 |
} else { |
| 71 |
menus, ok = params["menus"] |
| 72 |
if !ok { |
| 73 |
menus = params["menu"] |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
var err error |
| 78 |
p.pm, err = navigation.PageMenusFromPage(menus, p.p) |
| 79 |
if err != nil { |
| 80 |
p.p.s.Log.Errorln(p.p.wrapError(err)) |
| 81 |
} |
| 82 |
}) |
| 83 |
} |