reveal.js 38 KB

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