reveal.js 22 KB

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