reveal.js 30 KB

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