reveal.js 29 KB

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