reveal.js 45 KB

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