| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
const templateTocContents = require( ( './templates/TableOfContents__list.mustache' ) ); |
| 7 |
const templateTocLine = require( ( './templates/TableOfContents__line.mustache' ) ); |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
const tableOfContentsConfig = require( ( './tableOfContentsConfig.json' ) ); |
| 12 |
const deferUntilFrame = require( './deferUntilFrame.js' ); |
| 13 |
|
| 14 |
const SECTION_ID_PREFIX = 'toc-'; |
| 15 |
const SECTION_CLASS = 'vector-toc-list-item'; |
| 16 |
const ACTIVE_SECTION_CLASS = 'vector-toc-list-item-active'; |
| 17 |
const EXPANDED_SECTION_CLASS = 'vector-toc-list-item-expanded'; |
| 18 |
const TOP_SECTION_CLASS = 'vector-toc-level-1'; |
| 19 |
const ACTIVE_TOP_SECTION_CLASS = 'vector-toc-level-1-active'; |
| 20 |
const LINK_CLASS = 'vector-toc-link'; |
| 21 |
const TOGGLE_CLASS = 'vector-toc-toggle'; |
| 22 |
const TOC_CONTENTS_ID = 'mw-panel-toc-list'; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
module.exports = function tableOfContents( props ) { |
| 102 |
let activeTopSection; |
| 103 |
let activeSubSection; |
| 104 |
let expandedSections; |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
function getActiveSectionIds() { |
| 118 |
return { |
| 119 |
parent: ( activeTopSection ) ? activeTopSection.id : undefined, |
| 120 |
child: ( activeSubSection ) ? activeSubSection.id : undefined |
| 121 |
}; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
const prefersReducedMotion = () => window.matchMedia( '(prefers-reduced-motion: reduce)' ).matches; |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
function activateSection( id ) { |
| 140 |
const selectedTocSection = document.getElementById( id ); |
| 141 |
const { |
| 142 |
parent: previousActiveTopId, |
| 143 |
child: previousActiveSubSectionId |
| 144 |
} = getActiveSectionIds(); |
| 145 |
|
| 146 |
if ( |
| 147 |
!selectedTocSection || |
| 148 |
( previousActiveTopId === id ) || |
| 149 |
( previousActiveSubSectionId === id ) |
| 150 |
) { |
| 151 |
return; |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
activeTopSection = ( selectedTocSection.closest( `.${ TOP_SECTION_CLASS }` ) ); |
| 156 |
if ( activeTopSection ) { |
| 157 |
|
| 158 |
activeTopSection.classList.add( ACTIVE_TOP_SECTION_CLASS ); |
| 159 |
} |
| 160 |
activeSubSection = selectedTocSection; |
| 161 |
activeSubSection.classList.add( ACTIVE_SECTION_CLASS ); |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
function deactivateSections() { |
| 169 |
if ( activeSubSection ) { |
| 170 |
activeSubSection.classList.remove( ACTIVE_SECTION_CLASS ); |
| 171 |
activeSubSection = undefined; |
| 172 |
} |
| 173 |
if ( activeTopSection ) { |
| 174 |
activeTopSection.classList.remove( ACTIVE_TOP_SECTION_CLASS ); |
| 175 |
activeTopSection = undefined; |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
function scrollToActiveSection( id ) { |
| 185 |
const section = document.getElementById( id ); |
| 186 |
if ( !section ) { |
| 187 |
return; |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
let link = ( section.firstElementChild ); |
| 192 |
if ( link && !link.offsetParent ) { |
| 193 |
|
| 194 |
const { parent: activeTopId } = getActiveSectionIds(); |
| 195 |
const parentSection = document.getElementById( activeTopId || '' ); |
| 196 |
if ( parentSection ) { |
| 197 |
link = ( parentSection.firstElementChild ); |
| 198 |
} else { |
| 199 |
link = null; |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
const isContainerScrollable = props.container.scrollHeight > props.container.clientHeight; |
| 204 |
if ( link && isContainerScrollable ) { |
| 205 |
const containerRect = props.container.getBoundingClientRect(); |
| 206 |
const linkRect = link.getBoundingClientRect(); |
| 207 |
|
| 208 |
|
| 209 |
const hiddenThreshold = 100; |
| 210 |
const midpoint = ( containerRect.bottom - containerRect.top ) / 2; |
| 211 |
const linkHiddenTopValue = containerRect.top - linkRect.top; |
| 212 |
|
| 213 |
|
| 214 |
const linkHiddenBottomValue = linkRect.bottom - |
| 215 |
Math.min( containerRect.bottom, window.innerHeight ); |
| 216 |
|
| 217 |
|
| 218 |
const scrollBehavior = prefersReducedMotion() ? 'smooth' : undefined; |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
if ( linkHiddenTopValue + hiddenThreshold > 0 ) { |
| 223 |
props.container.scrollTo( { |
| 224 |
top: props.container.scrollTop - linkHiddenTopValue - midpoint, |
| 225 |
behavior: scrollBehavior |
| 226 |
} ); |
| 227 |
} |
| 228 |
if ( linkHiddenBottomValue + hiddenThreshold > 0 ) { |
| 229 |
props.container.scrollTo( { |
| 230 |
top: props.container.scrollTop + linkHiddenBottomValue + midpoint, |
| 231 |
behavior: scrollBehavior |
| 232 |
} ); |
| 233 |
} |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
function expandSection( id ) { |
| 244 |
const tocSection = document.getElementById( id ); |
| 245 |
|
| 246 |
if ( !tocSection ) { |
| 247 |
return; |
| 248 |
} |
| 249 |
|
| 250 |
const topSection = ( tocSection.closest( `.${ TOP_SECTION_CLASS }` ) ); |
| 251 |
const toggle = topSection.querySelector( `.${ TOGGLE_CLASS }` ); |
| 252 |
|
| 253 |
if ( topSection && toggle && expandedSections.indexOf( topSection ) < 0 ) { |
| 254 |
toggle.setAttribute( 'aria-expanded', 'true' ); |
| 255 |
topSection.classList.add( EXPANDED_SECTION_CLASS ); |
| 256 |
expandedSections.push( topSection ); |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
function getExpandedSectionIds() { |
| 266 |
return expandedSections.map( ( s ) => s.id ); |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
function changeActiveSection( id ) { |
| 273 |
|
| 274 |
const { parent: activeParentId, child: activeChildId } = getActiveSectionIds(); |
| 275 |
|
| 276 |
if ( id === activeParentId && id === activeChildId ) { |
| 277 |
return; |
| 278 |
} else { |
| 279 |
deactivateSections(); |
| 280 |
activateSection( id ); |
| 281 |
scrollToActiveSection( id ); |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
function isTopLevelSection( id ) { |
| 290 |
const section = document.getElementById( id ); |
| 291 |
return !!section && section.classList.contains( TOP_SECTION_CLASS ); |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
function collapseSections( selectedIds ) { |
| 301 |
const sectionIdsToCollapse = selectedIds || getExpandedSectionIds(); |
| 302 |
expandedSections = expandedSections.filter( ( section ) => { |
| 303 |
const isSelected = sectionIdsToCollapse.indexOf( section.id ) > -1; |
| 304 |
const toggle = isSelected ? section.getElementsByClassName( TOGGLE_CLASS ) : undefined; |
| 305 |
if ( isSelected && toggle && toggle.length > 0 ) { |
| 306 |
toggle[ 0 ].setAttribute( 'aria-expanded', 'false' ); |
| 307 |
section.classList.remove( EXPANDED_SECTION_CLASS ); |
| 308 |
return false; |
| 309 |
} |
| 310 |
return true; |
| 311 |
} ); |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
function toggleExpandSection( id ) { |
| 318 |
const expandedSectionIds = getExpandedSectionIds(); |
| 319 |
const indexOfExpandedSectionId = expandedSectionIds.indexOf( id ); |
| 320 |
if ( isTopLevelSection( id ) ) { |
| 321 |
if ( indexOfExpandedSectionId >= 0 ) { |
| 322 |
collapseSections( [ id ] ); |
| 323 |
} else { |
| 324 |
expandSection( id ); |
| 325 |
} |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
function initializeExpandedStatus() { |
| 333 |
const parentSections = props.container.querySelectorAll( `.${ TOP_SECTION_CLASS }` ); |
| 334 |
parentSections.forEach( ( section ) => { |
| 335 |
const expanded = section.classList.contains( EXPANDED_SECTION_CLASS ); |
| 336 |
const toggle = section.querySelector( `.${ TOGGLE_CLASS }` ); |
| 337 |
if ( toggle ) { |
| 338 |
toggle.setAttribute( 'aria-expanded', expanded.toString() ); |
| 339 |
} |
| 340 |
} ); |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
function handleHashChange() { |
| 347 |
const hash = location.hash.slice( 1 ); |
| 348 |
const listItem = mw.util.getTargetFromFragment( `${ SECTION_ID_PREFIX }${ hash }` ); |
| 349 |
|
| 350 |
if ( !listItem ) { |
| 351 |
return; |
| 352 |
} |
| 353 |
|
| 354 |
expandSection( listItem.id ); |
| 355 |
changeActiveSection( listItem.id ); |
| 356 |
|
| 357 |
props.onHashChange( listItem.id ); |
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
function bindHashChangeListener() { |
| 369 |
window.addEventListener( 'hashchange', handleHashChange ); |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
function unbindHashChangeListener() { |
| 376 |
window.removeEventListener( 'hashchange', handleHashChange ); |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
function bindPinnedToggleListeners() { |
| 383 |
const toggleButtons = document.querySelectorAll( '.vector-toc-pinnable-header button' ); |
| 384 |
toggleButtons.forEach( ( btn ) => { |
| 385 |
btn.addEventListener( 'click', () => { |
| 386 |
props.onTogglePinned(); |
| 387 |
} ); |
| 388 |
} ); |
| 389 |
} |
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
function bindSubsectionToggleListeners() { |
| 395 |
props.container.addEventListener( 'click', ( e ) => { |
| 396 |
if ( |
| 397 |
!( e.target instanceof HTMLElement ) |
| 398 |
) { |
| 399 |
return; |
| 400 |
} |
| 401 |
|
| 402 |
const tocSection = |
| 403 |
( e.target.closest( `.${ SECTION_CLASS }` ) ); |
| 404 |
|
| 405 |
if ( tocSection && tocSection.id ) { |
| 406 |
|
| 407 |
|
| 408 |
if ( e.target.closest( `.${ LINK_CLASS }` ) ) { |
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
unbindHashChangeListener(); |
| 414 |
|
| 415 |
expandSection( tocSection.id ); |
| 416 |
changeActiveSection( tocSection.id ); |
| 417 |
props.onHeadingClick( tocSection.id ); |
| 418 |
|
| 419 |
deferUntilFrame( () => { |
| 420 |
bindHashChangeListener(); |
| 421 |
}, 3 ); |
| 422 |
} |
| 423 |
|
| 424 |
|
| 425 |
if ( e.target.closest( `.${ TOGGLE_CLASS }` ) ) { |
| 426 |
toggleExpandSection( tocSection.id ); |
| 427 |
if ( props.onToggleClick ) { |
| 428 |
props.onToggleClick( tocSection.id ); |
| 429 |
} |
| 430 |
} |
| 431 |
} |
| 432 |
|
| 433 |
} ); |
| 434 |
} |
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
function initialize() { |
| 440 |
|
| 441 |
expandedSections = Array.from( |
| 442 |
props.container.querySelectorAll( `.${ EXPANDED_SECTION_CLASS }` ) |
| 443 |
); |
| 444 |
|
| 445 |
|
| 446 |
initializeExpandedStatus(); |
| 447 |
|
| 448 |
|
| 449 |
bindSubsectionToggleListeners(); |
| 450 |
bindPinnedToggleListeners(); |
| 451 |
bindHashChangeListener(); |
| 452 |
} |
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
function reExpandSections() { |
| 459 |
initializeExpandedStatus(); |
| 460 |
const expandedSectionIds = getExpandedSectionIds(); |
| 461 |
for ( const id of expandedSectionIds ) { |
| 462 |
expandSection( id ); |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
function updateTocToggleStyles( scrollBelow ) { |
| 472 |
const TOC_TITLEBAR_TOGGLE_ID = 'vector-page-titlebar-toc-label'; |
| 473 |
const QUIET_BUTTON_CLASS = 'cdx-button--weight-quiet'; |
| 474 |
const tocToggle = document.getElementById( TOC_TITLEBAR_TOGGLE_ID ); |
| 475 |
if ( tocToggle ) { |
| 476 |
if ( scrollBelow ) { |
| 477 |
tocToggle.classList.remove( QUIET_BUTTON_CLASS ); |
| 478 |
} else { |
| 479 |
tocToggle.classList.add( QUIET_BUTTON_CLASS ); |
| 480 |
} |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
function reloadTableOfContents( sections ) { |
| 491 |
if ( sections.length < 1 ) { |
| 492 |
reloadPartialHTML( TOC_CONTENTS_ID, '' ); |
| 493 |
return Promise.resolve( [] ); |
| 494 |
} |
| 495 |
const load = () => mw.loader.using( 'mediawiki.template.mustache' ).then( () => { |
| 496 |
const { parent: activeParentId, child: activeChildId } = getActiveSectionIds(); |
| 497 |
reloadPartialHTML( TOC_CONTENTS_ID, getTableOfContentsHTML( sections ) ); |
| 498 |
|
| 499 |
reExpandSections(); |
| 500 |
|
| 501 |
deactivateSections(); |
| 502 |
if ( activeParentId ) { |
| 503 |
activateSection( activeParentId ); |
| 504 |
} |
| 505 |
if ( activeChildId ) { |
| 506 |
activateSection( activeChildId ); |
| 507 |
} |
| 508 |
} ); |
| 509 |
return new Promise( ( resolve ) => { |
| 510 |
load().then( () => { |
| 511 |
resolve( sections ); |
| 512 |
} ); |
| 513 |
} ); |
| 514 |
} |
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
function reloadPartialHTML( elementId, html ) { |
| 523 |
const htmlElement = document.getElementById( elementId ); |
| 524 |
if ( htmlElement && html ) { |
| 525 |
htmlElement.innerHTML = html; |
| 526 |
} |
| 527 |
} |
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
function getTableOfContentsHTML( sections ) { |
| 536 |
return getTableOfContentsListHtml( getTableOfContentsData( sections ) ); |
| 537 |
} |
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
function getTableOfContentsListHtml( data ) { |
| 546 |
const mustacheCompiler = mw.template.getCompiler( 'mustache' ); |
| 547 |
const compiledTemplateTocContents = mustacheCompiler.compile( templateTocContents ); |
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
const partials = { |
| 552 |
|
| 553 |
TableOfContents__line: mustacheCompiler.compile( templateTocLine ) |
| 554 |
}; |
| 555 |
|
| 556 |
return compiledTemplateTocContents.render( data, partials ).html(); |
| 557 |
} |
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
function getTableOfContentsData( sections ) { |
| 564 |
const tableOfContentsLevel1Sections = getTableOfContentsSectionsData( sections, 1 ); |
| 565 |
return { |
| 566 |
'msg-vector-toc-beginning': mw.message( 'vector-toc-beginning' ).text(), |
| 567 |
'array-sections': tableOfContentsLevel1Sections, |
| 568 |
'vector-is-collapse-sections-enabled': tableOfContentsLevel1Sections.length > 3 && sections.length >= tableOfContentsConfig.VectorTableOfContentsCollapseAtCount |
| 569 |
}; |
| 570 |
} |
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
function getTableOfContentsSectionsData( sections, toclevel = 1 ) { |
| 584 |
const data = []; |
| 585 |
for ( let i = 0; i < sections.length; i++ ) { |
| 586 |
const section = sections[ i ]; |
| 587 |
if ( section.toclevel === toclevel ) { |
| 588 |
const childSections = getTableOfContentsSectionsData( |
| 589 |
sections.slice( i + 1 ), |
| 590 |
toclevel + 1 |
| 591 |
); |
| 592 |
section[ 'array-sections' ] = childSections; |
| 593 |
section[ 'is-top-level-section' ] = toclevel === 1; |
| 594 |
section[ 'is-parent-section' ] = Object.keys( childSections ).length > 0; |
| 595 |
data.push( section ); |
| 596 |
} |
| 597 |
|
| 598 |
if ( section.toclevel < toclevel ) { |
| 599 |
return data; |
| 600 |
} |
| 601 |
} |
| 602 |
|
| 603 |
return data; |
| 604 |
} |
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
function unmount() { |
| 614 |
unbindHashChangeListener(); |
| 615 |
} |
| 616 |
|
| 617 |
initialize(); |
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
return { |
| 634 |
reloadTableOfContents, |
| 635 |
expandSection, |
| 636 |
changeActiveSection, |
| 637 |
toggleExpandSection, |
| 638 |
updateTocToggleStyles, |
| 639 |
unmount, |
| 640 |
ACTIVE_SECTION_CLASS, |
| 641 |
ACTIVE_TOP_SECTION_CLASS, |
| 642 |
EXPANDED_SECTION_CLASS, |
| 643 |
LINK_CLASS, |
| 644 |
TOGGLE_CLASS |
| 645 |
}; |
| 646 |
}; |