navbar.html 6.9 KB

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