2021-06-21 17:49:47 +00:00
|
|
|
/**
|
2021-06-21 21:14:33 +00:00
|
|
|
* Initialize Tabber
|
|
|
|
*
|
|
|
|
* @param {HTMLElement} tabber
|
2021-06-21 17:49:47 +00:00
|
|
|
*/
|
2021-06-21 21:14:33 +00:00
|
|
|
function initTabber( tabber ) {
|
|
|
|
const key = tabber.getAttribute( 'id' ).substring( 7 ),
|
|
|
|
tabPanels = tabber.querySelectorAll( '.tabber__panel' );
|
|
|
|
|
|
|
|
const container = document.createElement( 'header' ),
|
2021-06-21 23:32:15 +00:00
|
|
|
tabList = document.createElement( 'nav' ),
|
2021-06-21 21:14:33 +00:00
|
|
|
prevButton = document.createElement( 'div' ),
|
|
|
|
nextButton = document.createElement( 'div' );
|
|
|
|
|
|
|
|
const buildTabs = () => {
|
2021-06-21 23:28:47 +00:00
|
|
|
const fragment = new DocumentFragment();
|
2021-06-21 21:14:33 +00:00
|
|
|
|
|
|
|
[ ...tabPanels ].forEach( ( tabPanel ) => {
|
|
|
|
const hash = mw.util.escapeIdForAttribute( tabPanel.title ) + '-' + key,
|
|
|
|
tab = document.createElement( 'a' );
|
|
|
|
|
|
|
|
tabPanel.setAttribute( 'id', hash );
|
|
|
|
tabPanel.setAttribute( 'role', 'tabpanel' );
|
|
|
|
tabPanel.setAttribute( 'aria-labelledby', 'tab-' + hash );
|
2021-06-21 23:38:59 +00:00
|
|
|
tabPanel.setAttribute( 'aria-hidden', true );
|
2021-06-21 21:14:33 +00:00
|
|
|
|
|
|
|
tab.innerText = tabPanel.title;
|
2021-06-21 23:46:17 +00:00
|
|
|
tab.classList.add( 'tabber__tab' );
|
2021-06-21 21:14:33 +00:00
|
|
|
tab.setAttribute( 'title', tabPanel.title );
|
|
|
|
tab.setAttribute( 'role', 'tab' );
|
|
|
|
tab.setAttribute( 'href', '#' + hash );
|
|
|
|
tab.setAttribute( 'id', 'tab-' + hash );
|
2021-06-21 23:38:59 +00:00
|
|
|
tab.setAttribute( 'aria-select', false );
|
2021-06-21 21:14:33 +00:00
|
|
|
tab.setAttribute( 'aria-controls', hash );
|
|
|
|
|
2021-06-21 23:28:47 +00:00
|
|
|
fragment.append( tab );
|
2021-06-21 21:14:33 +00:00
|
|
|
} );
|
|
|
|
|
2021-06-21 23:32:15 +00:00
|
|
|
tabList.append( fragment );
|
2021-06-21 23:28:47 +00:00
|
|
|
|
|
|
|
container.classList.add( 'tabber__header' );
|
2021-06-21 23:46:17 +00:00
|
|
|
tabList.classList.add( 'tabber__tabs' );
|
2021-06-21 23:32:15 +00:00
|
|
|
tabList.setAttribute( 'role', 'tablist' );
|
2021-06-21 23:28:47 +00:00
|
|
|
prevButton.classList.add( 'tabber__header__prev' );
|
|
|
|
nextButton.classList.add( 'tabber__header__next' );
|
|
|
|
|
2021-06-21 23:32:15 +00:00
|
|
|
container.append( prevButton, tabList, nextButton );
|
2021-06-21 21:14:33 +00:00
|
|
|
};
|
2021-06-21 17:49:47 +00:00
|
|
|
|
2021-06-21 21:14:33 +00:00
|
|
|
buildTabs();
|
|
|
|
tabber.prepend( container );
|
|
|
|
|
|
|
|
// Initalize previous and next buttons
|
|
|
|
const initButtons = () => {
|
|
|
|
const PREVCLASS = 'tabber__header--prev-visible',
|
|
|
|
NEXTCLASS = 'tabber__header--next-visible';
|
|
|
|
|
|
|
|
/* eslint-disable mediawiki/class-doc */
|
|
|
|
const scrollTabs = ( offset ) => {
|
2021-06-21 23:32:15 +00:00
|
|
|
const scrollLeft = tabList.scrollLeft + offset;
|
2021-06-21 21:14:33 +00:00
|
|
|
|
|
|
|
// Scroll to the start
|
|
|
|
if ( scrollLeft <= 0 ) {
|
2021-06-21 23:32:15 +00:00
|
|
|
tabList.scrollLeft = 0;
|
2021-06-21 21:14:33 +00:00
|
|
|
container.classList.remove( PREVCLASS );
|
|
|
|
container.classList.add( NEXTCLASS );
|
|
|
|
} else {
|
2021-06-21 23:32:15 +00:00
|
|
|
tabList.scrollLeft = scrollLeft;
|
2021-06-21 21:14:33 +00:00
|
|
|
// Scroll to the end
|
2021-06-21 23:32:15 +00:00
|
|
|
if ( scrollLeft + tabList.offsetWidth >= tabList.scrollWidth ) {
|
2021-06-21 21:14:33 +00:00
|
|
|
container.classList.remove( NEXTCLASS );
|
|
|
|
container.classList.add( PREVCLASS );
|
2021-06-21 17:49:47 +00:00
|
|
|
} else {
|
2021-06-21 21:14:33 +00:00
|
|
|
container.classList.add( NEXTCLASS );
|
|
|
|
container.classList.add( PREVCLASS );
|
2021-06-21 20:56:00 +00:00
|
|
|
}
|
|
|
|
}
|
2021-06-21 21:14:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const setupButtons = () => {
|
2021-06-21 23:32:15 +00:00
|
|
|
const isScrollable = ( tabList.scrollWidth > container.offsetWidth );
|
2021-06-21 21:14:33 +00:00
|
|
|
|
|
|
|
if ( isScrollable ) {
|
|
|
|
const scrollOffset = container.offsetWidth / 2;
|
|
|
|
|
|
|
|
// Just to add the right classes
|
|
|
|
scrollTabs( 0 );
|
|
|
|
prevButton.addEventListener( 'click', () => {
|
|
|
|
scrollTabs( -scrollOffset );
|
|
|
|
}, false );
|
|
|
|
|
|
|
|
nextButton.addEventListener( 'click', () => {
|
|
|
|
scrollTabs( scrollOffset );
|
|
|
|
}, false );
|
|
|
|
} else {
|
|
|
|
container.classList.remove( NEXTCLASS );
|
|
|
|
container.classList.remove( PREVCLASS );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
/* eslint-enable mediawiki/class-doc */
|
2021-06-21 20:56:00 +00:00
|
|
|
|
2021-06-21 21:14:33 +00:00
|
|
|
setupButtons();
|
2021-06-21 20:56:00 +00:00
|
|
|
|
2021-06-21 21:14:33 +00:00
|
|
|
// Listen for window resize
|
|
|
|
window.addEventListener( 'resize', () => {
|
|
|
|
mw.util.debounce( 250, setupButtons() );
|
|
|
|
} );
|
|
|
|
};
|
2021-06-21 20:56:00 +00:00
|
|
|
|
2021-06-21 21:14:33 +00:00
|
|
|
/**
|
|
|
|
* Show panel based on target hash
|
|
|
|
*
|
|
|
|
* @param {string} targetHash
|
|
|
|
*/
|
|
|
|
function showPanel( targetHash ) {
|
2021-06-21 23:46:17 +00:00
|
|
|
const ACTIVETABCLASS = 'tabber__tab--active',
|
2021-06-21 21:14:33 +00:00
|
|
|
ACTIVEPANELCLASS = 'tabber__panel--active',
|
|
|
|
targetPanel = document.getElementById( targetHash ),
|
|
|
|
targetTab = document.getElementById( 'tab-' + targetHash ),
|
|
|
|
section = targetPanel.parentElement,
|
|
|
|
activePanel = section.querySelector( '.' + ACTIVEPANELCLASS );
|
|
|
|
|
|
|
|
/* eslint-disable mediawiki/class-doc */
|
|
|
|
if ( activePanel ) {
|
2021-06-21 23:32:15 +00:00
|
|
|
const activeTab = tabList.querySelector( '.' + ACTIVETABCLASS );
|
2021-06-21 21:14:33 +00:00
|
|
|
|
|
|
|
if ( activeTab ) {
|
|
|
|
activeTab.classList.remove( ACTIVETABCLASS );
|
2021-06-21 23:38:59 +00:00
|
|
|
activeTab.setAttribute( 'aria-selected', false );
|
2021-06-21 17:49:47 +00:00
|
|
|
}
|
|
|
|
|
2021-06-21 21:14:33 +00:00
|
|
|
activePanel.classList.remove( ACTIVEPANELCLASS );
|
2021-06-21 23:38:59 +00:00
|
|
|
activePanel.setAttribute( 'aria-hidden', true );
|
2021-06-21 21:14:33 +00:00
|
|
|
section.style.height = activePanel.offsetHeight + 'px';
|
|
|
|
section.style.height = targetPanel.offsetHeight + 'px';
|
|
|
|
} else {
|
|
|
|
section.style.height = targetPanel.offsetHeight + 'px';
|
|
|
|
}
|
|
|
|
|
2021-06-21 23:46:17 +00:00
|
|
|
// Add active class to the tab
|
2021-06-21 21:14:33 +00:00
|
|
|
targetTab.classList.add( ACTIVETABCLASS );
|
2021-06-21 23:38:59 +00:00
|
|
|
targetTab.setAttribute( 'aria-selected', true );
|
2021-06-21 21:14:33 +00:00
|
|
|
targetPanel.classList.add( ACTIVEPANELCLASS );
|
2021-06-21 23:38:59 +00:00
|
|
|
targetPanel.setAttribute( 'aria-hidden', false );
|
2021-06-21 21:14:33 +00:00
|
|
|
|
|
|
|
// Scroll to tab
|
|
|
|
section.scrollLeft = targetPanel.offsetLeft;
|
|
|
|
/* eslint-enable mediawiki/class-doc */
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve target hash and trigger show panel
|
|
|
|
* If no targetHash is invalid, use the first panel
|
|
|
|
*
|
|
|
|
* @param {HTMLElement} tabber
|
|
|
|
*/
|
|
|
|
function switchTab() {
|
|
|
|
let targetHash = new mw.Uri( location.href ).fragment;
|
|
|
|
|
|
|
|
// Switch to the first tab if no targetHash or no tab is detected
|
2021-06-21 23:32:15 +00:00
|
|
|
if ( !targetHash || !tabList.querySelector( '#tab-' + targetHash ) ) {
|
|
|
|
targetHash = tabList.firstElementChild.getAttribute( 'id' ).substring( 4 );
|
2021-06-21 21:14:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
showPanel( targetHash );
|
|
|
|
}
|
|
|
|
|
|
|
|
switchTab();
|
|
|
|
|
|
|
|
// Only run if client is not a touch device
|
|
|
|
if ( matchMedia( '(hover: hover)' ).matches ) {
|
|
|
|
initButtons();
|
|
|
|
}
|
|
|
|
|
|
|
|
// window.addEventListener( 'hashchange', switchTab, false );
|
|
|
|
|
|
|
|
// Respond to clicks on the nav tabs
|
2021-06-21 23:32:15 +00:00
|
|
|
[ ...tabList.children ].forEach( ( tab ) => {
|
2021-06-21 21:14:33 +00:00
|
|
|
tab.addEventListener( 'click', ( event ) => {
|
|
|
|
const targetHash = tab.getAttribute( 'href' ).substring( 1 );
|
|
|
|
event.preventDefault();
|
|
|
|
// Add hash to the end of the URL
|
|
|
|
history.pushState( null, null, '#' + targetHash );
|
|
|
|
showPanel( targetHash );
|
|
|
|
} );
|
|
|
|
} );
|
2021-06-21 17:49:47 +00:00
|
|
|
|
2021-06-21 21:14:33 +00:00
|
|
|
tabber.classList.add( 'tabber--live' );
|
|
|
|
}
|
2021-06-21 17:49:47 +00:00
|
|
|
|
2021-06-21 21:14:33 +00:00
|
|
|
function main() {
|
|
|
|
const tabbers = document.querySelectorAll( '.tabber' );
|
2021-06-21 17:49:47 +00:00
|
|
|
|
2021-06-21 21:14:33 +00:00
|
|
|
if ( tabbers ) {
|
|
|
|
mw.loader.load( 'ext.tabberNeue.icons' );
|
|
|
|
tabbers.forEach( ( tabber ) => {
|
|
|
|
initTabber( tabber );
|
2021-06-21 17:49:47 +00:00
|
|
|
} );
|
2021-06-21 21:14:33 +00:00
|
|
|
}
|
|
|
|
}
|
2021-06-21 17:49:47 +00:00
|
|
|
|
2021-06-21 21:14:33 +00:00
|
|
|
main();
|