reveal.js 37 KB

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