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. clearTimeout( writeURLTimeout );
  547. writeURLTimeout = setTimeout( writeURL, 1500 );
  548. // Query all horizontal slides in the deck
  549. var horizontalSlides = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR );
  550. // Find the current horizontal slide and any possible vertical slides
  551. // within it
  552. var currentHorizontalSlide = horizontalSlides[ indexh ],
  553. currentVerticalSlides = currentHorizontalSlide.querySelectorAll( 'section' );
  554. // Store references to the previous and current slides
  555. currentSlide = currentVerticalSlides[ indexv ] || currentHorizontalSlide;
  556. // Dispatch an event if the slide changed
  557. if( indexh !== indexhBefore || indexv !== indexvBefore ) {
  558. dispatchEvent( 'slidechanged', {
  559. 'indexh': indexh,
  560. 'indexv': indexv,
  561. 'previousSlide': previousSlide,
  562. 'currentSlide': currentSlide
  563. } );
  564. }
  565. else {
  566. // Ensure that the previous slide is never the same as the current
  567. previousSlide = null;
  568. }
  569. // Solves an edge case where the previous slide maintains the
  570. // 'present' class when navigating between adjacent vertical
  571. // stacks
  572. if( previousSlide ) {
  573. previousSlide.classList.remove( 'present' );
  574. }
  575. }
  576. /**
  577. * Updates one dimension of slides by showing the slide
  578. * with the specified index.
  579. *
  580. * @param {String} selector A CSS selector that will fetch
  581. * the group of slides we are working with
  582. * @param {Number} index The index of the slide that should be
  583. * shown
  584. *
  585. * @return {Number} The index of the slide that is now shown,
  586. * might differ from the passed in index if it was out of
  587. * bounds.
  588. */
  589. function updateSlides( selector, index ) {
  590. // Select all slides and convert the NodeList result to
  591. // an array
  592. var slides = Array.prototype.slice.call( document.querySelectorAll( selector ) ),
  593. slidesLength = slides.length;
  594. if( slidesLength ) {
  595. // Should the index loop?
  596. if( config.loop ) {
  597. index %= slidesLength;
  598. if( index < 0 ) {
  599. index = slidesLength + index;
  600. }
  601. }
  602. // Enforce max and minimum index bounds
  603. index = Math.max( Math.min( index, slidesLength - 1 ), 0 );
  604. for( var i = 0; i < slidesLength; i++ ) {
  605. var element = slides[i];
  606. // Optimization; hide all slides that are three or more steps
  607. // away from the present slide
  608. if( isOverviewActive() === false ) {
  609. // The distance loops so that it measures 1 between the first
  610. // and last slides
  611. var distance = Math.abs( ( index - i ) % ( slidesLength - 3 ) ) || 0;
  612. element.style.display = distance > 3 ? 'none' : 'block';
  613. }
  614. slides[i].classList.remove( 'past' );
  615. slides[i].classList.remove( 'present' );
  616. slides[i].classList.remove( 'future' );
  617. if( i < index ) {
  618. // Any element previous to index is given the 'past' class
  619. slides[i].classList.add( 'past' );
  620. }
  621. else if( i > index ) {
  622. // Any element subsequent to index is given the 'future' class
  623. slides[i].classList.add( 'future' );
  624. }
  625. // If this element contains vertical slides
  626. if( element.querySelector( 'section' ) ) {
  627. slides[i].classList.add( 'stack' );
  628. }
  629. }
  630. // Mark the current slide as present
  631. slides[index].classList.add( 'present' );
  632. // If this slide has a state associated with it, add it
  633. // onto the current state of the deck
  634. var slideState = slides[index].getAttribute( 'data-state' );
  635. if( slideState ) {
  636. state = state.concat( slideState.split( ' ' ) );
  637. }
  638. // If this slide has a data-autoslide attribtue associated use this as
  639. // autoSlide value otherwise use the global configured time
  640. var slideAutoSlide = slides[index].getAttribute( 'data-autoslide' );
  641. if( slideAutoSlide ) {
  642. autoSlide = parseInt( slideAutoSlide );
  643. } else {
  644. autoSlide = config.autoSlide
  645. }
  646. }
  647. else {
  648. // Since there are no slides we can't be anywhere beyond the
  649. // zeroth index
  650. index = 0;
  651. }
  652. return index;
  653. }
  654. /**
  655. * Updates the state and link pointers of the controls.
  656. */
  657. function updateControls() {
  658. if ( config.controls && dom.controls ) {
  659. var routes = availableRoutes();
  660. // Remove the 'enabled' class from all directions
  661. [ dom.controlsLeft, dom.controlsRight, dom.controlsUp, dom.controlsDown ].forEach( function( node ) {
  662. node.classList.remove( 'enabled' );
  663. } );
  664. // Add the 'enabled' class to the available routes
  665. if( routes.left ) dom.controlsLeft.classList.add( 'enabled' );
  666. if( routes.right ) dom.controlsRight.classList.add( 'enabled' );
  667. if( routes.up ) dom.controlsUp.classList.add( 'enabled' );
  668. if( routes.down ) dom.controlsDown.classList.add( 'enabled' );
  669. }
  670. }
  671. /**
  672. * Determine what available routes there are for navigation.
  673. *
  674. * @return {Object} containing four booleans: left/right/up/down
  675. */
  676. function availableRoutes() {
  677. var horizontalSlides = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ),
  678. verticalSlides = document.querySelectorAll( VERTICAL_SLIDES_SELECTOR );
  679. return {
  680. left: indexh > 0,
  681. right: indexh < horizontalSlides.length - 1,
  682. up: indexv > 0,
  683. down: indexv < verticalSlides.length - 1
  684. };
  685. }
  686. /**
  687. * Reads the current URL (hash) and navigates accordingly.
  688. */
  689. function readURL() {
  690. var hash = window.location.hash;
  691. // Attempt to parse the hash as either an index or name
  692. var bits = hash.slice( 2 ).split( '/' ),
  693. name = hash.replace( /#|\//gi, '' );
  694. // If the first bit is invalid and there is a name we can
  695. // assume that this is a named link
  696. if( isNaN( parseInt( bits[0], 10 ) ) && name.length ) {
  697. // Find the slide with the specified name
  698. var element = document.querySelector( '#' + name );
  699. if( element ) {
  700. // Find the position of the named slide and navigate to it
  701. var indices = Reveal.getIndices( element );
  702. slide( indices.h, indices.v );
  703. }
  704. // If the slide doesn't exist, navigate to the current slide
  705. else {
  706. slide( indexh, indexv );
  707. }
  708. }
  709. else {
  710. // Read the index components of the hash
  711. var h = parseInt( bits[0], 10 ) || 0,
  712. v = parseInt( bits[1], 10 ) || 0;
  713. slide( h, v );
  714. }
  715. }
  716. /**
  717. * Updates the page URL (hash) to reflect the current
  718. * state.
  719. */
  720. function writeURL() {
  721. if( config.history ) {
  722. var url = '/';
  723. // Only include the minimum possible number of components in
  724. // the URL
  725. if( indexh > 0 || indexv > 0 ) url += indexh;
  726. if( indexv > 0 ) url += '/' + indexv;
  727. window.location.hash = url;
  728. }
  729. }
  730. /**
  731. * Retrieves the h/v location of the current, or specified,
  732. * slide.
  733. *
  734. * @param {HTMLElement} slide If specified, the returned
  735. * index will be for this slide rather than the currently
  736. * active one
  737. *
  738. * @return {Object} { h: <int>, v: <int> }
  739. */
  740. function getIndices( slide ) {
  741. // By default, return the current indices
  742. var h = indexh,
  743. v = indexv;
  744. // If a slide is specified, return the indices of that slide
  745. if( slide ) {
  746. var isVertical = !!slide.parentNode.nodeName.match( /section/gi );
  747. var slideh = isVertical ? slide.parentNode : slide;
  748. // Select all horizontal slides
  749. var horizontalSlides = Array.prototype.slice.call( document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) );
  750. // Now that we know which the horizontal slide is, get its index
  751. h = Math.max( horizontalSlides.indexOf( slideh ), 0 );
  752. // If this is a vertical slide, grab the vertical index
  753. if( isVertical ) {
  754. v = Math.max( Array.prototype.slice.call( slide.parentNode.children ).indexOf( slide ), 0 );
  755. }
  756. }
  757. return { h: h, v: v };
  758. }
  759. /**
  760. * Navigate to the next slide fragment.
  761. *
  762. * @return {Boolean} true if there was a next fragment,
  763. * false otherwise
  764. */
  765. function nextFragment() {
  766. // Vertical slides:
  767. if( document.querySelector( VERTICAL_SLIDES_SELECTOR + '.present' ) ) {
  768. var verticalFragments = document.querySelectorAll( VERTICAL_SLIDES_SELECTOR + '.present .fragment:not(.visible)' );
  769. if( verticalFragments.length ) {
  770. verticalFragments[0].classList.add( 'visible' );
  771. // Notify subscribers of the change
  772. dispatchEvent( 'fragmentshown', { fragment: verticalFragments[0] } );
  773. return true;
  774. }
  775. }
  776. // Horizontal slides:
  777. else {
  778. var horizontalFragments = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR + '.present .fragment:not(.visible)' );
  779. if( horizontalFragments.length ) {
  780. horizontalFragments[0].classList.add( 'visible' );
  781. // Notify subscribers of the change
  782. dispatchEvent( 'fragmentshown', { fragment: horizontalFragments[0] } );
  783. return true;
  784. }
  785. }
  786. return false;
  787. }
  788. /**
  789. * Navigate to the previous slide fragment.
  790. *
  791. * @return {Boolean} true if there was a previous fragment,
  792. * false otherwise
  793. */
  794. function previousFragment() {
  795. // Vertical slides:
  796. if( document.querySelector( VERTICAL_SLIDES_SELECTOR + '.present' ) ) {
  797. var verticalFragments = document.querySelectorAll( VERTICAL_SLIDES_SELECTOR + '.present .fragment.visible' );
  798. if( verticalFragments.length ) {
  799. verticalFragments[ verticalFragments.length - 1 ].classList.remove( 'visible' );
  800. // Notify subscribers of the change
  801. dispatchEvent( 'fragmenthidden', { fragment: verticalFragments[ verticalFragments.length - 1 ] } );
  802. return true;
  803. }
  804. }
  805. // Horizontal slides:
  806. else {
  807. var horizontalFragments = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR + '.present .fragment.visible' );
  808. if( horizontalFragments.length ) {
  809. horizontalFragments[ horizontalFragments.length - 1 ].classList.remove( 'visible' );
  810. // Notify subscribers of the change
  811. dispatchEvent( 'fragmenthidden', { fragment: horizontalFragments[ horizontalFragments.length - 1 ] } );
  812. return true;
  813. }
  814. }
  815. return false;
  816. }
  817. /**
  818. * Cues a new automated slide if enabled in the config.
  819. */
  820. function cueAutoSlide() {
  821. clearTimeout( autoSlideTimeout );
  822. // Cue the next auto-slide if enabled
  823. if( autoSlide ) {
  824. autoSlideTimeout = setTimeout( navigateNext, autoSlide );
  825. }
  826. }
  827. function navigateLeft() {
  828. // Prioritize hiding fragments
  829. if( isOverviewActive() || previousFragment() === false ) {
  830. slide( indexh - 1, 0 );
  831. }
  832. }
  833. function navigateRight() {
  834. // Prioritize revealing fragments
  835. if( isOverviewActive() || nextFragment() === false ) {
  836. slide( indexh + 1, 0 );
  837. }
  838. }
  839. function navigateUp() {
  840. // Prioritize hiding fragments
  841. if( isOverviewActive() || previousFragment() === false ) {
  842. slide( indexh, indexv - 1 );
  843. }
  844. }
  845. function navigateDown() {
  846. // Prioritize revealing fragments
  847. if( isOverviewActive() || nextFragment() === false ) {
  848. slide( indexh, indexv + 1 );
  849. }
  850. }
  851. /**
  852. * Navigates backwards, prioritized in the following order:
  853. * 1) Previous fragment
  854. * 2) Previous vertical slide
  855. * 3) Previous horizontal slide
  856. */
  857. function navigatePrev() {
  858. // Prioritize revealing fragments
  859. if( previousFragment() === false ) {
  860. if( availableRoutes().up ) {
  861. navigateUp();
  862. }
  863. else {
  864. // Fetch the previous horizontal slide, if there is one
  865. var previousSlide = document.querySelector( '.reveal .slides>section.past:nth-child(' + indexh + ')' );
  866. if( previousSlide ) {
  867. indexv = ( previousSlide.querySelectorAll('section').length + 1 ) || 0;
  868. indexh --;
  869. slide();
  870. }
  871. }
  872. }
  873. }
  874. /**
  875. * Same as #navigatePrev() but navigates forwards.
  876. */
  877. function navigateNext() {
  878. // Prioritize revealing fragments
  879. if( nextFragment() === false ) {
  880. availableRoutes().down ? navigateDown() : navigateRight();
  881. }
  882. // If auto-sliding is enabled we need to cue up
  883. // another timeout
  884. cueAutoSlide();
  885. }
  886. // --------------------------------------------------------------------//
  887. // ----------------------------- EVENTS -------------------------------//
  888. // --------------------------------------------------------------------//
  889. /**
  890. * Handler for the document level 'keydown' event.
  891. *
  892. * @param {Object} event
  893. */
  894. function onDocumentKeyDown( event ) {
  895. // Disregard the event if the target is editable or a
  896. // modifier is present
  897. if ( document.querySelector( ':focus' ) !== null || event.shiftKey || event.altKey || event.ctrlKey || event.metaKey ) return;
  898. var triggered = true;
  899. switch( event.keyCode ) {
  900. // p, page up
  901. case 80: case 33: navigatePrev(); break;
  902. // n, page down
  903. case 78: case 34: navigateNext(); break;
  904. // h, left
  905. case 72: case 37: navigateLeft(); break;
  906. // l, right
  907. case 76: case 39: navigateRight(); break;
  908. // k, up
  909. case 75: case 38: navigateUp(); break;
  910. // j, down
  911. case 74: case 40: navigateDown(); break;
  912. // home
  913. case 36: slide( 0 ); break;
  914. // end
  915. case 35: slide( Number.MAX_VALUE ); break;
  916. // space
  917. case 32: isOverviewActive() ? deactivateOverview() : navigateNext(); break;
  918. // return
  919. case 13: isOverviewActive() ? deactivateOverview() : triggered = false; break;
  920. // b, period
  921. case 66: case 190: togglePause(); break;
  922. // f
  923. case 70: enterFullscreen(); break;
  924. default:
  925. triggered = false;
  926. }
  927. // If the input resulted in a triggered action we should prevent
  928. // the browsers default behavior
  929. if( triggered ) {
  930. event.preventDefault();
  931. }
  932. else if ( event.keyCode === 27 && supports3DTransforms ) {
  933. toggleOverview();
  934. event.preventDefault();
  935. }
  936. // If auto-sliding is enabled we need to cue up
  937. // another timeout
  938. cueAutoSlide();
  939. }
  940. /**
  941. * Handler for the document level 'touchstart' event,
  942. * enables support for swipe and pinch gestures.
  943. */
  944. function onDocumentTouchStart( event ) {
  945. touch.startX = event.touches[0].clientX;
  946. touch.startY = event.touches[0].clientY;
  947. touch.startCount = event.touches.length;
  948. // If there's two touches we need to memorize the distance
  949. // between those two points to detect pinching
  950. if( event.touches.length === 2 && config.overview ) {
  951. touch.startSpan = distanceBetween( {
  952. x: event.touches[1].clientX,
  953. y: event.touches[1].clientY
  954. }, {
  955. x: touch.startX,
  956. y: touch.startY
  957. } );
  958. }
  959. }
  960. /**
  961. * Handler for the document level 'touchmove' event.
  962. */
  963. function onDocumentTouchMove( event ) {
  964. // Each touch should only trigger one action
  965. if( !touch.handled ) {
  966. var currentX = event.touches[0].clientX;
  967. var currentY = event.touches[0].clientY;
  968. // If the touch started off with two points and still has
  969. // two active touches; test for the pinch gesture
  970. if( event.touches.length === 2 && touch.startCount === 2 && config.overview ) {
  971. // The current distance in pixels between the two touch points
  972. var currentSpan = distanceBetween( {
  973. x: event.touches[1].clientX,
  974. y: event.touches[1].clientY
  975. }, {
  976. x: touch.startX,
  977. y: touch.startY
  978. } );
  979. // If the span is larger than the desire amount we've got
  980. // ourselves a pinch
  981. if( Math.abs( touch.startSpan - currentSpan ) > touch.threshold ) {
  982. touch.handled = true;
  983. if( currentSpan < touch.startSpan ) {
  984. activateOverview();
  985. }
  986. else {
  987. deactivateOverview();
  988. }
  989. }
  990. event.preventDefault();
  991. }
  992. // There was only one touch point, look for a swipe
  993. else if( event.touches.length === 1 && touch.startCount !== 2 ) {
  994. var deltaX = currentX - touch.startX,
  995. deltaY = currentY - touch.startY;
  996. if( deltaX > touch.threshold && Math.abs( deltaX ) > Math.abs( deltaY ) ) {
  997. touch.handled = true;
  998. navigateLeft();
  999. }
  1000. else if( deltaX < -touch.threshold && Math.abs( deltaX ) > Math.abs( deltaY ) ) {
  1001. touch.handled = true;
  1002. navigateRight();
  1003. }
  1004. else if( deltaY > touch.threshold ) {
  1005. touch.handled = true;
  1006. navigateUp();
  1007. }
  1008. else if( deltaY < -touch.threshold ) {
  1009. touch.handled = true;
  1010. navigateDown();
  1011. }
  1012. event.preventDefault();
  1013. }
  1014. }
  1015. // There's a bug with swiping on some Android devices unless
  1016. // the default action is always prevented
  1017. else if( navigator.userAgent.match( /android/gi ) ) {
  1018. event.preventDefault();
  1019. }
  1020. }
  1021. /**
  1022. * Handler for the document level 'touchend' event.
  1023. */
  1024. function onDocumentTouchEnd( event ) {
  1025. touch.handled = false;
  1026. }
  1027. /**
  1028. * Handles mouse wheel scrolling, throttled to avoid
  1029. * skipping multiple slides.
  1030. */
  1031. function onDocumentMouseScroll( event ){
  1032. clearTimeout( mouseWheelTimeout );
  1033. mouseWheelTimeout = setTimeout( function() {
  1034. var delta = event.detail || -event.wheelDelta;
  1035. if( delta > 0 ) {
  1036. navigateNext();
  1037. }
  1038. else {
  1039. navigatePrev();
  1040. }
  1041. }, 100 );
  1042. }
  1043. /**
  1044. * Handler for the window level 'hashchange' event.
  1045. *
  1046. * @param {Object} event
  1047. */
  1048. function onWindowHashChange( event ) {
  1049. readURL();
  1050. }
  1051. /**
  1052. * Invoked when a slide is and we're in the overview.
  1053. */
  1054. function onOverviewSlideClicked( event ) {
  1055. // TODO There's a bug here where the event listeners are not
  1056. // removed after deactivating the overview.
  1057. if( isOverviewActive() ) {
  1058. event.preventDefault();
  1059. deactivateOverview();
  1060. indexh = this.getAttribute( 'data-index-h' );
  1061. indexv = this.getAttribute( 'data-index-v' );
  1062. slide();
  1063. }
  1064. }
  1065. // --------------------------------------------------------------------//
  1066. // ------------------------------- API --------------------------------//
  1067. // --------------------------------------------------------------------//
  1068. return {
  1069. initialize: initialize,
  1070. // Navigation methods
  1071. slide: slide,
  1072. left: navigateLeft,
  1073. right: navigateRight,
  1074. up: navigateUp,
  1075. down: navigateDown,
  1076. prev: navigatePrev,
  1077. next: navigateNext,
  1078. nextFragment: nextFragment,
  1079. previousFragment: previousFragment,
  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. })();