reveal.js 32 KB

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