github.com/linchen2chris/hugo@v0.0.0-20230307053224-cec209389705/hugolib/page__menus.go (about)

     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.Do(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.Do(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. initMenus will, once, build the
    46  	// site menus and update any relevant page.
    47  	p.p.s.init.menus.Do(context.Background())
    48  
    49  	return p.menus()
    50  }
    51  
    52  func (p *pageMenus) menus() navigation.PageMenus {
    53  	p.init()
    54  	return p.pm
    55  }
    56  
    57  func (p *pageMenus) init() {
    58  	p.pmInit.Do(func() {
    59  		p.q = navigation.NewMenuQueryProvider(
    60  			p,
    61  			p.p.s,
    62  			p.p,
    63  		)
    64  
    65  		var err error
    66  		p.pm, err = navigation.PageMenusFromPage(p.p)
    67  		if err != nil {
    68  			p.p.s.Log.Errorln(p.p.wrapError(err))
    69  		}
    70  	})
    71  }