Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
31 changes: 26 additions & 5 deletions assets/carousel/frontblocks-carousel.js
Original file line number Diff line number Diff line change
@@ -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) => {
Expand Down Expand Up @@ -127,7 +140,7 @@ window.addEventListener('load', function (event) {
const leftSvg = carouselArrowLeftUrl ? '<img src="' + carouselArrowLeftUrl + '" alt="" aria-hidden="true">' : defaultLeftSvg;
const rightSvg = carouselArrowRightUrl ? '<img src="' + carouselArrowRightUrl + '" alt="" aria-hidden="true">' : defaultRightSvg;

arrowsHTML = '<button class="glide__arrow glide__arrow--left" data-glide-dir="<"';
let arrowsHTML = '<button class="glide__arrow glide__arrow--left" data-glide-dir="<"';
arrowsHTML += ' aria-label="Previous slide"';
arrowsHTML += ' style="background-color: ' + carouselbuttonsBackgroundColor + '"';
arrowsHTML += '>' + leftSvg + '</button>';
Expand Down Expand Up @@ -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(() => {
Expand Down Expand Up @@ -202,4 +215,12 @@ window.addEventListener('load', function (event) {
}
});
}
});
};

if (document.readyState === 'complete') {
window.frontblocksCarousel.init();
} else {
window.addEventListener('load', function () {
window.frontblocksCarousel.init();
});
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
118 changes: 118 additions & 0 deletions tests/js/carousel-init-api.test.js
Original file line number Diff line number Diff line change
@@ -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'));
});
Loading