mediawiki-skins-Citizen/resources/skins.citizen.scripts/skin.js

125 lines
3.4 KiB
JavaScript
Raw Normal View History

/**
* Wait for first paint before calling this function.
* (see T234570#5779890, T246419).
*
* @param {Document} document
* @return {void}
*/
function enableCssAnimations( document ) {
document.documentElement.classList.add( 'citizen-animations-ready' );
// Disable all CSS transition during resize
const onResize = () => {
document.documentElement.classList.remove( 'citizen-animations-ready' );
mw.util.debounce( () => {
document.documentElement.classList.add( 'citizen-animations-ready' );
}, 250 );
};
const onResizeEnd = mw.util.debounce( () => {
document.documentElement.classList.add( 'citizen-animations-ready' );
}, 250 );
window.addEventListener( 'resize', onResize );
window.addEventListener( 'resize', onResizeEnd );
}
/**
* 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
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: '/' } );
}
}
}
/**
* Initialize scripts related to wiki page content
*
* @param {HTMLElement} bodyContent
* @return {void}
*/
function initBodyContent( bodyContent ) {
const
sections = require( './sections.js' ),
overflowElements = require( './overflowElements.js' );
// Collapsable sections
sections.init( bodyContent );
// Overflow element enhancements
overflowElements.init( bodyContent );
}
/**
2021-03-13 17:45:03 +00:00
* @param {Window} window
* @return {void}
*/
function main( window ) {
const
config = require( './config.json' ),
echo = require( './echo.js' ),
search = require( './search.js' ),
dropdown = require( './dropdown.js' ),
setupObservers = require( './setupObservers.js' ),
stickyHeader = require( './stickyHeader.js' ),
lastModified = require( './lastModified.js' ),
share = require( './share.js' );
setupObservers.main();
dropdown.init();
search.init( window );
echo();
stickyHeader.init();
lastModified.init();
share.init();
mw.hook( 'wikipage.content' ).add( ( content ) => {
// content is a jQuery object
// note that this refers to .mw-body-content, not #bodyContent
initBodyContent( content[ 0 ] );
} );
// Preference module
if ( config.wgCitizenEnablePreferences === true && typeof document.createElement( 'div' ).prepend === 'function' ) {
mw.loader.load( 'skins.citizen.preferences' );
}
// Defer non-essential tasks
setTimeout( () => {
registerServiceWorker();
window.addEventListener( 'beforeunload', () => {
// Set up loading indicator
document.documentElement.classList.add( 'citizen-loading' );
}, false );
// Remove loading indicator once the page is unloaded/hidden
window.addEventListener( 'pagehide', () => {
document.documentElement.classList.remove( 'citizen-loading' );
} );
enableCssAnimations( window.document );
}, 0 );
}
if ( document.readyState === 'interactive' || document.readyState === 'complete' ) {
main( window );
} else {
document.addEventListener( 'DOMContentLoaded', () => {
main( window );
} );
}