| 1 |
let scrollLeftStyle = null; |
| 2 |
|
| 3 |
function testScrollLeftStyle() { |
| 4 |
if ( scrollLeftStyle !== null ) { |
| 5 |
return scrollLeftStyle; |
| 6 |
} |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 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 |
|
| 19 |
scrollLeftStyle = 'default'; |
| 20 |
} else { |
| 21 |
definer.scrollLeft = 1; |
| 22 |
if ( definer.scrollLeft === 0 ) { |
| 23 |
|
| 24 |
scrollLeftStyle = 'negative'; |
| 25 |
} else { |
| 26 |
|
| 27 |
scrollLeftStyle = 'reverse'; |
| 28 |
} |
| 29 |
} |
| 30 |
$definer.remove(); |
| 31 |
return scrollLeftStyle; |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
function initTabsScrollPosition() { |
| 40 |
|
| 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 |
|
| 53 |
|
| 54 |
|
| 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 |
|
| 73 |
const widthDiff = tabContainer.clientWidth - selectedTab.clientWidth; |
| 74 |
|
| 75 |
if ( selectedTab === leftMostChild ) { |
| 76 |
|
| 77 |
|
| 78 |
if ( dir === 'ltr' || widthDiff >= 0 ) { |
| 79 |
setScrollLeft( 0 ); |
| 80 |
} else { |
| 81 |
setScrollLeft( -widthDiff ); |
| 82 |
} |
| 83 |
} else if ( selectedTab === rightMostChild ) { |
| 84 |
|
| 85 |
|
| 86 |
if ( dir === 'rtl' || widthDiff >= 0 ) { |
| 87 |
setScrollLeft( maxScrollLeft ); |
| 88 |
} else { |
| 89 |
setScrollLeft( maxScrollLeft + widthDiff ); |
| 90 |
} |
| 91 |
} else { |
| 92 |
|
| 93 |
const tabPosition = $selectedTab.position(); |
| 94 |
const containerPosition = $tabContainer.position(); |
| 95 |
|
| 96 |
const left = tabPosition.left - containerPosition.left; |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
let increaseScrollLeft; |
| 102 |
if ( widthDiff >= 0 ) { |
| 103 |
|
| 104 |
increaseScrollLeft = left - widthDiff / 2; |
| 105 |
} else if ( dir === 'ltr' ) { |
| 106 |
|
| 107 |
increaseScrollLeft = left; |
| 108 |
} else { |
| 109 |
|
| 110 |
increaseScrollLeft = left - widthDiff; |
| 111 |
} |
| 112 |
tabContainer.scrollLeft += increaseScrollLeft * |
| 113 |
( testScrollLeftStyle() === 'reverse' ? -1 : 1 ); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
module.exports = { |
| 118 |
initTabsScrollPosition: initTabsScrollPosition |
| 119 |
}; |