reveal.js 29 KB

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