Files
hugo-snap-gallery/layouts/shortcodes/snap-gallery.html

69 lines
2.8 KiB
HTML
Raw Normal View History

{{/* Count number of gallery on page */}}
{{- $.Page.Scratch.Add "gallerycount" 1 -}}
2024-02-05 15:06:31 +01:00
{{- $galno := $.Page.Scratch.Get "gallerycount" -}}
2024-01-30 12:18:28 +01:00
{{/* Load CSS/JS and make sure it is only loaded once */}}
2024-02-05 15:06:31 +01:00
{{- if eq ($.Page.Scratch.Get "gallerycount") 1 -}}
{{- with resources.Get "scss/snap-gallery.scss" | toCSS -}}
<link rel="stylesheet" href="{{ .RelPermalink }}" crossorigin="anonymous">
2024-02-05 15:06:31 +01:00
{{- end -}}
<script src="/js/snap-gallery.js"></script>
2024-02-05 15:06:31 +01:00
{{- end -}}
{{/* Initialise index of this gallery */}}
<script>imageIndex[{{ $galno }}] = 1;</script>
{{/* Initialise variables holding image paths and extension */}}
2024-02-05 15:06:31 +01:00
{{- $imgs_collect := slice -}}
{{- $imgs := slice -}}
{{- $img_exts := slice ".jpg" ".jpeg" ".png" ".gif" ".webp" ".tiff" -}}
2024-01-30 12:18:28 +01:00
{{/* Get images from src Param, separated by comma */}}
2024-02-05 15:06:31 +01:00
{{- range (split .Params.src ",") -}}
{{- $img := (trim . " ") -}}
{{- $img_static := print "/static/" $img -}}
{{/* Only proceed when path exists */}}
2024-02-05 15:06:31 +01:00
{{- if os.FileExists $img_static -}}
{{/* If current item is a directory, range each of them, and add with full path to the slice */}}
2024-02-05 15:06:31 +01:00
{{- if (os.Stat $img_static).IsDir -}}
{{- range readDir $img_static -}}
2024-02-05 15:06:31 +01:00
{{- $imgs_collect = $imgs_collect | append (path.Join $img .Name ) -}}
{{- end -}}
{{/* If a single file, just add it to the slice */}}
2024-02-05 15:06:31 +01:00
{{- else -}}
{{- $imgs_collect = $imgs_collect | append $img -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{/* Filter collected files by whether they are an image. Throw others out */}}
2024-02-05 15:06:31 +01:00
{{- range $imgs_collect -}}
{{- if in $img_exts (lower (path.Ext .)) -}}
{{- $imgs = $imgs | append . -}}
{{- end -}}
{{- end -}}
2024-01-30 12:18:28 +01:00
{{/* Define cursor when hovering over images, depending on lightbox status */}}
2024-02-05 15:06:31 +01:00
{{- $lightbox := (default true .Params.lightbox) -}}
{{- $cursor := "auto" -}}
{{- if $lightbox }}{{ $cursor = "zoom-in" }}{{ end -}}
{{/* Visible images in separate modes */}}
2024-02-05 15:06:31 +01:00
{{- $mode := default "gallery" .Params.mode -}}
{{/* Gallery mode */}}
2024-02-05 15:06:31 +01:00
{{- if eq $mode "gallery" -}}
2024-02-05 15:33:43 +01:00
{{- partial "gallery" (dict "columns" .Params.columns "minwidth" .Params.minwidth "imgs" $imgs "galno" $galno "cursor" $cursor "aspectratio" .Params.aspectratio) -}}
{{/* Slideshow mode */}}
2024-02-05 15:06:31 +01:00
{{- else if eq $mode "slideshow" -}}
2024-02-05 15:33:43 +01:00
{{- partial "slideshow" (dict "imgs" $imgs "galno" $galno "width" .Params.slideshowwidth "cursor" $cursor "aspectratio" .Params.aspectratio) -}}
{{/* Set autorotate timer for slideshow, if configured (default: yes) */}}
2024-02-05 15:33:43 +01:00
{{- if (default true .Params.slideshowrotate) -}}
<script>const autoSlideshow = setInterval(moveSlideshowItem, {{ default 5000 .Params.slideshowrotate_timer }}, {{ $galno }}, 1, "auto");</script>
2024-02-05 15:06:31 +01:00
{{- end -}}
{{- end -}}
2024-01-30 12:18:28 +01:00
{{/* The Modal/Lightbox */}}
2024-02-05 15:06:31 +01:00
{{- if $lightbox -}}
{{- partial "lightbox" (dict "galno" $galno "imgs" $imgs) -}}
{{- end -}}