fly_out_nav.js 5.45 KB
Newer Older
1
import bp from './breakpoints';
2

Phil Hughes's avatar
Phil Hughes committed
3
const HIDE_INTERVAL_TIMEOUT = 300;
4 5 6 7 8 9
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;
Phil Hughes's avatar
Phil Hughes committed
10
let sidebar;
11 12 13

export const mousePos = [];

Phil Hughes's avatar
Phil Hughes committed
14
export const setSidebar = (el) => { sidebar = el; };
15
export const getOpenMenu = () => currentOpenMenu;
Phil Hughes's avatar
Phil Hughes committed
16
export const setOpenMenu = (menu = null) => { currentOpenMenu = menu; };
17 18 19

export const slope = (a, b) => (b.y - a.y) / (b.x - a.x);

20 21 22 23
let headerHeight = 50;

export const getHeaderHeight = () => headerHeight;

24
export const isSidebarCollapsed = () => sidebar && sidebar.classList.contains('sidebar-collapsed-desktop');
25

26
export const canShowActiveSubItems = (el) => {
27
  if (el.classList.contains('active') && !isSidebarCollapsed()) {
Phil Hughes's avatar
Phil Hughes committed
28
    return false;
29 30 31 32
  }

  return true;
};
33

34
export const canShowSubItems = () => bp.getBreakpointSize() === 'sm' || bp.getBreakpointSize() === 'md' || bp.getBreakpointSize() === 'lg';
35

36
export const getHideSubItemsInterval = () => {
Phil Hughes's avatar
Phil Hughes committed
37
  if (!currentOpenMenu || !mousePos.length) return 0;
38 39 40 41 42 43 44 45 46 47 48

  const currentMousePos = mousePos[mousePos.length - 1];
  const prevMousePos = mousePos[0];
  const currentMousePosY = currentMousePos.y;
  const [menuTop, menuBottom] = menuCornerLocs;

  if (currentMousePosY < menuTop.y ||
      currentMousePosY > menuBottom.y) return 0;

  if (slope(prevMousePos, menuBottom) < slope(currentMousePos, menuBottom) &&
    slope(prevMousePos, menuTop) > slope(currentMousePos, menuTop)) {
Phil Hughes's avatar
Phil Hughes committed
49
    return HIDE_INTERVAL_TIMEOUT;
50 51 52 53 54
  }

  return 0;
};

55 56 57 58
export const calculateTop = (boundingRect, outerHeight) => {
  const windowHeight = window.innerHeight;
  const bottomOverflow = windowHeight - (boundingRect.top + outerHeight);

59 60
  return bottomOverflow < 0 ? (boundingRect.top - outerHeight) + boundingRect.height :
    boundingRect.top;
61 62
};

63 64
export const hideMenu = (el) => {
  if (!el) return;
65

66
  const parentEl = el.parentNode;
67

68 69 70 71 72 73
  el.style.display = ''; // eslint-disable-line no-param-reassign
  el.style.transform = ''; // eslint-disable-line no-param-reassign
  el.classList.remove(IS_ABOVE_CLASS);
  parentEl.classList.remove(IS_OVER_CLASS);
  parentEl.classList.remove(IS_SHOWING_FLY_OUT_CLASS);

Phil Hughes's avatar
Phil Hughes committed
74
  setOpenMenu();
75
};
76

77
export const moveSubItemsToPosition = (el, subItems) => {
78
  const boundingRect = el.getBoundingClientRect();
Phil Hughes's avatar
Phil Hughes committed
79
  const top = calculateTop(boundingRect, subItems.offsetHeight);
80
  const left = sidebar ? sidebar.offsetWidth : 50;
81 82
  const isAbove = top < boundingRect.top;

83
  subItems.classList.add('fly-out-list');
84
  subItems.style.transform = `translate3d(${left}px, ${Math.floor(top) - headerHeight}px, 0)`; // eslint-disable-line no-param-reassign
85 86 87 88 89 90 91 92 93 94 95 96 97

  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
    },
  ];
98 99

  if (isAbove) {
100
    subItems.classList.add(IS_ABOVE_CLASS);
101 102 103
  }
};

104 105
export const showSubLevelItems = (el) => {
  const subItems = el.querySelector('.sidebar-sub-level-items');
106
  const isIconOnly = subItems && subItems.classList.contains('is-fly-out-only');
107 108 109 110 111

  if (!canShowSubItems() || !canShowActiveSubItems(el)) return;

  el.classList.add(IS_OVER_CLASS);

112
  if (!subItems || (!isSidebarCollapsed() && isIconOnly)) return;
113 114 115 116 117 118 119 120

  subItems.style.display = 'block';
  el.classList.add(IS_SHOWING_FLY_OUT_CLASS);

  setOpenMenu(subItems);
  moveSubItemsToPosition(el, subItems);
};

121
export const mouseEnterTopItems = (el, timeout = getHideSubItemsInterval()) => {
122 123 124 125 126 127
  clearTimeout(timeoutId);

  timeoutId = setTimeout(() => {
    if (currentOpenMenu) hideMenu(currentOpenMenu);

    showSubLevelItems(el);
128
  }, timeout);
129 130 131
};

export const mouseLeaveTopItem = (el) => {
Phil Hughes's avatar
Phil Hughes committed
132
  const subItems = el.querySelector('.sidebar-sub-level-items');
133

134 135
  if (!canShowSubItems() || !canShowActiveSubItems(el) ||
      (subItems && subItems === currentOpenMenu)) return;
136

137 138 139 140
  el.classList.remove(IS_OVER_CLASS);
};

export const documentMouseMove = (e) => {
Phil Hughes's avatar
Phil Hughes committed
141 142 143 144
  mousePos.push({
    x: e.clientX,
    y: e.clientY,
  });
145 146

  if (mousePos.length > 6) mousePos.shift();
147 148
};

149 150 151
export const subItemsMouseLeave = (relatedTarget) => {
  clearTimeout(timeoutId);

Phil Hughes's avatar
Phil Hughes committed
152
  if (relatedTarget && !relatedTarget.closest(`.${IS_OVER_CLASS}`)) {
153 154 155 156
    hideMenu(currentOpenMenu);
  }
};

157
export default () => {
Phil Hughes's avatar
Phil Hughes committed
158
  sidebar = document.querySelector('.nav-sidebar');
159 160 161

  if (!sidebar) return;

162 163
  const items = [...sidebar.querySelectorAll('.sidebar-top-level-items > li')];

164 165 166 167 168 169 170 171 172 173
  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());
    });
  }
174

175 176
  headerHeight = document.querySelector('.nav-sidebar').offsetTop;

177
  items.forEach((el) => {
178 179 180
    const subItems = el.querySelector('.sidebar-sub-level-items');

    if (subItems) {
181
      subItems.addEventListener('mouseleave', e => subItemsMouseLeave(e.relatedTarget));
182 183 184 185
    }

    el.addEventListener('mouseenter', e => mouseEnterTopItems(e.currentTarget));
    el.addEventListener('mouseleave', e => mouseLeaveTopItem(e.currentTarget));
186
  });
187 188

  document.addEventListener('mousemove', documentMouseMove);
189
};