reveal.js 46 KB

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