reveal.js 37 KB

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