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 = '';
@@ -168,7 +181,7 @@ window.addEventListener('load', function (event) {
const currentIndex = glideFrontBlocks.index;
const lastIndex = glideFrontBlocks.selector.querySelectorAll('.glide__slide').length;
- actualView = parseInt(currentIndex) + parseInt(carouselView);
+ const actualView = parseInt(currentIndex) + parseInt(carouselView);
if (actualView > lastIndex) {
setTimeout(() => {
@@ -202,4 +215,12 @@ window.addEventListener('load', function (event) {
}
});
}
-});
+};
+
+if (document.readyState === 'complete') {
+ window.frontblocksCarousel.init();
+} else {
+ window.addEventListener('load', function () {
+ window.frontblocksCarousel.init();
+ });
+}
diff --git a/package.json b/package.json
index f8daf1db..161557fd 100644
--- a/package.json
+++ b/package.json
@@ -64,6 +64,7 @@
"test:table-of-contents": "node --test tests/js/table-of-contents-frontend.test.js",
"test:accessibility-utils": "node --test tests/js/accessibility-utils.test.js",
"test:carousel-accessibility": "node --test tests/js/carousel-accessibility.test.js",
+ "test:carousel-init-api": "node --test tests/js/carousel-init-api.test.js",
"test:text-animation": "node --test tests/js/text-animation-inline-formatting.test.js",
"build:css": "postcss assets/admin/settings-src.css -o assets/admin/settings.css",
"watch:css": "postcss assets/admin/settings-src.css -o assets/admin/settings.css --watch",
diff --git a/tests/js/carousel-init-api.test.js b/tests/js/carousel-init-api.test.js
new file mode 100644
index 00000000..e81f7fd0
--- /dev/null
+++ b/tests/js/carousel-init-api.test.js
@@ -0,0 +1,118 @@
+const assert = require('node:assert/strict');
+const fs = require('node:fs');
+const path = require('node:path');
+const test = require('node:test');
+const vm = require('node:vm');
+
+const carouselScript = fs.readFileSync(path.resolve(__dirname, '../../assets/carousel/frontblocks-carousel.js'), 'utf8');
+
+function makeElement() {
+ const el = {
+ children: [],
+ classList: {
+ list: [],
+ add(...names) { names.forEach((n) => { if (!this.list.includes(n)) this.list.push(n); }); },
+ contains(name) { return this.list.includes(name); }
+ },
+ style: { setProperty() {} },
+ attributes: {},
+ setAttribute(name, value) { this.attributes[name] = String(value); },
+ getAttribute(name) { return Object.prototype.hasOwnProperty.call(this.attributes, name) ? this.attributes[name] : null; },
+ appendChild(child) { this.children.push(child); child.parentNode = el; return child; },
+ replaceChild(newChild, oldChild) {
+ const index = this.children.indexOf(oldChild);
+ if (index !== -1) this.children[index] = newChild;
+ newChild.parentNode = el;
+ return oldChild;
+ },
+ addEventListener() {},
+ querySelectorAll() { return []; }
+ };
+ return el;
+}
+
+function makeList() {
+ const list = makeElement();
+ list.classList.add('frontblocks-carousel');
+ list.appendChild(makeElement());
+ list.parentNode = makeElement();
+ return list;
+}
+
+/**
+ * Runs the script in a page that is still loading, so nothing starts until init() is called.
+ */
+function load({ readyState = 'loading', lists = [] } = {}) {
+ const mounted = [];
+ const loadListeners = [];
+
+ function Glide(el) {
+ mounted.push(el);
+ this.mount = () => {};
+ this.on = () => {};
+ }
+
+ const documentStub = {
+ readyState,
+ querySelectorAll: () => lists,
+ createElement: makeElement
+ };
+ const windowStub = {
+ innerWidth: 1200,
+ screen: { availWidth: 1200 },
+ matchMedia: () => ({ matches: false }),
+ addEventListener(type, cb) { if ('load' === type) loadListeners.push(cb); }
+ };
+
+ const context = { document: documentStub, window: windowStub, Glide, Math };
+ vm.createContext(context);
+ vm.runInContext(carouselScript, context);
+
+ return { api: windowStub.frontblocksCarousel, mounted, loadListeners };
+}
+
+test('exposes window.frontblocksCarousel.init', () => {
+ const { api } = load();
+
+ assert.equal(typeof api.init, 'function');
+});
+
+test('starts the lists on window load, as before', () => {
+ const { mounted, loadListeners } = load({ lists: [ makeList() ] });
+
+ assert.equal(mounted.length, 0);
+ loadListeners.forEach((cb) => cb());
+ assert.equal(mounted.length, 1);
+});
+
+test('starts at once when the script runs after the window has loaded', () => {
+ const { mounted } = load({ readyState: 'complete', lists: [ makeList() ] });
+
+ assert.equal(mounted.length, 1);
+});
+
+test('init starts lists added after load and never mounts one twice', () => {
+ const late = makeList();
+ const { api, mounted } = load({ readyState: 'complete', lists: [] });
+ const container = makeElement();
+ container.querySelectorAll = () => [ late ];
+
+ api.init(container);
+ api.init(container);
+
+ assert.equal(mounted.length, 1);
+ assert.ok(late.classList.contains('glide__slides'));
+});
+
+test('init(container) only looks inside the container', () => {
+ const outside = makeList();
+ const inside = makeList();
+ const { api, mounted } = load({ lists: [ outside ] });
+ const container = makeElement();
+ container.querySelectorAll = () => [ inside ];
+
+ api.init(container);
+
+ assert.equal(mounted.length, 1);
+ assert.ok(!outside.classList.contains('glide__slides'));
+});