reveal.js 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  1. /*!
  2. * reveal.js 1.5 r2
  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 = document.body.style['WebkitPerspective'] !== undefined ||
  50. document.body.style['MozPerspective'] !== undefined ||
  51. document.body.style['msPerspective'] !== undefined ||
  52. document.body.style['OPerspective'] !== undefined ||
  53. document.body.style['perspective'] !== undefined,
  54. supports2DTransforms = document.body.style['WebkitTransform'] !== undefined ||
  55. document.body.style['MozTransform'] !== undefined ||
  56. document.body.style['msTransform'] !== undefined ||
  57. document.body.style['OTransform'] !== undefined ||
  58. document.body.style['transform'] !== undefined,
  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 ) {
  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 ) {
  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. dispatchEvent( 'slidechanged', {
  590. 'indexh': indexh,
  591. 'indexv': indexv,
  592. 'previousSlide': previousSlide,
  593. 'currentSlide': currentSlide
  594. } );
  595. }
  596. else {
  597. // Ensure that the previous slide is never the same as the current
  598. previousSlide = null;
  599. }
  600. }
  601. /**
  602. * Updates the state and link pointers of the controls.
  603. */
  604. function updateControls() {
  605. if ( !config.controls || !dom.controls ) {
  606. return;
  607. }
  608. var routes = availableRoutes();
  609. // Remove the 'enabled' class from all directions
  610. [ dom.controlsLeft, dom.controlsRight, dom.controlsUp, dom.controlsDown ].forEach( function( node ) {
  611. node.classList.remove( 'enabled' );
  612. } )
  613. if( routes.left ) dom.controlsLeft.classList.add( 'enabled' );
  614. if( routes.right ) dom.controlsRight.classList.add( 'enabled' );
  615. if( routes.up ) dom.controlsUp.classList.add( 'enabled' );
  616. if( routes.down ) dom.controlsDown.classList.add( 'enabled' );
  617. }
  618. /**
  619. * Determine what available routes there are for navigation.
  620. *
  621. * @return {Object} containing four booleans: left/right/up/down
  622. */
  623. function availableRoutes() {
  624. var horizontalSlides = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR );
  625. var verticalSlides = document.querySelectorAll( VERTICAL_SLIDES_SELECTOR );
  626. return {
  627. left: indexh > 0,
  628. right: indexh < horizontalSlides.length - 1,
  629. up: indexv > 0,
  630. down: indexv < verticalSlides.length - 1
  631. };
  632. }
  633. /**
  634. * Reads the current URL (hash) and navigates accordingly.
  635. */
  636. function readURL() {
  637. // Break the hash down to separate components
  638. var bits = window.location.hash.slice(2).split('/');
  639. // Read the index components of the hash
  640. var h = parseInt( bits[0] ) || 0 ;
  641. var v = parseInt( bits[1] ) || 0 ;
  642. navigateTo( h, v );
  643. }
  644. /**
  645. * Updates the page URL (hash) to reflect the current
  646. * state.
  647. */
  648. function writeURL() {
  649. if( config.history ) {
  650. var url = '/';
  651. // Only include the minimum possible number of components in
  652. // the URL
  653. if( indexh > 0 || indexv > 0 ) url += indexh;
  654. if( indexv > 0 ) url += '/' + indexv;
  655. window.location.hash = url;
  656. }
  657. }
  658. /**
  659. * Dispatches an event of the specified type from the
  660. * reveal DOM element.
  661. */
  662. function dispatchEvent( type, properties ) {
  663. var event = document.createEvent( "HTMLEvents", 1, 2 );
  664. event.initEvent( type, true, true );
  665. extend( event, properties );
  666. dom.wrapper.dispatchEvent( event );
  667. }
  668. /**
  669. * Navigate to the next slide fragment.
  670. *
  671. * @return {Boolean} true if there was a next fragment,
  672. * false otherwise
  673. */
  674. function nextFragment() {
  675. // Vertical slides:
  676. if( document.querySelector( VERTICAL_SLIDES_SELECTOR + '.present' ) ) {
  677. var verticalFragments = document.querySelectorAll( VERTICAL_SLIDES_SELECTOR + '.present .fragment:not(.visible)' );
  678. if( verticalFragments.length ) {
  679. verticalFragments[0].classList.add( 'visible' );
  680. // Notify subscribers of the change
  681. dispatchEvent( 'fragmentshown', { fragment: verticalFragments[0] } );
  682. return true;
  683. }
  684. }
  685. // Horizontal slides:
  686. else {
  687. var horizontalFragments = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR + '.present .fragment:not(.visible)' );
  688. if( horizontalFragments.length ) {
  689. horizontalFragments[0].classList.add( 'visible' );
  690. // Notify subscribers of the change
  691. dispatchEvent( 'fragmentshown', { fragment: horizontalFragments[0] } );
  692. return true;
  693. }
  694. }
  695. return false;
  696. }
  697. /**
  698. * Navigate to the previous slide fragment.
  699. *
  700. * @return {Boolean} true if there was a previous fragment,
  701. * false otherwise
  702. */
  703. function previousFragment() {
  704. // Vertical slides:
  705. if( document.querySelector( VERTICAL_SLIDES_SELECTOR + '.present' ) ) {
  706. var verticalFragments = document.querySelectorAll( VERTICAL_SLIDES_SELECTOR + '.present .fragment.visible' );
  707. if( verticalFragments.length ) {
  708. verticalFragments[ verticalFragments.length - 1 ].classList.remove( 'visible' );
  709. // Notify subscribers of the change
  710. dispatchEvent( 'fragmenthidden', { fragment: verticalFragments[ verticalFragments.length - 1 ] } );
  711. return true;
  712. }
  713. }
  714. // Horizontal slides:
  715. else {
  716. var horizontalFragments = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR + '.present .fragment.visible' );
  717. if( horizontalFragments.length ) {
  718. horizontalFragments[ horizontalFragments.length - 1 ].classList.remove( 'visible' );
  719. // Notify subscribers of the change
  720. dispatchEvent( 'fragmenthidden', { fragment: horizontalFragments[ horizontalFragments.length - 1 ] } );
  721. return true;
  722. }
  723. }
  724. return false;
  725. }
  726. function cueAutoSlide() {
  727. clearTimeout( autoSlideTimeout );
  728. // Cue the next auto-slide if enabled
  729. if( config.autoSlide ) {
  730. autoSlideTimeout = setTimeout( navigateNext, config.autoSlide );
  731. }
  732. }
  733. /**
  734. * Triggers a navigation to the specified indices.
  735. *
  736. * @param {Number} h The horizontal index of the slide to show
  737. * @param {Number} v The vertical index of the slide to show
  738. */
  739. function navigateTo( h, v ) {
  740. slide( h, v );
  741. }
  742. function navigateLeft() {
  743. // Prioritize hiding fragments
  744. if( overviewIsActive() || previousFragment() === false ) {
  745. slide( indexh - 1, 0 );
  746. }
  747. }
  748. function navigateRight() {
  749. // Prioritize revealing fragments
  750. if( overviewIsActive() || nextFragment() === false ) {
  751. slide( indexh + 1, 0 );
  752. }
  753. }
  754. function navigateUp() {
  755. // Prioritize hiding fragments
  756. if( overviewIsActive() || previousFragment() === false ) {
  757. slide( indexh, indexv - 1 );
  758. }
  759. }
  760. function navigateDown() {
  761. // Prioritize revealing fragments
  762. if( overviewIsActive() || nextFragment() === false ) {
  763. slide( indexh, indexv + 1 );
  764. }
  765. }
  766. /**
  767. * Navigates backwards, prioritized in the following order:
  768. * 1) Previous fragment
  769. * 2) Previous vertical slide
  770. * 3) Previous horizontal slide
  771. */
  772. function navigatePrev() {
  773. // Prioritize revealing fragments
  774. if( previousFragment() === false ) {
  775. if( availableRoutes().up ) {
  776. navigateUp();
  777. }
  778. else {
  779. // Fetch the previous horizontal slide, if there is one
  780. var previousSlide = document.querySelector( '.reveal .slides>section.past:nth-child(' + indexh + ')' );
  781. if( previousSlide ) {
  782. indexv = ( previousSlide.querySelectorAll('section').length + 1 ) || 0;
  783. indexh --;
  784. slide();
  785. }
  786. }
  787. }
  788. }
  789. /**
  790. * Same as #navigatePrev() but navigates forwards.
  791. */
  792. function navigateNext() {
  793. // Prioritize revealing fragments
  794. if( nextFragment() === false ) {
  795. availableRoutes().down ? navigateDown() : navigateRight();
  796. }
  797. // If auto-sliding is enabled we need to cue up
  798. // another timeout
  799. cueAutoSlide();
  800. }
  801. /**
  802. * Toggles the slide overview mode on and off.
  803. */
  804. function toggleOverview() {
  805. if( overviewIsActive() ) {
  806. deactivateOverview();
  807. }
  808. else {
  809. activateOverview();
  810. }
  811. }
  812. // Expose some methods publicly
  813. return {
  814. initialize: initialize,
  815. navigateTo: navigateTo,
  816. navigateLeft: navigateLeft,
  817. navigateRight: navigateRight,
  818. navigateUp: navigateUp,
  819. navigateDown: navigateDown,
  820. navigatePrev: navigatePrev,
  821. navigateNext: navigateNext,
  822. toggleOverview: toggleOverview,
  823. // Adds or removes all internal event listeners (such as keyboard)
  824. addEventListeners: addEventListeners,
  825. removeEventListeners: removeEventListeners,
  826. // Returns the indices of the current slide
  827. getIndices: function() {
  828. return {
  829. h: indexh,
  830. v: indexv
  831. };
  832. },
  833. // Returns the previous slide element, may be null
  834. getPreviousSlide: function() {
  835. return previousSlide
  836. },
  837. // Returns the current slide element
  838. getCurrentSlide: function() {
  839. return currentSlide
  840. },
  841. // Forward event binding to the reveal DOM element
  842. addEventListener: function( type, listener, useCapture ) {
  843. ( dom.wrapper || document.querySelector( '.reveal' ) ).addEventListener( type, listener, useCapture );
  844. },
  845. removeEventListener: function( type, listener, useCapture ) {
  846. ( dom.wrapper || document.querySelector( '.reveal' ) ).removeEventListener( type, listener, useCapture );
  847. }
  848. };
  849. })();