navbar.html 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 := in $current_page.RelPermalink .URL }}
  73. {{ $is_widget_page := or $current_page.IsHome (eq $current_page.Type "widget_page") }}
  74. {{ $hash := findRE "#(.+)" .URL }}
  75. {{ $is_same_page := $is_link_in_current_path }}
  76. {{ if gt (len $hash) 0 }}
  77. {{ $hash = index $hash 0 }}
  78. {{ $hash_removed := replace .URL $hash "" }}
  79. {{ if eq (len $hash_removed) 0 }}
  80. {{ $hash_removed = "/" }}{{/* Add robustness for `/#SECTION` or `#SECTION` in `menus.toml`. */}}
  81. {{ end }}
  82. {{ $is_same_page = eq (path.Dir $current_page.RelPermalink) (path.Dir ($hash_removed|relLangURL)) }}
  83. {{ end }}
  84. <li class="nav-item">
  85. <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 }}>
  86. {{- .Pre -}}<span>{{ .Name | safeHTML }}</span>{{- .Post -}}
  87. </a>
  88. </li>
  89. {{ end }}
  90. {{ end }}
  91. {{ if site.Menus.main_right | and (eq $align_menu "l") }}
  92. </ul>
  93. <ul class="navbar-nav ml-md-auto">
  94. {{ end }}
  95. {{ range site.Menus.main_right }}
  96. {{/* Set target for link. */}}
  97. {{ $.Scratch.Set "target" "" }}
  98. {{ if gt (len .URL) 4 }}
  99. {{ if eq "http" (slicestr .URL 0 4) }}
  100. {{ $.Scratch.Set "target" " target=\"_blank\" rel=\"noopener\"" }}
  101. {{ end }}
  102. {{ end }}
  103. <li class="nav-item">
  104. <a class="nav-link" href="{{ .URL | relLangURL }}"{{ if $.IsHome }} data-target="{{ .URL }}"{{ end }}{{ ($.Scratch.Get "target") | safeHTMLAttr }}>
  105. {{- .Pre -}}<span>{{ .Name | safeHTML }}</span>{{- .Post -}}
  106. </a>
  107. </li>
  108. {{ end }}
  109. </ul>
  110. </div><!-- /.navbar-collapse -->
  111. <ul class="nav-icons navbar-nav flex-row ml-auto d-flex pl-md-2">
  112. {{ if site.Params.search.engine }}
  113. <li class="nav-item">
  114. <a class="nav-link js-search" href="#" aria-label="{{ i18n "search" }}"><i class="fas fa-search" aria-hidden="true"></i></a>
  115. </li>
  116. {{ end }}
  117. {{ if site.Params.day_night }}
  118. <li class="nav-item dropdown theme-dropdown">
  119. <a href="#" class="nav-link js-theme-selector" data-toggle="dropdown" aria-haspopup="true">
  120. <i class="fas fa-palette" aria-hidden="true"></i>
  121. </a>
  122. <div class="dropdown-menu">
  123. <a href="#" class="dropdown-item js-set-theme-light">
  124. <span>{{ i18n "theme_light" | default "Light" }}</span>
  125. </a>
  126. <a href="#" class="dropdown-item js-set-theme-dark">
  127. <span>{{ i18n "theme_dark" | default "Dark" }}</span>
  128. </a>
  129. <a href="#" class="dropdown-item js-set-theme-auto">
  130. <span>{{ i18n "theme_auto" | default "Automatic" }}</span>
  131. </a>
  132. </div>
  133. </li>
  134. {{ end }}
  135. {{ if .IsTranslated }}
  136. <li class="nav-item dropdown i18n-dropdown">
  137. <a href="#" class="nav-link {{ if $show_current_language }}dropdown-toggle{{end}}" data-toggle="dropdown" aria-haspopup="true">
  138. <i class="fas fa-globe mr-1" aria-hidden="true"></i>
  139. {{- if $show_current_language -}}
  140. <span class="d-none d-lg-inline">{{ index site.Data.i18n.languages .Lang }}</span>
  141. {{- end -}}
  142. </a>
  143. <div class="dropdown-menu">
  144. <div class="dropdown-item dropdown-item-active">
  145. <span>{{ index site.Data.i18n.languages .Lang }}</span>
  146. </div>
  147. {{ range .Translations }}
  148. <a class="dropdown-item" href="{{ .Permalink }}"{{ if $.IsHome }} data-target="{{ .RelPermalink }}"{{ end }}>
  149. <span>{{ index site.Data.i18n.languages .Lang }}</span>
  150. </a>
  151. {{ end }}
  152. </div>
  153. </li>
  154. {{ end }}
  155. </ul>
  156. </div><!-- /.container -->
  157. </nav>
  158. {{end}}{{/* End show navbar. */}}