| 1 |
|
| 2 |
|
| 3 |
const |
| 4 |
checkboxHack = require( ( 'mediawiki.page.ready' ) ).checkboxHack, |
| 5 |
CHECKBOX_HACK_CONTAINER_SELECTOR = '.vector-dropdown', |
| 6 |
CHECKBOX_HACK_CHECKBOX_SELECTOR = '.vector-dropdown-checkbox', |
| 7 |
CHECKBOX_HACK_BUTTON_SELECTOR = '.vector-dropdown-label', |
| 8 |
CHECKBOX_HACK_TARGET_SELECTOR = '.vector-dropdown-content'; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
function dropdownMenus( containers ) { |
| 16 |
|
| 17 |
containers = containers || document.querySelectorAll( CHECKBOX_HACK_CONTAINER_SELECTOR ); |
| 18 |
|
| 19 |
Array.prototype.forEach.call( containers, ( container ) => { |
| 20 |
const |
| 21 |
checkbox = container.querySelector( CHECKBOX_HACK_CHECKBOX_SELECTOR ), |
| 22 |
button = container.querySelector( CHECKBOX_HACK_BUTTON_SELECTOR ), |
| 23 |
target = container.querySelector( CHECKBOX_HACK_TARGET_SELECTOR ); |
| 24 |
|
| 25 |
if ( !( checkbox && button && target ) ) { |
| 26 |
return; |
| 27 |
} |
| 28 |
|
| 29 |
checkboxHack.bind( window, checkbox, button, target ); |
| 30 |
} ); |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
function createIconElement( menuElement, parentElement, id ) { |
| 43 |
|
| 44 |
const isIconCapable = menuElement && |
| 45 |
[ |
| 46 |
'p-personal', |
| 47 |
'p-personal-sticky-header' |
| 48 |
].indexOf( menuElement.getAttribute( 'id' ) || 'p-unknown' ) > -1; |
| 49 |
|
| 50 |
if ( !isIconCapable || !parentElement ) { |
| 51 |
return; |
| 52 |
} |
| 53 |
|
| 54 |
const iconElement = document.createElement( 'span' ); |
| 55 |
iconElement.classList.add( 'vector-icon' ); |
| 56 |
|
| 57 |
if ( id ) { |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
iconElement.classList.add( 'mw-ui-icon-vector-gadget-' + id ); |
| 63 |
} |
| 64 |
|
| 65 |
return iconElement; |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
function getAvailableViewMenuWidth() { |
| 77 |
const |
| 78 |
|
| 79 |
toolbar = document.querySelector( '.vector-page-toolbar-container' ), |
| 80 |
|
| 81 |
|
| 82 |
leftToolbarItems = document.querySelector( '#left-navigation > nav' ), |
| 83 |
|
| 84 |
rightToolbarItems = document.getElementById( 'right-navigation' ); |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
if ( window.innerWidth < 720 ) { |
| 89 |
return 0; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
if ( !( toolbar && leftToolbarItems && rightToolbarItems ) ) { |
| 95 |
return 0; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
return Math.max( 0, |
| 100 |
toolbar.clientWidth - leftToolbarItems.clientWidth - rightToolbarItems.clientWidth |
| 101 |
); |
| 102 |
} |
| 103 |
|
| 104 |
const handledLinks = []; |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
function addPortletLinkHandler( item, data ) { |
| 117 |
const linkIsHandled = handledLinks.indexOf( item ); |
| 118 |
let iconElement; |
| 119 |
|
| 120 |
if ( linkIsHandled >= 0 ) { |
| 121 |
return; |
| 122 |
} else { |
| 123 |
handledLinks.push( item ); |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
const link = item.querySelector( 'a' ); |
| 128 |
const menuElement = ( |
| 129 |
item.closest( '.vector-menu' ) |
| 130 |
); |
| 131 |
if ( !menuElement ) { |
| 132 |
return; |
| 133 |
} |
| 134 |
|
| 135 |
if ( data.id ) { |
| 136 |
iconElement = createIconElement( menuElement, link, data.id ); |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
if ( menuElement.id === 'p-views' ) { |
| 142 |
const availableWidth = getAvailableViewMenuWidth(); |
| 143 |
const moreDropdown = document.querySelector( '#p-cactions ul' ); |
| 144 |
|
| 145 |
if ( moreDropdown && !availableWidth ) { |
| 146 |
moreDropdown.appendChild( item ); |
| 147 |
|
| 148 |
mw.util.showPortlet( 'p-cactions' ); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
if ( link && iconElement ) { |
| 153 |
link.prepend( iconElement ); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
Array.prototype.forEach.call( |
| 159 |
document.querySelectorAll( '.mw-list-item-js' ), |
| 160 |
( item ) => { |
| 161 |
addPortletLinkHandler( item, { |
| 162 |
id: item.getAttribute( 'id' ) |
| 163 |
} ); |
| 164 |
} |
| 165 |
); |
| 166 |
|
| 167 |
mw.hook( 'util.addPortletLink' ).add( addPortletLinkHandler ); |
| 168 |
|
| 169 |
module.exports = { |
| 170 |
dropdownMenus, |
| 171 |
addPortletLinkHandler: addPortletLinkHandler |
| 172 |
}; |