2021-10-21 15:59:58 +00:00
|
|
|
/** @interface CheckboxHack */
|
|
|
|
|
|
|
|
var
|
|
|
|
checkboxHack = /** @type {CheckboxHack} */ require( /** @type {string} */( 'mediawiki.page.ready' ) ).checkboxHack,
|
|
|
|
CHECKBOX_HACK_CONTAINER_SELECTOR = '.vector-menu-dropdown',
|
|
|
|
CHECKBOX_HACK_CHECKBOX_SELECTOR = '.vector-menu-checkbox',
|
2021-12-16 22:33:01 +00:00
|
|
|
CHECKBOX_HACK_BUTTON_SELECTOR = '.vector-menu-heading',
|
2021-10-21 15:59:58 +00:00
|
|
|
CHECKBOX_HACK_TARGET_SELECTOR = '.vector-menu-content';
|
|
|
|
|
|
|
|
/**
|
2022-07-26 18:58:01 +00:00
|
|
|
* Enhance dropdownMenu functionality and accessibility using core's checkboxHack.
|
2021-10-21 15:59:58 +00:00
|
|
|
*/
|
2021-12-16 22:33:01 +00:00
|
|
|
function bind() {
|
2021-10-21 15:59:58 +00:00
|
|
|
// Search for all dropdown containers using the CHECKBOX_HACK_CONTAINER_SELECTOR.
|
|
|
|
var containers = document.querySelectorAll( CHECKBOX_HACK_CONTAINER_SELECTOR );
|
|
|
|
|
|
|
|
Array.prototype.forEach.call( containers, function ( container ) {
|
|
|
|
var
|
|
|
|
checkbox = container.querySelector( CHECKBOX_HACK_CHECKBOX_SELECTOR ),
|
|
|
|
button = container.querySelector( CHECKBOX_HACK_BUTTON_SELECTOR ),
|
|
|
|
target = container.querySelector( CHECKBOX_HACK_TARGET_SELECTOR );
|
|
|
|
|
|
|
|
if ( !( checkbox && button && target ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-16 22:33:01 +00:00
|
|
|
checkboxHack.bind( window, checkbox, button, target );
|
2021-10-21 15:59:58 +00:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2022-08-16 19:50:34 +00:00
|
|
|
/**
|
|
|
|
* Create an icon element to be appended inside the anchor tag.
|
|
|
|
*
|
|
|
|
* @param {HTMLElement|null} menuElement
|
|
|
|
* @param {HTMLElement|null} parentElement
|
|
|
|
* @param {string|null} id
|
|
|
|
*
|
|
|
|
* @return {HTMLElement|undefined}
|
|
|
|
*/
|
|
|
|
function createIconElement( menuElement, parentElement, id ) {
|
|
|
|
// Dropdowns which do not have the noicon class are icon capable.
|
|
|
|
var isIconCapable = menuElement &&
|
|
|
|
menuElement.classList.contains( 'vector-menu-dropdown' ) &&
|
|
|
|
!menuElement.classList.contains( 'vector-menu-dropdown-noicon' );
|
|
|
|
|
|
|
|
if ( !isIconCapable || !parentElement ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var iconElement = document.createElement( 'span' );
|
|
|
|
iconElement.classList.add( 'mw-ui-icon' );
|
|
|
|
|
|
|
|
if ( id ) {
|
|
|
|
// The following class allows gadgets developers to style or hide an icon.
|
|
|
|
// * mw-ui-icon-vector-gadget-<id>
|
|
|
|
// The class is considered stable and should not be removed without
|
|
|
|
// a #user-notice.
|
|
|
|
iconElement.classList.add( 'mw-ui-icon-vector-gadget-' + id );
|
|
|
|
}
|
|
|
|
|
|
|
|
return iconElement;
|
|
|
|
}
|
|
|
|
|
2021-09-23 22:09:44 +00:00
|
|
|
/**
|
|
|
|
* Adds icon placeholder for gadgets to use.
|
|
|
|
*
|
|
|
|
* @typedef {Object} PortletLinkData
|
|
|
|
* @property {string|null} id
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @param {HTMLElement} item
|
|
|
|
* @param {PortletLinkData} data
|
|
|
|
*/
|
|
|
|
function addPortletLinkHandler( item, data ) {
|
|
|
|
var link = item.querySelector( 'a' );
|
2021-10-04 17:54:22 +00:00
|
|
|
var $menu = $( item ).parents( '.vector-menu' );
|
|
|
|
var menuElement = $menu.length && $menu.get( 0 ) || null;
|
2022-08-16 19:50:34 +00:00
|
|
|
var iconElement = createIconElement( menuElement, link, data.id );
|
2021-09-23 22:09:44 +00:00
|
|
|
|
2022-04-27 19:32:19 +00:00
|
|
|
// The views menu has limited space so we need to decide whether there is space
|
|
|
|
// to accomodate the new item and if not to redirect to the more dropdown.
|
|
|
|
/* eslint-disable no-jquery/no-global-selector */
|
|
|
|
if ( $menu.prop( 'id' ) === 'p-views' ) {
|
|
|
|
// @ts-ignore if undefined as NaN will be ignored
|
|
|
|
var availableWidth = $( '.mw-article-toolbar-container' ).width() -
|
|
|
|
// @ts-ignore
|
|
|
|
$( '#p-namespaces' ).width() - $( '#p-variants' ).width() -
|
|
|
|
// @ts-ignore
|
|
|
|
$( '#p-views' ).width() - $( '#p-cactions' ).width();
|
|
|
|
var moreDropdown = document.querySelector( '#p-cactions ul' );
|
|
|
|
// If the screen width is less than 720px then the views menu is hidden
|
|
|
|
if ( moreDropdown && ( availableWidth < 0 || window.innerWidth < 720 ) ) {
|
|
|
|
moreDropdown.appendChild( item );
|
|
|
|
// reveal if hidden
|
|
|
|
mw.util.showPortlet( 'p-cactions' );
|
|
|
|
}
|
|
|
|
}
|
2022-08-16 19:50:34 +00:00
|
|
|
|
|
|
|
if ( link && iconElement ) {
|
|
|
|
link.prepend( iconElement );
|
2021-09-23 22:09:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enhance previously added items.
|
|
|
|
Array.prototype.forEach.call(
|
|
|
|
document.querySelectorAll( '.mw-list-item-js' ),
|
|
|
|
function ( item ) {
|
|
|
|
addPortletLinkHandler( item, {
|
|
|
|
id: item.getAttribute( 'id' )
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
mw.hook( 'util.addPortletLink' ).add( addPortletLinkHandler );
|
|
|
|
|
2022-08-16 19:50:34 +00:00
|
|
|
module.exports = {
|
|
|
|
dropdownMenus: function dropdownMenus() { bind(); },
|
|
|
|
addPortletLinkHandler: addPortletLinkHandler
|
2021-02-24 23:30:16 +00:00
|
|
|
};
|