| 1 |
const dropdownMenus = require( './dropdownMenus.js' ); |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
function addDefaultPortlet( portlet, isDropdown ) { |
| 18 |
const ul = portlet.querySelector( 'ul' ); |
| 19 |
if ( !ul ) { |
| 20 |
return portlet; |
| 21 |
} |
| 22 |
ul.classList.add( 'vector-menu-content-list' ); |
| 23 |
const label = portlet.querySelector( 'label' ); |
| 24 |
if ( label ) { |
| 25 |
const labelDiv = document.createElement( 'div' ); |
| 26 |
labelDiv.classList.add( 'vector-menu-heading' ); |
| 27 |
if ( !isDropdown ) { |
| 28 |
labelDiv.textContent = label.textContent || ''; |
| 29 |
portlet.insertBefore( labelDiv, label ); |
| 30 |
label.remove(); |
| 31 |
} |
| 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 |
portlet.classList.add( 'vector-menu' ); |
| 46 |
return portlet; |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
function makeDropdown( content ) { |
| 57 |
const id = content.id; |
| 58 |
const label = content.querySelector( 'label' ); |
| 59 |
if ( !content.parentNode || !label ) { |
| 60 |
return content; |
| 61 |
} |
| 62 |
label.id = `${ id }-dropdown-label`; |
| 63 |
label.setAttribute( 'for', `${ id }-dropdown-checkbox` ); |
| 64 |
label.classList.add( 'vector-dropdown-label' ); |
| 65 |
label.setAttribute( 'aria-hidden', 'true' ); |
| 66 |
const labelSpan = document.createElement( 'span' ); |
| 67 |
labelSpan.textContent = label.textContent; |
| 68 |
label.textContent = ''; |
| 69 |
labelSpan.classList.add( 'vector-dropdown-label-text' ); |
| 70 |
label.appendChild( labelSpan ); |
| 71 |
const dropdown = document.createElement( 'div' ); |
| 72 |
const checkbox = document.createElement( 'input' ); |
| 73 |
const dropdownContent = document.createElement( 'div' ); |
| 74 |
dropdownContent.classList.add( 'vector-dropdown-content' ); |
| 75 |
checkbox.type = 'checkbox'; |
| 76 |
checkbox.id = `${ id }-dropdown-checkbox`; |
| 77 |
checkbox.setAttribute( 'role', 'button' ); |
| 78 |
checkbox.setAttribute( 'aria-haspopup', 'true' ); |
| 79 |
checkbox.setAttribute( 'data-event-name', `ui.dropdown-${ id }-dropdown` ); |
| 80 |
checkbox.classList.add( 'vector-dropdown-checkbox' ); |
| 81 |
checkbox.setAttribute( 'aria-label', label.textContent || '' ); |
| 82 |
dropdown.id = `${ id }-dropdown`; |
| 83 |
dropdown.classList.add( 'vector-dropdown', `${ id }-dropdown` ); |
| 84 |
dropdown.appendChild( checkbox ); |
| 85 |
dropdown.appendChild( label ); |
| 86 |
dropdown.appendChild( dropdownContent ); |
| 87 |
content.parentNode.insertBefore( dropdown, content ); |
| 88 |
dropdownContent.appendChild( content ); |
| 89 |
dropdownMenus.dropdownMenus( [ dropdown ] ); |
| 90 |
return dropdown; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
function addPortletHandler( portlet, before ) { |
| 103 |
|
| 104 |
const isDropdown = !!( before && before === '#p-cactions' ); |
| 105 |
portlet.classList.remove( 'mw-portlet-js' ); |
| 106 |
|
| 107 |
const transformedPortlet = addDefaultPortlet( portlet, isDropdown ); |
| 108 |
if ( isDropdown ) { |
| 109 |
const pageToolsDropdown = document.querySelector( '#vector-page-tools-dropdown' ); |
| 110 |
const pageToolsMarker = pageToolsDropdown ? pageToolsDropdown.parentNode : null; |
| 111 |
|
| 112 |
if ( pageToolsMarker === null || !pageToolsMarker.parentNode ) { |
| 113 |
throw new Error( 'Vector 2022 addPortletLink: No #vector-page-tools-dropdown element in the DOM.' ); |
| 114 |
} |
| 115 |
const dropdown = makeDropdown( transformedPortlet ); |
| 116 |
pageToolsMarker.parentNode.insertBefore( dropdown, pageToolsMarker ); |
| 117 |
return transformedPortlet; |
| 118 |
} |
| 119 |
return transformedPortlet; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
function main() { |
| 126 |
mw.hook( 'util.addPortlet' ).add( addPortletHandler ); |
| 127 |
|
| 128 |
document.querySelectorAll( '.mw-portlet-js' ).forEach( ( node ) => { |
| 129 |
const nextID = node && node.nextElementSibling && node.nextElementSibling.id; |
| 130 |
addPortletHandler( node, nextID ? `#${ nextID }` : null ); |
| 131 |
} ); |
| 132 |
return { |
| 133 |
addPortletHandler |
| 134 |
}; |
| 135 |
} |
| 136 |
|
| 137 |
module.exports = { |
| 138 |
main, addPortletHandler |
| 139 |
}; |