2021-10-20 18:58:49 +00:00
|
|
|
// Enable Vector features limited to ES6 browse
|
2021-10-26 23:37:56 +00:00
|
|
|
const
|
|
|
|
searchToggle = require( './searchToggle.js' ),
|
|
|
|
stickyHeader = require( './stickyHeader.js' ),
|
|
|
|
scrollObserver = require( './scrollObserver.js' ),
|
2022-03-17 23:01:17 +00:00
|
|
|
initExperiment = require( './AB.js' ),
|
2022-01-21 20:15:34 +00:00
|
|
|
initSectionObserver = require( './sectionObserver.js' ),
|
|
|
|
initTableOfContents = require( './tableOfContents.js' ),
|
2022-02-08 21:14:33 +00:00
|
|
|
deferUntilFrame = require( './deferUntilFrame.js' ),
|
2022-03-17 23:02:39 +00:00
|
|
|
linkHijack = require( './linkHijack.js' ),
|
|
|
|
ABTestConfig = require( /** @type {string} */ ( './config.json' ) ).wgVectorWebABTestEnrollment || {},
|
2022-01-21 20:15:34 +00:00
|
|
|
TOC_ID = 'mw-panel-toc',
|
2022-03-24 20:49:22 +00:00
|
|
|
TOC_ID_LEGACY = 'toc',
|
2022-01-21 20:15:34 +00:00
|
|
|
BODY_CONTENT_ID = 'bodyContent',
|
|
|
|
HEADLINE_SELECTOR = '.mw-headline',
|
2022-03-17 23:01:17 +00:00
|
|
|
TOC_SECTION_ID_PREFIX = 'toc-',
|
2022-03-24 20:49:22 +00:00
|
|
|
TOC_LEGACY_PLACEHOLDER_TAG = 'mw:tocplace',
|
|
|
|
TOC_SCROLL_HOOK = 'table_of_contents',
|
|
|
|
PAGE_TITLE_SCROLL_HOOK = 'page_title',
|
2022-03-17 23:02:39 +00:00
|
|
|
TOC_QUERY_PARAM = 'tableofcontents',
|
2022-05-05 20:54:20 +00:00
|
|
|
TOC_EXPERIMENT_NAME = 'skin-vector-toc-experiment';
|
2021-10-20 18:58:49 +00:00
|
|
|
|
2022-03-17 16:54:26 +00:00
|
|
|
/**
|
|
|
|
* @callback OnIntersection
|
|
|
|
* @param {HTMLElement} element The section that triggered the new intersection change.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ignore
|
|
|
|
* @param {Function} changeActiveSection
|
|
|
|
* @return {OnIntersection}
|
|
|
|
*/
|
|
|
|
const getHeadingIntersectionHandler = ( changeActiveSection ) => {
|
|
|
|
/**
|
|
|
|
* @param {HTMLElement} section
|
|
|
|
*/
|
|
|
|
return ( section ) => {
|
|
|
|
const headline = section.classList.contains( 'mw-body-content' ) ?
|
|
|
|
section :
|
|
|
|
section.querySelector( HEADLINE_SELECTOR );
|
|
|
|
if ( headline ) {
|
|
|
|
changeActiveSection( `${TOC_SECTION_ID_PREFIX}${headline.id}` );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-10-20 18:58:49 +00:00
|
|
|
/**
|
|
|
|
* @return {void}
|
|
|
|
*/
|
|
|
|
const main = () => {
|
|
|
|
// Initialize the search toggle for the main header only. The sticky header
|
|
|
|
// toggle is initialized after wvui search loads.
|
2021-10-20 19:10:42 +00:00
|
|
|
const searchToggleElement = document.querySelector( '.mw-header .search-toggle' );
|
|
|
|
if ( searchToggleElement ) {
|
|
|
|
searchToggle( searchToggleElement );
|
|
|
|
}
|
2021-10-26 23:37:56 +00:00
|
|
|
|
2022-03-23 20:35:27 +00:00
|
|
|
// Sticky header
|
|
|
|
const
|
|
|
|
header = document.getElementById( stickyHeader.STICKY_HEADER_ID ),
|
|
|
|
stickyIntersection = document.getElementById( stickyHeader.FIRST_HEADING_ID ),
|
|
|
|
userMenu = document.getElementById( stickyHeader.USER_MENU_ID ),
|
|
|
|
allowedNamespace = stickyHeader.isAllowedNamespace( mw.config.get( 'wgNamespaceNumber' ) ),
|
|
|
|
allowedAction = stickyHeader.isAllowedAction( mw.config.get( 'wgAction' ) );
|
|
|
|
|
|
|
|
const isStickyHeaderAllowed =
|
|
|
|
!!header &&
|
|
|
|
!!stickyIntersection &&
|
|
|
|
!!userMenu &&
|
|
|
|
allowedNamespace &&
|
|
|
|
allowedAction &&
|
|
|
|
'IntersectionObserver' in window;
|
|
|
|
|
2022-05-27 16:36:17 +00:00
|
|
|
const isExperimentEnabled =
|
|
|
|
!!ABTestConfig.enabled &&
|
|
|
|
ABTestConfig.name === stickyHeader.STICKY_HEADER_EXPERIMENT_NAME &&
|
|
|
|
!mw.user.isAnon() &&
|
|
|
|
isStickyHeaderAllowed;
|
2022-03-23 20:35:27 +00:00
|
|
|
|
2022-05-27 16:36:17 +00:00
|
|
|
// If necessary, initialize experiment and fire the A/B test enrollment hook.
|
|
|
|
const stickyHeaderExperiment = isExperimentEnabled &&
|
|
|
|
initExperiment( Object.assign( {}, ABTestConfig, { token: mw.user.getId() } ) );
|
2022-03-17 23:01:17 +00:00
|
|
|
|
|
|
|
// Remove class if present on the html element so that scroll padding isn't undesirably
|
|
|
|
// applied to users who don't experience the new treatment.
|
2022-05-27 16:36:17 +00:00
|
|
|
if ( stickyHeaderExperiment && !stickyHeaderExperiment.isInTreatmentBucket() ) {
|
2022-03-17 23:01:17 +00:00
|
|
|
document.documentElement.classList.remove( 'vector-sticky-header-enabled' );
|
|
|
|
}
|
|
|
|
|
2022-05-27 16:36:17 +00:00
|
|
|
const isStickyHeaderEnabled = stickyHeaderExperiment ?
|
|
|
|
stickyHeaderExperiment.isInTreatmentBucket() :
|
|
|
|
isStickyHeaderAllowed;
|
|
|
|
|
2022-03-24 20:49:22 +00:00
|
|
|
// Table of contents
|
|
|
|
const tocElement = document.getElementById( TOC_ID );
|
|
|
|
const tocElementLegacy = document.getElementById( TOC_ID_LEGACY );
|
|
|
|
const bodyContent = document.getElementById( BODY_CONTENT_ID );
|
|
|
|
|
2022-05-05 20:01:51 +00:00
|
|
|
// Set up intersection observer for page title, used by sticky header
|
2021-10-26 23:37:56 +00:00
|
|
|
const observer = scrollObserver.initScrollObserver(
|
|
|
|
() => {
|
2022-05-27 16:36:17 +00:00
|
|
|
if ( isStickyHeaderAllowed && isStickyHeaderEnabled ) {
|
2022-05-05 20:54:20 +00:00
|
|
|
stickyHeader.show();
|
2021-12-01 22:31:48 +00:00
|
|
|
}
|
2022-03-24 20:49:22 +00:00
|
|
|
scrollObserver.fireScrollHook( 'down', PAGE_TITLE_SCROLL_HOOK );
|
2021-10-26 23:37:56 +00:00
|
|
|
},
|
|
|
|
() => {
|
2022-05-27 16:36:17 +00:00
|
|
|
if ( isStickyHeaderAllowed && isStickyHeaderEnabled ) {
|
2022-05-05 20:54:20 +00:00
|
|
|
stickyHeader.hide();
|
2021-12-01 22:31:48 +00:00
|
|
|
}
|
2022-03-24 20:49:22 +00:00
|
|
|
scrollObserver.fireScrollHook( 'up', PAGE_TITLE_SCROLL_HOOK );
|
2021-10-26 23:37:56 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2022-05-27 16:36:17 +00:00
|
|
|
if ( isStickyHeaderAllowed && isStickyHeaderEnabled ) {
|
2022-03-23 20:35:27 +00:00
|
|
|
stickyHeader.initStickyHeader( {
|
|
|
|
header,
|
|
|
|
userMenu,
|
|
|
|
observer,
|
2022-05-27 16:36:17 +00:00
|
|
|
stickyIntersection
|
2022-03-23 20:35:27 +00:00
|
|
|
} );
|
|
|
|
} else if ( stickyIntersection ) {
|
|
|
|
observer.observe( stickyIntersection );
|
2021-12-01 22:31:48 +00:00
|
|
|
}
|
2022-01-21 20:15:34 +00:00
|
|
|
|
2022-05-05 20:01:51 +00:00
|
|
|
// Setup intersection observer for TOC scroll event tracking
|
|
|
|
// fire hooks for event logging if AB tests are enabled
|
|
|
|
const tocLegacyPlaceholder = document.getElementsByTagName( TOC_LEGACY_PLACEHOLDER_TAG )[ 0 ];
|
|
|
|
const tocLegacyTargetIntersection = tocElementLegacy || tocLegacyPlaceholder;
|
2022-03-24 20:49:22 +00:00
|
|
|
// Initiate observer for table of contents in main content.
|
|
|
|
if ( tocLegacyTargetIntersection ) {
|
2022-05-05 20:01:51 +00:00
|
|
|
const tocObserver = scrollObserver.initScrollObserver(
|
|
|
|
() => {
|
|
|
|
scrollObserver.fireScrollHook( 'down', TOC_SCROLL_HOOK );
|
|
|
|
},
|
|
|
|
() => {
|
|
|
|
scrollObserver.fireScrollHook( 'up', TOC_SCROLL_HOOK );
|
|
|
|
}
|
|
|
|
);
|
|
|
|
tocObserver.observe( tocLegacyTargetIntersection );
|
2022-03-24 20:49:22 +00:00
|
|
|
}
|
2022-01-21 20:15:34 +00:00
|
|
|
|
2022-03-14 21:21:57 +00:00
|
|
|
// Add event data attributes to legacy TOC
|
2022-03-24 20:49:22 +00:00
|
|
|
if ( tocElementLegacy ) {
|
|
|
|
tocElementLegacy.setAttribute( 'data-event-name', 'ui.toc' );
|
2022-03-14 21:21:57 +00:00
|
|
|
}
|
|
|
|
|
2022-01-21 20:15:34 +00:00
|
|
|
if ( !(
|
|
|
|
tocElement &&
|
|
|
|
bodyContent &&
|
|
|
|
window.IntersectionObserver &&
|
2022-03-17 23:02:39 +00:00
|
|
|
window.requestAnimationFrame
|
|
|
|
) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const experiment =
|
|
|
|
!!ABTestConfig.enabled &&
|
|
|
|
ABTestConfig.name === TOC_EXPERIMENT_NAME &&
|
|
|
|
document.body.classList.contains( ABTestConfig.name ) &&
|
|
|
|
// eslint-disable-next-line compat/compat
|
|
|
|
window.URLSearchParams &&
|
|
|
|
initExperiment( ABTestConfig );
|
|
|
|
const isInTreatmentBucket = !!experiment && experiment.isInTreatmentBucket();
|
|
|
|
|
|
|
|
if ( experiment && experiment.isInSample() ) {
|
|
|
|
linkHijack( TOC_QUERY_PARAM, isInTreatmentBucket ? '1' : '0' );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( experiment && !isInTreatmentBucket ) {
|
|
|
|
// Return early if the old TOC is shown.
|
2022-01-21 20:15:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const tableOfContents = initTableOfContents( {
|
|
|
|
container: tocElement,
|
2022-02-15 22:43:04 +00:00
|
|
|
onHeadingClick: ( id ) => {
|
2022-02-07 19:20:17 +00:00
|
|
|
|
2022-02-11 18:14:25 +00:00
|
|
|
// eslint-disable-next-line no-use-before-define
|
2022-01-21 20:15:34 +00:00
|
|
|
sectionObserver.pause();
|
|
|
|
|
2022-02-07 19:20:17 +00:00
|
|
|
tableOfContents.expandSection( id );
|
|
|
|
tableOfContents.changeActiveSection( id );
|
|
|
|
|
2022-02-08 21:14:33 +00:00
|
|
|
// T297614: We want the link that the user has clicked inside the TOC to
|
|
|
|
// be "active" (e.g. bolded) regardless of whether the browser's scroll
|
|
|
|
// position corresponds to that section. Therefore, we need to temporarily
|
|
|
|
// ignore section observer until the browser has finished scrolling to the
|
|
|
|
// section (if needed).
|
|
|
|
//
|
|
|
|
// However, because the scroll event happens asyncronously after the user
|
|
|
|
// clicks on a link and may not even happen at all (e.g. the user has
|
|
|
|
// scrolled all the way to the bottom and clicks a section that is already
|
|
|
|
// in the viewport), determining when we should resume section observer is
|
|
|
|
// a bit tricky.
|
|
|
|
//
|
|
|
|
// Because a scroll event may not even be triggered after clicking the
|
|
|
|
// link, we instead allow the browser to perform a maximum number of
|
|
|
|
// repaints before resuming sectionObserver. Per T297614#7687656, Firefox
|
|
|
|
// 97.0 wasn't consistently activating the table of contents section that
|
|
|
|
// the user clicked even after waiting 2 frames. After further
|
|
|
|
// investigation, it sometimes waits up to 3 frames before painting the
|
|
|
|
// new scroll position so we have that as the limit.
|
2022-02-11 18:14:25 +00:00
|
|
|
//
|
|
|
|
// eslint-disable-next-line no-use-before-define
|
2022-02-08 21:14:33 +00:00
|
|
|
deferUntilFrame( () => sectionObserver.resume(), 3 );
|
2022-02-15 22:43:04 +00:00
|
|
|
},
|
|
|
|
onToggleClick: ( id ) => {
|
|
|
|
tableOfContents.toggleExpandSection( id );
|
2022-01-21 20:15:34 +00:00
|
|
|
}
|
|
|
|
} );
|
2022-03-31 16:30:36 +00:00
|
|
|
const headingSelector = [
|
|
|
|
'h1', 'h2', 'h3', 'h4', 'h5', 'h6'
|
|
|
|
].map( ( tag ) => `.mw-parser-output > ${tag}` ).join( ',' );
|
2022-02-11 18:14:25 +00:00
|
|
|
const sectionObserver = initSectionObserver( {
|
2022-03-31 16:30:36 +00:00
|
|
|
elements: bodyContent.querySelectorAll( `${headingSelector}, .mw-body-content` ),
|
2022-03-23 20:35:27 +00:00
|
|
|
topMargin: header ? header.getBoundingClientRect().height : 0,
|
2022-03-17 16:54:26 +00:00
|
|
|
onIntersection: getHeadingIntersectionHandler( tableOfContents.changeActiveSection )
|
2022-01-21 20:15:34 +00:00
|
|
|
} );
|
2021-10-20 19:10:42 +00:00
|
|
|
};
|
2021-10-20 18:58:49 +00:00
|
|
|
|
2021-10-30 01:01:36 +00:00
|
|
|
module.exports = {
|
2022-03-17 16:54:26 +00:00
|
|
|
main,
|
|
|
|
test: {
|
|
|
|
getHeadingIntersectionHandler
|
|
|
|
}
|
2021-10-30 01:01:36 +00:00
|
|
|
};
|