forked from hestiacp/hestiacp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnavigation.js
More file actions
120 lines (100 loc) · 3.01 KB
/
navigation.js
File metadata and controls
120 lines (100 loc) · 3.01 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Page navigation methods called by shortcuts
const state = {
active_menu: 1,
menu_selector: '.main-menu-item',
menu_active_selector: '.active',
};
export function moveFocusLeft() {
moveFocusLeftRight('left');
}
export function moveFocusRight() {
moveFocusLeftRight('right');
}
export function moveFocusDown() {
moveFocusUpDown('down');
}
export function moveFocusUp() {
moveFocusUpDown('up');
}
// Navigate to whatever item has been selected in the UI by other shortcuts
export function enterFocused() {
const activeMainMenuItem = document.querySelector(`${state.menu_selector}.focus a`);
if (activeMainMenuItem) {
location.href = activeMainMenuItem.getAttribute('href');
return;
}
const activeUnit = document.querySelector(
'.js-unit.focus .units-table-row-actions .shortcut-enter a',
);
if (activeUnit) {
location.href = activeUnit.getAttribute('href');
}
}
// Either click or follow a link based on the data-key-action attribute
export function executeShortcut(elm) {
const action = elm.dataset.keyAction;
if (action === 'js') {
return elm.querySelector('.data-controls').click();
}
if (action === 'href') {
location.href = elm.querySelector('a').getAttribute('href');
}
}
function moveFocusLeftRight(direction) {
const menuSelector = state.menu_selector;
const activeSelector = state.menu_active_selector;
const menuItems = Array.from(document.querySelectorAll(menuSelector));
const currentFocused = document.querySelector(`${menuSelector}.focus`);
const currentActive = document.querySelector(`${menuSelector}${activeSelector}`);
let index = menuItems.indexOf(currentFocused);
if (index === -1) {
index = menuItems.indexOf(currentActive);
}
menuItems.forEach((item) => item.classList.remove('focus'));
if (direction === 'left') {
if (index > 0) {
menuItems[index - 1].classList.add('focus');
} else {
switchMenu('last');
}
} else if (direction === 'right') {
if (index < menuItems.length - 1) {
menuItems[index + 1].classList.add('focus');
} else {
switchMenu('first');
}
}
}
function moveFocusUpDown(direction) {
const units = Array.from(document.querySelectorAll('.js-unit'));
const currentFocused = document.querySelector('.js-unit.focus');
let index = units.indexOf(currentFocused);
if (index === -1) {
index = 0;
}
if (direction === 'up' && index > 0) {
index--;
} else if (direction === 'down' && index < units.length - 1) {
index++;
} else {
return;
}
if (currentFocused) {
currentFocused.classList.remove('focus');
}
units[index].classList.add('focus');
window.scrollTo({
top: units[index].getBoundingClientRect().top - 200 + window.scrollY,
behavior: 'smooth',
});
}
function switchMenu(position = 'first') {
if (state.active_menu === 0) {
state.active_menu = 1;
state.menu_selector = '.main-menu-item';
state.menu_active_selector = '.active';
const menuItems = document.querySelectorAll(state.menu_selector);
const focusedIndex = position === 'first' ? 0 : menuItems.length - 1;
menuItems[focusedIndex].classList.add('focus');
}
}