2022-02-14 18:55:50 +00:00
|
|
|
/** @module TableOfContents */
|
|
|
|
|
2022-02-15 22:43:04 +00:00
|
|
|
const SECTION_CLASS = 'sidebar-toc-list-item';
|
2022-01-21 20:15:34 +00:00
|
|
|
const ACTIVE_SECTION_CLASS = 'sidebar-toc-list-item-active';
|
2022-02-07 19:20:17 +00:00
|
|
|
const EXPANDED_SECTION_CLASS = 'sidebar-toc-list-item-expanded';
|
2022-09-20 21:32:53 +00:00
|
|
|
const TOP_SECTION_CLASS = 'sidebar-toc-level-1';
|
|
|
|
const ACTIVE_TOP_SECTION_CLASS = 'sidebar-toc-level-1-active';
|
2022-01-21 20:15:34 +00:00
|
|
|
const LINK_CLASS = 'sidebar-toc-link';
|
2022-02-15 22:43:04 +00:00
|
|
|
const TOGGLE_CLASS = 'sidebar-toc-toggle';
|
2022-07-26 20:57:51 +00:00
|
|
|
const TOC_COLLAPSED_CLASS = 'vector-toc-collapsed';
|
2022-08-23 16:21:11 +00:00
|
|
|
const TOC_NOT_COLLAPSED_CLASS = 'vector-toc-not-collapsed';
|
2022-08-15 16:34:28 +00:00
|
|
|
const TOC_ID = 'mw-panel-toc';
|
|
|
|
/**
|
|
|
|
* TableOfContents Mustache templates
|
|
|
|
*/
|
|
|
|
const templateBody = require( /** @type {string} */ ( './templates/TableOfContents.mustache' ) );
|
|
|
|
const templateTocLine = require( /** @type {string} */ ( './templates/TableOfContents__line.mustache' ) );
|
|
|
|
/**
|
|
|
|
* TableOfContents Config object for filling mustache templates
|
|
|
|
*/
|
|
|
|
const tableOfContentsConfig = require( /** @type {string} */ ( './tableOfContentsConfig.json' ) );
|
2022-02-15 22:43:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @callback onHeadingClick
|
|
|
|
* @param {string} id The id of the clicked list item.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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
|
|
|
/**
|
|
|
|
* @callback onToggleCollapse
|
|
|
|
*/
|
|
|
|
|
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.
|
|
|
|
* @property {onToggleClick} onToggleClick Called when a list item is clicked.
|
2022-07-01 20:19:57 +00:00
|
|
|
* @property {onToggleCollapse} onToggleCollapse Called when collapse 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 {boolean} is-vector-toc-beginning-enabled
|
|
|
|
* @property {Section[]} array-sections
|
|
|
|
* @property {boolean} vector-is-collapse-sections-enabled
|
|
|
|
* @property {string} msg-vector-toc-heading
|
|
|
|
* @property {number} number-section-count
|
|
|
|
* @property {string} msg-vector-toc-beginning
|
|
|
|
* @property {string} msg-vector-toc-toggle-position-title
|
|
|
|
* @property {string} msg-vector-toc-toggle-position-sidebar
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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
|
2022-09-21 15:52:31 +00:00
|
|
|
activeTopSection = /** @type {HTMLElement} */ ( selectedTocSection.closest( `.${TOP_SECTION_CLASS}` ) );
|
|
|
|
activeSubSection = selectedTocSection;
|
2022-09-20 21:32:53 +00:00
|
|
|
activeTopSection.classList.add( ACTIVE_TOP_SECTION_CLASS );
|
|
|
|
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
|
|
|
|
let link = section.firstElementChild;
|
|
|
|
// @ts-ignore
|
|
|
|
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 ) {
|
|
|
|
link = parentSection.firstElementChild;
|
|
|
|
} 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}` ) );
|
2022-03-29 20:51:45 +00:00
|
|
|
const toggle = tocSection.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 ) => {
|
|
|
|
const expanded = section.classList.contains( EXPANDED_SECTION_CLASS );
|
|
|
|
const toggle = section.querySelector( `.${TOGGLE_CLASS}` );
|
|
|
|
if ( toggle ) {
|
|
|
|
toggle.setAttribute( 'aria-expanded', expanded.toString() );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2022-07-26 20:57:51 +00:00
|
|
|
/**
|
|
|
|
* Bind event listener for clicking on show/hide Table of Contents links.
|
|
|
|
*/
|
|
|
|
function bindCollapseToggleListeners() {
|
2022-08-23 16:21:11 +00:00
|
|
|
// Initialize toc collapsed status
|
2022-09-08 19:57:50 +00:00
|
|
|
// FIXME: Remove after I17f2699fec113d92b3f0f223c357654f52eb7eb3 has been in prod for a week
|
2022-08-23 16:21:11 +00:00
|
|
|
document.body.classList.add( TOC_NOT_COLLAPSED_CLASS );
|
|
|
|
|
2022-07-26 20:57:51 +00:00
|
|
|
const showHideTocElement = document.querySelectorAll( '#sidebar-toc-label button' );
|
|
|
|
showHideTocElement.forEach( function ( btn ) {
|
|
|
|
btn.addEventListener( 'click', () => {
|
|
|
|
document.body.classList.toggle( TOC_COLLAPSED_CLASS );
|
2022-08-23 16:21:11 +00:00
|
|
|
document.body.classList.toggle( TOC_NOT_COLLAPSED_CLASS );
|
|
|
|
|
2022-07-01 20:19:57 +00:00
|
|
|
props.onToggleCollapse();
|
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}` ) ) {
|
2022-02-15 22:43:04 +00:00
|
|
|
props.onHeadingClick( tocSection.id );
|
|
|
|
}
|
2022-05-02 18:33:59 +00:00
|
|
|
// Toggle button does not contain child elements,
|
|
|
|
// so classList check will suffice.
|
2022-02-15 22:43:04 +00:00
|
|
|
if ( e.target.classList.contains( TOGGLE_CLASS ) ) {
|
|
|
|
props.onToggleClick( tocSection.id );
|
|
|
|
}
|
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();
|
|
|
|
bindCollapseToggleListeners();
|
2022-06-10 21:20:07 +00:00
|
|
|
|
2022-08-15 16:34:28 +00:00
|
|
|
mw.hook( 'wikipage.tableOfContents' ).add( reloadTableOfContents );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reloads the table of contents from saved data
|
|
|
|
*
|
|
|
|
* @param {Section[]} sections
|
|
|
|
*/
|
|
|
|
function reloadTableOfContents( sections ) {
|
2022-08-26 14:47:16 +00:00
|
|
|
if ( sections.length < 1 ) {
|
|
|
|
reloadPartialHTML( TOC_ID, '' );
|
|
|
|
return;
|
|
|
|
}
|
2022-08-15 16:34:28 +00:00
|
|
|
mw.loader.using( 'mediawiki.template.mustache' ).then( () => {
|
2022-08-26 14:47:16 +00:00
|
|
|
reloadPartialHTML( TOC_ID, getTableOfContentsHTML( sections ) );
|
2022-08-15 16:34:28 +00:00
|
|
|
// Reexpand sections that were expanded before the table of contents was reloaded.
|
|
|
|
reExpandSections();
|
|
|
|
// Initialize Collapse toggle buttons
|
|
|
|
bindCollapseToggleListeners();
|
2022-06-10 21:20:07 +00:00
|
|
|
} );
|
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 );
|
|
|
|
if ( htmlElement ) {
|
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 ) {
|
|
|
|
// @ts-ignore
|
|
|
|
const mustacheCompiler = mw.template.getCompiler( 'mustache' );
|
|
|
|
const compiledTemplateBody = mustacheCompiler.compile( templateBody );
|
|
|
|
const compiledTemplateTocLine = mustacheCompiler.compile( templateTocLine );
|
|
|
|
|
|
|
|
// Identifier 'TableOfContents__line' is not in camel case
|
|
|
|
// (template name is 'TableOfContents__line')
|
|
|
|
const partials = {
|
|
|
|
TableOfContents__line: compiledTemplateTocLine // eslint-disable-line camelcase
|
|
|
|
};
|
|
|
|
|
|
|
|
return compiledTemplateBody.render( data, partials ).html();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Section[]} sections
|
|
|
|
* @return {SectionsListData}
|
|
|
|
*/
|
|
|
|
function getTableOfContentsData( sections ) {
|
|
|
|
return {
|
|
|
|
'number-section-count': sections.length,
|
|
|
|
'msg-vector-toc-heading': mw.message( 'vector-toc-heading' ).text(),
|
|
|
|
'msg-vector-toc-toggle-position-sidebar': mw.message( 'vector-toc-toggle-position-sidebar' ).text(),
|
|
|
|
'msg-vector-toc-toggle-position-title': mw.message( 'vector-toc-toggle-position-title' ).text(),
|
|
|
|
'msg-vector-toc-beginning': mw.message( 'vector-toc-beginning' ).text(),
|
|
|
|
'array-sections': getTableOfContentsSectionsData( sections, 1 ),
|
|
|
|
'vector-is-collapse-sections-enabled': sections.length >= tableOfContentsConfig.VectorTableOfContentsCollapseAtCount,
|
|
|
|
'is-vector-toc-beginning-enabled': tableOfContentsConfig.VectorTableOfContentsBeginning
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prepares the data for rendering the table of contents,
|
|
|
|
* nesting child sections within their parent sections.
|
|
|
|
* This shoul yield the same result as the php function SkinVector22::getTocData(),
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
* @property {changeActiveSection} changeActiveSection
|
|
|
|
* @property {expandSection} expandSection
|
|
|
|
* @property {toggleExpandSection} toggleExpandSection
|
|
|
|
* @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 {
|
2022-02-07 19:20:17 +00:00
|
|
|
expandSection,
|
|
|
|
changeActiveSection,
|
|
|
|
toggleExpandSection,
|
|
|
|
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
|
|
|
};
|
|
|
|
};
|