Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
1837 views
1
/*!
2
* reveal.js
3
* http://revealjs.com
4
* MIT licensed
5
*
6
* Copyright (C) 2018 Hakim El Hattab, http://hakim.se
7
*/
8
(function( root, factory ) {
9
if( typeof define === 'function' && define.amd ) {
10
// AMD. Register as an anonymous module.
11
define( function() {
12
root.Reveal = factory();
13
return root.Reveal;
14
} );
15
} else if( typeof exports === 'object' ) {
16
// Node. Does not work with strict CommonJS.
17
module.exports = factory();
18
} else {
19
// Browser globals.
20
root.Reveal = factory();
21
}
22
}( this, function() {
23
24
'use strict';
25
26
var Reveal;
27
28
// The reveal.js version
29
var VERSION = '3.7.0';
30
31
var SLIDES_SELECTOR = '.slides section',
32
HORIZONTAL_SLIDES_SELECTOR = '.slides>section',
33
VERTICAL_SLIDES_SELECTOR = '.slides>section.present>section',
34
HOME_SLIDE_SELECTOR = '.slides>section:first-of-type',
35
UA = navigator.userAgent,
36
37
// Configuration defaults, can be overridden at initialization time
38
config = {
39
40
// The "normal" size of the presentation, aspect ratio will be preserved
41
// when the presentation is scaled to fit different resolutions
42
width: 960,
43
height: 700,
44
45
// Factor of the display size that should remain empty around the content
46
margin: 0.04,
47
48
// Bounds for smallest/largest possible scale to apply to content
49
minScale: 0.2,
50
maxScale: 2.0,
51
52
// Display presentation control arrows
53
controls: true,
54
55
// Help the user learn the controls by providing hints, for example by
56
// bouncing the down arrow when they first encounter a vertical slide
57
controlsTutorial: true,
58
59
// Determines where controls appear, "edges" or "bottom-right"
60
controlsLayout: 'bottom-right',
61
62
// Visibility rule for backwards navigation arrows; "faded", "hidden"
63
// or "visible"
64
controlsBackArrows: 'faded',
65
66
// Display a presentation progress bar
67
progress: true,
68
69
// Display the page number of the current slide
70
slideNumber: false,
71
72
// Use 1 based indexing for # links to match slide number (default is zero
73
// based)
74
hashOneBasedIndex: false,
75
76
// Determine which displays to show the slide number on
77
showSlideNumber: 'all',
78
79
// Push each slide change to the browser history
80
history: false,
81
82
// Enable keyboard shortcuts for navigation
83
keyboard: true,
84
85
// Optional function that blocks keyboard events when retuning false
86
keyboardCondition: null,
87
88
// Enable the slide overview mode
89
overview: true,
90
91
// Disables the default reveal.js slide layout so that you can use
92
// custom CSS layout
93
disableLayout: false,
94
95
// Vertical centering of slides
96
center: true,
97
98
// Enables touch navigation on devices with touch input
99
touch: true,
100
101
// Loop the presentation
102
loop: false,
103
104
// Change the presentation direction to be RTL
105
rtl: false,
106
107
// Randomizes the order of slides each time the presentation loads
108
shuffle: false,
109
110
// Turns fragments on and off globally
111
fragments: true,
112
113
// Flags whether to include the current fragment in the URL,
114
// so that reloading brings you to the same fragment position
115
fragmentInURL: false,
116
117
// Flags if the presentation is running in an embedded mode,
118
// i.e. contained within a limited portion of the screen
119
embedded: false,
120
121
// Flags if we should show a help overlay when the question-mark
122
// key is pressed
123
help: true,
124
125
// Flags if it should be possible to pause the presentation (blackout)
126
pause: true,
127
128
// Flags if speaker notes should be visible to all viewers
129
showNotes: false,
130
131
// Global override for autolaying embedded media (video/audio/iframe)
132
// - null: Media will only autoplay if data-autoplay is present
133
// - true: All media will autoplay, regardless of individual setting
134
// - false: No media will autoplay, regardless of individual setting
135
autoPlayMedia: null,
136
137
// Controls automatic progression to the next slide
138
// - 0: Auto-sliding only happens if the data-autoslide HTML attribute
139
// is present on the current slide or fragment
140
// - 1+: All slides will progress automatically at the given interval
141
// - false: No auto-sliding, even if data-autoslide is present
142
autoSlide: 0,
143
144
// Stop auto-sliding after user input
145
autoSlideStoppable: true,
146
147
// Use this method for navigation when auto-sliding (defaults to navigateNext)
148
autoSlideMethod: null,
149
150
// Specify the average time in seconds that you think you will spend
151
// presenting each slide. This is used to show a pacing timer in the
152
// speaker view
153
defaultTiming: null,
154
155
// Enable slide navigation via mouse wheel
156
mouseWheel: false,
157
158
// Apply a 3D roll to links on hover
159
rollingLinks: false,
160
161
// Hides the address bar on mobile devices
162
hideAddressBar: true,
163
164
// Opens links in an iframe preview overlay
165
// Add `data-preview-link` and `data-preview-link="false"` to customise each link
166
// individually
167
previewLinks: false,
168
169
// Exposes the reveal.js API through window.postMessage
170
postMessage: true,
171
172
// Dispatches all reveal.js events to the parent window through postMessage
173
postMessageEvents: false,
174
175
// Focuses body when page changes visibility to ensure keyboard shortcuts work
176
focusBodyOnPageVisibilityChange: true,
177
178
// Transition style
179
transition: 'slide', // none/fade/slide/convex/concave/zoom
180
181
// Transition speed
182
transitionSpeed: 'default', // default/fast/slow
183
184
// Transition style for full page slide backgrounds
185
backgroundTransition: 'fade', // none/fade/slide/convex/concave/zoom
186
187
// Parallax background image
188
parallaxBackgroundImage: '', // CSS syntax, e.g. "a.jpg"
189
190
// Parallax background size
191
parallaxBackgroundSize: '', // CSS syntax, e.g. "3000px 2000px"
192
193
// Parallax background repeat
194
parallaxBackgroundRepeat: '', // repeat/repeat-x/repeat-y/no-repeat/initial/inherit
195
196
// Parallax background position
197
parallaxBackgroundPosition: '', // CSS syntax, e.g. "top left"
198
199
// Amount of pixels to move the parallax background per slide step
200
parallaxBackgroundHorizontal: null,
201
parallaxBackgroundVertical: null,
202
203
// The maximum number of pages a single slide can expand onto when printing
204
// to PDF, unlimited by default
205
pdfMaxPagesPerSlide: Number.POSITIVE_INFINITY,
206
207
// Prints each fragment on a separate slide
208
pdfSeparateFragments: true,
209
210
// Offset used to reduce the height of content within exported PDF pages.
211
// This exists to account for environment differences based on how you
212
// print to PDF. CLI printing options, like phantomjs and wkpdf, can end
213
// on precisely the total height of the document whereas in-browser
214
// printing has to end one pixel before.
215
pdfPageHeightOffset: -1,
216
217
// Number of slides away from the current that are visible
218
viewDistance: 3,
219
220
// The display mode that will be used to show slides
221
display: 'block',
222
223
// Script dependencies to load
224
dependencies: []
225
226
},
227
228
// Flags if Reveal.initialize() has been called
229
initialized = false,
230
231
// Flags if reveal.js is loaded (has dispatched the 'ready' event)
232
loaded = false,
233
234
// Flags if the overview mode is currently active
235
overview = false,
236
237
// Holds the dimensions of our overview slides, including margins
238
overviewSlideWidth = null,
239
overviewSlideHeight = null,
240
241
// The horizontal and vertical index of the currently active slide
242
indexh,
243
indexv,
244
245
// The previous and current slide HTML elements
246
previousSlide,
247
currentSlide,
248
249
previousBackground,
250
251
// Remember which directions that the user has navigated towards
252
hasNavigatedRight = false,
253
hasNavigatedDown = false,
254
255
// Slides may hold a data-state attribute which we pick up and apply
256
// as a class to the body. This list contains the combined state of
257
// all current slides.
258
state = [],
259
260
// The current scale of the presentation (see width/height config)
261
scale = 1,
262
263
// CSS transform that is currently applied to the slides container,
264
// split into two groups
265
slidesTransform = { layout: '', overview: '' },
266
267
// Cached references to DOM elements
268
dom = {},
269
270
// Features supported by the browser, see #checkCapabilities()
271
features = {},
272
273
// Client is a mobile device, see #checkCapabilities()
274
isMobileDevice,
275
276
// Client is a desktop Chrome, see #checkCapabilities()
277
isChrome,
278
279
// Throttles mouse wheel navigation
280
lastMouseWheelStep = 0,
281
282
// Delays updates to the URL due to a Chrome thumbnailer bug
283
writeURLTimeout = 0,
284
285
// Flags if the interaction event listeners are bound
286
eventsAreBound = false,
287
288
// The current auto-slide duration
289
autoSlide = 0,
290
291
// Auto slide properties
292
autoSlidePlayer,
293
autoSlideTimeout = 0,
294
autoSlideStartTime = -1,
295
autoSlidePaused = false,
296
297
// Holds information about the currently ongoing touch input
298
touch = {
299
startX: 0,
300
startY: 0,
301
startSpan: 0,
302
startCount: 0,
303
captured: false,
304
threshold: 40
305
},
306
307
// Holds information about the keyboard shortcuts
308
keyboardShortcuts = {
309
'N , SPACE': 'Next slide',
310
'P': 'Previous slide',
311
'← , H': 'Navigate left',
312
'→ , L': 'Navigate right',
313
'↑ , K': 'Navigate up',
314
'↓ , J': 'Navigate down',
315
'Home': 'First slide',
316
'End': 'Last slide',
317
'B , .': 'Pause',
318
'F': 'Fullscreen',
319
'ESC, O': 'Slide overview'
320
},
321
322
// Holds custom key code mappings
323
registeredKeyBindings = {};
324
325
/**
326
* Starts up the presentation if the client is capable.
327
*/
328
function initialize( options ) {
329
330
// Make sure we only initialize once
331
if( initialized === true ) return;
332
333
initialized = true;
334
335
checkCapabilities();
336
337
if( !features.transforms2d && !features.transforms3d ) {
338
document.body.setAttribute( 'class', 'no-transforms' );
339
340
// Since JS won't be running any further, we load all lazy
341
// loading elements upfront
342
var images = toArray( document.getElementsByTagName( 'img' ) ),
343
iframes = toArray( document.getElementsByTagName( 'iframe' ) );
344
345
var lazyLoadable = images.concat( iframes );
346
347
for( var i = 0, len = lazyLoadable.length; i < len; i++ ) {
348
var element = lazyLoadable[i];
349
if( element.getAttribute( 'data-src' ) ) {
350
element.setAttribute( 'src', element.getAttribute( 'data-src' ) );
351
element.removeAttribute( 'data-src' );
352
}
353
}
354
355
// If the browser doesn't support core features we won't be
356
// using JavaScript to control the presentation
357
return;
358
}
359
360
// Cache references to key DOM elements
361
dom.wrapper = document.querySelector( '.reveal' );
362
dom.slides = document.querySelector( '.reveal .slides' );
363
364
// Force a layout when the whole page, incl fonts, has loaded
365
window.addEventListener( 'load', layout, false );
366
367
var query = Reveal.getQueryHash();
368
369
// Do not accept new dependencies via query config to avoid
370
// the potential of malicious script injection
371
if( typeof query['dependencies'] !== 'undefined' ) delete query['dependencies'];
372
373
// Copy options over to our config object
374
extend( config, options );
375
extend( config, query );
376
377
// Hide the address bar in mobile browsers
378
hideAddressBar();
379
380
// Loads the dependencies and continues to #start() once done
381
load();
382
383
}
384
385
/**
386
* Inspect the client to see what it's capable of, this
387
* should only happens once per runtime.
388
*/
389
function checkCapabilities() {
390
391
isMobileDevice = /(iphone|ipod|ipad|android)/gi.test( UA );
392
isChrome = /chrome/i.test( UA ) && !/edge/i.test( UA );
393
394
var testElement = document.createElement( 'div' );
395
396
features.transforms3d = 'WebkitPerspective' in testElement.style ||
397
'MozPerspective' in testElement.style ||
398
'msPerspective' in testElement.style ||
399
'OPerspective' in testElement.style ||
400
'perspective' in testElement.style;
401
402
features.transforms2d = 'WebkitTransform' in testElement.style ||
403
'MozTransform' in testElement.style ||
404
'msTransform' in testElement.style ||
405
'OTransform' in testElement.style ||
406
'transform' in testElement.style;
407
408
features.requestAnimationFrameMethod = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame;
409
features.requestAnimationFrame = typeof features.requestAnimationFrameMethod === 'function';
410
411
features.canvas = !!document.createElement( 'canvas' ).getContext;
412
413
// Transitions in the overview are disabled in desktop and
414
// Safari due to lag
415
features.overviewTransitions = !/Version\/[\d\.]+.*Safari/.test( UA );
416
417
// Flags if we should use zoom instead of transform to scale
418
// up slides. Zoom produces crisper results but has a lot of
419
// xbrowser quirks so we only use it in whitelsited browsers.
420
features.zoom = 'zoom' in testElement.style && !isMobileDevice &&
421
( isChrome || /Version\/[\d\.]+.*Safari/.test( UA ) );
422
423
}
424
425
/**
426
* Loads the dependencies of reveal.js. Dependencies are
427
* defined via the configuration option 'dependencies'
428
* and will be loaded prior to starting/binding reveal.js.
429
* Some dependencies may have an 'async' flag, if so they
430
* will load after reveal.js has been started up.
431
*/
432
function load() {
433
434
var scripts = [],
435
scriptsAsync = [],
436
scriptsToPreload = 0;
437
438
// Called once synchronous scripts finish loading
439
function proceed() {
440
if( scriptsAsync.length ) {
441
// Load asynchronous scripts
442
head.js.apply( null, scriptsAsync );
443
}
444
445
start();
446
}
447
448
function loadScript( s ) {
449
head.ready( s.src.match( /([\w\d_\-]*)\.?js(\?[\w\d.=&]*)?$|[^\\\/]*$/i )[0], function() {
450
// Extension may contain callback functions
451
if( typeof s.callback === 'function' ) {
452
s.callback.apply( this );
453
}
454
455
if( --scriptsToPreload === 0 ) {
456
proceed();
457
}
458
});
459
}
460
461
for( var i = 0, len = config.dependencies.length; i < len; i++ ) {
462
var s = config.dependencies[i];
463
464
// Load if there's no condition or the condition is truthy
465
if( !s.condition || s.condition() ) {
466
if( s.async ) {
467
scriptsAsync.push( s.src );
468
}
469
else {
470
scripts.push( s.src );
471
}
472
473
loadScript( s );
474
}
475
}
476
477
if( scripts.length ) {
478
scriptsToPreload = scripts.length;
479
480
// Load synchronous scripts
481
head.js.apply( null, scripts );
482
}
483
else {
484
proceed();
485
}
486
487
}
488
489
/**
490
* Starts up reveal.js by binding input events and navigating
491
* to the current URL deeplink if there is one.
492
*/
493
function start() {
494
495
loaded = true;
496
497
// Make sure we've got all the DOM elements we need
498
setupDOM();
499
500
// Listen to messages posted to this window
501
setupPostMessage();
502
503
// Prevent the slides from being scrolled out of view
504
setupScrollPrevention();
505
506
// Resets all vertical slides so that only the first is visible
507
resetVerticalSlides();
508
509
// Updates the presentation to match the current configuration values
510
configure();
511
512
// Read the initial hash
513
readURL();
514
515
// Update all backgrounds
516
updateBackground( true );
517
518
// Notify listeners that the presentation is ready but use a 1ms
519
// timeout to ensure it's not fired synchronously after #initialize()
520
setTimeout( function() {
521
// Enable transitions now that we're loaded
522
dom.slides.classList.remove( 'no-transition' );
523
524
dom.wrapper.classList.add( 'ready' );
525
526
dispatchEvent( 'ready', {
527
'indexh': indexh,
528
'indexv': indexv,
529
'currentSlide': currentSlide
530
} );
531
}, 1 );
532
533
// Special setup and config is required when printing to PDF
534
if( isPrintingPDF() ) {
535
removeEventListeners();
536
537
// The document needs to have loaded for the PDF layout
538
// measurements to be accurate
539
if( document.readyState === 'complete' ) {
540
setupPDF();
541
}
542
else {
543
window.addEventListener( 'load', setupPDF );
544
}
545
}
546
547
}
548
549
/**
550
* Finds and stores references to DOM elements which are
551
* required by the presentation. If a required element is
552
* not found, it is created.
553
*/
554
function setupDOM() {
555
556
// Prevent transitions while we're loading
557
dom.slides.classList.add( 'no-transition' );
558
559
if( isMobileDevice ) {
560
dom.wrapper.classList.add( 'no-hover' );
561
}
562
else {
563
dom.wrapper.classList.remove( 'no-hover' );
564
}
565
566
if( /iphone/gi.test( UA ) ) {
567
dom.wrapper.classList.add( 'ua-iphone' );
568
}
569
else {
570
dom.wrapper.classList.remove( 'ua-iphone' );
571
}
572
573
// Background element
574
dom.background = createSingletonNode( dom.wrapper, 'div', 'backgrounds', null );
575
576
// Progress bar
577
dom.progress = createSingletonNode( dom.wrapper, 'div', 'progress', '<span></span>' );
578
dom.progressbar = dom.progress.querySelector( 'span' );
579
580
// Arrow controls
581
dom.controls = createSingletonNode( dom.wrapper, 'aside', 'controls',
582
'<button class="navigate-left" aria-label="previous slide"><div class="controls-arrow"></div></button>' +
583
'<button class="navigate-right" aria-label="next slide"><div class="controls-arrow"></div></button>' +
584
'<button class="navigate-up" aria-label="above slide"><div class="controls-arrow"></div></button>' +
585
'<button class="navigate-down" aria-label="below slide"><div class="controls-arrow"></div></button>' );
586
587
// Slide number
588
dom.slideNumber = createSingletonNode( dom.wrapper, 'div', 'slide-number', '' );
589
590
// Element containing notes that are visible to the audience
591
dom.speakerNotes = createSingletonNode( dom.wrapper, 'div', 'speaker-notes', null );
592
dom.speakerNotes.setAttribute( 'data-prevent-swipe', '' );
593
dom.speakerNotes.setAttribute( 'tabindex', '0' );
594
595
// Overlay graphic which is displayed during the paused mode
596
dom.pauseOverlay = createSingletonNode( dom.wrapper, 'div', 'pause-overlay', '<button class="resume-button">Resume presentation</button>' );
597
dom.resumeButton = dom.pauseOverlay.querySelector( '.resume-button' );
598
599
dom.wrapper.setAttribute( 'role', 'application' );
600
601
// There can be multiple instances of controls throughout the page
602
dom.controlsLeft = toArray( document.querySelectorAll( '.navigate-left' ) );
603
dom.controlsRight = toArray( document.querySelectorAll( '.navigate-right' ) );
604
dom.controlsUp = toArray( document.querySelectorAll( '.navigate-up' ) );
605
dom.controlsDown = toArray( document.querySelectorAll( '.navigate-down' ) );
606
dom.controlsPrev = toArray( document.querySelectorAll( '.navigate-prev' ) );
607
dom.controlsNext = toArray( document.querySelectorAll( '.navigate-next' ) );
608
609
// The right and down arrows in the standard reveal.js controls
610
dom.controlsRightArrow = dom.controls.querySelector( '.navigate-right' );
611
dom.controlsDownArrow = dom.controls.querySelector( '.navigate-down' );
612
613
dom.statusDiv = createStatusDiv();
614
}
615
616
/**
617
* Creates a hidden div with role aria-live to announce the
618
* current slide content. Hide the div off-screen to make it
619
* available only to Assistive Technologies.
620
*
621
* @return {HTMLElement}
622
*/
623
function createStatusDiv() {
624
625
var statusDiv = document.getElementById( 'aria-status-div' );
626
if( !statusDiv ) {
627
statusDiv = document.createElement( 'div' );
628
statusDiv.style.position = 'absolute';
629
statusDiv.style.height = '1px';
630
statusDiv.style.width = '1px';
631
statusDiv.style.overflow = 'hidden';
632
statusDiv.style.clip = 'rect( 1px, 1px, 1px, 1px )';
633
statusDiv.setAttribute( 'id', 'aria-status-div' );
634
statusDiv.setAttribute( 'aria-live', 'polite' );
635
statusDiv.setAttribute( 'aria-atomic','true' );
636
dom.wrapper.appendChild( statusDiv );
637
}
638
return statusDiv;
639
640
}
641
642
/**
643
* Converts the given HTML element into a string of text
644
* that can be announced to a screen reader. Hidden
645
* elements are excluded.
646
*/
647
function getStatusText( node ) {
648
649
var text = '';
650
651
// Text node
652
if( node.nodeType === 3 ) {
653
text += node.textContent;
654
}
655
// Element node
656
else if( node.nodeType === 1 ) {
657
658
var isAriaHidden = node.getAttribute( 'aria-hidden' );
659
var isDisplayHidden = window.getComputedStyle( node )['display'] === 'none';
660
if( isAriaHidden !== 'true' && !isDisplayHidden ) {
661
662
toArray( node.childNodes ).forEach( function( child ) {
663
text += getStatusText( child );
664
} );
665
666
}
667
668
}
669
670
return text;
671
672
}
673
674
/**
675
* Configures the presentation for printing to a static
676
* PDF.
677
*/
678
function setupPDF() {
679
680
var slideSize = getComputedSlideSize( window.innerWidth, window.innerHeight );
681
682
// Dimensions of the PDF pages
683
var pageWidth = Math.floor( slideSize.width * ( 1 + config.margin ) ),
684
pageHeight = Math.floor( slideSize.height * ( 1 + config.margin ) );
685
686
// Dimensions of slides within the pages
687
var slideWidth = slideSize.width,
688
slideHeight = slideSize.height;
689
690
// Let the browser know what page size we want to print
691
injectStyleSheet( '@page{size:'+ pageWidth +'px '+ pageHeight +'px; margin: 0px;}' );
692
693
// Limit the size of certain elements to the dimensions of the slide
694
injectStyleSheet( '.reveal section>img, .reveal section>video, .reveal section>iframe{max-width: '+ slideWidth +'px; max-height:'+ slideHeight +'px}' );
695
696
document.body.classList.add( 'print-pdf' );
697
document.body.style.width = pageWidth + 'px';
698
document.body.style.height = pageHeight + 'px';
699
700
// Make sure stretch elements fit on slide
701
layoutSlideContents( slideWidth, slideHeight );
702
703
// Add each slide's index as attributes on itself, we need these
704
// indices to generate slide numbers below
705
toArray( dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) ).forEach( function( hslide, h ) {
706
hslide.setAttribute( 'data-index-h', h );
707
708
if( hslide.classList.contains( 'stack' ) ) {
709
toArray( hslide.querySelectorAll( 'section' ) ).forEach( function( vslide, v ) {
710
vslide.setAttribute( 'data-index-h', h );
711
vslide.setAttribute( 'data-index-v', v );
712
} );
713
}
714
} );
715
716
// Slide and slide background layout
717
toArray( dom.wrapper.querySelectorAll( SLIDES_SELECTOR ) ).forEach( function( slide ) {
718
719
// Vertical stacks are not centred since their section
720
// children will be
721
if( slide.classList.contains( 'stack' ) === false ) {
722
// Center the slide inside of the page, giving the slide some margin
723
var left = ( pageWidth - slideWidth ) / 2,
724
top = ( pageHeight - slideHeight ) / 2;
725
726
var contentHeight = slide.scrollHeight;
727
var numberOfPages = Math.max( Math.ceil( contentHeight / pageHeight ), 1 );
728
729
// Adhere to configured pages per slide limit
730
numberOfPages = Math.min( numberOfPages, config.pdfMaxPagesPerSlide );
731
732
// Center slides vertically
733
if( numberOfPages === 1 && config.center || slide.classList.contains( 'center' ) ) {
734
top = Math.max( ( pageHeight - contentHeight ) / 2, 0 );
735
}
736
737
// Wrap the slide in a page element and hide its overflow
738
// so that no page ever flows onto another
739
var page = document.createElement( 'div' );
740
page.className = 'pdf-page';
741
page.style.height = ( ( pageHeight + config.pdfPageHeightOffset ) * numberOfPages ) + 'px';
742
slide.parentNode.insertBefore( page, slide );
743
page.appendChild( slide );
744
745
// Position the slide inside of the page
746
slide.style.left = left + 'px';
747
slide.style.top = top + 'px';
748
slide.style.width = slideWidth + 'px';
749
750
if( slide.slideBackgroundElement ) {
751
page.insertBefore( slide.slideBackgroundElement, slide );
752
}
753
754
// Inject notes if `showNotes` is enabled
755
if( config.showNotes ) {
756
757
// Are there notes for this slide?
758
var notes = getSlideNotes( slide );
759
if( notes ) {
760
761
var notesSpacing = 8;
762
var notesLayout = typeof config.showNotes === 'string' ? config.showNotes : 'inline';
763
var notesElement = document.createElement( 'div' );
764
notesElement.classList.add( 'speaker-notes' );
765
notesElement.classList.add( 'speaker-notes-pdf' );
766
notesElement.setAttribute( 'data-layout', notesLayout );
767
notesElement.innerHTML = notes;
768
769
if( notesLayout === 'separate-page' ) {
770
page.parentNode.insertBefore( notesElement, page.nextSibling );
771
}
772
else {
773
notesElement.style.left = notesSpacing + 'px';
774
notesElement.style.bottom = notesSpacing + 'px';
775
notesElement.style.width = ( pageWidth - notesSpacing*2 ) + 'px';
776
page.appendChild( notesElement );
777
}
778
779
}
780
781
}
782
783
// Inject slide numbers if `slideNumbers` are enabled
784
if( config.slideNumber && /all|print/i.test( config.showSlideNumber ) ) {
785
var slideNumberH = parseInt( slide.getAttribute( 'data-index-h' ), 10 ) + 1,
786
slideNumberV = parseInt( slide.getAttribute( 'data-index-v' ), 10 ) + 1;
787
788
var numberElement = document.createElement( 'div' );
789
numberElement.classList.add( 'slide-number' );
790
numberElement.classList.add( 'slide-number-pdf' );
791
numberElement.innerHTML = formatSlideNumber( slideNumberH, '.', slideNumberV );
792
page.appendChild( numberElement );
793
}
794
795
// Copy page and show fragments one after another
796
if( config.pdfSeparateFragments ) {
797
798
// Each fragment 'group' is an array containing one or more
799
// fragments. Multiple fragments that appear at the same time
800
// are part of the same group.
801
var fragmentGroups = sortFragments( page.querySelectorAll( '.fragment' ), true );
802
803
var previousFragmentStep;
804
var previousPage;
805
806
fragmentGroups.forEach( function( fragments ) {
807
808
// Remove 'current-fragment' from the previous group
809
if( previousFragmentStep ) {
810
previousFragmentStep.forEach( function( fragment ) {
811
fragment.classList.remove( 'current-fragment' );
812
} );
813
}
814
815
// Show the fragments for the current index
816
fragments.forEach( function( fragment ) {
817
fragment.classList.add( 'visible', 'current-fragment' );
818
} );
819
820
// Create a separate page for the current fragment state
821
var clonedPage = page.cloneNode( true );
822
page.parentNode.insertBefore( clonedPage, ( previousPage || page ).nextSibling );
823
824
previousFragmentStep = fragments;
825
previousPage = clonedPage;
826
827
} );
828
829
// Reset the first/original page so that all fragments are hidden
830
fragmentGroups.forEach( function( fragments ) {
831
fragments.forEach( function( fragment ) {
832
fragment.classList.remove( 'visible', 'current-fragment' );
833
} );
834
} );
835
836
}
837
// Show all fragments
838
else {
839
toArray( page.querySelectorAll( '.fragment:not(.fade-out)' ) ).forEach( function( fragment ) {
840
fragment.classList.add( 'visible' );
841
} );
842
}
843
844
}
845
846
} );
847
848
// Notify subscribers that the PDF layout is good to go
849
dispatchEvent( 'pdf-ready' );
850
851
}
852
853
/**
854
* This is an unfortunate necessity. Some actions – such as
855
* an input field being focused in an iframe or using the
856
* keyboard to expand text selection beyond the bounds of
857
* a slide – can trigger our content to be pushed out of view.
858
* This scrolling can not be prevented by hiding overflow in
859
* CSS (we already do) so we have to resort to repeatedly
860
* checking if the slides have been offset :(
861
*/
862
function setupScrollPrevention() {
863
864
setInterval( function() {
865
if( dom.wrapper.scrollTop !== 0 || dom.wrapper.scrollLeft !== 0 ) {
866
dom.wrapper.scrollTop = 0;
867
dom.wrapper.scrollLeft = 0;
868
}
869
}, 1000 );
870
871
}
872
873
/**
874
* Creates an HTML element and returns a reference to it.
875
* If the element already exists the existing instance will
876
* be returned.
877
*
878
* @param {HTMLElement} container
879
* @param {string} tagname
880
* @param {string} classname
881
* @param {string} innerHTML
882
*
883
* @return {HTMLElement}
884
*/
885
function createSingletonNode( container, tagname, classname, innerHTML ) {
886
887
// Find all nodes matching the description
888
var nodes = container.querySelectorAll( '.' + classname );
889
890
// Check all matches to find one which is a direct child of
891
// the specified container
892
for( var i = 0; i < nodes.length; i++ ) {
893
var testNode = nodes[i];
894
if( testNode.parentNode === container ) {
895
return testNode;
896
}
897
}
898
899
// If no node was found, create it now
900
var node = document.createElement( tagname );
901
node.className = classname;
902
if( typeof innerHTML === 'string' ) {
903
node.innerHTML = innerHTML;
904
}
905
container.appendChild( node );
906
907
return node;
908
909
}
910
911
/**
912
* Creates the slide background elements and appends them
913
* to the background container. One element is created per
914
* slide no matter if the given slide has visible background.
915
*/
916
function createBackgrounds() {
917
918
var printMode = isPrintingPDF();
919
920
// Clear prior backgrounds
921
dom.background.innerHTML = '';
922
dom.background.classList.add( 'no-transition' );
923
924
// Iterate over all horizontal slides
925
toArray( dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) ).forEach( function( slideh ) {
926
927
var backgroundStack = createBackground( slideh, dom.background );
928
929
// Iterate over all vertical slides
930
toArray( slideh.querySelectorAll( 'section' ) ).forEach( function( slidev ) {
931
932
createBackground( slidev, backgroundStack );
933
934
backgroundStack.classList.add( 'stack' );
935
936
} );
937
938
} );
939
940
// Add parallax background if specified
941
if( config.parallaxBackgroundImage ) {
942
943
dom.background.style.backgroundImage = 'url("' + config.parallaxBackgroundImage + '")';
944
dom.background.style.backgroundSize = config.parallaxBackgroundSize;
945
dom.background.style.backgroundRepeat = config.parallaxBackgroundRepeat;
946
dom.background.style.backgroundPosition = config.parallaxBackgroundPosition;
947
948
// Make sure the below properties are set on the element - these properties are
949
// needed for proper transitions to be set on the element via CSS. To remove
950
// annoying background slide-in effect when the presentation starts, apply
951
// these properties after short time delay
952
setTimeout( function() {
953
dom.wrapper.classList.add( 'has-parallax-background' );
954
}, 1 );
955
956
}
957
else {
958
959
dom.background.style.backgroundImage = '';
960
dom.wrapper.classList.remove( 'has-parallax-background' );
961
962
}
963
964
}
965
966
/**
967
* Creates a background for the given slide.
968
*
969
* @param {HTMLElement} slide
970
* @param {HTMLElement} container The element that the background
971
* should be appended to
972
* @return {HTMLElement} New background div
973
*/
974
function createBackground( slide, container ) {
975
976
977
// Main slide background element
978
var element = document.createElement( 'div' );
979
element.className = 'slide-background ' + slide.className.replace( /present|past|future/, '' );
980
981
// Inner background element that wraps images/videos/iframes
982
var contentElement = document.createElement( 'div' );
983
contentElement.className = 'slide-background-content';
984
985
element.appendChild( contentElement );
986
container.appendChild( element );
987
988
slide.slideBackgroundElement = element;
989
slide.slideBackgroundContentElement = contentElement;
990
991
// Syncs the background to reflect all current background settings
992
syncBackground( slide );
993
994
return element;
995
996
}
997
998
/**
999
* Renders all of the visual properties of a slide background
1000
* based on the various background attributes.
1001
*
1002
* @param {HTMLElement} slide
1003
*/
1004
function syncBackground( slide ) {
1005
1006
var element = slide.slideBackgroundElement,
1007
contentElement = slide.slideBackgroundContentElement;
1008
1009
// Reset the prior background state in case this is not the
1010
// initial sync
1011
slide.classList.remove( 'has-dark-background' );
1012
slide.classList.remove( 'has-light-background' );
1013
1014
element.removeAttribute( 'data-loaded' );
1015
element.removeAttribute( 'data-background-hash' );
1016
element.removeAttribute( 'data-background-size' );
1017
element.removeAttribute( 'data-background-transition' );
1018
element.style.backgroundColor = '';
1019
1020
contentElement.style.backgroundSize = '';
1021
contentElement.style.backgroundRepeat = '';
1022
contentElement.style.backgroundPosition = '';
1023
contentElement.style.backgroundImage = '';
1024
contentElement.style.opacity = '';
1025
contentElement.innerHTML = '';
1026
1027
var data = {
1028
background: slide.getAttribute( 'data-background' ),
1029
backgroundSize: slide.getAttribute( 'data-background-size' ),
1030
backgroundImage: slide.getAttribute( 'data-background-image' ),
1031
backgroundVideo: slide.getAttribute( 'data-background-video' ),
1032
backgroundIframe: slide.getAttribute( 'data-background-iframe' ),
1033
backgroundColor: slide.getAttribute( 'data-background-color' ),
1034
backgroundRepeat: slide.getAttribute( 'data-background-repeat' ),
1035
backgroundPosition: slide.getAttribute( 'data-background-position' ),
1036
backgroundTransition: slide.getAttribute( 'data-background-transition' ),
1037
backgroundOpacity: slide.getAttribute( 'data-background-opacity' )
1038
};
1039
1040
if( data.background ) {
1041
// Auto-wrap image urls in url(...)
1042
if( /^(http|file|\/\/)/gi.test( data.background ) || /\.(svg|png|jpg|jpeg|gif|bmp)([?#\s]|$)/gi.test( data.background ) ) {
1043
slide.setAttribute( 'data-background-image', data.background );
1044
}
1045
else {
1046
element.style.background = data.background;
1047
}
1048
}
1049
1050
// Create a hash for this combination of background settings.
1051
// This is used to determine when two slide backgrounds are
1052
// the same.
1053
if( data.background || data.backgroundColor || data.backgroundImage || data.backgroundVideo || data.backgroundIframe ) {
1054
element.setAttribute( 'data-background-hash', data.background +
1055
data.backgroundSize +
1056
data.backgroundImage +
1057
data.backgroundVideo +
1058
data.backgroundIframe +
1059
data.backgroundColor +
1060
data.backgroundRepeat +
1061
data.backgroundPosition +
1062
data.backgroundTransition +
1063
data.backgroundOpacity );
1064
}
1065
1066
// Additional and optional background properties
1067
if( data.backgroundSize ) element.setAttribute( 'data-background-size', data.backgroundSize );
1068
if( data.backgroundColor ) element.style.backgroundColor = data.backgroundColor;
1069
if( data.backgroundTransition ) element.setAttribute( 'data-background-transition', data.backgroundTransition );
1070
1071
// Background image options are set on the content wrapper
1072
if( data.backgroundSize ) contentElement.style.backgroundSize = data.backgroundSize;
1073
if( data.backgroundRepeat ) contentElement.style.backgroundRepeat = data.backgroundRepeat;
1074
if( data.backgroundPosition ) contentElement.style.backgroundPosition = data.backgroundPosition;
1075
if( data.backgroundOpacity ) contentElement.style.opacity = data.backgroundOpacity;
1076
1077
// If this slide has a background color, add a class that
1078
// signals if it is light or dark. If the slide has no background
1079
// color, no class will be set
1080
var computedBackgroundStyle = window.getComputedStyle( element );
1081
if( computedBackgroundStyle && computedBackgroundStyle.backgroundColor ) {
1082
var rgb = colorToRgb( computedBackgroundStyle.backgroundColor );
1083
1084
// Ignore fully transparent backgrounds. Some browsers return
1085
// rgba(0,0,0,0) when reading the computed background color of
1086
// an element with no background
1087
if( rgb && rgb.a !== 0 ) {
1088
if( colorBrightness( computedBackgroundStyle.backgroundColor ) < 128 ) {
1089
slide.classList.add( 'has-dark-background' );
1090
}
1091
else {
1092
slide.classList.add( 'has-light-background' );
1093
}
1094
}
1095
}
1096
1097
}
1098
1099
/**
1100
* Registers a listener to postMessage events, this makes it
1101
* possible to call all reveal.js API methods from another
1102
* window. For example:
1103
*
1104
* revealWindow.postMessage( JSON.stringify({
1105
* method: 'slide',
1106
* args: [ 2 ]
1107
* }), '*' );
1108
*/
1109
function setupPostMessage() {
1110
1111
if( config.postMessage ) {
1112
window.addEventListener( 'message', function ( event ) {
1113
var data = event.data;
1114
1115
// Make sure we're dealing with JSON
1116
if( typeof data === 'string' && data.charAt( 0 ) === '{' && data.charAt( data.length - 1 ) === '}' ) {
1117
data = JSON.parse( data );
1118
1119
// Check if the requested method can be found
1120
if( data.method && typeof Reveal[data.method] === 'function' ) {
1121
Reveal[data.method].apply( Reveal, data.args );
1122
}
1123
}
1124
}, false );
1125
}
1126
1127
}
1128
1129
/**
1130
* Applies the configuration settings from the config
1131
* object. May be called multiple times.
1132
*
1133
* @param {object} options
1134
*/
1135
function configure( options ) {
1136
1137
var oldTransition = config.transition;
1138
1139
// New config options may be passed when this method
1140
// is invoked through the API after initialization
1141
if( typeof options === 'object' ) extend( config, options );
1142
1143
// Abort if reveal.js hasn't finished loading, config
1144
// changes will be applied automatically once loading
1145
// finishes
1146
if( loaded === false ) return;
1147
1148
var numberOfSlides = dom.wrapper.querySelectorAll( SLIDES_SELECTOR ).length;
1149
1150
// Remove the previously configured transition class
1151
dom.wrapper.classList.remove( oldTransition );
1152
1153
// Force linear transition based on browser capabilities
1154
if( features.transforms3d === false ) config.transition = 'linear';
1155
1156
dom.wrapper.classList.add( config.transition );
1157
1158
dom.wrapper.setAttribute( 'data-transition-speed', config.transitionSpeed );
1159
dom.wrapper.setAttribute( 'data-background-transition', config.backgroundTransition );
1160
1161
dom.controls.style.display = config.controls ? 'block' : 'none';
1162
dom.progress.style.display = config.progress ? 'block' : 'none';
1163
1164
dom.controls.setAttribute( 'data-controls-layout', config.controlsLayout );
1165
dom.controls.setAttribute( 'data-controls-back-arrows', config.controlsBackArrows );
1166
1167
if( config.shuffle ) {
1168
shuffle();
1169
}
1170
1171
if( config.rtl ) {
1172
dom.wrapper.classList.add( 'rtl' );
1173
}
1174
else {
1175
dom.wrapper.classList.remove( 'rtl' );
1176
}
1177
1178
if( config.center ) {
1179
dom.wrapper.classList.add( 'center' );
1180
}
1181
else {
1182
dom.wrapper.classList.remove( 'center' );
1183
}
1184
1185
// Exit the paused mode if it was configured off
1186
if( config.pause === false ) {
1187
resume();
1188
}
1189
1190
if( config.showNotes ) {
1191
dom.speakerNotes.setAttribute( 'data-layout', typeof config.showNotes === 'string' ? config.showNotes : 'inline' );
1192
}
1193
1194
if( config.mouseWheel ) {
1195
document.addEventListener( 'DOMMouseScroll', onDocumentMouseScroll, false ); // FF
1196
document.addEventListener( 'mousewheel', onDocumentMouseScroll, false );
1197
}
1198
else {
1199
document.removeEventListener( 'DOMMouseScroll', onDocumentMouseScroll, false ); // FF
1200
document.removeEventListener( 'mousewheel', onDocumentMouseScroll, false );
1201
}
1202
1203
// Rolling 3D links
1204
if( config.rollingLinks ) {
1205
enableRollingLinks();
1206
}
1207
else {
1208
disableRollingLinks();
1209
}
1210
1211
// Iframe link previews
1212
if( config.previewLinks ) {
1213
enablePreviewLinks();
1214
disablePreviewLinks( '[data-preview-link=false]' );
1215
}
1216
else {
1217
disablePreviewLinks();
1218
enablePreviewLinks( '[data-preview-link]:not([data-preview-link=false])' );
1219
}
1220
1221
// Remove existing auto-slide controls
1222
if( autoSlidePlayer ) {
1223
autoSlidePlayer.destroy();
1224
autoSlidePlayer = null;
1225
}
1226
1227
// Generate auto-slide controls if needed
1228
if( numberOfSlides > 1 && config.autoSlide && config.autoSlideStoppable && features.canvas && features.requestAnimationFrame ) {
1229
autoSlidePlayer = new Playback( dom.wrapper, function() {
1230
return Math.min( Math.max( ( Date.now() - autoSlideStartTime ) / autoSlide, 0 ), 1 );
1231
} );
1232
1233
autoSlidePlayer.on( 'click', onAutoSlidePlayerClick );
1234
autoSlidePaused = false;
1235
}
1236
1237
// When fragments are turned off they should be visible
1238
if( config.fragments === false ) {
1239
toArray( dom.slides.querySelectorAll( '.fragment' ) ).forEach( function( element ) {
1240
element.classList.add( 'visible' );
1241
element.classList.remove( 'current-fragment' );
1242
} );
1243
}
1244
1245
// Slide numbers
1246
var slideNumberDisplay = 'none';
1247
if( config.slideNumber && !isPrintingPDF() ) {
1248
if( config.showSlideNumber === 'all' ) {
1249
slideNumberDisplay = 'block';
1250
}
1251
else if( config.showSlideNumber === 'speaker' && isSpeakerNotes() ) {
1252
slideNumberDisplay = 'block';
1253
}
1254
}
1255
1256
dom.slideNumber.style.display = slideNumberDisplay;
1257
1258
sync();
1259
1260
}
1261
1262
/**
1263
* Binds all event listeners.
1264
*/
1265
function addEventListeners() {
1266
1267
eventsAreBound = true;
1268
1269
window.addEventListener( 'hashchange', onWindowHashChange, false );
1270
window.addEventListener( 'resize', onWindowResize, false );
1271
1272
if( config.touch ) {
1273
if( 'onpointerdown' in window ) {
1274
// Use W3C pointer events
1275
dom.wrapper.addEventListener( 'pointerdown', onPointerDown, false );
1276
dom.wrapper.addEventListener( 'pointermove', onPointerMove, false );
1277
dom.wrapper.addEventListener( 'pointerup', onPointerUp, false );
1278
}
1279
else if( window.navigator.msPointerEnabled ) {
1280
// IE 10 uses prefixed version of pointer events
1281
dom.wrapper.addEventListener( 'MSPointerDown', onPointerDown, false );
1282
dom.wrapper.addEventListener( 'MSPointerMove', onPointerMove, false );
1283
dom.wrapper.addEventListener( 'MSPointerUp', onPointerUp, false );
1284
}
1285
else {
1286
// Fall back to touch events
1287
dom.wrapper.addEventListener( 'touchstart', onTouchStart, false );
1288
dom.wrapper.addEventListener( 'touchmove', onTouchMove, false );
1289
dom.wrapper.addEventListener( 'touchend', onTouchEnd, false );
1290
}
1291
}
1292
1293
if( config.keyboard ) {
1294
document.addEventListener( 'keydown', onDocumentKeyDown, false );
1295
document.addEventListener( 'keypress', onDocumentKeyPress, false );
1296
}
1297
1298
if( config.progress && dom.progress ) {
1299
dom.progress.addEventListener( 'click', onProgressClicked, false );
1300
}
1301
1302
dom.resumeButton.addEventListener( 'click', resume, false );
1303
1304
if( config.focusBodyOnPageVisibilityChange ) {
1305
var visibilityChange;
1306
1307
if( 'hidden' in document ) {
1308
visibilityChange = 'visibilitychange';
1309
}
1310
else if( 'msHidden' in document ) {
1311
visibilityChange = 'msvisibilitychange';
1312
}
1313
else if( 'webkitHidden' in document ) {
1314
visibilityChange = 'webkitvisibilitychange';
1315
}
1316
1317
if( visibilityChange ) {
1318
document.addEventListener( visibilityChange, onPageVisibilityChange, false );
1319
}
1320
}
1321
1322
// Listen to both touch and click events, in case the device
1323
// supports both
1324
var pointerEvents = [ 'touchstart', 'click' ];
1325
1326
// Only support touch for Android, fixes double navigations in
1327
// stock browser
1328
if( UA.match( /android/gi ) ) {
1329
pointerEvents = [ 'touchstart' ];
1330
}
1331
1332
pointerEvents.forEach( function( eventName ) {
1333
dom.controlsLeft.forEach( function( el ) { el.addEventListener( eventName, onNavigateLeftClicked, false ); } );
1334
dom.controlsRight.forEach( function( el ) { el.addEventListener( eventName, onNavigateRightClicked, false ); } );
1335
dom.controlsUp.forEach( function( el ) { el.addEventListener( eventName, onNavigateUpClicked, false ); } );
1336
dom.controlsDown.forEach( function( el ) { el.addEventListener( eventName, onNavigateDownClicked, false ); } );
1337
dom.controlsPrev.forEach( function( el ) { el.addEventListener( eventName, onNavigatePrevClicked, false ); } );
1338
dom.controlsNext.forEach( function( el ) { el.addEventListener( eventName, onNavigateNextClicked, false ); } );
1339
} );
1340
1341
}
1342
1343
/**
1344
* Unbinds all event listeners.
1345
*/
1346
function removeEventListeners() {
1347
1348
eventsAreBound = false;
1349
1350
document.removeEventListener( 'keydown', onDocumentKeyDown, false );
1351
document.removeEventListener( 'keypress', onDocumentKeyPress, false );
1352
window.removeEventListener( 'hashchange', onWindowHashChange, false );
1353
window.removeEventListener( 'resize', onWindowResize, false );
1354
1355
dom.wrapper.removeEventListener( 'pointerdown', onPointerDown, false );
1356
dom.wrapper.removeEventListener( 'pointermove', onPointerMove, false );
1357
dom.wrapper.removeEventListener( 'pointerup', onPointerUp, false );
1358
1359
dom.wrapper.removeEventListener( 'MSPointerDown', onPointerDown, false );
1360
dom.wrapper.removeEventListener( 'MSPointerMove', onPointerMove, false );
1361
dom.wrapper.removeEventListener( 'MSPointerUp', onPointerUp, false );
1362
1363
dom.wrapper.removeEventListener( 'touchstart', onTouchStart, false );
1364
dom.wrapper.removeEventListener( 'touchmove', onTouchMove, false );
1365
dom.wrapper.removeEventListener( 'touchend', onTouchEnd, false );
1366
1367
dom.resumeButton.removeEventListener( 'click', resume, false );
1368
1369
if ( config.progress && dom.progress ) {
1370
dom.progress.removeEventListener( 'click', onProgressClicked, false );
1371
}
1372
1373
[ 'touchstart', 'click' ].forEach( function( eventName ) {
1374
dom.controlsLeft.forEach( function( el ) { el.removeEventListener( eventName, onNavigateLeftClicked, false ); } );
1375
dom.controlsRight.forEach( function( el ) { el.removeEventListener( eventName, onNavigateRightClicked, false ); } );
1376
dom.controlsUp.forEach( function( el ) { el.removeEventListener( eventName, onNavigateUpClicked, false ); } );
1377
dom.controlsDown.forEach( function( el ) { el.removeEventListener( eventName, onNavigateDownClicked, false ); } );
1378
dom.controlsPrev.forEach( function( el ) { el.removeEventListener( eventName, onNavigatePrevClicked, false ); } );
1379
dom.controlsNext.forEach( function( el ) { el.removeEventListener( eventName, onNavigateNextClicked, false ); } );
1380
} );
1381
1382
}
1383
1384
/**
1385
* Add a custom key binding with optional description to
1386
* be added to the help screen.
1387
*/
1388
function addKeyBinding( binding, callback ) {
1389
1390
if( typeof binding === 'object' && binding.keyCode ) {
1391
registeredKeyBindings[binding.keyCode] = {
1392
callback: callback,
1393
key: binding.key,
1394
description: binding.description
1395
};
1396
}
1397
else {
1398
registeredKeyBindings[binding] = {
1399
callback: callback,
1400
key: null,
1401
description: null
1402
};
1403
}
1404
1405
}
1406
1407
/**
1408
* Removes the specified custom key binding.
1409
*/
1410
function removeKeyBinding( keyCode ) {
1411
1412
delete registeredKeyBindings[keyCode];
1413
1414
}
1415
1416
/**
1417
* Extend object a with the properties of object b.
1418
* If there's a conflict, object b takes precedence.
1419
*
1420
* @param {object} a
1421
* @param {object} b
1422
*/
1423
function extend( a, b ) {
1424
1425
for( var i in b ) {
1426
a[ i ] = b[ i ];
1427
}
1428
1429
return a;
1430
1431
}
1432
1433
/**
1434
* Converts the target object to an array.
1435
*
1436
* @param {object} o
1437
* @return {object[]}
1438
*/
1439
function toArray( o ) {
1440
1441
return Array.prototype.slice.call( o );
1442
1443
}
1444
1445
/**
1446
* Utility for deserializing a value.
1447
*
1448
* @param {*} value
1449
* @return {*}
1450
*/
1451
function deserialize( value ) {
1452
1453
if( typeof value === 'string' ) {
1454
if( value === 'null' ) return null;
1455
else if( value === 'true' ) return true;
1456
else if( value === 'false' ) return false;
1457
else if( value.match( /^-?[\d\.]+$/ ) ) return parseFloat( value );
1458
}
1459
1460
return value;
1461
1462
}
1463
1464
/**
1465
* Measures the distance in pixels between point a
1466
* and point b.
1467
*
1468
* @param {object} a point with x/y properties
1469
* @param {object} b point with x/y properties
1470
*
1471
* @return {number}
1472
*/
1473
function distanceBetween( a, b ) {
1474
1475
var dx = a.x - b.x,
1476
dy = a.y - b.y;
1477
1478
return Math.sqrt( dx*dx + dy*dy );
1479
1480
}
1481
1482
/**
1483
* Applies a CSS transform to the target element.
1484
*
1485
* @param {HTMLElement} element
1486
* @param {string} transform
1487
*/
1488
function transformElement( element, transform ) {
1489
1490
element.style.WebkitTransform = transform;
1491
element.style.MozTransform = transform;
1492
element.style.msTransform = transform;
1493
element.style.transform = transform;
1494
1495
}
1496
1497
/**
1498
* Applies CSS transforms to the slides container. The container
1499
* is transformed from two separate sources: layout and the overview
1500
* mode.
1501
*
1502
* @param {object} transforms
1503
*/
1504
function transformSlides( transforms ) {
1505
1506
// Pick up new transforms from arguments
1507
if( typeof transforms.layout === 'string' ) slidesTransform.layout = transforms.layout;
1508
if( typeof transforms.overview === 'string' ) slidesTransform.overview = transforms.overview;
1509
1510
// Apply the transforms to the slides container
1511
if( slidesTransform.layout ) {
1512
transformElement( dom.slides, slidesTransform.layout + ' ' + slidesTransform.overview );
1513
}
1514
else {
1515
transformElement( dom.slides, slidesTransform.overview );
1516
}
1517
1518
}
1519
1520
/**
1521
* Injects the given CSS styles into the DOM.
1522
*
1523
* @param {string} value
1524
*/
1525
function injectStyleSheet( value ) {
1526
1527
var tag = document.createElement( 'style' );
1528
tag.type = 'text/css';
1529
if( tag.styleSheet ) {
1530
tag.styleSheet.cssText = value;
1531
}
1532
else {
1533
tag.appendChild( document.createTextNode( value ) );
1534
}
1535
document.getElementsByTagName( 'head' )[0].appendChild( tag );
1536
1537
}
1538
1539
/**
1540
* Find the closest parent that matches the given
1541
* selector.
1542
*
1543
* @param {HTMLElement} target The child element
1544
* @param {String} selector The CSS selector to match
1545
* the parents against
1546
*
1547
* @return {HTMLElement} The matched parent or null
1548
* if no matching parent was found
1549
*/
1550
function closestParent( target, selector ) {
1551
1552
var parent = target.parentNode;
1553
1554
while( parent ) {
1555
1556
// There's some overhead doing this each time, we don't
1557
// want to rewrite the element prototype but should still
1558
// be enough to feature detect once at startup...
1559
var matchesMethod = parent.matches || parent.matchesSelector || parent.msMatchesSelector;
1560
1561
// If we find a match, we're all set
1562
if( matchesMethod && matchesMethod.call( parent, selector ) ) {
1563
return parent;
1564
}
1565
1566
// Keep searching
1567
parent = parent.parentNode;
1568
1569
}
1570
1571
return null;
1572
1573
}
1574
1575
/**
1576
* Converts various color input formats to an {r:0,g:0,b:0} object.
1577
*
1578
* @param {string} color The string representation of a color
1579
* @example
1580
* colorToRgb('#000');
1581
* @example
1582
* colorToRgb('#000000');
1583
* @example
1584
* colorToRgb('rgb(0,0,0)');
1585
* @example
1586
* colorToRgb('rgba(0,0,0)');
1587
*
1588
* @return {{r: number, g: number, b: number, [a]: number}|null}
1589
*/
1590
function colorToRgb( color ) {
1591
1592
var hex3 = color.match( /^#([0-9a-f]{3})$/i );
1593
if( hex3 && hex3[1] ) {
1594
hex3 = hex3[1];
1595
return {
1596
r: parseInt( hex3.charAt( 0 ), 16 ) * 0x11,
1597
g: parseInt( hex3.charAt( 1 ), 16 ) * 0x11,
1598
b: parseInt( hex3.charAt( 2 ), 16 ) * 0x11
1599
};
1600
}
1601
1602
var hex6 = color.match( /^#([0-9a-f]{6})$/i );
1603
if( hex6 && hex6[1] ) {
1604
hex6 = hex6[1];
1605
return {
1606
r: parseInt( hex6.substr( 0, 2 ), 16 ),
1607
g: parseInt( hex6.substr( 2, 2 ), 16 ),
1608
b: parseInt( hex6.substr( 4, 2 ), 16 )
1609
};
1610
}
1611
1612
var rgb = color.match( /^rgb\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)$/i );
1613
if( rgb ) {
1614
return {
1615
r: parseInt( rgb[1], 10 ),
1616
g: parseInt( rgb[2], 10 ),
1617
b: parseInt( rgb[3], 10 )
1618
};
1619
}
1620
1621
var rgba = color.match( /^rgba\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\,\s*([\d]+|[\d]*.[\d]+)\s*\)$/i );
1622
if( rgba ) {
1623
return {
1624
r: parseInt( rgba[1], 10 ),
1625
g: parseInt( rgba[2], 10 ),
1626
b: parseInt( rgba[3], 10 ),
1627
a: parseFloat( rgba[4] )
1628
};
1629
}
1630
1631
return null;
1632
1633
}
1634
1635
/**
1636
* Calculates brightness on a scale of 0-255.
1637
*
1638
* @param {string} color See colorToRgb for supported formats.
1639
* @see {@link colorToRgb}
1640
*/
1641
function colorBrightness( color ) {
1642
1643
if( typeof color === 'string' ) color = colorToRgb( color );
1644
1645
if( color ) {
1646
return ( color.r * 299 + color.g * 587 + color.b * 114 ) / 1000;
1647
}
1648
1649
return null;
1650
1651
}
1652
1653
/**
1654
* Returns the remaining height within the parent of the
1655
* target element.
1656
*
1657
* remaining height = [ configured parent height ] - [ current parent height ]
1658
*
1659
* @param {HTMLElement} element
1660
* @param {number} [height]
1661
*/
1662
function getRemainingHeight( element, height ) {
1663
1664
height = height || 0;
1665
1666
if( element ) {
1667
var newHeight, oldHeight = element.style.height;
1668
1669
// Change the .stretch element height to 0 in order find the height of all
1670
// the other elements
1671
element.style.height = '0px';
1672
newHeight = height - element.parentNode.offsetHeight;
1673
1674
// Restore the old height, just in case
1675
element.style.height = oldHeight + 'px';
1676
1677
return newHeight;
1678
}
1679
1680
return height;
1681
1682
}
1683
1684
/**
1685
* Checks if this instance is being used to print a PDF.
1686
*/
1687
function isPrintingPDF() {
1688
1689
return ( /print-pdf/gi ).test( window.location.search );
1690
1691
}
1692
1693
/**
1694
* Check if this instance is being used to print a PDF with fragments.
1695
*/
1696
function isPrintingPDFFragments() {
1697
1698
return ( /print-pdf-fragments/gi ).test( window.location.search );
1699
1700
}
1701
1702
/**
1703
* Hides the address bar if we're on a mobile device.
1704
*/
1705
function hideAddressBar() {
1706
1707
if( config.hideAddressBar && isMobileDevice ) {
1708
// Events that should trigger the address bar to hide
1709
window.addEventListener( 'load', removeAddressBar, false );
1710
window.addEventListener( 'orientationchange', removeAddressBar, false );
1711
}
1712
1713
}
1714
1715
/**
1716
* Causes the address bar to hide on mobile devices,
1717
* more vertical space ftw.
1718
*/
1719
function removeAddressBar() {
1720
1721
setTimeout( function() {
1722
window.scrollTo( 0, 1 );
1723
}, 10 );
1724
1725
}
1726
1727
/**
1728
* Dispatches an event of the specified type from the
1729
* reveal DOM element.
1730
*/
1731
function dispatchEvent( type, args ) {
1732
1733
var event = document.createEvent( 'HTMLEvents', 1, 2 );
1734
event.initEvent( type, true, true );
1735
extend( event, args );
1736
dom.wrapper.dispatchEvent( event );
1737
1738
// If we're in an iframe, post each reveal.js event to the
1739
// parent window. Used by the notes plugin
1740
if( config.postMessageEvents && window.parent !== window.self ) {
1741
window.parent.postMessage( JSON.stringify({ namespace: 'reveal', eventName: type, state: getState() }), '*' );
1742
}
1743
1744
}
1745
1746
/**
1747
* Wrap all links in 3D goodness.
1748
*/
1749
function enableRollingLinks() {
1750
1751
if( features.transforms3d && !( 'msPerspective' in document.body.style ) ) {
1752
var anchors = dom.wrapper.querySelectorAll( SLIDES_SELECTOR + ' a' );
1753
1754
for( var i = 0, len = anchors.length; i < len; i++ ) {
1755
var anchor = anchors[i];
1756
1757
if( anchor.textContent && !anchor.querySelector( '*' ) && ( !anchor.className || !anchor.classList.contains( anchor, 'roll' ) ) ) {
1758
var span = document.createElement('span');
1759
span.setAttribute('data-title', anchor.text);
1760
span.innerHTML = anchor.innerHTML;
1761
1762
anchor.classList.add( 'roll' );
1763
anchor.innerHTML = '';
1764
anchor.appendChild(span);
1765
}
1766
}
1767
}
1768
1769
}
1770
1771
/**
1772
* Unwrap all 3D links.
1773
*/
1774
function disableRollingLinks() {
1775
1776
var anchors = dom.wrapper.querySelectorAll( SLIDES_SELECTOR + ' a.roll' );
1777
1778
for( var i = 0, len = anchors.length; i < len; i++ ) {
1779
var anchor = anchors[i];
1780
var span = anchor.querySelector( 'span' );
1781
1782
if( span ) {
1783
anchor.classList.remove( 'roll' );
1784
anchor.innerHTML = span.innerHTML;
1785
}
1786
}
1787
1788
}
1789
1790
/**
1791
* Bind preview frame links.
1792
*
1793
* @param {string} [selector=a] - selector for anchors
1794
*/
1795
function enablePreviewLinks( selector ) {
1796
1797
var anchors = toArray( document.querySelectorAll( selector ? selector : 'a' ) );
1798
1799
anchors.forEach( function( element ) {
1800
if( /^(http|www)/gi.test( element.getAttribute( 'href' ) ) ) {
1801
element.addEventListener( 'click', onPreviewLinkClicked, false );
1802
}
1803
} );
1804
1805
}
1806
1807
/**
1808
* Unbind preview frame links.
1809
*/
1810
function disablePreviewLinks( selector ) {
1811
1812
var anchors = toArray( document.querySelectorAll( selector ? selector : 'a' ) );
1813
1814
anchors.forEach( function( element ) {
1815
if( /^(http|www)/gi.test( element.getAttribute( 'href' ) ) ) {
1816
element.removeEventListener( 'click', onPreviewLinkClicked, false );
1817
}
1818
} );
1819
1820
}
1821
1822
/**
1823
* Opens a preview window for the target URL.
1824
*
1825
* @param {string} url - url for preview iframe src
1826
*/
1827
function showPreview( url ) {
1828
1829
closeOverlay();
1830
1831
dom.overlay = document.createElement( 'div' );
1832
dom.overlay.classList.add( 'overlay' );
1833
dom.overlay.classList.add( 'overlay-preview' );
1834
dom.wrapper.appendChild( dom.overlay );
1835
1836
dom.overlay.innerHTML = [
1837
'<header>',
1838
'<a class="close" href="#"><span class="icon"></span></a>',
1839
'<a class="external" href="'+ url +'" target="_blank"><span class="icon"></span></a>',
1840
'</header>',
1841
'<div class="spinner"></div>',
1842
'<div class="viewport">',
1843
'<iframe src="'+ url +'"></iframe>',
1844
'<small class="viewport-inner">',
1845
'<span class="x-frame-error">Unable to load iframe. This is likely due to the site\'s policy (x-frame-options).</span>',
1846
'</small>',
1847
'</div>'
1848
].join('');
1849
1850
dom.overlay.querySelector( 'iframe' ).addEventListener( 'load', function( event ) {
1851
dom.overlay.classList.add( 'loaded' );
1852
}, false );
1853
1854
dom.overlay.querySelector( '.close' ).addEventListener( 'click', function( event ) {
1855
closeOverlay();
1856
event.preventDefault();
1857
}, false );
1858
1859
dom.overlay.querySelector( '.external' ).addEventListener( 'click', function( event ) {
1860
closeOverlay();
1861
}, false );
1862
1863
setTimeout( function() {
1864
dom.overlay.classList.add( 'visible' );
1865
}, 1 );
1866
1867
}
1868
1869
/**
1870
* Open or close help overlay window.
1871
*
1872
* @param {Boolean} [override] Flag which overrides the
1873
* toggle logic and forcibly sets the desired state. True means
1874
* help is open, false means it's closed.
1875
*/
1876
function toggleHelp( override ){
1877
1878
if( typeof override === 'boolean' ) {
1879
override ? showHelp() : closeOverlay();
1880
}
1881
else {
1882
if( dom.overlay ) {
1883
closeOverlay();
1884
}
1885
else {
1886
showHelp();
1887
}
1888
}
1889
}
1890
1891
/**
1892
* Opens an overlay window with help material.
1893
*/
1894
function showHelp() {
1895
1896
if( config.help ) {
1897
1898
closeOverlay();
1899
1900
dom.overlay = document.createElement( 'div' );
1901
dom.overlay.classList.add( 'overlay' );
1902
dom.overlay.classList.add( 'overlay-help' );
1903
dom.wrapper.appendChild( dom.overlay );
1904
1905
var html = '<p class="title">Keyboard Shortcuts</p><br/>';
1906
1907
html += '<table><th>KEY</th><th>ACTION</th>';
1908
for( var key in keyboardShortcuts ) {
1909
html += '<tr><td>' + key + '</td><td>' + keyboardShortcuts[ key ] + '</td></tr>';
1910
}
1911
1912
// Add custom key bindings that have associated descriptions
1913
for( var binding in registeredKeyBindings ) {
1914
if( registeredKeyBindings[binding].key && registeredKeyBindings[binding].description ) {
1915
html += '<tr><td>' + registeredKeyBindings[binding].key + '</td><td>' + registeredKeyBindings[binding].description + '</td></tr>';
1916
}
1917
}
1918
1919
html += '</table>';
1920
1921
dom.overlay.innerHTML = [
1922
'<header>',
1923
'<a class="close" href="#"><span class="icon"></span></a>',
1924
'</header>',
1925
'<div class="viewport">',
1926
'<div class="viewport-inner">'+ html +'</div>',
1927
'</div>'
1928
].join('');
1929
1930
dom.overlay.querySelector( '.close' ).addEventListener( 'click', function( event ) {
1931
closeOverlay();
1932
event.preventDefault();
1933
}, false );
1934
1935
setTimeout( function() {
1936
dom.overlay.classList.add( 'visible' );
1937
}, 1 );
1938
1939
}
1940
1941
}
1942
1943
/**
1944
* Closes any currently open overlay.
1945
*/
1946
function closeOverlay() {
1947
1948
if( dom.overlay ) {
1949
dom.overlay.parentNode.removeChild( dom.overlay );
1950
dom.overlay = null;
1951
}
1952
1953
}
1954
1955
/**
1956
* Applies JavaScript-controlled layout rules to the
1957
* presentation.
1958
*/
1959
function layout() {
1960
1961
if( dom.wrapper && !isPrintingPDF() ) {
1962
1963
if( !config.disableLayout ) {
1964
1965
var size = getComputedSlideSize();
1966
1967
// Layout the contents of the slides
1968
layoutSlideContents( config.width, config.height );
1969
1970
dom.slides.style.width = size.width + 'px';
1971
dom.slides.style.height = size.height + 'px';
1972
1973
// Determine scale of content to fit within available space
1974
scale = Math.min( size.presentationWidth / size.width, size.presentationHeight / size.height );
1975
1976
// Respect max/min scale settings
1977
scale = Math.max( scale, config.minScale );
1978
scale = Math.min( scale, config.maxScale );
1979
1980
// Don't apply any scaling styles if scale is 1
1981
if( scale === 1 ) {
1982
dom.slides.style.zoom = '';
1983
dom.slides.style.left = '';
1984
dom.slides.style.top = '';
1985
dom.slides.style.bottom = '';
1986
dom.slides.style.right = '';
1987
transformSlides( { layout: '' } );
1988
}
1989
else {
1990
// Prefer zoom for scaling up so that content remains crisp.
1991
// Don't use zoom to scale down since that can lead to shifts
1992
// in text layout/line breaks.
1993
if( scale > 1 && features.zoom ) {
1994
dom.slides.style.zoom = scale;
1995
dom.slides.style.left = '';
1996
dom.slides.style.top = '';
1997
dom.slides.style.bottom = '';
1998
dom.slides.style.right = '';
1999
transformSlides( { layout: '' } );
2000
}
2001
// Apply scale transform as a fallback
2002
else {
2003
dom.slides.style.zoom = '';
2004
dom.slides.style.left = '50%';
2005
dom.slides.style.top = '50%';
2006
dom.slides.style.bottom = 'auto';
2007
dom.slides.style.right = 'auto';
2008
transformSlides( { layout: 'translate(-50%, -50%) scale('+ scale +')' } );
2009
}
2010
}
2011
2012
// Select all slides, vertical and horizontal
2013
var slides = toArray( dom.wrapper.querySelectorAll( SLIDES_SELECTOR ) );
2014
2015
for( var i = 0, len = slides.length; i < len; i++ ) {
2016
var slide = slides[ i ];
2017
2018
// Don't bother updating invisible slides
2019
if( slide.style.display === 'none' ) {
2020
continue;
2021
}
2022
2023
if( config.center || slide.classList.contains( 'center' ) ) {
2024
// Vertical stacks are not centred since their section
2025
// children will be
2026
if( slide.classList.contains( 'stack' ) ) {
2027
slide.style.top = 0;
2028
}
2029
else {
2030
slide.style.top = Math.max( ( size.height - slide.scrollHeight ) / 2, 0 ) + 'px';
2031
}
2032
}
2033
else {
2034
slide.style.top = '';
2035
}
2036
2037
}
2038
2039
}
2040
2041
updateProgress();
2042
updateParallax();
2043
2044
if( isOverview() ) {
2045
updateOverview();
2046
}
2047
2048
}
2049
2050
}
2051
2052
/**
2053
* Applies layout logic to the contents of all slides in
2054
* the presentation.
2055
*
2056
* @param {string|number} width
2057
* @param {string|number} height
2058
*/
2059
function layoutSlideContents( width, height ) {
2060
2061
// Handle sizing of elements with the 'stretch' class
2062
toArray( dom.slides.querySelectorAll( 'section > .stretch' ) ).forEach( function( element ) {
2063
2064
// Determine how much vertical space we can use
2065
var remainingHeight = getRemainingHeight( element, height );
2066
2067
// Consider the aspect ratio of media elements
2068
if( /(img|video)/gi.test( element.nodeName ) ) {
2069
var nw = element.naturalWidth || element.videoWidth,
2070
nh = element.naturalHeight || element.videoHeight;
2071
2072
var es = Math.min( width / nw, remainingHeight / nh );
2073
2074
element.style.width = ( nw * es ) + 'px';
2075
element.style.height = ( nh * es ) + 'px';
2076
2077
}
2078
else {
2079
element.style.width = width + 'px';
2080
element.style.height = remainingHeight + 'px';
2081
}
2082
2083
} );
2084
2085
}
2086
2087
/**
2088
* Calculates the computed pixel size of our slides. These
2089
* values are based on the width and height configuration
2090
* options.
2091
*
2092
* @param {number} [presentationWidth=dom.wrapper.offsetWidth]
2093
* @param {number} [presentationHeight=dom.wrapper.offsetHeight]
2094
*/
2095
function getComputedSlideSize( presentationWidth, presentationHeight ) {
2096
2097
var size = {
2098
// Slide size
2099
width: config.width,
2100
height: config.height,
2101
2102
// Presentation size
2103
presentationWidth: presentationWidth || dom.wrapper.offsetWidth,
2104
presentationHeight: presentationHeight || dom.wrapper.offsetHeight
2105
};
2106
2107
// Reduce available space by margin
2108
size.presentationWidth -= ( size.presentationWidth * config.margin );
2109
size.presentationHeight -= ( size.presentationHeight * config.margin );
2110
2111
// Slide width may be a percentage of available width
2112
if( typeof size.width === 'string' && /%$/.test( size.width ) ) {
2113
size.width = parseInt( size.width, 10 ) / 100 * size.presentationWidth;
2114
}
2115
2116
// Slide height may be a percentage of available height
2117
if( typeof size.height === 'string' && /%$/.test( size.height ) ) {
2118
size.height = parseInt( size.height, 10 ) / 100 * size.presentationHeight;
2119
}
2120
2121
return size;
2122
2123
}
2124
2125
/**
2126
* Stores the vertical index of a stack so that the same
2127
* vertical slide can be selected when navigating to and
2128
* from the stack.
2129
*
2130
* @param {HTMLElement} stack The vertical stack element
2131
* @param {string|number} [v=0] Index to memorize
2132
*/
2133
function setPreviousVerticalIndex( stack, v ) {
2134
2135
if( typeof stack === 'object' && typeof stack.setAttribute === 'function' ) {
2136
stack.setAttribute( 'data-previous-indexv', v || 0 );
2137
}
2138
2139
}
2140
2141
/**
2142
* Retrieves the vertical index which was stored using
2143
* #setPreviousVerticalIndex() or 0 if no previous index
2144
* exists.
2145
*
2146
* @param {HTMLElement} stack The vertical stack element
2147
*/
2148
function getPreviousVerticalIndex( stack ) {
2149
2150
if( typeof stack === 'object' && typeof stack.setAttribute === 'function' && stack.classList.contains( 'stack' ) ) {
2151
// Prefer manually defined start-indexv
2152
var attributeName = stack.hasAttribute( 'data-start-indexv' ) ? 'data-start-indexv' : 'data-previous-indexv';
2153
2154
return parseInt( stack.getAttribute( attributeName ) || 0, 10 );
2155
}
2156
2157
return 0;
2158
2159
}
2160
2161
/**
2162
* Displays the overview of slides (quick nav) by scaling
2163
* down and arranging all slide elements.
2164
*/
2165
function activateOverview() {
2166
2167
// Only proceed if enabled in config
2168
if( config.overview && !isOverview() ) {
2169
2170
overview = true;
2171
2172
dom.wrapper.classList.add( 'overview' );
2173
dom.wrapper.classList.remove( 'overview-deactivating' );
2174
2175
if( features.overviewTransitions ) {
2176
setTimeout( function() {
2177
dom.wrapper.classList.add( 'overview-animated' );
2178
}, 1 );
2179
}
2180
2181
// Don't auto-slide while in overview mode
2182
cancelAutoSlide();
2183
2184
// Move the backgrounds element into the slide container to
2185
// that the same scaling is applied
2186
dom.slides.appendChild( dom.background );
2187
2188
// Clicking on an overview slide navigates to it
2189
toArray( dom.wrapper.querySelectorAll( SLIDES_SELECTOR ) ).forEach( function( slide ) {
2190
if( !slide.classList.contains( 'stack' ) ) {
2191
slide.addEventListener( 'click', onOverviewSlideClicked, true );
2192
}
2193
} );
2194
2195
// Calculate slide sizes
2196
var margin = 70;
2197
var slideSize = getComputedSlideSize();
2198
overviewSlideWidth = slideSize.width + margin;
2199
overviewSlideHeight = slideSize.height + margin;
2200
2201
// Reverse in RTL mode
2202
if( config.rtl ) {
2203
overviewSlideWidth = -overviewSlideWidth;
2204
}
2205
2206
updateSlidesVisibility();
2207
layoutOverview();
2208
updateOverview();
2209
2210
layout();
2211
2212
// Notify observers of the overview showing
2213
dispatchEvent( 'overviewshown', {
2214
'indexh': indexh,
2215
'indexv': indexv,
2216
'currentSlide': currentSlide
2217
} );
2218
2219
}
2220
2221
}
2222
2223
/**
2224
* Uses CSS transforms to position all slides in a grid for
2225
* display inside of the overview mode.
2226
*/
2227
function layoutOverview() {
2228
2229
// Layout slides
2230
toArray( dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) ).forEach( function( hslide, h ) {
2231
hslide.setAttribute( 'data-index-h', h );
2232
transformElement( hslide, 'translate3d(' + ( h * overviewSlideWidth ) + 'px, 0, 0)' );
2233
2234
if( hslide.classList.contains( 'stack' ) ) {
2235
2236
toArray( hslide.querySelectorAll( 'section' ) ).forEach( function( vslide, v ) {
2237
vslide.setAttribute( 'data-index-h', h );
2238
vslide.setAttribute( 'data-index-v', v );
2239
2240
transformElement( vslide, 'translate3d(0, ' + ( v * overviewSlideHeight ) + 'px, 0)' );
2241
} );
2242
2243
}
2244
} );
2245
2246
// Layout slide backgrounds
2247
toArray( dom.background.childNodes ).forEach( function( hbackground, h ) {
2248
transformElement( hbackground, 'translate3d(' + ( h * overviewSlideWidth ) + 'px, 0, 0)' );
2249
2250
toArray( hbackground.querySelectorAll( '.slide-background' ) ).forEach( function( vbackground, v ) {
2251
transformElement( vbackground, 'translate3d(0, ' + ( v * overviewSlideHeight ) + 'px, 0)' );
2252
} );
2253
} );
2254
2255
}
2256
2257
/**
2258
* Moves the overview viewport to the current slides.
2259
* Called each time the current slide changes.
2260
*/
2261
function updateOverview() {
2262
2263
var vmin = Math.min( window.innerWidth, window.innerHeight );
2264
var scale = Math.max( vmin / 5, 150 ) / vmin;
2265
2266
transformSlides( {
2267
overview: [
2268
'scale('+ scale +')',
2269
'translateX('+ ( -indexh * overviewSlideWidth ) +'px)',
2270
'translateY('+ ( -indexv * overviewSlideHeight ) +'px)'
2271
].join( ' ' )
2272
} );
2273
2274
}
2275
2276
/**
2277
* Exits the slide overview and enters the currently
2278
* active slide.
2279
*/
2280
function deactivateOverview() {
2281
2282
// Only proceed if enabled in config
2283
if( config.overview ) {
2284
2285
overview = false;
2286
2287
dom.wrapper.classList.remove( 'overview' );
2288
dom.wrapper.classList.remove( 'overview-animated' );
2289
2290
// Temporarily add a class so that transitions can do different things
2291
// depending on whether they are exiting/entering overview, or just
2292
// moving from slide to slide
2293
dom.wrapper.classList.add( 'overview-deactivating' );
2294
2295
setTimeout( function () {
2296
dom.wrapper.classList.remove( 'overview-deactivating' );
2297
}, 1 );
2298
2299
// Move the background element back out
2300
dom.wrapper.appendChild( dom.background );
2301
2302
// Clean up changes made to slides
2303
toArray( dom.wrapper.querySelectorAll( SLIDES_SELECTOR ) ).forEach( function( slide ) {
2304
transformElement( slide, '' );
2305
2306
slide.removeEventListener( 'click', onOverviewSlideClicked, true );
2307
} );
2308
2309
// Clean up changes made to backgrounds
2310
toArray( dom.background.querySelectorAll( '.slide-background' ) ).forEach( function( background ) {
2311
transformElement( background, '' );
2312
} );
2313
2314
transformSlides( { overview: '' } );
2315
2316
slide( indexh, indexv );
2317
2318
layout();
2319
2320
cueAutoSlide();
2321
2322
// Notify observers of the overview hiding
2323
dispatchEvent( 'overviewhidden', {
2324
'indexh': indexh,
2325
'indexv': indexv,
2326
'currentSlide': currentSlide
2327
} );
2328
2329
}
2330
}
2331
2332
/**
2333
* Toggles the slide overview mode on and off.
2334
*
2335
* @param {Boolean} [override] Flag which overrides the
2336
* toggle logic and forcibly sets the desired state. True means
2337
* overview is open, false means it's closed.
2338
*/
2339
function toggleOverview( override ) {
2340
2341
if( typeof override === 'boolean' ) {
2342
override ? activateOverview() : deactivateOverview();
2343
}
2344
else {
2345
isOverview() ? deactivateOverview() : activateOverview();
2346
}
2347
2348
}
2349
2350
/**
2351
* Checks if the overview is currently active.
2352
*
2353
* @return {Boolean} true if the overview is active,
2354
* false otherwise
2355
*/
2356
function isOverview() {
2357
2358
return overview;
2359
2360
}
2361
2362
/**
2363
* Return a hash URL that will resolve to the current slide location.
2364
*/
2365
function locationHash() {
2366
2367
var url = '/';
2368
2369
// Attempt to create a named link based on the slide's ID
2370
var id = currentSlide ? currentSlide.getAttribute( 'id' ) : null;
2371
if( id ) {
2372
id = encodeURIComponent( id );
2373
}
2374
2375
var indexf;
2376
if( config.fragmentInURL ) {
2377
indexf = getIndices().f;
2378
}
2379
2380
// If the current slide has an ID, use that as a named link,
2381
// but we don't support named links with a fragment index
2382
if( typeof id === 'string' && id.length && indexf === undefined ) {
2383
url = '/' + id;
2384
}
2385
// Otherwise use the /h/v index
2386
else {
2387
var hashIndexBase = config.hashOneBasedIndex ? 1 : 0;
2388
if( indexh > 0 || indexv > 0 || indexf !== undefined ) url += indexh + hashIndexBase;
2389
if( indexv > 0 || indexf !== undefined ) url += '/' + (indexv + hashIndexBase );
2390
if( indexf !== undefined ) url += '/' + indexf;
2391
}
2392
2393
return url;
2394
2395
}
2396
2397
/**
2398
* Checks if the current or specified slide is vertical
2399
* (nested within another slide).
2400
*
2401
* @param {HTMLElement} [slide=currentSlide] The slide to check
2402
* orientation of
2403
* @return {Boolean}
2404
*/
2405
function isVerticalSlide( slide ) {
2406
2407
// Prefer slide argument, otherwise use current slide
2408
slide = slide ? slide : currentSlide;
2409
2410
return slide && slide.parentNode && !!slide.parentNode.nodeName.match( /section/i );
2411
2412
}
2413
2414
/**
2415
* Handling the fullscreen functionality via the fullscreen API
2416
*
2417
* @see http://fullscreen.spec.whatwg.org/
2418
* @see https://developer.mozilla.org/en-US/docs/DOM/Using_fullscreen_mode
2419
*/
2420
function enterFullscreen() {
2421
2422
var element = document.documentElement;
2423
2424
// Check which implementation is available
2425
var requestMethod = element.requestFullscreen ||
2426
element.webkitRequestFullscreen ||
2427
element.webkitRequestFullScreen ||
2428
element.mozRequestFullScreen ||
2429
element.msRequestFullscreen;
2430
2431
if( requestMethod ) {
2432
requestMethod.apply( element );
2433
}
2434
2435
}
2436
2437
/**
2438
* Enters the paused mode which fades everything on screen to
2439
* black.
2440
*/
2441
function pause() {
2442
2443
if( config.pause ) {
2444
var wasPaused = dom.wrapper.classList.contains( 'paused' );
2445
2446
cancelAutoSlide();
2447
dom.wrapper.classList.add( 'paused' );
2448
2449
if( wasPaused === false ) {
2450
dispatchEvent( 'paused' );
2451
}
2452
}
2453
2454
}
2455
2456
/**
2457
* Exits from the paused mode.
2458
*/
2459
function resume() {
2460
2461
var wasPaused = dom.wrapper.classList.contains( 'paused' );
2462
dom.wrapper.classList.remove( 'paused' );
2463
2464
cueAutoSlide();
2465
2466
if( wasPaused ) {
2467
dispatchEvent( 'resumed' );
2468
}
2469
2470
}
2471
2472
/**
2473
* Toggles the paused mode on and off.
2474
*/
2475
function togglePause( override ) {
2476
2477
if( typeof override === 'boolean' ) {
2478
override ? pause() : resume();
2479
}
2480
else {
2481
isPaused() ? resume() : pause();
2482
}
2483
2484
}
2485
2486
/**
2487
* Checks if we are currently in the paused mode.
2488
*
2489
* @return {Boolean}
2490
*/
2491
function isPaused() {
2492
2493
return dom.wrapper.classList.contains( 'paused' );
2494
2495
}
2496
2497
/**
2498
* Toggles the auto slide mode on and off.
2499
*
2500
* @param {Boolean} [override] Flag which sets the desired state.
2501
* True means autoplay starts, false means it stops.
2502
*/
2503
2504
function toggleAutoSlide( override ) {
2505
2506
if( typeof override === 'boolean' ) {
2507
override ? resumeAutoSlide() : pauseAutoSlide();
2508
}
2509
2510
else {
2511
autoSlidePaused ? resumeAutoSlide() : pauseAutoSlide();
2512
}
2513
2514
}
2515
2516
/**
2517
* Checks if the auto slide mode is currently on.
2518
*
2519
* @return {Boolean}
2520
*/
2521
function isAutoSliding() {
2522
2523
return !!( autoSlide && !autoSlidePaused );
2524
2525
}
2526
2527
/**
2528
* Steps from the current point in the presentation to the
2529
* slide which matches the specified horizontal and vertical
2530
* indices.
2531
*
2532
* @param {number} [h=indexh] Horizontal index of the target slide
2533
* @param {number} [v=indexv] Vertical index of the target slide
2534
* @param {number} [f] Index of a fragment within the
2535
* target slide to activate
2536
* @param {number} [o] Origin for use in multimaster environments
2537
*/
2538
function slide( h, v, f, o ) {
2539
2540
// Remember where we were at before
2541
previousSlide = currentSlide;
2542
2543
// Query all horizontal slides in the deck
2544
var horizontalSlides = dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR );
2545
2546
// Abort if there are no slides
2547
if( horizontalSlides.length === 0 ) return;
2548
2549
// If no vertical index is specified and the upcoming slide is a
2550
// stack, resume at its previous vertical index
2551
if( v === undefined && !isOverview() ) {
2552
v = getPreviousVerticalIndex( horizontalSlides[ h ] );
2553
}
2554
2555
// If we were on a vertical stack, remember what vertical index
2556
// it was on so we can resume at the same position when returning
2557
if( previousSlide && previousSlide.parentNode && previousSlide.parentNode.classList.contains( 'stack' ) ) {
2558
setPreviousVerticalIndex( previousSlide.parentNode, indexv );
2559
}
2560
2561
// Remember the state before this slide
2562
var stateBefore = state.concat();
2563
2564
// Reset the state array
2565
state.length = 0;
2566
2567
var indexhBefore = indexh || 0,
2568
indexvBefore = indexv || 0;
2569
2570
// Activate and transition to the new slide
2571
indexh = updateSlides( HORIZONTAL_SLIDES_SELECTOR, h === undefined ? indexh : h );
2572
indexv = updateSlides( VERTICAL_SLIDES_SELECTOR, v === undefined ? indexv : v );
2573
2574
// Update the visibility of slides now that the indices have changed
2575
updateSlidesVisibility();
2576
2577
layout();
2578
2579
// Apply the new state
2580
stateLoop: for( var i = 0, len = state.length; i < len; i++ ) {
2581
// Check if this state existed on the previous slide. If it
2582
// did, we will avoid adding it repeatedly
2583
for( var j = 0; j < stateBefore.length; j++ ) {
2584
if( stateBefore[j] === state[i] ) {
2585
stateBefore.splice( j, 1 );
2586
continue stateLoop;
2587
}
2588
}
2589
2590
document.documentElement.classList.add( state[i] );
2591
2592
// Dispatch custom event matching the state's name
2593
dispatchEvent( state[i] );
2594
}
2595
2596
// Clean up the remains of the previous state
2597
while( stateBefore.length ) {
2598
document.documentElement.classList.remove( stateBefore.pop() );
2599
}
2600
2601
// Update the overview if it's currently active
2602
if( isOverview() ) {
2603
updateOverview();
2604
}
2605
2606
// Find the current horizontal slide and any possible vertical slides
2607
// within it
2608
var currentHorizontalSlide = horizontalSlides[ indexh ],
2609
currentVerticalSlides = currentHorizontalSlide.querySelectorAll( 'section' );
2610
2611
// Store references to the previous and current slides
2612
currentSlide = currentVerticalSlides[ indexv ] || currentHorizontalSlide;
2613
2614
// Show fragment, if specified
2615
if( typeof f !== 'undefined' ) {
2616
navigateFragment( f );
2617
}
2618
2619
// Dispatch an event if the slide changed
2620
var slideChanged = ( indexh !== indexhBefore || indexv !== indexvBefore );
2621
if (!slideChanged) {
2622
// Ensure that the previous slide is never the same as the current
2623
previousSlide = null;
2624
}
2625
2626
// Solves an edge case where the previous slide maintains the
2627
// 'present' class when navigating between adjacent vertical
2628
// stacks
2629
if( previousSlide && previousSlide !== currentSlide ) {
2630
previousSlide.classList.remove( 'present' );
2631
previousSlide.setAttribute( 'aria-hidden', 'true' );
2632
2633
// Reset all slides upon navigate to home
2634
// Issue: #285
2635
if ( dom.wrapper.querySelector( HOME_SLIDE_SELECTOR ).classList.contains( 'present' ) ) {
2636
// Launch async task
2637
setTimeout( function () {
2638
var slides = toArray( dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR + '.stack') ), i;
2639
for( i in slides ) {
2640
if( slides[i] ) {
2641
// Reset stack
2642
setPreviousVerticalIndex( slides[i], 0 );
2643
}
2644
}
2645
}, 0 );
2646
}
2647
}
2648
2649
if( slideChanged ) {
2650
dispatchEvent( 'slidechanged', {
2651
'indexh': indexh,
2652
'indexv': indexv,
2653
'previousSlide': previousSlide,
2654
'currentSlide': currentSlide,
2655
'origin': o
2656
} );
2657
}
2658
2659
// Handle embedded content
2660
if( slideChanged || !previousSlide ) {
2661
stopEmbeddedContent( previousSlide );
2662
startEmbeddedContent( currentSlide );
2663
}
2664
2665
// Announce the current slide contents, for screen readers
2666
dom.statusDiv.textContent = getStatusText( currentSlide );
2667
2668
updateControls();
2669
updateProgress();
2670
updateBackground();
2671
updateParallax();
2672
updateSlideNumber();
2673
updateNotes();
2674
2675
// Update the URL hash
2676
writeURL();
2677
2678
cueAutoSlide();
2679
2680
}
2681
2682
/**
2683
* Syncs the presentation with the current DOM. Useful
2684
* when new slides or control elements are added or when
2685
* the configuration has changed.
2686
*/
2687
function sync() {
2688
2689
// Subscribe to input
2690
removeEventListeners();
2691
addEventListeners();
2692
2693
// Force a layout to make sure the current config is accounted for
2694
layout();
2695
2696
// Reflect the current autoSlide value
2697
autoSlide = config.autoSlide;
2698
2699
// Start auto-sliding if it's enabled
2700
cueAutoSlide();
2701
2702
// Re-create the slide backgrounds
2703
createBackgrounds();
2704
2705
// Write the current hash to the URL
2706
writeURL();
2707
2708
sortAllFragments();
2709
2710
updateControls();
2711
updateProgress();
2712
updateSlideNumber();
2713
updateSlidesVisibility();
2714
updateBackground( true );
2715
updateNotesVisibility();
2716
updateNotes();
2717
2718
formatEmbeddedContent();
2719
2720
// Start or stop embedded content depending on global config
2721
if( config.autoPlayMedia === false ) {
2722
stopEmbeddedContent( currentSlide, { unloadIframes: false } );
2723
}
2724
else {
2725
startEmbeddedContent( currentSlide );
2726
}
2727
2728
if( isOverview() ) {
2729
layoutOverview();
2730
}
2731
2732
}
2733
2734
/**
2735
* Updates reveal.js to keep in sync with new slide attributes. For
2736
* example, if you add a new `data-background-image` you can call
2737
* this to have reveal.js render the new background image.
2738
*
2739
* Similar to #sync() but more efficient when you only need to
2740
* refresh a specific slide.
2741
*
2742
* @param {HTMLElement} slide
2743
*/
2744
function syncSlide( slide ) {
2745
2746
syncBackground( slide );
2747
syncFragments( slide );
2748
2749
updateBackground();
2750
updateNotes();
2751
2752
loadSlide( slide );
2753
2754
}
2755
2756
/**
2757
* Formats the fragments on the given slide so that they have
2758
* valid indices. Call this if fragments are changed in the DOM
2759
* after reveal.js has already initialized.
2760
*
2761
* @param {HTMLElement} slide
2762
*/
2763
function syncFragments( slide ) {
2764
2765
sortFragments( slide.querySelectorAll( '.fragment' ) );
2766
2767
}
2768
2769
/**
2770
* Resets all vertical slides so that only the first
2771
* is visible.
2772
*/
2773
function resetVerticalSlides() {
2774
2775
var horizontalSlides = toArray( dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) );
2776
horizontalSlides.forEach( function( horizontalSlide ) {
2777
2778
var verticalSlides = toArray( horizontalSlide.querySelectorAll( 'section' ) );
2779
verticalSlides.forEach( function( verticalSlide, y ) {
2780
2781
if( y > 0 ) {
2782
verticalSlide.classList.remove( 'present' );
2783
verticalSlide.classList.remove( 'past' );
2784
verticalSlide.classList.add( 'future' );
2785
verticalSlide.setAttribute( 'aria-hidden', 'true' );
2786
}
2787
2788
} );
2789
2790
} );
2791
2792
}
2793
2794
/**
2795
* Sorts and formats all of fragments in the
2796
* presentation.
2797
*/
2798
function sortAllFragments() {
2799
2800
var horizontalSlides = toArray( dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) );
2801
horizontalSlides.forEach( function( horizontalSlide ) {
2802
2803
var verticalSlides = toArray( horizontalSlide.querySelectorAll( 'section' ) );
2804
verticalSlides.forEach( function( verticalSlide, y ) {
2805
2806
sortFragments( verticalSlide.querySelectorAll( '.fragment' ) );
2807
2808
} );
2809
2810
if( verticalSlides.length === 0 ) sortFragments( horizontalSlide.querySelectorAll( '.fragment' ) );
2811
2812
} );
2813
2814
}
2815
2816
/**
2817
* Randomly shuffles all slides in the deck.
2818
*/
2819
function shuffle() {
2820
2821
var slides = toArray( dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) );
2822
2823
slides.forEach( function( slide ) {
2824
2825
// Insert this slide next to another random slide. This may
2826
// cause the slide to insert before itself but that's fine.
2827
dom.slides.insertBefore( slide, slides[ Math.floor( Math.random() * slides.length ) ] );
2828
2829
} );
2830
2831
}
2832
2833
/**
2834
* Updates one dimension of slides by showing the slide
2835
* with the specified index.
2836
*
2837
* @param {string} selector A CSS selector that will fetch
2838
* the group of slides we are working with
2839
* @param {number} index The index of the slide that should be
2840
* shown
2841
*
2842
* @return {number} The index of the slide that is now shown,
2843
* might differ from the passed in index if it was out of
2844
* bounds.
2845
*/
2846
function updateSlides( selector, index ) {
2847
2848
// Select all slides and convert the NodeList result to
2849
// an array
2850
var slides = toArray( dom.wrapper.querySelectorAll( selector ) ),
2851
slidesLength = slides.length;
2852
2853
var printMode = isPrintingPDF();
2854
2855
if( slidesLength ) {
2856
2857
// Should the index loop?
2858
if( config.loop ) {
2859
index %= slidesLength;
2860
2861
if( index < 0 ) {
2862
index = slidesLength + index;
2863
}
2864
}
2865
2866
// Enforce max and minimum index bounds
2867
index = Math.max( Math.min( index, slidesLength - 1 ), 0 );
2868
2869
for( var i = 0; i < slidesLength; i++ ) {
2870
var element = slides[i];
2871
2872
var reverse = config.rtl && !isVerticalSlide( element );
2873
2874
element.classList.remove( 'past' );
2875
element.classList.remove( 'present' );
2876
element.classList.remove( 'future' );
2877
2878
// http://www.w3.org/html/wg/drafts/html/master/editing.html#the-hidden-attribute
2879
element.setAttribute( 'hidden', '' );
2880
element.setAttribute( 'aria-hidden', 'true' );
2881
2882
// If this element contains vertical slides
2883
if( element.querySelector( 'section' ) ) {
2884
element.classList.add( 'stack' );
2885
}
2886
2887
// If we're printing static slides, all slides are "present"
2888
if( printMode ) {
2889
element.classList.add( 'present' );
2890
continue;
2891
}
2892
2893
if( i < index ) {
2894
// Any element previous to index is given the 'past' class
2895
element.classList.add( reverse ? 'future' : 'past' );
2896
2897
if( config.fragments ) {
2898
var pastFragments = toArray( element.querySelectorAll( '.fragment' ) );
2899
2900
// Show all fragments on prior slides
2901
while( pastFragments.length ) {
2902
var pastFragment = pastFragments.pop();
2903
pastFragment.classList.add( 'visible' );
2904
pastFragment.classList.remove( 'current-fragment' );
2905
}
2906
}
2907
}
2908
else if( i > index ) {
2909
// Any element subsequent to index is given the 'future' class
2910
element.classList.add( reverse ? 'past' : 'future' );
2911
2912
if( config.fragments ) {
2913
var futureFragments = toArray( element.querySelectorAll( '.fragment.visible' ) );
2914
2915
// No fragments in future slides should be visible ahead of time
2916
while( futureFragments.length ) {
2917
var futureFragment = futureFragments.pop();
2918
futureFragment.classList.remove( 'visible' );
2919
futureFragment.classList.remove( 'current-fragment' );
2920
}
2921
}
2922
}
2923
}
2924
2925
// Mark the current slide as present
2926
slides[index].classList.add( 'present' );
2927
slides[index].removeAttribute( 'hidden' );
2928
slides[index].removeAttribute( 'aria-hidden' );
2929
2930
// If this slide has a state associated with it, add it
2931
// onto the current state of the deck
2932
var slideState = slides[index].getAttribute( 'data-state' );
2933
if( slideState ) {
2934
state = state.concat( slideState.split( ' ' ) );
2935
}
2936
2937
}
2938
else {
2939
// Since there are no slides we can't be anywhere beyond the
2940
// zeroth index
2941
index = 0;
2942
}
2943
2944
return index;
2945
2946
}
2947
2948
/**
2949
* Optimization method; hide all slides that are far away
2950
* from the present slide.
2951
*/
2952
function updateSlidesVisibility() {
2953
2954
// Select all slides and convert the NodeList result to
2955
// an array
2956
var horizontalSlides = toArray( dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) ),
2957
horizontalSlidesLength = horizontalSlides.length,
2958
distanceX,
2959
distanceY;
2960
2961
if( horizontalSlidesLength && typeof indexh !== 'undefined' ) {
2962
2963
// The number of steps away from the present slide that will
2964
// be visible
2965
var viewDistance = isOverview() ? 10 : config.viewDistance;
2966
2967
// Limit view distance on weaker devices
2968
if( isMobileDevice ) {
2969
viewDistance = isOverview() ? 6 : 2;
2970
}
2971
2972
// All slides need to be visible when exporting to PDF
2973
if( isPrintingPDF() ) {
2974
viewDistance = Number.MAX_VALUE;
2975
}
2976
2977
for( var x = 0; x < horizontalSlidesLength; x++ ) {
2978
var horizontalSlide = horizontalSlides[x];
2979
2980
var verticalSlides = toArray( horizontalSlide.querySelectorAll( 'section' ) ),
2981
verticalSlidesLength = verticalSlides.length;
2982
2983
// Determine how far away this slide is from the present
2984
distanceX = Math.abs( ( indexh || 0 ) - x ) || 0;
2985
2986
// If the presentation is looped, distance should measure
2987
// 1 between the first and last slides
2988
if( config.loop ) {
2989
distanceX = Math.abs( ( ( indexh || 0 ) - x ) % ( horizontalSlidesLength - viewDistance ) ) || 0;
2990
}
2991
2992
// Show the horizontal slide if it's within the view distance
2993
if( distanceX < viewDistance ) {
2994
loadSlide( horizontalSlide );
2995
}
2996
else {
2997
unloadSlide( horizontalSlide );
2998
}
2999
3000
if( verticalSlidesLength ) {
3001
3002
var oy = getPreviousVerticalIndex( horizontalSlide );
3003
3004
for( var y = 0; y < verticalSlidesLength; y++ ) {
3005
var verticalSlide = verticalSlides[y];
3006
3007
distanceY = x === ( indexh || 0 ) ? Math.abs( ( indexv || 0 ) - y ) : Math.abs( y - oy );
3008
3009
if( distanceX + distanceY < viewDistance ) {
3010
loadSlide( verticalSlide );
3011
}
3012
else {
3013
unloadSlide( verticalSlide );
3014
}
3015
}
3016
3017
}
3018
}
3019
3020
// Flag if there are ANY vertical slides, anywhere in the deck
3021
if( dom.wrapper.querySelectorAll( '.slides>section>section' ).length ) {
3022
dom.wrapper.classList.add( 'has-vertical-slides' );
3023
}
3024
else {
3025
dom.wrapper.classList.remove( 'has-vertical-slides' );
3026
}
3027
3028
// Flag if there are ANY horizontal slides, anywhere in the deck
3029
if( dom.wrapper.querySelectorAll( '.slides>section' ).length > 1 ) {
3030
dom.wrapper.classList.add( 'has-horizontal-slides' );
3031
}
3032
else {
3033
dom.wrapper.classList.remove( 'has-horizontal-slides' );
3034
}
3035
3036
}
3037
3038
}
3039
3040
/**
3041
* Pick up notes from the current slide and display them
3042
* to the viewer.
3043
*
3044
* @see {@link config.showNotes}
3045
*/
3046
function updateNotes() {
3047
3048
if( config.showNotes && dom.speakerNotes && currentSlide && !isPrintingPDF() ) {
3049
3050
dom.speakerNotes.innerHTML = getSlideNotes() || '<span class="notes-placeholder">No notes on this slide.</span>';
3051
3052
}
3053
3054
}
3055
3056
/**
3057
* Updates the visibility of the speaker notes sidebar that
3058
* is used to share annotated slides. The notes sidebar is
3059
* only visible if showNotes is true and there are notes on
3060
* one or more slides in the deck.
3061
*/
3062
function updateNotesVisibility() {
3063
3064
if( config.showNotes && hasNotes() ) {
3065
dom.wrapper.classList.add( 'show-notes' );
3066
}
3067
else {
3068
dom.wrapper.classList.remove( 'show-notes' );
3069
}
3070
3071
}
3072
3073
/**
3074
* Checks if there are speaker notes for ANY slide in the
3075
* presentation.
3076
*/
3077
function hasNotes() {
3078
3079
return dom.slides.querySelectorAll( '[data-notes], aside.notes' ).length > 0;
3080
3081
}
3082
3083
/**
3084
* Updates the progress bar to reflect the current slide.
3085
*/
3086
function updateProgress() {
3087
3088
// Update progress if enabled
3089
if( config.progress && dom.progressbar ) {
3090
3091
dom.progressbar.style.width = getProgress() * dom.wrapper.offsetWidth + 'px';
3092
3093
}
3094
3095
}
3096
3097
3098
/**
3099
* Updates the slide number div to reflect the current slide.
3100
*
3101
* The following slide number formats are available:
3102
* "h.v": horizontal . vertical slide number (default)
3103
* "h/v": horizontal / vertical slide number
3104
* "c": flattened slide number
3105
* "c/t": flattened slide number / total slides
3106
*/
3107
function updateSlideNumber() {
3108
3109
// Update slide number if enabled
3110
if( config.slideNumber && dom.slideNumber ) {
3111
3112
var value = [];
3113
var format = 'h.v';
3114
3115
// Check if a custom number format is available
3116
if( typeof config.slideNumber === 'string' ) {
3117
format = config.slideNumber;
3118
}
3119
3120
// If there are ONLY vertical slides in this deck, always use
3121
// a flattened slide number
3122
if( !/c/.test( format ) && dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ).length === 1 ) {
3123
format = 'c';
3124
}
3125
3126
switch( format ) {
3127
case 'c':
3128
value.push( getSlidePastCount() + 1 );
3129
break;
3130
case 'c/t':
3131
value.push( getSlidePastCount() + 1, '/', getTotalSlides() );
3132
break;
3133
case 'h/v':
3134
value.push( indexh + 1 );
3135
if( isVerticalSlide() ) value.push( '/', indexv + 1 );
3136
break;
3137
default:
3138
value.push( indexh + 1 );
3139
if( isVerticalSlide() ) value.push( '.', indexv + 1 );
3140
}
3141
3142
dom.slideNumber.innerHTML = formatSlideNumber( value[0], value[1], value[2] );
3143
}
3144
3145
}
3146
3147
/**
3148
* Applies HTML formatting to a slide number before it's
3149
* written to the DOM.
3150
*
3151
* @param {number} a Current slide
3152
* @param {string} delimiter Character to separate slide numbers
3153
* @param {(number|*)} b Total slides
3154
* @return {string} HTML string fragment
3155
*/
3156
function formatSlideNumber( a, delimiter, b ) {
3157
3158
var url = '#' + locationHash();
3159
if( typeof b === 'number' && !isNaN( b ) ) {
3160
return '<a href="' + url + '">' +
3161
'<span class="slide-number-a">'+ a +'</span>' +
3162
'<span class="slide-number-delimiter">'+ delimiter +'</span>' +
3163
'<span class="slide-number-b">'+ b +'</span>' +
3164
'</a>';
3165
}
3166
else {
3167
return '<a href="' + url + '">' +
3168
'<span class="slide-number-a">'+ a +'</span>' +
3169
'</a>';
3170
}
3171
3172
}
3173
3174
/**
3175
* Updates the state of all control/navigation arrows.
3176
*/
3177
function updateControls() {
3178
3179
var routes = availableRoutes();
3180
var fragments = availableFragments();
3181
3182
// Remove the 'enabled' class from all directions
3183
dom.controlsLeft.concat( dom.controlsRight )
3184
.concat( dom.controlsUp )
3185
.concat( dom.controlsDown )
3186
.concat( dom.controlsPrev )
3187
.concat( dom.controlsNext ).forEach( function( node ) {
3188
node.classList.remove( 'enabled' );
3189
node.classList.remove( 'fragmented' );
3190
3191
// Set 'disabled' attribute on all directions
3192
node.setAttribute( 'disabled', 'disabled' );
3193
} );
3194
3195
// Add the 'enabled' class to the available routes; remove 'disabled' attribute to enable buttons
3196
if( routes.left ) dom.controlsLeft.forEach( function( el ) { el.classList.add( 'enabled' ); el.removeAttribute( 'disabled' ); } );
3197
if( routes.right ) dom.controlsRight.forEach( function( el ) { el.classList.add( 'enabled' ); el.removeAttribute( 'disabled' ); } );
3198
if( routes.up ) dom.controlsUp.forEach( function( el ) { el.classList.add( 'enabled' ); el.removeAttribute( 'disabled' ); } );
3199
if( routes.down ) dom.controlsDown.forEach( function( el ) { el.classList.add( 'enabled' ); el.removeAttribute( 'disabled' ); } );
3200
3201
// Prev/next buttons
3202
if( routes.left || routes.up ) dom.controlsPrev.forEach( function( el ) { el.classList.add( 'enabled' ); el.removeAttribute( 'disabled' ); } );
3203
if( routes.right || routes.down ) dom.controlsNext.forEach( function( el ) { el.classList.add( 'enabled' ); el.removeAttribute( 'disabled' ); } );
3204
3205
// Highlight fragment directions
3206
if( currentSlide ) {
3207
3208
// Always apply fragment decorator to prev/next buttons
3209
if( fragments.prev ) dom.controlsPrev.forEach( function( el ) { el.classList.add( 'fragmented', 'enabled' ); el.removeAttribute( 'disabled' ); } );
3210
if( fragments.next ) dom.controlsNext.forEach( function( el ) { el.classList.add( 'fragmented', 'enabled' ); el.removeAttribute( 'disabled' ); } );
3211
3212
// Apply fragment decorators to directional buttons based on
3213
// what slide axis they are in
3214
if( isVerticalSlide( currentSlide ) ) {
3215
if( fragments.prev ) dom.controlsUp.forEach( function( el ) { el.classList.add( 'fragmented', 'enabled' ); el.removeAttribute( 'disabled' ); } );
3216
if( fragments.next ) dom.controlsDown.forEach( function( el ) { el.classList.add( 'fragmented', 'enabled' ); el.removeAttribute( 'disabled' ); } );
3217
}
3218
else {
3219
if( fragments.prev ) dom.controlsLeft.forEach( function( el ) { el.classList.add( 'fragmented', 'enabled' ); el.removeAttribute( 'disabled' ); } );
3220
if( fragments.next ) dom.controlsRight.forEach( function( el ) { el.classList.add( 'fragmented', 'enabled' ); el.removeAttribute( 'disabled' ); } );
3221
}
3222
3223
}
3224
3225
if( config.controlsTutorial ) {
3226
3227
// Highlight control arrows with an animation to ensure
3228
// that the viewer knows how to navigate
3229
if( !hasNavigatedDown && routes.down ) {
3230
dom.controlsDownArrow.classList.add( 'highlight' );
3231
}
3232
else {
3233
dom.controlsDownArrow.classList.remove( 'highlight' );
3234
3235
if( !hasNavigatedRight && routes.right && indexv === 0 ) {
3236
dom.controlsRightArrow.classList.add( 'highlight' );
3237
}
3238
else {
3239
dom.controlsRightArrow.classList.remove( 'highlight' );
3240
}
3241
}
3242
3243
}
3244
3245
}
3246
3247
/**
3248
* Updates the background elements to reflect the current
3249
* slide.
3250
*
3251
* @param {boolean} includeAll If true, the backgrounds of
3252
* all vertical slides (not just the present) will be updated.
3253
*/
3254
function updateBackground( includeAll ) {
3255
3256
var currentBackground = null;
3257
3258
// Reverse past/future classes when in RTL mode
3259
var horizontalPast = config.rtl ? 'future' : 'past',
3260
horizontalFuture = config.rtl ? 'past' : 'future';
3261
3262
// Update the classes of all backgrounds to match the
3263
// states of their slides (past/present/future)
3264
toArray( dom.background.childNodes ).forEach( function( backgroundh, h ) {
3265
3266
backgroundh.classList.remove( 'past' );
3267
backgroundh.classList.remove( 'present' );
3268
backgroundh.classList.remove( 'future' );
3269
3270
if( h < indexh ) {
3271
backgroundh.classList.add( horizontalPast );
3272
}
3273
else if ( h > indexh ) {
3274
backgroundh.classList.add( horizontalFuture );
3275
}
3276
else {
3277
backgroundh.classList.add( 'present' );
3278
3279
// Store a reference to the current background element
3280
currentBackground = backgroundh;
3281
}
3282
3283
if( includeAll || h === indexh ) {
3284
toArray( backgroundh.querySelectorAll( '.slide-background' ) ).forEach( function( backgroundv, v ) {
3285
3286
backgroundv.classList.remove( 'past' );
3287
backgroundv.classList.remove( 'present' );
3288
backgroundv.classList.remove( 'future' );
3289
3290
if( v < indexv ) {
3291
backgroundv.classList.add( 'past' );
3292
}
3293
else if ( v > indexv ) {
3294
backgroundv.classList.add( 'future' );
3295
}
3296
else {
3297
backgroundv.classList.add( 'present' );
3298
3299
// Only if this is the present horizontal and vertical slide
3300
if( h === indexh ) currentBackground = backgroundv;
3301
}
3302
3303
} );
3304
}
3305
3306
} );
3307
3308
// Stop content inside of previous backgrounds
3309
if( previousBackground ) {
3310
3311
stopEmbeddedContent( previousBackground );
3312
3313
}
3314
3315
// Start content in the current background
3316
if( currentBackground ) {
3317
3318
startEmbeddedContent( currentBackground );
3319
3320
var currentBackgroundContent = currentBackground.querySelector( '.slide-background-content' );
3321
if( currentBackgroundContent ) {
3322
3323
var backgroundImageURL = currentBackgroundContent.style.backgroundImage || '';
3324
3325
// Restart GIFs (doesn't work in Firefox)
3326
if( /\.gif/i.test( backgroundImageURL ) ) {
3327
currentBackgroundContent.style.backgroundImage = '';
3328
window.getComputedStyle( currentBackgroundContent ).opacity;
3329
currentBackgroundContent.style.backgroundImage = backgroundImageURL;
3330
}
3331
3332
}
3333
3334
// Don't transition between identical backgrounds. This
3335
// prevents unwanted flicker.
3336
var previousBackgroundHash = previousBackground ? previousBackground.getAttribute( 'data-background-hash' ) : null;
3337
var currentBackgroundHash = currentBackground.getAttribute( 'data-background-hash' );
3338
if( currentBackgroundHash && currentBackgroundHash === previousBackgroundHash && currentBackground !== previousBackground ) {
3339
dom.background.classList.add( 'no-transition' );
3340
}
3341
3342
previousBackground = currentBackground;
3343
3344
}
3345
3346
// If there's a background brightness flag for this slide,
3347
// bubble it to the .reveal container
3348
if( currentSlide ) {
3349
[ 'has-light-background', 'has-dark-background' ].forEach( function( classToBubble ) {
3350
if( currentSlide.classList.contains( classToBubble ) ) {
3351
dom.wrapper.classList.add( classToBubble );
3352
}
3353
else {
3354
dom.wrapper.classList.remove( classToBubble );
3355
}
3356
} );
3357
}
3358
3359
// Allow the first background to apply without transition
3360
setTimeout( function() {
3361
dom.background.classList.remove( 'no-transition' );
3362
}, 1 );
3363
3364
}
3365
3366
/**
3367
* Updates the position of the parallax background based
3368
* on the current slide index.
3369
*/
3370
function updateParallax() {
3371
3372
if( config.parallaxBackgroundImage ) {
3373
3374
var horizontalSlides = dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ),
3375
verticalSlides = dom.wrapper.querySelectorAll( VERTICAL_SLIDES_SELECTOR );
3376
3377
var backgroundSize = dom.background.style.backgroundSize.split( ' ' ),
3378
backgroundWidth, backgroundHeight;
3379
3380
if( backgroundSize.length === 1 ) {
3381
backgroundWidth = backgroundHeight = parseInt( backgroundSize[0], 10 );
3382
}
3383
else {
3384
backgroundWidth = parseInt( backgroundSize[0], 10 );
3385
backgroundHeight = parseInt( backgroundSize[1], 10 );
3386
}
3387
3388
var slideWidth = dom.background.offsetWidth,
3389
horizontalSlideCount = horizontalSlides.length,
3390
horizontalOffsetMultiplier,
3391
horizontalOffset;
3392
3393
if( typeof config.parallaxBackgroundHorizontal === 'number' ) {
3394
horizontalOffsetMultiplier = config.parallaxBackgroundHorizontal;
3395
}
3396
else {
3397
horizontalOffsetMultiplier = horizontalSlideCount > 1 ? ( backgroundWidth - slideWidth ) / ( horizontalSlideCount-1 ) : 0;
3398
}
3399
3400
horizontalOffset = horizontalOffsetMultiplier * indexh * -1;
3401
3402
var slideHeight = dom.background.offsetHeight,
3403
verticalSlideCount = verticalSlides.length,
3404
verticalOffsetMultiplier,
3405
verticalOffset;
3406
3407
if( typeof config.parallaxBackgroundVertical === 'number' ) {
3408
verticalOffsetMultiplier = config.parallaxBackgroundVertical;
3409
}
3410
else {
3411
verticalOffsetMultiplier = ( backgroundHeight - slideHeight ) / ( verticalSlideCount-1 );
3412
}
3413
3414
verticalOffset = verticalSlideCount > 0 ? verticalOffsetMultiplier * indexv : 0;
3415
3416
dom.background.style.backgroundPosition = horizontalOffset + 'px ' + -verticalOffset + 'px';
3417
3418
}
3419
3420
}
3421
3422
/**
3423
* Called when the given slide is within the configured view
3424
* distance. Shows the slide element and loads any content
3425
* that is set to load lazily (data-src).
3426
*
3427
* @param {HTMLElement} slide Slide to show
3428
*/
3429
function loadSlide( slide, options ) {
3430
3431
options = options || {};
3432
3433
// Show the slide element
3434
slide.style.display = config.display;
3435
3436
// Media elements with data-src attributes
3437
toArray( slide.querySelectorAll( 'img[data-src], video[data-src], audio[data-src]' ) ).forEach( function( element ) {
3438
element.setAttribute( 'src', element.getAttribute( 'data-src' ) );
3439
element.setAttribute( 'data-lazy-loaded', '' );
3440
element.removeAttribute( 'data-src' );
3441
} );
3442
3443
// Media elements with <source> children
3444
toArray( slide.querySelectorAll( 'video, audio' ) ).forEach( function( media ) {
3445
var sources = 0;
3446
3447
toArray( media.querySelectorAll( 'source[data-src]' ) ).forEach( function( source ) {
3448
source.setAttribute( 'src', source.getAttribute( 'data-src' ) );
3449
source.removeAttribute( 'data-src' );
3450
source.setAttribute( 'data-lazy-loaded', '' );
3451
sources += 1;
3452
} );
3453
3454
// If we rewrote sources for this video/audio element, we need
3455
// to manually tell it to load from its new origin
3456
if( sources > 0 ) {
3457
media.load();
3458
}
3459
} );
3460
3461
3462
// Show the corresponding background element
3463
var background = slide.slideBackgroundElement;
3464
if( background ) {
3465
background.style.display = 'block';
3466
3467
var backgroundContent = slide.slideBackgroundContentElement;
3468
3469
// If the background contains media, load it
3470
if( background.hasAttribute( 'data-loaded' ) === false ) {
3471
background.setAttribute( 'data-loaded', 'true' );
3472
3473
var backgroundImage = slide.getAttribute( 'data-background-image' ),
3474
backgroundVideo = slide.getAttribute( 'data-background-video' ),
3475
backgroundVideoLoop = slide.hasAttribute( 'data-background-video-loop' ),
3476
backgroundVideoMuted = slide.hasAttribute( 'data-background-video-muted' ),
3477
backgroundIframe = slide.getAttribute( 'data-background-iframe' );
3478
3479
// Images
3480
if( backgroundImage ) {
3481
backgroundContent.style.backgroundImage = 'url('+ encodeURI( backgroundImage ) +')';
3482
}
3483
// Videos
3484
else if ( backgroundVideo && !isSpeakerNotes() ) {
3485
var video = document.createElement( 'video' );
3486
3487
if( backgroundVideoLoop ) {
3488
video.setAttribute( 'loop', '' );
3489
}
3490
3491
if( backgroundVideoMuted ) {
3492
video.muted = true;
3493
}
3494
3495
// Inline video playback works (at least in Mobile Safari) as
3496
// long as the video is muted and the `playsinline` attribute is
3497
// present
3498
if( isMobileDevice ) {
3499
video.muted = true;
3500
video.autoplay = true;
3501
video.setAttribute( 'playsinline', '' );
3502
}
3503
3504
// Support comma separated lists of video sources
3505
backgroundVideo.split( ',' ).forEach( function( source ) {
3506
video.innerHTML += '<source src="'+ source +'">';
3507
} );
3508
3509
backgroundContent.appendChild( video );
3510
}
3511
// Iframes
3512
else if( backgroundIframe && options.excludeIframes !== true ) {
3513
var iframe = document.createElement( 'iframe' );
3514
iframe.setAttribute( 'allowfullscreen', '' );
3515
iframe.setAttribute( 'mozallowfullscreen', '' );
3516
iframe.setAttribute( 'webkitallowfullscreen', '' );
3517
3518
// Only load autoplaying content when the slide is shown to
3519
// avoid having it play in the background
3520
if( /autoplay=(1|true|yes)/gi.test( backgroundIframe ) ) {
3521
iframe.setAttribute( 'data-src', backgroundIframe );
3522
}
3523
else {
3524
iframe.setAttribute( 'src', backgroundIframe );
3525
}
3526
3527
iframe.style.width = '100%';
3528
iframe.style.height = '100%';
3529
iframe.style.maxHeight = '100%';
3530
iframe.style.maxWidth = '100%';
3531
3532
backgroundContent.appendChild( iframe );
3533
}
3534
}
3535
3536
}
3537
3538
}
3539
3540
/**
3541
* Unloads and hides the given slide. This is called when the
3542
* slide is moved outside of the configured view distance.
3543
*
3544
* @param {HTMLElement} slide
3545
*/
3546
function unloadSlide( slide ) {
3547
3548
// Hide the slide element
3549
slide.style.display = 'none';
3550
3551
// Hide the corresponding background element
3552
var background = getSlideBackground( slide );
3553
if( background ) {
3554
background.style.display = 'none';
3555
}
3556
3557
// Reset lazy-loaded media elements with src attributes
3558
toArray( slide.querySelectorAll( 'video[data-lazy-loaded][src], audio[data-lazy-loaded][src]' ) ).forEach( function( element ) {
3559
element.setAttribute( 'data-src', element.getAttribute( 'src' ) );
3560
element.removeAttribute( 'src' );
3561
} );
3562
3563
// Reset lazy-loaded media elements with <source> children
3564
toArray( slide.querySelectorAll( 'video[data-lazy-loaded] source[src], audio source[src]' ) ).forEach( function( source ) {
3565
source.setAttribute( 'data-src', source.getAttribute( 'src' ) );
3566
source.removeAttribute( 'src' );
3567
} );
3568
3569
}
3570
3571
/**
3572
* Determine what available routes there are for navigation.
3573
*
3574
* @return {{left: boolean, right: boolean, up: boolean, down: boolean}}
3575
*/
3576
function availableRoutes() {
3577
3578
var horizontalSlides = dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ),
3579
verticalSlides = dom.wrapper.querySelectorAll( VERTICAL_SLIDES_SELECTOR );
3580
3581
var routes = {
3582
left: indexh > 0,
3583
right: indexh < horizontalSlides.length - 1,
3584
up: indexv > 0,
3585
down: indexv < verticalSlides.length - 1
3586
};
3587
3588
// Looped presentations can always be navigated as long as
3589
// there are slides available
3590
if( config.loop ) {
3591
if( horizontalSlides.length > 1 ) {
3592
routes.left = true;
3593
routes.right = true;
3594
}
3595
3596
if( verticalSlides.length > 1 ) {
3597
routes.up = true;
3598
routes.down = true;
3599
}
3600
}
3601
3602
// Reverse horizontal controls for rtl
3603
if( config.rtl ) {
3604
var left = routes.left;
3605
routes.left = routes.right;
3606
routes.right = left;
3607
}
3608
3609
return routes;
3610
3611
}
3612
3613
/**
3614
* Returns an object describing the available fragment
3615
* directions.
3616
*
3617
* @return {{prev: boolean, next: boolean}}
3618
*/
3619
function availableFragments() {
3620
3621
if( currentSlide && config.fragments ) {
3622
var fragments = currentSlide.querySelectorAll( '.fragment' );
3623
var hiddenFragments = currentSlide.querySelectorAll( '.fragment:not(.visible)' );
3624
3625
return {
3626
prev: fragments.length - hiddenFragments.length > 0,
3627
next: !!hiddenFragments.length
3628
};
3629
}
3630
else {
3631
return { prev: false, next: false };
3632
}
3633
3634
}
3635
3636
/**
3637
* Enforces origin-specific format rules for embedded media.
3638
*/
3639
function formatEmbeddedContent() {
3640
3641
var _appendParamToIframeSource = function( sourceAttribute, sourceURL, param ) {
3642
toArray( dom.slides.querySelectorAll( 'iframe['+ sourceAttribute +'*="'+ sourceURL +'"]' ) ).forEach( function( el ) {
3643
var src = el.getAttribute( sourceAttribute );
3644
if( src && src.indexOf( param ) === -1 ) {
3645
el.setAttribute( sourceAttribute, src + ( !/\?/.test( src ) ? '?' : '&' ) + param );
3646
}
3647
});
3648
};
3649
3650
// YouTube frames must include "?enablejsapi=1"
3651
_appendParamToIframeSource( 'src', 'youtube.com/embed/', 'enablejsapi=1' );
3652
_appendParamToIframeSource( 'data-src', 'youtube.com/embed/', 'enablejsapi=1' );
3653
3654
// Vimeo frames must include "?api=1"
3655
_appendParamToIframeSource( 'src', 'player.vimeo.com/', 'api=1' );
3656
_appendParamToIframeSource( 'data-src', 'player.vimeo.com/', 'api=1' );
3657
3658
// Always show media controls on mobile devices
3659
if( isMobileDevice ) {
3660
toArray( dom.slides.querySelectorAll( 'video, audio' ) ).forEach( function( el ) {
3661
el.controls = true;
3662
} );
3663
}
3664
3665
}
3666
3667
/**
3668
* Start playback of any embedded content inside of
3669
* the given element.
3670
*
3671
* @param {HTMLElement} element
3672
*/
3673
function startEmbeddedContent( element ) {
3674
3675
if( element && !isSpeakerNotes() ) {
3676
3677
// Restart GIFs
3678
toArray( element.querySelectorAll( 'img[src$=".gif"]' ) ).forEach( function( el ) {
3679
// Setting the same unchanged source like this was confirmed
3680
// to work in Chrome, FF & Safari
3681
el.setAttribute( 'src', el.getAttribute( 'src' ) );
3682
} );
3683
3684
// HTML5 media elements
3685
toArray( element.querySelectorAll( 'video, audio' ) ).forEach( function( el ) {
3686
if( closestParent( el, '.fragment' ) && !closestParent( el, '.fragment.visible' ) ) {
3687
return;
3688
}
3689
3690
// Prefer an explicit global autoplay setting
3691
var autoplay = config.autoPlayMedia;
3692
3693
// If no global setting is available, fall back on the element's
3694
// own autoplay setting
3695
if( typeof autoplay !== 'boolean' ) {
3696
autoplay = el.hasAttribute( 'data-autoplay' ) || !!closestParent( el, '.slide-background' );
3697
}
3698
3699
if( autoplay && typeof el.play === 'function' ) {
3700
3701
// If the media is ready, start playback
3702
if( el.readyState > 1 ) {
3703
startEmbeddedMedia( { target: el } );
3704
}
3705
// Mobile devices never fire a loaded event so instead
3706
// of waiting, we initiate playback
3707
else if( isMobileDevice ) {
3708
el.play();
3709
}
3710
// If the media isn't loaded, wait before playing
3711
else {
3712
el.removeEventListener( 'loadeddata', startEmbeddedMedia ); // remove first to avoid dupes
3713
el.addEventListener( 'loadeddata', startEmbeddedMedia );
3714
}
3715
3716
}
3717
} );
3718
3719
// Normal iframes
3720
toArray( element.querySelectorAll( 'iframe[src]' ) ).forEach( function( el ) {
3721
if( closestParent( el, '.fragment' ) && !closestParent( el, '.fragment.visible' ) ) {
3722
return;
3723
}
3724
3725
startEmbeddedIframe( { target: el } );
3726
} );
3727
3728
// Lazy loading iframes
3729
toArray( element.querySelectorAll( 'iframe[data-src]' ) ).forEach( function( el ) {
3730
if( closestParent( el, '.fragment' ) && !closestParent( el, '.fragment.visible' ) ) {
3731
return;
3732
}
3733
3734
if( el.getAttribute( 'src' ) !== el.getAttribute( 'data-src' ) ) {
3735
el.removeEventListener( 'load', startEmbeddedIframe ); // remove first to avoid dupes
3736
el.addEventListener( 'load', startEmbeddedIframe );
3737
el.setAttribute( 'src', el.getAttribute( 'data-src' ) );
3738
}
3739
} );
3740
3741
}
3742
3743
}
3744
3745
/**
3746
* Starts playing an embedded video/audio element after
3747
* it has finished loading.
3748
*
3749
* @param {object} event
3750
*/
3751
function startEmbeddedMedia( event ) {
3752
3753
var isAttachedToDOM = !!closestParent( event.target, 'html' ),
3754
isVisible = !!closestParent( event.target, '.present' );
3755
3756
if( isAttachedToDOM && isVisible ) {
3757
event.target.currentTime = 0;
3758
event.target.play();
3759
}
3760
3761
event.target.removeEventListener( 'loadeddata', startEmbeddedMedia );
3762
3763
}
3764
3765
/**
3766
* "Starts" the content of an embedded iframe using the
3767
* postMessage API.
3768
*
3769
* @param {object} event
3770
*/
3771
function startEmbeddedIframe( event ) {
3772
3773
var iframe = event.target;
3774
3775
if( iframe && iframe.contentWindow ) {
3776
3777
var isAttachedToDOM = !!closestParent( event.target, 'html' ),
3778
isVisible = !!closestParent( event.target, '.present' );
3779
3780
if( isAttachedToDOM && isVisible ) {
3781
3782
// Prefer an explicit global autoplay setting
3783
var autoplay = config.autoPlayMedia;
3784
3785
// If no global setting is available, fall back on the element's
3786
// own autoplay setting
3787
if( typeof autoplay !== 'boolean' ) {
3788
autoplay = iframe.hasAttribute( 'data-autoplay' ) || !!closestParent( iframe, '.slide-background' );
3789
}
3790
3791
// YouTube postMessage API
3792
if( /youtube\.com\/embed\//.test( iframe.getAttribute( 'src' ) ) && autoplay ) {
3793
iframe.contentWindow.postMessage( '{"event":"command","func":"playVideo","args":""}', '*' );
3794
}
3795
// Vimeo postMessage API
3796
else if( /player\.vimeo\.com\//.test( iframe.getAttribute( 'src' ) ) && autoplay ) {
3797
iframe.contentWindow.postMessage( '{"method":"play"}', '*' );
3798
}
3799
// Generic postMessage API
3800
else {
3801
iframe.contentWindow.postMessage( 'slide:start', '*' );
3802
}
3803
3804
}
3805
3806
}
3807
3808
}
3809
3810
/**
3811
* Stop playback of any embedded content inside of
3812
* the targeted slide.
3813
*
3814
* @param {HTMLElement} element
3815
*/
3816
function stopEmbeddedContent( element, options ) {
3817
3818
options = extend( {
3819
// Defaults
3820
unloadIframes: true
3821
}, options || {} );
3822
3823
if( element && element.parentNode ) {
3824
// HTML5 media elements
3825
toArray( element.querySelectorAll( 'video, audio' ) ).forEach( function( el ) {
3826
if( !el.hasAttribute( 'data-ignore' ) && typeof el.pause === 'function' ) {
3827
el.setAttribute('data-paused-by-reveal', '');
3828
el.pause();
3829
}
3830
} );
3831
3832
// Generic postMessage API for non-lazy loaded iframes
3833
toArray( element.querySelectorAll( 'iframe' ) ).forEach( function( el ) {
3834
if( el.contentWindow ) el.contentWindow.postMessage( 'slide:stop', '*' );
3835
el.removeEventListener( 'load', startEmbeddedIframe );
3836
});
3837
3838
// YouTube postMessage API
3839
toArray( element.querySelectorAll( 'iframe[src*="youtube.com/embed/"]' ) ).forEach( function( el ) {
3840
if( !el.hasAttribute( 'data-ignore' ) && el.contentWindow && typeof el.contentWindow.postMessage === 'function' ) {
3841
el.contentWindow.postMessage( '{"event":"command","func":"pauseVideo","args":""}', '*' );
3842
}
3843
});
3844
3845
// Vimeo postMessage API
3846
toArray( element.querySelectorAll( 'iframe[src*="player.vimeo.com/"]' ) ).forEach( function( el ) {
3847
if( !el.hasAttribute( 'data-ignore' ) && el.contentWindow && typeof el.contentWindow.postMessage === 'function' ) {
3848
el.contentWindow.postMessage( '{"method":"pause"}', '*' );
3849
}
3850
});
3851
3852
if( options.unloadIframes === true ) {
3853
// Unload lazy-loaded iframes
3854
toArray( element.querySelectorAll( 'iframe[data-src]' ) ).forEach( function( el ) {
3855
// Only removing the src doesn't actually unload the frame
3856
// in all browsers (Firefox) so we set it to blank first
3857
el.setAttribute( 'src', 'about:blank' );
3858
el.removeAttribute( 'src' );
3859
} );
3860
}
3861
}
3862
3863
}
3864
3865
/**
3866
* Returns the number of past slides. This can be used as a global
3867
* flattened index for slides.
3868
*
3869
* @return {number} Past slide count
3870
*/
3871
function getSlidePastCount() {
3872
3873
var horizontalSlides = toArray( dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) );
3874
3875
// The number of past slides
3876
var pastCount = 0;
3877
3878
// Step through all slides and count the past ones
3879
mainLoop: for( var i = 0; i < horizontalSlides.length; i++ ) {
3880
3881
var horizontalSlide = horizontalSlides[i];
3882
var verticalSlides = toArray( horizontalSlide.querySelectorAll( 'section' ) );
3883
3884
for( var j = 0; j < verticalSlides.length; j++ ) {
3885
3886
// Stop as soon as we arrive at the present
3887
if( verticalSlides[j].classList.contains( 'present' ) ) {
3888
break mainLoop;
3889
}
3890
3891
pastCount++;
3892
3893
}
3894
3895
// Stop as soon as we arrive at the present
3896
if( horizontalSlide.classList.contains( 'present' ) ) {
3897
break;
3898
}
3899
3900
// Don't count the wrapping section for vertical slides
3901
if( horizontalSlide.classList.contains( 'stack' ) === false ) {
3902
pastCount++;
3903
}
3904
3905
}
3906
3907
return pastCount;
3908
3909
}
3910
3911
/**
3912
* Returns a value ranging from 0-1 that represents
3913
* how far into the presentation we have navigated.
3914
*
3915
* @return {number}
3916
*/
3917
function getProgress() {
3918
3919
// The number of past and total slides
3920
var totalCount = getTotalSlides();
3921
var pastCount = getSlidePastCount();
3922
3923
if( currentSlide ) {
3924
3925
var allFragments = currentSlide.querySelectorAll( '.fragment' );
3926
3927
// If there are fragments in the current slide those should be
3928
// accounted for in the progress.
3929
if( allFragments.length > 0 ) {
3930
var visibleFragments = currentSlide.querySelectorAll( '.fragment.visible' );
3931
3932
// This value represents how big a portion of the slide progress
3933
// that is made up by its fragments (0-1)
3934
var fragmentWeight = 0.9;
3935
3936
// Add fragment progress to the past slide count
3937
pastCount += ( visibleFragments.length / allFragments.length ) * fragmentWeight;
3938
}
3939
3940
}
3941
3942
return pastCount / ( totalCount - 1 );
3943
3944
}
3945
3946
/**
3947
* Checks if this presentation is running inside of the
3948
* speaker notes window.
3949
*
3950
* @return {boolean}
3951
*/
3952
function isSpeakerNotes() {
3953
3954
return !!window.location.search.match( /receiver/gi );
3955
3956
}
3957
3958
/**
3959
* Reads the current URL (hash) and navigates accordingly.
3960
*/
3961
function readURL() {
3962
3963
var hash = window.location.hash;
3964
3965
// Attempt to parse the hash as either an index or name
3966
var bits = hash.slice( 2 ).split( '/' ),
3967
name = hash.replace( /#|\//gi, '' );
3968
3969
// If the first bit is not fully numeric and there is a name we
3970
// can assume that this is a named link
3971
if( !/^[0-9]*$/.test( bits[0] ) && name.length ) {
3972
var element;
3973
3974
// Ensure the named link is a valid HTML ID attribute
3975
try {
3976
element = document.getElementById( decodeURIComponent( name ) );
3977
}
3978
catch ( error ) { }
3979
3980
// Ensure that we're not already on a slide with the same name
3981
var isSameNameAsCurrentSlide = currentSlide ? currentSlide.getAttribute( 'id' ) === name : false;
3982
3983
if( element && !isSameNameAsCurrentSlide ) {
3984
// Find the position of the named slide and navigate to it
3985
var indices = Reveal.getIndices( element );
3986
slide( indices.h, indices.v );
3987
}
3988
// If the slide doesn't exist, navigate to the current slide
3989
else {
3990
slide( indexh || 0, indexv || 0 );
3991
}
3992
}
3993
else {
3994
var hashIndexBase = config.hashOneBasedIndex ? 1 : 0;
3995
3996
// Read the index components of the hash
3997
var h = ( parseInt( bits[0], 10 ) - hashIndexBase ) || 0,
3998
v = ( parseInt( bits[1], 10 ) - hashIndexBase ) || 0,
3999
f;
4000
4001
if( config.fragmentInURL ) {
4002
f = parseInt( bits[2], 10 );
4003
if( isNaN( f ) ) {
4004
f = undefined;
4005
}
4006
}
4007
4008
if( h !== indexh || v !== indexv || f !== undefined ) {
4009
slide( h, v, f );
4010
}
4011
}
4012
4013
}
4014
4015
/**
4016
* Updates the page URL (hash) to reflect the current
4017
* state.
4018
*
4019
* @param {number} delay The time in ms to wait before
4020
* writing the hash
4021
*/
4022
function writeURL( delay ) {
4023
4024
if( config.history ) {
4025
4026
// Make sure there's never more than one timeout running
4027
clearTimeout( writeURLTimeout );
4028
4029
// If a delay is specified, timeout this call
4030
if( typeof delay === 'number' ) {
4031
writeURLTimeout = setTimeout( writeURL, delay );
4032
}
4033
else if( currentSlide ) {
4034
window.location.hash = locationHash();
4035
}
4036
}
4037
4038
}
4039
/**
4040
* Retrieves the h/v location and fragment of the current,
4041
* or specified, slide.
4042
*
4043
* @param {HTMLElement} [slide] If specified, the returned
4044
* index will be for this slide rather than the currently
4045
* active one
4046
*
4047
* @return {{h: number, v: number, f: number}}
4048
*/
4049
function getIndices( slide ) {
4050
4051
// By default, return the current indices
4052
var h = indexh,
4053
v = indexv,
4054
f;
4055
4056
// If a slide is specified, return the indices of that slide
4057
if( slide ) {
4058
var isVertical = isVerticalSlide( slide );
4059
var slideh = isVertical ? slide.parentNode : slide;
4060
4061
// Select all horizontal slides
4062
var horizontalSlides = toArray( dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) );
4063
4064
// Now that we know which the horizontal slide is, get its index
4065
h = Math.max( horizontalSlides.indexOf( slideh ), 0 );
4066
4067
// Assume we're not vertical
4068
v = undefined;
4069
4070
// If this is a vertical slide, grab the vertical index
4071
if( isVertical ) {
4072
v = Math.max( toArray( slide.parentNode.querySelectorAll( 'section' ) ).indexOf( slide ), 0 );
4073
}
4074
}
4075
4076
if( !slide && currentSlide ) {
4077
var hasFragments = currentSlide.querySelectorAll( '.fragment' ).length > 0;
4078
if( hasFragments ) {
4079
var currentFragment = currentSlide.querySelector( '.current-fragment' );
4080
if( currentFragment && currentFragment.hasAttribute( 'data-fragment-index' ) ) {
4081
f = parseInt( currentFragment.getAttribute( 'data-fragment-index' ), 10 );
4082
}
4083
else {
4084
f = currentSlide.querySelectorAll( '.fragment.visible' ).length - 1;
4085
}
4086
}
4087
}
4088
4089
return { h: h, v: v, f: f };
4090
4091
}
4092
4093
/**
4094
* Retrieves all slides in this presentation.
4095
*/
4096
function getSlides() {
4097
4098
return toArray( dom.wrapper.querySelectorAll( SLIDES_SELECTOR + ':not(.stack)' ));
4099
4100
}
4101
4102
/**
4103
* Retrieves the total number of slides in this presentation.
4104
*
4105
* @return {number}
4106
*/
4107
function getTotalSlides() {
4108
4109
return getSlides().length;
4110
4111
}
4112
4113
/**
4114
* Returns the slide element matching the specified index.
4115
*
4116
* @return {HTMLElement}
4117
*/
4118
function getSlide( x, y ) {
4119
4120
var horizontalSlide = dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR )[ x ];
4121
var verticalSlides = horizontalSlide && horizontalSlide.querySelectorAll( 'section' );
4122
4123
if( verticalSlides && verticalSlides.length && typeof y === 'number' ) {
4124
return verticalSlides ? verticalSlides[ y ] : undefined;
4125
}
4126
4127
return horizontalSlide;
4128
4129
}
4130
4131
/**
4132
* Returns the background element for the given slide.
4133
* All slides, even the ones with no background properties
4134
* defined, have a background element so as long as the
4135
* index is valid an element will be returned.
4136
*
4137
* @param {mixed} x Horizontal background index OR a slide
4138
* HTML element
4139
* @param {number} y Vertical background index
4140
* @return {(HTMLElement[]|*)}
4141
*/
4142
function getSlideBackground( x, y ) {
4143
4144
var slide = typeof x === 'number' ? getSlide( x, y ) : x;
4145
if( slide ) {
4146
return slide.slideBackgroundElement;
4147
}
4148
4149
return undefined;
4150
4151
}
4152
4153
/**
4154
* Retrieves the speaker notes from a slide. Notes can be
4155
* defined in two ways:
4156
* 1. As a data-notes attribute on the slide <section>
4157
* 2. As an <aside class="notes"> inside of the slide
4158
*
4159
* @param {HTMLElement} [slide=currentSlide]
4160
* @return {(string|null)}
4161
*/
4162
function getSlideNotes( slide ) {
4163
4164
// Default to the current slide
4165
slide = slide || currentSlide;
4166
4167
// Notes can be specified via the data-notes attribute...
4168
if( slide.hasAttribute( 'data-notes' ) ) {
4169
return slide.getAttribute( 'data-notes' );
4170
}
4171
4172
// ... or using an <aside class="notes"> element
4173
var notesElement = slide.querySelector( 'aside.notes' );
4174
if( notesElement ) {
4175
return notesElement.innerHTML;
4176
}
4177
4178
return null;
4179
4180
}
4181
4182
/**
4183
* Retrieves the current state of the presentation as
4184
* an object. This state can then be restored at any
4185
* time.
4186
*
4187
* @return {{indexh: number, indexv: number, indexf: number, paused: boolean, overview: boolean}}
4188
*/
4189
function getState() {
4190
4191
var indices = getIndices();
4192
4193
return {
4194
indexh: indices.h,
4195
indexv: indices.v,
4196
indexf: indices.f,
4197
paused: isPaused(),
4198
overview: isOverview()
4199
};
4200
4201
}
4202
4203
/**
4204
* Restores the presentation to the given state.
4205
*
4206
* @param {object} state As generated by getState()
4207
* @see {@link getState} generates the parameter `state`
4208
*/
4209
function setState( state ) {
4210
4211
if( typeof state === 'object' ) {
4212
slide( deserialize( state.indexh ), deserialize( state.indexv ), deserialize( state.indexf ) );
4213
4214
var pausedFlag = deserialize( state.paused ),
4215
overviewFlag = deserialize( state.overview );
4216
4217
if( typeof pausedFlag === 'boolean' && pausedFlag !== isPaused() ) {
4218
togglePause( pausedFlag );
4219
}
4220
4221
if( typeof overviewFlag === 'boolean' && overviewFlag !== isOverview() ) {
4222
toggleOverview( overviewFlag );
4223
}
4224
}
4225
4226
}
4227
4228
/**
4229
* Return a sorted fragments list, ordered by an increasing
4230
* "data-fragment-index" attribute.
4231
*
4232
* Fragments will be revealed in the order that they are returned by
4233
* this function, so you can use the index attributes to control the
4234
* order of fragment appearance.
4235
*
4236
* To maintain a sensible default fragment order, fragments are presumed
4237
* to be passed in document order. This function adds a "fragment-index"
4238
* attribute to each node if such an attribute is not already present,
4239
* and sets that attribute to an integer value which is the position of
4240
* the fragment within the fragments list.
4241
*
4242
* @param {object[]|*} fragments
4243
* @param {boolean} grouped If true the returned array will contain
4244
* nested arrays for all fragments with the same index
4245
* @return {object[]} sorted Sorted array of fragments
4246
*/
4247
function sortFragments( fragments, grouped ) {
4248
4249
fragments = toArray( fragments );
4250
4251
var ordered = [],
4252
unordered = [],
4253
sorted = [];
4254
4255
// Group ordered and unordered elements
4256
fragments.forEach( function( fragment, i ) {
4257
if( fragment.hasAttribute( 'data-fragment-index' ) ) {
4258
var index = parseInt( fragment.getAttribute( 'data-fragment-index' ), 10 );
4259
4260
if( !ordered[index] ) {
4261
ordered[index] = [];
4262
}
4263
4264
ordered[index].push( fragment );
4265
}
4266
else {
4267
unordered.push( [ fragment ] );
4268
}
4269
} );
4270
4271
// Append fragments without explicit indices in their
4272
// DOM order
4273
ordered = ordered.concat( unordered );
4274
4275
// Manually count the index up per group to ensure there
4276
// are no gaps
4277
var index = 0;
4278
4279
// Push all fragments in their sorted order to an array,
4280
// this flattens the groups
4281
ordered.forEach( function( group ) {
4282
group.forEach( function( fragment ) {
4283
sorted.push( fragment );
4284
fragment.setAttribute( 'data-fragment-index', index );
4285
} );
4286
4287
index ++;
4288
} );
4289
4290
return grouped === true ? ordered : sorted;
4291
4292
}
4293
4294
/**
4295
* Navigate to the specified slide fragment.
4296
*
4297
* @param {?number} index The index of the fragment that
4298
* should be shown, -1 means all are invisible
4299
* @param {number} offset Integer offset to apply to the
4300
* fragment index
4301
*
4302
* @return {boolean} true if a change was made in any
4303
* fragments visibility as part of this call
4304
*/
4305
function navigateFragment( index, offset ) {
4306
4307
if( currentSlide && config.fragments ) {
4308
4309
var fragments = sortFragments( currentSlide.querySelectorAll( '.fragment' ) );
4310
if( fragments.length ) {
4311
4312
// If no index is specified, find the current
4313
if( typeof index !== 'number' ) {
4314
var lastVisibleFragment = sortFragments( currentSlide.querySelectorAll( '.fragment.visible' ) ).pop();
4315
4316
if( lastVisibleFragment ) {
4317
index = parseInt( lastVisibleFragment.getAttribute( 'data-fragment-index' ) || 0, 10 );
4318
}
4319
else {
4320
index = -1;
4321
}
4322
}
4323
4324
// If an offset is specified, apply it to the index
4325
if( typeof offset === 'number' ) {
4326
index += offset;
4327
}
4328
4329
var fragmentsShown = [],
4330
fragmentsHidden = [];
4331
4332
toArray( fragments ).forEach( function( element, i ) {
4333
4334
if( element.hasAttribute( 'data-fragment-index' ) ) {
4335
i = parseInt( element.getAttribute( 'data-fragment-index' ), 10 );
4336
}
4337
4338
// Visible fragments
4339
if( i <= index ) {
4340
if( !element.classList.contains( 'visible' ) ) fragmentsShown.push( element );
4341
element.classList.add( 'visible' );
4342
element.classList.remove( 'current-fragment' );
4343
4344
// Announce the fragments one by one to the Screen Reader
4345
dom.statusDiv.textContent = getStatusText( element );
4346
4347
if( i === index ) {
4348
element.classList.add( 'current-fragment' );
4349
startEmbeddedContent( element );
4350
}
4351
}
4352
// Hidden fragments
4353
else {
4354
if( element.classList.contains( 'visible' ) ) fragmentsHidden.push( element );
4355
element.classList.remove( 'visible' );
4356
element.classList.remove( 'current-fragment' );
4357
}
4358
4359
} );
4360
4361
if( fragmentsHidden.length ) {
4362
dispatchEvent( 'fragmenthidden', { fragment: fragmentsHidden[0], fragments: fragmentsHidden } );
4363
}
4364
4365
if( fragmentsShown.length ) {
4366
dispatchEvent( 'fragmentshown', { fragment: fragmentsShown[0], fragments: fragmentsShown } );
4367
}
4368
4369
updateControls();
4370
updateProgress();
4371
if( config.fragmentInURL ) {
4372
writeURL();
4373
}
4374
4375
return !!( fragmentsShown.length || fragmentsHidden.length );
4376
4377
}
4378
4379
}
4380
4381
return false;
4382
4383
}
4384
4385
/**
4386
* Navigate to the next slide fragment.
4387
*
4388
* @return {boolean} true if there was a next fragment,
4389
* false otherwise
4390
*/
4391
function nextFragment() {
4392
4393
return navigateFragment( null, 1 );
4394
4395
}
4396
4397
/**
4398
* Navigate to the previous slide fragment.
4399
*
4400
* @return {boolean} true if there was a previous fragment,
4401
* false otherwise
4402
*/
4403
function previousFragment() {
4404
4405
return navigateFragment( null, -1 );
4406
4407
}
4408
4409
/**
4410
* Cues a new automated slide if enabled in the config.
4411
*/
4412
function cueAutoSlide() {
4413
4414
cancelAutoSlide();
4415
4416
if( currentSlide && config.autoSlide !== false ) {
4417
4418
var fragment = currentSlide.querySelector( '.current-fragment' );
4419
4420
// When the slide first appears there is no "current" fragment so
4421
// we look for a data-autoslide timing on the first fragment
4422
if( !fragment ) fragment = currentSlide.querySelector( '.fragment' );
4423
4424
var fragmentAutoSlide = fragment ? fragment.getAttribute( 'data-autoslide' ) : null;
4425
var parentAutoSlide = currentSlide.parentNode ? currentSlide.parentNode.getAttribute( 'data-autoslide' ) : null;
4426
var slideAutoSlide = currentSlide.getAttribute( 'data-autoslide' );
4427
4428
// Pick value in the following priority order:
4429
// 1. Current fragment's data-autoslide
4430
// 2. Current slide's data-autoslide
4431
// 3. Parent slide's data-autoslide
4432
// 4. Global autoSlide setting
4433
if( fragmentAutoSlide ) {
4434
autoSlide = parseInt( fragmentAutoSlide, 10 );
4435
}
4436
else if( slideAutoSlide ) {
4437
autoSlide = parseInt( slideAutoSlide, 10 );
4438
}
4439
else if( parentAutoSlide ) {
4440
autoSlide = parseInt( parentAutoSlide, 10 );
4441
}
4442
else {
4443
autoSlide = config.autoSlide;
4444
}
4445
4446
// If there are media elements with data-autoplay,
4447
// automatically set the autoSlide duration to the
4448
// length of that media. Not applicable if the slide
4449
// is divided up into fragments.
4450
// playbackRate is accounted for in the duration.
4451
if( currentSlide.querySelectorAll( '.fragment' ).length === 0 ) {
4452
toArray( currentSlide.querySelectorAll( 'video, audio' ) ).forEach( function( el ) {
4453
if( el.hasAttribute( 'data-autoplay' ) ) {
4454
if( autoSlide && (el.duration * 1000 / el.playbackRate ) > autoSlide ) {
4455
autoSlide = ( el.duration * 1000 / el.playbackRate ) + 1000;
4456
}
4457
}
4458
} );
4459
}
4460
4461
// Cue the next auto-slide if:
4462
// - There is an autoSlide value
4463
// - Auto-sliding isn't paused by the user
4464
// - The presentation isn't paused
4465
// - The overview isn't active
4466
// - The presentation isn't over
4467
if( autoSlide && !autoSlidePaused && !isPaused() && !isOverview() && ( !Reveal.isLastSlide() || availableFragments().next || config.loop === true ) ) {
4468
autoSlideTimeout = setTimeout( function() {
4469
typeof config.autoSlideMethod === 'function' ? config.autoSlideMethod() : navigateNext();
4470
cueAutoSlide();
4471
}, autoSlide );
4472
autoSlideStartTime = Date.now();
4473
}
4474
4475
if( autoSlidePlayer ) {
4476
autoSlidePlayer.setPlaying( autoSlideTimeout !== -1 );
4477
}
4478
4479
}
4480
4481
}
4482
4483
/**
4484
* Cancels any ongoing request to auto-slide.
4485
*/
4486
function cancelAutoSlide() {
4487
4488
clearTimeout( autoSlideTimeout );
4489
autoSlideTimeout = -1;
4490
4491
}
4492
4493
function pauseAutoSlide() {
4494
4495
if( autoSlide && !autoSlidePaused ) {
4496
autoSlidePaused = true;
4497
dispatchEvent( 'autoslidepaused' );
4498
clearTimeout( autoSlideTimeout );
4499
4500
if( autoSlidePlayer ) {
4501
autoSlidePlayer.setPlaying( false );
4502
}
4503
}
4504
4505
}
4506
4507
function resumeAutoSlide() {
4508
4509
if( autoSlide && autoSlidePaused ) {
4510
autoSlidePaused = false;
4511
dispatchEvent( 'autoslideresumed' );
4512
cueAutoSlide();
4513
}
4514
4515
}
4516
4517
function navigateLeft() {
4518
4519
// Reverse for RTL
4520
if( config.rtl ) {
4521
if( ( isOverview() || nextFragment() === false ) && availableRoutes().left ) {
4522
slide( indexh + 1 );
4523
}
4524
}
4525
// Normal navigation
4526
else if( ( isOverview() || previousFragment() === false ) && availableRoutes().left ) {
4527
slide( indexh - 1 );
4528
}
4529
4530
}
4531
4532
function navigateRight() {
4533
4534
hasNavigatedRight = true;
4535
4536
// Reverse for RTL
4537
if( config.rtl ) {
4538
if( ( isOverview() || previousFragment() === false ) && availableRoutes().right ) {
4539
slide( indexh - 1 );
4540
}
4541
}
4542
// Normal navigation
4543
else if( ( isOverview() || nextFragment() === false ) && availableRoutes().right ) {
4544
slide( indexh + 1 );
4545
}
4546
4547
}
4548
4549
function navigateUp() {
4550
4551
// Prioritize hiding fragments
4552
if( ( isOverview() || previousFragment() === false ) && availableRoutes().up ) {
4553
slide( indexh, indexv - 1 );
4554
}
4555
4556
}
4557
4558
function navigateDown() {
4559
4560
hasNavigatedDown = true;
4561
4562
// Prioritize revealing fragments
4563
if( ( isOverview() || nextFragment() === false ) && availableRoutes().down ) {
4564
slide( indexh, indexv + 1 );
4565
}
4566
4567
}
4568
4569
/**
4570
* Navigates backwards, prioritized in the following order:
4571
* 1) Previous fragment
4572
* 2) Previous vertical slide
4573
* 3) Previous horizontal slide
4574
*/
4575
function navigatePrev() {
4576
4577
// Prioritize revealing fragments
4578
if( previousFragment() === false ) {
4579
if( availableRoutes().up ) {
4580
navigateUp();
4581
}
4582
else {
4583
// Fetch the previous horizontal slide, if there is one
4584
var previousSlide;
4585
4586
if( config.rtl ) {
4587
previousSlide = toArray( dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR + '.future' ) ).pop();
4588
}
4589
else {
4590
previousSlide = toArray( dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR + '.past' ) ).pop();
4591
}
4592
4593
if( previousSlide ) {
4594
var v = ( previousSlide.querySelectorAll( 'section' ).length - 1 ) || undefined;
4595
var h = indexh - 1;
4596
slide( h, v );
4597
}
4598
}
4599
}
4600
4601
}
4602
4603
/**
4604
* The reverse of #navigatePrev().
4605
*/
4606
function navigateNext() {
4607
4608
hasNavigatedRight = true;
4609
hasNavigatedDown = true;
4610
4611
// Prioritize revealing fragments
4612
if( nextFragment() === false ) {
4613
4614
var routes = availableRoutes();
4615
4616
// When looping is enabled `routes.down` is always available
4617
// so we need a separate check for when we've reached the
4618
// end of a stack and should move horizontally
4619
if( routes.down && routes.right && config.loop && Reveal.isLastVerticalSlide( currentSlide ) ) {
4620
routes.down = false;
4621
}
4622
4623
if( routes.down ) {
4624
navigateDown();
4625
}
4626
else if( config.rtl ) {
4627
navigateLeft();
4628
}
4629
else {
4630
navigateRight();
4631
}
4632
}
4633
4634
}
4635
4636
/**
4637
* Checks if the target element prevents the triggering of
4638
* swipe navigation.
4639
*/
4640
function isSwipePrevented( target ) {
4641
4642
while( target && typeof target.hasAttribute === 'function' ) {
4643
if( target.hasAttribute( 'data-prevent-swipe' ) ) return true;
4644
target = target.parentNode;
4645
}
4646
4647
return false;
4648
4649
}
4650
4651
4652
// --------------------------------------------------------------------//
4653
// ----------------------------- EVENTS -------------------------------//
4654
// --------------------------------------------------------------------//
4655
4656
/**
4657
* Called by all event handlers that are based on user
4658
* input.
4659
*
4660
* @param {object} [event]
4661
*/
4662
function onUserInput( event ) {
4663
4664
if( config.autoSlideStoppable ) {
4665
pauseAutoSlide();
4666
}
4667
4668
}
4669
4670
/**
4671
* Handler for the document level 'keypress' event.
4672
*
4673
* @param {object} event
4674
*/
4675
function onDocumentKeyPress( event ) {
4676
4677
// Check if the pressed key is question mark
4678
if( event.shiftKey && event.charCode === 63 ) {
4679
toggleHelp();
4680
}
4681
4682
}
4683
4684
/**
4685
* Handler for the document level 'keydown' event.
4686
*
4687
* @param {object} event
4688
*/
4689
function onDocumentKeyDown( event ) {
4690
4691
// If there's a condition specified and it returns false,
4692
// ignore this event
4693
if( typeof config.keyboardCondition === 'function' && config.keyboardCondition(event) === false ) {
4694
return true;
4695
}
4696
4697
// Remember if auto-sliding was paused so we can toggle it
4698
var autoSlideWasPaused = autoSlidePaused;
4699
4700
onUserInput( event );
4701
4702
// Check if there's a focused element that could be using
4703
// the keyboard
4704
var activeElementIsCE = document.activeElement && document.activeElement.contentEditable !== 'inherit';
4705
var activeElementIsInput = document.activeElement && document.activeElement.tagName && /input|textarea/i.test( document.activeElement.tagName );
4706
var activeElementIsNotes = document.activeElement && document.activeElement.className && /speaker-notes/i.test( document.activeElement.className);
4707
4708
// Disregard the event if there's a focused element or a
4709
// keyboard modifier key is present
4710
if( activeElementIsCE || activeElementIsInput || activeElementIsNotes || (event.shiftKey && event.keyCode !== 32) || event.altKey || event.ctrlKey || event.metaKey ) return;
4711
4712
// While paused only allow resume keyboard events; 'b', 'v', '.'
4713
var resumeKeyCodes = [66,86,190,191];
4714
var key;
4715
4716
// Custom key bindings for togglePause should be able to resume
4717
if( typeof config.keyboard === 'object' ) {
4718
for( key in config.keyboard ) {
4719
if( config.keyboard[key] === 'togglePause' ) {
4720
resumeKeyCodes.push( parseInt( key, 10 ) );
4721
}
4722
}
4723
}
4724
4725
if( isPaused() && resumeKeyCodes.indexOf( event.keyCode ) === -1 ) {
4726
return false;
4727
}
4728
4729
var triggered = false;
4730
4731
// 1. User defined key bindings
4732
if( typeof config.keyboard === 'object' ) {
4733
4734
for( key in config.keyboard ) {
4735
4736
// Check if this binding matches the pressed key
4737
if( parseInt( key, 10 ) === event.keyCode ) {
4738
4739
var value = config.keyboard[ key ];
4740
4741
// Callback function
4742
if( typeof value === 'function' ) {
4743
value.apply( null, [ event ] );
4744
}
4745
// String shortcuts to reveal.js API
4746
else if( typeof value === 'string' && typeof Reveal[ value ] === 'function' ) {
4747
Reveal[ value ].call();
4748
}
4749
4750
triggered = true;
4751
4752
}
4753
4754
}
4755
4756
}
4757
4758
// 2. Registered custom key bindings
4759
if( triggered === false ) {
4760
4761
for( key in registeredKeyBindings ) {
4762
4763
// Check if this binding matches the pressed key
4764
if( parseInt( key, 10 ) === event.keyCode ) {
4765
4766
var action = registeredKeyBindings[ key ].callback;
4767
4768
// Callback function
4769
if( typeof action === 'function' ) {
4770
action.apply( null, [ event ] );
4771
}
4772
// String shortcuts to reveal.js API
4773
else if( typeof action === 'string' && typeof Reveal[ action ] === 'function' ) {
4774
Reveal[ action ].call();
4775
}
4776
4777
triggered = true;
4778
}
4779
}
4780
}
4781
4782
// 3. System defined key bindings
4783
if( triggered === false ) {
4784
4785
// Assume true and try to prove false
4786
triggered = true;
4787
4788
switch( event.keyCode ) {
4789
// p, page up
4790
case 80: case 33: navigatePrev(); break;
4791
// n, page down
4792
case 78: case 34: navigateNext(); break;
4793
// h, left
4794
case 72: case 37: navigateLeft(); break;
4795
// l, right
4796
case 76: case 39: navigateRight(); break;
4797
// k, up
4798
case 75: case 38: navigateUp(); break;
4799
// j, down
4800
case 74: case 40: navigateDown(); break;
4801
// home
4802
case 36: slide( 0 ); break;
4803
// end
4804
case 35: slide( Number.MAX_VALUE ); break;
4805
// space
4806
case 32: isOverview() ? deactivateOverview() : event.shiftKey ? navigatePrev() : navigateNext(); break;
4807
// return
4808
case 13: isOverview() ? deactivateOverview() : triggered = false; break;
4809
// two-spot, semicolon, b, v, period, Logitech presenter tools "black screen" button
4810
case 58: case 59: case 66: case 86: case 190: case 191: togglePause(); break;
4811
// f
4812
case 70: enterFullscreen(); break;
4813
// a
4814
case 65: if ( config.autoSlideStoppable ) toggleAutoSlide( autoSlideWasPaused ); break;
4815
default:
4816
triggered = false;
4817
}
4818
4819
}
4820
4821
// If the input resulted in a triggered action we should prevent
4822
// the browsers default behavior
4823
if( triggered ) {
4824
event.preventDefault && event.preventDefault();
4825
}
4826
// ESC or O key
4827
else if ( ( event.keyCode === 27 || event.keyCode === 79 ) && features.transforms3d ) {
4828
if( dom.overlay ) {
4829
closeOverlay();
4830
}
4831
else {
4832
toggleOverview();
4833
}
4834
4835
event.preventDefault && event.preventDefault();
4836
}
4837
4838
// If auto-sliding is enabled we need to cue up
4839
// another timeout
4840
cueAutoSlide();
4841
4842
}
4843
4844
/**
4845
* Handler for the 'touchstart' event, enables support for
4846
* swipe and pinch gestures.
4847
*
4848
* @param {object} event
4849
*/
4850
function onTouchStart( event ) {
4851
4852
if( isSwipePrevented( event.target ) ) return true;
4853
4854
touch.startX = event.touches[0].clientX;
4855
touch.startY = event.touches[0].clientY;
4856
touch.startCount = event.touches.length;
4857
4858
// If there's two touches we need to memorize the distance
4859
// between those two points to detect pinching
4860
if( event.touches.length === 2 && config.overview ) {
4861
touch.startSpan = distanceBetween( {
4862
x: event.touches[1].clientX,
4863
y: event.touches[1].clientY
4864
}, {
4865
x: touch.startX,
4866
y: touch.startY
4867
} );
4868
}
4869
4870
}
4871
4872
/**
4873
* Handler for the 'touchmove' event.
4874
*
4875
* @param {object} event
4876
*/
4877
function onTouchMove( event ) {
4878
4879
if( isSwipePrevented( event.target ) ) return true;
4880
4881
// Each touch should only trigger one action
4882
if( !touch.captured ) {
4883
onUserInput( event );
4884
4885
var currentX = event.touches[0].clientX;
4886
var currentY = event.touches[0].clientY;
4887
4888
// If the touch started with two points and still has
4889
// two active touches; test for the pinch gesture
4890
if( event.touches.length === 2 && touch.startCount === 2 && config.overview ) {
4891
4892
// The current distance in pixels between the two touch points
4893
var currentSpan = distanceBetween( {
4894
x: event.touches[1].clientX,
4895
y: event.touches[1].clientY
4896
}, {
4897
x: touch.startX,
4898
y: touch.startY
4899
} );
4900
4901
// If the span is larger than the desire amount we've got
4902
// ourselves a pinch
4903
if( Math.abs( touch.startSpan - currentSpan ) > touch.threshold ) {
4904
touch.captured = true;
4905
4906
if( currentSpan < touch.startSpan ) {
4907
activateOverview();
4908
}
4909
else {
4910
deactivateOverview();
4911
}
4912
}
4913
4914
event.preventDefault();
4915
4916
}
4917
// There was only one touch point, look for a swipe
4918
else if( event.touches.length === 1 && touch.startCount !== 2 ) {
4919
4920
var deltaX = currentX - touch.startX,
4921
deltaY = currentY - touch.startY;
4922
4923
if( deltaX > touch.threshold && Math.abs( deltaX ) > Math.abs( deltaY ) ) {
4924
touch.captured = true;
4925
navigateLeft();
4926
}
4927
else if( deltaX < -touch.threshold && Math.abs( deltaX ) > Math.abs( deltaY ) ) {
4928
touch.captured = true;
4929
navigateRight();
4930
}
4931
else if( deltaY > touch.threshold ) {
4932
touch.captured = true;
4933
navigateUp();
4934
}
4935
else if( deltaY < -touch.threshold ) {
4936
touch.captured = true;
4937
navigateDown();
4938
}
4939
4940
// If we're embedded, only block touch events if they have
4941
// triggered an action
4942
if( config.embedded ) {
4943
if( touch.captured || isVerticalSlide( currentSlide ) ) {
4944
event.preventDefault();
4945
}
4946
}
4947
// Not embedded? Block them all to avoid needless tossing
4948
// around of the viewport in iOS
4949
else {
4950
event.preventDefault();
4951
}
4952
4953
}
4954
}
4955
// There's a bug with swiping on some Android devices unless
4956
// the default action is always prevented
4957
else if( UA.match( /android/gi ) ) {
4958
event.preventDefault();
4959
}
4960
4961
}
4962
4963
/**
4964
* Handler for the 'touchend' event.
4965
*
4966
* @param {object} event
4967
*/
4968
function onTouchEnd( event ) {
4969
4970
touch.captured = false;
4971
4972
}
4973
4974
/**
4975
* Convert pointer down to touch start.
4976
*
4977
* @param {object} event
4978
*/
4979
function onPointerDown( event ) {
4980
4981
if( event.pointerType === event.MSPOINTER_TYPE_TOUCH || event.pointerType === "touch" ) {
4982
event.touches = [{ clientX: event.clientX, clientY: event.clientY }];
4983
onTouchStart( event );
4984
}
4985
4986
}
4987
4988
/**
4989
* Convert pointer move to touch move.
4990
*
4991
* @param {object} event
4992
*/
4993
function onPointerMove( event ) {
4994
4995
if( event.pointerType === event.MSPOINTER_TYPE_TOUCH || event.pointerType === "touch" ) {
4996
event.touches = [{ clientX: event.clientX, clientY: event.clientY }];
4997
onTouchMove( event );
4998
}
4999
5000
}
5001
5002
/**
5003
* Convert pointer up to touch end.
5004
*
5005
* @param {object} event
5006
*/
5007
function onPointerUp( event ) {
5008
5009
if( event.pointerType === event.MSPOINTER_TYPE_TOUCH || event.pointerType === "touch" ) {
5010
event.touches = [{ clientX: event.clientX, clientY: event.clientY }];
5011
onTouchEnd( event );
5012
}
5013
5014
}
5015
5016
/**
5017
* Handles mouse wheel scrolling, throttled to avoid skipping
5018
* multiple slides.
5019
*
5020
* @param {object} event
5021
*/
5022
function onDocumentMouseScroll( event ) {
5023
5024
if( Date.now() - lastMouseWheelStep > 600 ) {
5025
5026
lastMouseWheelStep = Date.now();
5027
5028
var delta = event.detail || -event.wheelDelta;
5029
if( delta > 0 ) {
5030
navigateNext();
5031
}
5032
else if( delta < 0 ) {
5033
navigatePrev();
5034
}
5035
5036
}
5037
5038
}
5039
5040
/**
5041
* Clicking on the progress bar results in a navigation to the
5042
* closest approximate horizontal slide using this equation:
5043
*
5044
* ( clickX / presentationWidth ) * numberOfSlides
5045
*
5046
* @param {object} event
5047
*/
5048
function onProgressClicked( event ) {
5049
5050
onUserInput( event );
5051
5052
event.preventDefault();
5053
5054
var slidesTotal = toArray( dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) ).length;
5055
var slideIndex = Math.floor( ( event.clientX / dom.wrapper.offsetWidth ) * slidesTotal );
5056
5057
if( config.rtl ) {
5058
slideIndex = slidesTotal - slideIndex;
5059
}
5060
5061
slide( slideIndex );
5062
5063
}
5064
5065
/**
5066
* Event handler for navigation control buttons.
5067
*/
5068
function onNavigateLeftClicked( event ) { event.preventDefault(); onUserInput(); navigateLeft(); }
5069
function onNavigateRightClicked( event ) { event.preventDefault(); onUserInput(); navigateRight(); }
5070
function onNavigateUpClicked( event ) { event.preventDefault(); onUserInput(); navigateUp(); }
5071
function onNavigateDownClicked( event ) { event.preventDefault(); onUserInput(); navigateDown(); }
5072
function onNavigatePrevClicked( event ) { event.preventDefault(); onUserInput(); navigatePrev(); }
5073
function onNavigateNextClicked( event ) { event.preventDefault(); onUserInput(); navigateNext(); }
5074
5075
/**
5076
* Handler for the window level 'hashchange' event.
5077
*
5078
* @param {object} [event]
5079
*/
5080
function onWindowHashChange( event ) {
5081
5082
readURL();
5083
5084
}
5085
5086
/**
5087
* Handler for the window level 'resize' event.
5088
*
5089
* @param {object} [event]
5090
*/
5091
function onWindowResize( event ) {
5092
5093
layout();
5094
5095
}
5096
5097
/**
5098
* Handle for the window level 'visibilitychange' event.
5099
*
5100
* @param {object} [event]
5101
*/
5102
function onPageVisibilityChange( event ) {
5103
5104
var isHidden = document.webkitHidden ||
5105
document.msHidden ||
5106
document.hidden;
5107
5108
// If, after clicking a link or similar and we're coming back,
5109
// focus the document.body to ensure we can use keyboard shortcuts
5110
if( isHidden === false && document.activeElement !== document.body ) {
5111
// Not all elements support .blur() - SVGs among them.
5112
if( typeof document.activeElement.blur === 'function' ) {
5113
document.activeElement.blur();
5114
}
5115
document.body.focus();
5116
}
5117
5118
}
5119
5120
/**
5121
* Invoked when a slide is and we're in the overview.
5122
*
5123
* @param {object} event
5124
*/
5125
function onOverviewSlideClicked( event ) {
5126
5127
// TODO There's a bug here where the event listeners are not
5128
// removed after deactivating the overview.
5129
if( eventsAreBound && isOverview() ) {
5130
event.preventDefault();
5131
5132
var element = event.target;
5133
5134
while( element && !element.nodeName.match( /section/gi ) ) {
5135
element = element.parentNode;
5136
}
5137
5138
if( element && !element.classList.contains( 'disabled' ) ) {
5139
5140
deactivateOverview();
5141
5142
if( element.nodeName.match( /section/gi ) ) {
5143
var h = parseInt( element.getAttribute( 'data-index-h' ), 10 ),
5144
v = parseInt( element.getAttribute( 'data-index-v' ), 10 );
5145
5146
slide( h, v );
5147
}
5148
5149
}
5150
}
5151
5152
}
5153
5154
/**
5155
* Handles clicks on links that are set to preview in the
5156
* iframe overlay.
5157
*
5158
* @param {object} event
5159
*/
5160
function onPreviewLinkClicked( event ) {
5161
5162
if( event.currentTarget && event.currentTarget.hasAttribute( 'href' ) ) {
5163
var url = event.currentTarget.getAttribute( 'href' );
5164
if( url ) {
5165
showPreview( url );
5166
event.preventDefault();
5167
}
5168
}
5169
5170
}
5171
5172
/**
5173
* Handles click on the auto-sliding controls element.
5174
*
5175
* @param {object} [event]
5176
*/
5177
function onAutoSlidePlayerClick( event ) {
5178
5179
// Replay
5180
if( Reveal.isLastSlide() && config.loop === false ) {
5181
slide( 0, 0 );
5182
resumeAutoSlide();
5183
}
5184
// Resume
5185
else if( autoSlidePaused ) {
5186
resumeAutoSlide();
5187
}
5188
// Pause
5189
else {
5190
pauseAutoSlide();
5191
}
5192
5193
}
5194
5195
5196
// --------------------------------------------------------------------//
5197
// ------------------------ PLAYBACK COMPONENT ------------------------//
5198
// --------------------------------------------------------------------//
5199
5200
5201
/**
5202
* Constructor for the playback component, which displays
5203
* play/pause/progress controls.
5204
*
5205
* @param {HTMLElement} container The component will append
5206
* itself to this
5207
* @param {function} progressCheck A method which will be
5208
* called frequently to get the current progress on a range
5209
* of 0-1
5210
*/
5211
function Playback( container, progressCheck ) {
5212
5213
// Cosmetics
5214
this.diameter = 100;
5215
this.diameter2 = this.diameter/2;
5216
this.thickness = 6;
5217
5218
// Flags if we are currently playing
5219
this.playing = false;
5220
5221
// Current progress on a 0-1 range
5222
this.progress = 0;
5223
5224
// Used to loop the animation smoothly
5225
this.progressOffset = 1;
5226
5227
this.container = container;
5228
this.progressCheck = progressCheck;
5229
5230
this.canvas = document.createElement( 'canvas' );
5231
this.canvas.className = 'playback';
5232
this.canvas.width = this.diameter;
5233
this.canvas.height = this.diameter;
5234
this.canvas.style.width = this.diameter2 + 'px';
5235
this.canvas.style.height = this.diameter2 + 'px';
5236
this.context = this.canvas.getContext( '2d' );
5237
5238
this.container.appendChild( this.canvas );
5239
5240
this.render();
5241
5242
}
5243
5244
/**
5245
* @param value
5246
*/
5247
Playback.prototype.setPlaying = function( value ) {
5248
5249
var wasPlaying = this.playing;
5250
5251
this.playing = value;
5252
5253
// Start repainting if we weren't already
5254
if( !wasPlaying && this.playing ) {
5255
this.animate();
5256
}
5257
else {
5258
this.render();
5259
}
5260
5261
};
5262
5263
Playback.prototype.animate = function() {
5264
5265
var progressBefore = this.progress;
5266
5267
this.progress = this.progressCheck();
5268
5269
// When we loop, offset the progress so that it eases
5270
// smoothly rather than immediately resetting
5271
if( progressBefore > 0.8 && this.progress < 0.2 ) {
5272
this.progressOffset = this.progress;
5273
}
5274
5275
this.render();
5276
5277
if( this.playing ) {
5278
features.requestAnimationFrameMethod.call( window, this.animate.bind( this ) );
5279
}
5280
5281
};
5282
5283
/**
5284
* Renders the current progress and playback state.
5285
*/
5286
Playback.prototype.render = function() {
5287
5288
var progress = this.playing ? this.progress : 0,
5289
radius = ( this.diameter2 ) - this.thickness,
5290
x = this.diameter2,
5291
y = this.diameter2,
5292
iconSize = 28;
5293
5294
// Ease towards 1
5295
this.progressOffset += ( 1 - this.progressOffset ) * 0.1;
5296
5297
var endAngle = ( - Math.PI / 2 ) + ( progress * ( Math.PI * 2 ) );
5298
var startAngle = ( - Math.PI / 2 ) + ( this.progressOffset * ( Math.PI * 2 ) );
5299
5300
this.context.save();
5301
this.context.clearRect( 0, 0, this.diameter, this.diameter );
5302
5303
// Solid background color
5304
this.context.beginPath();
5305
this.context.arc( x, y, radius + 4, 0, Math.PI * 2, false );
5306
this.context.fillStyle = 'rgba( 0, 0, 0, 0.4 )';
5307
this.context.fill();
5308
5309
// Draw progress track
5310
this.context.beginPath();
5311
this.context.arc( x, y, radius, 0, Math.PI * 2, false );
5312
this.context.lineWidth = this.thickness;
5313
this.context.strokeStyle = 'rgba( 255, 255, 255, 0.2 )';
5314
this.context.stroke();
5315
5316
if( this.playing ) {
5317
// Draw progress on top of track
5318
this.context.beginPath();
5319
this.context.arc( x, y, radius, startAngle, endAngle, false );
5320
this.context.lineWidth = this.thickness;
5321
this.context.strokeStyle = '#fff';
5322
this.context.stroke();
5323
}
5324
5325
this.context.translate( x - ( iconSize / 2 ), y - ( iconSize / 2 ) );
5326
5327
// Draw play/pause icons
5328
if( this.playing ) {
5329
this.context.fillStyle = '#fff';
5330
this.context.fillRect( 0, 0, iconSize / 2 - 4, iconSize );
5331
this.context.fillRect( iconSize / 2 + 4, 0, iconSize / 2 - 4, iconSize );
5332
}
5333
else {
5334
this.context.beginPath();
5335
this.context.translate( 4, 0 );
5336
this.context.moveTo( 0, 0 );
5337
this.context.lineTo( iconSize - 4, iconSize / 2 );
5338
this.context.lineTo( 0, iconSize );
5339
this.context.fillStyle = '#fff';
5340
this.context.fill();
5341
}
5342
5343
this.context.restore();
5344
5345
};
5346
5347
Playback.prototype.on = function( type, listener ) {
5348
this.canvas.addEventListener( type, listener, false );
5349
};
5350
5351
Playback.prototype.off = function( type, listener ) {
5352
this.canvas.removeEventListener( type, listener, false );
5353
};
5354
5355
Playback.prototype.destroy = function() {
5356
5357
this.playing = false;
5358
5359
if( this.canvas.parentNode ) {
5360
this.container.removeChild( this.canvas );
5361
}
5362
5363
};
5364
5365
5366
// --------------------------------------------------------------------//
5367
// ------------------------------- API --------------------------------//
5368
// --------------------------------------------------------------------//
5369
5370
5371
Reveal = {
5372
VERSION: VERSION,
5373
5374
initialize: initialize,
5375
configure: configure,
5376
5377
sync: sync,
5378
syncSlide: syncSlide,
5379
syncFragments: syncFragments,
5380
5381
// Navigation methods
5382
slide: slide,
5383
left: navigateLeft,
5384
right: navigateRight,
5385
up: navigateUp,
5386
down: navigateDown,
5387
prev: navigatePrev,
5388
next: navigateNext,
5389
5390
// Fragment methods
5391
navigateFragment: navigateFragment,
5392
prevFragment: previousFragment,
5393
nextFragment: nextFragment,
5394
5395
// Deprecated aliases
5396
navigateTo: slide,
5397
navigateLeft: navigateLeft,
5398
navigateRight: navigateRight,
5399
navigateUp: navigateUp,
5400
navigateDown: navigateDown,
5401
navigatePrev: navigatePrev,
5402
navigateNext: navigateNext,
5403
5404
// Forces an update in slide layout
5405
layout: layout,
5406
5407
// Randomizes the order of slides
5408
shuffle: shuffle,
5409
5410
// Returns an object with the available routes as booleans (left/right/top/bottom)
5411
availableRoutes: availableRoutes,
5412
5413
// Returns an object with the available fragments as booleans (prev/next)
5414
availableFragments: availableFragments,
5415
5416
// Toggles a help overlay with keyboard shortcuts
5417
toggleHelp: toggleHelp,
5418
5419
// Toggles the overview mode on/off
5420
toggleOverview: toggleOverview,
5421
5422
// Toggles the "black screen" mode on/off
5423
togglePause: togglePause,
5424
5425
// Toggles the auto slide mode on/off
5426
toggleAutoSlide: toggleAutoSlide,
5427
5428
// State checks
5429
isOverview: isOverview,
5430
isPaused: isPaused,
5431
isAutoSliding: isAutoSliding,
5432
isSpeakerNotes: isSpeakerNotes,
5433
5434
// Slide preloading
5435
loadSlide: loadSlide,
5436
unloadSlide: unloadSlide,
5437
5438
// Adds or removes all internal event listeners (such as keyboard)
5439
addEventListeners: addEventListeners,
5440
removeEventListeners: removeEventListeners,
5441
5442
// Facility for persisting and restoring the presentation state
5443
getState: getState,
5444
setState: setState,
5445
5446
// Presentation progress
5447
getSlidePastCount: getSlidePastCount,
5448
5449
// Presentation progress on range of 0-1
5450
getProgress: getProgress,
5451
5452
// Returns the indices of the current, or specified, slide
5453
getIndices: getIndices,
5454
5455
// Returns an Array of all slides
5456
getSlides: getSlides,
5457
5458
// Returns the total number of slides
5459
getTotalSlides: getTotalSlides,
5460
5461
// Returns the slide element at the specified index
5462
getSlide: getSlide,
5463
5464
// Returns the slide background element at the specified index
5465
getSlideBackground: getSlideBackground,
5466
5467
// Returns the speaker notes string for a slide, or null
5468
getSlideNotes: getSlideNotes,
5469
5470
// Returns the previous slide element, may be null
5471
getPreviousSlide: function() {
5472
return previousSlide;
5473
},
5474
5475
// Returns the current slide element
5476
getCurrentSlide: function() {
5477
return currentSlide;
5478
},
5479
5480
// Returns the current scale of the presentation content
5481
getScale: function() {
5482
return scale;
5483
},
5484
5485
// Returns the current configuration object
5486
getConfig: function() {
5487
return config;
5488
},
5489
5490
// Helper method, retrieves query string as a key/value hash
5491
getQueryHash: function() {
5492
var query = {};
5493
5494
location.search.replace( /[A-Z0-9]+?=([\w\.%-]*)/gi, function(a) {
5495
query[ a.split( '=' ).shift() ] = a.split( '=' ).pop();
5496
} );
5497
5498
// Basic deserialization
5499
for( var i in query ) {
5500
var value = query[ i ];
5501
5502
query[ i ] = deserialize( unescape( value ) );
5503
}
5504
5505
return query;
5506
},
5507
5508
// Returns true if we're currently on the first slide
5509
isFirstSlide: function() {
5510
return ( indexh === 0 && indexv === 0 );
5511
},
5512
5513
// Returns true if we're currently on the last slide
5514
isLastSlide: function() {
5515
if( currentSlide ) {
5516
// Does this slide have a next sibling?
5517
if( currentSlide.nextElementSibling ) return false;
5518
5519
// If it's vertical, does its parent have a next sibling?
5520
if( isVerticalSlide( currentSlide ) && currentSlide.parentNode.nextElementSibling ) return false;
5521
5522
return true;
5523
}
5524
5525
return false;
5526
},
5527
5528
// Returns true if we're on the last slide in the current
5529
// vertical stack
5530
isLastVerticalSlide: function() {
5531
if( currentSlide && isVerticalSlide( currentSlide ) ) {
5532
// Does this slide have a next sibling?
5533
if( currentSlide.nextElementSibling ) return false;
5534
5535
return true;
5536
}
5537
5538
return false;
5539
},
5540
5541
// Checks if reveal.js has been loaded and is ready for use
5542
isReady: function() {
5543
return loaded;
5544
},
5545
5546
// Forward event binding to the reveal DOM element
5547
addEventListener: function( type, listener, useCapture ) {
5548
if( 'addEventListener' in window ) {
5549
( dom.wrapper || document.querySelector( '.reveal' ) ).addEventListener( type, listener, useCapture );
5550
}
5551
},
5552
removeEventListener: function( type, listener, useCapture ) {
5553
if( 'addEventListener' in window ) {
5554
( dom.wrapper || document.querySelector( '.reveal' ) ).removeEventListener( type, listener, useCapture );
5555
}
5556
},
5557
5558
// Adds a custom key binding
5559
addKeyBinding: addKeyBinding,
5560
5561
// Removes a custom key binding
5562
removeKeyBinding: removeKeyBinding,
5563
5564
// Programatically triggers a keyboard event
5565
triggerKey: function( keyCode ) {
5566
onDocumentKeyDown( { keyCode: keyCode } );
5567
},
5568
5569
// Registers a new shortcut to include in the help overlay
5570
registerKeyboardShortcut: function( key, value ) {
5571
keyboardShortcuts[key] = value;
5572
}
5573
};
5574
5575
return Reveal;
5576
5577
}));
5578
5579