reveal.js 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165
  1. /*!
  2. * reveal.js 2.1 r26
  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. // Only proceed if enabled in config
  449. if( config.overview ) {
  450. dom.wrapper.classList.add( 'overview' );
  451. var horizontalSlides = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR );
  452. for( var i = 0, len1 = horizontalSlides.length; i < len1; i++ ) {
  453. var hslide = horizontalSlides[i],
  454. htransform = 'translateZ(-2500px) translate(' + ( ( i - indexh ) * 105 ) + '%, 0%)';
  455. hslide.setAttribute( 'data-index-h', i );
  456. hslide.style.display = 'block';
  457. hslide.style.WebkitTransform = htransform;
  458. hslide.style.MozTransform = htransform;
  459. hslide.style.msTransform = htransform;
  460. hslide.style.OTransform = htransform;
  461. hslide.style.transform = htransform;
  462. if( !hslide.classList.contains( 'stack' ) ) {
  463. // Navigate to this slide on click
  464. hslide.addEventListener( 'click', onOverviewSlideClicked, true );
  465. }
  466. var verticalSlides = hslide.querySelectorAll( 'section' );
  467. for( var j = 0, len2 = verticalSlides.length; j < len2; j++ ) {
  468. var vslide = verticalSlides[j],
  469. vtransform = 'translate(0%, ' + ( ( j - ( i === indexh ? indexv : 0 ) ) * 105 ) + '%)';
  470. vslide.setAttribute( 'data-index-h', i );
  471. vslide.setAttribute( 'data-index-v', j );
  472. vslide.style.display = 'block';
  473. vslide.style.WebkitTransform = vtransform;
  474. vslide.style.MozTransform = vtransform;
  475. vslide.style.msTransform = vtransform;
  476. vslide.style.OTransform = vtransform;
  477. vslide.style.transform = vtransform;
  478. // Navigate to this slide on click
  479. vslide.addEventListener( 'click', onOverviewSlideClicked, true );
  480. }
  481. }
  482. }
  483. }
  484. /**
  485. * Exits the slide overview and enters the currently
  486. * active slide.
  487. */
  488. function deactivateOverview() {
  489. // Only proceed if enabled in config
  490. if( config.overview ) {
  491. dom.wrapper.classList.remove( 'overview' );
  492. // Select all slides
  493. var slides = Array.prototype.slice.call( document.querySelectorAll( '.reveal .slides section' ) );
  494. for( var i = 0, len = slides.length; i < len; i++ ) {
  495. var element = slides[i];
  496. // Resets all transforms to use the external styles
  497. element.style.WebkitTransform = '';
  498. element.style.MozTransform = '';
  499. element.style.msTransform = '';
  500. element.style.OTransform = '';
  501. element.style.transform = '';
  502. element.removeEventListener( 'click', onOverviewSlideClicked );
  503. }
  504. slide();
  505. }
  506. }
  507. /**
  508. * Checks if the overview is currently active.
  509. *
  510. * @return {Boolean} true if the overview is active,
  511. * false otherwise
  512. */
  513. function overviewIsActive() {
  514. return dom.wrapper.classList.contains( 'overview' );
  515. }
  516. /**
  517. * Invoked when a slide is and we're in the overview.
  518. */
  519. function onOverviewSlideClicked( event ) {
  520. // TODO There's a bug here where the event listeners are not
  521. // removed after deactivating the overview.
  522. if( overviewIsActive() ) {
  523. event.preventDefault();
  524. deactivateOverview();
  525. indexh = this.getAttribute( 'data-index-h' );
  526. indexv = this.getAttribute( 'data-index-v' );
  527. slide();
  528. }
  529. }
  530. /**
  531. * Updates one dimension of slides by showing the slide
  532. * with the specified index.
  533. *
  534. * @param {String} selector A CSS selector that will fetch
  535. * the group of slides we are working with
  536. * @param {Number} index The index of the slide that should be
  537. * shown
  538. *
  539. * @return {Number} The index of the slide that is now shown,
  540. * might differ from the passed in index if it was out of
  541. * bounds.
  542. */
  543. function updateSlides( selector, index ) {
  544. // Select all slides and convert the NodeList result to
  545. // an array
  546. var slides = Array.prototype.slice.call( document.querySelectorAll( selector ) ),
  547. slidesLength = slides.length;
  548. if( slidesLength ) {
  549. // Should the index loop?
  550. if( config.loop ) {
  551. index %= slidesLength;
  552. if( index < 0 ) {
  553. index = slidesLength + index;
  554. }
  555. }
  556. // Enforce max and minimum index bounds
  557. index = Math.max( Math.min( index, slidesLength - 1 ), 0 );
  558. for( var i = 0; i < slidesLength; i++ ) {
  559. var slide = slides[i];
  560. // Optimization; hide all slides that are three or more steps
  561. // away from the present slide
  562. if( overviewIsActive() === false ) {
  563. // The distance loops so that it measures 1 between the first
  564. // and last slides
  565. var distance = Math.abs( ( index - i ) % ( slidesLength - 3 ) ) || 0;
  566. slide.style.display = distance > 3 ? 'none' : 'block';
  567. }
  568. slides[i].classList.remove( 'past' );
  569. slides[i].classList.remove( 'present' );
  570. slides[i].classList.remove( 'future' );
  571. if( i < index ) {
  572. // Any element previous to index is given the 'past' class
  573. slides[i].classList.add( 'past' );
  574. }
  575. else if( i > index ) {
  576. // Any element subsequent to index is given the 'future' class
  577. slides[i].classList.add( 'future' );
  578. }
  579. // If this element contains vertical slides
  580. if( slide.querySelector( 'section' ) ) {
  581. slides[i].classList.add( 'stack' );
  582. }
  583. }
  584. // Mark the current slide as present
  585. slides[index].classList.add( 'present' );
  586. // If this slide has a state associated with it, add it
  587. // onto the current state of the deck
  588. var slideState = slides[index].getAttribute( 'data-state' );
  589. if( slideState ) {
  590. state = state.concat( slideState.split( ' ' ) );
  591. }
  592. }
  593. else {
  594. // Since there are no slides we can't be anywhere beyond the
  595. // zeroth index
  596. index = 0;
  597. }
  598. return index;
  599. }
  600. /**
  601. * Updates the visual slides to represent the currently
  602. * set indices.
  603. */
  604. function slide( h, v ) {
  605. // Remember where we were at before
  606. previousSlide = currentSlide;
  607. // Remember the state before this slide
  608. var stateBefore = state.concat();
  609. // Reset the state array
  610. state.length = 0;
  611. var indexhBefore = indexh,
  612. indexvBefore = indexv;
  613. // Activate and transition to the new slide
  614. indexh = updateSlides( HORIZONTAL_SLIDES_SELECTOR, h === undefined ? indexh : h );
  615. indexv = updateSlides( VERTICAL_SLIDES_SELECTOR, v === undefined ? indexv : v );
  616. // Apply the new state
  617. stateLoop: for( var i = 0, len = state.length; i < len; i++ ) {
  618. // Check if this state existed on the previous slide. If it
  619. // did, we will avoid adding it repeatedly.
  620. for( var j = 0; j < stateBefore.length; j++ ) {
  621. if( stateBefore[j] === state[i] ) {
  622. stateBefore.splice( j, 1 );
  623. continue stateLoop;
  624. }
  625. }
  626. document.documentElement.classList.add( state[i] );
  627. // Dispatch custom event matching the state's name
  628. dispatchEvent( state[i] );
  629. }
  630. // Clean up the remaints of the previous state
  631. while( stateBefore.length ) {
  632. document.documentElement.classList.remove( stateBefore.pop() );
  633. }
  634. // Update progress if enabled
  635. if( config.progress && dom.progress ) {
  636. dom.progressbar.style.width = ( indexh / ( document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ).length - 1 ) ) * window.innerWidth + 'px';
  637. }
  638. // Close the overview if it's active
  639. if( overviewIsActive() ) {
  640. activateOverview();
  641. }
  642. updateControls();
  643. clearTimeout( writeURLTimeout );
  644. writeURLTimeout = setTimeout( writeURL, 1500 );
  645. // Query all horizontal slides in the deck
  646. var horizontalSlides = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR );
  647. // Find the current horizontal slide and any possible vertical slides
  648. // within it
  649. var currentHorizontalSlide = horizontalSlides[ indexh ],
  650. currentVerticalSlides = currentHorizontalSlide.querySelectorAll( 'section' );
  651. // Store references to the previous and current slides
  652. currentSlide = currentVerticalSlides[ indexv ] || currentHorizontalSlide;
  653. // Dispatch an event if the slide changed
  654. if( indexh !== indexhBefore || indexv !== indexvBefore ) {
  655. dispatchEvent( 'slidechanged', {
  656. 'indexh': indexh,
  657. 'indexv': indexv,
  658. 'previousSlide': previousSlide,
  659. 'currentSlide': currentSlide
  660. } );
  661. }
  662. else {
  663. // Ensure that the previous slide is never the same as the current
  664. previousSlide = null;
  665. }
  666. // Solves an edge case where the previous slide maintains the
  667. // 'present' class when navigating between adjacent vertical
  668. // stacks
  669. if( previousSlide ) {
  670. previousSlide.classList.remove( 'present' );
  671. }
  672. }
  673. /**
  674. * Updates the state and link pointers of the controls.
  675. */
  676. function updateControls() {
  677. if ( !config.controls || !dom.controls ) {
  678. return;
  679. }
  680. var routes = availableRoutes();
  681. // Remove the 'enabled' class from all directions
  682. [ dom.controlsLeft, dom.controlsRight, dom.controlsUp, dom.controlsDown ].forEach( function( node ) {
  683. node.classList.remove( 'enabled' );
  684. } );
  685. // Add the 'enabled' class to the available routes
  686. if( routes.left ) dom.controlsLeft.classList.add( 'enabled' );
  687. if( routes.right ) dom.controlsRight.classList.add( 'enabled' );
  688. if( routes.up ) dom.controlsUp.classList.add( 'enabled' );
  689. if( routes.down ) dom.controlsDown.classList.add( 'enabled' );
  690. }
  691. /**
  692. * Determine what available routes there are for navigation.
  693. *
  694. * @return {Object} containing four booleans: left/right/up/down
  695. */
  696. function availableRoutes() {
  697. var horizontalSlides = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR );
  698. var verticalSlides = document.querySelectorAll( VERTICAL_SLIDES_SELECTOR );
  699. return {
  700. left: indexh > 0,
  701. right: indexh < horizontalSlides.length - 1,
  702. up: indexv > 0,
  703. down: indexv < verticalSlides.length - 1
  704. };
  705. }
  706. /**
  707. * Reads the current URL (hash) and navigates accordingly.
  708. */
  709. function readURL() {
  710. var hash = window.location.hash;
  711. // Attempt to parse the hash as either an index or name
  712. var bits = hash.slice( 2 ).split( '/' ),
  713. name = hash.replace( /#|\//gi, '' );
  714. // If the first bit is invalid and there is a name we can
  715. // assume that this is a named link
  716. if( isNaN( parseInt( bits[0] ) ) && name.length ) {
  717. // Find the slide with the specified name
  718. var slide = document.querySelector( '#' + name );
  719. if( slide ) {
  720. // Find the position of the named slide and navigate to it
  721. var indices = Reveal.getIndices( slide );
  722. navigateTo( indices.h, indices.v );
  723. }
  724. // If the slide doesn't exist, navigate to the current slide
  725. else {
  726. navigateTo( indexh, indexv );
  727. }
  728. }
  729. else {
  730. // Read the index components of the hash
  731. var h = parseInt( bits[0] ) || 0,
  732. v = parseInt( bits[1] ) || 0;
  733. navigateTo( h, v );
  734. }
  735. }
  736. /**
  737. * Updates the page URL (hash) to reflect the current
  738. * state.
  739. */
  740. function writeURL() {
  741. if( config.history ) {
  742. var url = '/';
  743. // Only include the minimum possible number of components in
  744. // the URL
  745. if( indexh > 0 || indexv > 0 ) url += indexh;
  746. if( indexv > 0 ) url += '/' + indexv;
  747. window.location.hash = url;
  748. }
  749. }
  750. /**
  751. * Dispatches an event of the specified type from the
  752. * reveal DOM element.
  753. */
  754. function dispatchEvent( type, properties ) {
  755. var event = document.createEvent( "HTMLEvents", 1, 2 );
  756. event.initEvent( type, true, true );
  757. extend( event, properties );
  758. dom.wrapper.dispatchEvent( event );
  759. }
  760. /**
  761. * Navigate to the next slide fragment.
  762. *
  763. * @return {Boolean} true if there was a next fragment,
  764. * false otherwise
  765. */
  766. function nextFragment() {
  767. // Vertical slides:
  768. if( document.querySelector( VERTICAL_SLIDES_SELECTOR + '.present' ) ) {
  769. var verticalFragments = document.querySelectorAll( VERTICAL_SLIDES_SELECTOR + '.present .fragment:not(.visible)' );
  770. if( verticalFragments.length ) {
  771. verticalFragments[0].classList.add( 'visible' );
  772. // Notify subscribers of the change
  773. dispatchEvent( 'fragmentshown', { fragment: verticalFragments[0] } );
  774. return true;
  775. }
  776. }
  777. // Horizontal slides:
  778. else {
  779. var horizontalFragments = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR + '.present .fragment:not(.visible)' );
  780. if( horizontalFragments.length ) {
  781. horizontalFragments[0].classList.add( 'visible' );
  782. // Notify subscribers of the change
  783. dispatchEvent( 'fragmentshown', { fragment: horizontalFragments[0] } );
  784. return true;
  785. }
  786. }
  787. return false;
  788. }
  789. /**
  790. * Navigate to the previous slide fragment.
  791. *
  792. * @return {Boolean} true if there was a previous fragment,
  793. * false otherwise
  794. */
  795. function previousFragment() {
  796. // Vertical slides:
  797. if( document.querySelector( VERTICAL_SLIDES_SELECTOR + '.present' ) ) {
  798. var verticalFragments = document.querySelectorAll( VERTICAL_SLIDES_SELECTOR + '.present .fragment.visible' );
  799. if( verticalFragments.length ) {
  800. verticalFragments[ verticalFragments.length - 1 ].classList.remove( 'visible' );
  801. // Notify subscribers of the change
  802. dispatchEvent( 'fragmenthidden', { fragment: verticalFragments[ verticalFragments.length - 1 ] } );
  803. return true;
  804. }
  805. }
  806. // Horizontal slides:
  807. else {
  808. var horizontalFragments = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR + '.present .fragment.visible' );
  809. if( horizontalFragments.length ) {
  810. horizontalFragments[ horizontalFragments.length - 1 ].classList.remove( 'visible' );
  811. // Notify subscribers of the change
  812. dispatchEvent( 'fragmenthidden', { fragment: horizontalFragments[ horizontalFragments.length - 1 ] } );
  813. return true;
  814. }
  815. }
  816. return false;
  817. }
  818. /**
  819. * Cues a new automated slide if enabled in the config.
  820. */
  821. function cueAutoSlide() {
  822. clearTimeout( autoSlideTimeout );
  823. // Cue the next auto-slide if enabled
  824. if( config.autoSlide ) {
  825. autoSlideTimeout = setTimeout( navigateNext, config.autoSlide );
  826. }
  827. }
  828. /**
  829. * Triggers a navigation to the specified indices.
  830. *
  831. * @param {Number} h The horizontal index of the slide to show
  832. * @param {Number} v The vertical index of the slide to show
  833. */
  834. function navigateTo( h, v ) {
  835. slide( h, v );
  836. }
  837. function navigateLeft() {
  838. // Prioritize hiding fragments
  839. if( overviewIsActive() || previousFragment() === false ) {
  840. slide( indexh - 1, 0 );
  841. }
  842. }
  843. function navigateRight() {
  844. // Prioritize revealing fragments
  845. if( overviewIsActive() || nextFragment() === false ) {
  846. slide( indexh + 1, 0 );
  847. }
  848. }
  849. function navigateUp() {
  850. // Prioritize hiding fragments
  851. if( overviewIsActive() || previousFragment() === false ) {
  852. slide( indexh, indexv - 1 );
  853. }
  854. }
  855. function navigateDown() {
  856. // Prioritize revealing fragments
  857. if( overviewIsActive() || nextFragment() === false ) {
  858. slide( indexh, indexv + 1 );
  859. }
  860. }
  861. /**
  862. * Navigates backwards, prioritized in the following order:
  863. * 1) Previous fragment
  864. * 2) Previous vertical slide
  865. * 3) Previous horizontal slide
  866. */
  867. function navigatePrev() {
  868. // Prioritize revealing fragments
  869. if( previousFragment() === false ) {
  870. if( availableRoutes().up ) {
  871. navigateUp();
  872. }
  873. else {
  874. // Fetch the previous horizontal slide, if there is one
  875. var previousSlide = document.querySelector( '.reveal .slides>section.past:nth-child(' + indexh + ')' );
  876. if( previousSlide ) {
  877. indexv = ( previousSlide.querySelectorAll('section').length + 1 ) || 0;
  878. indexh --;
  879. slide();
  880. }
  881. }
  882. }
  883. }
  884. /**
  885. * Same as #navigatePrev() but navigates forwards.
  886. */
  887. function navigateNext() {
  888. // Prioritize revealing fragments
  889. if( nextFragment() === false ) {
  890. availableRoutes().down ? navigateDown() : navigateRight();
  891. }
  892. // If auto-sliding is enabled we need to cue up
  893. // another timeout
  894. cueAutoSlide();
  895. }
  896. /**
  897. * Toggles the slide overview mode on and off.
  898. */
  899. function toggleOverview() {
  900. if( overviewIsActive() ) {
  901. deactivateOverview();
  902. }
  903. else {
  904. activateOverview();
  905. }
  906. }
  907. // Expose some methods publicly
  908. return {
  909. initialize: initialize,
  910. navigateTo: navigateTo,
  911. navigateLeft: navigateLeft,
  912. navigateRight: navigateRight,
  913. navigateUp: navigateUp,
  914. navigateDown: navigateDown,
  915. navigatePrev: navigatePrev,
  916. navigateNext: navigateNext,
  917. toggleOverview: toggleOverview,
  918. // Adds or removes all internal event listeners (such as keyboard)
  919. addEventListeners: addEventListeners,
  920. removeEventListeners: removeEventListeners,
  921. // Returns the indices of the current, or specified, slide
  922. getIndices: function( slide ) {
  923. // By default, return the current indices
  924. var h = indexh,
  925. v = indexv;
  926. // If a slide is specified, return the indices of that slide
  927. if( slide ) {
  928. var isVertical = !!slide.parentNode.nodeName.match( /section/gi );
  929. var slideh = isVertical ? slide.parentNode : slide;
  930. // Select all horizontal slides
  931. var horizontalSlides = Array.prototype.slice.call( document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) );
  932. // Now that we know which the horizontal slide is, get its index
  933. h = Math.max( horizontalSlides.indexOf( slideh ), 0 );
  934. // If this is a vertical slide, grab the vertical index
  935. if( isVertical ) {
  936. v = Math.max( Array.prototype.slice.call( slide.parentNode.children ).indexOf( slide ), 0 );
  937. }
  938. }
  939. return { h: h, v: v };
  940. },
  941. // Returns the previous slide element, may be null
  942. getPreviousSlide: function() {
  943. return previousSlide;
  944. },
  945. // Returns the current slide element
  946. getCurrentSlide: function() {
  947. return currentSlide;
  948. },
  949. // Helper method, retrieves query string as a key/value hash
  950. getQueryHash: function() {
  951. var query = {};
  952. location.search.replace( /[A-Z0-9]+?=(\w*)/gi, function(a) {
  953. query[ a.split( '=' ).shift() ] = a.split( '=' ).pop();
  954. } );
  955. return query;
  956. },
  957. // Forward event binding to the reveal DOM element
  958. addEventListener: function( type, listener, useCapture ) {
  959. if( 'addEventListener' in window ) {
  960. ( dom.wrapper || document.querySelector( '.reveal' ) ).addEventListener( type, listener, useCapture );
  961. }
  962. },
  963. removeEventListener: function( type, listener, useCapture ) {
  964. if( 'addEventListener' in window ) {
  965. ( dom.wrapper || document.querySelector( '.reveal' ) ).removeEventListener( type, listener, useCapture );
  966. }
  967. }
  968. };
  969. })();