-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
114 lines (99 loc) · 2.59 KB
/
index.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
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
"use strict";
init();
function init() {
console.info(`Help:
- Press -> for next slide.
- Press <- for previous slide.
- Click for next slide.
- Right click for previous slide.
- Tap for next slide.
- Two finger tap for previous slide.`);
initQuerySlideIndex();
addEventListeners();
}
function initQuerySlideIndex() {
const url = new URL(location.href);
if (url.searchParams.get("slide")) {
let slideIndex = parseInt(url.searchParams.get("slide")) - 1;
if (slideIndex < 0) {
slideIndex = 0;
} else if (slideIndex > document.body.children.length - 1) {
slideIndex = document.body.children.length - 1;
}
hideSlide(document.body.children[0]);
showSlide(document.body.children[slideIndex]);
} else {
replaceQuerySlideIndex(1);
}
}
function next() {
const visibleSlide = document.querySelector(".slide-visible");
hideSlide(visibleSlide);
if (visibleSlide.nextElementSibling) {
showSlide(visibleSlide.nextElementSibling);
} else {
showSlide(visibleSlide.parentElement.firstElementChild);
}
}
function prev() {
const visibleSlide = document.querySelector(".slide-visible");
hideSlide(visibleSlide);
if (visibleSlide.previousElementSibling) {
showSlide(visibleSlide.previousElementSibling);
} else {
showSlide(visibleSlide.parentElement.lastElementChild);
}
}
function showSlide(slide) {
slide.classList.add("slide-visible");
setQuerySlideIndex(getElementIndex(slide));
}
function hideSlide(slide) {
slide.classList.remove("slide-visible");
}
function setQuerySlideIndex(index) {
const url = new URL(location.href);
url.searchParams.set("slide", index);
history.pushState({}, "", url);
}
function replaceQuerySlideIndex(index) {
const url = new URL(location.href);
if (url.searchParams.get("slide") !== `${index}`) {
url.searchParams.set("slide", index);
history.replaceState({}, "", url);
}
}
function getElementIndex(el) {
var index = 0;
while ((el = el.previousElementSibling)) {
index++;
}
console.info(index);
return index + 1;
}
function addEventListeners() {
document.addEventListener("keydown", e => {
if (e.key === "ArrowRight") {
next();
} else if (e.key === "ArrowLeft") {
prev();
}
});
document.addEventListener("click", () => {
next();
});
let doubleTouch;
document.addEventListener("touchstart", e => {
doubleTouch = e.touches.length > 1;
});
document.addEventListener("touchend", e => {
if (doubleTouch) {
prev();
doubleTouch = false;
}
});
document.addEventListener("contextmenu", e => {
e.preventDefault();
prev();
});
}