Debian Codegraph @

skins/MinervaNeue/resources/skins.minerva.scripts/TabScroll.js

raw symbols SCIP
1 let scrollLeftStyle = null;
2
3 function testScrollLeftStyle() {
4 if ( scrollLeftStyle !== null ) {
5 return scrollLeftStyle;
6 }
7 // Detect which scrollLeft style the browser uses
8 // Adapted from <https://github.com/othree/jquery.rtl-scroll-type>.
9 // Original code copyright 2012 Wei-Ko Kao, licensed under the MIT License.
10 // Adaptation copied from OO.ui.Element.static.getScrollLeft
11 const $definer = $( '<div>' ).attr( {
12 dir: 'rtl',
13 style: 'font-size: 14px; width: 4px; height: 1px; position: absolute; top: -1000px; overflow: scroll;'
14 } ).text( 'ABCD' );
15 $definer.appendTo( document.body );
16 const definer = $definer[ 0 ];
17 if ( definer.scrollLeft > 0 ) {
18 // Safari, Chrome
19 scrollLeftStyle = 'default';
20 } else {
21 definer.scrollLeft = 1;
22 if ( definer.scrollLeft === 0 ) {
23 // Firefox, old Opera
24 scrollLeftStyle = 'negative';
25 } else {
26 // Internet Explorer, Edge
27 scrollLeftStyle = 'reverse';
28 }
29 }
30 $definer.remove();
31 return scrollLeftStyle;
32 }
33
34 /**
35 * When tabs are present and one is selected, scroll the selected tab into view.
36 *
37 * @ignore
38 */
39 function initTabsScrollPosition() {
40 // eslint-disable-next-line no-jquery/no-global-selector
41 const $selectedTab = $( '.minerva__tab.selected' );
42 if ( $selectedTab.length !== 1 ) {
43 return;
44 }
45 const selectedTab = $selectedTab.get( 0 );
46 const $tabContainer = $selectedTab.closest( '.minerva__tab-container' );
47 const tabContainer = $tabContainer.get( 0 );
48 const maxScrollLeft = tabContainer.scrollWidth - tabContainer.clientWidth;
49 const dir = $tabContainer.css( 'direction' ) || 'ltr';
50
51 /**
52 * Set tabContainer.scrollLeft, with adjustments for browser inconsistencies in RTL
53 *
54 * @param {number} sl New .scrollLeft value, in 'default' (WebKit) style
55 */
56 function setScrollLeft( sl ) {
57 if ( dir === 'ltr' ) {
58 tabContainer.scrollLeft = sl;
59 return;
60 }
61
62 if ( testScrollLeftStyle() === 'reverse' ) {
63 sl = maxScrollLeft - sl;
64 } else if ( testScrollLeftStyle() === 'negative' ) {
65 sl = -( maxScrollLeft - sl );
66 }
67 tabContainer.scrollLeft = sl;
68 }
69
70 const leftMostChild = dir === 'ltr' ? tabContainer.firstElementChild : tabContainer.lastElementChild;
71 const rightMostChild = dir === 'ltr' ? tabContainer.lastElementChild : tabContainer.firstElementChild;
72 // If the tab is wider than the container (doesn't fit), this value will be negative
73 const widthDiff = tabContainer.clientWidth - selectedTab.clientWidth;
74
75 if ( selectedTab === leftMostChild ) {
76 // The left-most tab is selected. If the tab fits, scroll all the way to the left.
77 // If the tab doesn't fit, align its start edge with the container's start edge.
78 if ( dir === 'ltr' || widthDiff >= 0 ) {
79 setScrollLeft( 0 );
80 } else {
81 setScrollLeft( -widthDiff );
82 }
83 } else if ( selectedTab === rightMostChild ) {
84 // The right-most tab is selected. If the tab fits, scroll all the way to the right.
85 // If the tab doesn't fit, align its start edge with the container's start edge.
86 if ( dir === 'rtl' || widthDiff >= 0 ) {
87 setScrollLeft( maxScrollLeft );
88 } else {
89 setScrollLeft( maxScrollLeft + widthDiff );
90 }
91 } else {
92 // The selected tab is not the left-most or right-most, it's somewhere in the middle
93 const tabPosition = $selectedTab.position();
94 const containerPosition = $tabContainer.position();
95 // Position of the left edge of $selectedTab relative to the left edge of $tabContainer
96 const left = tabPosition.left - containerPosition.left;
97 // Because the calculations above use the existing .scrollLeft from the browser,
98 // we should not use setScrollLeft() here. Instead, we rely on the fact that scrollLeft
99 // increases to the left in the 'default' and 'negative' modes, and to the right in
100 // the 'reverse' mode, so we can add/subtract a delta to/from scrollLeft accordingly.
101 let increaseScrollLeft;
102 if ( widthDiff >= 0 ) {
103 // The tab fits, center it
104 increaseScrollLeft = left - widthDiff / 2;
105 } else if ( dir === 'ltr' ) {
106 // The tab doesn't fit (LTR), align its left edge with the container's left edge
107 increaseScrollLeft = left;
108 } else {
109 // The tab doesn't fit (RTL), align its right edge with the container's right edge
110 increaseScrollLeft = left - widthDiff;
111 }
112 tabContainer.scrollLeft += increaseScrollLeft *
113 ( testScrollLeftStyle() === 'reverse' ? -1 : 1 );
114 }
115 }
116
117 module.exports = {
118 initTabsScrollPosition: initTabsScrollPosition
119 };