-
Notifications
You must be signed in to change notification settings - Fork 0
/
slideshow.js
195 lines (151 loc) · 4.35 KB
/
slideshow.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Keynote Extractor - Advanced output style
// Written in Vanilla Javascript
// @Johan Ronsse / Keynote Extractor 2017
// @1.2.0 Production
var showNotes = true;
var showShortcuts = false;
var slideNumber = 1;
var $shortcutsToggle = document.getElementById('show-shortcuts');
var $shortcuts = document.getElementById('shortcuts');
var $slide = document.querySelectorAll(".slides .slide")
var $slideNotes = document.querySelectorAll(".slides .slide .slide-notes")
var $slidePos = document.getElementById('slide-pos-current');
var $slideTotal = document.getElementById('slides-total');
var $btnPrevSlide = document.getElementById('btn-prev-slide');
var $btnNextSlide = document.getElementById('btn-next-slide');
function changeShowShortcutsState() {
if (showShortcuts) {
showShortcuts = false;
} else {
showShortcuts = true;
}
}
function countSlides() {
return $slide.length
}
function renderShortcutsState() {
if (showShortcuts) {
$shortcuts.style.display = 'block';
} else {
$shortcuts.style.display = 'none';
}
}
function renderNotesState() {
if (showNotes) {
for (var i = 0; i < $slideNotes.length; i++) {
$slideNotes[i].style.display = 'block';
}
document.body.classList.add('js-notes-active');
} else {
for (var i = 0; i < $slideNotes.length; i++) {
$slideNotes[i].style.display = 'none';
}
document.body.classList.remove('js-notes-active');
}
}
function hideSlides() {
for (var i = 0; i < $slide.length; i++) {
$slide[i].style.display = 'none';
}
}
function renderSlide(slideNumber, withHistory) {
if (slideNumber) {
hideSlides();
whichSlide = slideNumber - 1;
$slide[whichSlide].style.display = 'flex';
updateCounter(slideNumber);
if (withHistory) {
pushHistoryState(slideNumber);
}
} else {
throw "Please define a slide number to render"
}
}
function getHash() {
return parseInt(window.location.hash.substr(1));
}
function setHash(slideNumber) {
if (slideNumber) {
window.location.hash = slideNumber;
} else {
throw "Please provide an argument to setHash"
}
}
function renderUIState() {
renderShortcutsState();
renderNotesState();
}
function updateCounter(slideNumber) {
$slidePos.innerHTML = slideNumber;
}
function pushHistoryState(slideNumber) {
currentUrl = window.location.href.substr(0, window.location.href.indexOf('#'))
history.pushState({ lastSlideNumber: slideNumber },'', currentUrl + '#' + slideNumber);
}
function previousSlide() {
if (slideNumber != 1) {
slideNumber = slideNumber - 1;
renderSlide(slideNumber, true);
}
}
function nextSlide() {
if (slideNumber != countSlides()) {
slideNumber = slideNumber + 1;
renderSlide(slideNumber, true);
}
}
function init() {
renderUIState();
if (!window.location.hash) {
setHash("1");
slideNumber = getHash();
renderSlide(slideNumber, true);
} else {
if (window.location.hash == "#0") {
setHash("1");
slideNumber = getHash();
renderSlide(slideNumber, true);
} else {
slideNumber = getHash();
renderSlide(slideNumber, true);
}
}
$slideTotal.innerHTML = countSlides();
}
document.addEventListener("DOMContentLoaded", function(event) {
$btnPrevSlide.onclick = function() {
previousSlide();
}
$btnNextSlide.onclick = function() {
nextSlide();
}
$shortcutsToggle.onclick = function() {
changeShowShortcutsState();
renderShortcutsState();
};
document.body.onkeyup = function(e) {
if (e.keyCode === 191) {
changeShowShortcutsState();
renderShortcutsState();
}
if (e.keyCode === 78) {
if (showNotes) {
showNotes = false;
} else {
showNotes = true;
}
renderNotesState()
}
if (e.keyCode === 37) {
previousSlide()
}
if (e.keyCode === 39) {
nextSlide()
}
};
window.addEventListener('popstate', function(e) {
var histState = e.state;
histState && renderSlide(histState.lastSlideNumber);
});
init();
});