reveal.js 43 KB

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