diff --git a/layouts/shortcodes/snap-gallery.html b/layouts/shortcodes/snap-gallery.html index 5218e06..07c68e0 100644 --- a/layouts/shortcodes/snap-gallery.html +++ b/layouts/shortcodes/snap-gallery.html @@ -1,9 +1,8 @@ -{{/* TODO: Ensure CSS/JS is only loaded once */}} -{{- with resources.Get "scss/snap-gallery.scss" | toCSS }} - -{{- end }} +{{/* Count number of gallery on page */}} +{{- $.Page.Scratch.Add "gallerycount" 1 -}} +{{ $galno := $.Page.Scratch.Get "gallerycount" }} -{{/* Initialise variables */}} +{{/* Initialise variables holding image paths */}} {{ $imgs := slice }} {{/* Get/sanitise image paths */}} @@ -24,13 +23,13 @@
{{ range $i, $img := $imgs }}
- +
{{ end }}
{{/* The Modal/Lightbox */}} -
+
{{ range $i, $img := $imgs }}
{{ add $i 1 }} / {{ len $imgs }}
@@ -39,12 +38,10 @@ {{ end }} - × + × - - - {{/* - */}} + + {{/*
@@ -52,4 +49,11 @@
*/}}
+ +{{/* Load CSS/JS and make sure it is only loaded once */}} +{{- if eq ($.Page.Scratch.Get "gallerycount") 1 }} +{{- with resources.Get "scss/snap-gallery.scss" | toCSS }} + +{{- end }} +{{ end }} diff --git a/static/js/snap-gallery.js b/static/js/snap-gallery.js index 8c3820e..bd5dd80 100644 --- a/static/js/snap-gallery.js +++ b/static/js/snap-gallery.js @@ -1,29 +1,32 @@ // Open the Modal -function openModal() { - document.getElementsByClassName("snap-lightbox")[0].style.display = "block"; +function openModal(id) { + document.getElementById(baseid + id).style.display = "block"; } // Close the Modal -function closeModal() { - document.getElementsByClassName("snap-lightbox")[0].style.display = "none"; +function closeModal(id) { + document.getElementById(baseid + id).style.display = "none"; } +var baseid = "snap-lightbox-"; var slideIndex = 1; showSlides(slideIndex); + // Next/previous controls -function plusSlides(n) { - showSlides(slideIndex += n); +function plusSlides(id, n) { + showSlides(id, slideIndex += n); } // Thumbnail image controls -function currentSlide(n) { - showSlides(slideIndex = n); +function currentSlide(id, n) { + showSlides(id, slideIndex = n); } -function showSlides(n) { +function showSlides(id, n) { var i; - var slides = document.getElementsByClassName("snap-lightbox-inner"); + var lightbox = document.getElementById(baseid + id); + var slides = lightbox.querySelectorAll(".snap-lightbox-inner"); if (n > slides.length) { slideIndex = 1 } if (n < 1) { slideIndex = slides.length } for (i = 0; i < slides.length; i++) {