reveal.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149
  1. /*!
  2. * reveal.js 2.0 r22
  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. // FFT: Use document.querySelector( ':focus' ) === null
  280. // instead of checking contentEditable?
  281. // Disregard the event if the target is editable or a
  282. // modifier is present
  283. if ( event.target.contentEditable != 'inherit' || event.shiftKey || event.altKey || event.ctrlKey || event.metaKey ) return;
  284. var triggered = false;
  285. switch( event.keyCode ) {
  286. // p, page up
  287. case 80: case 33: navigatePrev(); triggered = true; break;
  288. // n, page down
  289. case 78: case 34: navigateNext(); triggered = true; break;
  290. // h, left
  291. case 72: case 37: navigateLeft(); triggered = true; break;
  292. // l, right
  293. case 76: case 39: navigateRight(); triggered = true; break;
  294. // k, up
  295. case 75: case 38: navigateUp(); triggered = true; break;
  296. // j, down
  297. case 74: case 40: navigateDown(); triggered = true; break;
  298. // home
  299. case 36: navigateTo( 0 ); triggered = true; break;
  300. // end
  301. case 35: navigateTo( Number.MAX_VALUE ); triggered = true; break;
  302. // space
  303. case 32: overviewIsActive() ? deactivateOverview() : navigateNext(); triggered = true; break;
  304. // return
  305. case 13: if( overviewIsActive() ) { deactivateOverview(); triggered = true; } break;
  306. }
  307. // If the input resulted in a triggered action we should prevent
  308. // the browsers default behavior
  309. if( triggered ) {
  310. event.preventDefault();
  311. }
  312. else if ( event.keyCode === 27 && supports3DTransforms ) {
  313. toggleOverview();
  314. event.preventDefault();
  315. }
  316. // If auto-sliding is enabled we need to cue up
  317. // another timeout
  318. cueAutoSlide();
  319. }
  320. /**
  321. * Handler for the document level 'touchstart' event,
  322. * enables support for swipe and pinch gestures.
  323. */
  324. function onDocumentTouchStart( event ) {
  325. touch.startX = event.touches[0].clientX;
  326. touch.startY = event.touches[0].clientY;
  327. touch.startCount = event.touches.length;
  328. // If there's two touches we need to memorize the distance
  329. // between those two points to detect pinching
  330. if( event.touches.length === 2 ) {
  331. touch.startSpan = distanceBetween( {
  332. x: event.touches[1].clientX,
  333. y: event.touches[1].clientY
  334. }, {
  335. x: touch.startX,
  336. y: touch.startY
  337. } );
  338. }
  339. }
  340. /**
  341. * Handler for the document level 'touchmove' event.
  342. */
  343. function onDocumentTouchMove( event ) {
  344. // Each touch should only trigger one action
  345. if( !touch.handled ) {
  346. var currentX = event.touches[0].clientX;
  347. var currentY = event.touches[0].clientY;
  348. // If the touch started off with two points and still has
  349. // two active touches; test for the pinch gesture
  350. if( event.touches.length === 2 && touch.startCount === 2 ) {
  351. // The current distance in pixels between the two touch points
  352. var currentSpan = distanceBetween( {
  353. x: event.touches[1].clientX,
  354. y: event.touches[1].clientY
  355. }, {
  356. x: touch.startX,
  357. y: touch.startY
  358. } );
  359. // If the span is larger than the desire amount we've got
  360. // ourselves a pinch
  361. if( Math.abs( touch.startSpan - currentSpan ) > touch.threshold ) {
  362. touch.handled = true;
  363. if( currentSpan < touch.startSpan ) {
  364. activateOverview();
  365. }
  366. else {
  367. deactivateOverview();
  368. }
  369. }
  370. }
  371. // There was only one touch point, look for a swipe
  372. else if( event.touches.length === 1 ) {
  373. var deltaX = currentX - touch.startX,
  374. deltaY = currentY - touch.startY;
  375. if( deltaX > touch.threshold && Math.abs( deltaX ) > Math.abs( deltaY ) ) {
  376. touch.handled = true;
  377. navigateLeft();
  378. }
  379. else if( deltaX < -touch.threshold && Math.abs( deltaX ) > Math.abs( deltaY ) ) {
  380. touch.handled = true;
  381. navigateRight();
  382. }
  383. else if( deltaY > touch.threshold ) {
  384. touch.handled = true;
  385. navigateUp();
  386. }
  387. else if( deltaY < -touch.threshold ) {
  388. touch.handled = true;
  389. navigateDown();
  390. }
  391. }
  392. event.preventDefault();
  393. }
  394. }
  395. /**
  396. * Handler for the document level 'touchend' event.
  397. */
  398. function onDocumentTouchEnd( event ) {
  399. touch.handled = false;
  400. }
  401. /**
  402. * Handles mouse wheel scrolling, throttled to avoid
  403. * skipping multiple slides.
  404. */
  405. function onDocumentMouseScroll( event ){
  406. clearTimeout( mouseWheelTimeout );
  407. mouseWheelTimeout = setTimeout( function() {
  408. var delta = event.detail || -event.wheelDelta;
  409. if( delta > 0 ) {
  410. navigateNext();
  411. }
  412. else {
  413. navigatePrev();
  414. }
  415. }, 100 );
  416. }
  417. /**
  418. * Handler for the window level 'hashchange' event.
  419. *
  420. * @param {Object} event
  421. */
  422. function onWindowHashChange( event ) {
  423. readURL();
  424. }
  425. /**
  426. * Wrap all links in 3D goodness.
  427. */
  428. function linkify() {
  429. if( supports3DTransforms && !( 'msPerspective' in document.body.style ) ) {
  430. var nodes = document.querySelectorAll( '.reveal .slides section a:not(.image)' );
  431. for( var i = 0, len = nodes.length; i < len; i++ ) {
  432. var node = nodes[i];
  433. if( node.textContent && !node.querySelector( 'img' ) && ( !node.className || !node.classList.contains( node, 'roll' ) ) ) {
  434. node.classList.add( 'roll' );
  435. node.innerHTML = '<span data-title="'+ node.text +'">' + node.innerHTML + '</span>';
  436. }
  437. };
  438. }
  439. }
  440. /**
  441. * Displays the overview of slides (quick nav) by
  442. * scaling down and arranging all slide elements.
  443. *
  444. * Experimental feature, might be dropped if perf
  445. * can't be improved.
  446. */
  447. function activateOverview() {
  448. dom.wrapper.classList.add( 'overview' );
  449. var horizontalSlides = Array.prototype.slice.call( document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) );
  450. for( var i = 0, len1 = horizontalSlides.length; i < len1; i++ ) {
  451. var hslide = horizontalSlides[i],
  452. htransform = 'translateZ(-2500px) translate(' + ( ( i - indexh ) * 105 ) + '%, 0%)';
  453. hslide.setAttribute( 'data-index-h', i );
  454. hslide.style.display = 'block';
  455. hslide.style.WebkitTransform = htransform;
  456. hslide.style.MozTransform = htransform;
  457. hslide.style.msTransform = htransform;
  458. hslide.style.OTransform = htransform;
  459. hslide.style.transform = htransform;
  460. if( !hslide.classList.contains( 'stack' ) ) {
  461. // Navigate to this slide on click
  462. hslide.addEventListener( 'click', onOverviewSlideClicked, true );
  463. }
  464. var verticalSlides = Array.prototype.slice.call( hslide.querySelectorAll( 'section' ) );
  465. for( var j = 0, len2 = verticalSlides.length; j < len2; j++ ) {
  466. var vslide = verticalSlides[j],
  467. vtransform = 'translate(0%, ' + ( ( j - ( i === indexh ? indexv : 0 ) ) * 105 ) + '%)';
  468. vslide.setAttribute( 'data-index-h', i );
  469. vslide.setAttribute( 'data-index-v', j );
  470. vslide.style.display = 'block';
  471. vslide.style.WebkitTransform = vtransform;
  472. vslide.style.MozTransform = vtransform;
  473. vslide.style.msTransform = vtransform;
  474. vslide.style.OTransform = vtransform;
  475. vslide.style.transform = vtransform;
  476. // Navigate to this slide on click
  477. vslide.addEventListener( 'click', onOverviewSlideClicked, true );
  478. }
  479. }
  480. }
  481. /**
  482. * Exits the slide overview and enters the currently
  483. * active slide.
  484. */
  485. function deactivateOverview() {
  486. dom.wrapper.classList.remove( 'overview' );
  487. var slides = Array.prototype.slice.call( document.querySelectorAll( '.reveal .slides section' ) );
  488. for( var i = 0, len = slides.length; i < len; i++ ) {
  489. var element = slides[i];
  490. // Resets all transforms to use the external styles
  491. element.style.WebkitTransform = '';
  492. element.style.MozTransform = '';
  493. element.style.msTransform = '';
  494. element.style.OTransform = '';
  495. element.style.transform = '';
  496. element.removeEventListener( 'click', onOverviewSlideClicked );
  497. }
  498. slide();
  499. }
  500. /**
  501. * Checks if the overview is currently active.
  502. *
  503. * @return {Boolean} true if the overview is active,
  504. * false otherwise
  505. */
  506. function overviewIsActive() {
  507. return dom.wrapper.classList.contains( 'overview' );
  508. }
  509. /**
  510. * Invoked when a slide is and we're in the overview.
  511. */
  512. function onOverviewSlideClicked( event ) {
  513. // TODO There's a bug here where the event listeners are not
  514. // removed after deactivating the overview.
  515. if( overviewIsActive() ) {
  516. event.preventDefault();
  517. deactivateOverview();
  518. indexh = this.getAttribute( 'data-index-h' );
  519. indexv = this.getAttribute( 'data-index-v' );
  520. slide();
  521. }
  522. }
  523. /**
  524. * Updates one dimension of slides by showing the slide
  525. * with the specified index.
  526. *
  527. * @param {String} selector A CSS selector that will fetch
  528. * the group of slides we are working with
  529. * @param {Number} index The index of the slide that should be
  530. * shown
  531. *
  532. * @return {Number} The index of the slide that is now shown,
  533. * might differ from the passed in index if it was out of
  534. * bounds.
  535. */
  536. function updateSlides( selector, index ) {
  537. // Select all slides and convert the NodeList result to
  538. // an array
  539. var slides = Array.prototype.slice.call( document.querySelectorAll( selector ) ),
  540. slidesLength = slides.length;
  541. if( slidesLength ) {
  542. // Should the index loop?
  543. if( config.loop ) {
  544. index %= slidesLength;
  545. if( index < 0 ) {
  546. index = slidesLength + index;
  547. }
  548. }
  549. // Enforce max and minimum index bounds
  550. index = Math.max( Math.min( index, slidesLength - 1 ), 0 );
  551. for( var i = 0; i < slidesLength; i++ ) {
  552. var slide = slides[i];
  553. // Optimization; hide all slides that are three or more steps
  554. // away from the present slide
  555. if( overviewIsActive() === false ) {
  556. // The distance loops so that it measures 1 between the first
  557. // and last slides
  558. var distance = Math.abs( ( index - i ) % ( slidesLength - 3 ) ) || 0;
  559. slide.style.display = distance > 3 ? 'none' : 'block';
  560. }
  561. slides[i].classList.remove( 'past' );
  562. slides[i].classList.remove( 'present' );
  563. slides[i].classList.remove( 'future' );
  564. if( i < index ) {
  565. // Any element previous to index is given the 'past' class
  566. slides[i].classList.add( 'past' );
  567. }
  568. else if( i > index ) {
  569. // Any element subsequent to index is given the 'future' class
  570. slides[i].classList.add( 'future' );
  571. }
  572. // If this element contains vertical slides
  573. if( slide.querySelector( 'section' ) ) {
  574. slides[i].classList.add( 'stack' );
  575. }
  576. }
  577. // Mark the current slide as present
  578. slides[index].classList.add( 'present' );
  579. // If this slide has a state associated with it, add it
  580. // onto the current state of the deck
  581. var slideState = slides[index].getAttribute( 'data-state' );
  582. if( slideState ) {
  583. state = state.concat( slideState.split( ' ' ) );
  584. }
  585. }
  586. else {
  587. // Since there are no slides we can't be anywhere beyond the
  588. // zeroth index
  589. index = 0;
  590. }
  591. return index;
  592. }
  593. /**
  594. * Updates the visual slides to represent the currently
  595. * set indices.
  596. */
  597. function slide( h, v ) {
  598. // Remember where we were at before
  599. previousSlide = currentSlide;
  600. // Remember the state before this slide
  601. var stateBefore = state.concat();
  602. // Reset the state array
  603. state.length = 0;
  604. var indexhBefore = indexh,
  605. indexvBefore = indexv;
  606. // Activate and transition to the new slide
  607. indexh = updateSlides( HORIZONTAL_SLIDES_SELECTOR, h === undefined ? indexh : h );
  608. indexv = updateSlides( VERTICAL_SLIDES_SELECTOR, v === undefined ? indexv : v );
  609. // Apply the new state
  610. stateLoop: for( var i = 0, len = state.length; i < len; i++ ) {
  611. // Check if this state existed on the previous slide. If it
  612. // did, we will avoid adding it repeatedly.
  613. for( var j = 0; j < stateBefore.length; j++ ) {
  614. if( stateBefore[j] === state[i] ) {
  615. stateBefore.splice( j, 1 );
  616. continue stateLoop;
  617. }
  618. }
  619. document.documentElement.classList.add( state[i] );
  620. // Dispatch custom event matching the state's name
  621. dispatchEvent( state[i] );
  622. }
  623. // Clean up the remaints of the previous state
  624. while( stateBefore.length ) {
  625. document.documentElement.classList.remove( stateBefore.pop() );
  626. }
  627. // Update progress if enabled
  628. if( config.progress && dom.progress ) {
  629. dom.progressbar.style.width = ( indexh / ( document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ).length - 1 ) ) * window.innerWidth + 'px';
  630. }
  631. // Close the overview if it's active
  632. if( overviewIsActive() ) {
  633. activateOverview();
  634. }
  635. updateControls();
  636. clearTimeout( writeURLTimeout );
  637. writeURLTimeout = setTimeout( writeURL, 1500 );
  638. // Query all horizontal slides in the deck
  639. var horizontalSlides = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR );
  640. // Find the current horizontal slide and any possible vertical slides
  641. // within it
  642. var currentHorizontalSlide = horizontalSlides[ indexh ],
  643. currentVerticalSlides = currentHorizontalSlide.querySelectorAll( 'section' );
  644. // Store references to the previous and current slides
  645. currentSlide = currentVerticalSlides[ indexv ] || currentHorizontalSlide;
  646. // Dispatch an event if the slide changed
  647. if( indexh !== indexhBefore || indexv !== indexvBefore ) {
  648. dispatchEvent( 'slidechanged', {
  649. 'indexh': indexh,
  650. 'indexv': indexv,
  651. 'previousSlide': previousSlide,
  652. 'currentSlide': currentSlide
  653. } );
  654. }
  655. else {
  656. // Ensure that the previous slide is never the same as the current
  657. previousSlide = null;
  658. }
  659. // Solves an edge case where the previous slide maintains the
  660. // 'present' class when navigating between adjacent vertical
  661. // stacks
  662. if( previousSlide ) {
  663. previousSlide.classList.remove( 'present' );
  664. }
  665. }
  666. /**
  667. * Updates the state and link pointers of the controls.
  668. */
  669. function updateControls() {
  670. if ( !config.controls || !dom.controls ) {
  671. return;
  672. }
  673. var routes = availableRoutes();
  674. // Remove the 'enabled' class from all directions
  675. [ dom.controlsLeft, dom.controlsRight, dom.controlsUp, dom.controlsDown ].forEach( function( node ) {
  676. node.classList.remove( 'enabled' );
  677. } )
  678. if( routes.left ) dom.controlsLeft.classList.add( 'enabled' );
  679. if( routes.right ) dom.controlsRight.classList.add( 'enabled' );
  680. if( routes.up ) dom.controlsUp.classList.add( 'enabled' );
  681. if( routes.down ) dom.controlsDown.classList.add( 'enabled' );
  682. }
  683. /**
  684. * Determine what available routes there are for navigation.
  685. *
  686. * @return {Object} containing four booleans: left/right/up/down
  687. */
  688. function availableRoutes() {
  689. var horizontalSlides = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR );
  690. var verticalSlides = document.querySelectorAll( VERTICAL_SLIDES_SELECTOR );
  691. return {
  692. left: indexh > 0,
  693. right: indexh < horizontalSlides.length - 1,
  694. up: indexv > 0,
  695. down: indexv < verticalSlides.length - 1
  696. };
  697. }
  698. /**
  699. * Reads the current URL (hash) and navigates accordingly.
  700. */
  701. function readURL() {
  702. var hash = window.location.hash;
  703. // Attempt to parse the hash as either an index or name
  704. var bits = hash.slice( 2 ).split( '/' ),
  705. name = hash.replace( /#|\//gi, '' );
  706. // If the first bit is invalid and there is a name we can
  707. // assume that this is a named link
  708. if( isNaN( parseInt( bits[0] ) ) && name.length ) {
  709. // Find the slide with the specified name
  710. var slide = document.querySelector( '#' + name );
  711. if( slide ) {
  712. // Find the position of the named slide and navigate to it
  713. var indices = Reveal.getIndices( slide );
  714. navigateTo( indices.h, indices.v );
  715. }
  716. // If the slide doesn't exist, navigate to the current slide
  717. else {
  718. navigateTo( indexh, indexv );
  719. }
  720. }
  721. else {
  722. // Read the index components of the hash
  723. var h = parseInt( bits[0] ) || 0,
  724. v = parseInt( bits[1] ) || 0;
  725. navigateTo( h, v );
  726. }
  727. }
  728. /**
  729. * Updates the page URL (hash) to reflect the current
  730. * state.
  731. */
  732. function writeURL() {
  733. if( config.history ) {
  734. var url = '/';
  735. // Only include the minimum possible number of components in
  736. // the URL
  737. if( indexh > 0 || indexv > 0 ) url += indexh;
  738. if( indexv > 0 ) url += '/' + indexv;
  739. window.location.hash = url;
  740. }
  741. }
  742. /**
  743. * Dispatches an event of the specified type from the
  744. * reveal DOM element.
  745. */
  746. function dispatchEvent( type, properties ) {
  747. var event = document.createEvent( "HTMLEvents", 1, 2 );
  748. event.initEvent( type, true, true );
  749. extend( event, properties );
  750. dom.wrapper.dispatchEvent( event );
  751. }
  752. /**
  753. * Navigate to the next slide fragment.
  754. *
  755. * @return {Boolean} true if there was a next fragment,
  756. * false otherwise
  757. */
  758. function nextFragment() {
  759. // Vertical slides:
  760. if( document.querySelector( VERTICAL_SLIDES_SELECTOR + '.present' ) ) {
  761. var verticalFragments = document.querySelectorAll( VERTICAL_SLIDES_SELECTOR + '.present .fragment:not(.visible)' );
  762. if( verticalFragments.length ) {
  763. verticalFragments[0].classList.add( 'visible' );
  764. // Notify subscribers of the change
  765. dispatchEvent( 'fragmentshown', { fragment: verticalFragments[0] } );
  766. return true;
  767. }
  768. }
  769. // Horizontal slides:
  770. else {
  771. var horizontalFragments = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR + '.present .fragment:not(.visible)' );
  772. if( horizontalFragments.length ) {
  773. horizontalFragments[0].classList.add( 'visible' );
  774. // Notify subscribers of the change
  775. dispatchEvent( 'fragmentshown', { fragment: horizontalFragments[0] } );
  776. return true;
  777. }
  778. }
  779. return false;
  780. }
  781. /**
  782. * Navigate to the previous slide fragment.
  783. *
  784. * @return {Boolean} true if there was a previous fragment,
  785. * false otherwise
  786. */
  787. function previousFragment() {
  788. // Vertical slides:
  789. if( document.querySelector( VERTICAL_SLIDES_SELECTOR + '.present' ) ) {
  790. var verticalFragments = document.querySelectorAll( VERTICAL_SLIDES_SELECTOR + '.present .fragment.visible' );
  791. if( verticalFragments.length ) {
  792. verticalFragments[ verticalFragments.length - 1 ].classList.remove( 'visible' );
  793. // Notify subscribers of the change
  794. dispatchEvent( 'fragmenthidden', { fragment: verticalFragments[ verticalFragments.length - 1 ] } );
  795. return true;
  796. }
  797. }
  798. // Horizontal slides:
  799. else {
  800. var horizontalFragments = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR + '.present .fragment.visible' );
  801. if( horizontalFragments.length ) {
  802. horizontalFragments[ horizontalFragments.length - 1 ].classList.remove( 'visible' );
  803. // Notify subscribers of the change
  804. dispatchEvent( 'fragmenthidden', { fragment: horizontalFragments[ horizontalFragments.length - 1 ] } );
  805. return true;
  806. }
  807. }
  808. return false;
  809. }
  810. function cueAutoSlide() {
  811. clearTimeout( autoSlideTimeout );
  812. // Cue the next auto-slide if enabled
  813. if( config.autoSlide ) {
  814. autoSlideTimeout = setTimeout( navigateNext, config.autoSlide );
  815. }
  816. }
  817. /**
  818. * Triggers a navigation to the specified indices.
  819. *
  820. * @param {Number} h The horizontal index of the slide to show
  821. * @param {Number} v The vertical index of the slide to show
  822. */
  823. function navigateTo( h, v ) {
  824. slide( h, v );
  825. }
  826. function navigateLeft() {
  827. // Prioritize hiding fragments
  828. if( overviewIsActive() || previousFragment() === false ) {
  829. slide( indexh - 1, 0 );
  830. }
  831. }
  832. function navigateRight() {
  833. // Prioritize revealing fragments
  834. if( overviewIsActive() || nextFragment() === false ) {
  835. slide( indexh + 1, 0 );
  836. }
  837. }
  838. function navigateUp() {
  839. // Prioritize hiding fragments
  840. if( overviewIsActive() || previousFragment() === false ) {
  841. slide( indexh, indexv - 1 );
  842. }
  843. }
  844. function navigateDown() {
  845. // Prioritize revealing fragments
  846. if( overviewIsActive() || nextFragment() === false ) {
  847. slide( indexh, indexv + 1 );
  848. }
  849. }
  850. /**
  851. * Navigates backwards, prioritized in the following order:
  852. * 1) Previous fragment
  853. * 2) Previous vertical slide
  854. * 3) Previous horizontal slide
  855. */
  856. function navigatePrev() {
  857. // Prioritize revealing fragments
  858. if( previousFragment() === false ) {
  859. if( availableRoutes().up ) {
  860. navigateUp();
  861. }
  862. else {
  863. // Fetch the previous horizontal slide, if there is one
  864. var previousSlide = document.querySelector( '.reveal .slides>section.past:nth-child(' + indexh + ')' );
  865. if( previousSlide ) {
  866. indexv = ( previousSlide.querySelectorAll('section').length + 1 ) || 0;
  867. indexh --;
  868. slide();
  869. }
  870. }
  871. }
  872. }
  873. /**
  874. * Same as #navigatePrev() but navigates forwards.
  875. */
  876. function navigateNext() {
  877. // Prioritize revealing fragments
  878. if( nextFragment() === false ) {
  879. availableRoutes().down ? navigateDown() : navigateRight();
  880. }
  881. // If auto-sliding is enabled we need to cue up
  882. // another timeout
  883. cueAutoSlide();
  884. }
  885. /**
  886. * Toggles the slide overview mode on and off.
  887. */
  888. function toggleOverview() {
  889. if( overviewIsActive() ) {
  890. deactivateOverview();
  891. }
  892. else {
  893. activateOverview();
  894. }
  895. }
  896. // Expose some methods publicly
  897. return {
  898. initialize: initialize,
  899. navigateTo: navigateTo,
  900. navigateLeft: navigateLeft,
  901. navigateRight: navigateRight,
  902. navigateUp: navigateUp,
  903. navigateDown: navigateDown,
  904. navigatePrev: navigatePrev,
  905. navigateNext: navigateNext,
  906. toggleOverview: toggleOverview,
  907. // Adds or removes all internal event listeners (such as keyboard)
  908. addEventListeners: addEventListeners,
  909. removeEventListeners: removeEventListeners,
  910. // Returns the indices of the current, or specified, slide
  911. getIndices: function( slide ) {
  912. // By default, return the current indices
  913. var h = indexh,
  914. v = indexv;
  915. // If a slide is specified, return the indices of that slide
  916. if( slide ) {
  917. var isVertical = !!slide.parentNode.nodeName.match( /section/gi );
  918. var slideh = isVertical ? slide.parentNode : slide;
  919. // Select all horizontal slides
  920. var horizontalSlides = Array.prototype.slice.call( document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) );
  921. // Now that we know which the horizontal slide is, get its index
  922. h = Math.max( horizontalSlides.indexOf( slideh ), 0 );
  923. // If this is a vertical slide, grab the vertical index
  924. if( isVertical ) {
  925. v = Math.max( Array.prototype.slice.call( slide.parentNode.children ).indexOf( slide ), 0 );
  926. }
  927. }
  928. return { h: h, v: v };
  929. },
  930. // Returns the previous slide element, may be null
  931. getPreviousSlide: function() {
  932. return previousSlide
  933. },
  934. // Returns the current slide element
  935. getCurrentSlide: function() {
  936. return currentSlide
  937. },
  938. // Helper method, retrieves query string as a key/value hash
  939. getQueryHash: function() {
  940. var query = {};
  941. location.search.replace( /[A-Z0-9]+?=(\w*)/gi, function(a) {
  942. query[ a.split( '=' ).shift() ] = a.split( '=' ).pop();
  943. } );
  944. return query;
  945. },
  946. // Forward event binding to the reveal DOM element
  947. addEventListener: function( type, listener, useCapture ) {
  948. if( 'addEventListener' in window ) {
  949. ( dom.wrapper || document.querySelector( '.reveal' ) ).addEventListener( type, listener, useCapture );
  950. }
  951. },
  952. removeEventListener: function( type, listener, useCapture ) {
  953. if( 'addEventListener' in window ) {
  954. ( dom.wrapper || document.querySelector( '.reveal' ) ).removeEventListener( type, listener, useCapture );
  955. }
  956. }
  957. };
  958. })();