reveal.js 47 KB

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