2020-02-10 04:13:01 +00:00
|
|
|
/**
|
|
|
|
* @param {jQuery.Object} $item The added list item, or null if no element was added.
|
|
|
|
* @return {Object} of arrays with mandatory class names for list item elements.
|
|
|
|
*/
|
|
|
|
function getClassesForItem( $item ) {
|
2021-09-23 22:44:49 +00:00
|
|
|
var $parent = $item.parent(),
|
2023-08-23 20:43:55 +00:00
|
|
|
useCodex = mw.loader.getState( 'codex-search-styles' ) !== 'registered',
|
2021-09-23 22:44:49 +00:00
|
|
|
// eslint-disable-next-line no-jquery/no-class-state
|
|
|
|
isPageActionList = $parent.hasClass( 'page-actions-menu__list' ),
|
|
|
|
// eslint-disable-next-line no-jquery/no-class-state
|
|
|
|
isToggleList = $parent.hasClass( 'toggle-list__list' );
|
|
|
|
|
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-23 20:43:55 +00:00
|
|
|
span: useCodex ? [] : [
|
2021-09-23 22:44:49 +00:00
|
|
|
'mw-ui-icon', 'mw-ui-icon-element',
|
|
|
|
'mw-ui-icon-with-label-desktop', 'mw-ui-button', 'mw-ui-quiet'
|
|
|
|
],
|
2023-08-23 20:43:55 +00:00
|
|
|
a: useCodex ? [
|
|
|
|
'cdx-button',
|
|
|
|
'cdx-button--size-large',
|
|
|
|
'cdx-button--fake-button',
|
|
|
|
'cdx-button--fake-button--enabled',
|
|
|
|
'cdx-button--icon-only',
|
|
|
|
'cdx-button--weight-quiet'
|
|
|
|
] : []
|
2021-09-23 22:44:49 +00:00
|
|
|
};
|
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.
|
|
|
|
*
|
|
|
|
* @param {jQuery.Object} $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 ) {
|
2023-08-23 20:43:55 +00:00
|
|
|
var useCodex = mw.loader.getState( 'codex-search-styles' ) !== 'registered';
|
2022-02-04 22:06:53 +00:00
|
|
|
var icon = document.createElement( 'span' ),
|
2023-08-23 20:43:55 +00:00
|
|
|
classes = useCodex ? 'minerva-icon' : 'mw-ui-icon';
|
2022-02-04 22:06:53 +00:00
|
|
|
if ( id ) {
|
|
|
|
classes += ' mw-ui-icon-portletlink-' + id;
|
|
|
|
}
|
|
|
|
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 ) {
|
|
|
|
var $item, $a, classes,
|
2021-09-23 22:44:49 +00:00
|
|
|
id = data.id;
|
2020-02-10 04:13:01 +00:00
|
|
|
|
2021-09-23 22:44:49 +00:00
|
|
|
if ( listItem && !listItem.dataset.minervaPortlet ) {
|
2021-09-23 19:47:46 +00:00
|
|
|
$item = $( listItem );
|
2020-02-10 04:13:01 +00:00
|
|
|
classes = getClassesForItem( $item );
|
|
|
|
$item.addClass( classes.li );
|
|
|
|
$a = $item.find( 'a' );
|
2021-09-23 19:47:46 +00:00
|
|
|
$a.addClass( classes.a );
|
|
|
|
$item.find( 'a > span' ).addClass( classes.span );
|
2021-09-23 22:44:49 +00:00
|
|
|
listItem.dataset.minervaPortlet = true;
|
2022-02-04 22:06:53 +00:00
|
|
|
if ( classes.span.indexOf( 'mw-ui-icon' ) === -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
|
|
|
};
|