Debian Codegraph @

skins/Vector/resources/skins.vector.js/dropdownMenus.js

raw symbols SCIP
1 /** @interface CheckboxHack */
2
3 const
4 checkboxHack = /** @type {CheckboxHack} */ require( /** @type {string} */( '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 * Enhance dropdownMenu functionality and accessibility using core's checkboxHack.
12 *
13 * @param {HTMLElement[]|NodeList} [containers]
14 */
15 function dropdownMenus( containers ) {
16 // Search for all dropdown containers using the CHECKBOX_HACK_CONTAINER_SELECTOR.
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 * Create an icon element to be appended inside the anchor tag.
35 *
36 * @param {HTMLElement|null} menuElement
37 * @param {HTMLElement|null} parentElement
38 * @param {string|null} id
39 *
40 * @return {HTMLElement|undefined}
41 */
42 function createIconElement( menuElement, parentElement, id ) {
43 // Only the p-personal menu in the user links dropdown supports icons
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 // The following class allows gadgets developers to style or hide an icon.
59 // * mw-ui-icon-vector-gadget-<id>
60 // The class is considered stable and should not be removed without
61 // a #user-notice.
62 iconElement.classList.add( 'mw-ui-icon-vector-gadget-' + id );
63 }
64
65 return iconElement;
66 }
67
68 /**
69 * Calculate the available width for adding links in the veiws menu,
70 * i.e. the remaining space in the toolbar between the right-navigation
71 * and left-navigation elements.
72 *
73 * @return {number} remaining available pixels in page toolbar or Zero
74 * if remaining space is negative.
75 */
76 function getAvailableViewMenuWidth() {
77 const
78 // Vector toolbar containing namespace, views, more menu etc.
79 toolbar = document.querySelector( '.vector-page-toolbar-container' ),
80 // Assumes all left-side menus are wrapped in a single nav element.
81 // Need to get child width since this node is flex-grow: 1;
82 leftToolbarItems = document.querySelector( '#left-navigation > nav' ),
83 // Right side elements are flex-grow:0 so top-level width is sufficient.
84 rightToolbarItems = document.getElementById( 'right-navigation' );
85
86 // Views menu collapses into "more" menu at this resolution.
87 // Move the link from views to actions menu in this situation.
88 if ( window.innerWidth < 720 ) {
89 return 0;
90 }
91
92 // If any of our assumption about the DOM are wrong, return 0
93 // in order to place the link in a known menu instead.
94 if ( !( toolbar && leftToolbarItems && rightToolbarItems ) ) {
95 return 0;
96 }
97
98 // returning zero instead of negative number makes boolean conversion easier.
99 return Math.max( 0,
100 toolbar.clientWidth - leftToolbarItems.clientWidth - rightToolbarItems.clientWidth
101 );
102 }
103
104 const /** @type {Array<HTMLElement>} */handledLinks = [];
105
106 /**
107 * Adds icon placeholder for gadgets to use.
108 *
109 * @typedef {Object} PortletLinkData
110 * @property {string|null} id
111 */
112 /**
113 * @param {HTMLElement} item
114 * @param {PortletLinkData} data
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 // assign variables after early return.
127 const link = item.querySelector( 'a' );
128 const menuElement = /** @type {HTMLElement} */(
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 // The views menu has limited space so we need to decide whether there is space
140 // to accommodate the new item and if not to redirect to the more dropdown.
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 // reveal if hidden
148 mw.util.showPortlet( 'p-cactions' );
149 }
150 }
151
152 if ( link && iconElement ) {
153 link.prepend( iconElement );
154 }
155 }
156
157 // Enhance previously added items.
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 };