reveal.js 45 KB

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