284 lines
9.7 KiB
JavaScript
284 lines
9.7 KiB
JavaScript
( function () {
|
|
'use strict';
|
|
|
|
const PANEL = {
|
|
search: 'search',
|
|
theme: 'theme',
|
|
menu: 'menu',
|
|
};
|
|
let cleanupCurrent = null;
|
|
|
|
function initialize( options = {} ) {
|
|
if ( cleanupCurrent ) {
|
|
cleanupCurrent();
|
|
}
|
|
|
|
const doc = options.document || ( typeof document === 'undefined' ? null : document );
|
|
if ( ! doc ) {
|
|
return false;
|
|
}
|
|
|
|
let media = options.media;
|
|
if ( media === undefined ) {
|
|
try {
|
|
media = window.matchMedia( '(max-width: 599px)' );
|
|
} catch {
|
|
media = null;
|
|
}
|
|
}
|
|
if (
|
|
! media ||
|
|
typeof media.matches !== 'boolean' ||
|
|
typeof media.addEventListener !== 'function' ||
|
|
typeof media.removeEventListener !== 'function'
|
|
) {
|
|
cleanupCurrent = null;
|
|
return false;
|
|
}
|
|
|
|
const menuButton = doc.querySelector( '[data-chrnx-menu-toggle]' );
|
|
const search = doc.querySelector( '[data-chrnx-search]' );
|
|
const themeButton = doc.querySelector( '[data-chrnx-theme-button]' );
|
|
const searchInput = search && search.querySelector( 'input[name="s"]' );
|
|
const searchButton = search && search.querySelector( '[data-chrnx-search-button]' );
|
|
const searchPanel =
|
|
searchButton && doc.getElementById( searchButton.getAttribute( 'aria-controls' ) );
|
|
const nav = menuButton && doc.getElementById( menuButton.getAttribute( 'aria-controls' ) );
|
|
const themePanel =
|
|
themeButton && doc.getElementById( themeButton.getAttribute( 'aria-controls' ) );
|
|
const theme = themeButton && themeButton.parentElement;
|
|
const shell = menuButton && menuButton.parentElement;
|
|
const brand = shell && shell.querySelector( '.chrnx-site-brand' );
|
|
const navLink = nav && nav.querySelector( 'a[href]' );
|
|
const themeRadio = themePanel && themePanel.querySelector( 'input[type="radio"]' );
|
|
const required = [
|
|
menuButton,
|
|
nav,
|
|
search,
|
|
searchInput,
|
|
searchPanel,
|
|
searchButton,
|
|
theme,
|
|
themeButton,
|
|
themePanel,
|
|
themeRadio,
|
|
brand,
|
|
navLink,
|
|
shell,
|
|
];
|
|
if (
|
|
required.some( ( element ) => ! element ) ||
|
|
searchPanel.parentElement !== search ||
|
|
! [ brand, nav, search, theme ].every( ( element ) => element.parentElement === shell )
|
|
) {
|
|
cleanupCurrent = null;
|
|
return false;
|
|
}
|
|
|
|
const elements = [ menuButton, nav, search, theme ];
|
|
const anchors = new Map();
|
|
const original = new Map();
|
|
const enhancedClassWasPresent = doc.documentElement.classList.contains(
|
|
'chrnx-navigation-enhanced',
|
|
);
|
|
const remember = ( element, attributes ) => {
|
|
const state = { hidden: element.hidden, attributes: {} };
|
|
attributes.forEach( ( attribute ) => {
|
|
state.attributes[ attribute ] = element.hasAttribute( attribute )
|
|
? element.getAttribute( attribute )
|
|
: null;
|
|
} );
|
|
original.set( element, state );
|
|
};
|
|
remember( menuButton, [ 'aria-expanded', 'aria-label' ] );
|
|
remember( nav, [] );
|
|
remember( search, [ 'data-expanded' ] );
|
|
remember( searchPanel, [] );
|
|
remember( searchButton, [ 'aria-expanded', 'aria-label' ] );
|
|
remember( themeButton, [ 'aria-expanded' ] );
|
|
remember( themePanel, [] );
|
|
elements.forEach( ( element ) => {
|
|
const anchor = doc.createComment( 'chrnx-original-position' );
|
|
element.parentNode.insertBefore( anchor, element );
|
|
anchors.set( element, anchor );
|
|
} );
|
|
|
|
let mobile = media.matches;
|
|
let activePanel = null;
|
|
|
|
const currentThemeRadio = () =>
|
|
themePanel.querySelector( 'input[type="radio"]:checked' ) || themeRadio;
|
|
const restoreOriginalOrder = () => {
|
|
elements.forEach( ( element ) => {
|
|
const anchor = anchors.get( element );
|
|
anchor.parentNode.insertBefore( element, anchor.nextSibling );
|
|
} );
|
|
};
|
|
const setSearchState = ( open ) => {
|
|
search.dataset.expanded = String( open );
|
|
searchPanel.hidden = ! open;
|
|
searchButton.setAttribute( 'aria-expanded', String( open ) );
|
|
searchButton.setAttribute(
|
|
'aria-label',
|
|
open ? searchButton.dataset.labelSubmit : searchButton.dataset.labelOpen,
|
|
);
|
|
};
|
|
const synchronize = () => {
|
|
const searchOpen = activePanel === PANEL.search;
|
|
const themeOpen = mobile && activePanel === PANEL.theme;
|
|
const menuOpen = mobile && activePanel === PANEL.menu;
|
|
setSearchState( searchOpen );
|
|
themeButton.hidden = ! mobile;
|
|
themeButton.setAttribute( 'aria-expanded', String( themeOpen ) );
|
|
themePanel.hidden = mobile && ! themeOpen;
|
|
menuButton.hidden = ! mobile;
|
|
menuButton.setAttribute( 'aria-expanded', String( menuOpen ) );
|
|
menuButton.setAttribute(
|
|
'aria-label',
|
|
menuOpen ? menuButton.dataset.labelClose : menuButton.dataset.labelOpen,
|
|
);
|
|
nav.hidden = mobile && ! menuOpen;
|
|
};
|
|
const setActivePanel = ( name, transitionOptions = {} ) => {
|
|
activePanel = name;
|
|
synchronize();
|
|
if ( name === PANEL.search ) {
|
|
searchInput.focus();
|
|
} else if ( transitionOptions.restoreFocus && name === null ) {
|
|
transitionOptions.trigger.focus();
|
|
}
|
|
};
|
|
const applyViewport = ( matches ) => {
|
|
const focused = doc.activeElement;
|
|
const focusWasSearch = searchPanel.contains( focused );
|
|
const focusWasMenuButton = focused === menuButton;
|
|
const focusWasThemeButton = focused === themeButton;
|
|
const focusWasNav = nav.contains( focused );
|
|
const focusWasThemePanel = themePanel.contains( focused );
|
|
|
|
mobile = matches;
|
|
activePanel = null;
|
|
if ( mobile ) {
|
|
shell.append( search, theme, menuButton, nav );
|
|
} else {
|
|
restoreOriginalOrder();
|
|
}
|
|
synchronize();
|
|
|
|
if ( ! mobile && focusWasSearch ) {
|
|
searchButton.focus();
|
|
} else if ( ! mobile && focusWasMenuButton ) {
|
|
navLink.focus();
|
|
} else if ( ! mobile && focusWasThemeButton ) {
|
|
currentThemeRadio().focus();
|
|
} else if ( ! mobile && ( focusWasNav || focusWasThemePanel ) ) {
|
|
focused.focus();
|
|
} else if ( mobile && focusWasSearch ) {
|
|
searchButton.focus();
|
|
} else if ( mobile && focusWasNav ) {
|
|
menuButton.focus();
|
|
} else if ( mobile && focusWasThemePanel ) {
|
|
themeButton.focus();
|
|
}
|
|
};
|
|
const focusSafely = ( element ) => {
|
|
try {
|
|
element.focus( { preventScroll: true } );
|
|
} catch {
|
|
element.focus();
|
|
}
|
|
};
|
|
const closeForOutsideTarget = ( target ) => {
|
|
if ( activePanel !== PANEL.theme || theme.contains( target ) ) {
|
|
return;
|
|
}
|
|
const focused = doc.activeElement;
|
|
const targetOwnsFocus =
|
|
focused !== doc.body &&
|
|
focused !== doc.documentElement &&
|
|
( focused === target || focused.contains( target ) || target.contains( focused ) );
|
|
if ( ! targetOwnsFocus ) {
|
|
focusSafely( themeButton );
|
|
}
|
|
setActivePanel( null );
|
|
};
|
|
const onMenuClick = () => {
|
|
focusSafely( menuButton );
|
|
setActivePanel( activePanel === PANEL.menu ? null : PANEL.menu );
|
|
};
|
|
const onThemeClick = () => {
|
|
focusSafely( themeButton );
|
|
setActivePanel( activePanel === PANEL.theme ? null : PANEL.theme );
|
|
};
|
|
const onSearchSubmit = ( event ) => {
|
|
if ( activePanel === PANEL.search && searchInput.value.trim() ) {
|
|
return;
|
|
}
|
|
event.preventDefault();
|
|
setActivePanel( PANEL.search );
|
|
};
|
|
const onKeydown = ( event ) => {
|
|
if ( event.key !== 'Escape' || ! activePanel ) {
|
|
return;
|
|
}
|
|
event.preventDefault();
|
|
const trigger = {
|
|
[ PANEL.search ]: searchButton,
|
|
[ PANEL.theme ]: themeButton,
|
|
[ PANEL.menu ]: menuButton,
|
|
}[ activePanel ];
|
|
setActivePanel( null, { restoreFocus: true, trigger } );
|
|
};
|
|
const onOutside = ( event ) => closeForOutsideTarget( event.target );
|
|
const onMediaChange = ( event ) => applyViewport( event.matches );
|
|
|
|
menuButton.addEventListener( 'click', onMenuClick );
|
|
themeButton.addEventListener( 'click', onThemeClick );
|
|
search.addEventListener( 'submit', onSearchSubmit );
|
|
doc.addEventListener( 'keydown', onKeydown );
|
|
doc.addEventListener( 'focusin', onOutside );
|
|
doc.addEventListener( 'click', onOutside );
|
|
media.addEventListener( 'change', onMediaChange );
|
|
doc.documentElement.classList.add( 'chrnx-navigation-enhanced' );
|
|
applyViewport( media.matches );
|
|
|
|
cleanupCurrent = () => {
|
|
menuButton.removeEventListener( 'click', onMenuClick );
|
|
themeButton.removeEventListener( 'click', onThemeClick );
|
|
search.removeEventListener( 'submit', onSearchSubmit );
|
|
doc.removeEventListener( 'keydown', onKeydown );
|
|
doc.removeEventListener( 'focusin', onOutside );
|
|
doc.removeEventListener( 'click', onOutside );
|
|
media.removeEventListener( 'change', onMediaChange );
|
|
restoreOriginalOrder();
|
|
anchors.forEach( ( anchor ) => anchor.remove() );
|
|
original.forEach( ( state, element ) => {
|
|
element.hidden = state.hidden;
|
|
Object.entries( state.attributes ).forEach( ( [ attribute, value ] ) => {
|
|
if ( value === null ) {
|
|
element.removeAttribute( attribute );
|
|
} else {
|
|
element.setAttribute( attribute, value );
|
|
}
|
|
} );
|
|
} );
|
|
if ( ! enhancedClassWasPresent ) {
|
|
doc.documentElement.classList.remove( 'chrnx-navigation-enhanced' );
|
|
}
|
|
cleanupCurrent = null;
|
|
};
|
|
return true;
|
|
}
|
|
|
|
const helpers = { 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();
|
|
}
|
|
}
|
|
} )();
|