reveal.js 47 KB

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