Debian Codegraph @

skins/MinervaNeue/resources/skins.minerva.scripts/addPortletLink.js

raw symbols SCIP
1 /**
2 * @private
3 * @param {jQuery} $item The added list item, or null if no element was added.
4 * @return {Object} of arrays with mandatory class names for list item elements.
5 */
6 function getClassesForItem( $item ) {
7 const $parent = $item.parent();
8 // eslint-disable-next-line no-jquery/no-class-state
9 const isPageActionList = $parent.hasClass( 'page-actions-menu__list' );
10 // eslint-disable-next-line no-jquery/no-class-state
11 const isTabContainer = $parent.hasClass( 'minerva__tab-container' );
12 // eslint-disable-next-line no-jquery/no-class-state
13 const isToggleList = $parent.hasClass( 'toggle-list__list' );
14
15 if ( isToggleList ) {
16 return {
17 li: [ 'toggle-list-item' ],
18 span: [ 'toggle-list-item__label' ],
19 a: [ 'toggle-list-item__anchor' ]
20 };
21 } else if ( isPageActionList ) {
22 return {
23 li: [ 'page-actions-menu__list-item' ],
24 span: [],
25 a: [
26 'cdx-button',
27 'cdx-button--size-large',
28 'cdx-button--fake-button',
29 'cdx-button--fake-button--enabled',
30 'cdx-button--icon-only',
31 'cdx-button--weight-quiet'
32 ]
33 };
34 } else if ( isTabContainer ) {
35 return {
36 li: [ 'minerva__tab' ],
37 span: [],
38 a: [ 'minerva__tab-text' ]
39 };
40 } else {
41 return {
42 li: [],
43 span: [],
44 a: []
45 };
46 }
47 }
48
49 /**
50 * Insert icon into the portlet link.
51 *
52 * @private
53 * @param {jQuery} $link
54 * @param {string|undefined} id for icon
55 */
56 function insertIcon( $link, id ) {
57 const icon = document.createElement( 'span' );
58 let classes = 'minerva-icon';
59 if ( id ) {
60 classes += ` minerva-icon-portletlink-${ id }`;
61 // FIXME: Please remove when following URL returns zero results:
62 // https://global-search.toolforge.org/?q=mw-ui-icon-portletlink&regex=1&namespaces=&title=
63 classes += ` mw-ui-icon-portletlink-${ id }`;
64 }
65 icon.setAttribute( 'class', classes );
66 $link.prepend( icon );
67 }
68
69 /**
70 * @param {HTMLElement|null} listItem The added list item, or null if no element was added.
71 * @param {Object} data
72 * @ignore
73 */
74 function hookHandler( listItem, data ) {
75 if ( listItem && !listItem.dataset.minervaPortlet ) {
76 const id = data.id;
77 const $item = $( listItem );
78
79 // add the corresponding classes
80 const classes = getClassesForItem( $item );
81 $item.addClass( classes.li );
82 const $a = $item.find( 'a' );
83 $a.addClass( classes.a );
84 $item.find( 'a > span' ).addClass( classes.span );
85
86 listItem.dataset.minervaPortlet = true;
87
88 // if the list item is not itself an icon, add the corresponding icon
89 // (except tabs, which do not have icons)
90 if ( classes.span.indexOf( 'minerva-icon' ) === -1 &&
91 classes.li.indexOf( 'minerva__tab' ) === -1 ) {
92 insertIcon( $a, id );
93 }
94 }
95 }
96
97 /**
98 * Init portlet link items added by gadgets prior to Minerva
99 * loading.
100 *
101 * @ignore
102 */
103 function init() {
104 Array.prototype.forEach.call(
105 document.querySelectorAll( '.mw-list-item-js' ),
106 ( item ) => {
107 hookHandler( item, {
108 id: item.getAttribute( 'id' )
109 } );
110 }
111 );
112 }
113 module.exports = {
114 init,
115 hookHandler
116 };