reveal.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  1. /*!
  2. * reveal.js 1.5 r11
  3. * http://lab.hakim.se/reveal-js
  4. * MIT licensed
  5. *
  6. * Copyright (C) 2012 Hakim El Hattab, http://hakim.se
  7. */
  8. var Reveal = (function(){
  9. var HORIZONTAL_SLIDES_SELECTOR = '.reveal .slides>section',
  10. VERTICAL_SLIDES_SELECTOR = '.reveal .slides>section.present>section',
  11. IS_TOUCH_DEVICE = !!( 'ontouchstart' in window ),
  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. // Loop the presentation
  23. loop: false,
  24. // Number of milliseconds between automatically proceeding to the
  25. // next slide, disabled when set to 0
  26. autoSlide: 0,
  27. // Enable slide navigation via mouse wheel
  28. mouseWheel: true,
  29. // Apply a 3D roll to links on hover
  30. rollingLinks: true,
  31. // UI style
  32. theme: 'default', // default/neon/beige
  33. // Transition style
  34. transition: 'default' // default/cube/page/concave/linear(2d)
  35. },
  36. // The horizontal and verical index of the currently active slide
  37. indexh = 0,
  38. indexv = 0,
  39. // The previous and current slide HTML elements
  40. previousSlide,
  41. currentSlide,
  42. // Slides may hold a data-state attribute which we pick up and apply
  43. // as a class to the body. This list contains the combined state of
  44. // all current slides.
  45. state = [],
  46. // Cached references to DOM elements
  47. dom = {},
  48. // Detect support for CSS 3D transforms
  49. supports3DTransforms = 'WebkitPerspective' in document.body.style ||
  50. 'MozPerspective' in document.body.style ||
  51. 'msPerspective' in document.body.style ||
  52. 'OPerspective' in document.body.style ||
  53. 'perspective' in document.body.style,
  54. supports2DTransforms = 'WebkitTransform' in document.body.style ||
  55. 'MozTransform' in document.body.style ||
  56. 'msTransform' in document.body.style ||
  57. 'OTransform' in document.body.style ||
  58. 'transform' in document.body.style,
  59. // Detect support for elem.classList
  60. supportsClassList = !!document.body.classList;
  61. // Throttles mouse wheel navigation
  62. mouseWheelTimeout = 0,
  63. // An interval used to automatically move on to the next slide
  64. autoSlideTimeout = 0,
  65. // Delays updates to the URL due to a Chrome thumbnailer bug
  66. writeURLTimeout = 0,
  67. // Holds information about the currently ongoing touch input
  68. touch = {
  69. startX: 0,
  70. startY: 0,
  71. startSpan: 0,
  72. startCount: 0,
  73. handled: false,
  74. threshold: 40
  75. };
  76. /**
  77. * Starts up the slideshow by applying configuration
  78. * options and binding various events.
  79. */
  80. function initialize( options ) {
  81. if( ( !supports2DTransforms && !supports3DTransforms ) || !supportsClassList ) {
  82. document.body.setAttribute( 'class', 'no-transforms' );
  83. // If the browser doesn't support core features we won't be
  84. // using JavaScript to control the presentation
  85. return;
  86. }
  87. // Cache references to DOM elements
  88. dom.wrapper = document.querySelector( '.reveal' );
  89. dom.progress = document.querySelector( '.reveal .progress' );
  90. dom.progressbar = document.querySelector( '.reveal .progress span' );
  91. if ( config.controls ) {
  92. dom.controls = document.querySelector( '.reveal .controls' );
  93. dom.controlsLeft = document.querySelector( '.reveal .controls .left' );
  94. dom.controlsRight = document.querySelector( '.reveal .controls .right' );
  95. dom.controlsUp = document.querySelector( '.reveal .controls .up' );
  96. dom.controlsDown = document.querySelector( '.reveal .controls .down' );
  97. }
  98. addEventListeners();
  99. // Copy options over to our config object
  100. extend( config, options );
  101. // Updates the presentation to match the current configuration values
  102. configure();
  103. // Read the initial hash
  104. readURL();
  105. // Start auto-sliding if it's enabled
  106. cueAutoSlide();
  107. // Set up hiding of the browser address bar
  108. if( navigator.userAgent.match( /(iphone|ipod|android)/i ) ) {
  109. // Give the page some scrollable overflow
  110. document.documentElement.style.overflow = 'scroll';
  111. document.body.style.height = '120%';
  112. // Events that should trigger the address bar to hide
  113. window.addEventListener( 'load', removeAddressBar, false );
  114. window.addEventListener( 'orientationchange', removeAddressBar, false );
  115. }
  116. }
  117. function configure() {
  118. if( supports3DTransforms === false ) {
  119. // Fall back on the 2D transform theme 'linear'
  120. config.transition = 'linear';
  121. }
  122. if( config.controls && dom.controls ) {
  123. dom.controls.style.display = 'block';
  124. }
  125. if( config.progress && dom.progress ) {
  126. dom.progress.style.display = 'block';
  127. }
  128. if( config.transition !== 'default' ) {
  129. dom.wrapper.classList.add( config.transition );
  130. }
  131. if( config.theme !== 'default' ) {
  132. document.documentElement.classList.add( 'theme-' + config.theme );
  133. }
  134. if( config.mouseWheel ) {
  135. document.addEventListener( 'DOMMouseScroll', onDocumentMouseScroll, false ); // FF
  136. document.addEventListener( 'mousewheel', onDocumentMouseScroll, false );
  137. }
  138. if( config.rollingLinks ) {
  139. // Add some 3D magic to our anchors
  140. linkify();
  141. }
  142. }
  143. function addEventListeners() {
  144. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  145. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  146. document.addEventListener( 'touchend', onDocumentTouchEnd, false );
  147. window.addEventListener( 'hashchange', onWindowHashChange, false );
  148. if( config.keyboard ) {
  149. document.addEventListener( 'keydown', onDocumentKeyDown, false );
  150. }
  151. if ( config.controls && dom.controls ) {
  152. dom.controlsLeft.addEventListener( 'click', preventAndForward( navigateLeft ), false );
  153. dom.controlsRight.addEventListener( 'click', preventAndForward( navigateRight ), false );
  154. dom.controlsUp.addEventListener( 'click', preventAndForward( navigateUp ), false );
  155. dom.controlsDown.addEventListener( 'click', preventAndForward( navigateDown ), false );
  156. }
  157. }
  158. function removeEventListeners() {
  159. document.removeEventListener( 'keydown', onDocumentKeyDown, false );
  160. document.removeEventListener( 'touchstart', onDocumentTouchStart, false );
  161. document.removeEventListener( 'touchmove', onDocumentTouchMove, false );
  162. document.removeEventListener( 'touchend', onDocumentTouchEnd, false );
  163. window.removeEventListener( 'hashchange', onWindowHashChange, false );
  164. if ( config.controls && dom.controls ) {
  165. dom.controlsLeft.removeEventListener( 'click', preventAndForward( navigateLeft ), false );
  166. dom.controlsRight.removeEventListener( 'click', preventAndForward( navigateRight ), false );
  167. dom.controlsUp.removeEventListener( 'click', preventAndForward( navigateUp ), false );
  168. dom.controlsDown.removeEventListener( 'click', preventAndForward( navigateDown ), false );
  169. }
  170. }
  171. /**
  172. * Extend object a with the properties of object b.
  173. * If there's a conflict, object b takes precedence.
  174. */
  175. function extend( a, b ) {
  176. for( var i in b ) {
  177. a[ i ] = b[ i ];
  178. }
  179. }
  180. /**
  181. * Measures the distance in pixels between point a
  182. * and point b.
  183. *
  184. * @param {Object} a point with x/y properties
  185. * @param {Object} b point with x/y properties
  186. */
  187. function distanceBetween( a, b ) {
  188. var dx = a.x - b.x,
  189. dy = a.y - b.y;
  190. return Math.sqrt( dx*dx + dy*dy );
  191. }
  192. /**
  193. * Prevents an events defaults behavior calls the
  194. * specified delegate.
  195. *
  196. * @param {Function} delegate The method to call
  197. * after the wrapper has been executed
  198. */
  199. function preventAndForward( delegate ) {
  200. return function( event ) {
  201. event.preventDefault();
  202. delegate.call();
  203. }
  204. }
  205. /**
  206. * Causes the address bar to hide on mobile devices,
  207. * more vertical space ftw.
  208. */
  209. function removeAddressBar() {
  210. setTimeout( function() {
  211. window.scrollTo( 0, 1 );
  212. }, 0 );
  213. }
  214. /**
  215. * Handler for the document level 'keydown' event.
  216. *
  217. * @param {Object} event
  218. */
  219. function onDocumentKeyDown( event ) {
  220. // FFT: Use document.querySelector( ':focus' ) === null
  221. // instead of checking contentEditable?
  222. // Disregard the event if the target is editable or a
  223. // modifier is present
  224. if ( event.target.contentEditable != 'inherit' || event.shiftKey || event.altKey || event.ctrlKey || event.metaKey ) return;
  225. var triggered = false;
  226. switch( event.keyCode ) {
  227. // p, page up
  228. case 80: case 33: navigatePrev(); triggered = true; break;
  229. // n, page down
  230. case 78: case 34: navigateNext(); triggered = true; break;
  231. // h, left
  232. case 72: case 37: navigateLeft(); triggered = true; break;
  233. // l, right
  234. case 76: case 39: navigateRight(); triggered = true; break;
  235. // k, up
  236. case 75: case 38: navigateUp(); triggered = true; break;
  237. // j, down
  238. case 74: case 40: navigateDown(); triggered = true; break;
  239. // home
  240. case 36: navigateTo( 0 ); triggered = true; break;
  241. // end
  242. case 35: navigateTo( Number.MAX_VALUE ); triggered = true; break;
  243. // space
  244. case 32: overviewIsActive() ? deactivateOverview() : navigateNext(); triggered = true; break;
  245. // return
  246. case 13: if( overviewIsActive() ) { deactivateOverview(); triggered = true; } break;
  247. }
  248. // If the input resulted in a triggered action we should prevent
  249. // the browsers default behavior
  250. if( triggered ) {
  251. event.preventDefault();
  252. }
  253. else if ( event.keyCode === 27 && supports3DTransforms ) {
  254. toggleOverview();
  255. event.preventDefault();
  256. }
  257. // If auto-sliding is enabled we need to cue up
  258. // another timeout
  259. cueAutoSlide();
  260. }
  261. /**
  262. * Handler for the document level 'touchstart' event,
  263. * enables support for swipe and pinch gestures.
  264. */
  265. function onDocumentTouchStart( event ) {
  266. touch.startX = event.touches[0].clientX;
  267. touch.startY = event.touches[0].clientY;
  268. touch.startCount = event.touches.length;
  269. // If there's two touches we need to memorize the distance
  270. // between those two points to detect pinching
  271. if( event.touches.length === 2 ) {
  272. touch.startSpan = distanceBetween( {
  273. x: event.touches[1].clientX,
  274. y: event.touches[1].clientY
  275. }, {
  276. x: touch.startX,
  277. y: touch.startY
  278. } );
  279. }
  280. }
  281. /**
  282. * Handler for the document level 'touchmove' event.
  283. */
  284. function onDocumentTouchMove( event ) {
  285. // Each touch should only trigger one action
  286. if( !touch.handled ) {
  287. var currentX = event.touches[0].clientX;
  288. var currentY = event.touches[0].clientY;
  289. // If the touch started off with two points and still has
  290. // two active touches; test for the pinch gesture
  291. if( event.touches.length === 2 && touch.startCount === 2 ) {
  292. // The current distance in pixels between the two touch points
  293. var currentSpan = distanceBetween( {
  294. x: event.touches[1].clientX,
  295. y: event.touches[1].clientY
  296. }, {
  297. x: touch.startX,
  298. y: touch.startY
  299. } );
  300. // If the span is larger than the desire amount we've got
  301. // ourselves a pinch
  302. if( Math.abs( touch.startSpan - currentSpan ) > touch.threshold ) {
  303. touch.handled = true;
  304. if( currentSpan < touch.startSpan ) {
  305. activateOverview();
  306. }
  307. else {
  308. deactivateOverview();
  309. }
  310. }
  311. }
  312. // There was only one touch point, look for a swipe
  313. else if( event.touches.length === 1 ) {
  314. var deltaX = currentX - touch.startX,
  315. deltaY = currentY - touch.startY;
  316. if( deltaX > touch.threshold && Math.abs( deltaX ) > Math.abs( deltaY ) ) {
  317. touch.handled = true;
  318. navigateLeft();
  319. }
  320. else if( deltaX < -touch.threshold && Math.abs( deltaX ) > Math.abs( deltaY ) ) {
  321. touch.handled = true;
  322. navigateRight();
  323. }
  324. else if( deltaY > touch.threshold ) {
  325. touch.handled = true;
  326. navigateUp();
  327. }
  328. else if( deltaY < -touch.threshold ) {
  329. touch.handled = true;
  330. navigateDown();
  331. }
  332. }
  333. event.preventDefault();
  334. }
  335. }
  336. /**
  337. * Handler for the document level 'touchend' event.
  338. */
  339. function onDocumentTouchEnd( event ) {
  340. touch.handled = false;
  341. }
  342. /**
  343. * Handles mouse wheel scrolling, throttled to avoid
  344. * skipping multiple slides.
  345. */
  346. function onDocumentMouseScroll( event ){
  347. clearTimeout( mouseWheelTimeout );
  348. mouseWheelTimeout = setTimeout( function() {
  349. var delta = event.detail || -event.wheelDelta;
  350. if( delta > 0 ) {
  351. navigateNext();
  352. }
  353. else {
  354. navigatePrev();
  355. }
  356. }, 100 );
  357. }
  358. /**
  359. * Handler for the window level 'hashchange' event.
  360. *
  361. * @param {Object} event
  362. */
  363. function onWindowHashChange( event ) {
  364. readURL();
  365. }
  366. /**
  367. * Wrap all links in 3D goodness.
  368. */
  369. function linkify() {
  370. if( supports3DTransforms && !( 'msPerspective' in document.body.style ) ) {
  371. var nodes = document.querySelectorAll( '.reveal .slides section a:not(.image)' );
  372. for( var i = 0, len = nodes.length; i < len; i++ ) {
  373. var node = nodes[i];
  374. if( node.textContent && !node.querySelector( 'img' ) && ( !node.className || !node.classList.contains( node, 'roll' ) ) ) {
  375. node.classList.add( 'roll' );
  376. node.innerHTML = '<span data-title="'+ node.text +'">' + node.innerHTML + '</span>';
  377. }
  378. };
  379. }
  380. }
  381. /**
  382. * Displays the overview of slides (quick nav) by
  383. * scaling down and arranging all slide elements.
  384. *
  385. * Experimental feature, might be dropped if perf
  386. * can't be improved.
  387. */
  388. function activateOverview() {
  389. dom.wrapper.classList.add( 'overview' );
  390. var horizontalSlides = Array.prototype.slice.call( document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) );
  391. for( var i = 0, len1 = horizontalSlides.length; i < len1; i++ ) {
  392. var hslide = horizontalSlides[i],
  393. htransform = 'translateZ(-2500px) translate(' + ( ( i - indexh ) * 105 ) + '%, 0%)';
  394. hslide.setAttribute( 'data-index-h', i );
  395. hslide.style.display = 'block';
  396. hslide.style.WebkitTransform = htransform;
  397. hslide.style.MozTransform = htransform;
  398. hslide.style.msTransform = htransform;
  399. hslide.style.OTransform = htransform;
  400. hslide.style.transform = htransform;
  401. if( !hslide.classList.contains( 'stack' ) ) {
  402. // Navigate to this slide on click
  403. hslide.addEventListener( 'click', onOverviewSlideClicked, true );
  404. }
  405. var verticalSlides = Array.prototype.slice.call( hslide.querySelectorAll( 'section' ) );
  406. for( var j = 0, len2 = verticalSlides.length; j < len2; j++ ) {
  407. var vslide = verticalSlides[j],
  408. vtransform = 'translate(0%, ' + ( ( j - indexv ) * 105 ) + '%)';
  409. vslide.setAttribute( 'data-index-h', i );
  410. vslide.setAttribute( 'data-index-v', j );
  411. vslide.style.display = 'block';
  412. vslide.style.WebkitTransform = vtransform;
  413. vslide.style.MozTransform = vtransform;
  414. vslide.style.msTransform = vtransform;
  415. vslide.style.OTransform = vtransform;
  416. vslide.style.transform = vtransform;
  417. // Navigate to this slide on click
  418. vslide.addEventListener( 'click', onOverviewSlideClicked, true );
  419. }
  420. }
  421. }
  422. /**
  423. * Exits the slide overview and enters the currently
  424. * active slide.
  425. */
  426. function deactivateOverview() {
  427. dom.wrapper.classList.remove( 'overview' );
  428. var slides = Array.prototype.slice.call( document.querySelectorAll( '.reveal .slides section' ) );
  429. for( var i = 0, len = slides.length; i < len; i++ ) {
  430. var element = slides[i];
  431. // Resets all transforms to use the external styles
  432. element.style.WebkitTransform = '';
  433. element.style.MozTransform = '';
  434. element.style.msTransform = '';
  435. element.style.OTransform = '';
  436. element.style.transform = '';
  437. element.removeEventListener( 'click', onOverviewSlideClicked );
  438. }
  439. slide();
  440. }
  441. /**
  442. * Checks if the overview is currently active.
  443. *
  444. * @return {Boolean} true if the overview is active,
  445. * false otherwise
  446. */
  447. function overviewIsActive() {
  448. return dom.wrapper.classList.contains( 'overview' );
  449. }
  450. /**
  451. * Invoked when a slide is and we're in the overview.
  452. */
  453. function onOverviewSlideClicked( event ) {
  454. // TODO There's a bug here where the event listeners are not
  455. // removed after deactivating the overview.
  456. if( overviewIsActive() ) {
  457. event.preventDefault();
  458. deactivateOverview();
  459. indexh = this.getAttribute( 'data-index-h' );
  460. indexv = this.getAttribute( 'data-index-v' );
  461. slide();
  462. }
  463. }
  464. /**
  465. * Updates one dimension of slides by showing the slide
  466. * with the specified index.
  467. *
  468. * @param {String} selector A CSS selector that will fetch
  469. * the group of slides we are working with
  470. * @param {Number} index The index of the slide that should be
  471. * shown
  472. *
  473. * @return {Number} The index of the slide that is now shown,
  474. * might differ from the passed in index if it was out of
  475. * bounds.
  476. */
  477. function updateSlides( selector, index ) {
  478. // Select all slides and convert the NodeList result to
  479. // an array
  480. var slides = Array.prototype.slice.call( document.querySelectorAll( selector ) ),
  481. slidesLength = slides.length;
  482. if( slidesLength ) {
  483. // Should the index loop?
  484. if( config.loop ) {
  485. index %= slidesLength;
  486. if( index < 0 ) {
  487. index = slidesLength + index;
  488. }
  489. }
  490. // Enforce max and minimum index bounds
  491. index = Math.max( Math.min( index, slidesLength - 1 ), 0 );
  492. for( var i = 0; i < slidesLength; i++ ) {
  493. var slide = slides[i];
  494. // Optimization; hide all slides that are three or more steps
  495. // away from the present slide
  496. if( overviewIsActive() === false ) {
  497. // The distance loops so that it measures 1 between the first
  498. // and last slides
  499. var distance = Math.abs( ( index - i ) % ( slidesLength - 3 ) ) || 0;
  500. slide.style.display = distance > 3 ? 'none' : 'block';
  501. }
  502. slides[i].classList.remove( 'past' );
  503. slides[i].classList.remove( 'present' );
  504. slides[i].classList.remove( 'future' );
  505. if( i < index ) {
  506. // Any element previous to index is given the 'past' class
  507. slides[i].classList.add( 'past' );
  508. }
  509. else if( i > index ) {
  510. // Any element subsequent to index is given the 'future' class
  511. slides[i].classList.add( 'future' );
  512. }
  513. // If this element contains vertical slides
  514. if( slide.querySelector( 'section' ) ) {
  515. slides[i].classList.add( 'stack' );
  516. }
  517. }
  518. // Mark the current slide as present
  519. slides[index].classList.add( 'present' );
  520. // If this slide has a state associated with it, add it
  521. // onto the current state of the deck
  522. var slideState = slides[index].getAttribute( 'data-state' );
  523. if( slideState ) {
  524. state = state.concat( slideState.split( ' ' ) );
  525. }
  526. }
  527. else {
  528. // Since there are no slides we can't be anywhere beyond the
  529. // zeroth index
  530. index = 0;
  531. }
  532. return index;
  533. }
  534. /**
  535. * Updates the visual slides to represent the currently
  536. * set indices.
  537. */
  538. function slide( h, v, origin ) {
  539. // Remember where we were at before
  540. previousSlide = currentSlide;
  541. // Remember the state before this slide
  542. var stateBefore = state.concat();
  543. // Reset the state array
  544. state.length = 0;
  545. var indexhBefore = indexh,
  546. indexvBefore = indexv;
  547. // Activate and transition to the new slide
  548. indexh = updateSlides( HORIZONTAL_SLIDES_SELECTOR, h === undefined ? indexh : h );
  549. indexv = updateSlides( VERTICAL_SLIDES_SELECTOR, v === undefined ? indexv : v );
  550. // Apply the new state
  551. stateLoop: for( var i = 0, len = state.length; i < len; i++ ) {
  552. // Check if this state existed on the previous slide. If it
  553. // did, we will avoid adding it repeatedly.
  554. for( var j = 0; j < stateBefore.length; j++ ) {
  555. if( stateBefore[j] === state[i] ) {
  556. stateBefore.splice( j, 1 );
  557. continue stateLoop;
  558. }
  559. }
  560. document.documentElement.classList.add( state[i] );
  561. // Dispatch custom event matching the state's name
  562. dispatchEvent( state[i] );
  563. }
  564. // Clean up the remaints of the previous state
  565. while( stateBefore.length ) {
  566. document.documentElement.classList.remove( stateBefore.pop() );
  567. }
  568. // Update progress if enabled
  569. if( config.progress && dom.progress ) {
  570. dom.progressbar.style.width = ( indexh / ( document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ).length - 1 ) ) * window.innerWidth + 'px';
  571. }
  572. // Close the overview if it's active
  573. if( overviewIsActive() ) {
  574. activateOverview();
  575. }
  576. updateControls();
  577. clearTimeout( writeURLTimeout );
  578. writeURLTimeout = setTimeout( writeURL, 1500 );
  579. // Query all horizontal slides in the deck
  580. var horizontalSlides = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR );
  581. // Find the current horizontal slide and any possible vertical slides
  582. // within it
  583. var currentHorizontalSlide = horizontalSlides[ indexh ],
  584. currentVerticalSlides = currentHorizontalSlide.querySelectorAll( 'section' );
  585. // Store references to the previous and current slides
  586. currentSlide = currentVerticalSlides[ indexv ] || currentHorizontalSlide;
  587. // Dispatch an event if the slide changed
  588. if( indexh !== indexhBefore || indexv !== indexvBefore ) {
  589. dispatchEvent( 'slidechanged', {
  590. 'origin': origin,
  591. 'indexh': indexh,
  592. 'indexv': indexv,
  593. 'previousSlide': previousSlide,
  594. 'currentSlide': currentSlide
  595. } );
  596. }
  597. else {
  598. // Ensure that the previous slide is never the same as the current
  599. previousSlide = null;
  600. }
  601. // Solves an edge case where the previous slide maintains the
  602. // 'present' class when navigating between adjacent vertical
  603. // stacks
  604. if( previousSlide ) {
  605. previousSlide.classList.remove( 'present' );
  606. }
  607. }
  608. /**
  609. * Updates the state and link pointers of the controls.
  610. */
  611. function updateControls() {
  612. if ( !config.controls || !dom.controls ) {
  613. return;
  614. }
  615. var routes = availableRoutes();
  616. // Remove the 'enabled' class from all directions
  617. [ dom.controlsLeft, dom.controlsRight, dom.controlsUp, dom.controlsDown ].forEach( function( node ) {
  618. node.classList.remove( 'enabled' );
  619. } )
  620. if( routes.left ) dom.controlsLeft.classList.add( 'enabled' );
  621. if( routes.right ) dom.controlsRight.classList.add( 'enabled' );
  622. if( routes.up ) dom.controlsUp.classList.add( 'enabled' );
  623. if( routes.down ) dom.controlsDown.classList.add( 'enabled' );
  624. }
  625. /**
  626. * Determine what available routes there are for navigation.
  627. *
  628. * @return {Object} containing four booleans: left/right/up/down
  629. */
  630. function availableRoutes() {
  631. var horizontalSlides = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR );
  632. var verticalSlides = document.querySelectorAll( VERTICAL_SLIDES_SELECTOR );
  633. return {
  634. left: indexh > 0,
  635. right: indexh < horizontalSlides.length - 1,
  636. up: indexv > 0,
  637. down: indexv < verticalSlides.length - 1
  638. };
  639. }
  640. /**
  641. * Reads the current URL (hash) and navigates accordingly.
  642. */
  643. function readURL() {
  644. // Break the hash down to separate components
  645. var bits = window.location.hash.slice(2).split('/');
  646. // Read the index components of the hash
  647. var h = parseInt( bits[0] ) || 0 ;
  648. var v = parseInt( bits[1] ) || 0 ;
  649. navigateTo( h, v );
  650. }
  651. /**
  652. * Updates the page URL (hash) to reflect the current
  653. * state.
  654. */
  655. function writeURL() {
  656. if( config.history ) {
  657. var url = '/';
  658. // Only include the minimum possible number of components in
  659. // the URL
  660. if( indexh > 0 || indexv > 0 ) url += indexh;
  661. if( indexv > 0 ) url += '/' + indexv;
  662. window.location.hash = url;
  663. }
  664. }
  665. /**
  666. * Dispatches an event of the specified type from the
  667. * reveal DOM element.
  668. */
  669. function dispatchEvent( type, properties ) {
  670. var event = document.createEvent( "HTMLEvents", 1, 2 );
  671. event.initEvent( type, true, true );
  672. extend( event, properties );
  673. dom.wrapper.dispatchEvent( event );
  674. }
  675. /**
  676. * Navigate to the next slide fragment.
  677. *
  678. * @return {Boolean} true if there was a next fragment,
  679. * false otherwise
  680. */
  681. function nextFragment() {
  682. // Vertical slides:
  683. if( document.querySelector( VERTICAL_SLIDES_SELECTOR + '.present' ) ) {
  684. var verticalFragments = document.querySelectorAll( VERTICAL_SLIDES_SELECTOR + '.present .fragment:not(.visible)' );
  685. if( verticalFragments.length ) {
  686. verticalFragments[0].classList.add( 'visible' );
  687. // Notify subscribers of the change
  688. dispatchEvent( 'fragmentshown', { fragment: verticalFragments[0] } );
  689. return true;
  690. }
  691. }
  692. // Horizontal slides:
  693. else {
  694. var horizontalFragments = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR + '.present .fragment:not(.visible)' );
  695. if( horizontalFragments.length ) {
  696. horizontalFragments[0].classList.add( 'visible' );
  697. // Notify subscribers of the change
  698. dispatchEvent( 'fragmentshown', { fragment: horizontalFragments[0] } );
  699. return true;
  700. }
  701. }
  702. return false;
  703. }
  704. /**
  705. * Navigate to the previous slide fragment.
  706. *
  707. * @return {Boolean} true if there was a previous fragment,
  708. * false otherwise
  709. */
  710. function previousFragment() {
  711. // Vertical slides:
  712. if( document.querySelector( VERTICAL_SLIDES_SELECTOR + '.present' ) ) {
  713. var verticalFragments = document.querySelectorAll( VERTICAL_SLIDES_SELECTOR + '.present .fragment.visible' );
  714. if( verticalFragments.length ) {
  715. verticalFragments[ verticalFragments.length - 1 ].classList.remove( 'visible' );
  716. // Notify subscribers of the change
  717. dispatchEvent( 'fragmenthidden', { fragment: verticalFragments[ verticalFragments.length - 1 ] } );
  718. return true;
  719. }
  720. }
  721. // Horizontal slides:
  722. else {
  723. var horizontalFragments = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR + '.present .fragment.visible' );
  724. if( horizontalFragments.length ) {
  725. horizontalFragments[ horizontalFragments.length - 1 ].classList.remove( 'visible' );
  726. // Notify subscribers of the change
  727. dispatchEvent( 'fragmenthidden', { fragment: horizontalFragments[ horizontalFragments.length - 1 ] } );
  728. return true;
  729. }
  730. }
  731. return false;
  732. }
  733. function cueAutoSlide() {
  734. clearTimeout( autoSlideTimeout );
  735. // Cue the next auto-slide if enabled
  736. if( config.autoSlide ) {
  737. autoSlideTimeout = setTimeout( navigateNext, config.autoSlide );
  738. }
  739. }
  740. /**
  741. * Triggers a navigation to the specified indices.
  742. *
  743. * @param {Number} h The horizontal index of the slide to show
  744. * @param {Number} v The vertical index of the slide to show
  745. */
  746. function navigateTo( h, v, origin ) {
  747. slide( h, v, origin );
  748. }
  749. function navigateLeft() {
  750. // Prioritize hiding fragments
  751. if( overviewIsActive() || previousFragment() === false ) {
  752. slide( indexh - 1, 0 );
  753. }
  754. }
  755. function navigateRight() {
  756. // Prioritize revealing fragments
  757. if( overviewIsActive() || nextFragment() === false ) {
  758. slide( indexh + 1, 0 );
  759. }
  760. }
  761. function navigateUp() {
  762. // Prioritize hiding fragments
  763. if( overviewIsActive() || previousFragment() === false ) {
  764. slide( indexh, indexv - 1 );
  765. }
  766. }
  767. function navigateDown() {
  768. // Prioritize revealing fragments
  769. if( overviewIsActive() || nextFragment() === false ) {
  770. slide( indexh, indexv + 1 );
  771. }
  772. }
  773. /**
  774. * Navigates backwards, prioritized in the following order:
  775. * 1) Previous fragment
  776. * 2) Previous vertical slide
  777. * 3) Previous horizontal slide
  778. */
  779. function navigatePrev() {
  780. // Prioritize revealing fragments
  781. if( previousFragment() === false ) {
  782. if( availableRoutes().up ) {
  783. navigateUp();
  784. }
  785. else {
  786. // Fetch the previous horizontal slide, if there is one
  787. var previousSlide = document.querySelector( '.reveal .slides>section.past:nth-child(' + indexh + ')' );
  788. if( previousSlide ) {
  789. indexv = ( previousSlide.querySelectorAll('section').length + 1 ) || 0;
  790. indexh --;
  791. slide();
  792. }
  793. }
  794. }
  795. }
  796. /**
  797. * Same as #navigatePrev() but navigates forwards.
  798. */
  799. function navigateNext() {
  800. // Prioritize revealing fragments
  801. if( nextFragment() === false ) {
  802. availableRoutes().down ? navigateDown() : navigateRight();
  803. }
  804. // If auto-sliding is enabled we need to cue up
  805. // another timeout
  806. cueAutoSlide();
  807. }
  808. /**
  809. * Toggles the slide overview mode on and off.
  810. */
  811. function toggleOverview() {
  812. if( overviewIsActive() ) {
  813. deactivateOverview();
  814. }
  815. else {
  816. activateOverview();
  817. }
  818. }
  819. // Expose some methods publicly
  820. return {
  821. initialize: initialize,
  822. navigateTo: navigateTo,
  823. navigateLeft: navigateLeft,
  824. navigateRight: navigateRight,
  825. navigateUp: navigateUp,
  826. navigateDown: navigateDown,
  827. navigatePrev: navigatePrev,
  828. navigateNext: navigateNext,
  829. toggleOverview: toggleOverview,
  830. // Adds or removes all internal event listeners (such as keyboard)
  831. addEventListeners: addEventListeners,
  832. removeEventListeners: removeEventListeners,
  833. // Returns the indices of the current slide
  834. getIndices: function() {
  835. return {
  836. h: indexh,
  837. v: indexv
  838. };
  839. },
  840. // Returns the previous slide element, may be null
  841. getPreviousSlide: function() {
  842. return previousSlide
  843. },
  844. // Returns the current slide element
  845. getCurrentSlide: function() {
  846. return currentSlide
  847. },
  848. // Helper method, retrieves query string as a key/value hash
  849. getQueryHash: function() {
  850. var query = {};
  851. location.search.replace( /[A-Z0-9]+?=(\w*)/gi, function(a) {
  852. query[ a.split( '=' ).shift() ] = a.split( '=' ).pop();
  853. } );
  854. return query;
  855. },
  856. // Forward event binding to the reveal DOM element
  857. addEventListener: function( type, listener, useCapture ) {
  858. if( 'addEventListener' in window ) {
  859. ( dom.wrapper || document.querySelector( '.reveal' ) ).addEventListener( type, listener, useCapture );
  860. }
  861. },
  862. removeEventListener: function( type, listener, useCapture ) {
  863. if( 'addEventListener' in window ) {
  864. ( dom.wrapper || document.querySelector( '.reveal' ) ).removeEventListener( type, listener, useCapture );
  865. }
  866. }
  867. };
  868. })();