reveal.js 37 KB

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