gitlab-org--gitlab-foss/app/assets/javascripts/fly_out_nav.js

47 lines
1.4 KiB
JavaScript
Raw Normal View History

export const calculateTop = (boundingRect, outerHeight) => {
const windowHeight = window.innerHeight;
const bottomOverflow = windowHeight - (boundingRect.top + outerHeight);
return bottomOverflow < 0 ? (boundingRect.top - outerHeight) + boundingRect.height :
boundingRect.top;
};
2017-08-01 07:49:03 +00:00
export const showSubLevelItems = (el) => {
const $subitems = el.querySelector('.sidebar-sub-level-items');
if (!$subitems) return;
$subitems.style.display = 'block';
el.classList.add('is-over');
2017-08-01 07:49:03 +00:00
const boundingRect = el.getBoundingClientRect();
const top = calculateTop(boundingRect, $subitems.offsetHeight);
const isAbove = top < boundingRect.top;
2017-08-01 11:41:55 +00:00
$subitems.style.transform = `translate3d(0, ${Math.floor(top)}px, 0)`;
2017-08-01 07:49:03 +00:00
if (isAbove) {
$subitems.classList.add('is-above');
}
};
export const hideSubLevelItems = (el) => {
const $subitems = el.querySelector('.sidebar-sub-level-items');
if (!$subitems) return;
2017-08-01 07:49:03 +00:00
el.classList.remove('is-over');
$subitems.style.display = 'none';
$subitems.classList.remove('is-above');
2017-08-01 07:49:03 +00:00
};
export default () => {
const items = [...document.querySelectorAll('.sidebar-top-level-items > li:not(.active)')]
.filter(el => el.querySelector('.sidebar-sub-level-items'));
2017-08-01 07:49:03 +00:00
items.forEach((el) => {
el.addEventListener('mouseenter', e => showSubLevelItems(e.currentTarget));
el.addEventListener('mouseleave', e => hideSubLevelItems(e.currentTarget));
});
};