reveal.js 32 KB

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