reveal.js 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378
  1. /*!
  2. * reveal.js 2.1 r35
  3. * http://lab.hakim.se/reveal-js
  4. * MIT licensed
  5. *
  6. * Copyright (C) 2011-2012 Hakim El Hattab, http://hakim.se
  7. */
  8. var Reveal = (function(){
  9. 'use strict';
  10. var HORIZONTAL_SLIDES_SELECTOR = '.reveal .slides>section',
  11. VERTICAL_SLIDES_SELECTOR = '.reveal .slides>section.present>section',
  12. // Configurations defaults, can be overridden at initialization time
  13. config = {
  14. // Display controls in the bottom right corner
  15. controls: true,
  16. // Display a presentation progress bar
  17. progress: true,
  18. // Push each slide change to the browser history
  19. history: false,
  20. // Enable keyboard shortcuts for navigation
  21. keyboard: true,
  22. // Enable the slide overview mode
  23. overview: true,
  24. // Loop the presentation
  25. loop: false,
  26. // Number of milliseconds between automatically proceeding to the
  27. // next slide, disabled when set to 0, this value can be overwritten
  28. // by using a data-autoslide attribute on your slides
  29. autoSlide: 0,
  30. // Enable slide navigation via mouse wheel
  31. mouseWheel: true,
  32. // Apply a 3D roll to links on hover
  33. rollingLinks: true,
  34. // Transition style (see /css/theme)
  35. theme: null,
  36. // Transition style
  37. transition: 'default', // default/cube/page/concave/zoom/linear/none
  38. // Script dependencies to load
  39. dependencies: []
  40. },
  41. // Stores if the next slide should be shown automatically
  42. // after n milliseconds
  43. autoSlide = config.autoSlide,
  44. // The horizontal and verical index of the currently active slide
  45. indexh = 0,
  46. indexv = 0,
  47. // The previous and current slide HTML elements
  48. previousSlide,
  49. currentSlide,
  50. // Slides may hold a data-state attribute which we pick up and apply
  51. // as a class to the body. This list contains the combined state of
  52. // all current slides.
  53. state = [],
  54. // Cached references to DOM elements
  55. dom = {},
  56. // Detect support for CSS 3D transforms
  57. supports3DTransforms = 'WebkitPerspective' in document.body.style ||
  58. 'MozPerspective' in document.body.style ||
  59. 'msPerspective' in document.body.style ||
  60. 'OPerspective' in document.body.style ||
  61. 'perspective' in document.body.style,
  62. supports2DTransforms = 'WebkitTransform' in document.body.style ||
  63. 'MozTransform' in document.body.style ||
  64. 'msTransform' in document.body.style ||
  65. 'OTransform' in document.body.style ||
  66. 'transform' in document.body.style,
  67. // Throttles mouse wheel navigation
  68. mouseWheelTimeout = 0,
  69. // An interval used to automatically move on to the next slide
  70. autoSlideTimeout = 0,
  71. // Delays updates to the URL due to a Chrome thumbnailer bug
  72. writeURLTimeout = 0,
  73. // Holds information about the currently ongoing touch input
  74. touch = {
  75. startX: 0,
  76. startY: 0,
  77. startSpan: 0,
  78. startCount: 0,
  79. handled: false,
  80. threshold: 80
  81. };
  82. /**
  83. * Starts up the presentation if the client is capable.
  84. */
  85. function initialize( options ) {
  86. if( ( !supports2DTransforms && !supports3DTransforms ) ) {
  87. document.body.setAttribute( 'class', 'no-transforms' );
  88. // If the browser doesn't support core features we won't be
  89. // using JavaScript to control the presentation
  90. return;
  91. }
  92. // Copy options over to our config object
  93. extend( config, options );
  94. // Hide the address bar in mobile browsers
  95. hideAddressBar();
  96. // Loads the dependencies and continues to #start() once done
  97. load();
  98. }
  99. /**
  100. * Finds and stores references to DOM elements which are
  101. * required by the presentation. If a required element is
  102. * not found, it is created.
  103. */
  104. function setupDOM() {
  105. // Cache references to key DOM elements
  106. dom.theme = document.querySelector( '#theme' );
  107. dom.wrapper = document.querySelector( '.reveal' );
  108. // Progress bar
  109. if( !dom.wrapper.querySelector( '.progress' ) && config.progress ) {
  110. var progressElement = document.createElement( 'div' );
  111. progressElement.classList.add( 'progress' );
  112. progressElement.innerHTML = '<span></span>';
  113. dom.wrapper.appendChild( progressElement );
  114. }
  115. // Arrow controls
  116. if( !dom.wrapper.querySelector( '.controls' ) && config.controls ) {
  117. var controlsElement = document.createElement( 'aside' );
  118. controlsElement.classList.add( 'controls' );
  119. controlsElement.innerHTML = '<div class="left"></div>' +
  120. '<div class="right"></div>' +
  121. '<div class="up"></div>' +
  122. '<div class="down"></div>';
  123. dom.wrapper.appendChild( controlsElement );
  124. }
  125. // Presentation background element
  126. if( !dom.wrapper.querySelector( '.state-background' ) ) {
  127. var backgroundElement = document.createElement( 'div' );
  128. backgroundElement.classList.add( 'state-background' );
  129. dom.wrapper.appendChild( backgroundElement );
  130. }
  131. // Overlay graphic which is displayed during the paused mode
  132. if( !dom.wrapper.querySelector( '.pause-overlay' ) ) {
  133. var pausedElement = document.createElement( 'div' );
  134. pausedElement.classList.add( 'pause-overlay' );
  135. dom.wrapper.appendChild( pausedElement );
  136. }
  137. // Cache references to elements
  138. dom.progress = document.querySelector( '.reveal .progress' );
  139. dom.progressbar = document.querySelector( '.reveal .progress span' );
  140. if ( config.controls ) {
  141. dom.controls = document.querySelector( '.reveal .controls' );
  142. dom.controlsLeft = document.querySelector( '.reveal .controls .left' );
  143. dom.controlsRight = document.querySelector( '.reveal .controls .right' );
  144. dom.controlsUp = document.querySelector( '.reveal .controls .up' );
  145. dom.controlsDown = document.querySelector( '.reveal .controls .down' );
  146. }
  147. }
  148. /**
  149. * Hides the address bar if we're on a mobile device.
  150. */
  151. function hideAddressBar() {
  152. if( navigator.userAgent.match( /(iphone|ipod|android)/i ) ) {
  153. // Give the page some scrollable overflow
  154. document.documentElement.style.overflow = 'scroll';
  155. document.body.style.height = '120%';
  156. // Events that should trigger the address bar to hide
  157. window.addEventListener( 'load', removeAddressBar, false );
  158. window.addEventListener( 'orientationchange', removeAddressBar, false );
  159. }
  160. }
  161. /**
  162. * Loads the dependencies of reveal.js. Dependencies are
  163. * defined via the configuration option 'dependencies'
  164. * and will be loaded prior to starting/binding reveal.js.
  165. * Some dependencies may have an 'async' flag, if so they
  166. * will load after reveal.js has been started up.
  167. */
  168. function load() {
  169. var scripts = [],
  170. scriptsAsync = [];
  171. for( var i = 0, len = config.dependencies.length; i < len; i++ ) {
  172. var s = config.dependencies[i];
  173. // Load if there's no condition or the condition is truthy
  174. if( !s.condition || s.condition() ) {
  175. if( s.async ) {
  176. scriptsAsync.push( s.src );
  177. }
  178. else {
  179. scripts.push( s.src );
  180. }
  181. // Extension may contain callback functions
  182. if( typeof s.callback === 'function' ) {
  183. head.ready( s.src.match( /([\w\d_\-]*)\.?[^\\\/]*$/i )[0], s.callback );
  184. }
  185. }
  186. }
  187. // Called once synchronous scritps finish loading
  188. function proceed() {
  189. // Load asynchronous scripts
  190. head.js.apply( null, scriptsAsync );
  191. start();
  192. }
  193. if( scripts.length ) {
  194. head.ready( proceed );
  195. // Load synchronous scripts
  196. head.js.apply( null, scripts );
  197. }
  198. else {
  199. proceed();
  200. }
  201. }
  202. /**
  203. * Starts up reveal.js by binding input events and navigating
  204. * to the current URL deeplink if there is one.
  205. */
  206. function start() {
  207. // Make sure we've got all the DOM elements we need
  208. setupDOM();
  209. // Subscribe to input
  210. addEventListeners();
  211. // Updates the presentation to match the current configuration values
  212. configure();
  213. // Read the initial hash
  214. readURL();
  215. // Start auto-sliding if it's enabled
  216. cueAutoSlide();
  217. // Notify listeners that the presentation is ready
  218. dispatchEvent( 'ready', {
  219. 'indexh': indexh,
  220. 'indexv': indexv,
  221. 'currentSlide': currentSlide
  222. } );
  223. }
  224. /**
  225. * Applies the configuration settings from the config object.
  226. */
  227. function configure() {
  228. if( supports3DTransforms === false ) {
  229. config.transition = 'linear';
  230. }
  231. if( config.controls && dom.controls ) {
  232. dom.controls.style.display = 'block';
  233. }
  234. if( config.progress && dom.progress ) {
  235. dom.progress.style.display = 'block';
  236. }
  237. if( config.transition !== 'default' ) {
  238. dom.wrapper.classList.add( config.transition );
  239. }
  240. if( config.mouseWheel ) {
  241. document.addEventListener( 'DOMMouseScroll', onDocumentMouseScroll, false ); // FF
  242. document.addEventListener( 'mousewheel', onDocumentMouseScroll, false );
  243. }
  244. // 3D links
  245. if( config.rollingLinks ) {
  246. linkify();
  247. }
  248. // Load the theme in the config, if it's not already loaded
  249. if( config.theme && dom.theme ) {
  250. var themeURL = dom.theme.getAttribute( 'href' );
  251. var themeFinder = /[^\/]*?(?=\.css)/;
  252. var themeName = themeURL.match(themeFinder)[0];
  253. if( config.theme !== themeName ) {
  254. themeURL = themeURL.replace(themeFinder, config.theme);
  255. dom.theme.setAttribute( 'href', themeURL );
  256. }
  257. }
  258. }
  259. /**
  260. * Binds all event listeners.
  261. */
  262. function addEventListeners() {
  263. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  264. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  265. document.addEventListener( 'touchend', onDocumentTouchEnd, false );
  266. window.addEventListener( 'hashchange', onWindowHashChange, false );
  267. if( config.keyboard ) {
  268. document.addEventListener( 'keydown', onDocumentKeyDown, false );
  269. }
  270. if ( config.progress && dom.progress ) {
  271. dom.progress.addEventListener( 'click', preventAndForward( onProgressClick ), false );
  272. }
  273. if ( config.controls && dom.controls ) {
  274. dom.controlsLeft.addEventListener( 'click', preventAndForward( navigateLeft ), false );
  275. dom.controlsRight.addEventListener( 'click', preventAndForward( navigateRight ), false );
  276. dom.controlsUp.addEventListener( 'click', preventAndForward( navigateUp ), false );
  277. dom.controlsDown.addEventListener( 'click', preventAndForward( navigateDown ), false );
  278. }
  279. }
  280. /**
  281. * Unbinds all event listeners.
  282. */
  283. function removeEventListeners() {
  284. document.removeEventListener( 'keydown', onDocumentKeyDown, false );
  285. document.removeEventListener( 'touchstart', onDocumentTouchStart, false );
  286. document.removeEventListener( 'touchmove', onDocumentTouchMove, false );
  287. document.removeEventListener( 'touchend', onDocumentTouchEnd, false );
  288. window.removeEventListener( 'hashchange', onWindowHashChange, false );
  289. if ( config.progress && dom.progress ) {
  290. dom.progress.removeEventListener( 'click', preventAndForward( onProgressClick ), false );
  291. }
  292. if ( config.controls && dom.controls ) {
  293. dom.controlsLeft.removeEventListener( 'click', preventAndForward( navigateLeft ), false );
  294. dom.controlsRight.removeEventListener( 'click', preventAndForward( navigateRight ), false );
  295. dom.controlsUp.removeEventListener( 'click', preventAndForward( navigateUp ), false );
  296. dom.controlsDown.removeEventListener( 'click', preventAndForward( navigateDown ), false );
  297. }
  298. }
  299. /**
  300. * Extend object a with the properties of object b.
  301. * If there's a conflict, object b takes precedence.
  302. */
  303. function extend( a, b ) {
  304. for( var i in b ) {
  305. a[ i ] = b[ i ];
  306. }
  307. }
  308. /**
  309. * Measures the distance in pixels between point a
  310. * and point b.
  311. *
  312. * @param {Object} a point with x/y properties
  313. * @param {Object} b point with x/y properties
  314. */
  315. function distanceBetween( a, b ) {
  316. var dx = a.x - b.x,
  317. dy = a.y - b.y;
  318. return Math.sqrt( dx*dx + dy*dy );
  319. }
  320. /**
  321. * Prevents an events defaults behavior calls the
  322. * specified delegate.
  323. *
  324. * @param {Function} delegate The method to call
  325. * after the wrapper has been executed
  326. */
  327. function preventAndForward( delegate ) {
  328. return function( event ) {
  329. event.preventDefault();
  330. delegate.call( null, event );
  331. };
  332. }
  333. /**
  334. * Causes the address bar to hide on mobile devices,
  335. * more vertical space ftw.
  336. */
  337. function removeAddressBar() {
  338. setTimeout( function() {
  339. window.scrollTo( 0, 1 );
  340. }, 0 );
  341. }
  342. /**
  343. * Dispatches an event of the specified type from the
  344. * reveal DOM element.
  345. */
  346. function dispatchEvent( type, properties ) {
  347. var event = document.createEvent( "HTMLEvents", 1, 2 );
  348. event.initEvent( type, true, true );
  349. extend( event, properties );
  350. dom.wrapper.dispatchEvent( event );
  351. }
  352. /**
  353. * Wrap all links in 3D goodness.
  354. */
  355. function linkify() {
  356. if( supports3DTransforms && !( 'msPerspective' in document.body.style ) ) {
  357. var nodes = document.querySelectorAll( '.reveal .slides section a:not(.image)' );
  358. for( var i = 0, len = nodes.length; i < len; i++ ) {
  359. var node = nodes[i];
  360. if( node.textContent && !node.querySelector( 'img' ) && ( !node.className || !node.classList.contains( node, 'roll' ) ) ) {
  361. node.classList.add( 'roll' );
  362. node.innerHTML = '<span data-title="'+ node.text +'">' + node.innerHTML + '</span>';
  363. }
  364. }
  365. }
  366. }
  367. /**
  368. * Displays the overview of slides (quick nav) by
  369. * scaling down and arranging all slide elements.
  370. *
  371. * Experimental feature, might be dropped if perf
  372. * can't be improved.
  373. */
  374. function activateOverview() {
  375. // Only proceed if enabled in config
  376. if( config.overview ) {
  377. dom.wrapper.classList.add( 'overview' );
  378. var horizontalSlides = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR );
  379. for( var i = 0, len1 = horizontalSlides.length; i < len1; i++ ) {
  380. var hslide = horizontalSlides[i],
  381. htransform = 'translateZ(-2500px) translate(' + ( ( i - indexh ) * 105 ) + '%, 0%)';
  382. hslide.setAttribute( 'data-index-h', i );
  383. hslide.style.display = 'block';
  384. hslide.style.WebkitTransform = htransform;
  385. hslide.style.MozTransform = htransform;
  386. hslide.style.msTransform = htransform;
  387. hslide.style.OTransform = htransform;
  388. hslide.style.transform = htransform;
  389. if( !hslide.classList.contains( 'stack' ) ) {
  390. // Navigate to this slide on click
  391. hslide.addEventListener( 'click', onOverviewSlideClicked, true );
  392. }
  393. var verticalSlides = hslide.querySelectorAll( 'section' );
  394. for( var j = 0, len2 = verticalSlides.length; j < len2; j++ ) {
  395. var vslide = verticalSlides[j],
  396. vtransform = 'translate(0%, ' + ( ( j - ( i === indexh ? indexv : 0 ) ) * 105 ) + '%)';
  397. vslide.setAttribute( 'data-index-h', i );
  398. vslide.setAttribute( 'data-index-v', j );
  399. vslide.style.display = 'block';
  400. vslide.style.WebkitTransform = vtransform;
  401. vslide.style.MozTransform = vtransform;
  402. vslide.style.msTransform = vtransform;
  403. vslide.style.OTransform = vtransform;
  404. vslide.style.transform = vtransform;
  405. // Navigate to this slide on click
  406. vslide.addEventListener( 'click', onOverviewSlideClicked, true );
  407. }
  408. }
  409. }
  410. }
  411. /**
  412. * Exits the slide overview and enters the currently
  413. * active slide.
  414. */
  415. function deactivateOverview() {
  416. // Only proceed if enabled in config
  417. if( config.overview ) {
  418. dom.wrapper.classList.remove( 'overview' );
  419. // Select all slides
  420. var slides = Array.prototype.slice.call( document.querySelectorAll( '.reveal .slides section' ) );
  421. for( var i = 0, len = slides.length; i < len; i++ ) {
  422. var element = slides[i];
  423. // Resets all transforms to use the external styles
  424. element.style.WebkitTransform = '';
  425. element.style.MozTransform = '';
  426. element.style.msTransform = '';
  427. element.style.OTransform = '';
  428. element.style.transform = '';
  429. element.removeEventListener( 'click', onOverviewSlideClicked );
  430. }
  431. slide();
  432. }
  433. }
  434. /**
  435. * Toggles the slide overview mode on and off.
  436. *
  437. * @param {Boolean} override Optional flag which overrides the
  438. * toggle logic and forcibly sets the desired state. True means
  439. * overview is open, false means it's closed.
  440. */
  441. function toggleOverview( override ) {
  442. if( typeof override === 'boolean' ) {
  443. override ? activateOverview() : deactivateOverview();
  444. }
  445. else {
  446. isOverviewActive() ? deactivateOverview() : activateOverview();
  447. }
  448. }
  449. /**
  450. * Checks if the overview is currently active.
  451. *
  452. * @return {Boolean} true if the overview is active,
  453. * false otherwise
  454. */
  455. function isOverviewActive() {
  456. return dom.wrapper.classList.contains( 'overview' );
  457. }
  458. /**
  459. * Handling the fullscreen functionality via the fullscreen API
  460. *
  461. * @see http://fullscreen.spec.whatwg.org/
  462. * @see https://developer.mozilla.org/en-US/docs/DOM/Using_fullscreen_mode
  463. */
  464. function enterFullscreen() {
  465. var element = document.body;
  466. // Check which implementation is available
  467. var requestMethod = element.requestFullScreen ||
  468. element.webkitRequestFullScreen ||
  469. element.mozRequestFullScreen ||
  470. element.msRequestFullScreen;
  471. if( requestMethod ) {
  472. requestMethod.apply( element );
  473. }
  474. }
  475. /**
  476. * Enters the paused mode which fades everything on screen to
  477. * black.
  478. */
  479. function pause() {
  480. dom.wrapper.classList.add( 'paused' );
  481. }
  482. /**
  483. * Exits from the paused mode.
  484. */
  485. function resume() {
  486. dom.wrapper.classList.remove( 'paused' );
  487. }
  488. /**
  489. * Toggles the paused mode on and off.
  490. */
  491. function togglePause() {
  492. if( isPaused() ) {
  493. resume();
  494. }
  495. else {
  496. pause();
  497. }
  498. }
  499. /**
  500. * Checks if we are currently in the paused mode.
  501. */
  502. function isPaused() {
  503. return dom.wrapper.classList.contains( 'paused' );
  504. }
  505. /**
  506. * Steps from the current point in the presentation to the
  507. * slide which matches the specified horizontal and vertical
  508. * indices.
  509. *
  510. * @param {int} h Horizontal index of the target slide
  511. * @param {int} v Vertical index of the target slide
  512. */
  513. function slide( h, v ) {
  514. // Remember where we were at before
  515. previousSlide = currentSlide;
  516. // Remember the state before this slide
  517. var stateBefore = state.concat();
  518. // Reset the state array
  519. state.length = 0;
  520. var indexhBefore = indexh,
  521. indexvBefore = indexv;
  522. // Activate and transition to the new slide
  523. indexh = updateSlides( HORIZONTAL_SLIDES_SELECTOR, h === undefined ? indexh : h );
  524. indexv = updateSlides( VERTICAL_SLIDES_SELECTOR, v === undefined ? indexv : v );
  525. // Apply the new state
  526. stateLoop: for( var i = 0, len = state.length; i < len; i++ ) {
  527. // Check if this state existed on the previous slide. If it
  528. // did, we will avoid adding it repeatedly.
  529. for( var j = 0; j < stateBefore.length; j++ ) {
  530. if( stateBefore[j] === state[i] ) {
  531. stateBefore.splice( j, 1 );
  532. continue stateLoop;
  533. }
  534. }
  535. document.documentElement.classList.add( state[i] );
  536. // Dispatch custom event matching the state's name
  537. dispatchEvent( state[i] );
  538. }
  539. // Clean up the remaints of the previous state
  540. while( stateBefore.length ) {
  541. document.documentElement.classList.remove( stateBefore.pop() );
  542. }
  543. // Update progress if enabled
  544. if( config.progress && dom.progress ) {
  545. dom.progressbar.style.width = ( indexh / ( document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ).length - 1 ) ) * window.innerWidth + 'px';
  546. }
  547. // If the overview is active, re-activate it to update positions
  548. if( isOverviewActive() ) {
  549. activateOverview();
  550. }
  551. updateControls();
  552. // Update the URL hash after a delay since updating it mid-transition
  553. // is likely to cause visual lag
  554. clearTimeout( writeURLTimeout );
  555. writeURLTimeout = setTimeout( writeURL, 1500 );
  556. // Query all horizontal slides in the deck
  557. var horizontalSlides = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR );
  558. // Find the current horizontal slide and any possible vertical slides
  559. // within it
  560. var currentHorizontalSlide = horizontalSlides[ indexh ],
  561. currentVerticalSlides = currentHorizontalSlide.querySelectorAll( 'section' );
  562. // Store references to the previous and current slides
  563. currentSlide = currentVerticalSlides[ indexv ] || currentHorizontalSlide;
  564. // Dispatch an event if the slide changed
  565. if( indexh !== indexhBefore || indexv !== indexvBefore ) {
  566. dispatchEvent( 'slidechanged', {
  567. 'indexh': indexh,
  568. 'indexv': indexv,
  569. 'previousSlide': previousSlide,
  570. 'currentSlide': currentSlide
  571. } );
  572. }
  573. else {
  574. // Ensure that the previous slide is never the same as the current
  575. previousSlide = null;
  576. }
  577. // Solves an edge case where the previous slide maintains the
  578. // 'present' class when navigating between adjacent vertical
  579. // stacks
  580. if( previousSlide ) {
  581. previousSlide.classList.remove( 'present' );
  582. }
  583. }
  584. /**
  585. * Updates one dimension of slides by showing the slide
  586. * with the specified index.
  587. *
  588. * @param {String} selector A CSS selector that will fetch
  589. * the group of slides we are working with
  590. * @param {Number} index The index of the slide that should be
  591. * shown
  592. *
  593. * @return {Number} The index of the slide that is now shown,
  594. * might differ from the passed in index if it was out of
  595. * bounds.
  596. */
  597. function updateSlides( selector, index ) {
  598. // Select all slides and convert the NodeList result to
  599. // an array
  600. var slides = Array.prototype.slice.call( document.querySelectorAll( selector ) ),
  601. slidesLength = slides.length;
  602. if( slidesLength ) {
  603. // Should the index loop?
  604. if( config.loop ) {
  605. index %= slidesLength;
  606. if( index < 0 ) {
  607. index = slidesLength + index;
  608. }
  609. }
  610. // Enforce max and minimum index bounds
  611. index = Math.max( Math.min( index, slidesLength - 1 ), 0 );
  612. for( var i = 0; i < slidesLength; i++ ) {
  613. var element = slides[i];
  614. // Optimization; hide all slides that are three or more steps
  615. // away from the present slide
  616. if( isOverviewActive() === false ) {
  617. // The distance loops so that it measures 1 between the first
  618. // and last slides
  619. var distance = Math.abs( ( index - i ) % ( slidesLength - 3 ) ) || 0;
  620. element.style.display = distance > 3 ? 'none' : 'block';
  621. }
  622. slides[i].classList.remove( 'past' );
  623. slides[i].classList.remove( 'present' );
  624. slides[i].classList.remove( 'future' );
  625. if( i < index ) {
  626. // Any element previous to index is given the 'past' class
  627. slides[i].classList.add( 'past' );
  628. }
  629. else if( i > index ) {
  630. // Any element subsequent to index is given the 'future' class
  631. slides[i].classList.add( 'future' );
  632. }
  633. // If this element contains vertical slides
  634. if( element.querySelector( 'section' ) ) {
  635. slides[i].classList.add( 'stack' );
  636. }
  637. }
  638. // Mark the current slide as present
  639. slides[index].classList.add( 'present' );
  640. // If this slide has a state associated with it, add it
  641. // onto the current state of the deck
  642. var slideState = slides[index].getAttribute( 'data-state' );
  643. if( slideState ) {
  644. state = state.concat( slideState.split( ' ' ) );
  645. }
  646. // If this slide has a data-autoslide attribtue associated use this as
  647. // autoSlide value otherwise use the global configured time
  648. var slideAutoSlide = slides[index].getAttribute( 'data-autoslide' );
  649. if( slideAutoSlide ) {
  650. autoSlide = parseInt( slideAutoSlide );
  651. } else {
  652. autoSlide = config.autoSlide
  653. }
  654. }
  655. else {
  656. // Since there are no slides we can't be anywhere beyond the
  657. // zeroth index
  658. index = 0;
  659. }
  660. return index;
  661. }
  662. /**
  663. * Updates the state and link pointers of the controls.
  664. */
  665. function updateControls() {
  666. if ( config.controls && dom.controls ) {
  667. var routes = availableRoutes();
  668. // Remove the 'enabled' class from all directions
  669. [ dom.controlsLeft, dom.controlsRight, dom.controlsUp, dom.controlsDown ].forEach( function( node ) {
  670. node.classList.remove( 'enabled' );
  671. } );
  672. // Add the 'enabled' class to the available routes
  673. if( routes.left ) dom.controlsLeft.classList.add( 'enabled' );
  674. if( routes.right ) dom.controlsRight.classList.add( 'enabled' );
  675. if( routes.up ) dom.controlsUp.classList.add( 'enabled' );
  676. if( routes.down ) dom.controlsDown.classList.add( 'enabled' );
  677. }
  678. }
  679. /**
  680. * Determine what available routes there are for navigation.
  681. *
  682. * @return {Object} containing four booleans: left/right/up/down
  683. */
  684. function availableRoutes() {
  685. var horizontalSlides = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ),
  686. verticalSlides = document.querySelectorAll( VERTICAL_SLIDES_SELECTOR );
  687. return {
  688. left: indexh > 0,
  689. right: indexh < horizontalSlides.length - 1,
  690. up: indexv > 0,
  691. down: indexv < verticalSlides.length - 1
  692. };
  693. }
  694. /**
  695. * Reads the current URL (hash) and navigates accordingly.
  696. */
  697. function readURL() {
  698. var hash = window.location.hash;
  699. // Attempt to parse the hash as either an index or name
  700. var bits = hash.slice( 2 ).split( '/' ),
  701. name = hash.replace( /#|\//gi, '' );
  702. // If the first bit is invalid and there is a name we can
  703. // assume that this is a named link
  704. if( isNaN( parseInt( bits[0], 10 ) ) && name.length ) {
  705. // Find the slide with the specified name
  706. var element = document.querySelector( '#' + name );
  707. if( element ) {
  708. // Find the position of the named slide and navigate to it
  709. var indices = Reveal.getIndices( element );
  710. slide( indices.h, indices.v );
  711. }
  712. // If the slide doesn't exist, navigate to the current slide
  713. else {
  714. slide( indexh, indexv );
  715. }
  716. }
  717. else {
  718. // Read the index components of the hash
  719. var h = parseInt( bits[0], 10 ) || 0,
  720. v = parseInt( bits[1], 10 ) || 0;
  721. slide( h, v );
  722. }
  723. }
  724. /**
  725. * Updates the page URL (hash) to reflect the current
  726. * state.
  727. */
  728. function writeURL() {
  729. if( config.history ) {
  730. var url = '/';
  731. // Only include the minimum possible number of components in
  732. // the URL
  733. if( indexh > 0 || indexv > 0 ) url += indexh;
  734. if( indexv > 0 ) url += '/' + indexv;
  735. window.location.hash = url;
  736. }
  737. }
  738. /**
  739. * Retrieves the h/v location of the current, or specified,
  740. * slide.
  741. *
  742. * @param {HTMLElement} slide If specified, the returned
  743. * index will be for this slide rather than the currently
  744. * active one
  745. *
  746. * @return {Object} { h: <int>, v: <int> }
  747. */
  748. function getIndices( slide ) {
  749. // By default, return the current indices
  750. var h = indexh,
  751. v = indexv;
  752. // If a slide is specified, return the indices of that slide
  753. if( slide ) {
  754. var isVertical = !!slide.parentNode.nodeName.match( /section/gi );
  755. var slideh = isVertical ? slide.parentNode : slide;
  756. // Select all horizontal slides
  757. var horizontalSlides = Array.prototype.slice.call( document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) );
  758. // Now that we know which the horizontal slide is, get its index
  759. h = Math.max( horizontalSlides.indexOf( slideh ), 0 );
  760. // If this is a vertical slide, grab the vertical index
  761. if( isVertical ) {
  762. v = Math.max( Array.prototype.slice.call( slide.parentNode.children ).indexOf( slide ), 0 );
  763. }
  764. }
  765. return { h: h, v: v };
  766. }
  767. /**
  768. * Navigate to the next slide fragment.
  769. *
  770. * @return {Boolean} true if there was a next fragment,
  771. * false otherwise
  772. */
  773. function nextFragment() {
  774. // Vertical slides:
  775. if( document.querySelector( VERTICAL_SLIDES_SELECTOR + '.present' ) ) {
  776. var verticalFragments = document.querySelectorAll( VERTICAL_SLIDES_SELECTOR + '.present .fragment:not(.visible)' );
  777. if( verticalFragments.length ) {
  778. verticalFragments[0].classList.add( 'visible' );
  779. // Notify subscribers of the change
  780. dispatchEvent( 'fragmentshown', { fragment: verticalFragments[0] } );
  781. return true;
  782. }
  783. }
  784. // Horizontal slides:
  785. else {
  786. var horizontalFragments = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR + '.present .fragment:not(.visible)' );
  787. if( horizontalFragments.length ) {
  788. horizontalFragments[0].classList.add( 'visible' );
  789. // Notify subscribers of the change
  790. dispatchEvent( 'fragmentshown', { fragment: horizontalFragments[0] } );
  791. return true;
  792. }
  793. }
  794. return false;
  795. }
  796. /**
  797. * Navigate to the previous slide fragment.
  798. *
  799. * @return {Boolean} true if there was a previous fragment,
  800. * false otherwise
  801. */
  802. function previousFragment() {
  803. // Vertical slides:
  804. if( document.querySelector( VERTICAL_SLIDES_SELECTOR + '.present' ) ) {
  805. var verticalFragments = document.querySelectorAll( VERTICAL_SLIDES_SELECTOR + '.present .fragment.visible' );
  806. if( verticalFragments.length ) {
  807. verticalFragments[ verticalFragments.length - 1 ].classList.remove( 'visible' );
  808. // Notify subscribers of the change
  809. dispatchEvent( 'fragmenthidden', { fragment: verticalFragments[ verticalFragments.length - 1 ] } );
  810. return true;
  811. }
  812. }
  813. // Horizontal slides:
  814. else {
  815. var horizontalFragments = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR + '.present .fragment.visible' );
  816. if( horizontalFragments.length ) {
  817. horizontalFragments[ horizontalFragments.length - 1 ].classList.remove( 'visible' );
  818. // Notify subscribers of the change
  819. dispatchEvent( 'fragmenthidden', { fragment: horizontalFragments[ horizontalFragments.length - 1 ] } );
  820. return true;
  821. }
  822. }
  823. return false;
  824. }
  825. /**
  826. * Cues a new automated slide if enabled in the config.
  827. */
  828. function cueAutoSlide() {
  829. clearTimeout( autoSlideTimeout );
  830. // Cue the next auto-slide if enabled
  831. if( autoSlide ) {
  832. autoSlideTimeout = setTimeout( navigateNext, autoSlide );
  833. }
  834. }
  835. function navigateLeft() {
  836. // Prioritize hiding fragments
  837. if( isOverviewActive() || previousFragment() === false ) {
  838. slide( indexh - 1, 0 );
  839. }
  840. }
  841. function navigateRight() {
  842. // Prioritize revealing fragments
  843. if( isOverviewActive() || nextFragment() === false ) {
  844. slide( indexh + 1, 0 );
  845. }
  846. }
  847. function navigateUp() {
  848. // Prioritize hiding fragments
  849. if( isOverviewActive() || previousFragment() === false ) {
  850. slide( indexh, indexv - 1 );
  851. }
  852. }
  853. function navigateDown() {
  854. // Prioritize revealing fragments
  855. if( isOverviewActive() || nextFragment() === false ) {
  856. slide( indexh, indexv + 1 );
  857. }
  858. }
  859. /**
  860. * Navigates backwards, prioritized in the following order:
  861. * 1) Previous fragment
  862. * 2) Previous vertical slide
  863. * 3) Previous horizontal slide
  864. */
  865. function navigatePrev() {
  866. // Prioritize revealing fragments
  867. if( previousFragment() === false ) {
  868. if( availableRoutes().up ) {
  869. navigateUp();
  870. }
  871. else {
  872. // Fetch the previous horizontal slide, if there is one
  873. var previousSlide = document.querySelector( '.reveal .slides>section.past:nth-child(' + indexh + ')' );
  874. if( previousSlide ) {
  875. indexv = ( previousSlide.querySelectorAll('section').length + 1 ) || 0;
  876. indexh --;
  877. slide();
  878. }
  879. }
  880. }
  881. }
  882. /**
  883. * Same as #navigatePrev() but navigates forwards.
  884. */
  885. function navigateNext() {
  886. // Prioritize revealing fragments
  887. if( nextFragment() === false ) {
  888. availableRoutes().down ? navigateDown() : navigateRight();
  889. }
  890. // If auto-sliding is enabled we need to cue up
  891. // another timeout
  892. cueAutoSlide();
  893. }
  894. // --------------------------------------------------------------------//
  895. // ----------------------------- EVENTS -------------------------------//
  896. // --------------------------------------------------------------------//
  897. /**
  898. * Handler for the document level 'keydown' event.
  899. *
  900. * @param {Object} event
  901. */
  902. function onDocumentKeyDown( event ) {
  903. // Check if there's a focused element that could be using
  904. // the keyboard
  905. var activeElement = document.activeElement;
  906. var hasFocus = !!( document.activeElement && ( document.activeElement.type || document.activeElement.href || document.activeElement.contentEditable !== 'inherit' ) );
  907. // Disregard the event if there's a focused element or a
  908. // keyboard modifier key is present
  909. if ( hasFocus || event.shiftKey || event.altKey || event.ctrlKey || event.metaKey ) return;
  910. var triggered = true;
  911. switch( event.keyCode ) {
  912. // p, page up
  913. case 80: case 33: navigatePrev(); break;
  914. // n, page down
  915. case 78: case 34: navigateNext(); break;
  916. // h, left
  917. case 72: case 37: navigateLeft(); break;
  918. // l, right
  919. case 76: case 39: navigateRight(); break;
  920. // k, up
  921. case 75: case 38: navigateUp(); break;
  922. // j, down
  923. case 74: case 40: navigateDown(); break;
  924. // home
  925. case 36: slide( 0 ); break;
  926. // end
  927. case 35: slide( Number.MAX_VALUE ); break;
  928. // space
  929. case 32: isOverviewActive() ? deactivateOverview() : navigateNext(); break;
  930. // return
  931. case 13: isOverviewActive() ? deactivateOverview() : triggered = false; break;
  932. // b, period
  933. case 66: case 190: togglePause(); break;
  934. // f
  935. case 70: enterFullscreen(); break;
  936. default:
  937. triggered = false;
  938. }
  939. // If the input resulted in a triggered action we should prevent
  940. // the browsers default behavior
  941. if( triggered ) {
  942. event.preventDefault();
  943. }
  944. else if ( event.keyCode === 27 && supports3DTransforms ) {
  945. toggleOverview();
  946. event.preventDefault();
  947. }
  948. // If auto-sliding is enabled we need to cue up
  949. // another timeout
  950. cueAutoSlide();
  951. }
  952. /**
  953. * Handler for the document level 'touchstart' event,
  954. * enables support for swipe and pinch gestures.
  955. */
  956. function onDocumentTouchStart( event ) {
  957. touch.startX = event.touches[0].clientX;
  958. touch.startY = event.touches[0].clientY;
  959. touch.startCount = event.touches.length;
  960. // If there's two touches we need to memorize the distance
  961. // between those two points to detect pinching
  962. if( event.touches.length === 2 && config.overview ) {
  963. touch.startSpan = distanceBetween( {
  964. x: event.touches[1].clientX,
  965. y: event.touches[1].clientY
  966. }, {
  967. x: touch.startX,
  968. y: touch.startY
  969. } );
  970. }
  971. }
  972. /**
  973. * Handler for the document level 'touchmove' event.
  974. */
  975. function onDocumentTouchMove( event ) {
  976. // Each touch should only trigger one action
  977. if( !touch.handled ) {
  978. var currentX = event.touches[0].clientX;
  979. var currentY = event.touches[0].clientY;
  980. // If the touch started off with two points and still has
  981. // two active touches; test for the pinch gesture
  982. if( event.touches.length === 2 && touch.startCount === 2 && config.overview ) {
  983. // The current distance in pixels between the two touch points
  984. var currentSpan = distanceBetween( {
  985. x: event.touches[1].clientX,
  986. y: event.touches[1].clientY
  987. }, {
  988. x: touch.startX,
  989. y: touch.startY
  990. } );
  991. // If the span is larger than the desire amount we've got
  992. // ourselves a pinch
  993. if( Math.abs( touch.startSpan - currentSpan ) > touch.threshold ) {
  994. touch.handled = true;
  995. if( currentSpan < touch.startSpan ) {
  996. activateOverview();
  997. }
  998. else {
  999. deactivateOverview();
  1000. }
  1001. }
  1002. event.preventDefault();
  1003. }
  1004. // There was only one touch point, look for a swipe
  1005. else if( event.touches.length === 1 && touch.startCount !== 2 ) {
  1006. var deltaX = currentX - touch.startX,
  1007. deltaY = currentY - touch.startY;
  1008. if( deltaX > touch.threshold && Math.abs( deltaX ) > Math.abs( deltaY ) ) {
  1009. touch.handled = true;
  1010. navigateLeft();
  1011. }
  1012. else if( deltaX < -touch.threshold && Math.abs( deltaX ) > Math.abs( deltaY ) ) {
  1013. touch.handled = true;
  1014. navigateRight();
  1015. }
  1016. else if( deltaY > touch.threshold ) {
  1017. touch.handled = true;
  1018. navigateUp();
  1019. }
  1020. else if( deltaY < -touch.threshold ) {
  1021. touch.handled = true;
  1022. navigateDown();
  1023. }
  1024. event.preventDefault();
  1025. }
  1026. }
  1027. // There's a bug with swiping on some Android devices unless
  1028. // the default action is always prevented
  1029. else if( navigator.userAgent.match( /android/gi ) ) {
  1030. event.preventDefault();
  1031. }
  1032. }
  1033. /**
  1034. * Handler for the document level 'touchend' event.
  1035. */
  1036. function onDocumentTouchEnd( event ) {
  1037. touch.handled = false;
  1038. }
  1039. /**
  1040. * Handles mouse wheel scrolling, throttled to avoid skipping
  1041. * multiple slides.
  1042. */
  1043. function onDocumentMouseScroll( event ){
  1044. clearTimeout( mouseWheelTimeout );
  1045. mouseWheelTimeout = setTimeout( function() {
  1046. var delta = event.detail || -event.wheelDelta;
  1047. if( delta > 0 ) {
  1048. navigateNext();
  1049. }
  1050. else {
  1051. navigatePrev();
  1052. }
  1053. }, 100 );
  1054. }
  1055. /**
  1056. * Clicking on the progress bar results in a navigation to the
  1057. * closest approximate horizontal slide using this equation:
  1058. *
  1059. * ( clickX / presentationWidth ) * numberOfSlides
  1060. */
  1061. function onProgressClick( event ) {
  1062. var slidesTotal = Array.prototype.slice.call( document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) ).length;
  1063. var slideIndex = Math.floor( ( event.clientX / dom.wrapper.offsetWidth ) * slidesTotal );
  1064. slide( slideIndex );
  1065. }
  1066. /**
  1067. * Handler for the window level 'hashchange' event.
  1068. *
  1069. * @param {Object} event
  1070. */
  1071. function onWindowHashChange( event ) {
  1072. readURL();
  1073. }
  1074. /**
  1075. * Invoked when a slide is and we're in the overview.
  1076. */
  1077. function onOverviewSlideClicked( event ) {
  1078. // TODO There's a bug here where the event listeners are not
  1079. // removed after deactivating the overview.
  1080. if( isOverviewActive() ) {
  1081. event.preventDefault();
  1082. deactivateOverview();
  1083. indexh = this.getAttribute( 'data-index-h' );
  1084. indexv = this.getAttribute( 'data-index-v' );
  1085. slide();
  1086. }
  1087. }
  1088. // --------------------------------------------------------------------//
  1089. // ------------------------------- API --------------------------------//
  1090. // --------------------------------------------------------------------//
  1091. return {
  1092. initialize: initialize,
  1093. // Navigation methods
  1094. slide: slide,
  1095. left: navigateLeft,
  1096. right: navigateRight,
  1097. up: navigateUp,
  1098. down: navigateDown,
  1099. prev: navigatePrev,
  1100. next: navigateNext,
  1101. // Deprecated aliases
  1102. navigateTo: slide,
  1103. navigateLeft: navigateLeft,
  1104. navigateRight: navigateRight,
  1105. navigateUp: navigateUp,
  1106. navigateDown: navigateDown,
  1107. navigatePrev: navigatePrev,
  1108. navigateNext: navigateNext,
  1109. // Toggles the overview mode on/off
  1110. toggleOverview: toggleOverview,
  1111. // Adds or removes all internal event listeners (such as keyboard)
  1112. addEventListeners: addEventListeners,
  1113. removeEventListeners: removeEventListeners,
  1114. // Returns the indices of the current, or specified, slide
  1115. getIndices: getIndices,
  1116. // Returns the previous slide element, may be null
  1117. getPreviousSlide: function() {
  1118. return previousSlide;
  1119. },
  1120. // Returns the current slide element
  1121. getCurrentSlide: function() {
  1122. return currentSlide;
  1123. },
  1124. // Helper method, retrieves query string as a key/value hash
  1125. getQueryHash: function() {
  1126. var query = {};
  1127. location.search.replace( /[A-Z0-9]+?=(\w*)/gi, function(a) {
  1128. query[ a.split( '=' ).shift() ] = a.split( '=' ).pop();
  1129. } );
  1130. return query;
  1131. },
  1132. // Forward event binding to the reveal DOM element
  1133. addEventListener: function( type, listener, useCapture ) {
  1134. if( 'addEventListener' in window ) {
  1135. ( dom.wrapper || document.querySelector( '.reveal' ) ).addEventListener( type, listener, useCapture );
  1136. }
  1137. },
  1138. removeEventListener: function( type, listener, useCapture ) {
  1139. if( 'addEventListener' in window ) {
  1140. ( dom.wrapper || document.querySelector( '.reveal' ) ).removeEventListener( type, listener, useCapture );
  1141. }
  1142. }
  1143. };
  1144. })();