2021-01-16 22:01:58 +00:00
|
|
|
/**
|
2022-05-13 02:37:28 +00:00
|
|
|
* Wait for first paint before calling this function.
|
2021-03-13 17:43:28 +00:00
|
|
|
* (see T234570#5779890, T246419).
|
|
|
|
*
|
|
|
|
* @param {Document} document
|
2021-01-16 22:01:58 +00:00
|
|
|
* @return {void}
|
|
|
|
*/
|
2021-03-13 17:43:28 +00:00
|
|
|
function enableCssAnimations( document ) {
|
|
|
|
document.documentElement.classList.add( 'citizen-animations-ready' );
|
|
|
|
}
|
|
|
|
|
2021-04-26 15:39:27 +00:00
|
|
|
/**
|
2022-10-22 23:58:54 +00:00
|
|
|
* Add a class to indicate that sticky header is active
|
2021-04-26 15:39:27 +00:00
|
|
|
*
|
|
|
|
* @param {Document} document
|
|
|
|
* @return {void}
|
|
|
|
*/
|
2022-10-22 23:58:54 +00:00
|
|
|
function initStickyHeader( document ) {
|
2022-11-10 01:02:21 +00:00
|
|
|
const scrollObserver = require( './scrollObserver.js' );
|
|
|
|
|
|
|
|
// Detect scroll direction and add the right class
|
|
|
|
scrollObserver.initDirectionObserver(
|
|
|
|
() => {
|
|
|
|
document.body.classList.remove( 'citizen-scroll--up' );
|
|
|
|
document.body.classList.add( 'citizen-scroll--down' );
|
|
|
|
},
|
|
|
|
() => {
|
|
|
|
document.body.classList.remove( 'citizen-scroll--down' );
|
|
|
|
document.body.classList.add( 'citizen-scroll--up' );
|
|
|
|
},
|
2022-11-23 19:11:48 +00:00
|
|
|
10
|
2022-11-10 01:02:21 +00:00
|
|
|
);
|
|
|
|
|
2024-05-15 05:24:43 +00:00
|
|
|
const sentinel = document.getElementById( 'citizen-page-header-sticky-sentinel' );
|
2021-04-26 15:39:27 +00:00
|
|
|
|
2022-11-09 22:34:40 +00:00
|
|
|
// In some pages we use display:none to disable the sticky header
|
|
|
|
// Do not start observer if it is set to display:none
|
|
|
|
if ( sentinel && getComputedStyle( sentinel ).getPropertyValue( 'display' ) !== 'none' ) {
|
2022-11-10 01:02:21 +00:00
|
|
|
const observer = scrollObserver.initIntersectionObserver(
|
2022-05-13 04:21:08 +00:00
|
|
|
() => {
|
2024-05-15 05:24:43 +00:00
|
|
|
document.body.classList.add( 'citizen-page-header--sticky' );
|
2022-05-13 04:21:08 +00:00
|
|
|
},
|
|
|
|
() => {
|
2024-05-15 05:24:43 +00:00
|
|
|
document.body.classList.remove( 'citizen-page-header--sticky' );
|
2021-04-26 15:39:27 +00:00
|
|
|
}
|
2022-05-13 04:21:08 +00:00
|
|
|
);
|
2022-10-22 23:58:54 +00:00
|
|
|
observer.observe( sentinel );
|
2021-04-26 15:39:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-23 20:37:43 +00:00
|
|
|
/**
|
|
|
|
* Register service worker
|
|
|
|
*
|
|
|
|
* @return {void}
|
|
|
|
*/
|
|
|
|
function registerServiceWorker() {
|
|
|
|
const scriptPath = mw.config.get( 'wgScriptPath' );
|
|
|
|
|
|
|
|
// Only allow serviceWorker when the scriptPath is at root because of its scope
|
2022-11-09 22:40:31 +00:00
|
|
|
// I can't figure out how to add the Service-Worker-Allowed HTTP header
|
|
|
|
// to change the default scope
|
2022-10-23 20:37:43 +00:00
|
|
|
if ( scriptPath === '' ) {
|
|
|
|
if ( 'serviceWorker' in navigator ) {
|
|
|
|
const SW_MODULE_NAME = 'skins.citizen.serviceWorker',
|
|
|
|
version = mw.loader.moduleRegistry[ SW_MODULE_NAME ].version,
|
|
|
|
// HACK: Faking a RL link
|
|
|
|
swUrl = scriptPath +
|
|
|
|
'/load.php?modules=' + SW_MODULE_NAME +
|
|
|
|
'&only=scripts&raw=true&skin=citizen&version=' + version;
|
|
|
|
navigator.serviceWorker.register( swUrl, { scope: '/' } );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-12 01:44:34 +00:00
|
|
|
/**
|
|
|
|
* Initialize scripts related to wiki page content
|
|
|
|
*
|
|
|
|
* @param {HTMLElement} bodyContent
|
|
|
|
* @return {void}
|
|
|
|
*/
|
|
|
|
function initBodyContent( bodyContent ) {
|
|
|
|
const
|
|
|
|
sections = require( './sections.js' ),
|
|
|
|
tables = require( './tables.js' ),
|
|
|
|
toc = require( './tableOfContents.js' );
|
|
|
|
|
|
|
|
// Collapsable sections
|
|
|
|
sections.init( bodyContent );
|
|
|
|
// Table enhancements
|
|
|
|
tables.init( bodyContent );
|
|
|
|
// Table of contents
|
|
|
|
toc.init( bodyContent );
|
|
|
|
}
|
|
|
|
|
2021-03-13 17:43:28 +00:00
|
|
|
/**
|
2021-03-13 17:45:03 +00:00
|
|
|
* @param {Window} window
|
2021-03-13 17:43:28 +00:00
|
|
|
* @return {void}
|
|
|
|
*/
|
|
|
|
function main( window ) {
|
2023-01-26 00:40:52 +00:00
|
|
|
const
|
2023-03-27 18:08:38 +00:00
|
|
|
config = require( './config.json' ),
|
2023-01-26 00:40:52 +00:00
|
|
|
search = require( './search.js' ),
|
2024-05-08 20:39:47 +00:00
|
|
|
lastModified = require( './lastModified.js' ),
|
2023-01-26 00:40:52 +00:00
|
|
|
checkbox = require( './checkbox.js' );
|
2022-05-13 04:21:08 +00:00
|
|
|
|
2021-03-13 17:43:28 +00:00
|
|
|
enableCssAnimations( window.document );
|
2021-04-21 22:47:03 +00:00
|
|
|
search.init( window );
|
2024-05-08 20:39:47 +00:00
|
|
|
lastModified.init();
|
2022-10-22 23:58:54 +00:00
|
|
|
initStickyHeader( window.document );
|
2021-04-27 16:57:54 +00:00
|
|
|
|
2022-10-23 20:37:43 +00:00
|
|
|
// Set up checkbox hacks
|
2023-01-26 00:40:52 +00:00
|
|
|
checkbox.bind();
|
2022-05-13 02:37:28 +00:00
|
|
|
|
2023-04-30 21:01:29 +00:00
|
|
|
mw.hook( 'wikipage.content' ).add( function ( content ) {
|
|
|
|
// content is a jQuery object
|
|
|
|
// note that this refers to .mw-body-content, not #bodyContent
|
2023-07-12 01:44:34 +00:00
|
|
|
initBodyContent( content[ 0 ] );
|
2023-04-30 21:01:29 +00:00
|
|
|
} );
|
|
|
|
|
2023-03-27 18:08:38 +00:00
|
|
|
// Preference module
|
2023-07-03 12:48:41 +00:00
|
|
|
if ( config.wgCitizenEnablePreferences === true && typeof document.createElement( 'div' ).prepend === 'function' ) {
|
2023-03-27 18:08:38 +00:00
|
|
|
mw.loader.load( 'skins.citizen.preferences' );
|
|
|
|
}
|
|
|
|
|
2022-11-09 22:46:55 +00:00
|
|
|
registerServiceWorker();
|
2022-10-23 20:37:43 +00:00
|
|
|
|
|
|
|
window.addEventListener( 'beforeunload', () => {
|
2022-11-09 22:46:55 +00:00
|
|
|
// T295085: Close all dropdown menus when page is unloaded to prevent them
|
|
|
|
// from being open when navigating back to a page.
|
2023-01-26 00:40:52 +00:00
|
|
|
checkbox.uncheckCheckboxHacks();
|
2022-11-09 22:46:55 +00:00
|
|
|
// Set up loading indicator
|
2022-10-23 20:37:43 +00:00
|
|
|
document.documentElement.classList.add( 'citizen-loading' );
|
|
|
|
}, false );
|
2023-10-26 21:41:14 +00:00
|
|
|
|
2024-03-19 22:49:35 +00:00
|
|
|
// Remove loading indicator once the page is unloaded/hidden
|
|
|
|
window.addEventListener( 'pagehide', () => {
|
|
|
|
document.documentElement.classList.remove( 'citizen-loading' );
|
2023-10-26 21:41:14 +00:00
|
|
|
} );
|
2021-01-16 22:01:58 +00:00
|
|
|
}
|
|
|
|
|
2022-06-04 21:42:06 +00:00
|
|
|
if ( document.readyState === 'interactive' || document.readyState === 'complete' ) {
|
|
|
|
main( window );
|
|
|
|
} else {
|
|
|
|
document.addEventListener( 'DOMContentLoaded', function () {
|
|
|
|
main( window );
|
|
|
|
} );
|
|
|
|
}
|