Skip to content
Snippets Groups Projects
Unverified Commit 48ec7025 authored by Phil Hughes's avatar Phil Hughes
Browse files

reverted the JS timeout code

improved specs so that they pass
parent 20bfc4f6
No related branches found
No related tags found
No related merge requests found
let hideTimeoutInterval = 0;
let hideTimeout;
let subitems;
export const getHideTimeoutInterval = () => hideTimeoutInterval;
export const hideAllSubItems = () => {
subitems.forEach((el) => {
el.parentNode.classList.remove('is-over');
el.style.display = 'none'; // eslint-disable-line no-param-reassign
});
};
export const calculateTop = (boundingRect, outerHeight) => { export const calculateTop = (boundingRect, outerHeight) => {
const windowHeight = window.innerHeight; const windowHeight = window.innerHeight;
const bottomOverflow = windowHeight - (boundingRect.top + outerHeight); const bottomOverflow = windowHeight - (boundingRect.top + outerHeight);
Loading
@@ -24,14 +11,8 @@ export const showSubLevelItems = (el) => {
Loading
@@ -24,14 +11,8 @@ export const showSubLevelItems = (el) => {
   
if (!$subitems) return; if (!$subitems) return;
   
hideAllSubItems(); $subitems.style.display = 'block';
el.classList.add('is-over');
if (el.classList.contains('is-over')) {
clearTimeout(hideTimeout);
} else {
$subitems.style.display = 'block';
el.classList.add('is-over');
}
   
const boundingRect = el.getBoundingClientRect(); const boundingRect = el.getBoundingClientRect();
const top = calculateTop(boundingRect, $subitems.offsetHeight); const top = calculateTop(boundingRect, $subitems.offsetHeight);
Loading
@@ -46,37 +27,20 @@ export const showSubLevelItems = (el) => {
Loading
@@ -46,37 +27,20 @@ export const showSubLevelItems = (el) => {
   
export const hideSubLevelItems = (el) => { export const hideSubLevelItems = (el) => {
const $subitems = el.querySelector('.sidebar-sub-level-items'); const $subitems = el.querySelector('.sidebar-sub-level-items');
const hideFn = () => {
el.classList.remove('is-over');
$subitems.style.display = 'none';
$subitems.classList.remove('is-above');
   
hideTimeoutInterval = 0; if (!$subitems) return;
};
if ($subitems && hideTimeoutInterval) {
hideTimeout = setTimeout(hideFn, hideTimeoutInterval);
} else if ($subitems) {
hideFn();
}
};
   
export const setMouseOutTimeout = (el) => { el.classList.remove('is-over');
if (el.closest('.sidebar-sub-level-items')) { $subitems.style.display = 'none';
hideTimeoutInterval = 250; $subitems.classList.remove('is-above');
} else {
hideTimeoutInterval = 0;
}
}; };
   
export default () => { export default () => {
const items = [...document.querySelectorAll('.sidebar-top-level-items > li:not(.active)')]; const items = [...document.querySelectorAll('.sidebar-top-level-items > li:not(.active)')]
subitems = [...document.querySelectorAll('.sidebar-top-level-items > li:not(.active) .sidebar-sub-level-items')]; .filter(el => el.querySelector('.sidebar-sub-level-items'));
   
items.forEach((el) => { items.forEach((el) => {
el.addEventListener('mouseenter', e => showSubLevelItems(e.currentTarget)); el.addEventListener('mouseenter', e => showSubLevelItems(e.currentTarget));
el.addEventListener('mouseleave', e => hideSubLevelItems(e.currentTarget)); el.addEventListener('mouseleave', e => hideSubLevelItems(e.currentTarget));
}); });
subitems.forEach(el => el.addEventListener('mouseleave', e => setMouseOutTimeout(e.target)));
}; };
Loading
@@ -226,10 +226,10 @@ $new-sidebar-width: 220px;
Loading
@@ -226,10 +226,10 @@ $new-sidebar-width: 220px;
&::before { &::before {
content: ""; content: "";
position: absolute; position: absolute;
top: -20px; top: -30px;
bottom: -20px; bottom: -30px;
left: 0; left: 0;
right: -20px; right: -30px;
z-index: -1; z-index: -1;
} }
   
Loading
Loading
import { import {
calculateTop, calculateTop,
setMouseOutTimeout,
getHideTimeoutInterval,
hideSubLevelItems, hideSubLevelItems,
showSubLevelItems, showSubLevelItems,
} from '~/fly_out_nav'; } from '~/fly_out_nav';
Loading
@@ -41,26 +39,6 @@ describe('Fly out sidebar navigation', () => {
Loading
@@ -41,26 +39,6 @@ describe('Fly out sidebar navigation', () => {
}); });
}); });
   
describe('setMouseOutTimeout', () => {
it('sets hideTimeoutInterval to 150 when inside sub items', () => {
el.innerHTML = '<div class="sidebar-sub-level-items"><div class="js-test"></div></div>';
setMouseOutTimeout(el.querySelector('.js-test'));
expect(
getHideTimeoutInterval(),
).toBe(150);
});
it('resets hideTimeoutInterval when not inside sub items', () => {
setMouseOutTimeout(el);
expect(
getHideTimeoutInterval(),
).toBe(0);
});
});
describe('hideSubLevelItems', () => { describe('hideSubLevelItems', () => {
beforeEach(() => { beforeEach(() => {
el.innerHTML = '<div class="sidebar-sub-level-items"></div>'; el.innerHTML = '<div class="sidebar-sub-level-items"></div>';
Loading
@@ -142,16 +120,17 @@ describe('Fly out sidebar navigation', () => {
Loading
@@ -142,16 +120,17 @@ describe('Fly out sidebar navigation', () => {
   
it('sets is-above when element is above', () => { it('sets is-above when element is above', () => {
const subItems = el.querySelector('.sidebar-sub-level-items'); const subItems = el.querySelector('.sidebar-sub-level-items');
subItems.style.height = '5000px'; subItems.style.height = `${window.innerHeight + el.offsetHeight}px`;
subItems.style.position = 'absolute';
el.style.position = 'relative'; el.style.position = 'relative';
el.style.top = '1000px'; el.style.top = `${window.innerHeight - el.offsetHeight}px`;
   
spyOn(el.classList, 'add'); spyOn(subItems.classList, 'add');
   
showSubLevelItems(el); showSubLevelItems(el);
   
expect( expect(
el.classList.add, subItems.classList.add,
).toHaveBeenCalledWith('is-above'); ).toHaveBeenCalledWith('is-above');
}); });
}); });
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment