| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
function addDefaultPortlet( portlet ) { |
| 15 |
portlet.classList.add( 'vector-menu' ); |
| 16 |
const ul = portlet.querySelector( 'ul' ); |
| 17 |
if ( !ul ) { |
| 18 |
return portlet; |
| 19 |
} |
| 20 |
ul.classList.add( 'vector-menu-content-list' ); |
| 21 |
const label = portlet.querySelector( 'label' ); |
| 22 |
const labelId = `${ portlet.id }-label`; |
| 23 |
if ( label ) { |
| 24 |
const labelDiv = document.createElement( 'div' ); |
| 25 |
labelDiv.id = labelId; |
| 26 |
labelDiv.classList.add( 'vector-menu-heading' ); |
| 27 |
labelDiv.textContent = label.textContent || ''; |
| 28 |
portlet.insertBefore( labelDiv, label ); |
| 29 |
label.remove(); |
| 30 |
portlet.setAttribute( 'aria-labelledby', labelId ); |
| 31 |
portlet.setAttribute( 'role', 'navigation' ); |
| 32 |
} |
| 33 |
let wrapper = portlet.querySelector( 'div:last-child' ); |
| 34 |
if ( wrapper ) { |
| 35 |
ul.remove(); |
| 36 |
wrapper.appendChild( ul ); |
| 37 |
wrapper.classList.add( 'vector-menu-content' ); |
| 38 |
} else { |
| 39 |
wrapper = document.createElement( 'div' ); |
| 40 |
wrapper.classList.add( 'vector-menu-content' ); |
| 41 |
ul.remove(); |
| 42 |
wrapper.appendChild( ul ); |
| 43 |
portlet.appendChild( wrapper ); |
| 44 |
} |
| 45 |
|
| 46 |
if ( portlet.closest( '.vector-legacy-sidebar' ) ) { |
| 47 |
portlet.classList.add( 'vector-menu-portal' ); |
| 48 |
} |
| 49 |
|
| 50 |
return portlet; |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
function addPortletHandler( portlet ) { |
| 61 |
const parent = ( portlet.parentNode ); |
| 62 |
|
| 63 |
|
| 64 |
if ( parent && parent.id === 'right-navigation' ) { |
| 65 |
const id = portlet.id; |
| 66 |
portlet.classList.add( 'vector-menu-dropdown' ); |
| 67 |
const checkbox = document.createElement( 'input' ); |
| 68 |
checkbox.type = 'checkbox'; |
| 69 |
checkbox.id = `${ id }-checkbox`; |
| 70 |
checkbox.setAttribute( 'role', 'button' ); |
| 71 |
checkbox.setAttribute( 'aria-haspopup', 'true' ); |
| 72 |
checkbox.setAttribute( 'data-event-name', `ui.dropdown-${ id }` ); |
| 73 |
checkbox.setAttribute( 'class', 'vector-menu-checkbox' ); |
| 74 |
checkbox.setAttribute( 'aria-labelledby', `${ id }-label` ); |
| 75 |
portlet.prepend( checkbox ); |
| 76 |
} |
| 77 |
portlet.classList.remove( 'mw-portlet-js' ); |
| 78 |
return addDefaultPortlet( portlet ); |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
function main() { |
| 85 |
mw.hook( 'util.addPortlet' ).add( addPortletHandler ); |
| 86 |
|
| 87 |
document.querySelectorAll( '.mw-portlet-js' ).forEach( addPortletHandler ); |
| 88 |
return { |
| 89 |
addPortletHandler |
| 90 |
}; |
| 91 |
} |
| 92 |
|
| 93 |
module.exports = { |
| 94 |
main, addPortletHandler |
| 95 |
}; |