menu: optimize performance #685

This commit is contained in:
Sören Weber 2024-09-08 13:14:34 +02:00
parent 07cd94cc09
commit d2c86cc8da
No known key found for this signature in database
GPG key ID: BEC6D55545451B6D
3 changed files with 48 additions and 49 deletions

View file

@ -1 +1 @@
{{- return .Scratch.Get "relearnIsHiddenNode" }}
{{- return or (.Params.hidden) (eq .Title "") }}

View file

@ -26,7 +26,7 @@
{{- $pages := partialCached "_relearn/pages.gotmpl" (dict "page" .Site.Home) .Site.Home.Path }}
{{- $defaultAlwaysopen := .Site.Params.alwaysopen | default false }}
{{- range $pages }}
{{- template "section-tree-nav" dict "sect" . "currentnode" $currentNode "showvisitedlinks" $showvisitedlinks "alwaysopen" $defaultAlwaysopen }}
{{- partial "inline/menu-walker" (dict "sect" . "currentnode" $currentNode "showvisitedlinks" $showvisitedlinks "alwaysopen" $defaultAlwaysopen) }}
{{- end }}
</ul>
</div>
@ -113,49 +113,47 @@
</div>
</div>
</aside>
{{- define "section-tree-nav" }}
{{- $currentNode := .currentnode }}
{{- $showvisitedlinks := .showvisitedlinks }}
{{- $alwaysopen := .alwaysopen }}
{{- $currentFileRelPermalink := .currentnode.RelPermalink }}
{{- with .sect }}
{{- $isSelf := eq .RelPermalink $currentFileRelPermalink }}
{{- $isAncestor := and (not $isSelf) (.IsAncestor $currentNode) }}
{{- $isActive := $isSelf }}
{{- $pages := partialCached "_relearn/pages.gotmpl" (dict "page" .) .Path }}
{{- $isHidden := or (.Params.hidden) (eq .Title "") }}
{{- $relearnIsHiddenFrom := index ($currentNode.Scratch.Get "relearnIsHiddenFrom") .RelPermalink }}
{{- $hidden := and $relearnIsHiddenFrom (not $.showhidden) (not $isSelf) (not $isAncestor) }}
{{- if $hidden }}
{{- else }}
{{- $numberOfVisibleChildren := 0 }}
{{- range $pages }}
{{- $isSelfSub := eq .RelPermalink $currentFileRelPermalink }}
{{- $isAncestorSub := and (not $isSelf) (.IsAncestor $currentNode) }}
{{- $relearnIsSubHiddenFrom := index ($currentNode.Scratch.Get "relearnIsHiddenFrom") .RelPermalink }}
{{- $subHidden := and $relearnIsSubHiddenFrom (not $.showhidden) (not $isSelfSub) (not $isAncestorSub) }}
{{- $numberOfVisibleChildren = add $numberOfVisibleChildren (int (not $subHidden)) }}
{{- end }}
{{- $title := partial "title.gotmpl" (dict "page" . "linkTitle" true) }}
{{- $url := partial "permalink.gotmpl" (dict "to" .) }}
{{- safeHTML .Params.head }}
{{- if $numberOfVisibleChildren }}
{{- $isCollapsible := or (.Params.collapsibleMenu | default .Site.Params.collapsibleMenu) (not $url) }}
{{- $currentAlwaysopen := .Params.alwaysopen | default $alwaysopen }}
{{- $pageHash := md5 .Page }}
{{- $isOpen := or $currentAlwaysopen $isSelf $isAncestor }}
<li class="{{if $isHidden }}hidden {{end}}{{if $isActive }}active {{end}}{{if (or $isSelf $isAncestor) }}parent {{end}}{{if $currentAlwaysopen}}alwaysopen {{end}}" data-nav-id="{{ $url }}">{{ if $isCollapsible }}<input type="checkbox" id="R-section-{{ $pageHash }}" aria-controls="R-subsections-{{ $pageHash }}"{{ if $isOpen }} checked{{ end }}><label for="R-section-{{ $pageHash }}"><i class="fa-fw fas fa-chevron-down"></i><i class="fa-fw fas fa-chevron-right"></i><span class="a11y-only">{{ T "Submenu" $title }}</span></label>{{ end }}{{ if $url }}<a class="padding" href="{{ $url }}">{{ else }}<span class="padding">{{ end }}
{{- partial "menu-pre.html" . }}{{ $title }}{{ partial "menu-post.html" . }}
{{- if $url }}{{ if $showvisitedlinks }}<i class="fa-fw fas fa-check read-icon"></i>{{ end }}</a>{{ else }}</span>{{ end }}<ul id="R-subsections-{{ $pageHash }}" class="collapsible-menu">
{{- $defaultAlwaysopen := .Site.Params.alwaysopen | default true }}
{{- range $pages }}
{{- template "section-tree-nav" dict "sect" . "currentnode" $currentNode "showvisitedlinks" $showvisitedlinks "alwaysopen" $defaultAlwaysopen }}
{{- end }}</ul></li>
{{- else }}
<li class="{{if $isHidden }}hidden {{end}}{{if $isActive }}active {{end}}" data-nav-id="{{ $url }}">{{ if $url }}<a class="padding" href="{{ $url }}">{{ else }}<span class="padding">{{ end }}
{{- partial "menu-pre.html" . }}{{ $title }}{{ partial "menu-post.html" . }}
{{- if $url }}{{ if $showvisitedlinks }}<i class="fa-fw fas fa-check read-icon"></i>{{ end }}</a>{{ else }}</span>{{ end }}</li>
{{- end }}
{{- define "partials/inline/menu-walker" }}
{{- $currentNode := .currentnode }}
{{- $showvisitedlinks := .showvisitedlinks }}
{{- $alwaysopen := .alwaysopen }}
{{- $currentFileRelPermalink := .currentnode.RelPermalink }}
{{- with .sect }}
{{- $isSelf := eq .RelPermalink $currentFileRelPermalink }}
{{- $isAncestor := and (not $isSelf) (.IsAncestor $currentNode) }}
{{- $isHidden := or (.Params.hidden) (eq .Title "") }}
{{- if or $isAncestor $isSelf (not $isHidden) }}
{{- $isActive := $isSelf }}
{{- $pages := partialCached "_relearn/pages.gotmpl" (dict "page" .) .Path }}
{{- $hasVisibleChildren := false }}
{{- range $pages }}
{{- $isSubSelf := eq . $currentNode }}
{{- $isSubAncestor := and (not $isSubSelf) (.IsAncestor $currentNode) }}
{{- $isSubHidden := or (.Params.hidden) (eq .Title "") }}
{{- if or $isSubAncestor $isSubSelf (not $isSubHidden) }}
{{- $hasVisibleChildren = true }}
{{- break }}
{{- end }}
{{- end }}
{{- end }}
{{- $title := partial "title.gotmpl" (dict "page" . "linkTitle" true) }}
{{- $url := partial "permalink.gotmpl" (dict "to" .) }}
{{- if $hasVisibleChildren }}
{{- $isCollapsible := or (.Params.collapsibleMenu | default .Site.Params.collapsibleMenu) (not $url) }}
{{- $currentAlwaysopen := .Params.alwaysopen | default $alwaysopen }}
{{- $pageId := md5 .Page }}
{{- $isOpen := or $currentAlwaysopen $isSelf $isAncestor }}
<li class="{{if $isHidden }}hidden {{end}}{{if $isActive }}active {{end}}{{if (or $isSelf $isAncestor) }}parent {{end}}{{if $currentAlwaysopen}}alwaysopen {{end}}" data-nav-id="{{ $url }}">{{ if $isCollapsible }}<input type="checkbox" id="R-section-{{ $pageId }}" aria-controls="R-subsections-{{ $pageId }}"{{ if $isOpen }} checked{{ end }}><label for="R-section-{{ $pageId }}"><i class="fa-fw fas fa-chevron-down"></i><i class="fa-fw fas fa-chevron-right"></i><span class="a11y-only">{{ T "Submenu" $title }}</span></label>{{ end }}{{ if $url }}<a class="padding" href="{{ $url }}">{{ else }}<span class="padding">{{ end }}
{{- partial "menu-pre.html" . }}{{ $title }}{{ partial "menu-post.html" . }}
{{- if $url }}{{ if $showvisitedlinks }}<i class="fa-fw fas fa-check read-icon"></i>{{ end }}</a>{{ else }}</span>{{ end }}<ul id="R-subsections-{{ $pageId }}" class="collapsible-menu">
{{- $defaultAlwaysopen := .Site.Params.alwaysopen | default true }}
{{- range $pages }}
{{- partial "inline/menu-walker" (dict "sect" . "currentnode" $currentNode "showvisitedlinks" $showvisitedlinks "alwaysopen" $defaultAlwaysopen) }}
{{- end }}</ul></li>
{{- else }}
<li class="{{if $isHidden }}hidden {{end}}{{if $isActive }}active {{end}}" data-nav-id="{{ $url }}">{{ if $url }}<a class="padding" href="{{ $url }}">{{ else }}<span class="padding">{{ end }}
{{- partial "menu-pre.html" . }}{{ $title }}{{ partial "menu-post.html" . }}
{{- if $url }}{{ if $showvisitedlinks }}<i class="fa-fw fas fa-check read-icon"></i>{{ end }}</a>{{ else }}</span>{{ end }}</li>
{{- end }}
{{- end }}
{{- end }}
{{- end }}

View file

@ -15,7 +15,6 @@ section: {{- .Section }}
{{- $currentNode.Scratch.Delete "relearnNextPage" }}
{{- $currentNode.Scratch.Delete "relearnIsHiddenNode" }}{{/* the node itself is flagged as hidden */}}
{{- $currentNode.Scratch.Delete "relearnIsHiddenStem" }}{{/* the node or one of its parents is flagged as hidden */}}
{{- $currentNode.Scratch.Delete "relearnIsHiddenFrom" }}{{/* the node is hidden from the current page */}}
{{- template "relearn-structure" dict "node" .Site.Home "currentnode" $currentNode "hiddenstem" false "hiddencurrent" false }}
{{- if not ($currentNode.Scratch.Get "relearnIsSelfFound") }}
{{- if not $currentNode.IsHome }}
@ -39,13 +38,11 @@ section: {{- .Section }}
{{- $hidden_node := or (.node.Params.hidden) (eq .node.Title "") }}
{{- $hidden_stem := or $hidden_node .hiddenstem }}
{{- $hidden_current_stem := or $hidden_node .hiddencurrent }}
{{- $hidden_from_current := or (and $hidden_node (not $isAncestor) (not $isSelf) ) (and .hiddencurrent (or $isPreSelf $isPostSelf $isDescendant) ) }}
{{- if $isSelf }}
{{- $currentNode.Scratch.Set "relearnIsHiddenNode" $hidden_node }}
{{- $currentNode.Scratch.Set "relearnIsHiddenStem" $hidden_stem }}
{{- end }}
{{- $currentNode.Scratch.SetInMap "relearnIsHiddenFrom" .node.RelPermalink $hidden_current_stem }}
{{- if or (eq $currentNode.Kind "home") (eq $currentNode.Kind "section") (eq $currentNode.Kind "page") }}
{{- if not $hidden_from_current }}
@ -53,6 +50,7 @@ section: {{- .Section }}
{{- $currentNode.Scratch.Set "relearnPrevPage" .node }}
{{- else if and $isPostSelf .node.RelPermalink (eq ($currentNode.Scratch.Get "relearnNextPage") nil) }}
{{- $currentNode.Scratch.Set "relearnNextPage" .node }}
{{- return }}
{{- end }}
{{- end }}
{{- end }}
@ -60,5 +58,8 @@ section: {{- .Section }}
{{- $pages := partialCached "_relearn/pages.gotmpl" (dict "page" .node) .node.Path }}
{{- range $pages }}
{{- template "relearn-structure" dict "node" . "currentnode" $currentNode "hiddenstem" $hidden_stem "hiddencurrent" $hidden_from_current }}
{{- if $currentNode.Scratch.Get "relearnNextPage" }}
{{- return }}
{{- end }}
{{- end }}
{{- end }}