reveal.js 38 KB

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