2022-02-14 18:55:50 +00:00
|
|
|
/** @module TableOfContents */
|
|
|
|
|
2022-08-15 16:34:28 +00:00
|
|
|
/**
|
|
|
|
* TableOfContents Mustache templates
|
|
|
|
*/
|
2023-01-16 11:39:05 +00:00
|
|
|
const templateTocContents = require( /** @type {string} */ ( './templates/TableOfContents__list.mustache' ) );
|
2022-08-15 16:34:28 +00:00
|
|
|
const templateTocLine = require( /** @type {string} */ ( './templates/TableOfContents__line.mustache' ) );
|
|
|
|
/**
|
|
|
|
* TableOfContents Config object for filling mustache templates
|
|
|
|
*/
|
|
|
|
const tableOfContentsConfig = require( /** @type {string} */ ( './tableOfContentsConfig.json' ) );
|
2023-03-03 08:32:02 +00:00
|
|
|
const deferUntilFrame = require( './deferUntilFrame.js' );
|
2022-02-15 22:43:04 +00:00
|
|
|
|
2023-03-03 08:32:02 +00:00
|
|
|
const SECTION_ID_PREFIX = 'toc-';
|
2023-01-13 20:56:22 +00:00
|
|
|
const SECTION_CLASS = 'vector-toc-list-item';
|
|
|
|
const ACTIVE_SECTION_CLASS = 'vector-toc-list-item-active';
|
|
|
|
const EXPANDED_SECTION_CLASS = 'vector-toc-list-item-expanded';
|
|
|
|
const TOP_SECTION_CLASS = 'vector-toc-level-1';
|
|
|
|
const ACTIVE_TOP_SECTION_CLASS = 'vector-toc-level-1-active';
|
|
|
|
const LINK_CLASS = 'vector-toc-link';
|
|
|
|
const TOGGLE_CLASS = 'vector-toc-toggle';
|
2023-01-16 11:39:05 +00:00
|
|
|
const TOC_CONTENTS_ID = 'mw-panel-toc-list';
|
|
|
|
|
2022-02-15 22:43:04 +00:00
|
|
|
/**
|
2023-03-03 08:32:02 +00:00
|
|
|
* Fired when the user clicks a toc link. Note that this callback takes
|
|
|
|
* precedence over the onHashChange callback. The onHashChange callback will not
|
|
|
|
* be called when the user clicks a toc link.
|
|
|
|
*
|
2022-02-15 22:43:04 +00:00
|
|
|
* @callback onHeadingClick
|
|
|
|
* @param {string} id The id of the clicked list item.
|
|
|
|
*/
|
|
|
|
|
2023-03-03 08:32:02 +00:00
|
|
|
/**
|
|
|
|
* Fired when the page's hash fragment has changed. Note that if the user clicks
|
|
|
|
* a link inside the TOC, the `onHeadingClick` callback will fire instead of the
|
|
|
|
* `onHashChange` callback to avoid redundant behavior.
|
|
|
|
*
|
|
|
|
* @callback onHashChange
|
|
|
|
* @param {string} id The id of the list item that corresponds to the hash change event.
|
|
|
|
*/
|
|
|
|
|
2022-02-15 22:43:04 +00:00
|
|
|
/**
|
|
|
|
* @callback onToggleClick
|
|
|
|
* @param {string} id The id of the list item corresponding to the arrow.
|
|
|
|
*/
|
2022-01-21 20:15:34 +00:00
|
|
|
|
2022-07-01 20:19:57 +00:00
|
|
|
/**
|
2022-10-20 21:32:07 +00:00
|
|
|
* @callback onTogglePinned
|
2022-07-01 20:19:57 +00:00
|
|
|
*/
|
|
|
|
|
2023-02-16 01:20:00 +00:00
|
|
|
/**
|
|
|
|
* @callback tableOfContents
|
|
|
|
* @param {TableOfContentsProps} props
|
|
|
|
* @return {TableOfContents}
|
|
|
|
*/
|
|
|
|
|
2022-02-18 00:19:50 +00:00
|
|
|
/**
|
|
|
|
* @typedef {Object} TableOfContentsProps
|
|
|
|
* @property {HTMLElement} container The container element for the table of contents.
|
|
|
|
* @property {onHeadingClick} onHeadingClick Called when an arrow is clicked.
|
2023-03-03 08:32:02 +00:00
|
|
|
* @property {onHashChange} onHashChange Called when a hash change event
|
|
|
|
* matches the id of a LINK_CLASS anchor element.
|
|
|
|
* @property {onToggleClick} [onToggleClick] Called when an arrow is clicked.
|
2022-10-20 21:32:07 +00:00
|
|
|
* @property {onTogglePinned} onTogglePinned Called when pinned toggle buttons are clicked.
|
2022-02-18 00:19:50 +00:00
|
|
|
*/
|
|
|
|
|
2022-08-15 16:34:28 +00:00
|
|
|
/**
|
|
|
|
* @typedef {Object} Section
|
|
|
|
* @property {number} toclevel
|
|
|
|
* @property {string} anchor
|
|
|
|
* @property {string} line
|
|
|
|
* @property {string} number
|
|
|
|
* @property {string} index
|
|
|
|
* @property {number} byteoffset
|
|
|
|
* @property {string} fromtitle
|
|
|
|
* @property {boolean} is-parent-section
|
|
|
|
* @property {boolean} is-top-level-section
|
|
|
|
* @property {Section[]} array-sections
|
|
|
|
* @property {string} level
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {Object} SectionsListData
|
|
|
|
* @property {Section[]} array-sections
|
|
|
|
* @property {boolean} vector-is-collapse-sections-enabled
|
|
|
|
* @property {string} msg-vector-toc-beginning
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {Object} ArraySectionsData
|
|
|
|
* @property {number} number-section-count
|
|
|
|
* @property {Section[]} array-sections
|
|
|
|
*/
|
|
|
|
|
2022-01-21 20:15:34 +00:00
|
|
|
/**
|
|
|
|
* Initializes the sidebar's Table of Contents.
|
|
|
|
*
|
2022-02-18 00:19:50 +00:00
|
|
|
* @param {TableOfContentsProps} props
|
2022-01-21 20:15:34 +00:00
|
|
|
* @return {TableOfContents}
|
|
|
|
*/
|
|
|
|
module.exports = function tableOfContents( props ) {
|
2022-02-07 19:20:17 +00:00
|
|
|
let /** @type {HTMLElement | undefined} */ activeTopSection;
|
|
|
|
let /** @type {HTMLElement | undefined} */ activeSubSection;
|
2022-02-18 00:19:50 +00:00
|
|
|
let /** @type {Array<HTMLElement>} */ expandedSections;
|
2022-01-21 20:15:34 +00:00
|
|
|
|
|
|
|
/**
|
2022-02-07 19:20:17 +00:00
|
|
|
* @typedef {Object} activeSectionIds
|
|
|
|
* @property {string|undefined} parent - The active top level section ID
|
|
|
|
* @property {string|undefined} child - The active subsection ID
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the ids of the active sections.
|
|
|
|
*
|
|
|
|
* @return {activeSectionIds}
|
|
|
|
*/
|
|
|
|
function getActiveSectionIds() {
|
|
|
|
return {
|
|
|
|
parent: ( activeTopSection ) ? activeTopSection.id : undefined,
|
|
|
|
child: ( activeSubSection ) ? activeSubSection.id : undefined
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-04-19 21:56:29 +00:00
|
|
|
/**
|
|
|
|
* Does the user prefer reduced motion?
|
|
|
|
*
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
const prefersReducedMotion = () => {
|
|
|
|
return window.matchMedia( '(prefers-reduced-motion: reduce)' ).matches;
|
|
|
|
};
|
|
|
|
|
2022-02-07 19:20:17 +00:00
|
|
|
/**
|
|
|
|
* Sets an `ACTIVE_SECTION_CLASS` on the element with an id that matches `id`.
|
2022-09-20 21:32:53 +00:00
|
|
|
* Sets an `ACTIVE_TOP_SECTION_CLASS` on the top level heading (e.g. element with the
|
|
|
|
* `TOP_SECTION_CLASS`).
|
2022-09-21 15:52:31 +00:00
|
|
|
* If the element is a top level heading, the element will have both classes.
|
2022-02-07 19:20:17 +00:00
|
|
|
*
|
|
|
|
* @param {string} id The id of the element to be activated in the Table of Contents.
|
2022-01-21 20:15:34 +00:00
|
|
|
*/
|
|
|
|
function activateSection( id ) {
|
2022-02-07 19:20:17 +00:00
|
|
|
const selectedTocSection = document.getElementById( id );
|
|
|
|
const {
|
|
|
|
parent: previousActiveTopId,
|
|
|
|
child: previousActiveSubSectionId
|
|
|
|
} = getActiveSectionIds();
|
|
|
|
|
|
|
|
if (
|
|
|
|
!selectedTocSection ||
|
|
|
|
( previousActiveTopId === id ) ||
|
|
|
|
( previousActiveSubSectionId === id )
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-20 21:32:53 +00:00
|
|
|
// Assign the active top and sub sections, apply classes
|
2023-01-30 19:31:36 +00:00
|
|
|
activeTopSection = /** @type {HTMLElement|undefined} */ ( selectedTocSection.closest( `.${TOP_SECTION_CLASS}` ) );
|
|
|
|
if ( activeTopSection ) {
|
|
|
|
// T328089 Sometimes activeTopSection is null
|
|
|
|
activeTopSection.classList.add( ACTIVE_TOP_SECTION_CLASS );
|
|
|
|
}
|
2022-09-21 15:52:31 +00:00
|
|
|
activeSubSection = selectedTocSection;
|
2022-09-20 21:32:53 +00:00
|
|
|
activeSubSection.classList.add( ACTIVE_SECTION_CLASS );
|
2022-02-07 19:20:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes the `ACTIVE_SECTION_CLASS` from all ToC sections.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
function deactivateSections() {
|
|
|
|
if ( activeSubSection ) {
|
|
|
|
activeSubSection.classList.remove( ACTIVE_SECTION_CLASS );
|
|
|
|
activeSubSection = undefined;
|
|
|
|
}
|
|
|
|
if ( activeTopSection ) {
|
2022-09-20 21:32:53 +00:00
|
|
|
activeTopSection.classList.remove( ACTIVE_TOP_SECTION_CLASS );
|
2022-02-07 19:20:17 +00:00
|
|
|
activeTopSection = undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-09 17:35:25 +00:00
|
|
|
/**
|
|
|
|
* Scroll active section into view if necessary
|
|
|
|
*
|
|
|
|
* @param {string} id The id of the element to be scrolled to in the Table of Contents.
|
|
|
|
*/
|
|
|
|
function scrollToActiveSection( id ) {
|
|
|
|
const section = document.getElementById( id );
|
|
|
|
if ( !section ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get currently visible active link
|
2023-04-11 17:22:58 +00:00
|
|
|
let link = /** @type {HTMLElement|null} */( section.firstElementChild );
|
2022-03-09 17:35:25 +00:00
|
|
|
if ( link && !link.offsetParent ) {
|
|
|
|
// If active link is a hidden subsection, use active parent link
|
|
|
|
const { parent: activeTopId } = getActiveSectionIds();
|
|
|
|
const parentSection = document.getElementById( activeTopId || '' );
|
|
|
|
if ( parentSection ) {
|
2023-04-11 17:22:58 +00:00
|
|
|
link = /** @type {HTMLElement|null} */( parentSection.firstElementChild );
|
2022-03-09 17:35:25 +00:00
|
|
|
} else {
|
|
|
|
link = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const isContainerScrollable = props.container.scrollHeight > props.container.clientHeight;
|
|
|
|
if ( link && isContainerScrollable ) {
|
|
|
|
const containerRect = props.container.getBoundingClientRect();
|
|
|
|
const linkRect = link.getBoundingClientRect();
|
|
|
|
|
|
|
|
// Pixels above or below the TOC where we start scrolling the active section into view
|
|
|
|
const hiddenThreshold = 100;
|
|
|
|
const midpoint = ( containerRect.bottom - containerRect.top ) / 2;
|
|
|
|
const linkHiddenTopValue = containerRect.top - linkRect.top;
|
|
|
|
// Because the bottom of the TOC can extend below the viewport,
|
|
|
|
// min() is used to find the value where the active section first becomes hidden
|
|
|
|
const linkHiddenBottomValue = linkRect.bottom -
|
|
|
|
Math.min( containerRect.bottom, window.innerHeight );
|
|
|
|
|
|
|
|
// Respect 'prefers-reduced-motion' user preference
|
2022-04-19 21:56:29 +00:00
|
|
|
const scrollBehavior = prefersReducedMotion() ? 'smooth' : undefined;
|
2022-03-09 17:35:25 +00:00
|
|
|
|
|
|
|
// Manually increment and decrement TOC scroll rather than using scrollToView
|
|
|
|
// in order to account for threshold
|
|
|
|
if ( linkHiddenTopValue + hiddenThreshold > 0 ) {
|
|
|
|
props.container.scrollTo( {
|
|
|
|
top: props.container.scrollTop - linkHiddenTopValue - midpoint,
|
|
|
|
behavior: scrollBehavior
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
if ( linkHiddenBottomValue + hiddenThreshold > 0 ) {
|
|
|
|
props.container.scrollTo( {
|
|
|
|
top: props.container.scrollTop + linkHiddenBottomValue + midpoint,
|
|
|
|
behavior: scrollBehavior
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-07 19:20:17 +00:00
|
|
|
/**
|
|
|
|
* Adds the `EXPANDED_SECTION_CLASS` CSS class name
|
|
|
|
* to a top level heading in the ToC.
|
|
|
|
*
|
|
|
|
* @param {string} id
|
|
|
|
*/
|
|
|
|
function expandSection( id ) {
|
2022-01-21 20:15:34 +00:00
|
|
|
const tocSection = document.getElementById( id );
|
|
|
|
|
|
|
|
if ( !tocSection ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-21 15:52:31 +00:00
|
|
|
const topSection = /** @type {HTMLElement} */ ( tocSection.closest( `.${TOP_SECTION_CLASS}` ) );
|
2023-03-03 08:32:02 +00:00
|
|
|
const toggle = topSection.querySelector( `.${TOGGLE_CLASS}` );
|
2022-01-21 20:15:34 +00:00
|
|
|
|
2022-09-21 15:52:31 +00:00
|
|
|
if ( topSection && toggle && expandedSections.indexOf( topSection ) < 0 ) {
|
2022-03-29 20:51:45 +00:00
|
|
|
toggle.setAttribute( 'aria-expanded', 'true' );
|
2022-09-21 15:52:31 +00:00
|
|
|
topSection.classList.add( EXPANDED_SECTION_CLASS );
|
|
|
|
expandedSections.push( topSection );
|
2022-01-21 20:15:34 +00:00
|
|
|
}
|
2022-02-07 19:20:17 +00:00
|
|
|
}
|
2022-01-21 20:15:34 +00:00
|
|
|
|
2022-02-07 19:20:17 +00:00
|
|
|
/**
|
|
|
|
* Get the IDs of expanded sections.
|
|
|
|
*
|
|
|
|
* @return {Array<string>}
|
|
|
|
*/
|
|
|
|
function getExpandedSectionIds() {
|
|
|
|
return expandedSections.map( ( s ) => s.id );
|
|
|
|
}
|
2022-01-21 20:15:34 +00:00
|
|
|
|
2022-02-07 19:20:17 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {string} id
|
|
|
|
*/
|
|
|
|
function changeActiveSection( id ) {
|
|
|
|
|
|
|
|
const { parent: activeParentId, child: activeChildId } = getActiveSectionIds();
|
|
|
|
|
|
|
|
if ( id === activeParentId && id === activeChildId ) {
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
deactivateSections();
|
|
|
|
activateSection( id );
|
2022-03-09 17:35:25 +00:00
|
|
|
scrollToActiveSection( id );
|
2022-01-21 20:15:34 +00:00
|
|
|
}
|
2022-02-07 19:20:17 +00:00
|
|
|
}
|
2022-01-21 20:15:34 +00:00
|
|
|
|
2022-02-07 19:20:17 +00:00
|
|
|
/**
|
|
|
|
* @param {string} id
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
function isTopLevelSection( id ) {
|
|
|
|
const section = document.getElementById( id );
|
2022-09-20 21:32:53 +00:00
|
|
|
return !!section && section.classList.contains( TOP_SECTION_CLASS );
|
2022-01-21 20:15:34 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 19:20:17 +00:00
|
|
|
/**
|
|
|
|
* Removes all `EXPANDED_SECTION_CLASS` CSS class names
|
|
|
|
* from the top level sections in the ToC.
|
|
|
|
*
|
|
|
|
* @param {Array<string>} [selectedIds]
|
|
|
|
*/
|
|
|
|
function collapseSections( selectedIds ) {
|
|
|
|
const sectionIdsToCollapse = selectedIds || getExpandedSectionIds();
|
|
|
|
expandedSections = expandedSections.filter( function ( section ) {
|
2022-03-29 20:51:45 +00:00
|
|
|
const isSelected = sectionIdsToCollapse.indexOf( section.id ) > -1;
|
|
|
|
const toggle = isSelected ? section.getElementsByClassName( TOGGLE_CLASS ) : undefined;
|
|
|
|
if ( isSelected && toggle && toggle.length > 0 ) {
|
|
|
|
toggle[ 0 ].setAttribute( 'aria-expanded', 'false' );
|
2022-02-07 19:20:17 +00:00
|
|
|
section.classList.remove( EXPANDED_SECTION_CLASS );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} id
|
|
|
|
*/
|
|
|
|
function toggleExpandSection( id ) {
|
|
|
|
const expandedSectionIds = getExpandedSectionIds();
|
|
|
|
const indexOfExpandedSectionId = expandedSectionIds.indexOf( id );
|
|
|
|
if ( isTopLevelSection( id ) ) {
|
|
|
|
if ( indexOfExpandedSectionId >= 0 ) {
|
|
|
|
collapseSections( [ id ] );
|
|
|
|
} else {
|
|
|
|
expandSection( id );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-29 20:51:45 +00:00
|
|
|
/**
|
|
|
|
* Set aria-expanded attribute for all toggle buttons.
|
|
|
|
*/
|
|
|
|
function initializeExpandedStatus() {
|
2022-09-20 21:32:53 +00:00
|
|
|
const parentSections = props.container.querySelectorAll( `.${TOP_SECTION_CLASS}` );
|
2022-03-29 20:51:45 +00:00
|
|
|
parentSections.forEach( ( section ) => {
|
2023-01-13 20:56:22 +00:00
|
|
|
const expanded = section.classList.contains( EXPANDED_SECTION_CLASS );
|
2022-03-29 20:51:45 +00:00
|
|
|
const toggle = section.querySelector( `.${TOGGLE_CLASS}` );
|
|
|
|
if ( toggle ) {
|
|
|
|
toggle.setAttribute( 'aria-expanded', expanded.toString() );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2023-03-03 08:32:02 +00:00
|
|
|
/**
|
|
|
|
* Event handler for hash change event.
|
|
|
|
*/
|
|
|
|
function handleHashChange() {
|
|
|
|
const hash = location.hash.slice( 1 );
|
2023-03-28 18:45:35 +00:00
|
|
|
const listItem = mw.util.getTargetFromFragment( `${SECTION_ID_PREFIX}${hash}` );
|
2023-03-03 08:32:02 +00:00
|
|
|
|
|
|
|
if ( !listItem ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
expandSection( listItem.id );
|
|
|
|
changeActiveSection( listItem.id );
|
|
|
|
|
|
|
|
props.onHashChange( listItem.id );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Bind event listener for hash change events that match the hash of
|
|
|
|
* LINK_CLASS.
|
|
|
|
*
|
|
|
|
* Note that if the user clicks a link inside the TOC, the onHeadingClick
|
|
|
|
* callback will fire instead of the onHashChange callback, since it takes
|
|
|
|
* precedence.
|
|
|
|
*/
|
|
|
|
function bindHashChangeListener() {
|
|
|
|
window.addEventListener( 'hashchange', handleHashChange );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unbinds event listener for hash change events.
|
|
|
|
*/
|
|
|
|
function unbindHashChangeListener() {
|
|
|
|
window.removeEventListener( 'hashchange', handleHashChange );
|
|
|
|
}
|
|
|
|
|
2022-07-26 20:57:51 +00:00
|
|
|
/**
|
|
|
|
* Bind event listener for clicking on show/hide Table of Contents links.
|
|
|
|
*/
|
2022-10-20 21:32:07 +00:00
|
|
|
function bindPinnedToggleListeners() {
|
2022-11-28 20:38:57 +00:00
|
|
|
const toggleButtons = document.querySelectorAll( '.vector-toc-pinnable-header button' );
|
2022-11-21 20:46:04 +00:00
|
|
|
toggleButtons.forEach( function ( btn ) {
|
2022-07-26 20:57:51 +00:00
|
|
|
btn.addEventListener( 'click', () => {
|
2022-10-20 21:32:07 +00:00
|
|
|
props.onTogglePinned();
|
2022-07-26 20:57:51 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2022-03-29 20:51:45 +00:00
|
|
|
/**
|
|
|
|
* Bind event listeners for clicking on section headings and toggle buttons.
|
|
|
|
*/
|
2022-07-26 20:57:51 +00:00
|
|
|
function bindSubsectionToggleListeners() {
|
2022-01-21 20:15:34 +00:00
|
|
|
props.container.addEventListener( 'click', function ( e ) {
|
|
|
|
if (
|
2022-02-15 22:43:04 +00:00
|
|
|
!( e.target instanceof HTMLElement )
|
2022-01-21 20:15:34 +00:00
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const tocSection =
|
2022-02-15 22:43:04 +00:00
|
|
|
/** @type {HTMLElement | null} */ ( e.target.closest( `.${SECTION_CLASS}` ) );
|
2022-01-21 20:15:34 +00:00
|
|
|
|
|
|
|
if ( tocSection && tocSection.id ) {
|
2022-05-02 18:33:59 +00:00
|
|
|
// In case section link contains HTML,
|
|
|
|
// test if click occurs on any child elements.
|
|
|
|
if ( e.target.closest( `.${LINK_CLASS}` ) ) {
|
2023-03-03 08:32:02 +00:00
|
|
|
// Temporarily unbind the hash change listener to avoid redundant
|
|
|
|
// behavior caused by firing both the onHeadingClick callback and the
|
|
|
|
// onHashChange callback. Instead, only fire the onHeadingClick
|
|
|
|
// callback.
|
|
|
|
unbindHashChangeListener();
|
|
|
|
|
|
|
|
expandSection( tocSection.id );
|
|
|
|
changeActiveSection( tocSection.id );
|
2022-02-15 22:43:04 +00:00
|
|
|
props.onHeadingClick( tocSection.id );
|
2023-03-03 08:32:02 +00:00
|
|
|
|
|
|
|
deferUntilFrame( () => {
|
|
|
|
bindHashChangeListener();
|
|
|
|
}, 3 );
|
2022-02-15 22:43:04 +00:00
|
|
|
}
|
2022-05-02 18:33:59 +00:00
|
|
|
// Toggle button does not contain child elements,
|
|
|
|
// so classList check will suffice.
|
2023-04-27 20:39:40 +00:00
|
|
|
if ( e.target.closest( `.${TOGGLE_CLASS}` ) ) {
|
2023-03-03 08:32:02 +00:00
|
|
|
toggleExpandSection( tocSection.id );
|
|
|
|
if ( props.onToggleClick ) {
|
|
|
|
props.onToggleClick( tocSection.id );
|
|
|
|
}
|
2022-02-15 22:43:04 +00:00
|
|
|
}
|
2022-01-21 20:15:34 +00:00
|
|
|
}
|
2022-02-15 22:43:04 +00:00
|
|
|
|
2022-01-21 20:15:34 +00:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2022-02-18 00:19:50 +00:00
|
|
|
/**
|
|
|
|
* Binds event listeners and sets the default state of the component.
|
|
|
|
*/
|
|
|
|
function initialize() {
|
|
|
|
// Sync component state to the default rendered state of the table of contents.
|
|
|
|
expandedSections = Array.from(
|
|
|
|
props.container.querySelectorAll( `.${EXPANDED_SECTION_CLASS}` )
|
|
|
|
);
|
|
|
|
|
2022-03-29 20:51:45 +00:00
|
|
|
// Initialize toggle buttons aria-expanded attribute.
|
|
|
|
initializeExpandedStatus();
|
|
|
|
|
2022-02-18 00:19:50 +00:00
|
|
|
// Bind event listeners.
|
2022-07-26 20:57:51 +00:00
|
|
|
bindSubsectionToggleListeners();
|
2022-10-20 21:32:07 +00:00
|
|
|
bindPinnedToggleListeners();
|
2023-03-03 08:32:02 +00:00
|
|
|
bindHashChangeListener();
|
2022-08-15 16:34:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reexpands all sections that were expanded before the table of contents was reloaded.
|
|
|
|
* Edited Sections are not reexpanded, as the ID of the edited section is changed after reload.
|
|
|
|
*/
|
|
|
|
function reExpandSections() {
|
|
|
|
initializeExpandedStatus();
|
|
|
|
const expandedSectionIds = getExpandedSectionIds();
|
|
|
|
for ( const id of expandedSectionIds ) {
|
|
|
|
expandSection( id );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-14 18:01:14 +00:00
|
|
|
/**
|
|
|
|
* Updates button styling for the TOC toggle button when scrolled below the page title
|
|
|
|
*
|
|
|
|
* @param {boolean} scrollBelow
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
function updateTocToggleStyles( scrollBelow ) {
|
|
|
|
const TOC_TITLEBAR_TOGGLE_ID = 'vector-page-titlebar-toc-label';
|
2023-04-27 20:39:40 +00:00
|
|
|
const QUIET_BUTTON_CLASS = 'cdx-button--weight-quiet';
|
2023-02-14 18:01:14 +00:00
|
|
|
const tocToggle = document.getElementById( TOC_TITLEBAR_TOGGLE_ID );
|
|
|
|
if ( tocToggle ) {
|
|
|
|
if ( scrollBelow ) {
|
|
|
|
tocToggle.classList.remove( QUIET_BUTTON_CLASS );
|
|
|
|
} else {
|
|
|
|
tocToggle.classList.add( QUIET_BUTTON_CLASS );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-15 16:34:28 +00:00
|
|
|
/**
|
|
|
|
* Reloads the table of contents from saved data
|
|
|
|
*
|
|
|
|
* @param {Section[]} sections
|
2023-02-16 01:20:00 +00:00
|
|
|
* @return {Promise<any>}
|
2022-08-15 16:34:28 +00:00
|
|
|
*/
|
|
|
|
function reloadTableOfContents( sections ) {
|
2022-08-26 14:47:16 +00:00
|
|
|
if ( sections.length < 1 ) {
|
2023-01-16 11:39:05 +00:00
|
|
|
reloadPartialHTML( TOC_CONTENTS_ID, '' );
|
2023-02-16 01:20:00 +00:00
|
|
|
return Promise.resolve( [] );
|
2022-08-26 14:47:16 +00:00
|
|
|
}
|
2023-02-16 01:20:00 +00:00
|
|
|
const load = () => mw.loader.using( 'mediawiki.template.mustache' ).then( () => {
|
2023-01-16 11:39:05 +00:00
|
|
|
const { parent: activeParentId, child: activeChildId } = getActiveSectionIds();
|
|
|
|
reloadPartialHTML( TOC_CONTENTS_ID, getTableOfContentsHTML( sections ) );
|
2022-08-15 16:34:28 +00:00
|
|
|
// Reexpand sections that were expanded before the table of contents was reloaded.
|
|
|
|
reExpandSections();
|
2023-01-16 11:39:05 +00:00
|
|
|
// reActivate the active sections
|
|
|
|
deactivateSections();
|
|
|
|
if ( activeParentId ) {
|
|
|
|
activateSection( activeParentId );
|
|
|
|
}
|
|
|
|
if ( activeChildId ) {
|
|
|
|
activateSection( activeChildId );
|
|
|
|
}
|
2022-06-10 21:20:07 +00:00
|
|
|
} );
|
2023-02-16 01:20:00 +00:00
|
|
|
return new Promise( ( resolve ) => {
|
|
|
|
load().then( () => {
|
|
|
|
resolve( sections );
|
|
|
|
} );
|
|
|
|
} );
|
2022-02-18 00:19:50 +00:00
|
|
|
}
|
|
|
|
|
2022-08-15 16:34:28 +00:00
|
|
|
/**
|
|
|
|
* Replaces the contents of the given element with the given HTML
|
|
|
|
*
|
|
|
|
* @param {string} elementId
|
2022-08-26 14:47:16 +00:00
|
|
|
* @param {string} html
|
2022-08-15 16:34:28 +00:00
|
|
|
*/
|
2022-09-12 23:45:24 +00:00
|
|
|
function reloadPartialHTML( elementId, html ) {
|
2022-08-15 16:34:28 +00:00
|
|
|
const htmlElement = document.getElementById( elementId );
|
2023-01-16 11:39:05 +00:00
|
|
|
if ( htmlElement && html ) {
|
2022-09-12 23:45:24 +00:00
|
|
|
htmlElement.innerHTML = html;
|
2022-08-15 16:34:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates the HTML for the table of contents.
|
|
|
|
*
|
|
|
|
* @param {Section[]} sections
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
function getTableOfContentsHTML( sections ) {
|
|
|
|
return getTableOfContentsListHtml( getTableOfContentsData( sections ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates the table of contents List HTML from the templates
|
|
|
|
*
|
|
|
|
* @param {Object} data
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
function getTableOfContentsListHtml( data ) {
|
|
|
|
const mustacheCompiler = mw.template.getCompiler( 'mustache' );
|
2023-01-16 11:39:05 +00:00
|
|
|
const compiledTemplateTocContents = mustacheCompiler.compile( templateTocContents );
|
2022-08-15 16:34:28 +00:00
|
|
|
|
|
|
|
// Identifier 'TableOfContents__line' is not in camel case
|
|
|
|
// (template name is 'TableOfContents__line')
|
|
|
|
const partials = {
|
2023-01-11 01:54:03 +00:00
|
|
|
// eslint-disable-next-line camelcase
|
2023-01-16 11:39:05 +00:00
|
|
|
TableOfContents__line: mustacheCompiler.compile( templateTocLine )
|
2022-08-15 16:34:28 +00:00
|
|
|
};
|
|
|
|
|
2023-01-16 11:39:05 +00:00
|
|
|
return compiledTemplateTocContents.render( data, partials ).html();
|
2022-08-15 16:34:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Section[]} sections
|
|
|
|
* @return {SectionsListData}
|
|
|
|
*/
|
|
|
|
function getTableOfContentsData( sections ) {
|
2023-04-04 07:54:13 +00:00
|
|
|
const tableOfContentsLevel1Sections = getTableOfContentsSectionsData( sections, 1 );
|
2022-08-15 16:34:28 +00:00
|
|
|
return {
|
|
|
|
'msg-vector-toc-beginning': mw.message( 'vector-toc-beginning' ).text(),
|
2023-04-04 07:54:13 +00:00
|
|
|
'array-sections': tableOfContentsLevel1Sections,
|
2023-05-08 22:10:52 +00:00
|
|
|
'vector-is-collapse-sections-enabled': tableOfContentsLevel1Sections.length > 3 && sections.length >= tableOfContentsConfig.VectorTableOfContentsCollapseAtCount
|
2022-08-15 16:34:28 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prepares the data for rendering the table of contents,
|
|
|
|
* nesting child sections within their parent sections.
|
2022-12-13 20:28:22 +00:00
|
|
|
* This should yield the same result as the php function
|
|
|
|
* VectorComponentTableOfContents::getTemplateData(),
|
2022-08-15 16:34:28 +00:00
|
|
|
* please make sure to keep them in sync.
|
|
|
|
*
|
|
|
|
* @param {Section[]} sections
|
|
|
|
* @param {number} toclevel
|
|
|
|
* @return {Section[]}
|
|
|
|
*/
|
|
|
|
function getTableOfContentsSectionsData( sections, toclevel = 1 ) {
|
|
|
|
const data = [];
|
|
|
|
for ( let i = 0; i < sections.length; i++ ) {
|
|
|
|
const section = sections[ i ];
|
|
|
|
if ( section.toclevel === toclevel ) {
|
|
|
|
const childSections = getTableOfContentsSectionsData(
|
|
|
|
sections.slice( i + 1 ),
|
|
|
|
toclevel + 1
|
|
|
|
);
|
|
|
|
section[ 'array-sections' ] = childSections;
|
|
|
|
section[ 'is-top-level-section' ] = toclevel === 1;
|
|
|
|
section[ 'is-parent-section' ] = Object.keys( childSections ).length > 0;
|
|
|
|
data.push( section );
|
|
|
|
}
|
|
|
|
// Child section belongs to a higher parent.
|
|
|
|
if ( section.toclevel < toclevel ) {
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2023-03-03 08:32:02 +00:00
|
|
|
/**
|
|
|
|
* Cleans up the hash change event listener to prevent memory leaks. This
|
|
|
|
* should be called when the table of contents is permanently no longer
|
|
|
|
* needed.
|
|
|
|
*
|
|
|
|
* @ignore
|
|
|
|
*/
|
|
|
|
function unmount() {
|
|
|
|
unbindHashChangeListener();
|
|
|
|
}
|
|
|
|
|
2022-02-18 00:19:50 +00:00
|
|
|
initialize();
|
2022-01-21 20:15:34 +00:00
|
|
|
|
2022-02-07 19:20:17 +00:00
|
|
|
/**
|
|
|
|
* @typedef {Object} TableOfContents
|
2023-02-16 01:20:00 +00:00
|
|
|
* @property {reloadTableOfContents} reloadTableOfContents
|
2022-02-07 19:20:17 +00:00
|
|
|
* @property {changeActiveSection} changeActiveSection
|
|
|
|
* @property {expandSection} expandSection
|
|
|
|
* @property {toggleExpandSection} toggleExpandSection
|
2023-02-14 18:01:14 +00:00
|
|
|
* @property {updateTocToggleStyles} updateTocToggleStyles
|
2023-03-03 08:32:02 +00:00
|
|
|
* @property {unmount} unmount
|
2022-02-07 19:20:17 +00:00
|
|
|
* @property {string} ACTIVE_SECTION_CLASS
|
2022-09-20 21:32:53 +00:00
|
|
|
* @property {string} ACTIVE_TOP_SECTION_CLASS
|
2022-02-07 19:20:17 +00:00
|
|
|
* @property {string} EXPANDED_SECTION_CLASS
|
2022-02-15 22:43:04 +00:00
|
|
|
* @property {string} LINK_CLASS
|
|
|
|
* @property {string} TOGGLE_CLASS
|
2022-02-07 19:20:17 +00:00
|
|
|
*/
|
2022-01-21 20:15:34 +00:00
|
|
|
return {
|
2023-02-16 01:20:00 +00:00
|
|
|
reloadTableOfContents,
|
2022-02-07 19:20:17 +00:00
|
|
|
expandSection,
|
|
|
|
changeActiveSection,
|
|
|
|
toggleExpandSection,
|
2023-02-14 18:01:14 +00:00
|
|
|
updateTocToggleStyles,
|
2023-03-03 08:32:02 +00:00
|
|
|
unmount,
|
2022-02-07 19:20:17 +00:00
|
|
|
ACTIVE_SECTION_CLASS,
|
2022-09-20 21:32:53 +00:00
|
|
|
ACTIVE_TOP_SECTION_CLASS,
|
2022-02-15 22:43:04 +00:00
|
|
|
EXPANDED_SECTION_CLASS,
|
|
|
|
LINK_CLASS,
|
|
|
|
TOGGLE_CLASS
|
2022-01-21 20:15:34 +00:00
|
|
|
};
|
|
|
|
};
|