diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml index 936ab2ad..66408d1f 100644 --- a/.github/workflows/phpunit.yml +++ b/.github/workflows/phpunit.yml @@ -62,6 +62,7 @@ jobs: npm run test:table-of-contents npm run test:accessibility-utils npm run test:carousel-accessibility + npm run test:carousel-init-api npm run test:text-animation phpunit: diff --git a/assets/carousel/frontblocks-carousel.js b/assets/carousel/frontblocks-carousel.js index e793a18a..5d621e24 100644 --- a/assets/carousel/frontblocks-carousel.js +++ b/assets/carousel/frontblocks-carousel.js @@ -1,5 +1,18 @@ -window.addEventListener('load', function (event) { - const carouselItem = document.querySelectorAll('.frontblocks-carousel'); +/** + * FrontBlocks carousel. + * + * Starts every ".frontblocks-carousel" list on window "load". Code that adds lists later (AJAX, REST) can call + * window.frontblocksCarousel.init( container ) to start only the new lists inside "container" (default: the whole + * document). Lists that are already running are skipped, so calling it again is safe. + */ +window.frontblocksCarousel = window.frontblocksCarousel || {}; +window.frontblocksCarousel.init = function (root) { + const scope = root && root.querySelectorAll ? root : document; + // A running carousel has "glide__slides"; it is skipped so the list is never wrapped twice. + const carouselItem = Array.prototype.filter.call( + scope.querySelectorAll('.frontblocks-carousel'), + (el) => ! el.classList.contains('glide__slides') + ); if (carouselItem.length > 0) { carouselItem.forEach((item) => { @@ -127,7 +140,7 @@ window.addEventListener('load', function (event) { const leftSvg = carouselArrowLeftUrl ? '' : defaultLeftSvg; const rightSvg = carouselArrowRightUrl ? '' : defaultRightSvg; - arrowsHTML = '