navbar.html 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. {{ $show_navbar := site.Params.main_menu.enable | default true }}
  2. {{ if $show_navbar }}
  3. {{ $current_page := . }}
  4. {{ $highlight_active_link := site.Params.main_menu.highlight_active_link | default true }}
  5. {{ $show_current_language := site.Params.main_menu.show_language | default false }}
  6. {{/* Get site logo. */}}
  7. {{ $show_logo := site.Params.main_menu.show_logo | default true }}
  8. {{ $has_logo := fileExists "assets/images/logo.png" | or (fileExists "assets/images/logo.svg") }}
  9. {{ $logo := "" }}
  10. {{ if $has_logo }}
  11. {{ $logo = (partial "functions/get_logo" (dict "constraint" "max_height" "size" 70)) }}
  12. {{ end }}
  13. <nav class="navbar navbar-expand-lg navbar-light compensate-for-scrollbar" id="navbar-main">
  14. <div class="container">
  15. {{if $show_logo}}
  16. <div class="d-none d-lg-inline-flex">
  17. <a class="navbar-brand" href="{{ "/" | relLangURL }}">
  18. {{- if $has_logo -}}
  19. <img src="{{ $logo.RelPermalink }}" alt="{{ site.Title }}">
  20. {{- else -}}
  21. {{- site.Title -}}
  22. {{- end -}}
  23. </a>
  24. </div>
  25. {{end}}
  26. {{ if site.Menus.main }}
  27. <button type="button" class="navbar-toggler" data-toggle="collapse"
  28. data-target="#navbar-content" aria-controls="navbar" aria-expanded="false" aria-label="{{ i18n "toggle_navigation" }}">
  29. <span><i class="fas fa-bars"></i></span>
  30. </button>
  31. {{ end }}
  32. {{if $show_logo}}
  33. <div class="navbar-brand-mobile-wrapper d-inline-flex d-lg-none">
  34. <a class="navbar-brand" href="{{ "/" | relLangURL }}">
  35. {{- if $has_logo -}}
  36. <img src="{{ $logo.RelPermalink }}" alt="{{ site.Title }}">
  37. {{- else -}}
  38. {{- site.Title -}}
  39. {{- end -}}
  40. </a>
  41. </div>
  42. {{end}}
  43. <!-- Collect the nav links, forms, and other content for toggling -->
  44. {{ $align_menu := site.Params.main_menu.align | default "l" }}
  45. <div class="navbar-collapse main-menu-item collapse {{ if eq $align_menu "c" }}justify-content-center{{ else if eq $align_menu "r" }}justify-content-end{{else}}justify-content-start{{ end }}" id="navbar-content">
  46. <!-- Left Nav Bar -->
  47. <ul class="navbar-nav d-md-inline-flex">
  48. {{ range site.Menus.main }}
  49. {{ if .HasChildren }}
  50. <li class="nav-item dropdown">
  51. <a href="#" class="nav-link dropdown-toggle" data-toggle="dropdown" aria-haspopup="true">
  52. {{- .Pre -}}<span>{{ .Name | safeHTML }}</span>{{- .Post -}}
  53. <span class="caret"></span>
  54. </a>
  55. <div class="dropdown-menu">
  56. {{ range .Children }}
  57. <a class="dropdown-item" href="{{ .URL | relLangURL }}"{{ if $.IsHome }} data-target="{{ .URL }}"{{ end }}>
  58. {{- .Pre -}}<span>{{ .Name | safeHTML }}</span>{{- .Post -}}
  59. </a>
  60. {{ end }}
  61. </div>
  62. </li>
  63. {{ else }}
  64. {{/* Set target for link. */}}
  65. {{ $.Scratch.Set "target" "" }}
  66. {{ if gt (len .URL) 4 }}
  67. {{ if eq "http" (slicestr .URL 0 4) }}
  68. {{ $.Scratch.Set "target" " target=\"_blank\" rel=\"noopener\"" }}
  69. {{ end }}
  70. {{ end }}
  71. {{/* Get active page. */}}
  72. {{ $is_link_in_current_path := false }}
  73. {{ $is_widget_page := or $current_page.IsHome (eq $current_page.Type "widget_page") }}
  74. {{ $is_same_page := false }}
  75. {{ $hash := findRE "#(.+)" .URL }}
  76. {{ if $current_page.IsHome | and (or (eq .URL "/") (eq .URL "")) }}
  77. {{ $is_link_in_current_path = true }}
  78. {{else}}
  79. {{ if gt (len .URL) 1 }}{{/* Ignore root URL */}}
  80. {{ $is_link_in_current_path = in $current_page.RelPermalink .URL }}
  81. {{ $is_same_page = $is_link_in_current_path }}
  82. {{end}}
  83. {{ if gt (len $hash) 0 }}
  84. {{ $hash = index $hash 0 }}
  85. {{ $hash_removed := replace .URL $hash "" }}
  86. {{ if eq (len $hash_removed) 0 }}
  87. {{ $hash_removed = "/" }}{{/* Add robustness for `/#SECTION` or `#SECTION` in `menus.toml`. */}}
  88. {{ end }}
  89. {{ $is_same_page = eq (path.Dir $current_page.RelPermalink) (path.Dir ($hash_removed|relLangURL)) }}
  90. {{ end }}
  91. {{end}}
  92. <li class="nav-item">
  93. <a class="nav-link {{if and $highlight_active_link $is_link_in_current_path }} active{{end}}" href="{{.URL | relLangURL}}"{{ if and $is_widget_page $is_same_page }} data-target="{{$hash}}"{{ end }}{{ ($.Scratch.Get "target") | safeHTMLAttr }}>
  94. {{- .Pre -}}<span>{{ .Name | safeHTML }}</span>{{- .Post -}}
  95. </a>
  96. </li>
  97. {{ end }}
  98. {{ end }}
  99. {{ if site.Menus.main_right | and (eq $align_menu "l") }}
  100. </ul>
  101. <ul class="navbar-nav ml-md-auto">
  102. {{ end }}
  103. {{ range site.Menus.main_right }}
  104. {{/* Set target for link. */}}
  105. {{ $.Scratch.Set "target" "" }}
  106. {{ if gt (len .URL) 4 }}
  107. {{ if eq "http" (slicestr .URL 0 4) }}
  108. {{ $.Scratch.Set "target" " target=\"_blank\" rel=\"noopener\"" }}
  109. {{ end }}
  110. {{ end }}
  111. <li class="nav-item">
  112. <a class="nav-link" href="{{ .URL | relLangURL }}"{{ if $.IsHome }} data-target="{{ .URL }}"{{ end }}{{ ($.Scratch.Get "target") | safeHTMLAttr }}>
  113. {{- .Pre -}}<span>{{ .Name | safeHTML }}</span>{{- .Post -}}
  114. </a>
  115. </li>
  116. {{ end }}
  117. </ul>
  118. </div><!-- /.navbar-collapse -->
  119. <ul class="nav-icons navbar-nav flex-row ml-auto d-flex pl-md-2">
  120. {{ $show_search := site.Params.main_menu.show_search | default true }}
  121. {{ if and site.Params.search.engine $show_search }}
  122. <li class="nav-item">
  123. <a class="nav-link js-search" href="#" aria-label="{{ i18n "search" }}"><i class="fas fa-search" aria-hidden="true"></i></a>
  124. </li>
  125. {{ end }}
  126. {{ $show_day_night := site.Params.main_menu.show_day_night | default true }}
  127. {{ if and site.Params.day_night $show_day_night }}
  128. <li class="nav-item dropdown theme-dropdown">
  129. <a href="#" class="nav-link" data-toggle="dropdown" aria-haspopup="true">
  130. <i class="fas fa-moon" aria-hidden="true"></i>
  131. </a>
  132. <div class="dropdown-menu">
  133. <a href="#" class="dropdown-item js-set-theme-light">
  134. <span>{{ i18n "theme_light" | default "Light" }}</span>
  135. </a>
  136. <a href="#" class="dropdown-item js-set-theme-dark">
  137. <span>{{ i18n "theme_dark" | default "Dark" }}</span>
  138. </a>
  139. <a href="#" class="dropdown-item js-set-theme-auto">
  140. <span>{{ i18n "theme_auto" | default "Automatic" }}</span>
  141. </a>
  142. </div>
  143. </li>
  144. {{ end }}
  145. {{ if .IsTranslated }}
  146. <li class="nav-item dropdown i18n-dropdown">
  147. <a href="#" class="nav-link {{ if $show_current_language }}dropdown-toggle{{end}}" data-toggle="dropdown" aria-haspopup="true">
  148. <i class="fas fa-globe mr-1" aria-hidden="true"></i>
  149. {{- if $show_current_language -}}
  150. <span class="d-none d-lg-inline">{{ index site.Data.i18n.languages .Lang }}</span>
  151. {{- end -}}
  152. </a>
  153. <div class="dropdown-menu">
  154. <div class="dropdown-item dropdown-item-active">
  155. <span>{{ index site.Data.i18n.languages .Lang }}</span>
  156. </div>
  157. {{ range .Translations }}
  158. <a class="dropdown-item" href="{{ .Permalink }}"{{ if $.IsHome }} data-target="{{ .RelPermalink }}"{{ end }}>
  159. <span>{{ index site.Data.i18n.languages .Lang }}</span>
  160. </a>
  161. {{ end }}
  162. </div>
  163. </li>
  164. {{ end }}
  165. </ul>
  166. </div><!-- /.container -->
  167. </nav>
  168. {{end}}{{/* End show navbar. */}}