forked from inspire-js/inspire.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslideshow.js
More file actions
executable file
·313 lines (257 loc) · 7.97 KB
/
Copy pathslideshow.js
File metadata and controls
executable file
·313 lines (257 loc) · 7.97 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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/**
* CSSS javascript code
* @author Lea Verou (http://leaverou.me)
* @version 1.0
*/
(function(){
// Cache <title> element, we may need it for slides that don't have titles
var documentTitle = document.getElementsByTagName('title')[0].textContent;
window.SlideShow = function(container, slide) {
var me = this;
container = container || document.body;
// Current slide
this.slide = slide || 0;
// Current .delayed item in the slide
this.item = 0;
// Get the slide elements into an array
this.slides = Array.prototype.slice.apply(container.querySelectorAll('.slide'));
for(var i=0; i<this.slides.length; i++) {
var slide = this.slides[i]; // to speed up references
// Asign ids to slides that don't have one
if(!slide.id) {
slide.id = 'slide' + (i+1);
}
// Set data-title attribute to the title of the slide
if(!slide.title) {
// no title attribute, fetch title from heading(s)
var heading = slide.querySelector('hgroup') || slide.querySelector('h1,h2,h3,h4,h5,h6');
if(heading && heading.textContent.trim()) {
slide.setAttribute('data-title', heading.textContent);
}
}
else {
// The title attribute is set, use that
slide.setAttribute('data-title', slide.title);
slide.removeAttribute('title');
}
}
// If there's already a hash, update current slide number...
this.goto(location.hash.substr(1) || 0);
// ...and keep doing so every time the hash changes
this.onhashchange = function() {
me.goto(location.hash.substr(1) || 0);
};
window.addEventListener('hashchange', this.onhashchange, false);
// Adjust the font-size when the window is resized
window.addEventListener('resize', function() {
me.adjustFontSize();
}, false);
// In some browsers DOMContentLoaded is too early, so try again onload
window.addEventListener('load', function() {
me.adjustFontSize();
}, false);
/**
Keyboard navigation
Home : First slide
End : Last slide
Up/Right arrow : Next item/slide
Ctrl + Up/Right arrow : Next slide
Down/Left arrow : Previous item/slide
Ctrl + Down/Left arrow : Previous slide
Ctrl+G : Go to slide...
Ctrl+H : Show thumbnails and go to slide
(Shift instead of Ctrl works too)
*/
document.addEventListener('keyup', function(evt) {
if(evt.ctrlKey || evt.shiftKey) {
switch(evt.keyCode) {
case 71:
var slide = prompt('Which slide?');
me.goto(+slide? slide - 1 : slide);
break;
case 72:
if(document.body.classList.contains('show-thumbnails')) {
document.body.classList.remove('show-thumbnails');
}
else {
document.body.classList.add('show-thumbnails');
document.body.addEventListener('click', function(evt) {
var slide = evt.target;
while(slide && !slide.classList.contains('slide')) {
slide = slide.parentNode;
}
if(slide) {
me.goto(slide.id);
setTimeout(function() { me.adjustFontSize(); }, 1000); // for Opera
}
document.body.classList.remove('show-thumbnails');
}, false);
}
}
}
if(evt.target === document.body || evt.target === document.body.parentNode) {
if(evt.keyCode >= 35 && evt.keyCode <= 40) {
evt.preventDefault();
}
switch(evt.keyCode) {
case 35: // end
me.end();
break;
case 36: // home
me.start();
break;
case 37: // <-
case 38: // up arrow
me.previous(evt.ctrlKey || evt.shiftKey);
break;
case 39: // ->
case 40: // down arrow
me.next(evt.ctrlKey || evt.shiftKey);
break;
}
}
}, false);
}
SlideShow.prototype = {
start: function() {
this.goto(0);
},
end: function() {
this.goto(this.slides.length - 1);
},
/**
@param hard {Boolean} Whether to advance to the next slide (true) or
just the next step (which could very well be showing a list item)
*/
next: function(hard) {
if(!hard && this.items.length) {
// If there's no current, then just mark the first one as such
if(!this.item) {
this.items[this.item++].classList.add('current');
}
// Add .current to current item if it exists, otherwise advance to next slide
else if(this.item < this.items.length) {
classes = this.items[this.item - 1].classList; // to speed up lookups
classes.remove('current');
classes.add('displayed');
this.items[this.item++].classList.add('current');
}
else {
this.item = 0;
this.next(true);
}
}
else {
this.goto(this.slide + 1);
this.item = 0;
// Mark all items as not displayed, if there are any
if(this.items.length) {
for (var i=0; i<this.items.length; i++) {
if(this.items[i].classList) {
this.items[i].classList.remove('displayed');
this.items[i].classList.remove('current');
}
}
}
}
},
previous: function(hard) {
if(!hard && this.item > 0) {
var classes = this.items[this.item - 1].classList; // to speed up lookups
classes.remove('current');
if(this.item > 1) {
classes = this.items[--this.item - 1].classList;
classes.remove('displayed');
classes.add('current');
}
else {
this.item = 0;
}
}
else {
this.goto(this.slide - 1);
this.item = this.items.length;
// Mark all items as displayed, if there are any
if(this.items.length) {
for (var i=0; i<this.items.length; i++) {
if(this.items[i].classList) {
this.items[i].classList.add('displayed');
}
}
// Mark the last one as current
var lastItem = this.items[this.items.length - 1];
lastItem.classList.remove('displayed');
lastItem.classList.add('current');
}
}
},
/**
Go to an aribtary slide
@param which {String|Integer} Which slide (identifier or slide number)
*/
goto: function(which) {
var slide;
// We have to remove it to prevent multiple calls to goto messing up
// our current item (and there's no point either, so we save on performance)
window.removeEventListener('hashchange', this.onhashchange, false);
if(which + 0 === which && which in this.slides) { // Argument is a valid slide number
this.slide = which;
slide = this.slides[this.slide];
location.hash = '#' + slide.id;
}
else if(which + '' === which) { // Argument is a slide id
slide = document.getElementById(which);
if(slide) {
this.slide = this.slides.indexOf(slide);
location.hash = '#' + which;
}
}
if(slide) { // Slide actually changed, perform any other tasks needed
document.title = slide.getAttribute('data-title') || documentTitle;
this.adjustFontSize();
// Update items collection
this.items = this.slides[this.slide].querySelectorAll('.delayed');
this.item = 0;
}
// If you attach the listener immediately again then it will catch the event
// We have to do it asynchronously
var me = this;
setTimeout(function() {
window.addEventListener('hashchange', me.onhashchange, false);
}, 1000);
},
adjustFontSize: function() {
// Cache long lookup chains, for performance
var bodyStyle = document.body.style,
scrollRoot = document[document.documentElement.scrollHeight? 'documentElement' : 'body'],
innerHeight = window.innerHeight,
innerWidth = window.innerWidth;
// Clear previous styles
bodyStyle.fontSize = '';
if(document.body.classList.contains('show-thumbnails')) {
return;
}
for(
var percent = 100;
(scrollRoot.scrollHeight > innerHeight || scrollRoot.scrollWidth > innerWidth) && percent >= 35;
percent-=5
) {
bodyStyle.fontSize = percent + '%';
}
// Individual slide
var slide = this.slides[this.slide];
if(slide.clientHeight && slide.clientWidth) {
// Strange FF bug: scrollHeight doesn't work properly with overflow:hidden
slide.style.overflow = 'auto';
for(
;
(slide.scrollHeight > slide.clientHeight || slide.scrollWidth > slide.clientWidth) && percent >= 35;
percent--
) {
bodyStyle.fontSize = percent + '%';
}
slide.style.overflow = '';
}
}
};
})();