-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbase.js
51 lines (37 loc) · 1.57 KB
/
base.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
(function() {
var isMobileSafari = navigator.userAgent.toLowerCase().match(/(iphone|ipod|ipad)/);
var openMenu = null;
document.addEventListener('click', dropdown);
if (isMobileSafari) {
document.addEventListener('touchstart', dropdown);
}
function dropdown(event) {
event = event || window.event;
var target = event.target || event.srcElement;
// Check if menu button click.
var isButtonClick = target.matches('.dropdown > button')
|| target.parentNode.matches && target.parentNode.matches('.navbar > button');
if (event.type === 'click' && isButtonClick) {
var menu = target.parentNode.querySelector('.dropdown-menu')
|| target.parentNode.parentNode.querySelector('.nav-collapse');
// Close other open menus first.
if (openMenu && openMenu !== menu) {
toggleDropdownMenu(openMenu);
}
// Open/Close clicked menu.
toggleDropdownMenu(menu);
openMenu = openMenu !== menu ? menu : null;
}
// Close open menu on menu item or background click.
// On mobile Safari a background click must be handled with the touchstart event.
else if (event.type === 'click' && openMenu
|| openMenu && isMobileSafari && !isButtonClick && !openMenu.contains(target)) {
toggleDropdownMenu(openMenu);
openMenu = null;
}
return true;
}
function toggleDropdownMenu(menu) {
menu.classList.toggle('show');
}
})();