2020-02-10 04:13:01 +00:00
|
|
|
/**
|
2023-10-19 14:07:38 +00:00
|
|
|
* @param {jQuery} $item The added list item, or null if no element was added.
|
2020-02-10 04:13:01 +00:00
|
|
|
* @return {Object} of arrays with mandatory class names for list item elements.
|
|
|
|
*/
|
|
|
|
function getClassesForItem( $item ) {
|
2024-03-24 20:53:43 +00:00
|
|
|
const $parent = $item.parent();
|
|
|
|
// eslint-disable-next-line no-jquery/no-class-state
|
|
|
|
const isPageActionList = $parent.hasClass( 'page-actions-menu__list' );
|
|
|
|
// eslint-disable-next-line no-jquery/no-class-state
|
|
|
|
const isTabContainer = $parent.hasClass( 'minerva__tab-container' );
|
|
|
|
// eslint-disable-next-line no-jquery/no-class-state
|
|
|
|
const isToggleList = $parent.hasClass( 'toggle-list__list' );
|
2021-09-23 22:44:49 +00:00
|
|
|
|
2020-02-10 04:13:01 +00:00
|
|
|
if ( isToggleList ) {
|
|
|
|
return {
|
|
|
|
li: [ 'toggle-list-item' ],
|
|
|
|
span: [ 'toggle-list-item__label' ],
|
|
|
|
a: [ 'toggle-list-item__anchor' ]
|
|
|
|
};
|
2021-09-23 22:44:49 +00:00
|
|
|
} else if ( isPageActionList ) {
|
|
|
|
return {
|
|
|
|
li: [ 'page-actions-menu__list-item' ],
|
2023-08-18 21:48:33 +00:00
|
|
|
span: [],
|
|
|
|
a: [
|
2023-08-23 20:43:55 +00:00
|
|
|
'cdx-button',
|
|
|
|
'cdx-button--size-large',
|
|
|
|
'cdx-button--fake-button',
|
|
|
|
'cdx-button--fake-button--enabled',
|
|
|
|
'cdx-button--icon-only',
|
|
|
|
'cdx-button--weight-quiet'
|
2023-08-18 21:48:33 +00:00
|
|
|
]
|
2021-09-23 22:44:49 +00:00
|
|
|
};
|
2023-12-21 19:54:31 +00:00
|
|
|
} else if ( isTabContainer ) {
|
|
|
|
return {
|
|
|
|
li: [ 'minerva__tab' ],
|
|
|
|
span: [],
|
|
|
|
a: []
|
|
|
|
};
|
2020-02-10 04:13:01 +00:00
|
|
|
} else {
|
|
|
|
return {
|
|
|
|
li: [],
|
|
|
|
span: [],
|
|
|
|
a: []
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-09-23 19:47:46 +00:00
|
|
|
* Insert icon into the portlet link.
|
|
|
|
*
|
2023-10-19 14:07:38 +00:00
|
|
|
* @param {jQuery} $link
|
2022-02-04 22:06:53 +00:00
|
|
|
* @param {string|undefined} id for icon
|
2021-09-23 19:47:46 +00:00
|
|
|
*/
|
|
|
|
function insertIcon( $link, id ) {
|
2024-03-24 20:53:43 +00:00
|
|
|
const icon = document.createElement( 'span' );
|
|
|
|
let classes = 'minerva-icon';
|
2022-02-04 22:06:53 +00:00
|
|
|
if ( id ) {
|
2024-01-24 22:02:12 +00:00
|
|
|
classes += ` minerva-icon-portletlink-${ id }`;
|
2023-09-12 21:22:20 +00:00
|
|
|
// FIXME: Please remove when following URL returns zero results:
|
|
|
|
// https://global-search.toolforge.org/?q=mw-ui-icon-portletlink®ex=1&namespaces=&title=
|
2024-01-24 22:02:12 +00:00
|
|
|
classes += ` mw-ui-icon-portletlink-${ id }`;
|
2022-02-04 22:06:53 +00:00
|
|
|
}
|
|
|
|
icon.setAttribute( 'class', classes );
|
2021-09-23 19:47:46 +00:00
|
|
|
$link.prepend( icon );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {HTMLElement|null} listItem The added list item, or null if no element was added.
|
2020-02-10 04:13:01 +00:00
|
|
|
* @param {Object} data
|
|
|
|
*/
|
2021-09-23 19:47:46 +00:00
|
|
|
function hookHandler( listItem, data ) {
|
2021-09-23 22:44:49 +00:00
|
|
|
if ( listItem && !listItem.dataset.minervaPortlet ) {
|
2024-03-24 20:53:43 +00:00
|
|
|
const id = data.id;
|
|
|
|
const $item = $( listItem );
|
2023-12-21 19:54:31 +00:00
|
|
|
|
|
|
|
// add the corresponding classes
|
2024-03-24 20:53:43 +00:00
|
|
|
const classes = getClassesForItem( $item );
|
2020-02-10 04:13:01 +00:00
|
|
|
$item.addClass( classes.li );
|
2024-03-24 20:53:43 +00:00
|
|
|
const $a = $item.find( 'a' );
|
2021-09-23 19:47:46 +00:00
|
|
|
$a.addClass( classes.a );
|
|
|
|
$item.find( 'a > span' ).addClass( classes.span );
|
2023-12-21 19:54:31 +00:00
|
|
|
|
2021-09-23 22:44:49 +00:00
|
|
|
listItem.dataset.minervaPortlet = true;
|
2023-12-21 19:54:31 +00:00
|
|
|
|
|
|
|
// if the list item is not itself an icon, add the corresponding icon
|
|
|
|
// (except tabs, which do not have icons)
|
|
|
|
if ( classes.span.indexOf( 'minerva-icon' ) === -1 &&
|
|
|
|
classes.li.indexOf( 'minerva__tab' ) === -1 ) {
|
2021-09-23 22:44:49 +00:00
|
|
|
insertIcon( $a, id );
|
|
|
|
}
|
2020-02-10 04:13:01 +00:00
|
|
|
}
|
2021-09-23 19:47:46 +00:00
|
|
|
}
|
|
|
|
|
2021-09-23 22:44:49 +00:00
|
|
|
/**
|
|
|
|
* Init portlet link items added by gadgets prior to Minerva
|
|
|
|
* loading.
|
|
|
|
*/
|
|
|
|
function init() {
|
|
|
|
Array.prototype.forEach.call(
|
|
|
|
document.querySelectorAll( '.mw-list-item-js' ),
|
|
|
|
function ( item ) {
|
|
|
|
hookHandler( item, {
|
|
|
|
id: item.getAttribute( 'id' )
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2021-09-23 19:47:46 +00:00
|
|
|
module.exports = {
|
2021-09-23 22:44:49 +00:00
|
|
|
init: init,
|
2021-09-23 19:47:46 +00:00
|
|
|
hookHandler: hookHandler
|
2020-02-10 04:13:01 +00:00
|
|
|
};
|