reveal.js 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655
  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. // Change the presentation direction to be 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. var span = document.createElement('span');
  408. span.setAttribute('data-title', node.text);
  409. span.innerHTML = node.innerHTML;
  410. node.classList.add( 'roll' );
  411. node.innerHTML = '';
  412. node.appendChild(span);
  413. }
  414. }
  415. }
  416. }
  417. /**
  418. * Applies JavaScript-controlled layout rules to the
  419. * presentation.
  420. */
  421. function layout() {
  422. if( config.center ) {
  423. // Select all slides, vertical and horizontal
  424. var slides = toArray( document.querySelectorAll( SLIDES_SELECTOR ) );
  425. // Determine the minimum top offset for slides
  426. var minTop = -dom.wrapper.offsetHeight / 2;
  427. for( var i = 0, len = slides.length; i < len; i++ ) {
  428. var slide = slides[ i ];
  429. // Don't bother update invisible slides
  430. if( slide.style.display === 'none' ) {
  431. continue;
  432. }
  433. // Vertical stacks are not centered since their section
  434. // children will be
  435. if( slide.classList.contains( 'stack' ) ) {
  436. slide.style.top = 0;
  437. }
  438. else {
  439. slide.style.top = Math.max( - ( slide.offsetHeight / 2 ) - 20, minTop ) + 'px';
  440. }
  441. }
  442. }
  443. }
  444. /**
  445. * Stores the vertical index of a stack so that the same
  446. * vertical slide can be selected when navigating to and
  447. * from the stack.
  448. *
  449. * @param {HTMLElement} stack The vertical stack element
  450. * @param {int} v Index to memorize
  451. */
  452. function setPreviousVerticalIndex( stack, v ) {
  453. if( stack ) {
  454. stack.setAttribute( 'data-previous-indexv', v || 0 );
  455. }
  456. }
  457. /**
  458. * Retrieves the vertical index which was stored using
  459. * #setPreviousVerticalIndex() or 0 if no previous index
  460. * exists.
  461. *
  462. * @param {HTMLElement} stack The vertical stack element
  463. */
  464. function getPreviousVerticalIndex( stack ) {
  465. if( stack && stack.classList.contains( 'stack' ) ) {
  466. return parseInt( stack.getAttribute( 'data-previous-indexv' ) || 0, 10 );
  467. }
  468. return 0;
  469. }
  470. /**
  471. * Displays the overview of slides (quick nav) by
  472. * scaling down and arranging all slide elements.
  473. *
  474. * Experimental feature, might be dropped if perf
  475. * can't be improved.
  476. */
  477. function activateOverview() {
  478. // Only proceed if enabled in config
  479. if( config.overview ) {
  480. dom.wrapper.classList.add( 'overview' );
  481. clearTimeout( activateOverviewTimeout );
  482. // Not the pretties solution, but need to let the overview
  483. // class apply first so that slides are measured accurately
  484. // before we can positon them
  485. activateOverviewTimeout = setTimeout( function(){
  486. var horizontalSlides = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR );
  487. for( var i = 0, len1 = horizontalSlides.length; i < len1; i++ ) {
  488. var hslide = horizontalSlides[i],
  489. htransform = 'translateZ(-2500px) translate(' + ( ( i - indexh ) * 105 ) + '%, 0%)';
  490. hslide.setAttribute( 'data-index-h', i );
  491. hslide.style.display = 'block';
  492. hslide.style.WebkitTransform = htransform;
  493. hslide.style.MozTransform = htransform;
  494. hslide.style.msTransform = htransform;
  495. hslide.style.OTransform = htransform;
  496. hslide.style.transform = htransform;
  497. if( hslide.classList.contains( 'stack' ) ) {
  498. var verticalSlides = hslide.querySelectorAll( 'section' );
  499. for( var j = 0, len2 = verticalSlides.length; j < len2; j++ ) {
  500. var verticalIndex = i === indexh ? indexv : getPreviousVerticalIndex( hslide );
  501. var vslide = verticalSlides[j],
  502. vtransform = 'translate(0%, ' + ( ( j - verticalIndex ) * 105 ) + '%)';
  503. vslide.setAttribute( 'data-index-h', i );
  504. vslide.setAttribute( 'data-index-v', j );
  505. vslide.style.display = 'block';
  506. vslide.style.WebkitTransform = vtransform;
  507. vslide.style.MozTransform = vtransform;
  508. vslide.style.msTransform = vtransform;
  509. vslide.style.OTransform = vtransform;
  510. vslide.style.transform = vtransform;
  511. // Navigate to this slide on click
  512. vslide.addEventListener( 'click', onOverviewSlideClicked, true );
  513. }
  514. }
  515. else {
  516. // Navigate to this slide on click
  517. hslide.addEventListener( 'click', onOverviewSlideClicked, true );
  518. }
  519. }
  520. layout();
  521. }, 10 );
  522. }
  523. }
  524. /**
  525. * Exits the slide overview and enters the currently
  526. * active slide.
  527. */
  528. function deactivateOverview() {
  529. // Only proceed if enabled in config
  530. if( config.overview ) {
  531. clearTimeout( activateOverviewTimeout );
  532. dom.wrapper.classList.remove( 'overview' );
  533. // Select all slides
  534. var slides = toArray( document.querySelectorAll( SLIDES_SELECTOR ) );
  535. for( var i = 0, len = slides.length; i < len; i++ ) {
  536. var element = slides[i];
  537. element.style.display = '';
  538. // Resets all transforms to use the external styles
  539. element.style.WebkitTransform = '';
  540. element.style.MozTransform = '';
  541. element.style.msTransform = '';
  542. element.style.OTransform = '';
  543. element.style.transform = '';
  544. element.removeEventListener( 'click', onOverviewSlideClicked, true );
  545. }
  546. slide( indexh, indexv );
  547. }
  548. }
  549. /**
  550. * Toggles the slide overview mode on and off.
  551. *
  552. * @param {Boolean} override Optional flag which overrides the
  553. * toggle logic and forcibly sets the desired state. True means
  554. * overview is open, false means it's closed.
  555. */
  556. function toggleOverview( override ) {
  557. if( typeof override === 'boolean' ) {
  558. override ? activateOverview() : deactivateOverview();
  559. }
  560. else {
  561. isOverviewActive() ? deactivateOverview() : activateOverview();
  562. }
  563. }
  564. /**
  565. * Checks if the overview is currently active.
  566. *
  567. * @return {Boolean} true if the overview is active,
  568. * false otherwise
  569. */
  570. function isOverviewActive() {
  571. return dom.wrapper.classList.contains( 'overview' );
  572. }
  573. /**
  574. * Handling the fullscreen functionality via the fullscreen API
  575. *
  576. * @see http://fullscreen.spec.whatwg.org/
  577. * @see https://developer.mozilla.org/en-US/docs/DOM/Using_fullscreen_mode
  578. */
  579. function enterFullscreen() {
  580. var element = document.body;
  581. // Check which implementation is available
  582. var requestMethod = element.requestFullScreen ||
  583. element.webkitRequestFullScreen ||
  584. element.mozRequestFullScreen ||
  585. element.msRequestFullScreen;
  586. if( requestMethod ) {
  587. requestMethod.apply( element );
  588. }
  589. }
  590. /**
  591. * Enters the paused mode which fades everything on screen to
  592. * black.
  593. */
  594. function pause() {
  595. dom.wrapper.classList.add( 'paused' );
  596. }
  597. /**
  598. * Exits from the paused mode.
  599. */
  600. function resume() {
  601. dom.wrapper.classList.remove( 'paused' );
  602. }
  603. /**
  604. * Toggles the paused mode on and off.
  605. */
  606. function togglePause() {
  607. if( isPaused() ) {
  608. resume();
  609. }
  610. else {
  611. pause();
  612. }
  613. }
  614. /**
  615. * Checks if we are currently in the paused mode.
  616. */
  617. function isPaused() {
  618. return dom.wrapper.classList.contains( 'paused' );
  619. }
  620. /**
  621. * Steps from the current point in the presentation to the
  622. * slide which matches the specified horizontal and vertical
  623. * indices.
  624. *
  625. * @param {int} h Horizontal index of the target slide
  626. * @param {int} v Vertical index of the target slide
  627. * @param {int} f Optional index of a fragment within the
  628. * target slide to activate
  629. */
  630. function slide( h, v, f ) {
  631. // Remember where we were at before
  632. previousSlide = currentSlide;
  633. // Query all horizontal slides in the deck
  634. var horizontalSlides = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR );
  635. // If no vertical index is specified and the upcoming slide is a
  636. // stack, resume at its previous vertical index
  637. if( v === undefined ) {
  638. v = getPreviousVerticalIndex( horizontalSlides[ h ] );
  639. }
  640. // If we were on a vertical stack, remember what vertical index
  641. // it was on so we can resume at the same position when returning
  642. if( previousSlide && previousSlide.parentNode && previousSlide.parentNode.classList.contains( 'stack' ) ) {
  643. setPreviousVerticalIndex( previousSlide.parentNode, indexv );
  644. }
  645. // Remember the state before this slide
  646. var stateBefore = state.concat();
  647. // Reset the state array
  648. state.length = 0;
  649. var indexhBefore = indexh,
  650. indexvBefore = indexv;
  651. // Activate and transition to the new slide
  652. indexh = updateSlides( HORIZONTAL_SLIDES_SELECTOR, h === undefined ? indexh : h );
  653. indexv = updateSlides( VERTICAL_SLIDES_SELECTOR, v === undefined ? indexv : v );
  654. layout();
  655. // Apply the new state
  656. stateLoop: for( var i = 0, len = state.length; i < len; i++ ) {
  657. // Check if this state existed on the previous slide. If it
  658. // did, we will avoid adding it repeatedly
  659. for( var j = 0; j < stateBefore.length; j++ ) {
  660. if( stateBefore[j] === state[i] ) {
  661. stateBefore.splice( j, 1 );
  662. continue stateLoop;
  663. }
  664. }
  665. document.documentElement.classList.add( state[i] );
  666. // Dispatch custom event matching the state's name
  667. dispatchEvent( state[i] );
  668. }
  669. // Clean up the remaints of the previous state
  670. while( stateBefore.length ) {
  671. document.documentElement.classList.remove( stateBefore.pop() );
  672. }
  673. // If the overview is active, re-activate it to update positions
  674. if( isOverviewActive() ) {
  675. activateOverview();
  676. }
  677. // Update the URL hash after a delay since updating it mid-transition
  678. // is likely to cause visual lag
  679. writeURL( 1500 );
  680. // Find the current horizontal slide and any possible vertical slides
  681. // within it
  682. var currentHorizontalSlide = horizontalSlides[ indexh ],
  683. currentVerticalSlides = currentHorizontalSlide.querySelectorAll( 'section' );
  684. // Store references to the previous and current slides
  685. currentSlide = currentVerticalSlides[ indexv ] || currentHorizontalSlide;
  686. // Show fragment, if specified
  687. if( ( indexh !== indexhBefore || indexv !== indexvBefore ) && f ) {
  688. var fragments = currentSlide.querySelectorAll( '.fragment' );
  689. toArray( fragments ).forEach( function( fragment, indexf ) {
  690. if( indexf < f ) {
  691. fragment.classList.add( 'visible' );
  692. }
  693. else {
  694. fragment.classList.remove( 'visible' );
  695. }
  696. } );
  697. }
  698. // Dispatch an event if the slide changed
  699. if( indexh !== indexhBefore || indexv !== indexvBefore ) {
  700. dispatchEvent( 'slidechanged', {
  701. 'indexh': indexh,
  702. 'indexv': indexv,
  703. 'previousSlide': previousSlide,
  704. 'currentSlide': currentSlide
  705. } );
  706. }
  707. else {
  708. // Ensure that the previous slide is never the same as the current
  709. previousSlide = null;
  710. }
  711. // Solves an edge case where the previous slide maintains the
  712. // 'present' class when navigating between adjacent vertical
  713. // stacks
  714. if( previousSlide ) {
  715. previousSlide.classList.remove( 'present' );
  716. // Reset all slides upon navigate to home
  717. // Issue: #285
  718. if ( document.querySelector(HOME_SLIDE_SELECTOR).classList.contains('present') ) {
  719. // Launch async task
  720. setTimeout(function () {
  721. var slides = toArray( document.querySelectorAll(HORIZONTAL_SLIDES_SELECTOR + '.stack')), i;
  722. for ( i in slides ) {
  723. if (slides[i]) {
  724. // Reset stack
  725. setPreviousVerticalIndex(slides[i], 0);
  726. }
  727. }
  728. }, 0);
  729. }
  730. }
  731. updateControls();
  732. updateProgress();
  733. }
  734. /**
  735. * Updates one dimension of slides by showing the slide
  736. * with the specified index.
  737. *
  738. * @param {String} selector A CSS selector that will fetch
  739. * the group of slides we are working with
  740. * @param {Number} index The index of the slide that should be
  741. * shown
  742. *
  743. * @return {Number} The index of the slide that is now shown,
  744. * might differ from the passed in index if it was out of
  745. * bounds.
  746. */
  747. function updateSlides( selector, index ) {
  748. // Select all slides and convert the NodeList result to
  749. // an array
  750. var slides = toArray( document.querySelectorAll( selector ) ),
  751. slidesLength = slides.length;
  752. if( slidesLength ) {
  753. // Should the index loop?
  754. if( config.loop ) {
  755. index %= slidesLength;
  756. if( index < 0 ) {
  757. index = slidesLength + index;
  758. }
  759. }
  760. // Enforce max and minimum index bounds
  761. index = Math.max( Math.min( index, slidesLength - 1 ), 0 );
  762. for( var i = 0; i < slidesLength; i++ ) {
  763. var element = slides[i];
  764. // Optimization; hide all slides that are three or more steps
  765. // away from the present slide
  766. if( isOverviewActive() === false ) {
  767. // The distance loops so that it measures 1 between the first
  768. // and last slides
  769. var distance = Math.abs( ( index - i ) % ( slidesLength - 3 ) ) || 0;
  770. element.style.display = distance > 3 ? 'none' : 'block';
  771. }
  772. slides[i].classList.remove( 'past' );
  773. slides[i].classList.remove( 'present' );
  774. slides[i].classList.remove( 'future' );
  775. if( i < index ) {
  776. // Any element previous to index is given the 'past' class
  777. slides[i].classList.add( 'past' );
  778. }
  779. else if( i > index ) {
  780. // Any element subsequent to index is given the 'future' class
  781. slides[i].classList.add( 'future' );
  782. }
  783. // If this element contains vertical slides
  784. if( element.querySelector( 'section' ) ) {
  785. slides[i].classList.add( 'stack' );
  786. }
  787. }
  788. // Mark the current slide as present
  789. slides[index].classList.add( 'present' );
  790. // If this slide has a state associated with it, add it
  791. // onto the current state of the deck
  792. var slideState = slides[index].getAttribute( 'data-state' );
  793. if( slideState ) {
  794. state = state.concat( slideState.split( ' ' ) );
  795. }
  796. // If this slide has a data-autoslide attribtue associated use this as
  797. // autoSlide value otherwise use the global configured time
  798. var slideAutoSlide = slides[index].getAttribute( 'data-autoslide' );
  799. if( slideAutoSlide ) {
  800. autoSlide = parseInt( slideAutoSlide, 10 );
  801. }
  802. else {
  803. autoSlide = config.autoSlide;
  804. }
  805. }
  806. else {
  807. // Since there are no slides we can't be anywhere beyond the
  808. // zeroth index
  809. index = 0;
  810. }
  811. return index;
  812. }
  813. /**
  814. * Updates the progress bar to reflect the current slide.
  815. */
  816. function updateProgress() {
  817. // Update progress if enabled
  818. if( config.progress && dom.progress ) {
  819. var horizontalSlides = toArray( document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) );
  820. // The number of past and total slides
  821. var totalCount = document.querySelectorAll( SLIDES_SELECTOR + ':not(.stack)' ).length;
  822. var pastCount = 0;
  823. // Step through all slides and count the past ones
  824. mainLoop: for( var i = 0; i < horizontalSlides.length; i++ ) {
  825. var horizontalSlide = horizontalSlides[i];
  826. var verticalSlides = toArray( horizontalSlide.querySelectorAll( 'section' ) );
  827. for( var j = 0; j < verticalSlides.length; j++ ) {
  828. // Stop as soon as we arrive at the present
  829. if( verticalSlides[j].classList.contains( 'present' ) ) {
  830. break mainLoop;
  831. }
  832. pastCount++;
  833. }
  834. // Stop as soon as we arrive at the present
  835. if( horizontalSlide.classList.contains( 'present' ) ) {
  836. break;
  837. }
  838. // Don't count the wrapping section for vertical slides
  839. if( horizontalSlide.classList.contains( 'stack' ) === false ) {
  840. pastCount++;
  841. }
  842. }
  843. dom.progressbar.style.width = ( pastCount / ( totalCount - 1 ) ) * window.innerWidth + 'px';
  844. }
  845. }
  846. /**
  847. * Updates the state of all control/navigation arrows.
  848. */
  849. function updateControls() {
  850. if ( config.controls && dom.controls ) {
  851. var routes = availableRoutes();
  852. // Remove the 'enabled' class from all directions
  853. dom.controlsLeft.concat( dom.controlsRight )
  854. .concat( dom.controlsUp )
  855. .concat( dom.controlsDown )
  856. .concat( dom.controlsPrev )
  857. .concat( dom.controlsNext ).forEach( function( node ) {
  858. node.classList.remove( 'enabled' );
  859. } );
  860. // Add the 'enabled' class to the available routes
  861. if( routes.left ) dom.controlsLeft.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  862. if( routes.right ) dom.controlsRight.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  863. if( routes.up ) dom.controlsUp.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  864. if( routes.down ) dom.controlsDown.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  865. // Prev/next buttons
  866. if( routes.left || routes.up ) dom.controlsPrev.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  867. if( routes.right || routes.down ) dom.controlsNext.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  868. }
  869. }
  870. /**
  871. * Determine what available routes there are for navigation.
  872. *
  873. * @return {Object} containing four booleans: left/right/up/down
  874. */
  875. function availableRoutes() {
  876. var horizontalSlides = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ),
  877. verticalSlides = document.querySelectorAll( VERTICAL_SLIDES_SELECTOR );
  878. return {
  879. left: indexh > 0,
  880. right: indexh < horizontalSlides.length - 1,
  881. up: indexv > 0,
  882. down: indexv < verticalSlides.length - 1
  883. };
  884. }
  885. /**
  886. * Reads the current URL (hash) and navigates accordingly.
  887. */
  888. function readURL() {
  889. var hash = window.location.hash;
  890. // Attempt to parse the hash as either an index or name
  891. var bits = hash.slice( 2 ).split( '/' ),
  892. name = hash.replace( /#|\//gi, '' );
  893. // If the first bit is invalid and there is a name we can
  894. // assume that this is a named link
  895. if( isNaN( parseInt( bits[0], 10 ) ) && name.length ) {
  896. // Find the slide with the specified name
  897. var element = document.querySelector( '#' + name );
  898. if( element ) {
  899. // Find the position of the named slide and navigate to it
  900. var indices = Reveal.getIndices( element );
  901. slide( indices.h, indices.v );
  902. }
  903. // If the slide doesn't exist, navigate to the current slide
  904. else {
  905. slide( indexh, indexv );
  906. }
  907. }
  908. else {
  909. // Read the index components of the hash
  910. var h = parseInt( bits[0], 10 ) || 0,
  911. v = parseInt( bits[1], 10 ) || 0;
  912. slide( h, v );
  913. }
  914. }
  915. /**
  916. * Updates the page URL (hash) to reflect the current
  917. * state.
  918. *
  919. * @param {Number} delay The time in ms to wait before
  920. * writing the hash
  921. */
  922. function writeURL( delay ) {
  923. if( config.history ) {
  924. // Make sure there's never more than one timeout running
  925. clearTimeout( writeURLTimeout );
  926. // If a delay is specified, timeout this call
  927. if( typeof delay === 'number' ) {
  928. writeURLTimeout = setTimeout( writeURL, delay );
  929. }
  930. else {
  931. var url = '/';
  932. // If the current slide has an ID, use that as a named link
  933. if( currentSlide && typeof currentSlide.getAttribute( 'id' ) === 'string' ) {
  934. url = '/' + currentSlide.getAttribute( 'id' );
  935. }
  936. // Otherwise use the /h/v index
  937. else {
  938. if( indexh > 0 || indexv > 0 ) url += indexh;
  939. if( indexv > 0 ) url += '/' + indexv;
  940. }
  941. window.location.hash = url;
  942. }
  943. }
  944. }
  945. /**
  946. * Retrieves the h/v location of the current, or specified,
  947. * slide.
  948. *
  949. * @param {HTMLElement} slide If specified, the returned
  950. * index will be for this slide rather than the currently
  951. * active one
  952. *
  953. * @return {Object} { h: <int>, v: <int> }
  954. */
  955. function getIndices( slide ) {
  956. // By default, return the current indices
  957. var h = indexh,
  958. v = indexv;
  959. // If a slide is specified, return the indices of that slide
  960. if( slide ) {
  961. var isVertical = !!slide.parentNode.nodeName.match( /section/gi );
  962. var slideh = isVertical ? slide.parentNode : slide;
  963. // Select all horizontal slides
  964. var horizontalSlides = toArray( document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) );
  965. // Now that we know which the horizontal slide is, get its index
  966. h = Math.max( horizontalSlides.indexOf( slideh ), 0 );
  967. // If this is a vertical slide, grab the vertical index
  968. if( isVertical ) {
  969. v = Math.max( toArray( slide.parentNode.querySelectorAll( 'section' ) ).indexOf( slide ), 0 );
  970. }
  971. }
  972. return { h: h, v: v };
  973. }
  974. /**
  975. * Navigate to the next slide fragment.
  976. *
  977. * @return {Boolean} true if there was a next fragment,
  978. * false otherwise
  979. */
  980. function nextFragment() {
  981. // Vertical slides:
  982. if( document.querySelector( VERTICAL_SLIDES_SELECTOR + '.present' ) ) {
  983. var verticalFragments = document.querySelectorAll( VERTICAL_SLIDES_SELECTOR + '.present .fragment:not(.visible)' );
  984. if( verticalFragments.length ) {
  985. verticalFragments[0].classList.add( 'visible' );
  986. // Notify subscribers of the change
  987. dispatchEvent( 'fragmentshown', { fragment: verticalFragments[0] } );
  988. return true;
  989. }
  990. }
  991. // Horizontal slides:
  992. else {
  993. var horizontalFragments = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR + '.present .fragment:not(.visible)' );
  994. if( horizontalFragments.length ) {
  995. horizontalFragments[0].classList.add( 'visible' );
  996. // Notify subscribers of the change
  997. dispatchEvent( 'fragmentshown', { fragment: horizontalFragments[0] } );
  998. return true;
  999. }
  1000. }
  1001. return false;
  1002. }
  1003. /**
  1004. * Navigate to the previous slide fragment.
  1005. *
  1006. * @return {Boolean} true if there was a previous fragment,
  1007. * false otherwise
  1008. */
  1009. function previousFragment() {
  1010. // Vertical slides:
  1011. if( document.querySelector( VERTICAL_SLIDES_SELECTOR + '.present' ) ) {
  1012. var verticalFragments = document.querySelectorAll( VERTICAL_SLIDES_SELECTOR + '.present .fragment.visible' );
  1013. if( verticalFragments.length ) {
  1014. verticalFragments[ verticalFragments.length - 1 ].classList.remove( 'visible' );
  1015. // Notify subscribers of the change
  1016. dispatchEvent( 'fragmenthidden', { fragment: verticalFragments[ verticalFragments.length - 1 ] } );
  1017. return true;
  1018. }
  1019. }
  1020. // Horizontal slides:
  1021. else {
  1022. var horizontalFragments = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR + '.present .fragment.visible' );
  1023. if( horizontalFragments.length ) {
  1024. horizontalFragments[ horizontalFragments.length - 1 ].classList.remove( 'visible' );
  1025. // Notify subscribers of the change
  1026. dispatchEvent( 'fragmenthidden', { fragment: horizontalFragments[ horizontalFragments.length - 1 ] } );
  1027. return true;
  1028. }
  1029. }
  1030. return false;
  1031. }
  1032. /**
  1033. * Cues a new automated slide if enabled in the config.
  1034. */
  1035. function cueAutoSlide() {
  1036. clearTimeout( autoSlideTimeout );
  1037. // Cue the next auto-slide if enabled
  1038. if( autoSlide ) {
  1039. autoSlideTimeout = setTimeout( navigateNext, autoSlide );
  1040. }
  1041. }
  1042. function navigateLeft() {
  1043. // Prioritize hiding fragments
  1044. if( availableRoutes().left && isOverviewActive() || previousFragment() === false ) {
  1045. slide( indexh - 1 );
  1046. }
  1047. }
  1048. function navigateRight() {
  1049. // Prioritize revealing fragments
  1050. if( availableRoutes().right && isOverviewActive() || nextFragment() === false ) {
  1051. slide( indexh + 1 );
  1052. }
  1053. }
  1054. function navigateUp() {
  1055. // Prioritize hiding fragments
  1056. if( availableRoutes().up && isOverviewActive() || previousFragment() === false ) {
  1057. slide( indexh, indexv - 1 );
  1058. }
  1059. }
  1060. function navigateDown() {
  1061. // Prioritize revealing fragments
  1062. if( availableRoutes().down && isOverviewActive() || nextFragment() === false ) {
  1063. slide( indexh, indexv + 1 );
  1064. }
  1065. }
  1066. /**
  1067. * Navigates backwards, prioritized in the following order:
  1068. * 1) Previous fragment
  1069. * 2) Previous vertical slide
  1070. * 3) Previous horizontal slide
  1071. */
  1072. function navigatePrev() {
  1073. // Prioritize revealing fragments
  1074. if( previousFragment() === false ) {
  1075. if( availableRoutes().up ) {
  1076. navigateUp();
  1077. }
  1078. else {
  1079. // Fetch the previous horizontal slide, if there is one
  1080. var previousSlide = document.querySelector( HORIZONTAL_SLIDES_SELECTOR + '.past:nth-child(' + indexh + ')' );
  1081. if( previousSlide ) {
  1082. indexv = ( previousSlide.querySelectorAll( 'section' ).length + 1 ) || undefined;
  1083. indexh --;
  1084. slide();
  1085. }
  1086. }
  1087. }
  1088. }
  1089. /**
  1090. * Same as #navigatePrev() but navigates forwards.
  1091. */
  1092. function navigateNext() {
  1093. // Prioritize revealing fragments
  1094. if( nextFragment() === false ) {
  1095. availableRoutes().down ? navigateDown() : navigateRight();
  1096. }
  1097. // If auto-sliding is enabled we need to cue up
  1098. // another timeout
  1099. cueAutoSlide();
  1100. }
  1101. // --------------------------------------------------------------------//
  1102. // ----------------------------- EVENTS -------------------------------//
  1103. // --------------------------------------------------------------------//
  1104. /**
  1105. * Handler for the document level 'keydown' event.
  1106. *
  1107. * @param {Object} event
  1108. */
  1109. function onDocumentKeyDown( event ) {
  1110. // Check if there's a focused element that could be using
  1111. // the keyboard
  1112. var activeElement = document.activeElement;
  1113. var hasFocus = !!( document.activeElement && ( document.activeElement.type || document.activeElement.href || document.activeElement.contentEditable !== 'inherit' ) );
  1114. // Disregard the event if there's a focused element or a
  1115. // keyboard modifier key is present
  1116. if ( hasFocus || event.shiftKey || event.altKey || event.ctrlKey || event.metaKey ) return;
  1117. var triggered = true;
  1118. switch( event.keyCode ) {
  1119. // p, page up
  1120. case 80: case 33: navigatePrev(); break;
  1121. // n, page down
  1122. case 78: case 34: navigateNext(); break;
  1123. // h, left
  1124. case 72: case 37: navigateLeft(); break;
  1125. // l, right
  1126. case 76: case 39: navigateRight(); break;
  1127. // k, up
  1128. case 75: case 38: navigateUp(); break;
  1129. // j, down
  1130. case 74: case 40: navigateDown(); break;
  1131. // home
  1132. case 36: slide( 0 ); break;
  1133. // end
  1134. case 35: slide( Number.MAX_VALUE ); break;
  1135. // space
  1136. case 32: isOverviewActive() ? deactivateOverview() : navigateNext(); break;
  1137. // return
  1138. case 13: isOverviewActive() ? deactivateOverview() : triggered = false; break;
  1139. // b, period
  1140. case 66: case 190: togglePause(); break;
  1141. // f
  1142. case 70: enterFullscreen(); break;
  1143. default:
  1144. triggered = false;
  1145. }
  1146. // If the input resulted in a triggered action we should prevent
  1147. // the browsers default behavior
  1148. if( triggered ) {
  1149. event.preventDefault();
  1150. }
  1151. else if ( event.keyCode === 27 && supports3DTransforms ) {
  1152. toggleOverview();
  1153. event.preventDefault();
  1154. }
  1155. // If auto-sliding is enabled we need to cue up
  1156. // another timeout
  1157. cueAutoSlide();
  1158. }
  1159. /**
  1160. * Handler for the document level 'touchstart' event,
  1161. * enables support for swipe and pinch gestures.
  1162. */
  1163. function onDocumentTouchStart( event ) {
  1164. touch.startX = event.touches[0].clientX;
  1165. touch.startY = event.touches[0].clientY;
  1166. touch.startCount = event.touches.length;
  1167. // If there's two touches we need to memorize the distance
  1168. // between those two points to detect pinching
  1169. if( event.touches.length === 2 && config.overview ) {
  1170. touch.startSpan = distanceBetween( {
  1171. x: event.touches[1].clientX,
  1172. y: event.touches[1].clientY
  1173. }, {
  1174. x: touch.startX,
  1175. y: touch.startY
  1176. } );
  1177. }
  1178. }
  1179. /**
  1180. * Handler for the document level 'touchmove' event.
  1181. */
  1182. function onDocumentTouchMove( event ) {
  1183. // Each touch should only trigger one action
  1184. if( !touch.handled ) {
  1185. var currentX = event.touches[0].clientX;
  1186. var currentY = event.touches[0].clientY;
  1187. // If the touch started off with two points and still has
  1188. // two active touches; test for the pinch gesture
  1189. if( event.touches.length === 2 && touch.startCount === 2 && config.overview ) {
  1190. // The current distance in pixels between the two touch points
  1191. var currentSpan = distanceBetween( {
  1192. x: event.touches[1].clientX,
  1193. y: event.touches[1].clientY
  1194. }, {
  1195. x: touch.startX,
  1196. y: touch.startY
  1197. } );
  1198. // If the span is larger than the desire amount we've got
  1199. // ourselves a pinch
  1200. if( Math.abs( touch.startSpan - currentSpan ) > touch.threshold ) {
  1201. touch.handled = true;
  1202. if( currentSpan < touch.startSpan ) {
  1203. activateOverview();
  1204. }
  1205. else {
  1206. deactivateOverview();
  1207. }
  1208. }
  1209. event.preventDefault();
  1210. }
  1211. // There was only one touch point, look for a swipe
  1212. else if( event.touches.length === 1 && touch.startCount !== 2 ) {
  1213. var deltaX = currentX - touch.startX,
  1214. deltaY = currentY - touch.startY;
  1215. if( deltaX > touch.threshold && Math.abs( deltaX ) > Math.abs( deltaY ) ) {
  1216. touch.handled = true;
  1217. navigateLeft();
  1218. }
  1219. else if( deltaX < -touch.threshold && Math.abs( deltaX ) > Math.abs( deltaY ) ) {
  1220. touch.handled = true;
  1221. navigateRight();
  1222. }
  1223. else if( deltaY > touch.threshold ) {
  1224. touch.handled = true;
  1225. navigateUp();
  1226. }
  1227. else if( deltaY < -touch.threshold ) {
  1228. touch.handled = true;
  1229. navigateDown();
  1230. }
  1231. event.preventDefault();
  1232. }
  1233. }
  1234. // There's a bug with swiping on some Android devices unless
  1235. // the default action is always prevented
  1236. else if( navigator.userAgent.match( /android/gi ) ) {
  1237. event.preventDefault();
  1238. }
  1239. }
  1240. /**
  1241. * Handler for the document level 'touchend' event.
  1242. */
  1243. function onDocumentTouchEnd( event ) {
  1244. touch.handled = false;
  1245. }
  1246. /**
  1247. * Handles mouse wheel scrolling, throttled to avoid skipping
  1248. * multiple slides.
  1249. */
  1250. function onDocumentMouseScroll( event ){
  1251. clearTimeout( mouseWheelTimeout );
  1252. mouseWheelTimeout = setTimeout( function() {
  1253. var delta = event.detail || -event.wheelDelta;
  1254. if( delta > 0 ) {
  1255. navigateNext();
  1256. }
  1257. else {
  1258. navigatePrev();
  1259. }
  1260. }, 100 );
  1261. }
  1262. /**
  1263. * Clicking on the progress bar results in a navigation to the
  1264. * closest approximate horizontal slide using this equation:
  1265. *
  1266. * ( clickX / presentationWidth ) * numberOfSlides
  1267. */
  1268. function onProgressClick( event ) {
  1269. var slidesTotal = toArray( document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) ).length;
  1270. var slideIndex = Math.floor( ( event.clientX / dom.wrapper.offsetWidth ) * slidesTotal );
  1271. slide( slideIndex );
  1272. }
  1273. /**
  1274. * Handler for the window level 'hashchange' event.
  1275. */
  1276. function onWindowHashChange( event ) {
  1277. readURL();
  1278. }
  1279. /**
  1280. * Handler for the window level 'resize' event.
  1281. */
  1282. function onWindowResize( event ) {
  1283. layout();
  1284. }
  1285. /**
  1286. * Invoked when a slide is and we're in the overview.
  1287. */
  1288. function onOverviewSlideClicked( event ) {
  1289. // TODO There's a bug here where the event listeners are not
  1290. // removed after deactivating the overview.
  1291. if( isOverviewActive() ) {
  1292. event.preventDefault();
  1293. deactivateOverview();
  1294. var element = event.target;
  1295. while( element && !element.nodeName.match( /section/gi ) ) {
  1296. element = element.parentNode;
  1297. }
  1298. if( element.nodeName.match( /section/gi ) ) {
  1299. var h = parseInt( element.getAttribute( 'data-index-h' ), 10 ),
  1300. v = parseInt( element.getAttribute( 'data-index-v' ), 10 );
  1301. slide( h, v );
  1302. }
  1303. }
  1304. }
  1305. // --------------------------------------------------------------------//
  1306. // ------------------------------- API --------------------------------//
  1307. // --------------------------------------------------------------------//
  1308. return {
  1309. initialize: initialize,
  1310. // Navigation methods
  1311. slide: slide,
  1312. left: navigateLeft,
  1313. right: navigateRight,
  1314. up: navigateUp,
  1315. down: navigateDown,
  1316. prev: navigatePrev,
  1317. next: navigateNext,
  1318. prevFragment: previousFragment,
  1319. nextFragment: nextFragment,
  1320. // Deprecated aliases
  1321. navigateTo: slide,
  1322. navigateLeft: navigateLeft,
  1323. navigateRight: navigateRight,
  1324. navigateUp: navigateUp,
  1325. navigateDown: navigateDown,
  1326. navigatePrev: navigatePrev,
  1327. navigateNext: navigateNext,
  1328. // Toggles the overview mode on/off
  1329. toggleOverview: toggleOverview,
  1330. // Adds or removes all internal event listeners (such as keyboard)
  1331. addEventListeners: addEventListeners,
  1332. removeEventListeners: removeEventListeners,
  1333. // Returns the indices of the current, or specified, slide
  1334. getIndices: getIndices,
  1335. // Returns the previous slide element, may be null
  1336. getPreviousSlide: function() {
  1337. return previousSlide;
  1338. },
  1339. // Returns the current slide element
  1340. getCurrentSlide: function() {
  1341. return currentSlide;
  1342. },
  1343. // Helper method, retrieves query string as a key/value hash
  1344. getQueryHash: function() {
  1345. var query = {};
  1346. location.search.replace( /[A-Z0-9]+?=(\w*)/gi, function(a) {
  1347. query[ a.split( '=' ).shift() ] = a.split( '=' ).pop();
  1348. } );
  1349. return query;
  1350. },
  1351. // Forward event binding to the reveal DOM element
  1352. addEventListener: function( type, listener, useCapture ) {
  1353. if( 'addEventListener' in window ) {
  1354. ( dom.wrapper || document.querySelector( '.reveal' ) ).addEventListener( type, listener, useCapture );
  1355. }
  1356. },
  1357. removeEventListener: function( type, listener, useCapture ) {
  1358. if( 'addEventListener' in window ) {
  1359. ( dom.wrapper || document.querySelector( '.reveal' ) ).removeEventListener( type, listener, useCapture );
  1360. }
  1361. }
  1362. };
  1363. })();