page__menus.go (1670B)
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 "sync"
18
19 "github.com/gohugoio/hugo/navigation"
20 )
21
22 type pageMenus struct {
23 p *pageState
24
25 q navigation.MenuQueryProvider
26
27 pmInit sync.Once
28 pm navigation.PageMenus
29 }
30
31 func (p *pageMenus) HasMenuCurrent(menuID string, me *navigation.MenuEntry) bool {
32 p.p.s.init.menus.Do()
33 p.init()
34 return p.q.HasMenuCurrent(menuID, me)
35 }
36
37 func (p *pageMenus) IsMenuCurrent(menuID string, inme *navigation.MenuEntry) bool {
38 p.p.s.init.menus.Do()
39 p.init()
40 return p.q.IsMenuCurrent(menuID, inme)
41 }
42
43 func (p *pageMenus) Menus() navigation.PageMenus {
44 // There is a reverse dependency here. initMenus will, once, build the
45 // site menus and update any relevant page.
46 p.p.s.init.menus.Do()
47
48 return p.menus()
49 }
50
51 func (p *pageMenus) menus() navigation.PageMenus {
52 p.init()
53 return p.pm
54 }
55
56 func (p *pageMenus) init() {
57 p.pmInit.Do(func() {
58 p.q = navigation.NewMenuQueryProvider(
59 p,
60 p.p.s,
61 p.p,
62 )
63
64 var err error
65 p.pm, err = navigation.PageMenusFromPage(p.p)
66 if err != nil {
67 p.p.s.Log.Errorln(p.p.wrapError(err))
68 }
69 })
70 }