| 1 |
|
| 2 |
const |
| 3 |
stickyHeader = require( './stickyHeader.js' ), |
| 4 |
scrollObserver = require( './scrollObserver.js' ), |
| 5 |
initExperiment = require( './AB.js' ), |
| 6 |
initSectionObserver = require( './sectionObserver.js' ), |
| 7 |
initTableOfContents = require( './tableOfContents.js' ), |
| 8 |
pinnableElement = require( './pinnableElement.js' ), |
| 9 |
popupNotification = require( './popupNotification.js' ), |
| 10 |
features = require( './features.js' ), |
| 11 |
deferUntilFrame = require( './deferUntilFrame.js' ), |
| 12 |
ABTestConfig = require( ( './activeABTest.json' ) ), |
| 13 |
STICKY_HEADER_VISIBLE_CLASS = 'vector-sticky-header-visible', |
| 14 |
TOC_ID = 'vector-toc', |
| 15 |
BODY_CONTENT_ID = 'bodyContent', |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
HEADING_TAGS = [ 'h1', 'h2', 'h3', 'h4', 'h5', 'h6' ], |
| 21 |
HEADING_SELECTOR = [ '.mw-heading', ...HEADING_TAGS.map( ( tag ) => `${ tag }:not([id])` ) ] |
| 22 |
.map( ( sel ) => `.mw-parser-output ${ sel }` ).join( ', ' ), |
| 23 |
HEADLINE_SELECTOR = [ '.mw-headline', ...HEADING_TAGS.map( ( tag ) => `${ tag }[id]` ) ] |
| 24 |
.map( ( sel ) => `.mw-parser-output ${ sel }` ).join( ', ' ), |
| 25 |
TOC_SECTION_ID_PREFIX = 'toc-', |
| 26 |
PAGE_TITLE_INTERSECTION_CLASS = 'vector-below-page-title'; |
| 27 |
|
| 28 |
const belowDesktopMedia = window.matchMedia( '(max-width: 1119px)' ); |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
const getHeadingIntersectionHandler = ( changeActiveSection ) => |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
( section ) => { |
| 46 |
const headline = section.classList.contains( 'mw-body-content' ) ? |
| 47 |
section : |
| 48 |
section.querySelector( HEADLINE_SELECTOR ); |
| 49 |
if ( headline ) { |
| 50 |
changeActiveSection( `${ TOC_SECTION_ID_PREFIX }${ headline.id }` ); |
| 51 |
} |
| 52 |
}; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
function initStickyHeaderABTests( abConfig, isStickyHeaderFeatureAllowed, getEnabledExperiment ) { |
| 66 |
let showStickyHeader = isStickyHeaderFeatureAllowed, |
| 67 |
stickyHeaderExperiment; |
| 68 |
|
| 69 |
|
| 70 |
const abTestName = abConfig.name, |
| 71 |
isStickyHeaderExperiment = abTestName === stickyHeader.STICKY_HEADER_EXPERIMENT_NAME; |
| 72 |
|
| 73 |
|
| 74 |
if ( |
| 75 |
isStickyHeaderFeatureAllowed && |
| 76 |
abConfig.enabled && |
| 77 |
isStickyHeaderExperiment |
| 78 |
) { |
| 79 |
|
| 80 |
stickyHeaderExperiment = getEnabledExperiment( abConfig ); |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
if ( abTestName === stickyHeader.STICKY_HEADER_EXPERIMENT_NAME ) { |
| 85 |
showStickyHeader = stickyHeaderExperiment.isInTreatmentBucket(); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
return { |
| 90 |
showStickyHeader |
| 91 |
}; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
const updateTocLocation = () => { |
| 101 |
const isPinned = features.isEnabled( 'toc-pinned' ); |
| 102 |
const isStickyHeaderVisible = document.body.classList.contains( STICKY_HEADER_VISIBLE_CLASS ); |
| 103 |
const isBelowDesktop = belowDesktopMedia.matches; |
| 104 |
|
| 105 |
const pinnedContainerId = 'vector-toc-pinned-container'; |
| 106 |
const stickyHeaderUnpinnedContainerId = 'vector-sticky-header-toc-unpinned-container'; |
| 107 |
const pageTitlebarUnpinnedContainerId = 'vector-page-titlebar-toc-unpinned-container'; |
| 108 |
|
| 109 |
let newContainerId = ''; |
| 110 |
if ( isPinned ) { |
| 111 |
if ( isBelowDesktop ) { |
| 112 |
|
| 113 |
newContainerId = pageTitlebarUnpinnedContainerId; |
| 114 |
} else { |
| 115 |
newContainerId = pinnedContainerId; |
| 116 |
} |
| 117 |
} else { |
| 118 |
if ( isStickyHeaderVisible && !isBelowDesktop ) { |
| 119 |
newContainerId = stickyHeaderUnpinnedContainerId; |
| 120 |
} else { |
| 121 |
newContainerId = pageTitlebarUnpinnedContainerId; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
pinnableElement.movePinnableElement( TOC_ID, newContainerId ); |
| 126 |
}; |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
function getDocumentScrollPaddingTop() { |
| 137 |
const defaultScrollPaddingTop = 75; |
| 138 |
const documentStyles = getComputedStyle( document.documentElement ); |
| 139 |
const scrollPaddingTopString = documentStyles.getPropertyValue( 'scroll-padding-top' ); |
| 140 |
return ( parseInt( scrollPaddingTopString, 10 ) || defaultScrollPaddingTop ); |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
const setupTableOfContents = ( tocElement, bodyContent, initSectionObserverFn ) => { |
| 150 |
if ( !( |
| 151 |
tocElement && |
| 152 |
bodyContent |
| 153 |
) ) { |
| 154 |
return null; |
| 155 |
} |
| 156 |
|
| 157 |
const handleTocSectionChange = () => { |
| 158 |
|
| 159 |
sectionObserver.pause(); |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
deferUntilFrame( () => { |
| 182 |
|
| 183 |
sectionObserver.resume(); |
| 184 |
}, 3 ); |
| 185 |
}; |
| 186 |
|
| 187 |
const tableOfContents = initTableOfContents( { |
| 188 |
container: tocElement, |
| 189 |
onHeadingClick: handleTocSectionChange, |
| 190 |
onHashChange: handleTocSectionChange, |
| 191 |
onTogglePinned: () => { |
| 192 |
updateTocLocation(); |
| 193 |
pinnableElement.setFocusAfterToggle( TOC_ID ); |
| 194 |
if ( !features.isEnabled( 'toc-pinned' ) ) { |
| 195 |
const isStickyHeaderVisible = document.body.classList |
| 196 |
.contains( STICKY_HEADER_VISIBLE_CLASS ); |
| 197 |
const containerSelector = !isStickyHeaderVisible ? |
| 198 |
'.vector-page-titlebar .vector-toc-landmark' : '#vector-sticky-header .vector-toc-landmark'; |
| 199 |
const container = ( |
| 200 |
document.querySelector( containerSelector ) |
| 201 |
); |
| 202 |
if ( container ) { |
| 203 |
const containerId = !isStickyHeaderVisible ? 'toc-page-titlebar' : 'toc-sticky-header'; |
| 204 |
popupNotification.add( container, mw.message( 'vector-toc-unpinned-popup' ).text(), containerId ) |
| 205 |
.then( ( popupWidget ) => { |
| 206 |
if ( popupWidget ) { |
| 207 |
popupNotification.show( popupWidget ); |
| 208 |
} |
| 209 |
} ); |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
} |
| 214 |
} ); |
| 215 |
const elements = () => bodyContent.querySelectorAll( `${ HEADING_SELECTOR }, .mw-body-content` ); |
| 216 |
|
| 217 |
const sectionObserver = initSectionObserverFn( { |
| 218 |
elements: elements(), |
| 219 |
topMargin: getDocumentScrollPaddingTop(), |
| 220 |
onIntersection: getHeadingIntersectionHandler( tableOfContents.changeActiveSection ) |
| 221 |
} ); |
| 222 |
const updateElements = () => { |
| 223 |
sectionObserver.resume(); |
| 224 |
sectionObserver.setElements( elements() ); |
| 225 |
}; |
| 226 |
mw.hook( 've.activationStart' ).add( () => { |
| 227 |
sectionObserver.pause(); |
| 228 |
} ); |
| 229 |
mw.hook( 'wikipage.tableOfContents' ).add( ( sections ) => { |
| 230 |
tableOfContents.reloadTableOfContents( sections ).then( () => { |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
mw.hook( 'wikipage.tableOfContents.vector' ).fire( sections ); |
| 236 |
updateElements(); |
| 237 |
} ); |
| 238 |
} ); |
| 239 |
mw.hook( 've.deactivationComplete' ).add( updateElements ); |
| 240 |
|
| 241 |
const setInitialActiveSection = () => { |
| 242 |
const hash = location.hash.slice( 1 ); |
| 243 |
|
| 244 |
|
| 245 |
if ( hash === '' ) { |
| 246 |
sectionObserver.calcIntersection(); |
| 247 |
return; |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
const hashSection = ( mw.util.getTargetFromFragment( `${ TOC_SECTION_ID_PREFIX }${ hash }` ) ); |
| 253 |
if ( hashSection ) { |
| 254 |
tableOfContents.expandSection( hashSection.id ); |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
if ( |
| 267 |
hashSection && |
| 268 |
Math.round( window.innerHeight + window.scrollY ) >= document.body.scrollHeight |
| 269 |
) { |
| 270 |
tableOfContents.changeActiveSection( hashSection.id ); |
| 271 |
} else { |
| 272 |
|
| 273 |
sectionObserver.calcIntersection(); |
| 274 |
} |
| 275 |
}; |
| 276 |
|
| 277 |
setInitialActiveSection(); |
| 278 |
|
| 279 |
return tableOfContents; |
| 280 |
}; |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
const main = () => { |
| 286 |
const isIntersectionObserverSupported = 'IntersectionObserver' in window; |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
const tocElement = document.getElementById( TOC_ID ); |
| 292 |
const bodyContent = document.getElementById( BODY_CONTENT_ID ); |
| 293 |
|
| 294 |
const isToCUpdatingAllowed = isIntersectionObserverSupported && |
| 295 |
window.requestAnimationFrame; |
| 296 |
const tableOfContents = isToCUpdatingAllowed ? |
| 297 |
setupTableOfContents( tocElement, bodyContent, initSectionObserver ) : null; |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
const |
| 303 |
stickyHeaderElement = document.getElementById( stickyHeader.STICKY_HEADER_ID ), |
| 304 |
stickyIntersection = document.getElementById( stickyHeader.FIRST_HEADING_ID ), |
| 305 |
userLinksDropdown = document.getElementById( stickyHeader.USER_LINKS_DROPDOWN_ID ), |
| 306 |
allowedNamespace = stickyHeader.isAllowedNamespace( mw.config.get( 'wgNamespaceNumber' ) ), |
| 307 |
allowedAction = stickyHeader.isAllowedAction( mw.config.get( 'wgAction' ) ); |
| 308 |
|
| 309 |
const isStickyHeaderAllowed = |
| 310 |
!!stickyHeaderElement && |
| 311 |
!!stickyIntersection && |
| 312 |
!!userLinksDropdown && |
| 313 |
allowedNamespace && |
| 314 |
allowedAction && |
| 315 |
isIntersectionObserverSupported; |
| 316 |
|
| 317 |
const { showStickyHeader } = initStickyHeaderABTests( |
| 318 |
ABTestConfig, |
| 319 |
isStickyHeaderAllowed && !mw.user.isAnon(), |
| 320 |
( config ) => initExperiment( |
| 321 |
config, |
| 322 |
String( mw.user.getId() ) |
| 323 |
) |
| 324 |
); |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
const observer = scrollObserver.initScrollObserver( |
| 329 |
() => { |
| 330 |
if ( isStickyHeaderAllowed && showStickyHeader ) { |
| 331 |
stickyHeader.show(); |
| 332 |
updateTocLocation(); |
| 333 |
} |
| 334 |
document.body.classList.add( PAGE_TITLE_INTERSECTION_CLASS ); |
| 335 |
if ( tableOfContents ) { |
| 336 |
tableOfContents.updateTocToggleStyles( true ); |
| 337 |
} |
| 338 |
scrollObserver.firePageTitleScrollHook( 'down' ); |
| 339 |
}, |
| 340 |
() => { |
| 341 |
if ( isStickyHeaderAllowed && showStickyHeader ) { |
| 342 |
stickyHeader.hide(); |
| 343 |
updateTocLocation(); |
| 344 |
} |
| 345 |
document.body.classList.remove( PAGE_TITLE_INTERSECTION_CLASS ); |
| 346 |
if ( tableOfContents ) { |
| 347 |
tableOfContents.updateTocToggleStyles( false ); |
| 348 |
} |
| 349 |
scrollObserver.firePageTitleScrollHook( 'up' ); |
| 350 |
} |
| 351 |
); |
| 352 |
|
| 353 |
|
| 354 |
belowDesktopMedia.onchange = () => { |
| 355 |
updateTocLocation(); |
| 356 |
}; |
| 357 |
|
| 358 |
updateTocLocation(); |
| 359 |
|
| 360 |
if ( !showStickyHeader ) { |
| 361 |
stickyHeader.hide(); |
| 362 |
} |
| 363 |
|
| 364 |
if ( isStickyHeaderAllowed && showStickyHeader ) { |
| 365 |
stickyHeader.initStickyHeader( { |
| 366 |
header: stickyHeaderElement, |
| 367 |
userLinksDropdown, |
| 368 |
observer, |
| 369 |
stickyIntersection |
| 370 |
} ); |
| 371 |
} else if ( stickyIntersection ) { |
| 372 |
observer.observe( stickyIntersection ); |
| 373 |
} |
| 374 |
}; |
| 375 |
|
| 376 |
module.exports = { |
| 377 |
main, |
| 378 |
test: { |
| 379 |
setupTableOfContents, |
| 380 |
initStickyHeaderABTests, |
| 381 |
getHeadingIntersectionHandler |
| 382 |
} |
| 383 |
}; |