reveal.js 35 KB

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