2022-05-27 15:40:21 +01:00
|
|
|
<script data-no-instant>
|
|
|
|
function switchTheme(theme) {
|
|
|
|
switch (theme) {
|
|
|
|
case 'light':
|
|
|
|
document.body.classList.remove('dark');
|
|
|
|
break;
|
|
|
|
case 'dark':
|
|
|
|
document.body.classList.add('dark');
|
|
|
|
break;
|
|
|
|
// auto
|
|
|
|
default:
|
|
|
|
if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
|
2020-09-17 10:58:03 +01:00
|
|
|
document.body.classList.add('dark');
|
2022-05-27 15:40:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-26 16:49:05 +00:00
|
|
|
|
2022-05-27 15:40:21 +01:00
|
|
|
function isDarkTheme() {
|
|
|
|
return document.body.className.includes("dark");
|
|
|
|
}
|
2020-11-04 08:34:45 +00:00
|
|
|
|
2022-05-27 15:40:21 +01:00
|
|
|
function getPrefTheme() {
|
|
|
|
return localStorage.getItem("pref-theme");
|
|
|
|
}
|
|
|
|
|
|
|
|
function setPrefTheme(theme) {
|
|
|
|
switchTheme(theme)
|
|
|
|
localStorage.setItem("pref-theme", theme);
|
|
|
|
}
|
2020-10-26 16:49:05 +00:00
|
|
|
|
2022-06-01 10:14:46 +01:00
|
|
|
const toggleThemeCallbacks = {}
|
|
|
|
toggleThemeCallbacks['main'] = (isDark) => {
|
2022-05-27 19:10:33 +01:00
|
|
|
// console.log('window toggle-theme 1')
|
2022-05-27 15:40:21 +01:00
|
|
|
if (isDark) {
|
|
|
|
setPrefTheme('light');
|
|
|
|
} else {
|
|
|
|
setPrefTheme('dark');
|
|
|
|
}
|
2022-06-01 10:14:46 +01:00
|
|
|
}
|
2022-05-27 15:40:21 +01:00
|
|
|
|
|
|
|
// listen to set-theme event,
|
|
|
|
// because window is never changed by InstantClick,
|
|
|
|
// we add the listener to window to ensure the event is always received
|
|
|
|
window.addEventListener('toggle-theme', function() {
|
2022-05-27 19:10:33 +01:00
|
|
|
// console.log('window toggle-theme')
|
2022-05-27 15:40:21 +01:00
|
|
|
const isDark = isDarkTheme()
|
2022-06-01 10:14:46 +01:00
|
|
|
for (const key in toggleThemeCallbacks) {
|
|
|
|
toggleThemeCallbacks[key](isDark)
|
|
|
|
}
|
2022-05-27 15:40:21 +01:00
|
|
|
});
|
2022-06-01 10:42:42 +01:00
|
|
|
|
|
|
|
// this function is used as the event listener for toggle-theme button click
|
|
|
|
function toggleThemeListener() {
|
|
|
|
// console.log('click theme-toggle')
|
|
|
|
window.dispatchEvent(new CustomEvent('toggle-theme'));
|
|
|
|
}
|
|
|
|
|
2020-10-05 10:03:48 +01:00
|
|
|
</script>
|
2020-11-26 03:40:24 +00:00
|
|
|
<script>
|
2022-05-27 15:40:21 +01:00
|
|
|
// load theme, as early as possible
|
|
|
|
(function() {
|
|
|
|
const defaultTheme = '{{ site.Params.defaultTheme | default "light" }}';
|
|
|
|
const prefTheme = getPrefTheme();
|
|
|
|
const theme = prefTheme ? prefTheme : defaultTheme;
|
2020-11-26 03:40:24 +00:00
|
|
|
|
2022-05-27 15:40:21 +01:00
|
|
|
switchTheme(theme);
|
|
|
|
})();
|
2020-11-26 03:40:24 +00:00
|
|
|
</script>
|
2021-03-30 13:37:36 +01:00
|
|
|
|
2020-07-21 11:06:30 +01:00
|
|
|
<header class="header">
|
|
|
|
<nav class="nav">
|
2020-10-26 16:49:05 +00:00
|
|
|
<div class="logo">
|
2022-05-07 18:24:38 +01:00
|
|
|
{{- $label_text := (site.Params.logo.text | default site.Title) }}
|
2022-03-27 14:14:06 +01:00
|
|
|
{{- if site.Title }}
|
2022-05-04 18:25:37 +01:00
|
|
|
<a href="{{ "" | absURL }}" accesskey="h" title="{{ $label_text }} (Alt + H)">
|
2022-05-07 18:24:38 +01:00
|
|
|
{{- if site.Params.logo.icon }}
|
|
|
|
{{- $img := resources.Get site.Params.logo.icon }}
|
2022-03-27 15:47:49 +01:00
|
|
|
{{- if $img }}
|
|
|
|
{{- $processableFormats := (slice "jpg" "jpeg" "png" "tif" "bmp" "gif") -}}
|
|
|
|
{{- if hugo.IsExtended -}}
|
|
|
|
{{- $processableFormats = $processableFormats | append "webp" -}}
|
|
|
|
{{- end -}}
|
|
|
|
{{- $prod := (hugo.IsProduction | or (eq site.Params.env "production")) }}
|
|
|
|
{{- if and (in $processableFormats $img.MediaType.SubType) (eq $prod true)}}
|
2022-05-07 18:24:38 +01:00
|
|
|
{{- if site.Params.logo.iconHeight }}
|
|
|
|
{{- $img = $img.Resize (printf "x%d" site.Params.logo.iconHeight) }}
|
2022-03-27 15:47:49 +01:00
|
|
|
{{ else }}
|
|
|
|
{{- $img = $img.Resize "x30" }}
|
|
|
|
{{- end }}
|
|
|
|
{{- end }}
|
2022-03-27 17:52:28 +01:00
|
|
|
<img src="{{ $img.Permalink }}" alt="logo" aria-label="logo"
|
2022-05-07 18:24:38 +01:00
|
|
|
height="{{- site.Params.logo.iconHeight | default "30" -}}">
|
2022-03-27 15:47:49 +01:00
|
|
|
{{- else }}
|
2022-05-07 18:24:38 +01:00
|
|
|
<img src="{{- site.Params.logo.icon | absURL -}}" alt="logo" aria-label="logo"
|
|
|
|
height="{{- site.Params.logo.iconHeight | default "30" -}}">
|
2020-10-16 19:17:46 +01:00
|
|
|
{{- end -}}
|
2022-03-27 15:47:49 +01:00
|
|
|
{{- end -}}
|
2021-03-27 06:21:48 +00:00
|
|
|
{{- $label_text -}}
|
2020-10-16 19:17:46 +01:00
|
|
|
</a>
|
2021-01-31 11:35:40 +00:00
|
|
|
{{- end }}
|
2020-10-26 16:49:05 +00:00
|
|
|
<span class="logo-switches">
|
2022-03-27 14:14:06 +01:00
|
|
|
{{- if (not site.Params.disableThemeToggle) }}
|
2021-02-12 05:18:04 +00:00
|
|
|
<button id="theme-toggle" accesskey="t" title="(Alt + T)">
|
2021-01-31 11:35:40 +00:00
|
|
|
<svg id="moon" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"
|
|
|
|
fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"
|
|
|
|
stroke-linejoin="round">
|
|
|
|
<path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"></path>
|
|
|
|
</svg>
|
|
|
|
<svg id="sun" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"
|
|
|
|
fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"
|
|
|
|
stroke-linejoin="round">
|
|
|
|
<circle cx="12" cy="12" r="5"></circle>
|
|
|
|
<line x1="12" y1="1" x2="12" y2="3"></line>
|
|
|
|
<line x1="12" y1="21" x2="12" y2="23"></line>
|
|
|
|
<line x1="4.22" y1="4.22" x2="5.64" y2="5.64"></line>
|
|
|
|
<line x1="18.36" y1="18.36" x2="19.78" y2="19.78"></line>
|
|
|
|
<line x1="1" y1="12" x2="3" y2="12"></line>
|
|
|
|
<line x1="21" y1="12" x2="23" y2="12"></line>
|
|
|
|
<line x1="4.22" y1="19.78" x2="5.64" y2="18.36"></line>
|
|
|
|
<line x1="18.36" y1="5.64" x2="19.78" y2="4.22"></line>
|
|
|
|
</svg>
|
2021-02-12 05:18:04 +00:00
|
|
|
</button>
|
2021-03-30 13:37:36 +01:00
|
|
|
{{- end }}
|
2020-10-04 16:13:24 +01:00
|
|
|
</span>
|
2020-10-26 16:49:05 +00:00
|
|
|
</div>
|
2020-09-23 08:22:32 +01:00
|
|
|
{{- $currentPage := . }}
|
2021-04-29 14:32:59 +01:00
|
|
|
<ul id="menu">
|
2022-03-27 14:14:06 +01:00
|
|
|
{{- range site.Menus.main }}
|
2022-05-29 14:50:06 +01:00
|
|
|
{{- $menu_item_url := (cond (strings.HasSuffix .URL "/") .URL (printf "%s/" .URL)) | absURL }}
|
|
|
|
{{- $page_url:= $currentPage.Permalink }}
|
2022-03-27 14:14:06 +01:00
|
|
|
{{- $is_search := eq (site.GetPage .KeyName).Layout `search` }}
|
2020-07-21 11:06:30 +01:00
|
|
|
<li>
|
2022-05-29 14:50:06 +01:00
|
|
|
<a href="{{ $menu_item_url }}" title="{{ .Title | default .Name }} {{- cond $is_search (" (Alt + /)" | safeHTMLAttr) ("" | safeHTMLAttr ) }}"
|
2022-04-12 16:24:51 +01:00
|
|
|
{{- if $is_search -}}data-no-instant{{- end -}}
|
2022-04-13 10:23:43 +01:00
|
|
|
{{- cond $is_search (" accesskey=/" | safeHTMLAttr) ("" | safeHTMLAttr ) }}
|
2022-09-07 05:22:54 +01:00
|
|
|
{{- if strings.HasPrefix $page_url $menu_item_url }} class="active" {{- end }}
|
2022-04-14 09:27:34 +01:00
|
|
|
{{- if .Params.External }} target="_blank" {{- end }}
|
|
|
|
>
|
2022-04-13 10:23:43 +01:00
|
|
|
{{- .Pre }}
|
|
|
|
{{- .Name -}}
|
|
|
|
{{ .Post -}}
|
2022-04-14 09:27:34 +01:00
|
|
|
{{- if .Params.External }}<span class="external-link">{{ safeHTML (index $.Site.Data.svg "external-link") }}</span>{{- end }}
|
2020-09-20 14:33:43 +01:00
|
|
|
</a>
|
2020-07-21 11:06:30 +01:00
|
|
|
</li>
|
2021-03-30 13:37:36 +01:00
|
|
|
{{- end }}
|
2020-07-21 11:06:30 +01:00
|
|
|
</ul>
|
|
|
|
</nav>
|
2020-10-16 19:17:46 +01:00
|
|
|
</header>
|