Debian Codegraph @

skins/Vector/resources/skins.vector.js/disableNightModeIfGadget.js

raw symbols SCIP
1 const userPreferences = require( './userPreferences.js' );
2
3 /**
4 * T365083 - Disable night mode if night mode gadget is enabled
5 *
6 * While our implementation of night mode is still in beta, we want to respect the existing gadget
7 * and disable our version to avoid a double invert - that said, we will still provide a prompt for
8 * the user to disable the gadget so they can try our night mode
9 */
10
11 /**
12 * Are any of the gadgets associated with the broader night mode gadget enabled?
13 * Note: This is localized to the names of the gadget in our particular language
14 *
15 * @return {boolean}
16 */
17 function isNightModeGadgetEnabled() {
18 return mw.msg( 'vector-night-mode-gadget-names' ).split( '|' ).some( ( gadget ) => {
19 const state = mw.loader.getState( `ext.gadget.${ gadget }` );
20
21 // the state is null if it's not installed or we're on the preference page, otherwise it's
22 // registered if the user doesn't have it turned on - all other states we consider enabled
23 return state !== null && state !== 'registered';
24 } );
25 }
26
27 /**
28 * Manually mark the page we're on as excluded
29 */
30 function disableNightModeForGadget() {
31 document.documentElement.classList.remove( 'skin-theme-clientpref-night', 'skin-theme-clientpref-os' );
32
33 document.documentElement.classList.add( 'skin-theme-clientpref--excluded' );
34 }
35
36 /**
37 * Modify the link to disable the gadget so that, when clicked, it will disable the night mode
38 * gadget rather than simply take you to the page
39 * Note: The gadget names are similarly localized to the current language
40 *
41 * @param {Element} container an html element containing a link
42 */
43 function alterDisableLink( container ) {
44 const gadgetNames = mw.msg( 'vector-night-mode-gadget-names' );
45 const link = container.querySelector( 'a' );
46
47 // if we can't disable the gadget, keep the link functional
48 // (if we can't find a link, not much we can do)
49 if ( !gadgetNames || !link ) {
50 return;
51 }
52
53 link.removeAttribute( 'title' );
54 link.removeAttribute( 'href' );
55 link.style.display = 'inline';
56
57 link.addEventListener( 'click', () => {
58 /** @type {Object.<string, number>} */
59 const disableOptions = {};
60
61 gadgetNames.split( '|' ).forEach( ( gadgetName ) => {
62 disableOptions[ `gadget-${ gadgetName }` ] = 0;
63 } );
64
65 userPreferences.saveOptions( disableOptions ).then( () => {
66 window.location.reload();
67 } );
68 } );
69 }
70
71 /**
72 * Removes the link that solicits user feedback for dark mode.
73 */
74 function removeBetaNotice() {
75 const feedbackNoticeContainer = document.getElementById( 'skin-theme-beta-notice' );
76 if ( feedbackNoticeContainer ) {
77 feedbackNoticeContainer.remove();
78 }
79 }
80
81 /**
82 * Modify the default exclusion message to indicate that we've disabled night mode on the page due
83 * to a conflicting gadget, providing a link to disable the gadget in favor of our night mode
84 */
85 function alterExclusionMessage() {
86 const noticeContainer = document.querySelector( '#skin-client-prefs-skin-theme .exclusion-notice' );
87 // if there's no exclusion notice, nothing we can do
88 if ( !noticeContainer ) {
89 return;
90 }
91
92 mw.loader.using( 'mediawiki.jqueryMsg' ).then( () => {
93 // remove existing message
94 noticeContainer.textContent = '';
95
96 mw.message( 'vector-night-mode-gadget-warning' ).parseDom().appendTo( noticeContainer );
97
98 alterDisableLink( noticeContainer );
99 } );
100 }
101
102 module.exports = {
103 isNightModeGadgetEnabled,
104 disableNightModeForGadget,
105 alterDisableLink,
106 alterExclusionMessage,
107 removeBetaNotice
108 };