reveal.js 44 KB

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