2021-01-22 02:08:55 +00:00
|
|
|
var DRAWER_ID = 'mw-drawer',
|
2021-01-22 02:43:20 +00:00
|
|
|
DRAWER_BUTTON_ID = 'mw-drawer-button',
|
2021-01-22 02:08:55 +00:00
|
|
|
DRAWER_CHECKBOX_ID = 'mw-drawer-checkbox',
|
|
|
|
PERSONAL_MENU_ID = 'p-personal',
|
2021-01-22 02:43:20 +00:00
|
|
|
PERSONAL_MENU_BUTTON_ID = 'personalmenu-button',
|
2021-01-22 02:08:55 +00:00
|
|
|
PERSONAL_MENU_CHECKBOX_ID = 'personalmenu-checkbox';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Uncheck CSS hack checkbox when clicked outside
|
|
|
|
*
|
2021-02-03 20:00:43 +00:00
|
|
|
* @param {HTMLElement|HTMLElement[]} clickableElement
|
2021-01-22 02:43:20 +00:00
|
|
|
* @param {HTMLElement} button
|
2021-01-22 02:08:55 +00:00
|
|
|
* @param {HTMLElement} checkbox
|
|
|
|
*/
|
2021-02-03 20:00:43 +00:00
|
|
|
function uncheckOnClickOutside( clickableElement, button, checkbox ) {
|
2021-01-22 02:08:55 +00:00
|
|
|
var listener = function ( e ) {
|
2021-02-03 20:00:43 +00:00
|
|
|
var hideCond = e.target !== clickableElement;
|
|
|
|
|
|
|
|
if ( Array.isArray( clickableElement ) ) {
|
|
|
|
hideCond = clickableElement.indexOf( e.target ) === -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( e.target !== checkbox && hideCond ) {
|
2021-01-22 02:43:20 +00:00
|
|
|
if ( e.target !== button ) {
|
|
|
|
checkbox.checked = false;
|
|
|
|
}
|
2021-01-22 02:08:55 +00:00
|
|
|
document.removeEventListener( 'click', listener );
|
|
|
|
}
|
|
|
|
};
|
2021-01-29 18:50:20 +00:00
|
|
|
|
2021-02-03 20:00:43 +00:00
|
|
|
var keyboardListener = function ( event ) {
|
2021-01-29 18:50:20 +00:00
|
|
|
if ( event.key === 'Escape' && checkbox.checked === true ) {
|
|
|
|
checkbox.checked = false;
|
|
|
|
document.removeEventListener( 'click', listener );
|
|
|
|
}
|
2021-02-03 20:00:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
var checkboxFn = function () {
|
|
|
|
if ( checkbox.checked ) {
|
|
|
|
document.addEventListener( 'click', listener );
|
|
|
|
} else {
|
|
|
|
document.removeEventListener( 'click', listener );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
checkbox.removeEventListener( 'click', checkboxFn );
|
|
|
|
checkbox.addEventListener( 'click', checkboxFn );
|
|
|
|
|
|
|
|
document.removeEventListener( 'keydown', keyboardListener );
|
|
|
|
document.addEventListener( 'keydown', keyboardListener );
|
2021-01-22 02:08:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {void}
|
|
|
|
*/
|
|
|
|
function init() {
|
|
|
|
var drawer = document.getElementById( DRAWER_ID ),
|
2021-01-22 02:43:20 +00:00
|
|
|
drawerButton = document.getElementById( DRAWER_BUTTON_ID ),
|
2021-01-22 02:08:55 +00:00
|
|
|
drawerCheckbox = document.getElementById( DRAWER_CHECKBOX_ID ),
|
2021-02-03 20:47:32 +00:00
|
|
|
drawerSearch = document.getElementById( 'mw-drawer-search-input' ),
|
2021-01-22 02:08:55 +00:00
|
|
|
personalMenu = document.getElementById( PERSONAL_MENU_ID ),
|
2021-01-22 02:43:20 +00:00
|
|
|
personalMenuButton = document.getElementById( PERSONAL_MENU_BUTTON_ID ),
|
2021-02-03 20:00:43 +00:00
|
|
|
personalMenuCheckbox = document.getElementById( PERSONAL_MENU_CHECKBOX_ID ),
|
2021-03-08 19:32:29 +00:00
|
|
|
toc = document.getElementById( 'toc' ),
|
|
|
|
tocButton,
|
|
|
|
tocCheckbox,
|
2021-02-03 20:00:43 +00:00
|
|
|
clickableDrawerElements = [];
|
|
|
|
|
|
|
|
drawer.querySelectorAll( '#mw-drawer-menu .mw-portal' ).forEach( function ( portal ) {
|
|
|
|
clickableDrawerElements.push( portal );
|
|
|
|
} );
|
|
|
|
|
|
|
|
drawer.querySelectorAll( '#mw-drawer-menu .mw-portal h3' ).forEach( function ( portalHeading ) {
|
|
|
|
clickableDrawerElements.push( portalHeading );
|
|
|
|
} );
|
|
|
|
|
|
|
|
if ( drawerSearch !== null ) {
|
|
|
|
clickableDrawerElements.push( drawerSearch );
|
|
|
|
}
|
|
|
|
|
|
|
|
uncheckOnClickOutside( clickableDrawerElements, drawerButton, drawerCheckbox );
|
2021-01-22 02:43:20 +00:00
|
|
|
uncheckOnClickOutside( personalMenu, personalMenuButton, personalMenuCheckbox );
|
2021-03-08 19:32:29 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This should be in ToC script
|
|
|
|
* And the media query needs to be synced with the less variable
|
|
|
|
*/
|
|
|
|
if ( toc && window.matchMedia( 'screen and (max-width: 1300px)' ) ) {
|
|
|
|
tocButton = document.querySelector( 'toctogglelabel' );
|
|
|
|
tocCheckbox = document.getElementById( 'toctogglecheckbox' );
|
|
|
|
|
|
|
|
uncheckOnClickOutside( toc, tocButton, tocCheckbox );
|
|
|
|
}
|
2021-01-22 02:08:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
init: init
|
|
|
|
};
|