diff --git a/javascript/Main.js b/javascript/Main.js index 5f5c0853d8d..60342bce3c2 100644 --- a/javascript/Main.js +++ b/javascript/Main.js @@ -3,49 +3,6 @@ The equivalent to this file on production is Special:RLA, and should be kept in sync with it. */ -// List of JavaScript modules to include -const jsModules = [ - 'Analytics', - 'BattleRoyale', - 'Bracket', - 'Carousel', - 'Collapse', - 'Commons_mainpage', - 'Commons_tools', - 'CopyToClipboard', - 'Countdown', - 'Crosstable', - 'Dialog', - 'Dropdown', - 'ExportImage', - 'FilterButtons', - 'Liquipedia_links', - 'Mainpage', - 'Miscellaneous', - 'PanelBoxCollapsible', - 'Prizepooltable', - 'RankingTable', - 'Selectall', - 'Slider', - 'SwitchButtons', - 'Tabs' -]; - -// Dynamically load JavaScript modules -jsModules.forEach( ( module ) => { - const script = document.createElement( 'script' ); - script.src = `../../javascript/commons/${ module }.js`; - script.async = false; // Ensure scripts load in order - document.head.appendChild( script ); -} ); - -// Initialize liquipedia global object -window.liquipedia = window.liquipedia || { - core: { - modules: [] - } -}; - // Mock MediaWiki APIs for testing window.mw = window.mw || { config: { @@ -83,17 +40,41 @@ window.mw = window.mw || { } }; -// Auto-initialize all loaded modules -onload = () => { - liquipedia.core.modules.forEach( ( module ) => { - if ( !liquipedia[ module ] || typeof liquipedia[ module ].init !== 'function' ) { - return; - } - try { - liquipedia[ module ].init(); - } catch ( e ) { - // eslint-disable-next-line no-console - console.error( `Failed to initialize module: ${ module }. Error: ${ e }` ); - } - } ); -}; +// List of JavaScript modules to include +const jsModules = [ + 'UseStrict', + 'Core', + 'Analytics', + 'BattleRoyale', + 'Bracket', + 'Carousel', + 'Collapse', + 'Commons_mainpage', + 'Commons_tools', + 'CopyToClipboard', + 'Countdown', + 'Crosstable', + 'Dialog', + 'Dropdown', + 'ExportImage', + 'FilterButtons', + 'Liquipedia_links', + 'Mainpage', + 'Miscellaneous', + 'PanelBoxCollapsible', + 'Prizepooltable', + 'RankingTable', + 'Selectall', + 'Slider', + 'SwitchButtons', + 'Tabs', + 'CoreEnd' +]; + +// Dynamically load JavaScript modules +jsModules.forEach( ( module ) => { + const script = document.createElement( 'script' ); + script.src = `../../javascript/commons/${ module }.js`; + script.async = false; // Ensure scripts load in order + document.head.appendChild( script ); +} ); diff --git a/javascript/commons/Core.js b/javascript/commons/Core.js new file mode 100644 index 00000000000..ac8daf6c6b1 --- /dev/null +++ b/javascript/commons/Core.js @@ -0,0 +1,23 @@ +/******************************************************************************* + * Template(s): JavaScript Core + * Author(s): FO-nTTaX + * Information: All modules need to have a parameterless init function. The script + * calls this init function in an asynchronous way so a failing module can not + * stop the execution of the rest of the scripts. A slow script will also not + * block execution of other modules. The script does not check for dependencies + * (impossible to do since modules are in different files), so these need to be + * resolved manually. Modules need to register themselves into the core part of + * the liquipedia.core.modules array, so the script can find them. + ******************************************************************************/ +const liquipedia = { }; +liquipedia.core = { + modules: [ ], + init: function() { + liquipedia.core.modules.forEach( ( module ) => { + // Usage of setTimeout to make scripts asynchronous + window.setTimeout( () => { + window.liquipedia[ module ].init(); + }, 0 ); + } ); + } +}; diff --git a/javascript/commons/CoreEnd.js b/javascript/commons/CoreEnd.js new file mode 100644 index 00000000000..742ab3daf86 --- /dev/null +++ b/javascript/commons/CoreEnd.js @@ -0,0 +1,5 @@ +/******************************************************************************* + * Template(s): JavaScript Core + * Author(s): FO-nTTaX + ******************************************************************************/ +liquipedia.core.init(); diff --git a/javascript/commons/UseStrict.js b/javascript/commons/UseStrict.js new file mode 100644 index 00000000000..c1ff221e946 --- /dev/null +++ b/javascript/commons/UseStrict.js @@ -0,0 +1,5 @@ +/******************************************************************************* + * Template(s): JavaScript Strict Mode + * Author(s): FO-nTTaX + ******************************************************************************/ +'use strict';