Skip to content

Commit

Permalink
refactor: modular instantclick & fix a mistake
Browse files Browse the repository at this point in the history
  • Loading branch information
reuixiy committed Nov 21, 2019
1 parent 0679e3b commit 6a49a25
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 53 deletions.
Empty file.
2 changes: 1 addition & 1 deletion layouts/partials/head.html
Original file line number Diff line number Diff line change
Expand Up @@ -122,5 +122,5 @@
{{ partial "third-party/google-analytics.html" . }}
{{- end }}

{{ partial "custom/footer.html" . }}
{{ partial "custom/head.html" . }}
</head>
54 changes: 2 additions & 52 deletions layouts/partials/script.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,58 +31,8 @@
{{- end -}}

{{- if .Site.Params.enableInstantClick -}}
<script data-no-instant>InstantClick.init();</script>

{{ if .Site.Params.enableDarkMode -}}
{{- $themeColor := .Site.Params.themeColor -}}
{{- $themeColorDark := .Site.Params.themeColorDark }}
<script data-no-instant>
InstantClick.on('change', function () {
const theme = localStorage.getItem('theme');
if (theme === "dark") {
document.documentElement.setAttribute('data-theme', 'dark');
document.querySelector('meta[name="theme-color"]').setAttribute('content', '{{ $themeColorDark }}');
document.getElementById("theme-toggle").innerHTML = "🌙";

var els = [].slice.apply(document.getElementsByClassName("chroma"));
for (var i = 0; i < els.length; i++) {
els[i].className = els[i].className.replace(/ *\bchroma\b/g, "chroma-dark");
}
} else if (theme === "light") {
document.documentElement.setAttribute('data-theme', 'light');
document.querySelector('meta[name="theme-color"]').setAttribute('content', '{{ $themeColor }}');
document.getElementById("theme-toggle").innerHTML = "🌞";

var els = [].slice.apply(document.getElementsByClassName("chroma-dark"));
for (var i = 0; i < els.length; i++) {
els[i].className = els[i].className.replace(/ *\bchroma-dark\b/g, "chroma");
}
}
});
</script>
{{ end -}}

{{ if and .Site.Params.enableGoogleAnalytics (eq hugo.Environment "production") -}}
{{- $id := .Site.Params.trackingID -}}

{{ if eq .Site.Params.trackingCodeType "gtag" -}}
<script data-no-instant>
InstantClick.on('change', function () {
gtag('config', '{{ $id }}', {
'page_path': location.pathname + location.search
});
});
</script>
{{ end -}}

{{ if eq .Site.Params.trackingCodeType "analytics" -}}
<script data-no-instant>
InstantClick.on('change', function () {
ga('send', 'pageview', location.pathname + location.search);
});
</script>
{{ end -}}
{{ end -}}
{{ partial "third-party/instantclick.html" . }}
{{ partial "custom/instantclick.html" . }}
{{- end -}}

{{ if .Params.katex | default .Site.Params.enableKaTeX }}
Expand Down
52 changes: 52 additions & 0 deletions layouts/partials/third-party/instantclick.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<script data-no-instant>InstantClick.init();</script>

{{ if .Site.Params.enableDarkMode -}}
{{- $themeColor := .Site.Params.themeColor -}}
{{- $themeColorDark := .Site.Params.themeColorDark }}
<script data-no-instant>
InstantClick.on('change', function () {
const theme = localStorage.getItem('theme');
if (theme === "dark") {
document.documentElement.setAttribute('data-theme', 'dark');
document.querySelector('meta[name="theme-color"]').setAttribute('content', '{{ $themeColorDark }}');
document.getElementById("theme-toggle").innerHTML = "🌙";

var els = [].slice.apply(document.getElementsByClassName("chroma"));
for (var i = 0; i < els.length; i++) {
els[i].className = els[i].className.replace(/ *\bchroma\b/g, "chroma-dark");
}
} else if (theme === "light") {
document.documentElement.setAttribute('data-theme', 'light');
document.querySelector('meta[name="theme-color"]').setAttribute('content', '{{ $themeColor }}');
document.getElementById("theme-toggle").innerHTML = "🌞";

var els = [].slice.apply(document.getElementsByClassName("chroma-dark"));
for (var i = 0; i < els.length; i++) {
els[i].className = els[i].className.replace(/ *\bchroma-dark\b/g, "chroma");
}
}
});
</script>
{{ end -}}

{{ if and .Site.Params.enableGoogleAnalytics (eq hugo.Environment "production") -}}
{{- $id := .Site.Params.trackingID -}}

{{ if eq .Site.Params.trackingCodeType "gtag" -}}
<script data-no-instant>
InstantClick.on('change', function () {
gtag('config', '{{ $id }}', {
'page_path': location.pathname + location.search
});
});
</script>
{{ end -}}

{{ if eq .Site.Params.trackingCodeType "analytics" -}}
<script data-no-instant>
InstantClick.on('change', function () {
ga('send', 'pageview', location.pathname + location.search);
});
</script>
{{ end -}}
{{ end -}}

0 comments on commit 6a49a25

Please sign in to comment.