docbrown/app/assets/javascripts/initializers/initializeTouchDevice.js
ludwiczakpawel 73e560f7a2
Updating navigation (especially mobile) (#10424)
* .

* .

* .

* .

* .

* specs

* specs

* .

* .

* merge

* moving cheese around

* layout

* .

* merge

* Fixed invalid selector for search.

* Fixed issue with URL not updating after a search.

* do things better

* kinda merge

* whoops

* whoops

* .

* .

* im a backend developer

* tooltips 1.0.1

* tooltips 1.0.1

* fix

* specs

* .

* reduce the spacing

* schema drop

* reverts

* revert

* reverts

* revert

* drop unused nav

* drop unused nav

* drop unused nav

* drop unused nav

* typos

* paths fixes

* speeding up JS

* js

* search

* Reworked initializeTopBarIcons.js

* Reworked initializeNavigation.js

* Small refactor and updated exported function API docs.

* Fixed a bug I created when refactoring the top navigation icons.

* sidebar nav links

* spec

* Fixed issues with logged out experience for top nav icons.

* Small refactor of top nav icons.

* Small refactor and added some API docs.

* Added @testing-library/dom

* Added tests for top navigation utilities.

* spec

* spec

Co-authored-by: Nick Taylor <nick@dev.to>
2020-12-08 15:59:21 +01:00

57 lines
1.7 KiB
JavaScript

function getById(className) {
return document.getElementById(className);
}
function getClassList(className) {
return getById(className).classList;
}
function blur(event, className) {
setTimeout(() => {
if (document.activeElement !== getById(className)) {
getClassList('crayons-header__menu').remove('showing');
}
}, 10);
}
function removeShowingMenu() {
getClassList('crayons-header__menu').remove('showing');
setTimeout(() => {
getClassList('crayons-header__menu').remove('showing');
}, 5);
setTimeout(() => {
getClassList('crayons-header__menu').remove('showing');
}, 150);
}
function toggleMenu() {
getClassList('crayons-header__menu').toggle('showing');
}
function initializeTouchDevice() {
var isTouchDevice = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini|DEV-Native-ios/i.test(
navigator.userAgent,
);
if (navigator.userAgent === 'DEV-Native-ios') {
document.body.classList.add('dev-ios-native-body');
}
if (document.querySelector('.crayons-header__menu')) {
setTimeout(() => {
removeShowingMenu();
if (isTouchDevice) {
// Use a named function instead of anonymous so duplicate event handlers are discarded
getById('navigation-butt').addEventListener('click', toggleMenu);
} else {
getClassList('crayons-header__menu').add('desktop');
getById('navigation-butt').addEventListener('focus', (e) =>
getClassList('crayons-header__menu').add('showing'),
);
getById('last-nav-link').addEventListener('blur', (e) =>
blur(e, 'second-last-nav-link'),
);
getById('navigation-butt').addEventListener('blur', (e) =>
blur(e, 'first-nav-link'),
);
}
}, 10);
}
}