forked from jdan/cleaver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnavigation.js
135 lines (112 loc) · 3.29 KB
/
navigation.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/**
* Takes the last slide and places it at the front.
*/
function goBack() {
var wrapper = document.querySelector('#wrapper');
var lastSlide = wrapper.lastChild;
while (lastSlide != null && lastSlide.nodeType !== 1) {
lastSlide = lastSlide.previousSibling;
}
wrapper.removeChild(lastSlide);
wrapper.insertBefore(lastSlide, wrapper.firstChild);
setCurrentProgress();
updateURL();
}
/**
* Takes the first slide and places it at the end.
*/
function goForward() {
var wrapper = document.querySelector('#wrapper');
var firstSlide = wrapper.firstChild;
while (firstSlide != null && firstSlide.nodeType !== 1) {
firstSlide = firstSlide.nextSibling;
}
wrapper.removeChild(firstSlide);
wrapper.appendChild(firstSlide);
setCurrentProgress();
updateURL();
}
/**
* Updates the current URL to include a hashtag of the current page number.
*/
function updateURL() {
window.history.replaceState({} , null, '#' + currentPage());
}
/**
* Returns the current page number of the presentation.
*/
function currentPage() {
return document.querySelector('#wrapper .slide').dataset.page;
}
/**
* Returns a NodeList of each .slide element.
*/
function allSlides() {
return document.querySelectorAll('#wrapper .slide');
}
/**
* Give each slide a "page" data attribute.
*/
function setPageNumbers() {
var wrapper = document.querySelector('#wrapper');
var pages = wrapper.querySelectorAll('section');
var page;
for (var i = 0; i < pages.length; ++i) {
page = pages[i];
page.dataset.page = i;
}
}
/**
* Set the current progress indicator.
*/
function setCurrentProgress() {
var wrapper = document.querySelector('#wrapper');
var progressBar = document.querySelector('.progress-bar');
if (progressBar !== null) {
var pagesNumber = wrapper.querySelectorAll('section').length;
var currentNumber = parseInt(currentPage());
var currentPercent = pagesNumber === 1 ? 100 : 100 * currentNumber / (pagesNumber - 1);
progressBar.style.width = currentPercent.toString() + '%';
}
}
/**
* Go to the specified page of content.
*/
function goToPage(page) {
// Try to find the target slide.
var targetSlide = document.querySelector('#wrapper .slide[data-page="' + page + '"]');
// If it actually exists, go forward until we find it.
if (targetSlide) {
var numSlides = allSlides().length;
for (var i = 0; currentPage() != page && i < numSlides; i++) {
goForward();
}
}
}
window.onload = function () {
// Give each slide a "page" data attribute.
setPageNumbers();
// If the location hash specifies a page number, go to it.
var page = window.location.hash.slice(1);
if (page) goToPage(page);
document.onkeydown = function (e) {
var kc = e.keyCode;
// left, down, H, J, backspace - BACK
// up, right, K, L, space, enter - FORWARD
if (kc == 37 || kc == 40 || kc == 8 || kc == 72 || kc == 74) {
goBack();
} else if (kc == 38 || kc == 39 || kc == 13 || kc == 32 || kc == 75 || kc == 76) {
goForward();
}
}
if (document.querySelector('.next') && document.querySelector('.prev')) {
document.querySelector('.next').onclick = function (e) {
e.preventDefault();
goForward();
}
document.querySelector('.prev').onclick = function (e) {
e.preventDefault();
goBack();
}
}
}