reveal.js 48 KB

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