use partial for footer scripts

This commit is contained in:
Ben Goldsworthy 2025-06-04 17:54:51 +02:00
parent 7f421830c5
commit ed9e769b2d
No known key found for this signature in database
5 changed files with 125 additions and 78 deletions

View file

@ -32,45 +32,7 @@
{{ end }} {{ end }}
{{ define "footer-scripts" }} {{ define "footer-scripts" }}
{{ with .Params.locations }} {{- partialCached "single/footer-scripts.html" ( dict "pc" . "sc" $ ) . }}
<script>
var map = L.map('map').setView([55, -3], 13);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 19,
attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a>'
}).addTo(map);
var latLons = [];
{{ $locations := partialCached "util/get_location_leaf_nodes.html" $.Site.Data.locations -}}
{{- range . -}}
{{- with ( index $locations ( replaceRE `, [A-Z]+` "" . ) ) -}}
L.marker([{{ .lat }}, {{ .lon }}]).addTo(map);
latLons.push([{{ .lat }}, {{ .lon }}]);
{{- else -}}
{{- erroridf "missing-lat-lon" "Could not find lat-lon for %q (%q)" . $.File.Path -}}
{{- end -}}
{{- end }}
map.fitBounds(latLons);
</script>
{{ end }}
{{ with .Params.scripts }}
{{ if in . "charts" }}
{{ $chartsConfigJS := $.Page.Resources.GetMatch "charts-init" | fingerprint }}
<script
id="Charts-script"
src="/js/chart/chart.js"
></script>
<script src="/js/chartjs-plugin-annotation/dist/chartjs-plugin-annotation.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-adapter-date-fns/dist/chartjs-adapter-date-fns.bundle.min.js"></script>
<script
id="Charts-config-script"
src="{{ $chartsConfigJS.Permalink }}"
integrity="{{ $chartsConfigJS.Data.Integrity }}"
></script>
{{ end }}
{{ end }}
{{ end }} {{ end }}
{{ define "main-header" }} {{ define "main-header" }}

View file

@ -30,48 +30,11 @@
{{ end }} {{ end }}
{{ define "footer-scripts" }} {{ define "footer-scripts" }}
{{ with .Params.locations }} {{- partialCached "single/footer-scripts.html" ( dict "pc" . "sc" $ ) . -}}
<script>
var map = L.map('map').setView([55, -3], 13);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 19,
attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a>'
}).addTo(map);
var latLons = [];
{{ $locations := partialCached "util/get_location_leaf_nodes.html" $.Site.Data.locations -}}
{{- range . -}}
{{- with ( index $locations ( replaceRE `, [A-Z]+` "" . ) ) -}}
L.marker([{{ .lat }}, {{ .lon }}]).addTo(map);
latLons.push([{{ .lat }}, {{ .lon }}]);
{{- else -}}
{{- erroridf "missing-lat-lon" "Could not find lat-lon for %q (%q)" . $.File.Path -}}
{{- end -}}
{{- end }}
map.fitBounds(latLons);
if (map.getZoom() < 10) map.setZoom(10);
</script>
{{ end }}
{{ if $.HasShortcode "chart" }}
{{ $chartsConfigJS := $.Page.Resources.GetMatch "charts-init" | fingerprint }}
<script
id="Charts-script"
src="/js/chart/chart.js"
></script>
<script src="/js/chartjs-plugin-annotation/dist/chartjs-plugin-annotation.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-adapter-date-fns/dist/chartjs-adapter-date-fns.bundle.min.js"></script>
<script
id="Charts-config-script"
src="{{ $chartsConfigJS.Permalink }}"
integrity="{{ $chartsConfigJS.Data.Integrity }}"
></script>
{{ end }}
{{ end }} {{ end }}
{{ define "main-header" }} {{ define "main-header" }}
{{ partial "single/main-header.html" ( dict "pc" . "sc" $ ) }} {{- partial "single/main-header.html" ( dict "pc" . "sc" $ ) -}}
{{ end }} {{ end }}
{{ define "main-body" }} {{ define "main-body" }}

View file

@ -0,0 +1,30 @@
{{- /*
Renders charts on a post.
@params . Chart JS config script.
*/
-}}
<!-- Validation -->
{{- if ( not . ) -}}
{{- errorf "No config JS defined" -}}
{{- end -}}
<!-- Variable Assignment -->
{{- $chartsConfigJS := . -}}
<!-- Rendering -->
<script
id="Charts-script"
src="/js/chart/chart.js"
></script>
<script src="/js/chartjs-plugin-annotation/dist/chartjs-plugin-annotation.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-adapter-date-fns/dist/chartjs-adapter-date-fns.bundle.min.js"></script>
<script
id="Charts-config-script"
src="{{ $chartsConfigJS.Permalink }}"
integrity="{{ $chartsConfigJS.Data.Integrity }}"
></script>

View file

@ -0,0 +1,46 @@
{{- /*
Renders a post's locations on a map.
@params pc Page context.
@params locations_data Site data of locations.
@params locations Post locations.
*/
-}}
<!-- Validation -->
{{- if ( not ( isset . "pc" ) ) -}}
{{- errorf "No page context passed" -}}
{{- end -}}
{{- if ( not ( isset . "locations_data" ) ) -}}
{{- errorf "No location site data defined" -}}
{{- end -}}
{{- if ( not ( isset . "locations" ) ) -}}
{{- errorf "No post locations defined" -}}
{{- end -}}
<!-- Rendering -->
<script>
var map = L.map('map').setView([55, -3], 13);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 19,
attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a>'
}).addTo(map);
var latLons = [];
{{ $locations_leaf_nodes := partialCached "util/get_location_leaf_nodes.html" .locations_data -}}
{{- range .locations -}}
{{- with ( index $locations_leaf_nodes ( replaceRE `, [A-Z]+` "" . ) ) -}}
L.marker([{{ .lat }}, {{ .lon }}]).addTo(map);
latLons.push([{{ .lat }}, {{ .lon }}]);
{{- else -}}
{{- erroridf "missing-lat-lon" "Could not find lat-lon for %q" . -}}
{{- end -}}
{{- end }}
map.fitBounds(latLons);
</script>

View file

@ -0,0 +1,46 @@
{{- /*
Renders a single item's required footer JS scripts.
@params sc Site context.
@params pc Page context.
*/
-}}
<!-- Validation -->
{{- if ( not ( isset . "sc" ) ) -}}
{{- errorf "No site context passed" -}}
{{- end -}}
{{- if ( not ( isset . "pc" ) ) -}}
{{- errorf "No page context passed" -}}
{{- end -}}
{{- if ( and ( .pc.HasShortcode "chart" ) ( not ( .pc.Page.Resources.GetMatch "charts-init" ) ) ) -}}
{{- errorf "No 'chart-init' page resource defined (%q)" .pc.Page.File.Path -}}
{{- end -}}
<!-- Rendering -->
{{ if .pc.Params.locations }}
{{-
partialCached
"scripts/show_post_locations_on_map.html"
( dict
"pc" .pc
"locations_data" .sc.Site.Data.locations
"locations" .pc.Params.locations
)
.pc.Params.locations
-}}
{{ end }}
{{ if .pc.HasShortcode "chart" }}
{{- $configJs := ( .pc.Page.Resources.GetMatch "charts-init" | fingerprint ) -}}
{{-
partialCached
"scripts/charts.html"
$configJs
$configJs
-}}
{{ end }}