reveal.js 35 KB

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