const makeTemplate = function ( description ) {
const templateString = `
${ mw.message( 'skin-minerva-night-mode-launch-subtitle' ).escaped() }
${ description }
`;
const templateElement = document.createElement( 'div' );
templateElement.id = 'minerva-dark-mode-launch-banner';
templateElement.innerHTML = templateString;
return templateElement;
};
function closeModal() {
document.getElementById( 'minerva-dark-mode-launch-banner' ).remove();
}
function primaryActionHandler() {
closeModal();
const url = mw.util.getUrl( 'Special:MobileOptions', { returnto: mw.config.get( 'wgPageName' ) } );
window.location.href = url;
}
function init() {
const mountElement = document.getElementById( 'mw-teleport-target' ),
matchMediaDark = window.matchMedia( '(prefers-color-scheme: dark)' ),
colorMode = mw.user.clientPrefs.get( 'skin-theme' ),
description = (
colorMode === 'night' ||
( colorMode === 'auto' && matchMediaDark )
) ?
mw.message( 'skin-minerva-night-mode-launch-description-night' ).escaped() :
mw.message( 'skin-minerva-night-mode-launch-description-day' ).escaped(),
templateElement = makeTemplate( description ),
dialogOverlay = templateElement.querySelector( '.cdx-dialog-backdrop' ),
dialogCloseButton = templateElement.querySelector( '.cdx-dialog__header__close-button' ),
primaryActionButton = templateElement.querySelector( '.cdx-dialog__footer__primary-action' );
dialogOverlay.addEventListener( 'click', ( e ) => {
if ( e.target === dialogOverlay ) {
closeModal();
}
} );
dialogCloseButton.addEventListener( 'click', closeModal );
primaryActionButton.addEventListener( 'click', primaryActionHandler );
mountElement.appendChild( templateElement );
}
module.exports = init;