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