reveal.js 48 KB

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