reveal.js 28 KB

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