reveal.js 29 KB

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