reveal.js 32 KB

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