reveal.js 36 KB

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