| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
function getClassesForItem( $item ) { |
| 7 |
const $parent = $item.parent(); |
| 8 |
|
| 9 |
const isPageActionList = $parent.hasClass( 'page-actions-menu__list' ); |
| 10 |
|
| 11 |
const isTabContainer = $parent.hasClass( 'minerva__tab-container' ); |
| 12 |
|
| 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 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 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 |
|
| 62 |
|
| 63 |
classes += ` mw-ui-icon-portletlink-${ id }`; |
| 64 |
} |
| 65 |
icon.setAttribute( 'class', classes ); |
| 66 |
$link.prepend( icon ); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
function hookHandler( listItem, data ) { |
| 75 |
if ( listItem && !listItem.dataset.minervaPortlet ) { |
| 76 |
const id = data.id; |
| 77 |
const $item = $( listItem ); |
| 78 |
|
| 79 |
|
| 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 |
|
| 89 |
|
| 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 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 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 |
}; |