Debian Codegraph @

skins/Vector/resources/skins.vector.js/skin.js

raw symbols SCIP
1 const languageButton = require( './languageButton.js' ),
2 pinnableElement = require( './pinnableElement.js' ),
3 searchToggle = require( './searchToggle.js' ),
4 echo = require( './echo.js' ),
5 initExperiment = require( './AB.js' ),
6 ABTestConfig = require( /** @type {string} */ ( './activeABTest.json' ) ),
7 initSearchLoader = require( './searchLoader.js' ).initSearchLoader,
8 portletsManager = require( './portlets.js' ),
9 dropdownMenus = require( './dropdownMenus.js' ).dropdownMenus,
10 tables = require( './tables.js' ).init,
11 watchstar = require( './watchstar.js' ).init,
12 setupIntersectionObservers = require( './setupIntersectionObservers.js' ),
13 menuTabs = require( './menuTabs.js' ),
14 userPreferences = require( './userPreferences.js' ),
15 { isNightModeGadgetEnabled, disableNightModeForGadget, alterExclusionMessage, removeBetaNotice } = require( './disableNightModeIfGadget.js' ),
16 teleportTarget = /** @type {HTMLElement} */require( /** @type {string} */ ( 'mediawiki.page.ready' ) ).teleportTarget;
17
18 /**
19 * Wait for first paint before calling this function. That's its whole purpose.
20 *
21 * Some CSS animations and transitions are "disabled" by default as a workaround to this old Chrome
22 * bug, https://bugs.chromium.org/p/chromium/issues/detail?id=332189, which otherwise causes them to
23 * render in their terminal state on page load. By adding the `vector-animations-ready` class to the
24 * `html` root element **after** first paint, the animation selectors suddenly match causing the
25 * animations to become "enabled" when they will work properly. A similar pattern is used in Minerva
26 * (see T234570#5779890, T246419).
27 *
28 * Example usage in Less:
29 *
30 * ```less
31 * .foo {
32 * color: #f00;
33 * transform: translateX( -100% );
34 * }
35 *
36 * // This transition will be disabled initially for JavaScript users. It will never be enabled for
37 * // non-JavaScript users.
38 * .vector-animations-ready .foo {
39 * transition: transform 100ms ease-out;
40 * }
41 * ```
42 *
43 * @param {Document} document
44 * @return {void}
45 */
46 function enableCssAnimations( document ) {
47 document.documentElement.classList.add( 'vector-animations-ready' );
48 }
49
50 /**
51 * @param {Window} window
52 * @return {void}
53 */
54 function main( window ) {
55 enableCssAnimations( window.document );
56 initSearchLoader( document );
57 languageButton();
58 echo();
59 portletsManager.main();
60 watchstar();
61 // Initialize the search toggle for the main header only. The sticky header
62 // toggle is initialized after Codex search loads.
63 const searchToggleElement = document.querySelector( '.mw-header .search-toggle' );
64 if ( searchToggleElement ) {
65 searchToggle( searchToggleElement );
66 }
67 pinnableElement.initPinnableElement();
68 // Initializes the TOC and sticky header, behaviour of which depend on scroll behaviour.
69 setupIntersectionObservers.main();
70 // Apply body styles to teleported elements
71 teleportTarget.classList.add( 'vector-body' );
72
73 // Load client preferences
74 const appearanceMenuSelector = '#vector-appearance';
75 const appearanceMenuExists = document.querySelectorAll( appearanceMenuSelector ).length > 0;
76 if ( appearanceMenuExists ) {
77 mw.loader.using( [
78 'skins.vector.clientPreferences',
79 'skins.vector.search.codex.styles',
80 'skins.vector.search.codex.scripts'
81 ] ).then( () => {
82 const clientPreferences = require( /** @type {string} */ ( 'skins.vector.clientPreferences' ) );
83 const clientPreferenceConfig = ( require( './clientPreferences.json' ) );
84 // Can be removed once wgVectorNightMode is removed.
85 if ( document.documentElement.classList.contains( 'vector-feature-night-mode-disabled' ) ) {
86 // @ts-ignore issues relating to delete operator are not relevant here.
87 delete clientPreferenceConfig[ 'skin-theme' ];
88 }
89
90 // while we're in beta, temporarily check if the night mode gadget is installed and
91 // disable our night mode if so
92 if ( isNightModeGadgetEnabled() ) {
93 disableNightModeForGadget();
94 clientPreferences.render(
95 appearanceMenuSelector, clientPreferenceConfig, userPreferences
96 );
97 alterExclusionMessage();
98 removeBetaNotice();
99 } else {
100 clientPreferences.render(
101 appearanceMenuSelector, clientPreferenceConfig, userPreferences
102 );
103 }
104 } );
105 }
106
107 dropdownMenus();
108 // menuTabs should follow `dropdownMenus` as that can move menu items from a
109 // tab menu to a dropdown.
110 menuTabs();
111 tables();
112 }
113
114 /**
115 * @param {Window} window
116 * @return {void}
117 */
118 function init( window ) {
119 const now = mw.now();
120 // This is the earliest time we can run JS for users (and bucket anonymous
121 // users for A/B tests).
122 // Where the browser supports it, for a 10% sample of users
123 // we record a value to give us a sense of the expected delay in running A/B tests or
124 // disabling JS features. This will inform us on various things including what to expect
125 // with regards to delay while running A/B tests to anonymous users.
126 // When EventLogging is not available this will reject.
127 // This code can be removed by the end of the Desktop improvements project.
128 // https://www.mediawiki.org/wiki/Desktop_improvements
129 mw.loader.using( 'ext.eventLogging' ).then( () => {
130 if (
131 mw.eventLog &&
132 mw.eventLog.eventInSample( 100 /* 1 in 100 */ ) &&
133 window.performance &&
134 window.performance.timing &&
135 window.performance.timing.navigationStart
136 ) {
137 mw.track( 'timing.Vector.ready', now - window.performance.timing.navigationStart ); // milliseconds
138 }
139 } );
140 }
141
142 init( window );
143 if ( ABTestConfig.enabled && !mw.user.isAnon() ) {
144 initExperiment( ABTestConfig, String( mw.user.getId() ) );
145 }
146 if ( document.readyState === 'interactive' || document.readyState === 'complete' ) {
147 main( window );
148 } else {
149 // This is needed when document.readyState === 'loading'.
150 document.addEventListener( 'DOMContentLoaded', () => {
151 main( window );
152 } );
153 }
154
155 // Provider of skins.vector.js module:
156 /**
157 * skins.vector.js
158 *
159 * @stable for use inside WikimediaEvents ONLY.
160 */
161 module.exports = { pinnableElement };