reveal.js 28 KB

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