| 1 | /* eslint-disable no-jquery/no-jquery-constructor, no-jquery/no-other-methods, no-jquery/no-css, |
| 2 | no-jquery/no-find-collection, no-jquery/no-each-collection, no-jquery/no-attr */ |
| 3 | /** |
| 4 | * Collapsible tabs for Vector |
| 5 | */ |
| 6 | function init() { |
| 7 | const cactionsId = 'p-cactions', |
| 8 | $cactions = $( '#' + cactionsId ), |
| 9 | // eslint-disable-next-line no-jquery/no-global-selector |
| 10 | $tabContainer = $( '#p-views ul' ); |
| 11 | let initialCactionsWidth = function () { |
| 12 | // HACK: This depends on a discouraged feature of jQuery width(). |
| 13 | // The #p-cactions element is generally hidden by default, but |
| 14 | // the consumers of this function need to know the width that the |
| 15 | // "More" menu would consume if it were visible. This means it |
| 16 | // must not return 0 if hidden, but rather virtually render it |
| 17 | // and compute its width, then hide it again. jQuery width() does |
| 18 | // all that for us. |
| 19 | const width = $cactions.width() || 0; |
| 20 | initialCactionsWidth = function () { |
| 21 | return width; |
| 22 | }; |
| 23 | return width; |
| 24 | }; |
| 25 | |
| 26 | // Bind callback functions to animate our drop down menu in and out |
| 27 | // and then call the collapsibleTabs function on the menu |
| 28 | $tabContainer |
| 29 | .on( 'beforeTabCollapse', () => { |
| 30 | let expandedWidth; |
| 31 | // If the dropdown was hidden, show it |
| 32 | if ( !mw.util.isPortletVisible( cactionsId ) ) { |
| 33 | mw.util.showPortlet( cactionsId ); |
| 34 | // Now that it is visible, force-render it virtually |
| 35 | // to get its expanded width, then shrink it 1px before we |
| 36 | // yield from JS (which means the expansion won't be visible). |
| 37 | // Then animate from the 1px to the expanded width. |
| 38 | expandedWidth = $cactions.width(); |
| 39 | // eslint-disable-next-line no-jquery/no-animate |
| 40 | $cactions |
| 41 | .css( 'width', '1px' ) |
| 42 | .animate( { width: expandedWidth }, 'normal' ); |
| 43 | } |
| 44 | } ) |
| 45 | .on( 'beforeTabExpand', () => { |
| 46 | // If we're removing the last child node right now, hide the dropdown |
| 47 | if ( $cactions.find( 'li' ).length === 1 ) { |
| 48 | // eslint-disable-next-line no-jquery/no-animate |
| 49 | $cactions.animate( { width: '1px' }, 'normal', function () { |
| 50 | $( this ).attr( 'style', '' ); |
| 51 | mw.util.hidePortlet( cactionsId ); |
| 52 | } ); |
| 53 | } |
| 54 | } ) |
| 55 | .collapsibleTabs( { |
| 56 | expandCondition: function ( eleWidth ) { |
| 57 | // This looks a bit awkward because we're doing expensive queries as late |
| 58 | // as possible. |
| 59 | const distance = $.collapsibleTabs.calculateTabDistance(); |
| 60 | // If there are at least eleWidth + 1 pixels of free space, expand. |
| 61 | // We add 1 because .width() will truncate fractional values but .offset() will not. |
| 62 | if ( distance >= eleWidth + 1 ) { |
| 63 | return true; |
| 64 | } else { |
| 65 | // Maybe we can still expand? Account for the width of the "Actions" dropdown |
| 66 | // if the expansion would hide it. |
| 67 | if ( $cactions.find( 'li' ).length === 1 ) { |
| 68 | return distance >= eleWidth + 1 - initialCactionsWidth(); |
| 69 | } else { |
| 70 | return false; |
| 71 | } |
| 72 | } |
| 73 | }, |
| 74 | collapseCondition: function () { |
| 75 | let collapsibleWidth = 0, |
| 76 | doCollapse = false; |
| 77 | |
| 78 | // This looks a bit awkward because we're doing expensive queries as late |
| 79 | // as possible. |
| 80 | // TODO: The dropdown itself should probably "fold" to just the down-arrow |
| 81 | // (hiding the text) if it can't fit on the line? |
| 82 | |
| 83 | // Never collapse if there is no overlap. |
| 84 | if ( $.collapsibleTabs.calculateTabDistance() >= 0 ) { |
| 85 | return false; |
| 86 | } |
| 87 | |
| 88 | // Always collapse if the "More" button is already shown. |
| 89 | if ( mw.util.isPortletVisible( cactionsId ) ) { |
| 90 | return true; |
| 91 | } |
| 92 | |
| 93 | // If we reach here, this means: |
| 94 | // 1. #p-cactions is currently empty and invisible (e.g. when logged out), |
| 95 | // 2. and, there is at least one li.collapsible link in #p-views, as asserted |
| 96 | // by handleResize() before calling here. Such link exists e.g. as |
| 97 | // "View history" on articles, but generally not on special pages. |
| 98 | // 3. and, the left-navigation and right-navigation are overlapping |
| 99 | // each other, e.g. when making the window very narrow, or if a gadget |
| 100 | // added a lot of tabs. |
| 101 | $tabContainer.children( 'li.collapsible' ).each( ( _index, element ) => { |
| 102 | collapsibleWidth += $( element ).width() || 0; |
| 103 | if ( collapsibleWidth > initialCactionsWidth() ) { |
| 104 | // We've found one or more collapsible links that are wider |
| 105 | // than the "More" menu would be if it were made visible, |
| 106 | // which means it is worth doing a collapsing. |
| 107 | doCollapse = true; |
| 108 | // Stop this possibly expensive loop the moment the condition is met once. |
| 109 | return false; |
| 110 | } |
| 111 | return; |
| 112 | } ); |
| 113 | return doCollapse; |
| 114 | } |
| 115 | } ); |
| 116 | } |
| 117 | |
| 118 | module.exports = Object.freeze( { init: init } ); |