forked from javascript-tutorial/server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnavigationArrows.js
More file actions
executable file
·65 lines (51 loc) · 1.73 KB
/
navigationArrows.js
File metadata and controls
executable file
·65 lines (51 loc) · 1.73 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
// navigation starts to work right now
let onSwipe = require('client/onSwipe');
let ctrlOrAlt = ~navigator.userAgent.toLowerCase().indexOf("mac os x") ? 'ctrl' : 'alt';
function onKeyDown(event) {
// don't react on Ctrl-> <- if in text
if (document.activeElement) {
if (~['INPUT', 'TEXTAREA', 'SELECT'].indexOf(document.activeElement.tagName)) return;
}
if (!event[ctrlOrAlt + 'Key']) return;
let rel = null;
switch (event.keyCode) {
case 0x25:
rel = 'prev';
break;
case 0x27:
rel = 'next';
break;
default:
return;
}
let link = document.querySelector('link[rel="' + rel + '"]');
if (!link) return;
document.location = link.href;
event.preventDefault();
}
function showHotKeys() {
let keyDesc = ctrlOrAlt[0].toUpperCase() + ctrlOrAlt.slice(1);
let shortcut;
let next = document.querySelector('link[rel="next"]');
if (next) {
shortcut = document.querySelector('a[href="' + next.getAttribute('href') + '"] .page__nav-text-shortcut');
shortcut.innerHTML = keyDesc + ' + <span class="page__nav-text-arr">→</span>';
}
let prev = document.querySelector('link[rel="prev"]');
if (prev) {
shortcut = document.querySelector('a[href="' + prev.getAttribute('href') + '"] .page__nav-text-shortcut');
shortcut.innerHTML = keyDesc + ' + <span class="page__nav-text-arr">←</span>';
}
}
onSwipe(document, {
onRight: function() {
let link = document.querySelector('link[rel="prev"]');
if (link) document.location = link.href;
},
onLeft: function() {
let link = document.querySelector('link[rel="next"]');
if (link) document.location = link.href;
}
});
document.addEventListener('keydown', onKeyDown);
document.addEventListener('DOMContentLoaded', showHotKeys);