237 lines
7.4 KiB
JavaScript
237 lines
7.4 KiB
JavaScript
( function () {
|
|
'use strict';
|
|
|
|
let cleanupCurrent = null;
|
|
|
|
function validMediaQuery( media ) {
|
|
return Boolean(
|
|
media &&
|
|
typeof media.matches === 'boolean' &&
|
|
typeof media.addEventListener === 'function' &&
|
|
typeof media.removeEventListener === 'function',
|
|
);
|
|
}
|
|
|
|
function mediaQuery( provided, win, query ) {
|
|
if ( provided !== undefined ) {
|
|
return provided;
|
|
}
|
|
if ( ! win || typeof win.matchMedia !== 'function' ) {
|
|
return null;
|
|
}
|
|
try {
|
|
return win.matchMedia( query );
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
function rememberAttribute( element, name ) {
|
|
return element.hasAttribute( name ) ? element.getAttribute( name ) : null;
|
|
}
|
|
|
|
function restoreAttribute( element, name, value ) {
|
|
if ( value === null ) {
|
|
element.removeAttribute( name );
|
|
} else {
|
|
element.setAttribute( name, value );
|
|
}
|
|
}
|
|
|
|
function focusWithoutScroll( element ) {
|
|
try {
|
|
element.focus( { preventScroll: true } );
|
|
} catch {
|
|
element.focus();
|
|
}
|
|
}
|
|
|
|
function initializeSection( section, environment ) {
|
|
if ( section.classList.contains( 'is-layout-grid' ) ) {
|
|
return null;
|
|
}
|
|
|
|
const adaptive = section.classList.contains( 'is-layout-adaptive' );
|
|
const carousel = section.classList.contains( 'is-layout-carousel' );
|
|
const headingId = section.getAttribute( 'aria-labelledby' );
|
|
const heading = headingId && environment.document.getElementById( headingId );
|
|
const track = section.querySelector( '[data-chrnx-related-track]' );
|
|
const controls = section.querySelector( '.chrnx-related-posts__controls' );
|
|
const previous = controls && controls.querySelector( '[data-chrnx-related-previous]' );
|
|
const next = controls && controls.querySelector( '[data-chrnx-related-next]' );
|
|
const cards = track ? [ ...track.querySelectorAll( 'article' ) ] : [];
|
|
|
|
if (
|
|
( ! adaptive && ! carousel ) ||
|
|
! heading ||
|
|
! track ||
|
|
! controls ||
|
|
! previous ||
|
|
! next ||
|
|
! cards.length ||
|
|
typeof cards[ 0 ].getBoundingClientRect !== 'function' ||
|
|
typeof track.scrollBy !== 'function'
|
|
) {
|
|
return null;
|
|
}
|
|
|
|
const original = {
|
|
controlsHidden: controls.hidden,
|
|
previousDisabled: previous.disabled,
|
|
nextDisabled: next.disabled,
|
|
role: rememberAttribute( track, 'role' ),
|
|
labelledBy: rememberAttribute( track, 'aria-labelledby' ),
|
|
tabIndex: rememberAttribute( track, 'tabindex' ),
|
|
};
|
|
let rail = carousel || environment.media.matches;
|
|
|
|
const updateEdges = () => {
|
|
const maximum = Math.max( 0, track.scrollWidth - track.clientWidth );
|
|
previous.disabled = track.scrollLeft <= 1;
|
|
next.disabled = track.scrollLeft >= maximum - 1;
|
|
};
|
|
const measuredStep = () => {
|
|
const style = environment.getComputedStyle( track );
|
|
let gap = Number.parseFloat( style.columnGap );
|
|
if ( ! Number.isFinite( gap ) ) {
|
|
gap = Number.parseFloat( style.gap );
|
|
}
|
|
if ( ! Number.isFinite( gap ) ) {
|
|
gap = 0;
|
|
}
|
|
return cards[ 0 ].getBoundingClientRect().width + gap;
|
|
};
|
|
const scroll = ( direction ) => {
|
|
if ( ! rail ) {
|
|
return;
|
|
}
|
|
track.scrollBy( {
|
|
left: direction * measuredStep(),
|
|
behavior: environment.reducedMotion.matches ? 'auto' : 'smooth',
|
|
} );
|
|
};
|
|
const focusHeadingBeforeHide = () => {
|
|
const active = environment.document.activeElement;
|
|
if ( active !== previous && active !== next ) {
|
|
return;
|
|
}
|
|
const previousTabIndex = rememberAttribute( heading, 'tabindex' );
|
|
heading.setAttribute( 'tabindex', '-1' );
|
|
focusWithoutScroll( heading );
|
|
restoreAttribute( heading, 'tabindex', previousTabIndex );
|
|
};
|
|
const applyViewport = () => {
|
|
const nextRail = carousel || environment.media.matches;
|
|
if ( rail && ! nextRail ) {
|
|
focusHeadingBeforeHide();
|
|
}
|
|
rail = nextRail;
|
|
if ( rail ) {
|
|
track.setAttribute( 'role', 'region' );
|
|
track.setAttribute( 'aria-labelledby', headingId );
|
|
track.setAttribute( 'tabindex', '0' );
|
|
controls.hidden = false;
|
|
updateEdges();
|
|
} else {
|
|
controls.hidden = true;
|
|
track.removeAttribute( 'role' );
|
|
track.removeAttribute( 'aria-labelledby' );
|
|
track.removeAttribute( 'tabindex' );
|
|
}
|
|
};
|
|
const onPrevious = () => scroll( -1 );
|
|
const onNext = () => scroll( 1 );
|
|
const onMediaChange = () => applyViewport();
|
|
const onResize = () => updateEdges();
|
|
|
|
previous.addEventListener( 'click', onPrevious );
|
|
next.addEventListener( 'click', onNext );
|
|
track.addEventListener( 'scroll', updateEdges );
|
|
environment.window.addEventListener( 'resize', onResize );
|
|
if ( adaptive ) {
|
|
environment.media.addEventListener( 'change', onMediaChange );
|
|
}
|
|
applyViewport();
|
|
|
|
return () => {
|
|
previous.removeEventListener( 'click', onPrevious );
|
|
next.removeEventListener( 'click', onNext );
|
|
track.removeEventListener( 'scroll', updateEdges );
|
|
environment.window.removeEventListener( 'resize', onResize );
|
|
if ( adaptive ) {
|
|
environment.media.removeEventListener( 'change', onMediaChange );
|
|
}
|
|
controls.hidden = original.controlsHidden;
|
|
previous.disabled = original.previousDisabled;
|
|
next.disabled = original.nextDisabled;
|
|
restoreAttribute( track, 'role', original.role );
|
|
restoreAttribute( track, 'aria-labelledby', original.labelledBy );
|
|
restoreAttribute( track, 'tabindex', original.tabIndex );
|
|
};
|
|
}
|
|
|
|
function cleanup() {
|
|
if ( cleanupCurrent ) {
|
|
const activeCleanup = cleanupCurrent;
|
|
cleanupCurrent = null;
|
|
activeCleanup();
|
|
}
|
|
}
|
|
|
|
function initialize( options = {} ) {
|
|
cleanup();
|
|
|
|
const doc = options.document || ( typeof document === 'undefined' ? null : document );
|
|
const win = options.window || ( doc && doc.defaultView );
|
|
if (
|
|
! doc ||
|
|
! win ||
|
|
typeof win.addEventListener !== 'function' ||
|
|
typeof win.removeEventListener !== 'function'
|
|
) {
|
|
return false;
|
|
}
|
|
|
|
const media = mediaQuery( options.media, win, '(max-width: 900px)' );
|
|
const reducedMotion = mediaQuery(
|
|
options.reducedMotion,
|
|
win,
|
|
'(prefers-reduced-motion: reduce)',
|
|
);
|
|
const getStyle =
|
|
options.getComputedStyle ||
|
|
( typeof win.getComputedStyle === 'function' ? win.getComputedStyle.bind( win ) : null );
|
|
if ( ! validMediaQuery( media ) || ! validMediaQuery( reducedMotion ) || ! getStyle ) {
|
|
return false;
|
|
}
|
|
|
|
const environment = {
|
|
document: doc,
|
|
window: win,
|
|
media,
|
|
reducedMotion,
|
|
getComputedStyle: getStyle,
|
|
};
|
|
const cleanups = [ ...doc.querySelectorAll( '.chrnx-related-posts' ) ]
|
|
.map( ( section ) => initializeSection( section, environment ) )
|
|
.filter( Boolean );
|
|
if ( ! cleanups.length ) {
|
|
return false;
|
|
}
|
|
|
|
cleanupCurrent = () => cleanups.forEach( ( callback ) => callback() );
|
|
return cleanupCurrent;
|
|
}
|
|
|
|
const helpers = { cleanup, initialize };
|
|
if ( typeof module !== 'undefined' && module.exports ) {
|
|
module.exports = helpers;
|
|
} else if ( typeof document !== 'undefined' ) {
|
|
if ( document.readyState === 'loading' ) {
|
|
document.addEventListener( 'DOMContentLoaded', () => initialize(), { once: true } );
|
|
} else {
|
|
initialize();
|
|
}
|
|
}
|
|
} )();
|