2019-12-23 07:08:18 -05:00
|
|
|
import { GlBreakpointInstance as bp } from '@gitlab/ui/dist/utils';
|
2019-03-11 14:19:34 -04:00
|
|
|
import { SIDEBAR_COLLAPSED_CLASS } from './contextual_sidebar';
|
2017-08-02 05:22:13 -04:00
|
|
|
|
2017-08-14 03:58:22 -04:00
|
|
|
const HIDE_INTERVAL_TIMEOUT = 300;
|
2021-07-06 08:07:43 -04:00
|
|
|
const COLLAPSED_PANEL_WIDTH = 48;
|
2017-08-04 10:32:44 -04:00
|
|
|
const IS_OVER_CLASS = 'is-over';
|
|
|
|
const IS_ABOVE_CLASS = 'is-above';
|
|
|
|
const IS_SHOWING_FLY_OUT_CLASS = 'is-showing-fly-out';
|
|
|
|
let currentOpenMenu = null;
|
|
|
|
let menuCornerLocs;
|
|
|
|
let timeoutId;
|
2017-08-19 15:41:23 -04:00
|
|
|
let sidebar;
|
2017-08-04 10:32:44 -04:00
|
|
|
|
|
|
|
export const mousePos = [];
|
|
|
|
|
2020-12-23 19:10:25 -05:00
|
|
|
export const setSidebar = (el) => {
|
2018-10-24 15:17:03 -04:00
|
|
|
sidebar = el;
|
|
|
|
};
|
2017-08-30 06:48:33 -04:00
|
|
|
export const getOpenMenu = () => currentOpenMenu;
|
2018-10-24 15:17:03 -04:00
|
|
|
export const setOpenMenu = (menu = null) => {
|
|
|
|
currentOpenMenu = menu;
|
|
|
|
};
|
2017-08-04 10:32:44 -04:00
|
|
|
|
|
|
|
export const slope = (a, b) => (b.y - a.y) / (b.x - a.x);
|
|
|
|
|
2021-06-10 20:10:14 -04:00
|
|
|
export const getHeaderHeight = () => sidebar?.offsetTop || 0;
|
2017-08-10 04:10:25 -04:00
|
|
|
|
2018-10-24 15:17:03 -04:00
|
|
|
export const isSidebarCollapsed = () =>
|
2019-03-11 14:19:34 -04:00
|
|
|
sidebar && sidebar.classList.contains(SIDEBAR_COLLAPSED_CLASS);
|
2017-09-01 03:41:31 -04:00
|
|
|
|
2020-12-23 19:10:25 -05:00
|
|
|
export const canShowActiveSubItems = (el) => {
|
2017-09-01 03:41:31 -04:00
|
|
|
if (el.classList.contains('active') && !isSidebarCollapsed()) {
|
2017-08-19 15:41:23 -04:00
|
|
|
return false;
|
2017-08-08 15:27:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
2017-08-04 10:32:44 -04:00
|
|
|
|
2019-12-23 07:08:18 -05:00
|
|
|
export const canShowSubItems = () => ['md', 'lg', 'xl'].includes(bp.getBreakpointSize());
|
2017-08-02 05:22:13 -04:00
|
|
|
|
2017-08-04 10:32:44 -04:00
|
|
|
export const getHideSubItemsInterval = () => {
|
2017-09-22 12:22:21 -04:00
|
|
|
if (!currentOpenMenu || !mousePos.length) return 0;
|
2017-08-04 10:32:44 -04:00
|
|
|
|
|
|
|
const currentMousePos = mousePos[mousePos.length - 1];
|
|
|
|
const prevMousePos = mousePos[0];
|
|
|
|
const currentMousePosY = currentMousePos.y;
|
|
|
|
const [menuTop, menuBottom] = menuCornerLocs;
|
|
|
|
|
2018-10-24 15:17:03 -04:00
|
|
|
if (currentMousePosY < menuTop.y || currentMousePosY > menuBottom.y) return 0;
|
2017-08-04 10:32:44 -04:00
|
|
|
|
2018-10-24 15:17:03 -04:00
|
|
|
if (
|
|
|
|
slope(prevMousePos, menuBottom) < slope(currentMousePos, menuBottom) &&
|
|
|
|
slope(prevMousePos, menuTop) > slope(currentMousePos, menuTop)
|
|
|
|
) {
|
2017-08-14 03:58:22 -04:00
|
|
|
return HIDE_INTERVAL_TIMEOUT;
|
2017-08-04 10:32:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
|
2017-07-19 05:36:08 -04:00
|
|
|
export const calculateTop = (boundingRect, outerHeight) => {
|
|
|
|
const windowHeight = window.innerHeight;
|
|
|
|
const bottomOverflow = windowHeight - (boundingRect.top + outerHeight);
|
|
|
|
|
2018-10-24 15:17:03 -04:00
|
|
|
return bottomOverflow < 0
|
|
|
|
? boundingRect.top - outerHeight + boundingRect.height
|
|
|
|
: boundingRect.top;
|
2017-07-19 05:36:08 -04:00
|
|
|
};
|
|
|
|
|
2020-12-23 19:10:25 -05:00
|
|
|
export const hideMenu = (el) => {
|
2017-08-04 10:32:44 -04:00
|
|
|
if (!el) return;
|
2017-08-01 03:49:03 -04:00
|
|
|
|
2017-08-04 10:32:44 -04:00
|
|
|
const parentEl = el.parentNode;
|
2017-08-01 03:49:03 -04:00
|
|
|
|
2018-08-27 10:21:02 -04:00
|
|
|
el.style.display = '';
|
|
|
|
el.style.transform = '';
|
2017-08-04 10:32:44 -04:00
|
|
|
el.classList.remove(IS_ABOVE_CLASS);
|
2021-05-31 11:11:12 -04:00
|
|
|
el.classList.remove('fly-out-list');
|
2017-08-04 10:32:44 -04:00
|
|
|
parentEl.classList.remove(IS_OVER_CLASS);
|
|
|
|
parentEl.classList.remove(IS_SHOWING_FLY_OUT_CLASS);
|
|
|
|
|
2017-08-14 03:58:22 -04:00
|
|
|
setOpenMenu();
|
2017-08-04 10:32:44 -04:00
|
|
|
};
|
2017-08-01 03:49:03 -04:00
|
|
|
|
2017-08-04 10:32:44 -04:00
|
|
|
export const moveSubItemsToPosition = (el, subItems) => {
|
2021-06-10 20:10:14 -04:00
|
|
|
const hasSubItems = subItems.parentNode.querySelector('.has-sub-items');
|
|
|
|
const header = subItems.querySelector('.fly-out-top-item');
|
2017-08-01 03:49:03 -04:00
|
|
|
const boundingRect = el.getBoundingClientRect();
|
2021-06-10 20:10:14 -04:00
|
|
|
const left = sidebar ? sidebar.offsetWidth : COLLAPSED_PANEL_WIDTH;
|
|
|
|
let top = calculateTop(boundingRect, subItems.offsetHeight);
|
2021-07-08 11:10:06 -04:00
|
|
|
const isAbove = top < boundingRect.top;
|
2021-07-06 08:07:43 -04:00
|
|
|
if (hasSubItems) {
|
2021-07-08 11:10:06 -04:00
|
|
|
top = isAbove ? top : top - header.offsetHeight;
|
2021-07-06 08:07:43 -04:00
|
|
|
} else {
|
2021-06-10 20:10:14 -04:00
|
|
|
top = boundingRect.top;
|
|
|
|
}
|
2017-08-01 03:49:03 -04:00
|
|
|
|
2017-08-02 21:19:43 -04:00
|
|
|
subItems.classList.add('fly-out-list');
|
2021-06-10 20:10:14 -04:00
|
|
|
subItems.style.transform = `translate3d(${left}px, ${Math.floor(top) - getHeaderHeight()}px, 0)`; // eslint-disable-line no-param-reassign
|
2017-08-04 10:32:44 -04:00
|
|
|
const subItemsRect = subItems.getBoundingClientRect();
|
|
|
|
|
|
|
|
menuCornerLocs = [
|
|
|
|
{
|
|
|
|
x: subItemsRect.left, // left position of the sub items
|
|
|
|
y: subItemsRect.top, // top position of the sub items
|
|
|
|
},
|
|
|
|
{
|
|
|
|
x: subItemsRect.left, // left position of the sub items
|
|
|
|
y: subItemsRect.top + subItemsRect.height, // bottom position of the sub items
|
|
|
|
},
|
|
|
|
];
|
2017-08-01 03:49:03 -04:00
|
|
|
|
|
|
|
if (isAbove) {
|
2017-08-04 10:32:44 -04:00
|
|
|
subItems.classList.add(IS_ABOVE_CLASS);
|
2017-08-01 03:49:03 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-12-23 19:10:25 -05:00
|
|
|
export const showSubLevelItems = (el) => {
|
2017-08-04 10:32:44 -04:00
|
|
|
const subItems = el.querySelector('.sidebar-sub-level-items');
|
2017-09-01 03:41:31 -04:00
|
|
|
const isIconOnly = subItems && subItems.classList.contains('is-fly-out-only');
|
2017-08-04 10:32:44 -04:00
|
|
|
|
|
|
|
if (!canShowSubItems() || !canShowActiveSubItems(el)) return;
|
|
|
|
|
|
|
|
el.classList.add(IS_OVER_CLASS);
|
|
|
|
|
2017-09-01 03:41:31 -04:00
|
|
|
if (!subItems || (!isSidebarCollapsed() && isIconOnly)) return;
|
2017-08-04 10:32:44 -04:00
|
|
|
|
|
|
|
subItems.style.display = 'block';
|
|
|
|
el.classList.add(IS_SHOWING_FLY_OUT_CLASS);
|
|
|
|
|
|
|
|
setOpenMenu(subItems);
|
|
|
|
moveSubItemsToPosition(el, subItems);
|
|
|
|
};
|
|
|
|
|
2018-01-23 10:19:56 -05:00
|
|
|
export const mouseEnterTopItems = (el, timeout = getHideSubItemsInterval()) => {
|
2017-08-04 10:32:44 -04:00
|
|
|
clearTimeout(timeoutId);
|
|
|
|
|
|
|
|
timeoutId = setTimeout(() => {
|
|
|
|
if (currentOpenMenu) hideMenu(currentOpenMenu);
|
|
|
|
|
|
|
|
showSubLevelItems(el);
|
2018-01-23 10:19:56 -05:00
|
|
|
}, timeout);
|
2017-08-04 10:32:44 -04:00
|
|
|
};
|
|
|
|
|
2020-12-23 19:10:25 -05:00
|
|
|
export const mouseLeaveTopItem = (el) => {
|
2017-08-02 03:46:25 -04:00
|
|
|
const subItems = el.querySelector('.sidebar-sub-level-items');
|
2017-08-01 03:49:03 -04:00
|
|
|
|
2018-10-24 15:17:03 -04:00
|
|
|
if (
|
|
|
|
!canShowSubItems() ||
|
|
|
|
!canShowActiveSubItems(el) ||
|
|
|
|
(subItems && subItems === currentOpenMenu)
|
|
|
|
)
|
|
|
|
return;
|
2017-08-01 03:49:03 -04:00
|
|
|
|
2017-08-04 10:32:44 -04:00
|
|
|
el.classList.remove(IS_OVER_CLASS);
|
|
|
|
};
|
|
|
|
|
2020-12-23 19:10:25 -05:00
|
|
|
export const documentMouseMove = (e) => {
|
2017-08-14 03:58:22 -04:00
|
|
|
mousePos.push({
|
|
|
|
x: e.clientX,
|
|
|
|
y: e.clientY,
|
|
|
|
});
|
2017-08-04 10:32:44 -04:00
|
|
|
|
|
|
|
if (mousePos.length > 6) mousePos.shift();
|
2017-08-01 03:49:03 -04:00
|
|
|
};
|
|
|
|
|
2020-12-23 19:10:25 -05:00
|
|
|
export const subItemsMouseLeave = (relatedTarget) => {
|
2017-08-30 06:48:33 -04:00
|
|
|
clearTimeout(timeoutId);
|
|
|
|
|
2017-09-12 05:09:18 -04:00
|
|
|
if (relatedTarget && !relatedTarget.closest(`.${IS_OVER_CLASS}`)) {
|
2017-08-30 06:48:33 -04:00
|
|
|
hideMenu(currentOpenMenu);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-07-19 05:36:08 -04:00
|
|
|
export default () => {
|
2017-08-19 15:41:23 -04:00
|
|
|
sidebar = document.querySelector('.nav-sidebar');
|
2017-08-08 06:25:36 -04:00
|
|
|
|
|
|
|
if (!sidebar) return;
|
|
|
|
|
2017-08-04 10:32:44 -04:00
|
|
|
const items = [...sidebar.querySelectorAll('.sidebar-top-level-items > li')];
|
|
|
|
|
2017-12-21 10:05:47 -05:00
|
|
|
const topItems = sidebar.querySelector('.sidebar-top-level-items');
|
|
|
|
if (topItems) {
|
|
|
|
sidebar.querySelector('.sidebar-top-level-items').addEventListener('mouseleave', () => {
|
|
|
|
clearTimeout(timeoutId);
|
|
|
|
|
|
|
|
timeoutId = setTimeout(() => {
|
|
|
|
if (currentOpenMenu) hideMenu(currentOpenMenu);
|
|
|
|
}, getHideSubItemsInterval());
|
|
|
|
});
|
|
|
|
}
|
2017-08-01 03:49:03 -04:00
|
|
|
|
2020-12-23 19:10:25 -05:00
|
|
|
items.forEach((el) => {
|
2017-08-04 10:32:44 -04:00
|
|
|
const subItems = el.querySelector('.sidebar-sub-level-items');
|
|
|
|
|
|
|
|
if (subItems) {
|
2020-12-23 19:10:25 -05:00
|
|
|
subItems.addEventListener('mouseleave', (e) => subItemsMouseLeave(e.relatedTarget));
|
2017-08-04 10:32:44 -04:00
|
|
|
}
|
|
|
|
|
2020-12-23 19:10:25 -05:00
|
|
|
el.addEventListener('mouseenter', (e) => mouseEnterTopItems(e.currentTarget));
|
|
|
|
el.addEventListener('mouseleave', (e) => mouseLeaveTopItem(e.currentTarget));
|
2017-08-01 03:49:03 -04:00
|
|
|
});
|
2017-08-04 10:32:44 -04:00
|
|
|
|
|
|
|
document.addEventListener('mousemove', documentMouseMove);
|
2017-07-19 05:36:08 -04:00
|
|
|
};
|