reveal.js 27 KB

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