reveal.js 44 KB

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