reveal.js 48 KB

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