2021-10-21 15:59:58 +00:00
|
|
|
/** @interface CheckboxHack */
|
|
|
|
|
2023-04-11 17:22:58 +00:00
|
|
|
const
|
2021-10-21 15:59:58 +00:00
|
|
|
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.
|
2023-04-11 17:22:58 +00:00
|
|
|
const containers = document.querySelectorAll( CHECKBOX_HACK_CONTAINER_SELECTOR );
|
2021-10-21 15:59:58 +00:00
|
|
|
|
|
|
|
Array.prototype.forEach.call( containers, function ( container ) {
|
2023-04-11 17:22:58 +00:00
|
|
|
const
|
2021-10-21 15:59:58 +00:00
|
|
|
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 ) {
|
2023-02-16 17:06:22 +00:00
|
|
|
// Only the p-personal menu in the user links dropdown supports icons
|
2023-04-11 17:22:58 +00:00
|
|
|
const isIconCapable = menuElement &&
|
2023-02-16 20:12:58 +00:00
|
|
|
[
|
|
|
|
'p-personal',
|
|
|
|
'p-personal-sticky-header'
|
|
|
|
].indexOf( menuElement.getAttribute( 'id' ) || 'p-unknown' ) > -1;
|
2022-08-16 19:50:34 +00:00
|
|
|
|
|
|
|
if ( !isIconCapable || !parentElement ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-11 17:22:58 +00:00
|
|
|
const iconElement = document.createElement( 'span' );
|
2022-08-16 19:50:34 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-02-14 14:58:54 +00:00
|
|
|
/**
|
|
|
|
* Calculate the available width for adding links in the veiws menu,
|
|
|
|
* i.e. the remaining space in the toolbar between the right-navigation
|
|
|
|
* and left-navigation elements.
|
|
|
|
*
|
|
|
|
* @return {number} remaining available pixels in page toolbar or Zero
|
|
|
|
* if remaining space is negative.
|
|
|
|
*/
|
|
|
|
function getAvailableViewMenuWidth() {
|
2023-04-11 17:22:58 +00:00
|
|
|
const
|
2023-02-14 14:58:54 +00:00
|
|
|
// Vector toolbar containing namespace, views, more menu etc.
|
|
|
|
toolbar = document.querySelector( '.vector-page-toolbar-container' ),
|
|
|
|
// Assumes all left-side menus are wrapped in a single nav element.
|
|
|
|
// Need to get child width since this node is flex-grow: 1;
|
|
|
|
leftToolbarItems = document.querySelector( '#left-navigation > nav' ),
|
|
|
|
// Right side elements are flex-grow:0 so top-level width is sufficient.
|
|
|
|
rightToolbarItems = document.getElementById( 'right-navigation' );
|
|
|
|
|
|
|
|
// Views menu collapses into "more" menu at this resolution.
|
|
|
|
// Move the link from views to actions menu in this situation.
|
|
|
|
if ( window.innerWidth < 720 ) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If any of our assumption about the DOM are wrong, return 0
|
|
|
|
// in order to place the link in a known menu instead.
|
|
|
|
if ( !( toolbar && leftToolbarItems && rightToolbarItems ) ) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// returning zero instead of negative number makes boolean conversion easier.
|
|
|
|
return Math.max( 0,
|
|
|
|
toolbar.clientWidth - leftToolbarItems.clientWidth - rightToolbarItems.clientWidth
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-04-11 17:22:58 +00:00
|
|
|
const /** @type {Array<HTMLElement>} */handledLinks = [];
|
2022-09-28 15:40:43 +00:00
|
|
|
|
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 ) {
|
2023-04-11 17:22:58 +00:00
|
|
|
const linkIsHandled = handledLinks.indexOf( item );
|
|
|
|
let iconElement;
|
2023-02-14 14:58:54 +00:00
|
|
|
|
|
|
|
if ( linkIsHandled >= 0 ) {
|
2022-09-28 15:40:43 +00:00
|
|
|
return;
|
2023-02-14 14:58:54 +00:00
|
|
|
} else {
|
|
|
|
handledLinks.push( item );
|
|
|
|
}
|
|
|
|
|
|
|
|
// assign variables after early return.
|
2023-04-11 17:22:58 +00:00
|
|
|
const link = item.querySelector( 'a' );
|
2023-04-12 22:27:28 +00:00
|
|
|
const menuElement = /** @type {HTMLElement} */(
|
|
|
|
item.closest( '.vector-menu' )
|
|
|
|
);
|
|
|
|
if ( !menuElement ) {
|
|
|
|
return;
|
|
|
|
}
|
2023-02-14 14:58:54 +00:00
|
|
|
|
|
|
|
if ( data.id ) {
|
|
|
|
iconElement = createIconElement( menuElement, link, data.id );
|
2022-09-28 15:40:43 +00:00
|
|
|
}
|
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
|
2023-02-14 14:58:54 +00:00
|
|
|
// to accommodate the new item and if not to redirect to the more dropdown.
|
2023-04-12 22:27:28 +00:00
|
|
|
if ( menuElement.id === 'p-views' ) {
|
2023-04-11 17:22:58 +00:00
|
|
|
const availableWidth = getAvailableViewMenuWidth();
|
|
|
|
const moreDropdown = document.querySelector( '#p-cactions ul' );
|
2023-02-14 14:58:54 +00:00
|
|
|
|
|
|
|
if ( moreDropdown && !availableWidth ) {
|
2022-04-27 19:32:19 +00:00
|
|
|
moreDropdown.appendChild( item );
|
|
|
|
// reveal if hidden
|
|
|
|
mw.util.showPortlet( 'p-cactions' );
|
|
|
|
}
|
|
|
|
}
|
2022-08-16 19:50:34 +00:00
|
|
|
|
2023-04-12 22:27:28 +00:00
|
|
|
if ( link && iconElement ) {
|
2022-08-16 19:50:34 +00:00
|
|
|
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
|
|
|
};
|