| 1 |
import {Core} from "../Core"; |
| 2 |
|
| 3 |
const adaptiveHeight = (slideshow, slide, splide) => { |
| 4 |
let options = splide.options; |
| 5 |
let currentlyActive = splide.index; |
| 6 |
if (slide.isWithin(parseInt(currentlyActive), parseInt(options.perPage) - 1)) { |
| 7 |
const allSlides = splide.Components.Elements.slides; |
| 8 |
let lastVisible = parseInt(currentlyActive) + parseInt(options.perPage); |
| 9 |
let visibleSlides = allSlides.slice(currentlyActive, lastVisible); |
| 10 |
let maxHeight = 0; |
| 11 |
// Need requestAnimationFrame, otherwise offsetHeight returns 0 for first photo |
| 12 |
requestAnimationFrame(() => { |
| 13 |
Array.prototype.forEach.call(visibleSlides, (visible) => { |
| 14 |
let visibleImage = visible.querySelector('img'); |
| 15 |
let offsetHeight = visibleImage.offsetHeight; |
| 16 |
if (visibleImage && offsetHeight > maxHeight) { |
| 17 |
maxHeight = offsetHeight; |
| 18 |
} |
| 19 |
}); |
| 20 |
slide.slide.style.height = `${maxHeight}px`; |
| 21 |
|
| 22 |
const splideTrack = slideshow.querySelector('.splide__track'); |
| 23 |
const splideTrackHeight = splideTrack ? splideTrack.offsetHeight : 0; |
| 24 |
|
| 25 |
if (maxHeight !== splideTrackHeight) { |
| 26 |
const splideList = slideshow.querySelector('.splide__list'); |
| 27 |
splideList.style.height = `${maxHeight}px`; |
| 28 |
splideTrack.style.height = `${maxHeight}px`; |
| 29 |
} |
| 30 |
}); |
| 31 |
} |
| 32 |
}; |
| 33 |
|
| 34 |
const fixedHeight = (slideshow, splideObj) => { |
| 35 |
let maxHeight = 0, |
| 36 |
maxAspect = 0, |
| 37 |
containerWidth = slideshow.offsetWidth, |
| 38 |
children = slideshow.querySelectorAll('.splide__slide img'); |
| 39 |
|
| 40 |
Array.prototype.forEach.call(children, (img) => { |
| 41 |
if (img.naturalHeight !== 0) { |
| 42 |
const childAspect = img.naturalWidth / img.naturalHeight; |
| 43 |
if (childAspect >= maxAspect) { |
| 44 |
maxAspect = childAspect; |
| 45 |
let heightFactor = img.naturalWidth > containerWidth ? (containerWidth / img.naturalWidth) : 1; |
| 46 |
const cols = parseInt(splideObj.options.perPage, 10); |
| 47 |
if (!isNaN(cols) && cols !== 0) { |
| 48 |
heightFactor = heightFactor / cols; |
| 49 |
} |
| 50 |
maxHeight = img.naturalHeight * heightFactor; |
| 51 |
} |
| 52 |
} |
| 53 |
}); |
| 54 |
|
| 55 |
Array.prototype.forEach.call(children, (img) => { |
| 56 |
img.style.height = maxHeight + 'px'; |
| 57 |
}); |
| 58 |
|
| 59 |
Array.prototype.forEach.call(slideshow.querySelectorAll('.splide__slide, .splide__list'), (slideOrList) => { |
| 60 |
slideOrList.style.height = maxHeight + 'px'; |
| 61 |
}); |
| 62 |
slideshow.style.height = maxHeight + 'px'; |
| 63 |
}; |
| 64 |
|
| 65 |
export const Slider = (slideshow) => { |
| 66 |
if (slideshow) { |
| 67 |
const content = slideshow.querySelector('.photonic-slideshow-content'); |
| 68 |
if (content) { |
| 69 |
Core.waitForImages(slideshow).then(() => { |
| 70 |
const idStr = '#' + slideshow.getAttribute('id'); |
| 71 |
|
| 72 |
let splideThumbs = document.querySelector(idStr + '-thumbs'); |
| 73 |
if (splideThumbs != null) { |
| 74 |
splideThumbs = new Splide(idStr + '-thumbs'); |
| 75 |
splideThumbs.mount(); |
| 76 |
} |
| 77 |
|
| 78 |
const splide = new Splide(idStr); |
| 79 |
splide.on('mounted resize', function (slide) { |
| 80 |
if (slideshow.classList.contains('photonic-slideshow-side-white') || slideshow.classList.contains('photonic-slideshow-start-next')) { |
| 81 |
fixedHeight(slideshow, splide); |
| 82 |
} |
| 83 |
}); |
| 84 |
|
| 85 |
splide.on('visible', function (slide) { |
| 86 |
if (slideshow.classList.contains('photonic-slideshow-adapt-height')) { |
| 87 |
adaptiveHeight(slideshow, slide, splide); |
| 88 |
} |
| 89 |
}); |
| 90 |
|
| 91 |
if (splideThumbs == null) { |
| 92 |
splide.mount(); |
| 93 |
} |
| 94 |
else { |
| 95 |
splide.sync(splideThumbs).mount(); |
| 96 |
} |
| 97 |
|
| 98 |
slideshow.querySelectorAll('img').forEach((img) => { |
| 99 |
img.style.display = 'inline'; |
| 100 |
}); |
| 101 |
}); |
| 102 |
} |
| 103 |
} |
| 104 |
}; |
| 105 |
|
| 106 |
export const initializeSliders = () => { |
| 107 |
const primarySliders = document.querySelectorAll('.photonic-slideshow'); |
| 108 |
if (typeof Splide != "undefined") { |
| 109 |
primarySliders.forEach((slideshow) => Slider(slideshow)); |
| 110 |
} |
| 111 |
else if (console !== undefined && primarySliders.length > 0) { |
| 112 |
console.error('Splide not found! Please ensure that the Splide script is available and loaded before Photonic.'); |
| 113 |
} |
| 114 |
}; |
| 115 |
|