diff --git a/resources/skins.vector.js/checkbox.js b/resources/skins.vector.js/checkbox.js index 75a99ff2..a0e17f4d 100644 --- a/resources/skins.vector.js/checkbox.js +++ b/resources/skins.vector.js/checkbox.js @@ -5,12 +5,6 @@ * However the main menu and collapsible TOC use a variation of the checkbox hack * that requires their own JS for enhancements. * - * This code targets any element with a mw-checkbox-hack-button class. It must have - * a `for` attribute to qualify for enhancements. - * - * Enhancements include: - * - Update `aria-role`s based on expanded/collapsed state. - * - Update button icon based on expanded/collapsed state. */ /** @interface MwApiConstructor */ @@ -96,15 +90,15 @@ function bindToggleOnSpaceEnter( button ) { } /** - * Improve the interactivity of the sidebar panel by binding optional checkbox hack enhancements - * for focus and `aria-expanded`. Also, flip the icon image on click. + * Improve the interactivity of the sidebar panel by binding checkbox hack enhancements. * * @param {HTMLElement|null} checkbox * @param {HTMLElement|null} button + * @param {HTMLElement|null} target * @return {void} */ -function initCheckboxHack( checkbox, button ) { - if ( checkbox instanceof HTMLInputElement && button ) { +function initMainMenu( checkbox, button, target ) { + if ( checkbox instanceof HTMLInputElement && button && target ) { checkboxHack.bindToggleOnClick( checkbox, button ); bindUpdateAriaExpandedOnInput( checkbox, button ); updateAriaExpanded( checkbox, button ); @@ -113,22 +107,41 @@ function initCheckboxHack( checkbox, button ) { } /** - * Initialize all JavaScript sidebar enhancements. + * Improve the interactivity of the collapsed TOC by binding checkbox hack enhancements. * - * @param {Window} window + * @param {HTMLElement|null} checkbox + * @param {HTMLElement|null} button + * @param {HTMLElement|null} target + * @return {void} */ -function init( window ) { - var buttons = window.document.querySelectorAll( '.mw-checkbox-hack-button' ); +function initCollapsedToc( checkbox, button, target ) { + if ( checkbox instanceof HTMLInputElement && button && target ) { + checkboxHack.bindToggleOnClick( checkbox, button ); + checkboxHack.bindDismissOnClickOutside( window, checkbox, button, target ); + checkboxHack.bindDismissOnClickLink( checkbox, target ); + bindUpdateAriaExpandedOnInput( checkbox, button ); + updateAriaExpanded( checkbox, button ); + bindToggleOnSpaceEnter( button ); + } +} - Array.prototype.forEach.call( buttons, function ( button ) { - var checkboxId = button.getAttribute( 'for' ), - checkbox = checkboxId ? window.document.getElementById( checkboxId ) : null; - - if ( checkbox ) { - initCheckboxHack( checkbox, button ); - } - } ); +/** + * Initialize main menu and collapsed TOC enhancements. + * + * @param {Document} document + */ +function init( document ) { + initMainMenu( + document.getElementById( 'mw-sidebar-checkbox' ), + document.getElementById( 'mw-sidebar-button' ), + document.getElementById( 'mw-navigation' ) + ); + initCollapsedToc( + document.getElementById( 'vector-toc-collapsed-checkbox' ), + document.getElementById( 'vector-toc-collapsed-button' ), + document.getElementById( 'mw-panel-toc' ) + ); } module.exports = { diff --git a/resources/skins.vector.js/dropdownMenus.js b/resources/skins.vector.js/dropdownMenus.js index 432a23cc..4aebdc06 100644 --- a/resources/skins.vector.js/dropdownMenus.js +++ b/resources/skins.vector.js/dropdownMenus.js @@ -8,8 +8,7 @@ var CHECKBOX_HACK_TARGET_SELECTOR = '.vector-menu-content'; /** - * Add the ability for users to toggle dropdown menus using the enter key (as - * well as space) using core's checkboxHack. + * Enhance dropdownMenu functionality and accessibility using core's checkboxHack. */ function bind() { // Search for all dropdown containers using the CHECKBOX_HACK_CONTAINER_SELECTOR. @@ -29,19 +28,6 @@ function bind() { } ); } -/** - * T295085: Close all dropdown menus when page is unloaded to prevent them from - * being open when navigating back to a page. - */ -function bindCloseOnUnload() { - addEventListener( 'beforeunload', function () { - var checkboxes = document.querySelectorAll( CHECKBOX_HACK_CHECKBOX_SELECTOR + ':checked' ); - Array.prototype.forEach.call( checkboxes, function ( checkbox ) { - /** @type {HTMLInputElement} */ ( checkbox ).checked = false; - } ); - } ); -} - /** * Adds icon placeholder for gadgets to use. * @@ -115,5 +101,4 @@ mw.hook( 'util.addPortletLink' ).add( addPortletLinkHandler ); module.exports = function dropdownMenus() { bind(); - bindCloseOnUnload(); }; diff --git a/resources/skins.vector.js/skin.js b/resources/skins.vector.js/skin.js index a2c9111e..3208da1d 100644 --- a/resources/skins.vector.js/skin.js +++ b/resources/skins.vector.js/skin.js @@ -43,7 +43,7 @@ function enableCssAnimations( document ) { function main( window ) { enableCssAnimations( window.document ); sidebarPersistence.init(); - checkbox.init( window ); + checkbox.init( window.document ); initSearchLoader( document ); languageButton(); dropdownMenus();